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'];
21 $baseLink = '?q=' . urlencode($query);
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);
32 $activeFilters = array();
33 foreach ($filters as $type => $value) {
34 $activeFilters[$type] = array(
36 'removeUrl' => buildLink($baseLink, $filters, $type, null),
40 function buildLink($baseLink, $filters, $addFilterType, $addFilterValue)
42 if ($addFilterValue === null) {
43 if (array_key_exists($addFilterType, $filters)) {
44 unset($filters[$addFilterType]);
47 $filters[$addFilterType] = $addFilterValue;
50 $params = http_build_query(array('filter' => $filters));
51 if (strlen($params)) {
52 return $baseLink . '&' . $params;
58 if (preg_match('#site:([^ ]*)#', $query, $matches)) {
60 $cleanQuery = trim(str_replace('site:' . $site, '', $query));
61 $site = Helper::noSchema($site);
66 $timeBegin = microtime(true);
67 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
68 $res = $es->search($cleanQuery, $filters, $site, $page, $perPage);
69 $timeEnd = microtime(true);
71 $pager = new Html_Pager(
72 $res->hits->total, $perPage, $page + 1,
76 foreach ($res->hits->hits as &$hit) {
78 if ($doc->title == '') {
79 $doc->htmlTitle = '(no title)';
81 if (isset($hit->highlight->title[0])) {
82 $doc->htmlTitle = $hit->highlight->title[0];
84 $doc->htmlTitle = htmlspecialchars($doc->title);
86 if (isset($hit->highlight->text[0])) {
87 $doc->htmlText = $hit->highlight->text[0];
89 $doc->htmlText = null;
92 $doc->extra = new \stdClass();
93 $doc->extra->cleanUrl = preg_replace('#^.*://#', '', $doc->url);
94 if (isset($doc->modate)) {
95 $doc->extra->day = substr($doc->modate, 0, 10);
99 foreach ($res->aggregations as $key => &$aggregation) {
100 foreach ($aggregation->buckets as &$bucket) {
101 $bucket->url = buildLink($baseLink, $filters, $key, $bucket->key);
108 'queryTime' => round($timeEnd - $timeBegin, 2) . 'ms',
110 'cleanQuery' => $cleanQuery,
112 'hitcount' => $res->hits->total,
113 'hits' => $res->hits->hits,
114 'aggregations' => $res->aggregations,
115 'activeFilters' => $activeFilters,