-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.php
154 lines (138 loc) · 3.88 KB
/
base.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
/**
* @package Elasticsearch
* @author Alan Hardman <alan@phpizza.com>
* @version 0.1.0
*/
namespace Plugin\Elasticsearch;
use Elasticsearch\ClientBuilder;
use Elasticsearch\Common\Exceptions\ElasticsearchException;
use Elasticsearch\Common\Exceptions\Missing404Exception;
class Base extends \Plugin
{
protected $client = null;
const INDEX_NAME = 'phproject_issues';
/**
* Initialize the plugin
*/
public function _load()
{
$f3 = \Base::instance();
if (!is_file(__DIR__ . '/vendor/autoload.php')) {
$f3->set('error', 'Run `composer install` from app/plugin/elasticsearch/ to complete the Elasticsearch installation.');
return;
}
// Load composer libraries
require_once __DIR__ . '/vendor/autoload.php';
// Hook into issue events
$this->_hook('model/issue.after_save', [$this, 'issueSaveHook']);
// Add/override routes
$f3->route('GET /search', 'Plugin\Elasticsearch\Controller->search');
$f3->route('POST /search/reindex', 'Plugin\Elasticsearch\Controller->reindex');
}
/**
* Generate page for admin panel
*/
public function _admin()
{
$f3 = \Base::instance();
$f3->set('info', $this->client()->info());
echo \Helper\View::instance()->render('elasticsearch/view/admin.html');
}
/**
* Get an Elasticsearch client instance
* @return \Elasticsearch\Client
*/
public function client()
{
if ($this->client === null) {
$this->client = ClientBuilder::create()->build();
}
return $this->client;
}
/**
* Delete all indexed records
*
* @return array
*/
public function truncate()
{
$indices = $this->client()->indices();
$params = [
'index' => self::INDEX_NAME
];
if ($indices->exists($params)) {
return $this->client()->indices()->delete($params);
}
return null;
}
/**
* Index all issues
*
* @return array
*/
public function indexAll()
{
$detail = new \Model\Issue\Detail;
$issues = $detail->find(['deleted_date IS NULL']);
foreach ($issues as $issue) {
$this->indexIssue($issue);
}
return count($issues);
}
/**
* Index an issue
* @param \Model\Issue\Detail $issue
* @return void
*/
public function indexIssue(\Model\Issue\Detail $issue)
{
$this->client()->index([
'index' => self::INDEX_NAME,
'type' => 'issue',
'id' => $issue->id,
'body' => [
'name' => $issue->name,
'description' => $issue->description,
'author_name' => $issue->author_name,
'owner_name' => $issue->owner_name,
]
]);
}
/**
* Delete an issue
* @param \Model\Issue $issue
* @return void
*/
public function deleteIssue(\Model\Issue $issue)
{
$this->client()->delete([
'index' => self::INDEX_NAME,
'type' => 'issue',
'id' => $issue->id
]);
}
/**
* Handle issue saving
* @param $issue \Model\Issue
* @return void
*/
public function issueSaveHook(\Model\Issue $issue)
{
if ($issue->deleted_date) {
try {
$this->deleteIssue($issue);
} catch (Missing404Exception $e) {
// Silently ignore 404s
} catch (ElasticsearchException $e) {
\Base::instance()->set('error', 'Failed to delete issue from Elasticsearch index.');
}
} else {
$detail = new \Model\Issue\Detail;
$detail->load(['id = ?', $issue->id]);
if ($detail->id) {
$this->indexIssue($detail);
}
}
}
}