From: Christian Weiske Date: Tue, 1 May 2012 18:40:24 +0000 (+0200) Subject: searching works now (pager missing) X-Git-Tag: v0.2.0~36 X-Git-Url: https://git.cweiske.de/phorkie.git/commitdiff_plain/1a8bb56c72ad34cf159e03b97a467e5e35a614c5?hp=c78bab0c18fb9db18408f1601280997ee8b0779f searching works now (pager missing) --- diff --git a/README.rst b/README.rst index aaa38cc..f5c93f2 100644 --- a/README.rst +++ b/README.rst @@ -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 @@ 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 @@
  • List all
  • - 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 %} 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 @@ +
  • + + {{repo.id}} + {{repo.getDescription}} + +
  • 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 %} +

    + Sorry, no results for "{{query}}". +

    +{% else %} +

    + Found {{sres.results}} search results for "{{query}}": +

    + +{% 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 @@ + 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 @@ +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 @@ +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 @@ +getSearch(); + +$sres = $search->search($query, $page, $perPage); +render( + 'search', + array( + 'query' => $query, + 'sres' => $sres, + ) +); +?>