move elasticsearch code in one folder
[phorkie.git] / src / phorkie / Database / Adapter / Elasticsearch / Indexer.php
1 <?php
2 namespace phorkie;
3
4 class Database_Adapter_Elasticsearch_Indexer
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             $repoData['crdate'] = gmdate('c', $crdate);
34         }
35         $r->setBody(json_encode((object)$repoData));
36         $r->send();
37
38         //add files
39         //clean up before adding files; files might have been deleted
40         $this->deleteRepoFiles($repo);
41
42         foreach ($repo->getFiles() as $file) {
43             $r = new Database_Adapter_Elasticsearch_HTTPRequest(
44                 $this->searchInstance . 'file/?parent=' . $repo->id,
45                 \HTTP_Request2::METHOD_POST
46             );
47             $r->setBody(
48                 json_encode(
49                     (object)array(
50                         'name'      => $file->getFilename(),
51                         'extension' => $file->getExt(),
52                         'content'   => $file->isText() ? $file->getContent() : '',
53                     )
54                 )
55             );
56             $r->send();
57         }
58     }
59
60     public function deleteAllRepos()
61     {
62         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
63             $this->searchInstance . 'repo/_query',
64             \HTTP_Request2::METHOD_DELETE
65         );
66         $r->setBody(
67             json_encode(
68                 (object)array(
69                     'match_all' => (object)array()
70                 )
71             )
72         );
73         $r->send();
74         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
75             $this->searchInstance . 'file/_query',
76             \HTTP_Request2::METHOD_DELETE
77         );
78         $r->setBody(
79             json_encode(
80                 (object)array(
81             'match_all' => (object)array()
82                 )
83             )
84         );
85         $r->send();
86     }
87
88     public function deleteRepo(Repository $repo)
89     {
90         //delete repository from index
91         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
92             $this->searchInstance . 'repo/' . $repo->id,
93             \HTTP_Request2::METHOD_DELETE
94         );
95         $r->send();
96
97         $this->deleteRepoFiles($repo);
98     }
99
100     protected function deleteRepoFiles(Repository $repo)
101     {
102         //delete files of that repository
103         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
104             $this->searchInstance . 'file/_query',
105             \HTTP_Request2::METHOD_DELETE
106         );
107         $r->setBody(
108             json_encode(
109                 (object)array(
110                     'field' => (object)array(
111                         '_parent' => $repo->id
112                     )
113                 )
114             )
115         );
116         $r->send();
117     }
118
119 }
120
121 ?>