use httprequest wrapper to automatically check for errors
[phorkie.git] / src / phorkie / Indexer / Elasticsearch.php
index 2921239..077865e 100644 (file)
@@ -9,26 +9,30 @@ class Indexer_Elasticsearch
     }
 
 
     }
 
 
-    public function addRepo(Repository $repo)
+    public function addRepo(Repository $repo, $crdate = null)
     {
     {
-        $this->updateRepo($repo);
+        if ($crdate == null) {
+            $crdate = time();
+        }
+        $this->updateRepo($repo, $crdate);
     }
 
     }
 
-    public function updateRepo(Repository $repo)
+    public function updateRepo(Repository $repo, $crdate = null)
     {
         //add repository
     {
         //add repository
-        $r = new \HTTP_Request2(
+        $r = new Database_Adapter_Elasticsearch_HTTPRequest(
             $this->searchInstance . 'repo/' . $repo->id,
             \HTTP_Request2::METHOD_PUT
         );
             $this->searchInstance . 'repo/' . $repo->id,
             \HTTP_Request2::METHOD_PUT
         );
-        $r->setBody(
-            json_encode(
-                (object)array(
-                    'id' => $repo->id,
-                    'description' => $repo->getDescription(),
-                )
-            )
+        $repoData = array(
+            'id'          => $repo->id,
+            'description' => $repo->getDescription(),
+            'tstamp'      => gmdate('c', time()),
         );
         );
+        if ($crdate !== null) {
+            $repoData['crdate'] = gmdate('c', $crdate);
+        }
+        $r->setBody(json_encode((object)$repoData));
         $r->send();
 
         //add files
         $r->send();
 
         //add files
@@ -36,7 +40,7 @@ class Indexer_Elasticsearch
         $this->deleteRepoFiles($repo);
 
         foreach ($repo->getFiles() as $file) {
         $this->deleteRepoFiles($repo);
 
         foreach ($repo->getFiles() as $file) {
-            $r = new \HTTP_Request2(
+            $r = new Database_Adapter_Elasticsearch_HTTPRequest(
                 $this->searchInstance . 'file/?parent=' . $repo->id,
                 \HTTP_Request2::METHOD_POST
             );
                 $this->searchInstance . 'file/?parent=' . $repo->id,
                 \HTTP_Request2::METHOD_POST
             );
@@ -53,10 +57,38 @@ class Indexer_Elasticsearch
         }
     }
 
         }
     }
 
+    public function deleteAllRepos()
+    {
+        $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+            $this->searchInstance . 'repo/_query',
+            \HTTP_Request2::METHOD_DELETE
+        );
+        $r->setBody(
+            json_encode(
+                (object)array(
+                    'match_all' => (object)array()
+                )
+            )
+        );
+        $r->send();
+        $r = new Database_Adapter_Elasticsearch_HTTPRequest(
+            $this->searchInstance . 'file/_query',
+            \HTTP_Request2::METHOD_DELETE
+        );
+        $r->setBody(
+            json_encode(
+                (object)array(
+            'match_all' => (object)array()
+                )
+            )
+        );
+        $r->send();
+    }
+
     public function deleteRepo(Repository $repo)
     {
         //delete repository from index
     public function deleteRepo(Repository $repo)
     {
         //delete repository from index
-        $r = new \HTTP_Request2(
+        $r = new Database_Adapter_Elasticsearch_HTTPRequest(
             $this->searchInstance . 'repo/' . $repo->id,
             \HTTP_Request2::METHOD_DELETE
         );
             $this->searchInstance . 'repo/' . $repo->id,
             \HTTP_Request2::METHOD_DELETE
         );
@@ -68,7 +100,7 @@ class Indexer_Elasticsearch
     protected function deleteRepoFiles(Repository $repo)
     {
         //delete files of that repository
     protected function deleteRepoFiles(Repository $repo)
     {
         //delete files of that repository
-        $r = new \HTTP_Request2(
+        $r = new Database_Adapter_Elasticsearch_HTTPRequest(
             $this->searchInstance . 'file/_query',
             \HTTP_Request2::METHOD_DELETE
         );
             $this->searchInstance . 'file/_query',
             \HTTP_Request2::METHOD_DELETE
         );