first frontend
[phinde.git] / www / index.php
index 96d6f7a28bfb3ca511f5cf78b82a820d3b15f298..8cdca41b9b4928f4665aae7142869487bbd84a3e 100644 (file)
@@ -1,14 +1,7 @@
 <?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');
@@ -25,41 +18,53 @@ if (isset($_GET['page'])) {
 }
 $perPage = 10;//$GLOBALS['phinde']['perPage'];
 
+$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);
+        }
+    }
+}
+
 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
-$res = $es->search($query, $page, $perPage);
+$res = $es->search($query, $filters, $page, $perPage);
 
 $pager = new Html_Pager(
     $res->hits->total, $perPage, $page + 1,
     '?q=' . $query
 );
 
-foreach ($res->hits->hits as $hit) {
+foreach ($res->hits->hits as &$hit) {
     $doc = $hit->_source;
     if ($doc->title == '') {
         $doc->title = '(no 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>';
-    }
-    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);
+$baseLink = '?q=' . urlencode($query);
+foreach ($res->aggregations as $key => &$aggregation) {
+    foreach ($aggregation->buckets as &$bucket) {
+        $bucket->url = $baseLink
+            . '&filter[' . urlencode($key) . ']=' . urlencode($bucket->key);
+    }
+}
+//var_dump($res->aggregations);
+
+render(
+    'search',
+    array(
+        'query' => $query,
+        'hitcount' => $res->hits->total,
+        'hits' => $res->hits->hits,
+        'aggregations' => $res->aggregations,
+        'pager' => $pager
+    )
+);
 ?>