remove multiple tags
[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     $urlNoSite = buildLink('?q=' . urlencode($cleanQuery), $filters, null, null);
63 } else {
64     $cleanQuery = $query;
65     $urlNoSite = null;
66 }
67
68 $timeBegin = microtime(true);
69 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
70 $res = $es->search($cleanQuery, $filters, $site, $page, $perPage);
71 $timeEnd = microtime(true);
72
73 $pager = new Html_Pager(
74     $res->hits->total, $perPage, $page + 1,
75     $baseLink
76 );
77
78 foreach ($res->hits->hits as &$hit) {
79     $doc = $hit->_source;
80     if ($doc->title == '') {
81         $doc->htmlTitle = '(no title)';
82     }
83     if (isset($hit->highlight->title[0])) {
84         $doc->htmlTitle = $hit->highlight->title[0];
85     } else {
86         $doc->htmlTitle = htmlspecialchars($doc->title);
87     }
88     if (isset($hit->highlight->text[0])) {
89         $doc->htmlText = $hit->highlight->text[0];
90     } else {
91         $doc->htmlText = null;
92     }
93
94     $doc->extra = new \stdClass();
95     $doc->extra->cleanUrl = preg_replace('#^.*://#', '', $doc->url);
96     if (isset($doc->modate)) {
97         $doc->extra->day = substr($doc->modate, 0, 10);
98     }
99 }
100
101 foreach ($res->aggregations as $key => &$aggregation) {
102     foreach ($aggregation->buckets as &$bucket) {
103         $bucket->url = buildLink($baseLink, $filters, $key, $bucket->key);
104     }
105 }
106
107 render(
108     'search',
109     array(
110         'queryTime' => round($timeEnd - $timeBegin, 2) . 's',
111         'query' => $query,
112         'cleanQuery' => $cleanQuery,
113         'urlNoSite' => $urlNoSite,
114         'site' => $site,
115         'hitcount' => $res->hits->total,
116         'hits' => $res->hits->hits,
117         'aggregations' => $res->aggregations,
118         'activeFilters' => $activeFilters,
119         'pager' => $pager
120     )
121 );
122 ?>