3 // web interface to search
4 require 'www-header.php';
6 if (!isset($_GET['q'])) {
12 if (isset($_GET['page'])) {
13 if (!is_numeric($_GET['page'])) {
14 throw new Exception_Input('List page is not numeric');
16 //PEAR Pager begins at 1
17 $page = (int)$_GET['page'] - 1;
19 $perPage = 10;//$GLOBALS['phinde']['perPage'];
22 $baseLink = '?q=' . urlencode($query);
24 if (preg_match('#site:([^ ]*)#', $query, $matches)) {
26 $cleanQuery = trim(str_replace('site:' . $site, '', $query));
27 $site = Helper::noSchema($site);
28 } else if (isset($_GET['site']) && trim(isset($_GET['site'])) != '') {
29 $site = trim($_GET['site']);
32 $baseLink .= '&site=' . urlencode($site);
38 if (isset($_GET['filter'])) {
39 $allowedFilter = array('domain', 'language', 'tags', 'term');
40 foreach ($_GET['filter'] as $type => $value) {
41 if (in_array($type, $allowedFilter)) {
42 $filters[$type] = filter_var($value, FILTER_SANITIZE_STRING);
46 $activeFilters = array();
47 foreach ($filters as $type => $value) {
48 $activeFilters[$type] = array(
50 'removeUrl' => buildLink($baseLink, $filters, $type, null),
54 function buildLink($baseLink, $filters, $addFilterType, $addFilterValue)
56 if ($addFilterValue === null) {
57 if (array_key_exists($addFilterType, $filters)) {
58 unset($filters[$addFilterType]);
61 $filters[$addFilterType] = $addFilterValue;
64 $params = http_build_query(array('filter' => $filters));
65 if (strlen($params)) {
66 return $baseLink . '&' . $params;
71 if (preg_match('#site:([^ ]*)#', $query, $matches)) {
73 $cleanQuery = trim(str_replace('site:' . $site, '', $query));
74 $site = Helper::noSchema($site);
75 $urlNoSite = buildLink('?q=' . urlencode($cleanQuery), $filters, null, null);
81 $timeBegin = microtime(true);
82 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
83 $res = $es->search($cleanQuery, $filters, $site, $page, $perPage);
84 $timeEnd = microtime(true);
86 $pager = new Html_Pager(
87 $res->hits->total, $perPage, $page + 1,
91 foreach ($res->hits->hits as &$hit) {
93 if (!isset($doc->title) || $doc->title == '') {
94 $doc->title = '(no title)';
95 $doc->htmlTitle = '(no title)';
97 if (isset($hit->highlight->title[0])) {
98 $doc->htmlTitle = $hit->highlight->title[0];
100 $doc->htmlTitle = htmlspecialchars($doc->title);
102 if (isset($hit->highlight->text[0])) {
103 $doc->htmlText = $hit->highlight->text[0];
105 $doc->htmlText = null;
108 $doc->extra = new \stdClass();
109 $doc->extra->cleanUrl = preg_replace('#^.*://#', '', $doc->url);
110 if (isset($doc->modate)) {
111 $doc->extra->day = substr($doc->modate, 0, 10);
115 foreach ($res->aggregations as $key => &$aggregation) {
116 foreach ($aggregation->buckets as &$bucket) {
117 $bucket->url = buildLink($baseLink, $filters, $key, $bucket->key);
121 if ($site !== null) {
122 $urlNoSite = buildLink('?q=' . urlencode($cleanQuery), $filters, null, null);
130 'queryTime' => round($timeEnd - $timeBegin, 2) . 's',
132 'cleanQuery' => $cleanQuery,
133 'urlNoSite' => $urlNoSite,
135 'siteParam' => $siteParam,
136 'hitcount' => $res->hits->total,
137 'hits' => $res->hits->hits,
138 'aggregations' => $res->aggregations,
139 'activeFilters' => $activeFilters,