X-Git-Url: https://git.cweiske.de/phinde.git/blobdiff_plain/e5c7780adb59e8038a040a1e534a4cee1fac8cd8..c49e7695ae4bc74019dfd614bfa3b5fc4e587154:/www/index.php?ds=sidebyside diff --git a/www/index.php b/www/index.php index 257ad87..5261156 100644 --- a/www/index.php +++ b/www/index.php @@ -4,10 +4,10 @@ namespace phinde; require 'www-header.php'; if (!isset($_GET['q'])) { - exit('no query'); + $_GET['q'] = ''; } +$query = trim($_GET['q']); -$query = $_GET['q']; $page = 0; if (isset($_GET['page'])) { if (!is_numeric($_GET['page'])) { @@ -16,10 +16,32 @@ if (isset($_GET['page'])) { //PEAR Pager begins at 1 $page = (int)$_GET['page'] - 1; } -$perPage = 10;//$GLOBALS['phinde']['perPage']; +$perPage = 10;//$GLOBALS['phinde']['perPage']; +$site = null; +$siteParam = false; $baseLink = '?q=' . urlencode($query); +if (preg_match('#site:([^ ]*)#', $query, $matches)) { + $site = $matches[1]; + $cleanQuery = trim(str_replace('site:' . $site, '', $query)); + $site = Helper::noSchema($site); +} else if (isset($_GET['site']) && trim(isset($_GET['site'])) != '') { + $site = trim($_GET['site']); + $siteParam = true; + $cleanQuery = $query; + $baseLink .= '&site=' . urlencode($site); +} else { + $cleanQuery = $query; +} + +if (isset($_GET['sort']) && $_GET['sort'] == 'date') { + $sort = 'date'; + $baseLink .= '&sort=date'; +} else { + $sort = ''; +} + $filters = array(); if (isset($_GET['filter'])) { $allowedFilter = array('domain', 'language', 'tags', 'term'); @@ -54,8 +76,20 @@ function buildLink($baseLink, $filters, $addFilterType, $addFilterValue) return $baseLink; } +if (preg_match('#site:([^ ]*)#', $query, $matches)) { + $site = $matches[1]; + $cleanQuery = trim(str_replace('site:' . $site, '', $query)); + $site = Helper::noSchema($site); + $urlNoSite = buildLink('?q=' . urlencode($cleanQuery), $filters, null, null); +} else { + $cleanQuery = $query; + $urlNoSite = null; +} + +$timeBegin = microtime(true); $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']); -$res = $es->search($query, $filters, $page, $perPage); +$res = $es->search($cleanQuery, $filters, $site, $page, $perPage, $sort); +$timeEnd = microtime(true); $pager = new Html_Pager( $res->hits->total, $perPage, $page + 1, @@ -64,9 +98,21 @@ $pager = new Html_Pager( foreach ($res->hits->hits as &$hit) { $doc = $hit->_source; - if ($doc->title == '') { + if (!isset($doc->title) || $doc->title == '') { $doc->title = '(no title)'; + $doc->htmlTitle = '(no title)'; + } + if (isset($hit->highlight->title[0])) { + $doc->htmlTitle = $hit->highlight->title[0]; + } else { + $doc->htmlTitle = htmlspecialchars($doc->title); } + if (isset($hit->highlight->text[0])) { + $doc->htmlText = $hit->highlight->text[0]; + } else { + $doc->htmlText = null; + } + $doc->extra = new \stdClass(); $doc->extra->cleanUrl = preg_replace('#^.*://#', '', $doc->url); if (isset($doc->modate)) { @@ -79,17 +125,45 @@ foreach ($res->aggregations as $key => &$aggregation) { $bucket->url = buildLink($baseLink, $filters, $key, $bucket->key); } } -//var_dump($res->aggregations); + +if ($site !== null) { + $urlNoSite = buildLink('?q=' . urlencode($cleanQuery), $filters, null, null); +} else { + $urlNoSite = null; +} + +$urlSortRelevance = buildLink( + str_replace('&sort=date', '', $baseLink), $filters, null, null +); +$urlSortDate = $urlSortRelevance . '&sort=date'; + +if (isset($_GET['format']) && $_GET['format'] == 'opensearch') { + $template = 'opensearch'; + $baseLink .= '&format=opensearch'; + header('Content-type: application/atom+xml'); +} else { + $template = 'search'; +} render( - 'search', + $template, array( + 'queryTime' => round($timeEnd - $timeBegin, 2) . 's', 'query' => $query, + 'fullUrl' => Helper::fullUrl($baseLink), + 'cleanQuery' => $cleanQuery, + 'urlNoSite' => $urlNoSite, + 'site' => $site, + 'siteParam' => $siteParam, 'hitcount' => $res->hits->total, 'hits' => $res->hits->hits, 'aggregations' => $res->aggregations, 'activeFilters' => $activeFilters, - 'pager' => $pager + 'pager' => $pager, + 'sort' => $sort, + 'urlSortRelevance' => $urlSortRelevance, + 'urlSortDate' => $urlSortDate, + 'hitTemplate' => 'search/' . $GLOBALS['phinde']['hitTemplate'], ) ); ?>