Fix bug #22: Edited pastes not in "recently created"
[phorkie.git] / src / phorkie / Database / Adapter / Elasticsearch / Indexer.php
index 41aa7d71bda77abad0fba12c6de61ab3f5ed487a..dbe769e56a647e21fef33ebd0ce3d707649bc536 100644 (file)
@@ -29,9 +29,13 @@ 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);
         }
+
         $r->setBody(json_encode((object)$repoData));
         $r->send();
 
@@ -57,6 +61,28 @@ 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);
+    }
+
     public function deleteAllRepos()
     {
         $r = new Database_Adapter_Elasticsearch_HTTPRequest(