X-Git-Url: https://git.cweiske.de/phorkie.git/blobdiff_plain/58919afe451ad5b2088d61ff1c856f44dd522b32..d5901a1fcc8b79f9a23bd3888ef2f281f07b9459:/src/phorkie/Database/Adapter/Elasticsearch/Indexer.php diff --git a/src/phorkie/Database/Adapter/Elasticsearch/Indexer.php b/src/phorkie/Database/Adapter/Elasticsearch/Indexer.php index 41aa7d7..e93cfa7 100644 --- a/src/phorkie/Database/Adapter/Elasticsearch/Indexer.php +++ b/src/phorkie/Database/Adapter/Elasticsearch/Indexer.php @@ -9,15 +9,18 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer } - public function addRepo(Repository $repo, $crdate = null) + public function addRepo(Repository $repo, $crdate = null, $modate = null) { if ($crdate == null) { $crdate = time(); } - $this->updateRepo($repo, $crdate); + if ($modate == null) { + $modate = time(); + } + $this->updateRepo($repo, $crdate, $modate); } - public function updateRepo(Repository $repo, $crdate = null) + public function updateRepo(Repository $repo, $crdate = null, $modate = null) { //add repository $r = new Database_Adapter_Elasticsearch_HTTPRequest( @@ -29,9 +32,19 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer 'description' => $repo->getDescription(), 'tstamp' => gmdate('c', time()), ); + if ($crdate == null) { + $crdate = $this->getCrDate($repo); + } if ($crdate !== null) { $repoData['crdate'] = gmdate('c', $crdate); } + if ($modate == null) { + $modate = $this->getMoDate($repo); + } + if ($modate !== null) { + $repoData['modate'] = gmdate('c', $modate); + } + $r->setBody(json_encode((object)$repoData)); $r->send(); @@ -57,12 +70,57 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer } } + /** + * When updating the repository, we don't have a creation date. + * We need to keep it, but elasticsearch does not have a simple way + * to update some fields only (without using a custom script). + * + * @return integer Unix timestamp + */ + protected function getCrDate(Repository $repo) + { + $r = new Database_Adapter_Elasticsearch_HTTPRequest( + $this->searchInstance . 'repo/' . $repo->id, + \HTTP_Request2::METHOD_GET + ); + $json = json_decode($r->send()->getBody()); + + if (!isset($json->_source->crdate)) { + return null; + } + + return strtotime($json->_source->crdate); + } + + /** + * When updating the repository, we don't have a modification date. + * We need to keep it, but elasticsearch does not have a simple way + * to update some fields only (without using a custom script). + * + * @return integer Unix timestamp + */ + protected function getMoDate(Repository $repo) + { + $r = new Database_Adapter_Elasticsearch_HTTPRequest( + $this->searchInstance . 'repo/' . $repo->id, + \HTTP_Request2::METHOD_GET + ); + $json = json_decode($r->send()->getBody()); + + if (!isset($json->_source->modate)) { + return null; + } + + return strtotime($json->_source->modate); + } + public function deleteAllRepos() { $r = new Database_Adapter_Elasticsearch_HTTPRequest( $this->searchInstance . 'repo/_query', \HTTP_Request2::METHOD_DELETE ); + $r->allow404 = true; $r->setBody( json_encode( (object)array( @@ -75,6 +133,7 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer $this->searchInstance . 'file/_query', \HTTP_Request2::METHOD_DELETE ); + $r->allow404 = true; $r->setBody( json_encode( (object)array( @@ -92,6 +151,7 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer $this->searchInstance . 'repo/' . $repo->id, \HTTP_Request2::METHOD_DELETE ); + $r->allow404 = true; $r->send(); $this->deleteRepoFiles($repo); @@ -104,6 +164,7 @@ class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer $this->searchInstance . 'file/_query', \HTTP_Request2::METHOD_DELETE ); + $r->allow404 = true; $r->setBody( json_encode( (object)array(