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