7c4904b4d45a6cac241fd0564d5535df8c213a85
[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     $_GET['q'] = '';
8 }
9 $query = trim($_GET['q']);
10
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     if ($page >= 100) {
20         //Elasticsearch by default only allows 100 pages
21         // we do not want exceptions in our logs
22         throw new Exception_Input('List page is too large');
23     }
24 }
25
26 $perPage = 10;//$GLOBALS['phinde']['perPage'];
27 $site = null;
28 $siteParam = false;
29 $baseLink = '?q=' . urlencode($query);
30
31 if (preg_match('#site:([^ ]*)#', $query, $matches)) {
32     $site = $matches[1];
33     $cleanQuery = trim(str_replace('site:' . $site, '', $query));
34     $site = Helper::noSchema($site);
35 } else if (isset($_GET['site']) && trim(isset($_GET['site'])) != '') {
36     $site = trim($_GET['site']);
37     $siteParam = true;
38     $cleanQuery = $query;
39     $baseLink .= '&site=' . urlencode($site);
40 } else {
41     $cleanQuery = $query;
42 }
43
44 if (isset($_GET['sort'])
45     && ($_GET['sort'] === 'date' || $_GET['sort'] === 'score')
46 ) {
47     $sortMode = $_GET['sort'];
48 } else {
49     $sortMode = $GLOBALS['phinde']['defaultSort'];
50 }
51 $sort = $sortMode;
52 if ($sortMode !== $GLOBALS['phinde']['defaultSort']) {
53     $baseLink .= '&sort=' . $sortMode;
54 }
55
56 $filters = array();
57 if (isset($_GET['filter'])) {
58     $allowedFilter = array('domain', 'language', 'tags', 'term');
59     foreach ($_GET['filter'] as $type => $value) {
60         if (in_array($type, $allowedFilter)) {
61             $filters[$type] = filter_var($value, FILTER_SANITIZE_STRING);
62         }
63     }
64 }
65 $activeFilters = array();
66 foreach ($filters as $type => $value) {
67     $activeFilters[$type] = array(
68         'label' => $value,
69         'removeUrl' => buildLink($baseLink, $filters, $type, null),
70     );
71 }
72
73 function buildLink($baseLink, $filters, $addFilterType, $addFilterValue)
74 {
75     if ($addFilterValue === null) {
76         if (array_key_exists($addFilterType, $filters)) {
77             unset($filters[$addFilterType]);
78         }
79     } else {
80         $filters[$addFilterType] = $addFilterValue;
81     }
82
83     $params = http_build_query(array('filter' => $filters));
84     if (strlen($params)) {
85         return $baseLink . '&' . $params;
86     }
87     return $baseLink;
88 }
89
90 if (preg_match('#site:([^ ]*)#', $query, $matches)) {
91     $site = $matches[1];
92     $cleanQuery = trim(str_replace('site:' . $site, '', $query));
93     $site = Helper::noSchema($site);
94     $urlNoSite = buildLink('?q=' . urlencode($cleanQuery), $filters, null, null);
95 } else {
96     $cleanQuery = $query;
97     $urlNoSite = null;
98 }
99
100 $timeBegin = microtime(true);
101 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
102 $res = $es->search($cleanQuery, $filters, $site, $page, $perPage, $sort);
103 $timeEnd = microtime(true);
104
105 $pager = new Html_Pager(
106     $res->hits->total, $perPage, $page + 1,
107     buildLink($baseLink, $filters, null, null)
108 );
109
110 foreach ($res->hits->hits as &$hit) {
111     $doc = $hit->_source;
112     if (!isset($doc->title) || $doc->title == '') {
113         $doc->title = '(no title)';
114         $doc->htmlTitle = '(no title)';
115     }
116     if (isset($hit->highlight->title[0])) {
117         $doc->htmlTitle = $hit->highlight->title[0];
118     } else {
119         $doc->htmlTitle = htmlspecialchars($doc->title);
120     }
121     if (isset($hit->highlight->text[0])) {
122         $doc->htmlText = $hit->highlight->text[0];
123     } else {
124         $doc->htmlText = null;
125     }
126
127     $doc->extra = new \stdClass();
128     $doc->extra->cleanUrl = preg_replace('#^.*://#', '', $doc->url);
129     if (isset($doc->status->modate)) {
130         $doc->extra->day = substr($doc->status->modate, 0, 10);
131     }
132 }
133
134 foreach ($res->aggregations as $key => &$aggregation) {
135     foreach ($aggregation->buckets as &$bucket) {
136         $bucket->url = buildLink($baseLink, $filters, $key, $bucket->key);
137     }
138 }
139
140 if ($site !== null) {
141     $urlNoSite = buildLink('?q=' . urlencode($cleanQuery), $filters, null, null);
142 } else {
143     $urlNoSite = null;
144 }
145
146 $urlSortBase = buildLink(
147     preg_replace('#&sort=[^&]+#', '', $baseLink), $filters, null, null
148 );
149 $urlSorts = [];
150 foreach (['date', 'score'] as $sortMode) {
151     if ($sortMode === $GLOBALS['phinde']['defaultSort']) {
152         $urlSorts[$sortMode] = $urlSortBase;
153     } else {
154         $urlSorts[$sortMode] = $urlSortBase . '&sort=' . $sortMode;
155     }
156 }
157
158 if (isset($_GET['format']) && $_GET['format'] == 'opensearch') {
159     $template = 'opensearch';
160     $baseLink .= '&format=opensearch';
161     header('Content-type: application/atom+xml');
162 } else {
163     $template = 'search';
164 }
165
166 render(
167     $template,
168     array(
169         'queryTime' => round($timeEnd - $timeBegin, 2) . 's',
170         'query' => $query,
171         'fullUrl' => Helper::fullUrl($baseLink),
172         'cleanQuery' => $cleanQuery,
173         'urlNoSite' => $urlNoSite,
174         'site' => $site,
175         'siteParam' => $siteParam,
176         'hitcount' => $res->hits->total,
177         'hits' => $res->hits->hits,
178         'aggregations' => $res->aggregations,
179         'activeFilters' => $activeFilters,
180         'pager' => $pager,
181         'sort' => $sort,
182         'urlSorts' => $urlSorts,
183         'hitTemplate' => 'search/' . $GLOBALS['phinde']['hitTemplate'],
184         'sidebarinclude' => $GLOBALS['phinde']['sidebarinclude'],
185     )
186 );
187 ?>