diff options
| author | Christian Weiske <cweiske@cweiske.de> | 2012-05-01 20:40:24 +0200 |
|---|---|---|
| committer | Christian Weiske <cweiske@cweiske.de> | 2012-05-01 20:40:24 +0200 |
| commit | 1a8bb56c72ad34cf159e03b97a467e5e35a614c5 (patch) | |
| tree | 9a2d3f9a369aed9c967cf8001a883a2a60f3a002 | |
| parent | c78bab0c18fb9db18408f1601280997ee8b0779f (diff) | |
| download | phorkie-1a8bb56c72ad34cf159e03b97a467e5e35a614c5.tar.gz phorkie-1a8bb56c72ad34cf159e03b97a467e5e35a614c5.zip | |
searching works now (pager missing)
| -rw-r--r-- | README.rst | 6 | ||||
| -rw-r--r-- | data/config.default.php | 17 | ||||
| -rw-r--r-- | data/config.php.dist | 1 | ||||
| -rw-r--r-- | data/templates/base.htm | 4 | ||||
| -rw-r--r-- | data/templates/list.htm | 7 | ||||
| -rw-r--r-- | data/templates/repo-list.htm | 6 | ||||
| -rw-r--r-- | data/templates/search.htm | 20 | ||||
| -rw-r--r-- | scripts/search.php | 3 | ||||
| -rw-r--r-- | src/phorkie/Database.php | 18 | ||||
| -rw-r--r-- | src/phorkie/Search/Elasticsearch.php | 76 | ||||
| -rw-r--r-- | src/phorkie/Search/Result.php | 47 | ||||
| -rw-r--r-- | www/.htaccess | 3 | ||||
| -rw-r--r-- | www/search.php | 34 |
13 files changed, 221 insertions, 21 deletions
@@ -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 diff --git a/data/config.default.php b/data/config.default.php index 4ef5d05..c1446cc 100644 --- a/data/config.default.php +++ b/data/config.default.php @@ -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, diff --git a/data/config.php.dist b/data/config.php.dist index 04b42a6..c9b891e 100644 --- a/data/config.php.dist +++ b/data/config.php.dist @@ -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; ?> diff --git a/data/templates/base.htm b/data/templates/base.htm index 867481c..906453e 100644 --- a/data/templates/base.htm +++ b/data/templates/base.htm @@ -23,13 +23,11 @@ <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> diff --git a/data/templates/list.htm b/data/templates/list.htm index 4e2e4a6..b92e588 100644 --- a/data/templates/list.htm +++ b/data/templates/list.htm @@ -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 index 0000000..80c8f6c --- /dev/null +++ b/data/templates/repo-list.htm @@ -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 index 0000000..cb03cec --- /dev/null +++ b/data/templates/search.htm @@ -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 %} diff --git a/scripts/search.php b/scripts/search.php index b6273b4..af7a085 100644 --- a/scripts/search.php +++ b/scripts/search.php @@ -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 index 0000000..add38be --- /dev/null +++ b/src/phorkie/Database.php @@ -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 index 0000000..d03ef3e --- /dev/null +++ b/src/phorkie/Search/Elasticsearch.php @@ -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 index 0000000..e64a3a6 --- /dev/null +++ b/src/phorkie/Search/Result.php @@ -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; + } +} + +?> diff --git a/www/.htaccess b/www/.htaccess index 697b6ec..98d92ec 100644 --- a/www/.htaccess +++ b/www/.htaccess @@ -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 index 0000000..aef209f --- /dev/null +++ b/www/search.php @@ -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, + ) +); +?> |
