Esempio n. 1
0
<?php

/*
 * Return a list of URLs matching a query, separated by line feeds.
 */
header('Content-Type: text/plain');
if (empty($_REQUEST['q'])) {
    echo '';
} else {
    $results = array();
    foreach (Typeframe::Registry()->pages() as $app) {
        if ($app->pageid()) {
            if (strpos($app->uri(), $_REQUEST['q']) !== false) {
                $results[] = $app->uri();
            }
        }
    }
    $metas = new Model_UrlMeta();
    $metas->where('CONCAT(?, page.uri, pathinfo) LIKE ?', TYPEF_WEB_DIR, "%{$_REQUEST['q']}%");
    foreach ($metas as $meta) {
        $results[] = $meta['fullpath'];
    }
    sort($results);
    $results = array_unique($results);
    echo implode("\n", $results);
}
exit;
Esempio n. 2
0
 /**
  * Get URL metadata for a URL.
  * @param string $url The URL. If null, use the current page.
  * @param boolean $create Create a new record if the URL is valid.
  * @param string $newLabel The label for the URL if it doesn't exist.
  * @return Dbi_Record The URL metadata record.
  */
 public static function GetUrl($url = null, $create = true, $newLabel = '')
 {
     if (is_null($url)) {
         //$url = Typeframe::CurrentPage()->uri();
         //$app = Typeframe::CurrentPage()->application();
         $url = Typeframe::CurrentPage()->uri();
         if (substr($url, 0, -1) == '/') {
             $url = substr($url, -1);
         }
         $response = Typeframe::CurrentPage();
     } else {
         $url = parse_url($url, PHP_URL_PATH);
         if (substr($url, 0, -1) == '/') {
             $url = substr($url, -1);
         }
         $url = preg_replace('/\\/+/', '/', $url);
         //$app = Typeframe::Registry()->applicationAt($url);
         $response = Typeframe::Registry()->responseAt($url);
     }
     $app = $response->application();
     // Return a blank (nonexistent) Dbi_Record for invalid URLs
     if (!$app) {
         return Model_UrlMeta::Create();
     }
     $model = new Model_UrlMeta();
     $model->where('pageid = ?', $response->pageid());
     if ($response->pageid()) {
         $pathinfo = substr($url, strlen($response->applicationUri()));
     } else {
         $pathinfo = substr($response->applicationUri(), strlen(TYPEF_WEB_DIR));
     }
     if ($pathinfo == '/') {
         $pathinfo = '';
     }
     $model->where('pathinfo = ?', $pathinfo);
     $urlmeta = $model->getFirst();
     if (!$urlmeta->exists() && $create) {
         // Record does not exist. Generate a label and save it.
         $urlmeta['pageid'] = $response->pageid();
         $urlmeta['pathinfo'] = $pathinfo;
         if (!$newLabel) {
             // We need to save the metadata before we request the URL by proxy. Otherwise it's hammers all the way down.
             $urlmeta->save();
             // Create a label for the URL metadata
             $html = Typeframe::GetByProxy($url);
             if (!$html) {
                 return Model_UrlMeta::Create();
             } else {
                 $xml = @Pagemill_SimpleXmlElement::LoadString($html);
                 if ($xml) {
                     $selector = URLMETA_LABEL_SELECTOR ? URLMETA_LABEL_SELECTOR : 'title';
                     $parts = explode(',', $selector);
                     foreach ($parts as $part) {
                         if (trim($part)) {
                             $elements = $xml->select(trim($part));
                             if ($elements) {
                                 $urlmeta['label'] = trim($elements[0]->innerXml());
                                 break;
                             }
                         }
                     }
                 }
             }
             if (!$urlmeta['label']) {
                 $urlmeta['label'] = $app->title();
             }
         } else {
             $urlmeta['label'] = $newLabel;
         }
         $urlmeta->save();
     }
     return $urlmeta;
 }