rework crawler; add atom link extraction
[phinde.git] / www / index.php
index 8cdca41b9b4928f4665aae7142869487bbd84a3e..6f4ec8fd15f0286b535dd0ba9208060ceec307a5 100644 (file)
@@ -4,7 +4,7 @@ namespace phinde;
 require 'www-header.php';
 
 if (!isset($_GET['q'])) {
 require 'www-header.php';
 
 if (!isset($_GET['q'])) {
-    exit('no query');
+    $_GET['q'] = '';
 }
 
 $query = $_GET['q'];
 }
 
 $query = $_GET['q'];
@@ -17,6 +17,22 @@ if (isset($_GET['page'])) {
     $page = (int)$_GET['page'] - 1;
 }
 $perPage = 10;//$GLOBALS['phinde']['perPage'];
     $page = (int)$_GET['page'] - 1;
 }
 $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;
+}
 
 $filters = array();
 if (isset($_GET['filter'])) {
 
 $filters = array();
 if (isset($_GET['filter'])) {
@@ -27,20 +43,68 @@ if (isset($_GET['filter'])) {
         }
     }
 }
         }
     }
 }
+$activeFilters = array();
+foreach ($filters as $type => $value) {
+    $activeFilters[$type] = array(
+        'label' => $value,
+        'removeUrl' => buildLink($baseLink, $filters, $type, null),
+    );
+}
+
+function buildLink($baseLink, $filters, $addFilterType, $addFilterValue)
+{
+    if ($addFilterValue === null) {
+        if (array_key_exists($addFilterType, $filters)) {
+            unset($filters[$addFilterType]);
+        }
+    } else {
+        $filters[$addFilterType] = $addFilterValue;
+    }
+
+    $params = http_build_query(array('filter' => $filters));
+    if (strlen($params)) {
+        return $baseLink . '&' . $params;
+    }
+    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']);
 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
-$res = $es->search($query, $filters, $page, $perPage);
+$res = $es->search($cleanQuery, $filters, $site, $page, $perPage);
+$timeEnd = microtime(true);
 
 $pager = new Html_Pager(
     $res->hits->total, $perPage, $page + 1,
 
 $pager = new Html_Pager(
     $res->hits->total, $perPage, $page + 1,
-    '?q=' . $query
+    $baseLink
 );
 
 foreach ($res->hits->hits as &$hit) {
     $doc = $hit->_source;
 );
 
 foreach ($res->hits->hits as &$hit) {
     $doc = $hit->_source;
-    if ($doc->title == '') {
+    if (!isset($doc->title) || $doc->title == '') {
         $doc->title = '(no 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)) {
     $doc->extra = new \stdClass();
     $doc->extra->cleanUrl = preg_replace('#^.*://#', '', $doc->url);
     if (isset($doc->modate)) {
@@ -48,22 +112,31 @@ foreach ($res->hits->hits as &$hit) {
     }
 }
 
     }
 }
 
-$baseLink = '?q=' . urlencode($query);
 foreach ($res->aggregations as $key => &$aggregation) {
     foreach ($aggregation->buckets as &$bucket) {
 foreach ($res->aggregations as $key => &$aggregation) {
     foreach ($aggregation->buckets as &$bucket) {
-        $bucket->url = $baseLink
-            . '&filter[' . urlencode($key) . ']=' . urlencode($bucket->key);
+        $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;
+}
 
 render(
     'search',
     array(
 
 render(
     'search',
     array(
+        'queryTime' => round($timeEnd - $timeBegin, 2) . 's',
         'query' => $query,
         'query' => $query,
+        'cleanQuery' => $cleanQuery,
+        'urlNoSite' => $urlNoSite,
+        'site' => $site,
+        'siteParam' => $siteParam,
         'hitcount' => $res->hits->total,
         'hits' => $res->hits->hits,
         'aggregations' => $res->aggregations,
         'hitcount' => $res->hits->total,
         'hits' => $res->hits->hits,
         'aggregations' => $res->aggregations,
+        'activeFilters' => $activeFilters,
         'pager' => $pager
     )
 );
         'pager' => $pager
     )
 );