add date sorting
[phinde.git] / www / index.php
index 96d6f7a28bfb3ca511f5cf78b82a820d3b15f298..54d5da80f4ba2afcdb455a4294854acd6b77d9a6 100644 (file)
@@ -1,17 +1,10 @@
 <?php
 namespace phinde;
 // web interface to search
-set_include_path(__DIR__ . '/../src/' . PATH_SEPARATOR . get_include_path());
-require_once __DIR__ . '/../data/config.php';
-require_once 'HTTP/Request2.php';
-require_once 'Pager.php';
-require_once 'Html/Pager.php';
-require_once 'Elasticsearch.php';
-require_once 'Elasticsearch/Request.php';
-require_once 'functions.php';
+require 'www-header.php';
 
 if (!isset($_GET['q'])) {
-    exit('no query');
+    $_GET['q'] = '';
 }
 
 $query = $_GET['q'];
@@ -23,43 +16,144 @@ if (isset($_GET['page'])) {
     //PEAR Pager begins at 1
     $page = (int)$_GET['page'] - 1;
 }
+
 $perPage = 10;//$GLOBALS['phinde']['perPage'];
+$site = null;
+$siteParam = false;
+$baseLink = '?q=' . urlencode($query);
+
+if (preg_match('#site:([^ ]*)#', $query, $matches)) {
+    $site = $matches[1];
+    $cleanQuery = trim(str_replace('site:' . $site, '', $query));
+    $site = Helper::noSchema($site);
+} else if (isset($_GET['site']) && trim(isset($_GET['site'])) != '') {
+    $site = trim($_GET['site']);
+    $siteParam = true;
+    $cleanQuery = $query;
+    $baseLink .= '&site=' . urlencode($site);
+} else {
+    $cleanQuery = $query;
+}
+
+if (isset($_GET['sort']) && $_GET['sort'] == 'date') {
+    $sort = 'date';
+    $baseLink .= '&sort=date';
+} else {
+    $sort = '';
+}
+
+$filters = array();
+if (isset($_GET['filter'])) {
+    $allowedFilter = array('domain', 'language', 'tags', 'term');
+    foreach ($_GET['filter'] as $type => $value) {
+        if (in_array($type, $allowedFilter)) {
+            $filters[$type] = filter_var($value, FILTER_SANITIZE_STRING);
+        }
+    }
+}
+$activeFilters = array();
+foreach ($filters as $type => $value) {
+    $activeFilters[$type] = array(
+        'label' => $value,
+        'removeUrl' => buildLink($baseLink, $filters, $type, null),
+    );
+}
+
+function buildLink($baseLink, $filters, $addFilterType, $addFilterValue)
+{
+    if ($addFilterValue === null) {
+        if (array_key_exists($addFilterType, $filters)) {
+            unset($filters[$addFilterType]);
+        }
+    } else {
+        $filters[$addFilterType] = $addFilterValue;
+    }
+
+    $params = http_build_query(array('filter' => $filters));
+    if (strlen($params)) {
+        return $baseLink . '&' . $params;
+    }
+    return $baseLink;
+}
+
+if (preg_match('#site:([^ ]*)#', $query, $matches)) {
+    $site = $matches[1];
+    $cleanQuery = trim(str_replace('site:' . $site, '', $query));
+    $site = Helper::noSchema($site);
+    $urlNoSite = buildLink('?q=' . urlencode($cleanQuery), $filters, null, null);
+} else {
+    $cleanQuery = $query;
+    $urlNoSite = null;
+}
 
+$timeBegin = microtime(true);
 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
-$res = $es->search($query, $page, $perPage);
+$res = $es->search($cleanQuery, $filters, $site, $page, $perPage, $sort);
+$timeEnd = microtime(true);
 
 $pager = new Html_Pager(
     $res->hits->total, $perPage, $page + 1,
-    '?q=' . $query
+    $baseLink
 );
 
-foreach ($res->hits->hits as $hit) {
+foreach ($res->hits->hits as &$hit) {
     $doc = $hit->_source;
-    if ($doc->title == '') {
+    if (!isset($doc->title) || $doc->title == '') {
         $doc->title = '(no title)';
+        $doc->htmlTitle = '(no title)';
+    }
+    if (isset($hit->highlight->title[0])) {
+        $doc->htmlTitle = $hit->highlight->title[0];
+    } else {
+        $doc->htmlTitle = htmlspecialchars($doc->title);
     }
-    echo '<p>'
-        . '<a href="' . htmlspecialchars($doc->url) . '">'
-        . htmlspecialchars($doc->title)
-        . '</a>';
-    if (isset($doc->author->name)) {
-        echo ' by <a href="' . htmlspecialchars($doc->author->url) . '">'
-            . htmlspecialchars($doc->author->name)
-            . '</a>';
+    if (isset($hit->highlight->text[0])) {
+        $doc->htmlText = $hit->highlight->text[0];
+    } else {
+        $doc->htmlText = null;
     }
-    echo  '<br/><tt>'
-        . htmlspecialchars(preg_replace('#^.*://#', '', $doc->url))
-        . '</tt>';
+
+    $doc->extra = new \stdClass();
+    $doc->extra->cleanUrl = preg_replace('#^.*://#', '', $doc->url);
     if (isset($doc->modate)) {
-        echo '<br/>Changed: ' . substr($doc->modate, 0, 10);
+        $doc->extra->day = substr($doc->modate, 0, 10);
     }
-    echo '</p>';
 }
 
-$links = $pager->getLinks();
-echo $links['back']
-    . ' ' . implode(' ', $links['pages'])
-    . ' ' . $links['next'];
-//var_dump($links);
-var_dump($res->aggregations->domain);
+foreach ($res->aggregations as $key => &$aggregation) {
+    foreach ($aggregation->buckets as &$bucket) {
+        $bucket->url = buildLink($baseLink, $filters, $key, $bucket->key);
+    }
+}
+
+if ($site !== null) {
+    $urlNoSite = buildLink('?q=' . urlencode($cleanQuery), $filters, null, null);
+} else {
+    $urlNoSite = null;
+}
+
+$urlSortRelevance = buildLink(
+    str_replace('&sort=date', '', $baseLink), $filters, null, null
+);
+$urlSortDate = $urlSortRelevance . '&sort=date';
+
+render(
+    'search',
+    array(
+        'queryTime' => round($timeEnd - $timeBegin, 2) . 's',
+        'query' => $query,
+        'cleanQuery' => $cleanQuery,
+        'urlNoSite' => $urlNoSite,
+        'site' => $site,
+        'siteParam' => $siteParam,
+        'hitcount' => $res->hits->total,
+        'hits' => $res->hits->hits,
+        'aggregations' => $res->aggregations,
+        'activeFilters' => $activeFilters,
+        'pager' => $pager,
+        'sort' => $sort,
+        'urlSortRelevance' => $urlSortRelevance,
+        'urlSortDate' => $urlSortDate,
+    )
+);
 ?>