diff options
| author | Christian Weiske <cweiske@cweiske.de> | 2012-05-07 22:22:59 +0200 |
|---|---|---|
| committer | Christian Weiske <cweiske@cweiske.de> | 2012-05-07 22:22:59 +0200 |
| commit | 347d191850d9a3c5a84f3dff1971e6d955555ac9 (patch) | |
| tree | 32bac7638c9bed2d9f4843e4fd93f46d6757c9c0 /src | |
| parent | fa61fdfea420bb770d4390f49fa5e2f379891d21 (diff) | |
| download | phorkie-347d191850d9a3c5a84f3dff1971e6d955555ac9.tar.gz phorkie-347d191850d9a3c5a84f3dff1971e6d955555ac9.zip | |
implement request #3: show recent pastes in "New paste" screen sidebar
Diffstat (limited to 'src')
| -rw-r--r-- | src/phorkie/Database/Adapter/Elasticsearch/Search.php | 79 | ||||
| -rw-r--r-- | src/phorkie/Database/Adapter/Null/Search.php | 9 | ||||
| -rw-r--r-- | src/phorkie/Database/ISearch.php | 15 |
3 files changed, 103 insertions, 0 deletions
diff --git a/src/phorkie/Database/Adapter/Elasticsearch/Search.php b/src/phorkie/Database/Adapter/Elasticsearch/Search.php index d077747..e960e04 100644 --- a/src/phorkie/Database/Adapter/Elasticsearch/Search.php +++ b/src/phorkie/Database/Adapter/Elasticsearch/Search.php @@ -3,12 +3,75 @@ namespace phorkie; class Database_Adapter_Elasticsearch_Search implements Database_ISearch { + protected static $sortMap = array( + 'id' => array('id', 'asc'), + 'crdate' => array('crdate', 'desc'), + 'tstamp' => array('tstamp', 'desc'), + ); + public function __construct() { $this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch']; } /** + * List all repositories + * + * @param integer $page Page of search results, starting with 0 + * @param integer $perPage Number of results per page + * @param string $sort Sort order. Allowed values: + * - id - repository id + * - crdate - creation date + * - tstamp - modification date + * + * @return Search_Result Search result object + */ + public function listAll($page = 0, $perPage = 10, $sort = 'id', $sortOrder = null) + { + list($sortField, $orderField) = $this->getSortField($sort, $sortOrder); + $r = new Database_Adapter_Elasticsearch_HTTPRequest( + $this->searchInstance . 'repo/_search', + \HTTP_Request2::METHOD_GET + ); + $r->setBody( + json_encode( + (object)array( + 'from' => $page * $perPage, + 'size' => $perPage, + 'sort' => array( + $sortField => $orderField + ), + 'query' => (object)array( + 'match_all' => (object)array() + ), + ) + ) + ); + $httpRes = $r->send(); + $jRes = json_decode($httpRes->getBody()); + if (isset($jRes->error)) { + throw new Exception( + 'Search exception: ' . $jRes->error, $jRes->status + ); + } + + $sres = new Search_Result(); + $sres->results = $jRes->hits->total; + $sres->page = $page; + $sres->perPage = $perPage; + + foreach ($jRes->hits->hits as $hit) { + $r = new Repository(); + $r->loadById($hit->_source->id); + $r->crdate = strtotime($hit->_source->crdate); + $sres->repos[] = $r; + } + + return $sres; + } + + + /** * Search for a given term and return repositories that contain it * in their description, file names or file content * @@ -77,6 +140,22 @@ class Database_Adapter_Elasticsearch_Search implements Database_ISearch return $sres; } + + protected function getSortField($sort, $sortOrder) + { + if (!isset(self::$sortMap[$sort])) { + throw new Exception('Invalid sort parameter: ' . $sort); + } + if ($sortOrder !== 'asc' && $sortOrder !== 'desc') { + throw new Exception('Invalid sortOrder parameter: ' . $sortOrder); + } + + $data = self::$sortMap[$sort]; + if ($sortOrder !== null) { + $data[1] = $sortOrder; + } + return $data; + } } ?> diff --git a/src/phorkie/Database/Adapter/Null/Search.php b/src/phorkie/Database/Adapter/Null/Search.php index 0731932..579c13a 100644 --- a/src/phorkie/Database/Adapter/Null/Search.php +++ b/src/phorkie/Database/Adapter/Null/Search.php @@ -11,6 +11,15 @@ class Database_Adapter_Null_Search implements Database_ISearch $sres->perPage = $perPage; return $sres; } + + public function listAll($page = 0, $perPage = 10, $sort = 'id', $sortOrder = null) + { + $sres = new Search_Result(); + $sres->results = 0; + $sres->page = $page; + $sres->perPage = $perPage; + return $sres; + } } ?> diff --git a/src/phorkie/Database/ISearch.php b/src/phorkie/Database/ISearch.php index b18d43a..c33867b 100644 --- a/src/phorkie/Database/ISearch.php +++ b/src/phorkie/Database/ISearch.php @@ -4,6 +4,21 @@ namespace phorkie; interface Database_ISearch { public function search($term, $page = 0, $perPage = 10); + + /** + * List all repositories + * + * @param integer $page Page of search results, starting with 0 + * @param integer $perPage Number of results per page + * @param string $sort Sort order. Allowed values: + * - id - repository id + * - crdate - creation date + * - tstamp - modification date + * + * @return Search_Result Search result object + */ + public function listAll($page = 0, $perPage = 10, $sort = 'id', $sortOrder = null); + } ?> |
