add site search, highlighting
[phinde.git] / www / index.php
1 <?php
2 namespace phinde;
3 // web interface to search
4 require 'www-header.php';
5
6 if (!isset($_GET['q'])) {
7     exit('no query');
8 }
9
10 $query = $_GET['q'];
11 $page = 0;
12 if (isset($_GET['page'])) {
13     if (!is_numeric($_GET['page'])) {
14         throw new Exception_Input('List page is not numeric');
15     }
16     //PEAR Pager begins at 1
17     $page = (int)$_GET['page'] - 1;
18 }
19 $perPage = 10;//$GLOBALS['phinde']['perPage'];
20
21 $baseLink = '?q=' . urlencode($query);
22
23 $filters = array();
24 if (isset($_GET['filter'])) {
25     $allowedFilter = array('domain', 'language', 'tags', 'term');
26     foreach ($_GET['filter'] as $type => $value) {
27         if (in_array($type, $allowedFilter)) {
28             $filters[$type] = filter_var($value, FILTER_SANITIZE_STRING);
29         }
30     }
31 }
32 $activeFilters = array();
33 foreach ($filters as $type => $value) {
34     $activeFilters[$type] = array(
35         'label' => $value,
36         'removeUrl' => buildLink($baseLink, $filters, $type, null),
37     );
38 }
39
40 function buildLink($baseLink, $filters, $addFilterType, $addFilterValue)
41 {
42     if ($addFilterValue === null) {
43         if (array_key_exists($addFilterType, $filters)) {
44             unset($filters[$addFilterType]);
45         }
46     } else {
47         $filters[$addFilterType] = $addFilterValue;
48     }
49
50     $params = http_build_query(array('filter' => $filters));
51     if (strlen($params)) {
52         return $baseLink . '&' . $params;
53     }
54     return $baseLink;
55 }
56
57 $site = null;
58 if (preg_match('#site:([^ ]*)#', $query, $matches)) {
59     $site = $matches[1];
60     $cleanQuery = trim(str_replace('site:' . $site, '', $query));
61     $site = Helper::noSchema($site);
62 } else {
63     $cleanQuery = $query;
64 }
65
66 $timeBegin = microtime(true);
67 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
68 $res = $es->search($cleanQuery, $filters, $site, $page, $perPage);
69 $timeEnd = microtime(true);
70
71 $pager = new Html_Pager(
72     $res->hits->total, $perPage, $page + 1,
73     $baseLink
74 );
75
76 foreach ($res->hits->hits as &$hit) {
77     $doc = $hit->_source;
78     if ($doc->title == '') {
79         $doc->htmlTitle = '(no title)';
80     }
81     if (isset($hit->highlight->title[0])) {
82         $doc->htmlTitle = $hit->highlight->title[0];
83     } else {
84         $doc->htmlTitle = htmlspecialchars($doc->title);
85     }
86     if (isset($hit->highlight->text[0])) {
87         $doc->htmlText = $hit->highlight->text[0];
88     } else {
89         $doc->htmlText = null;
90     }
91
92     $doc->extra = new \stdClass();
93     $doc->extra->cleanUrl = preg_replace('#^.*://#', '', $doc->url);
94     if (isset($doc->modate)) {
95         $doc->extra->day = substr($doc->modate, 0, 10);
96     }
97 }
98
99 foreach ($res->aggregations as $key => &$aggregation) {
100     foreach ($aggregation->buckets as &$bucket) {
101         $bucket->url = buildLink($baseLink, $filters, $key, $bucket->key);
102     }
103 }
104
105 render(
106     'search',
107     array(
108         'queryTime' => round($timeEnd - $timeBegin, 2) . 'ms',
109         'query' => $query,
110         'cleanQuery' => $cleanQuery,
111         'site' => $site,
112         'hitcount' => $res->hits->total,
113         'hits' => $res->hits->hits,
114         'aggregations' => $res->aggregations,
115         'activeFilters' => $activeFilters,
116         'pager' => $pager
117     )
118 );
119 ?>