96d6f7a28bfb3ca511f5cf78b82a820d3b15f298
[phinde.git] / www / index.php
1 <?php
2 namespace phinde;
3 // web interface to search
4 set_include_path(__DIR__ . '/../src/' . PATH_SEPARATOR . get_include_path());
5 require_once __DIR__ . '/../data/config.php';
6 require_once 'HTTP/Request2.php';
7 require_once 'Pager.php';
8 require_once 'Html/Pager.php';
9 require_once 'Elasticsearch.php';
10 require_once 'Elasticsearch/Request.php';
11 require_once 'functions.php';
12
13 if (!isset($_GET['q'])) {
14     exit('no query');
15 }
16
17 $query = $_GET['q'];
18 $page = 0;
19 if (isset($_GET['page'])) {
20     if (!is_numeric($_GET['page'])) {
21         throw new Exception_Input('List page is not numeric');
22     }
23     //PEAR Pager begins at 1
24     $page = (int)$_GET['page'] - 1;
25 }
26 $perPage = 10;//$GLOBALS['phinde']['perPage'];
27
28 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
29 $res = $es->search($query, $page, $perPage);
30
31 $pager = new Html_Pager(
32     $res->hits->total, $perPage, $page + 1,
33     '?q=' . $query
34 );
35
36 foreach ($res->hits->hits as $hit) {
37     $doc = $hit->_source;
38     if ($doc->title == '') {
39         $doc->title = '(no title)';
40     }
41     echo '<p>'
42         . '<a href="' . htmlspecialchars($doc->url) . '">'
43         . htmlspecialchars($doc->title)
44         . '</a>';
45     if (isset($doc->author->name)) {
46         echo ' by <a href="' . htmlspecialchars($doc->author->url) . '">'
47             . htmlspecialchars($doc->author->name)
48             . '</a>';
49     }
50     echo  '<br/><tt>'
51         . htmlspecialchars(preg_replace('#^.*://#', '', $doc->url))
52         . '</tt>';
53     if (isset($doc->modate)) {
54         echo '<br/>Changed: ' . substr($doc->modate, 0, 10);
55     }
56     echo '</p>';
57 }
58
59 $links = $pager->getLinks();
60 echo $links['back']
61     . ' ' . implode(' ', $links['pages'])
62     . ' ' . $links['next'];
63 //var_dump($links);
64 var_dump($res->aggregations->domain);
65 ?>