1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* FullTextSearch_Elasticsearch - Use Elasticsearch to index the content of your nextcloud |
7
|
|
|
* |
8
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
9
|
|
|
* later. See the COPYING file. |
10
|
|
|
* |
11
|
|
|
* @author Maxence Lange <[email protected]> |
12
|
|
|
* @copyright 2018 |
13
|
|
|
* @license GNU AGPL version 3 or any later version |
14
|
|
|
* |
15
|
|
|
* This program is free software: you can redistribute it and/or modify |
16
|
|
|
* it under the terms of the GNU Affero General Public License as |
17
|
|
|
* published by the Free Software Foundation, either version 3 of the |
18
|
|
|
* License, or (at your option) any later version. |
19
|
|
|
* |
20
|
|
|
* This program is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU Affero General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* You should have received a copy of the GNU Affero General Public License |
26
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
27
|
|
|
* |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
namespace OCA\FullTextSearch_Elasticsearch\Service; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
use OCA\FullTextSearch_Elasticsearch\AppInfo\Application; |
35
|
|
|
use OCA\FullTextSearch_Elasticsearch\Exceptions\ConfigurationException; |
36
|
|
|
use OCP\IConfig; |
37
|
|
|
use OCP\PreConditionNotMetException; |
38
|
|
|
use OCP\Util; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Class ConfigService |
43
|
|
|
* |
44
|
|
|
* @package OCA\FullTextSearch_Elasticsearch\Service |
45
|
|
|
*/ |
46
|
|
|
class ConfigService { |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
const FIELDS_LIMIT = 'fields_limit'; |
50
|
|
|
const ELASTIC_HOST = 'elastic_host'; |
51
|
|
|
const ELASTIC_INDEX = 'elastic_index'; |
52
|
|
|
const ELASTIC_VER_BELOW66 = 'es_ver_below66'; |
53
|
|
|
const ANALYZER_TOKENIZER = 'analyzer_tokenizer'; |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
public $defaults = [ |
57
|
|
|
self::ELASTIC_HOST => '', |
58
|
|
|
self::ELASTIC_INDEX => '', |
59
|
|
|
self::FIELDS_LIMIT => '10000', |
60
|
|
|
self::ELASTIC_VER_BELOW66 => '0', |
61
|
|
|
self::ANALYZER_TOKENIZER => 'standard' |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** @var IConfig */ |
66
|
|
|
private $config; |
67
|
|
|
|
68
|
|
|
/** @var string */ |
69
|
|
|
private $userId; |
70
|
|
|
|
71
|
|
|
/** @var MiscService */ |
72
|
|
|
private $miscService; |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* ConfigService constructor. |
77
|
|
|
* |
78
|
|
|
* @param IConfig $config |
79
|
|
|
* @param string $userId |
80
|
|
|
* @param MiscService $miscService |
81
|
|
|
*/ |
82
|
|
|
public function __construct(IConfig $config, $userId, MiscService $miscService) { |
83
|
|
|
$this->config = $config; |
84
|
|
|
$this->userId = $userId; |
85
|
|
|
$this->miscService = $miscService; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public function getConfig(): array { |
93
|
|
|
$keys = array_keys($this->defaults); |
94
|
|
|
$data = []; |
95
|
|
|
|
96
|
|
|
foreach ($keys as $k) { |
97
|
|
|
$data[$k] = $this->getAppValue($k); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $data; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param array $save |
106
|
|
|
*/ |
107
|
|
|
public function setConfig(array $save) { |
108
|
|
|
$keys = array_keys($this->defaults); |
109
|
|
|
|
110
|
|
|
foreach ($keys as $k) { |
111
|
|
|
if (array_key_exists($k, $save)) { |
112
|
|
|
$this->setAppValue($k, $save[$k]); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return array |
120
|
|
|
* @throws ConfigurationException |
121
|
|
|
*/ |
122
|
|
|
public function getElasticHost(): array { |
123
|
|
|
|
124
|
|
|
$strHost = $this->getAppValue(self::ELASTIC_HOST); |
125
|
|
|
if ($strHost === '') { |
126
|
|
|
throw new ConfigurationException( |
127
|
|
|
'Your ElasticSearchPlatform is not configured properly' |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$hosts = explode(',', $strHost); |
132
|
|
|
|
133
|
|
|
return array_map('trim', $hosts); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
* @throws ConfigurationException |
140
|
|
|
*/ |
141
|
|
|
public function getElasticIndex(): string { |
142
|
|
|
|
143
|
|
|
$index = $this->getAppValue(self::ELASTIC_INDEX); |
144
|
|
|
if ($index === '') { |
145
|
|
|
throw new ConfigurationException( |
146
|
|
|
'Your ElasticSearchPlatform is not configured properly' |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $index; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Get a value by key |
156
|
|
|
* |
157
|
|
|
* @param string $key |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
View Code Duplication |
public function getAppValue(string $key): string { |
|
|
|
|
162
|
|
|
$defaultValue = null; |
163
|
|
|
if (array_key_exists($key, $this->defaults)) { |
164
|
|
|
$defaultValue = $this->defaults[$key]; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this->config->getAppValue(Application::APP_NAME, $key, $defaultValue); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Set a value by key |
172
|
|
|
* |
173
|
|
|
* @param string $key |
174
|
|
|
* @param string $value |
175
|
|
|
*/ |
176
|
|
|
public function setAppValue(string $key, string $value) { |
177
|
|
|
$this->config->setAppValue(Application::APP_NAME, $key, $value); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* remove a key |
182
|
|
|
* |
183
|
|
|
* @param string $key |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function deleteAppValue(string $key): string { |
188
|
|
|
return $this->config->deleteAppValue(Application::APP_NAME, $key); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get a user value by key |
193
|
|
|
* |
194
|
|
|
* @param string $key |
195
|
|
|
* |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
View Code Duplication |
public function getUserValue(string $key): string { |
|
|
|
|
199
|
|
|
$defaultValue = null; |
200
|
|
|
if (array_key_exists($key, $this->defaults)) { |
201
|
|
|
$defaultValue = $this->defaults[$key]; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $this->config->getUserValue( |
205
|
|
|
$this->userId, Application::APP_NAME, $key, $defaultValue |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Set a user value by key |
211
|
|
|
* |
212
|
|
|
* @param string $key |
213
|
|
|
* @param string $value |
214
|
|
|
* |
215
|
|
|
* @throws PreConditionNotMetException |
216
|
|
|
*/ |
217
|
|
|
public function setUserValue(string $key, string $value) { |
218
|
|
|
$this->config->setUserValue($this->userId, Application::APP_NAME, $key, $value); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get a user value by key and user |
223
|
|
|
* |
224
|
|
|
* @param string $userId |
225
|
|
|
* @param string $key |
226
|
|
|
* |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
|
|
public function getValueForUser(string $userId, string $key): string { |
230
|
|
|
return $this->config->getUserValue($userId, Application::APP_NAME, $key); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Set a user value by key |
235
|
|
|
* |
236
|
|
|
* @param string $userId |
237
|
|
|
* @param string $key |
238
|
|
|
* @param string $value |
239
|
|
|
* |
240
|
|
|
* @throws PreConditionNotMetException |
241
|
|
|
*/ |
242
|
|
|
public function setValueForUser($userId, $key, $value) { |
243
|
|
|
$this->config->setUserValue($userId, Application::APP_NAME, $key, $value); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return int |
249
|
|
|
*/ |
250
|
|
|
public function getNcVersion(): int { |
251
|
|
|
$ver = Util::getVersion(); |
252
|
|
|
|
253
|
|
|
return $ver[0]; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.