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