cf50bbe3fb2b592a1d2498fbeeae9974ab7c41e3
[phorkie.git] / src / phorkie / Database / Adapter / Elasticsearch / Indexer.php
1 <?php
2 namespace phorkie;
3
4 class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer
5 {
6     public function __construct()
7     {
8         $this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
9     }
10
11
12     public function addRepo(Repository $repo, $crdate = null)
13     {
14         if ($crdate == null) {
15             $crdate = time();
16         }
17         $this->updateRepo($repo, $crdate);
18     }
19
20     public function updateRepo(Repository $repo, $crdate = null)
21     {
22         //add repository
23         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
24             $this->searchInstance . 'repo/' . $repo->id,
25             \HTTP_Request2::METHOD_PUT
26         );
27         $repoData = array(
28             'id'          => $repo->id,
29             'description' => $repo->getDescription(),
30             'tstamp'      => gmdate('c', time()),
31         );
32         if ($crdate == null) {
33             $crdate = $this->getCrDate($repo);
34         }
35         if ($crdate !== null) {
36             $repoData['crdate'] = gmdate('c', $crdate);
37         }
38
39         $r->setBody(json_encode((object)$repoData));
40         $r->send();
41
42         //add files
43         //clean up before adding files; files might have been deleted
44         $this->deleteRepoFiles($repo);
45
46         foreach ($repo->getFiles() as $file) {
47             $r = new Database_Adapter_Elasticsearch_HTTPRequest(
48                 $this->searchInstance . 'file/?parent=' . $repo->id,
49                 \HTTP_Request2::METHOD_POST
50             );
51             $r->setBody(
52                 json_encode(
53                     (object)array(
54                         'name'      => $file->getFilename(),
55                         'extension' => $file->getExt(),
56                         'content'   => $file->isText() ? $file->getContent() : '',
57                     )
58                 )
59             );
60             $r->send();
61         }
62     }
63
64     /**
65      * When updating the repository, we don't have a creation date.
66      * We need to keep it, but elasticsearch does not have a simple way
67      * to update some fields only (without using a custom script).
68      *
69      * @return integer Unix timestamp
70      */
71     protected function getCrDate(Repository $repo)
72     {
73         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
74             $this->searchInstance . 'repo/' . $repo->id,
75             \HTTP_Request2::METHOD_GET
76         );
77         $json = json_decode($r->send()->getBody());
78
79         if (!isset($json->_source->crdate)) {
80             return null;
81         }
82
83         return strtotime($json->_source->crdate);
84     }
85
86     public function deleteAllRepos()
87     {
88         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
89             $this->searchInstance . 'repo/_query',
90             \HTTP_Request2::METHOD_DELETE
91         );
92         $r->allow404 = true;
93         $r->setBody(
94             json_encode(
95                 (object)array(
96                     'match_all' => (object)array()
97                 )
98             )
99         );
100         $r->send();
101         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
102             $this->searchInstance . 'file/_query',
103             \HTTP_Request2::METHOD_DELETE
104         );
105         $r->allow404 = true;
106         $r->setBody(
107             json_encode(
108                 (object)array(
109             'match_all' => (object)array()
110                 )
111             )
112         );
113         $r->send();
114     }
115
116     public function deleteRepo(Repository $repo)
117     {
118         //delete repository from index
119         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
120             $this->searchInstance . 'repo/' . $repo->id,
121             \HTTP_Request2::METHOD_DELETE
122         );
123         $r->allow404 = true;
124         $r->send();
125
126         $this->deleteRepoFiles($repo);
127     }
128
129     protected function deleteRepoFiles(Repository $repo)
130     {
131         //delete files of that repository
132         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
133             $this->searchInstance . 'file/_query',
134             \HTTP_Request2::METHOD_DELETE
135         );
136         $r->allow404 = true;
137         $r->setBody(
138             json_encode(
139                 (object)array(
140                     'field' => (object)array(
141                         '_parent' => $repo->id
142                     )
143                 )
144             )
145         );
146         $r->send();
147     }
148
149 }
150
151 ?>