index pastes when they change/get created
[phorkie.git] / src / phorkie / Indexer / Elasticsearch.php
1 <?php
2 namespace phorkie;
3
4 class Indexer_Elasticsearch
5 {
6     public function __construct()
7     {
8         $this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
9     }
10
11
12     public function addRepo(Repository $repo)
13     {
14         $this->updateRepo($repo);
15     }
16
17     public function updateRepo(Repository $repo)
18     {
19         //add repository
20         $r = new \HTTP_Request2(
21             $this->searchInstance . 'repo/' . $repo->id,
22             \HTTP_Request2::METHOD_PUT
23         );
24         $r->setBody(
25             json_encode(
26                 (object)array(
27                     'id' => $repo->id,
28                     'description' => $repo->getDescription(),
29                 )
30             )
31         );
32         $r->send();
33
34         //add files
35         foreach ($repo->getFiles() as $file) {
36             $r = new \HTTP_Request2(
37                 $this->searchInstance . 'file/?parent=' . $repo->id,
38                 \HTTP_Request2::METHOD_POST
39             );
40             $r->setBody(
41                 json_encode(
42                     (object)array(
43                         'name'      => $file->getFilename(),
44                         'extension' => $file->getExt(),
45                         'content'   => $file->isText() ? $file->getContent() : '',
46                     )
47                 )
48             );
49             $r->send();
50         }
51     }
52
53     public function deleteRepo(Repository $repo)
54     {
55         //delete repository from index
56         $r = new \HTTP_Request2(
57             $this->searchInstance . 'repo/' . $repo->id,
58             \HTTP_Request2::METHOD_DELETE
59         );
60         $r->send();
61
62         //delete files of that repository
63         $r = new \HTTP_Request2(
64             $this->searchInstance . 'file/_query',
65             \HTTP_Request2::METHOD_DELETE
66         );
67         $r->setBody(
68             json_encode(
69                 (object)array(
70                     '_parent' => 'repo#' . $repo->id
71                 )
72             )
73         );
74         $r->send();
75     }
76
77 }
78
79 ?>