searching works now (pager missing)
authorChristian Weiske <cweiske@cweiske.de>
Tue, 1 May 2012 18:40:24 +0000 (20:40 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 1 May 2012 18:40:24 +0000 (20:40 +0200)
13 files changed:
README.rst
data/config.default.php
data/config.php.dist
data/templates/base.htm
data/templates/list.htm
data/templates/repo-list.htm [new file with mode: 0644]
data/templates/search.htm [new file with mode: 0644]
scripts/search.php
src/phorkie/Database.php [new file with mode: 0644]
src/phorkie/Search/Elasticsearch.php [new file with mode: 0644]
src/phorkie/Search/Result.php [new file with mode: 0644]
www/.htaccess
www/search.php [new file with mode: 0644]

index aaa38cc..f5c93f2 100644 (file)
@@ -133,10 +133,10 @@ URLs
   Display raw file contents
 ``/[0-9]/delete``
   Delete the paste
-``/search(/.+)?``
-  Search for term
+``/search(/[0-9]+)?q=..``
+  Search for term, with optional page
 ``/list(/[0-9])?``
-  List all pastes
+  List all pastes, with optional page
 
 
 Internal directory layout
index 4ef5d05..c1446cc 100644 (file)
@@ -1,13 +1,14 @@
 <?php
 $GLOBALS['phorkie']['cfg'] = array(
-    'debug'      => false,
-    'gitdir'     => __DIR__ . '/../repos/git/',
-    'workdir'    => __DIR__ . '/../repos/work/',
-    'tpl'        => __DIR__ . '/templates/',
-    'css'        => 'http://twitter.github.com/bootstrap/assets/css/bootstrap.css',
-    'title'      => 'phorkie',
-    'topbar'     => '',
-    'setupcheck' => true,
+    'debug'         => false,
+    'gitdir'        => __DIR__ . '/../repos/git/',
+    'workdir'       => __DIR__ . '/../repos/work/',
+    'tpl'           => __DIR__ . '/templates/',
+    'css'           => 'http://twitter.github.com/bootstrap/assets/css/bootstrap.css',
+    'title'         => 'phorkie',
+    'topbar'        => '',
+    'setupcheck'    => true,
+    'elasticsearch' => null,
 );
 $GLOBALS['phorkie']['tools'] = array(
     '\\phorkie\\Tool_Xmllint' => true,
index 04b42a6..c9b891e 100644 (file)
@@ -3,5 +3,6 @@
 //$GLOBALS['phorkie']['cfg']['workdir'] = '/var/cache/git/paste/work/';
 //$GLOBALS['phorkie']['cfg']['git']['public'] = 'git://bogo/git/paste/';
 //$GLOBALS['phorkie']['cfg']['git']['private'] = 'ssh://git@bogo:paste/';
+//$GLOBALS['phorkie']['cfg']['elasticsearch'] = 'http://localhost:9020/phorkie/';
 //$GLOBALS['phorkie']['cfg']['setupcheck'] = false;
 ?>
index 867481c..906453e 100644 (file)
       <li>
        <a href="/list">List all</a>
       </li>
-      <!--
       <li>
        <form class="navbar-search pull-left" action="/search" method="get">
-        <input type="text" class="search-query" name="q" placeholder="Search"/>
+        <input type="text" class="search-query" name="q" placeholder="Search" value="{{query}}"/>
        </form>
       </li>
-      -->
      </ul>
     </div>
    </div>
index 4e2e4a6..b92e588 100644 (file)
@@ -4,12 +4,7 @@
 {% block content %}
 <ul class="nav nav-pills nav-stacked">
 {% for repo in repos %}
- <li>
-  <a href="{{repo.getLink('display')}}">
-   {{repo.id}}
-   {{repo.getDescription}}
-  </a>
- </li>
+ {% include 'repo-list.htm' %}
 {% endfor %}
 </ul>
 
diff --git a/data/templates/repo-list.htm b/data/templates/repo-list.htm
new file mode 100644 (file)
index 0000000..80c8f6c
--- /dev/null
@@ -0,0 +1,6 @@
+ <li>
+  <a href="{{repo.getLink('display')}}">
+   {{repo.id}}
+   {{repo.getDescription}}
+  </a>
+ </li>
diff --git a/data/templates/search.htm b/data/templates/search.htm
new file mode 100644 (file)
index 0000000..cb03cec
--- /dev/null
@@ -0,0 +1,20 @@
+{% extends "base.htm" %}
+{% block title %}Search results{% endblock %}
+
+{% block content %}
+{% if sres.results == 0 %}
+ <p>
+  Sorry, no results for "<tt>{{query}}</tt>".
+ </p>
+{% else %}
+ <p>
+  Found {{sres.results}} search results for "<tt>{{query}}</tt>":
+ </p>
+ <ul class="nav nav-pills nav-stacked">
+ {% for repo in sres.repos %}
+   {% include 'repo-list.htm' %}
+ {% endfor %}
+ </ul>
+{% endif %}
+
+{% endblock %}
index b6273b4..af7a085 100644 (file)
@@ -24,7 +24,6 @@ if ($GLOBALS['phorkie']['cfg']['setupcheck']) {
     SetupCheck::run();
 }
 
-//delete all repos
 $r = new \HTTP_Request2(
     'http://localhost:9200/phorkie/repo/_search',
     \HTTP_Request2::METHOD_GET
@@ -32,6 +31,8 @@ $r = new \HTTP_Request2(
 $r->setBody(
     json_encode(
         (object)array(
+            'from' => 0,
+            'size' => 2,
             'query' => (object)array(
                 'bool' => (object)array(
                     'should' => array(
diff --git a/src/phorkie/Database.php b/src/phorkie/Database.php
new file mode 100644 (file)
index 0000000..add38be
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+namespace phorkie;
+
+class Database
+{
+    public function getSearch()
+    {
+        return new Search_Elasticsearch();
+    }
+
+    public function getIndexer()
+    {
+        //FIXME
+    }
+
+}
+
+?>
diff --git a/src/phorkie/Search/Elasticsearch.php b/src/phorkie/Search/Elasticsearch.php
new file mode 100644 (file)
index 0000000..d03ef3e
--- /dev/null
@@ -0,0 +1,76 @@
+<?php
+namespace phorkie;
+
+class Search_Elasticsearch
+{
+    public function __construct()
+    {
+        $this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
+    }
+
+    /**
+     * Search for a given term and return repositories that contain it
+     * in their description, file names or file content
+     *
+     * @param string  $term    Search term
+     * @param integer $page    Page of search results, starting with 0
+     * @param integer $perPage Number of results per page
+     *
+     * @return Search_Result Search result object
+     */
+    public function search($term, $page = 0, $perPage = 10)
+    {
+        $r = new \HTTP_Request2(
+            $this->searchInstance . 'repo/_search',
+            \HTTP_Request2::METHOD_GET
+        );
+        $r->setBody(
+            json_encode(
+                (object)array(
+                    'from' => $page * $perPage,
+                    'size' => $perPage,
+                    'query' => (object)array(
+                        'bool' => (object)array(
+                            'should' => array(
+                                (object)array(
+                                    'query_string' => (object)array(
+                                        'query' => $term
+                                    ),
+                                ),
+                                (object)array(
+                                    'has_child' => (object)array(
+                                        'type'         => 'file',
+                                        'query' => (object)array(
+                                            'query_string' => (object)array(
+                                                'query' => $term
+                                            )
+                                        )
+                                    )
+                                )
+                            )
+                        )
+                    )
+                )
+            )
+        );
+        //FIXME: error handling
+        $httpRes = $r->send();
+        $jRes = json_decode($httpRes->getBody());
+
+        $sres = new Search_Result();
+        $sres->results = $jRes->hits->total;
+        $sres->page    = $page;
+        $sres->perPage = $perPage;
+
+        foreach ($jRes->hits->hits as $hit) {
+            $r = new Repository();
+            //FIXME: error handling. what about deleted repos?
+            $r->loadById($hit->_source->id);
+            $sres->repos[] = $r;
+        }
+
+        return $sres;
+    }
+}
+
+?>
diff --git a/src/phorkie/Search/Result.php b/src/phorkie/Search/Result.php
new file mode 100644 (file)
index 0000000..e64a3a6
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+namespace phorkie;
+
+class Search_Result
+{
+    public $repos;
+    public $results;
+    public $page;
+    public $perPage;
+
+    public function getRepos()
+    {
+        return $this->repos;
+    }
+
+    /**
+     * Returns the number of results
+     *
+     * @return integer Number of results
+     */
+    public function getResults()
+    {
+        return $this->results;
+    }
+
+    /**
+     * Returns the number of the current page, 0 based
+     *
+     * @return integer Number of current page
+     */
+    public function getPage()
+    {
+        return $this->page;
+    }
+
+    /**
+     * Returns the search results per page
+     *
+     * @return integer Number of results per page
+     */
+    public function getPerPage()
+    {
+        return $this->perPage;
+    }
+}
+
+?>
index 697b6ec..98d92ec 100644 (file)
@@ -14,3 +14,6 @@ RewriteRule ^([0-9]+)/tool/([^/]+)/(.+)$ /tool.php?id=$1&tool=$2&file=$3
 
 RewriteRule ^list$ /list.php
 RewriteRule ^list/([0-9]+)$ /list.php?page=$1
+
+RewriteRule ^search$ /search.php
+RewriteRule ^search/([0-9]+)$ /search.php?page=$1
diff --git a/www/search.php b/www/search.php
new file mode 100644 (file)
index 0000000..aef209f
--- /dev/null
@@ -0,0 +1,34 @@
+<?php
+namespace phorkie;
+/**
+ * Search for a search term
+ */
+require_once 'www-header.php';
+
+if (!isset($_GET['q']) || $_GET['q'] == '') {
+    header('Location: ' . Tools::fullUrl('/list'));
+    exit();
+}
+$query = $_GET['q'];
+
+$page = 0;
+if (isset($_GET['page'])) {
+    if (!is_numeric($_GET['page'])) {
+        throw new Exception_Input('List page is not numeric');
+    }
+    $page = (int)$_GET['page'];
+}
+$perPage = 10;
+
+$db     = new Database();
+$search = $db->getSearch();
+
+$sres = $search->search($query, $page, $perPage);
+render(
+    'search',
+    array(
+        'query' => $query,
+        'sres'  => $sres,
+    )
+);
+?>