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
<?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,
//$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;
?>
<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>
{% 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>
--- /dev/null
+ <li>
+ <a href="{{repo.getLink('display')}}">
+ {{repo.id}}
+ {{repo.getDescription}}
+ </a>
+ </li>
--- /dev/null
+{% 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 %}
SetupCheck::run();
}
-//delete all repos
$r = new \HTTP_Request2(
'http://localhost:9200/phorkie/repo/_search',
\HTTP_Request2::METHOD_GET
$r->setBody(
json_encode(
(object)array(
+ 'from' => 0,
+ 'size' => 2,
'query' => (object)array(
'bool' => (object)array(
'should' => array(
--- /dev/null
+<?php
+namespace phorkie;
+
+class Database
+{
+ public function getSearch()
+ {
+ return new Search_Elasticsearch();
+ }
+
+ public function getIndexer()
+ {
+ //FIXME
+ }
+
+}
+
+?>
--- /dev/null
+<?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;
+ }
+}
+
+?>
--- /dev/null
+<?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;
+ }
+}
+
+?>
RewriteRule ^list$ /list.php
RewriteRule ^list/([0-9]+)$ /list.php?page=$1
+
+RewriteRule ^search$ /search.php
+RewriteRule ^search/([0-9]+)$ /search.php?page=$1
--- /dev/null
+<?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,
+ )
+);
+?>