4 class Database_Adapter_Elasticsearch_Indexer implements Database_IIndexer
6 public function __construct()
8 $this->searchInstance = $GLOBALS['phorkie']['cfg']['elasticsearch'];
12 public function addRepo(Repository $repo, $crdate = null, $modate = null)
14 if ($crdate == null) {
17 if ($modate == null) {
20 $this->updateRepo($repo, $crdate, $modate);
23 public function updateRepo(Repository $repo, $crdate = null, $modate = null)
26 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
27 $this->searchInstance . 'repo/' . $repo->id,
28 \HTTP_Request2::METHOD_PUT
32 'description' => $repo->getDescription(),
33 'tstamp' => gmdate('c', time()),
35 if ($crdate == null) {
36 $crdate = $this->getCrDate($repo);
38 if ($crdate !== null) {
39 $repoData['crdate'] = gmdate('c', $crdate);
41 if ($modate == null) {
42 $modate = $this->getMoDate($repo);
44 if ($modate !== null) {
45 $repoData['modate'] = gmdate('c', $modate);
48 $r->setBody(json_encode((object)$repoData));
52 //clean up before adding files; files might have been deleted
53 $this->deleteRepoFiles($repo);
55 foreach ($repo->getFiles() as $file) {
56 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
57 $this->searchInstance . 'file/?parent=' . $repo->id,
58 \HTTP_Request2::METHOD_POST
63 'name' => $file->getFilename(),
64 'extension' => $file->getExt(),
65 'content' => $file->isText() ? $file->getContent() : '',
74 * When updating the repository, we don't have a creation date.
75 * We need to keep it, but elasticsearch does not have a simple way
76 * to update some fields only (without using a custom script).
78 * @return integer Unix timestamp
80 protected function getCrDate(Repository $repo)
82 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
83 $this->searchInstance . 'repo/' . $repo->id,
84 \HTTP_Request2::METHOD_GET
86 $json = json_decode($r->send()->getBody());
88 if (!isset($json->_source->crdate)) {
92 return strtotime($json->_source->crdate);
96 * When updating the repository, we don't have a modification date.
97 * We need to keep it, but elasticsearch does not have a simple way
98 * to update some fields only (without using a custom script).
100 * @return integer Unix timestamp
102 protected function getMoDate(Repository $repo)
104 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
105 $this->searchInstance . 'repo/' . $repo->id,
106 \HTTP_Request2::METHOD_GET
108 $json = json_decode($r->send()->getBody());
110 if (!isset($json->_source->modate)) {
114 return strtotime($json->_source->modate);
117 public function deleteAllRepos()
119 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
120 $this->searchInstance . 'repo',
121 \HTTP_Request2::METHOD_DELETE
125 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
126 $this->searchInstance . 'file',
127 \HTTP_Request2::METHOD_DELETE
132 public function deleteRepo(Repository $repo)
134 //delete repository from index
135 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
136 $this->searchInstance . 'repo/' . $repo->id,
137 \HTTP_Request2::METHOD_DELETE
142 $this->deleteRepoFiles($repo);
145 protected function deleteRepoFiles(Repository $repo)
147 //delete files of that repository
148 $r = new Database_Adapter_Elasticsearch_HTTPRequest(
149 $this->searchInstance . 'file/_query'
150 . '?q=_parent:' . $repo->id,
151 \HTTP_Request2::METHOD_DELETE