@@ -58,420 +58,420 @@ |
||
58 | 58 | class ElasticSearchPlatform implements IFullTextSearchPlatform { |
59 | 59 | |
60 | 60 | |
61 | - use TArrayTools; |
|
62 | - |
|
63 | - private ?Client $client = null; |
|
64 | - private ?IRunner $runner = null; |
|
65 | - |
|
66 | - public function __construct( |
|
67 | - private ConfigService $configService, |
|
68 | - private IndexService $indexService, |
|
69 | - private SearchService $searchService, |
|
70 | - ) { |
|
71 | - } |
|
72 | - |
|
73 | - |
|
74 | - /** |
|
75 | - * return a unique Id of the platform. |
|
76 | - */ |
|
77 | - public function getId(): string { |
|
78 | - return 'elastic_search'; |
|
79 | - } |
|
80 | - |
|
81 | - |
|
82 | - /** |
|
83 | - * return a unique Id of the platform. |
|
84 | - */ |
|
85 | - public function getName(): string { |
|
86 | - return 'Elasticsearch'; |
|
87 | - } |
|
88 | - |
|
89 | - |
|
90 | - /** |
|
91 | - * @return array |
|
92 | - * @throws ConfigurationException |
|
93 | - */ |
|
94 | - public function getConfiguration(): array { |
|
95 | - $result = $this->configService->getConfig(); |
|
96 | - |
|
97 | - $sanitizedHosts = []; |
|
98 | - $hosts = $this->configService->getElasticHost(); |
|
99 | - foreach ($hosts as $host) { |
|
100 | - $parsedHost = parse_url($host); |
|
101 | - $safeHost = $parsedHost['scheme'] . '://'; |
|
102 | - if (array_key_exists('user', $parsedHost)) { |
|
103 | - $safeHost .= $parsedHost['user'] . ':' . '********' . '@'; |
|
104 | - } |
|
105 | - $safeHost .= $parsedHost['host']; |
|
106 | - $safeHost .= ':' . $parsedHost['port']; |
|
107 | - |
|
108 | - $sanitizedHosts[] = $safeHost; |
|
109 | - } |
|
110 | - |
|
111 | - $result['elastic_host'] = $sanitizedHosts; |
|
112 | - |
|
113 | - return $result; |
|
114 | - } |
|
115 | - |
|
116 | - |
|
117 | - /** |
|
118 | - * @param IRunner $runner |
|
119 | - */ |
|
120 | - public function setRunner(IRunner $runner) { |
|
121 | - $this->runner = $runner; |
|
122 | - } |
|
123 | - |
|
124 | - |
|
125 | - /** |
|
126 | - * Called when loading the platform. |
|
127 | - * |
|
128 | - * Loading some container and connect to ElasticSearch. |
|
129 | - * |
|
130 | - * @throws ConfigurationException |
|
131 | - * @throws Exception |
|
132 | - */ |
|
133 | - public function loadPlatform() { |
|
134 | - try { |
|
135 | - $this->connectToElastic($this->configService->getElasticHost()); |
|
136 | - } catch (ConfigurationException $e) { |
|
137 | - throw $e; |
|
138 | - } |
|
139 | - } |
|
140 | - |
|
141 | - |
|
142 | - /** |
|
143 | - * not used yet. |
|
144 | - * |
|
145 | - * @return bool |
|
146 | - */ |
|
147 | - public function testPlatform(): bool { |
|
148 | - $ping = $this->getClient()->ping(); |
|
149 | - return $ping->asBool(); |
|
150 | - } |
|
151 | - |
|
152 | - |
|
153 | - /** |
|
154 | - * called before any index |
|
155 | - * |
|
156 | - * We create a general index. |
|
157 | - * |
|
158 | - * @throws ConfigurationException |
|
159 | - * @throws BadRequest400Exception |
|
160 | - */ |
|
161 | - public function initializeIndex() { |
|
162 | - $this->indexService->initializeIndex($this->getClient()); |
|
163 | - } |
|
164 | - |
|
165 | - |
|
166 | - /** |
|
167 | - * resetIndex(); |
|
168 | - * |
|
169 | - * Called when admin wants to remove an index specific to a $provider. |
|
170 | - * $provider can be null, meaning a reset of the whole index. |
|
171 | - * |
|
172 | - * @param string $providerId |
|
173 | - * |
|
174 | - * @throws ConfigurationException |
|
175 | - */ |
|
176 | - public function resetIndex(string $providerId) { |
|
177 | - if ($providerId === 'all') { |
|
178 | - $this->indexService->resetIndexAll($this->getClient()); |
|
179 | - } else { |
|
180 | - $this->indexService->resetIndex($this->getClient(), $providerId); |
|
181 | - } |
|
182 | - } |
|
183 | - |
|
184 | - |
|
185 | - /** |
|
186 | - * @param IIndexDocument $document |
|
187 | - * |
|
188 | - * @return IIndex |
|
189 | - */ |
|
190 | - public function indexDocument(IIndexDocument $document): IIndex { |
|
191 | - $document->initHash(); |
|
192 | - |
|
193 | - $index = null; |
|
194 | - try { |
|
195 | - $result = $this->indexService->indexDocument($this->getClient(), $document); |
|
196 | - $index = $this->indexService->parseIndexResult($document->getIndex(), $result); |
|
197 | - |
|
198 | - $this->updateNewIndexResult( |
|
199 | - $document->getIndex(), json_encode($result), 'ok', |
|
200 | - IRunner::RESULT_TYPE_SUCCESS |
|
201 | - ); |
|
202 | - |
|
203 | - return $index; |
|
204 | - } catch (Exception $e) { |
|
205 | - $this->manageIndexErrorException($document, $e); |
|
206 | - } |
|
207 | - |
|
208 | - try { |
|
209 | - $result = $this->indexDocumentError($document, $e); |
|
210 | - $index = $this->indexService->parseIndexResult($document->getIndex(), $result); |
|
211 | - |
|
212 | - $this->updateNewIndexResult( |
|
213 | - $document->getIndex(), json_encode($result), 'ok', |
|
214 | - IRunner::RESULT_TYPE_WARNING |
|
215 | - ); |
|
216 | - |
|
217 | - return $index; |
|
218 | - } catch (Exception $e) { |
|
219 | - $this->updateNewIndexResult( |
|
220 | - $document->getIndex(), '', 'fail', |
|
221 | - IRunner::RESULT_TYPE_FAIL |
|
222 | - ); |
|
223 | - $this->manageIndexErrorException($document, $e); |
|
224 | - } |
|
225 | - |
|
226 | - return $document->getIndex(); |
|
227 | - } |
|
228 | - |
|
229 | - |
|
230 | - /** |
|
231 | - * @param IIndexDocument $document |
|
232 | - * @param Exception $e |
|
233 | - * |
|
234 | - * @return array |
|
235 | - * @throws AccessIsEmptyException |
|
236 | - * @throws ConfigurationException |
|
237 | - * @throws Exception |
|
238 | - */ |
|
239 | - private function indexDocumentError(IIndexDocument $document, Exception $e): array { |
|
240 | - |
|
241 | - $this->updateRunnerAction('indexDocumentWithoutContent', true); |
|
242 | - |
|
243 | - $document->setContent(''); |
|
61 | + use TArrayTools; |
|
62 | + |
|
63 | + private ?Client $client = null; |
|
64 | + private ?IRunner $runner = null; |
|
65 | + |
|
66 | + public function __construct( |
|
67 | + private ConfigService $configService, |
|
68 | + private IndexService $indexService, |
|
69 | + private SearchService $searchService, |
|
70 | + ) { |
|
71 | + } |
|
72 | + |
|
73 | + |
|
74 | + /** |
|
75 | + * return a unique Id of the platform. |
|
76 | + */ |
|
77 | + public function getId(): string { |
|
78 | + return 'elastic_search'; |
|
79 | + } |
|
80 | + |
|
81 | + |
|
82 | + /** |
|
83 | + * return a unique Id of the platform. |
|
84 | + */ |
|
85 | + public function getName(): string { |
|
86 | + return 'Elasticsearch'; |
|
87 | + } |
|
88 | + |
|
89 | + |
|
90 | + /** |
|
91 | + * @return array |
|
92 | + * @throws ConfigurationException |
|
93 | + */ |
|
94 | + public function getConfiguration(): array { |
|
95 | + $result = $this->configService->getConfig(); |
|
96 | + |
|
97 | + $sanitizedHosts = []; |
|
98 | + $hosts = $this->configService->getElasticHost(); |
|
99 | + foreach ($hosts as $host) { |
|
100 | + $parsedHost = parse_url($host); |
|
101 | + $safeHost = $parsedHost['scheme'] . '://'; |
|
102 | + if (array_key_exists('user', $parsedHost)) { |
|
103 | + $safeHost .= $parsedHost['user'] . ':' . '********' . '@'; |
|
104 | + } |
|
105 | + $safeHost .= $parsedHost['host']; |
|
106 | + $safeHost .= ':' . $parsedHost['port']; |
|
107 | + |
|
108 | + $sanitizedHosts[] = $safeHost; |
|
109 | + } |
|
110 | + |
|
111 | + $result['elastic_host'] = $sanitizedHosts; |
|
112 | + |
|
113 | + return $result; |
|
114 | + } |
|
115 | + |
|
116 | + |
|
117 | + /** |
|
118 | + * @param IRunner $runner |
|
119 | + */ |
|
120 | + public function setRunner(IRunner $runner) { |
|
121 | + $this->runner = $runner; |
|
122 | + } |
|
123 | + |
|
124 | + |
|
125 | + /** |
|
126 | + * Called when loading the platform. |
|
127 | + * |
|
128 | + * Loading some container and connect to ElasticSearch. |
|
129 | + * |
|
130 | + * @throws ConfigurationException |
|
131 | + * @throws Exception |
|
132 | + */ |
|
133 | + public function loadPlatform() { |
|
134 | + try { |
|
135 | + $this->connectToElastic($this->configService->getElasticHost()); |
|
136 | + } catch (ConfigurationException $e) { |
|
137 | + throw $e; |
|
138 | + } |
|
139 | + } |
|
140 | + |
|
141 | + |
|
142 | + /** |
|
143 | + * not used yet. |
|
144 | + * |
|
145 | + * @return bool |
|
146 | + */ |
|
147 | + public function testPlatform(): bool { |
|
148 | + $ping = $this->getClient()->ping(); |
|
149 | + return $ping->asBool(); |
|
150 | + } |
|
151 | + |
|
152 | + |
|
153 | + /** |
|
154 | + * called before any index |
|
155 | + * |
|
156 | + * We create a general index. |
|
157 | + * |
|
158 | + * @throws ConfigurationException |
|
159 | + * @throws BadRequest400Exception |
|
160 | + */ |
|
161 | + public function initializeIndex() { |
|
162 | + $this->indexService->initializeIndex($this->getClient()); |
|
163 | + } |
|
164 | + |
|
165 | + |
|
166 | + /** |
|
167 | + * resetIndex(); |
|
168 | + * |
|
169 | + * Called when admin wants to remove an index specific to a $provider. |
|
170 | + * $provider can be null, meaning a reset of the whole index. |
|
171 | + * |
|
172 | + * @param string $providerId |
|
173 | + * |
|
174 | + * @throws ConfigurationException |
|
175 | + */ |
|
176 | + public function resetIndex(string $providerId) { |
|
177 | + if ($providerId === 'all') { |
|
178 | + $this->indexService->resetIndexAll($this->getClient()); |
|
179 | + } else { |
|
180 | + $this->indexService->resetIndex($this->getClient(), $providerId); |
|
181 | + } |
|
182 | + } |
|
183 | + |
|
184 | + |
|
185 | + /** |
|
186 | + * @param IIndexDocument $document |
|
187 | + * |
|
188 | + * @return IIndex |
|
189 | + */ |
|
190 | + public function indexDocument(IIndexDocument $document): IIndex { |
|
191 | + $document->initHash(); |
|
192 | + |
|
193 | + $index = null; |
|
194 | + try { |
|
195 | + $result = $this->indexService->indexDocument($this->getClient(), $document); |
|
196 | + $index = $this->indexService->parseIndexResult($document->getIndex(), $result); |
|
197 | + |
|
198 | + $this->updateNewIndexResult( |
|
199 | + $document->getIndex(), json_encode($result), 'ok', |
|
200 | + IRunner::RESULT_TYPE_SUCCESS |
|
201 | + ); |
|
202 | + |
|
203 | + return $index; |
|
204 | + } catch (Exception $e) { |
|
205 | + $this->manageIndexErrorException($document, $e); |
|
206 | + } |
|
207 | + |
|
208 | + try { |
|
209 | + $result = $this->indexDocumentError($document, $e); |
|
210 | + $index = $this->indexService->parseIndexResult($document->getIndex(), $result); |
|
211 | + |
|
212 | + $this->updateNewIndexResult( |
|
213 | + $document->getIndex(), json_encode($result), 'ok', |
|
214 | + IRunner::RESULT_TYPE_WARNING |
|
215 | + ); |
|
216 | + |
|
217 | + return $index; |
|
218 | + } catch (Exception $e) { |
|
219 | + $this->updateNewIndexResult( |
|
220 | + $document->getIndex(), '', 'fail', |
|
221 | + IRunner::RESULT_TYPE_FAIL |
|
222 | + ); |
|
223 | + $this->manageIndexErrorException($document, $e); |
|
224 | + } |
|
225 | + |
|
226 | + return $document->getIndex(); |
|
227 | + } |
|
228 | + |
|
229 | + |
|
230 | + /** |
|
231 | + * @param IIndexDocument $document |
|
232 | + * @param Exception $e |
|
233 | + * |
|
234 | + * @return array |
|
235 | + * @throws AccessIsEmptyException |
|
236 | + * @throws ConfigurationException |
|
237 | + * @throws Exception |
|
238 | + */ |
|
239 | + private function indexDocumentError(IIndexDocument $document, Exception $e): array { |
|
240 | + |
|
241 | + $this->updateRunnerAction('indexDocumentWithoutContent', true); |
|
242 | + |
|
243 | + $document->setContent(''); |
|
244 | 244 | // $index = $document->getIndex(); |
245 | 245 | // $index->unsetStatus(Index::INDEX_CONTENT); |
246 | 246 | |
247 | - return $this->indexService->indexDocument($this->getClient(), $document); |
|
248 | - } |
|
249 | - |
|
250 | - |
|
251 | - /** |
|
252 | - * @param IIndexDocument $document |
|
253 | - * @param Exception $e |
|
254 | - */ |
|
255 | - private function manageIndexErrorException(IIndexDocument $document, Exception $e) { |
|
256 | - [$level, $message, $status] = $this->parseIndexErrorException($e); |
|
257 | - switch ($level) { |
|
258 | - case 'error': |
|
259 | - $document->getIndex() |
|
260 | - ->addError($message, get_class($e), IIndex::ERROR_SEV_3); |
|
261 | - $this->updateNewIndexError( |
|
262 | - $document->getIndex(), $message, get_class($e), IIndex::ERROR_SEV_3 |
|
263 | - ); |
|
264 | - break; |
|
265 | - |
|
266 | - case 'notice': |
|
267 | - $this->updateNewIndexResult( |
|
268 | - $document->getIndex(), |
|
269 | - $message, |
|
270 | - $status, |
|
271 | - IRunner::RESULT_TYPE_WARNING |
|
272 | - ); |
|
273 | - break; |
|
274 | - } |
|
275 | - |
|
276 | - } |
|
277 | - |
|
278 | - |
|
279 | - /** |
|
280 | - * @param Exception $e |
|
281 | - * |
|
282 | - * @return array |
|
283 | - */ |
|
284 | - private function parseIndexErrorException(Exception $e): array { |
|
285 | - $arr = json_decode($e->getMessage(), true); |
|
286 | - if (!is_array($arr)) { |
|
287 | - return ['error', 'unknown error', '']; |
|
288 | - } |
|
289 | - |
|
290 | - if (empty($this->getArray('error', $arr))) { |
|
291 | - return ['error', $e->getMessage(), '']; |
|
292 | - } |
|
293 | - |
|
294 | - try { |
|
295 | - return $this->parseCausedBy($arr['error']); |
|
296 | - } catch (InvalidArgumentException $e) { |
|
297 | - } |
|
298 | - |
|
299 | - $cause = $this->getArray('error.root_cause', $arr); |
|
300 | - if (!empty($cause) && $this->get('reason', $cause[0]) !== '') { |
|
301 | - return ['error', $this->get('reason', $cause[0]), $this->get('type', $cause[0])]; |
|
302 | - } |
|
303 | - |
|
304 | - return ['error', $e->getMessage(), '']; |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * @throws InvalidArgumentException |
|
309 | - */ |
|
310 | - private function parseCausedBy(array $error): array { |
|
311 | - $causedBy = $this->getArray('caused_by.caused_by', $error); |
|
312 | - if (empty($causedBy)) { |
|
313 | - $causedBy = $this->getArray('caused_by', $error); |
|
314 | - } |
|
315 | - |
|
316 | - if (empty($causedBy)) { |
|
317 | - if ($this->get('reason', $error) === '') { |
|
318 | - throw new InvalidArgumentException('Unable to parse given response structure'); |
|
319 | - } |
|
320 | - |
|
321 | - return ['error', $this->get('reason', $error), $this->get('type', $error)]; |
|
322 | - } |
|
323 | - |
|
324 | - $warnings = [ |
|
325 | - 'encrypted_document_exception', |
|
326 | - 'invalid_password_exception' |
|
327 | - ]; |
|
328 | - |
|
329 | - $level = 'error'; |
|
330 | - if (in_array($this->get('type', $causedBy), $warnings)) { |
|
331 | - $level = 'notice'; |
|
332 | - } |
|
333 | - |
|
334 | - return [$level, $this->get('reason', $causedBy), $this->get('type', $causedBy)]; |
|
335 | - } |
|
336 | - |
|
337 | - |
|
338 | - /** |
|
339 | - * {@inheritdoc} |
|
340 | - */ |
|
341 | - public function deleteIndexes(array $indexes) { |
|
342 | - foreach ($indexes as $index) { |
|
343 | - try { |
|
344 | - $this->indexService->deleteIndex($this->getClient(), $index); |
|
345 | - $this->updateNewIndexResult($index, 'index deleted', 'success', IRunner::RESULT_TYPE_SUCCESS); |
|
346 | - } catch (Exception $e) { |
|
347 | - $this->updateNewIndexResult( |
|
348 | - $index, 'index not deleted', 'issue while deleting index', IRunner::RESULT_TYPE_WARNING |
|
349 | - ); |
|
350 | - } |
|
351 | - } |
|
352 | - } |
|
353 | - |
|
354 | - |
|
355 | - /** |
|
356 | - * {@inheritdoc} |
|
357 | - * @throws Exception |
|
358 | - */ |
|
359 | - public function searchRequest(ISearchResult $result, IDocumentAccess $access) { |
|
360 | - $this->searchService->searchRequest($this->getClient(), $result, $access); |
|
361 | - } |
|
362 | - |
|
363 | - |
|
364 | - /** |
|
365 | - * @param string $providerId |
|
366 | - * @param string $documentId |
|
367 | - * |
|
368 | - * @return IIndexDocument |
|
369 | - * @throws ConfigurationException |
|
370 | - */ |
|
371 | - public function getDocument(string $providerId, string $documentId): IIndexDocument { |
|
372 | - return $this->searchService->getDocument($this->getClient(), $providerId, $documentId); |
|
373 | - } |
|
374 | - |
|
375 | - |
|
376 | - private function cleanHost(string $host): string { |
|
377 | - if ($host === '/') { |
|
378 | - return $host; |
|
379 | - } |
|
380 | - |
|
381 | - return trim(rtrim($host, '/')); |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * @param array $hosts |
|
386 | - * |
|
387 | - * @throws Exception |
|
388 | - */ |
|
389 | - private function connectToElastic(array $hosts) { |
|
390 | - |
|
391 | - try { |
|
392 | - $hosts = array_map([$this, 'cleanHost'], $hosts); |
|
393 | - $cb = ClientBuilder::create() |
|
394 | - ->setHosts($hosts) |
|
395 | - ->setRetries(3); |
|
396 | - $this->configureAuthentication($cb, $hosts); |
|
397 | - |
|
398 | - $this->client = $cb->build(); |
|
399 | - } catch (Exception $e) { |
|
400 | - throw $e; |
|
401 | - } |
|
402 | - } |
|
403 | - |
|
404 | - /** |
|
405 | - * setBasicAuthentication() on ClientBuilder if available, using list of hosts |
|
406 | - */ |
|
407 | - private function configureAuthentication(ClientBuilder $cb, array $hosts): void { |
|
408 | - foreach ($hosts as $host) { |
|
409 | - $user = parse_url($host, PHP_URL_USER) ?? ''; |
|
410 | - $pass = parse_url($host, PHP_URL_PASS) ?? ''; |
|
411 | - |
|
412 | - if ($user !== '' || $pass !== '') { |
|
413 | - $cb->setBasicAuthentication($user, $pass); |
|
414 | - return; |
|
415 | - } |
|
416 | - } |
|
417 | - } |
|
418 | - |
|
419 | - |
|
420 | - /** |
|
421 | - * @param string $action |
|
422 | - * @param bool $force |
|
423 | - * |
|
424 | - * @throws Exception |
|
425 | - */ |
|
426 | - private function updateRunnerAction(string $action, bool $force = false) { |
|
427 | - if ($this->runner === null) { |
|
428 | - return; |
|
429 | - } |
|
430 | - |
|
431 | - $this->runner->updateAction($action, $force); |
|
432 | - } |
|
433 | - |
|
434 | - |
|
435 | - /** |
|
436 | - * @param IIndex $index |
|
437 | - * @param string $message |
|
438 | - * @param string $exception |
|
439 | - * @param int $sev |
|
440 | - */ |
|
441 | - private function updateNewIndexError(IIndex $index, string $message, string $exception, int $sev |
|
442 | - ) { |
|
443 | - if ($this->runner === null) { |
|
444 | - return; |
|
445 | - } |
|
446 | - |
|
447 | - $this->runner->newIndexError($index, $message, $exception, $sev); |
|
448 | - } |
|
449 | - |
|
450 | - |
|
451 | - /** |
|
452 | - * @param IIndex $index |
|
453 | - * @param string $message |
|
454 | - * @param string $status |
|
455 | - * @param int $type |
|
456 | - */ |
|
457 | - private function updateNewIndexResult(IIndex $index, string $message, string $status, int $type) { |
|
458 | - if ($this->runner === null) { |
|
459 | - return; |
|
460 | - } |
|
461 | - |
|
462 | - $this->runner->newIndexResult($index, $message, $status, $type); |
|
463 | - } |
|
464 | - |
|
465 | - |
|
466 | - /** |
|
467 | - * @return Client |
|
468 | - * @throws ClientException |
|
469 | - */ |
|
470 | - private function getClient(): Client { |
|
471 | - if ($this->client === null) { |
|
472 | - throw new ClientException('platform not loaded'); |
|
473 | - } |
|
474 | - |
|
475 | - return $this->client; |
|
476 | - } |
|
247 | + return $this->indexService->indexDocument($this->getClient(), $document); |
|
248 | + } |
|
249 | + |
|
250 | + |
|
251 | + /** |
|
252 | + * @param IIndexDocument $document |
|
253 | + * @param Exception $e |
|
254 | + */ |
|
255 | + private function manageIndexErrorException(IIndexDocument $document, Exception $e) { |
|
256 | + [$level, $message, $status] = $this->parseIndexErrorException($e); |
|
257 | + switch ($level) { |
|
258 | + case 'error': |
|
259 | + $document->getIndex() |
|
260 | + ->addError($message, get_class($e), IIndex::ERROR_SEV_3); |
|
261 | + $this->updateNewIndexError( |
|
262 | + $document->getIndex(), $message, get_class($e), IIndex::ERROR_SEV_3 |
|
263 | + ); |
|
264 | + break; |
|
265 | + |
|
266 | + case 'notice': |
|
267 | + $this->updateNewIndexResult( |
|
268 | + $document->getIndex(), |
|
269 | + $message, |
|
270 | + $status, |
|
271 | + IRunner::RESULT_TYPE_WARNING |
|
272 | + ); |
|
273 | + break; |
|
274 | + } |
|
275 | + |
|
276 | + } |
|
277 | + |
|
278 | + |
|
279 | + /** |
|
280 | + * @param Exception $e |
|
281 | + * |
|
282 | + * @return array |
|
283 | + */ |
|
284 | + private function parseIndexErrorException(Exception $e): array { |
|
285 | + $arr = json_decode($e->getMessage(), true); |
|
286 | + if (!is_array($arr)) { |
|
287 | + return ['error', 'unknown error', '']; |
|
288 | + } |
|
289 | + |
|
290 | + if (empty($this->getArray('error', $arr))) { |
|
291 | + return ['error', $e->getMessage(), '']; |
|
292 | + } |
|
293 | + |
|
294 | + try { |
|
295 | + return $this->parseCausedBy($arr['error']); |
|
296 | + } catch (InvalidArgumentException $e) { |
|
297 | + } |
|
298 | + |
|
299 | + $cause = $this->getArray('error.root_cause', $arr); |
|
300 | + if (!empty($cause) && $this->get('reason', $cause[0]) !== '') { |
|
301 | + return ['error', $this->get('reason', $cause[0]), $this->get('type', $cause[0])]; |
|
302 | + } |
|
303 | + |
|
304 | + return ['error', $e->getMessage(), '']; |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * @throws InvalidArgumentException |
|
309 | + */ |
|
310 | + private function parseCausedBy(array $error): array { |
|
311 | + $causedBy = $this->getArray('caused_by.caused_by', $error); |
|
312 | + if (empty($causedBy)) { |
|
313 | + $causedBy = $this->getArray('caused_by', $error); |
|
314 | + } |
|
315 | + |
|
316 | + if (empty($causedBy)) { |
|
317 | + if ($this->get('reason', $error) === '') { |
|
318 | + throw new InvalidArgumentException('Unable to parse given response structure'); |
|
319 | + } |
|
320 | + |
|
321 | + return ['error', $this->get('reason', $error), $this->get('type', $error)]; |
|
322 | + } |
|
323 | + |
|
324 | + $warnings = [ |
|
325 | + 'encrypted_document_exception', |
|
326 | + 'invalid_password_exception' |
|
327 | + ]; |
|
328 | + |
|
329 | + $level = 'error'; |
|
330 | + if (in_array($this->get('type', $causedBy), $warnings)) { |
|
331 | + $level = 'notice'; |
|
332 | + } |
|
333 | + |
|
334 | + return [$level, $this->get('reason', $causedBy), $this->get('type', $causedBy)]; |
|
335 | + } |
|
336 | + |
|
337 | + |
|
338 | + /** |
|
339 | + * {@inheritdoc} |
|
340 | + */ |
|
341 | + public function deleteIndexes(array $indexes) { |
|
342 | + foreach ($indexes as $index) { |
|
343 | + try { |
|
344 | + $this->indexService->deleteIndex($this->getClient(), $index); |
|
345 | + $this->updateNewIndexResult($index, 'index deleted', 'success', IRunner::RESULT_TYPE_SUCCESS); |
|
346 | + } catch (Exception $e) { |
|
347 | + $this->updateNewIndexResult( |
|
348 | + $index, 'index not deleted', 'issue while deleting index', IRunner::RESULT_TYPE_WARNING |
|
349 | + ); |
|
350 | + } |
|
351 | + } |
|
352 | + } |
|
353 | + |
|
354 | + |
|
355 | + /** |
|
356 | + * {@inheritdoc} |
|
357 | + * @throws Exception |
|
358 | + */ |
|
359 | + public function searchRequest(ISearchResult $result, IDocumentAccess $access) { |
|
360 | + $this->searchService->searchRequest($this->getClient(), $result, $access); |
|
361 | + } |
|
362 | + |
|
363 | + |
|
364 | + /** |
|
365 | + * @param string $providerId |
|
366 | + * @param string $documentId |
|
367 | + * |
|
368 | + * @return IIndexDocument |
|
369 | + * @throws ConfigurationException |
|
370 | + */ |
|
371 | + public function getDocument(string $providerId, string $documentId): IIndexDocument { |
|
372 | + return $this->searchService->getDocument($this->getClient(), $providerId, $documentId); |
|
373 | + } |
|
374 | + |
|
375 | + |
|
376 | + private function cleanHost(string $host): string { |
|
377 | + if ($host === '/') { |
|
378 | + return $host; |
|
379 | + } |
|
380 | + |
|
381 | + return trim(rtrim($host, '/')); |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * @param array $hosts |
|
386 | + * |
|
387 | + * @throws Exception |
|
388 | + */ |
|
389 | + private function connectToElastic(array $hosts) { |
|
390 | + |
|
391 | + try { |
|
392 | + $hosts = array_map([$this, 'cleanHost'], $hosts); |
|
393 | + $cb = ClientBuilder::create() |
|
394 | + ->setHosts($hosts) |
|
395 | + ->setRetries(3); |
|
396 | + $this->configureAuthentication($cb, $hosts); |
|
397 | + |
|
398 | + $this->client = $cb->build(); |
|
399 | + } catch (Exception $e) { |
|
400 | + throw $e; |
|
401 | + } |
|
402 | + } |
|
403 | + |
|
404 | + /** |
|
405 | + * setBasicAuthentication() on ClientBuilder if available, using list of hosts |
|
406 | + */ |
|
407 | + private function configureAuthentication(ClientBuilder $cb, array $hosts): void { |
|
408 | + foreach ($hosts as $host) { |
|
409 | + $user = parse_url($host, PHP_URL_USER) ?? ''; |
|
410 | + $pass = parse_url($host, PHP_URL_PASS) ?? ''; |
|
411 | + |
|
412 | + if ($user !== '' || $pass !== '') { |
|
413 | + $cb->setBasicAuthentication($user, $pass); |
|
414 | + return; |
|
415 | + } |
|
416 | + } |
|
417 | + } |
|
418 | + |
|
419 | + |
|
420 | + /** |
|
421 | + * @param string $action |
|
422 | + * @param bool $force |
|
423 | + * |
|
424 | + * @throws Exception |
|
425 | + */ |
|
426 | + private function updateRunnerAction(string $action, bool $force = false) { |
|
427 | + if ($this->runner === null) { |
|
428 | + return; |
|
429 | + } |
|
430 | + |
|
431 | + $this->runner->updateAction($action, $force); |
|
432 | + } |
|
433 | + |
|
434 | + |
|
435 | + /** |
|
436 | + * @param IIndex $index |
|
437 | + * @param string $message |
|
438 | + * @param string $exception |
|
439 | + * @param int $sev |
|
440 | + */ |
|
441 | + private function updateNewIndexError(IIndex $index, string $message, string $exception, int $sev |
|
442 | + ) { |
|
443 | + if ($this->runner === null) { |
|
444 | + return; |
|
445 | + } |
|
446 | + |
|
447 | + $this->runner->newIndexError($index, $message, $exception, $sev); |
|
448 | + } |
|
449 | + |
|
450 | + |
|
451 | + /** |
|
452 | + * @param IIndex $index |
|
453 | + * @param string $message |
|
454 | + * @param string $status |
|
455 | + * @param int $type |
|
456 | + */ |
|
457 | + private function updateNewIndexResult(IIndex $index, string $message, string $status, int $type) { |
|
458 | + if ($this->runner === null) { |
|
459 | + return; |
|
460 | + } |
|
461 | + |
|
462 | + $this->runner->newIndexResult($index, $message, $status, $type); |
|
463 | + } |
|
464 | + |
|
465 | + |
|
466 | + /** |
|
467 | + * @return Client |
|
468 | + * @throws ClientException |
|
469 | + */ |
|
470 | + private function getClient(): Client { |
|
471 | + if ($this->client === null) { |
|
472 | + throw new ClientException('platform not loaded'); |
|
473 | + } |
|
474 | + |
|
475 | + return $this->client; |
|
476 | + } |
|
477 | 477 | } |
@@ -98,12 +98,12 @@ |
||
98 | 98 | $hosts = $this->configService->getElasticHost(); |
99 | 99 | foreach ($hosts as $host) { |
100 | 100 | $parsedHost = parse_url($host); |
101 | - $safeHost = $parsedHost['scheme'] . '://'; |
|
101 | + $safeHost = $parsedHost['scheme'].'://'; |
|
102 | 102 | if (array_key_exists('user', $parsedHost)) { |
103 | - $safeHost .= $parsedHost['user'] . ':' . '********' . '@'; |
|
103 | + $safeHost .= $parsedHost['user'].':'.'********'.'@'; |
|
104 | 104 | } |
105 | 105 | $safeHost .= $parsedHost['host']; |
106 | - $safeHost .= ':' . $parsedHost['port']; |
|
106 | + $safeHost .= ':'.$parsedHost['port']; |
|
107 | 107 | |
108 | 108 | $sanitizedHosts[] = $safeHost; |
109 | 109 | } |