Fix bug #22: Edited pastes not in "recently created"
[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->setBody(
93             json_encode(
94                 (object)array(
95                     'match_all' => (object)array()
96                 )
97             )
98         );
99         $r->send();
100         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
101             $this->searchInstance . 'file/_query',
102             \HTTP_Request2::METHOD_DELETE
103         );
104         $r->setBody(
105             json_encode(
106                 (object)array(
107             'match_all' => (object)array()
108                 )
109             )
110         );
111         $r->send();
112     }
113
114     public function deleteRepo(Repository $repo)
115     {
116         //delete repository from index
117         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
118             $this->searchInstance . 'repo/' . $repo->id,
119             \HTTP_Request2::METHOD_DELETE
120         );
121         $r->send();
122
123         $this->deleteRepoFiles($repo);
124     }
125
126     protected function deleteRepoFiles(Repository $repo)
127     {
128         //delete files of that repository
129         $r = new Database_Adapter_Elasticsearch_HTTPRequest(
130             $this->searchInstance . 'file/_query',
131             \HTTP_Request2::METHOD_DELETE
132         );
133         $r->setBody(
134             json_encode(
135                 (object)array(
136                     'field' => (object)array(
137                         '_parent' => $repo->id
138                     )
139                 )
140             )
141         );
142         $r->send();
143     }
144
145 }
146
147 ?>