1 | <?php |
||||
2 | namespace ApacheSolrForTypo3\Solr\IndexQueue; |
||||
3 | |||||
4 | /*************************************************************** |
||||
5 | * Copyright notice |
||||
6 | * |
||||
7 | * (c) 2009-2015 Ingo Renner <[email protected]> |
||||
8 | * All rights reserved |
||||
9 | * |
||||
10 | * This script is part of the TYPO3 project. The TYPO3 project is |
||||
11 | * free software; you can redistribute it and/or modify |
||||
12 | * it under the terms of the GNU General Public License as published by |
||||
13 | * the Free Software Foundation; either version 3 of the License, or |
||||
14 | * (at your option) any later version. |
||||
15 | * |
||||
16 | * The GNU General Public License can be found at |
||||
17 | * http://www.gnu.org/copyleft/gpl.html. |
||||
18 | * |
||||
19 | * This script is distributed in the hope that it will be useful, |
||||
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
22 | * GNU General Public License for more details. |
||||
23 | * |
||||
24 | * This copyright notice MUST APPEAR in all copies of the script! |
||||
25 | ***************************************************************/ |
||||
26 | |||||
27 | use ApacheSolrForTypo3\Solr\ConnectionManager; |
||||
28 | use ApacheSolrForTypo3\Solr\Domain\Search\ApacheSolrDocument\Builder; |
||||
29 | use ApacheSolrForTypo3\Solr\FieldProcessor\Service; |
||||
30 | use ApacheSolrForTypo3\Solr\FrontendEnvironment; |
||||
31 | use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException; |
||||
32 | use ApacheSolrForTypo3\Solr\Domain\Site\Site; |
||||
33 | use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager; |
||||
34 | use ApacheSolrForTypo3\Solr\System\Records\Pages\PagesRepository; |
||||
35 | use ApacheSolrForTypo3\Solr\System\Solr\Document\Document; |
||||
36 | use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter; |
||||
37 | use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection; |
||||
38 | use Solarium\Exception\HttpException; |
||||
39 | use TYPO3\CMS\Core\Context\LanguageAspectFactory; |
||||
40 | use TYPO3\CMS\Core\Exception\SiteNotFoundException; |
||||
41 | use TYPO3\CMS\Core\Site\SiteFinder; |
||||
42 | use TYPO3\CMS\Core\Utility\GeneralUtility; |
||||
43 | use TYPO3\CMS\Core\Utility\RootlineUtility; |
||||
44 | use TYPO3\CMS\Frontend\Page\PageRepository; |
||||
45 | |||||
46 | /** |
||||
47 | * A general purpose indexer to be used for indexing of any kind of regular |
||||
48 | * records like tt_news, tt_address, and so on. |
||||
49 | * Specialized indexers can extend this class to handle advanced stuff like |
||||
50 | * category resolution in tt_news or file indexing. |
||||
51 | * |
||||
52 | * @author Ingo Renner <[email protected]> |
||||
53 | */ |
||||
54 | class Indexer extends AbstractIndexer |
||||
55 | { |
||||
56 | |||||
57 | # TODO change to singular $document instead of plural $documents |
||||
58 | |||||
59 | /** |
||||
60 | * A Solr service instance to interact with the Solr server |
||||
61 | * |
||||
62 | * @var SolrConnection |
||||
63 | */ |
||||
64 | protected $solr; |
||||
65 | |||||
66 | /** |
||||
67 | * @var ConnectionManager |
||||
68 | */ |
||||
69 | protected $connectionManager; |
||||
70 | |||||
71 | /** |
||||
72 | * Holds options for a specific indexer |
||||
73 | * |
||||
74 | * @var array |
||||
75 | */ |
||||
76 | protected $options = []; |
||||
77 | |||||
78 | /** |
||||
79 | * To log or not to log... #Shakespeare |
||||
80 | * |
||||
81 | * @var bool |
||||
82 | */ |
||||
83 | protected $loggingEnabled = false; |
||||
84 | |||||
85 | /** |
||||
86 | * @var SolrLogManager |
||||
87 | */ |
||||
88 | protected $logger = null; |
||||
89 | |||||
90 | /** |
||||
91 | * @var PagesRepository |
||||
92 | */ |
||||
93 | protected $pagesRepository; |
||||
94 | |||||
95 | /** |
||||
96 | * @var Builder |
||||
97 | */ |
||||
98 | protected $documentBuilder; |
||||
99 | |||||
100 | /** |
||||
101 | * @var FrontendEnvironment |
||||
102 | */ |
||||
103 | protected $frontendEnvironment = null; |
||||
104 | |||||
105 | 36 | /** |
|||
106 | * Constructor |
||||
107 | 36 | * |
|||
108 | 36 | * @param array $options array of indexer options |
|||
109 | 36 | * @param PagesRepository|null $pagesRepository |
|||
110 | 36 | * @param Builder|null $documentBuilder |
|||
111 | 36 | * @param SolrLogManager|null $logger |
|||
112 | 36 | * @param ConnectionManager|null $connectionManager |
|||
113 | * @param FrontendEnvironment|null $frontendEnvironment |
||||
114 | */ |
||||
115 | public function __construct( |
||||
116 | array $options = [], |
||||
117 | PagesRepository $pagesRepository = null, |
||||
118 | Builder $documentBuilder = null, |
||||
119 | SolrLogManager $logger = null, |
||||
120 | 20 | ConnectionManager $connectionManager = null, |
|||
121 | FrontendEnvironment $frontendEnvironment = null |
||||
122 | 20 | ) |
|||
123 | { |
||||
124 | 20 | $this->options = $options; |
|||
125 | 20 | $this->pagesRepository = $pagesRepository ?? GeneralUtility::makeInstance(PagesRepository::class); |
|||
126 | $this->documentBuilder = $documentBuilder ?? GeneralUtility::makeInstance(Builder::class); |
||||
127 | 20 | $this->logger = $logger ?? GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__); |
|||
128 | 20 | $this->connectionManager = $connectionManager ?? GeneralUtility::makeInstance(ConnectionManager::class); |
|||
129 | 20 | $this->frontendEnvironment = $frontendEnvironment ?? GeneralUtility::makeInstance(FrontendEnvironment::class); |
|||
130 | } |
||||
131 | 20 | ||||
132 | /** |
||||
133 | * Indexes an item from the indexing queue. |
||||
134 | * |
||||
135 | * @param Item $item An index queue item |
||||
136 | * @return bool returns true when indexed, false when not |
||||
137 | */ |
||||
138 | public function index(Item $item) |
||||
139 | { |
||||
140 | 20 | $indexed = true; |
|||
141 | |||||
142 | $this->type = $item->getType(); |
||||
143 | $this->setLogging($item); |
||||
144 | 20 | ||||
145 | $solrConnections = $this->getSolrConnectionsByItem($item); |
||||
146 | foreach ($solrConnections as $systemLanguageUid => $solrConnection) { |
||||
147 | $this->solr = $solrConnection; |
||||
148 | |||||
149 | if (!$this->indexItem($item, $systemLanguageUid)) { |
||||
150 | /* |
||||
151 | * A single language voting for "not indexed" should make the whole |
||||
152 | * item count as being not indexed, even if all other languages are |
||||
153 | * indexed. |
||||
154 | 20 | * If there is no translation for a single language, this item counts |
|||
155 | * as TRUE since it's not an error which that should make the item |
||||
156 | 20 | * being reindexed during another index run. |
|||
157 | 20 | */ |
|||
158 | $indexed = false; |
||||
159 | 20 | } |
|||
160 | 20 | } |
|||
161 | |||||
162 | return $indexed; |
||||
163 | } |
||||
164 | |||||
165 | /** |
||||
166 | * Creates a single Solr Document for an item in a specific language. |
||||
167 | 1 | * |
|||
168 | * @param Item $item An index queue item to index. |
||||
169 | * @param int $language The language to use. |
||||
170 | 20 | * @return bool TRUE if item was indexed successfully, FALSE on failure |
|||
171 | 20 | */ |
|||
172 | 20 | protected function indexItem(Item $item, $language = 0) |
|||
173 | 20 | { |
|||
174 | $itemIndexed = false; |
||||
175 | 20 | $documents = []; |
|||
176 | 20 | ||||
177 | 20 | $itemDocument = $this->itemToDocument($item, $language); |
|||
178 | if (is_null($itemDocument)) { |
||||
179 | /* |
||||
180 | 20 | * If there is no itemDocument, this means there was no translation |
|||
181 | * for this record. This should not stop the current item to count as |
||||
182 | 20 | * being valid because not-indexing not-translated items is perfectly |
|||
183 | * fine. |
||||
184 | */ |
||||
185 | return true; |
||||
186 | } |
||||
187 | |||||
188 | $documents[] = $itemDocument; |
||||
189 | $documents = array_merge($documents, $this->getAdditionalDocuments($item, $language, $itemDocument)); |
||||
190 | $documents = $this->processDocuments($item, $documents); |
||||
191 | $documents = $this->preAddModifyDocuments($item, $language, $documents); |
||||
192 | |||||
193 | try { |
||||
194 | $response = $this->solr->getWriteService()->addDocuments($documents); |
||||
195 | if ($response->getHttpStatus() == 200) { |
||||
196 | 20 | $itemIndexed = true; |
|||
197 | } |
||||
198 | 20 | } catch (HttpException $e) { |
|||
199 | $response = new ResponseAdapter($e->getBody(), $httpStatus = 500, $e->getStatusMessage()); |
||||
200 | 20 | } |
|||
201 | 20 | ||||
202 | $this->log($item, $documents, $response); |
||||
203 | |||||
204 | return $itemIndexed; |
||||
205 | } |
||||
206 | |||||
207 | /** |
||||
208 | 20 | * Gets the full item record. |
|||
209 | 20 | * |
|||
210 | * This general record indexer simply gets the record from the item. Other |
||||
211 | * more specialized indexers may provide more data for their specific item |
||||
212 | * types. |
||||
213 | * |
||||
214 | * @param Item $item The item to be indexed |
||||
215 | * @param int $language Language Id (sys_language.uid) |
||||
216 | * @return array|NULL The full record with fields of data to be used for indexing or NULL to prevent an item from being indexed |
||||
217 | */ |
||||
218 | protected function getFullItemRecord(Item $item, $language = 0) |
||||
219 | { |
||||
220 | $itemRecord = $this->getItemRecordOverlayed($item, $language); |
||||
221 | |||||
222 | if (!is_null($itemRecord)) { |
||||
223 | $itemRecord['__solr_index_language'] = $language; |
||||
224 | 20 | } |
|||
225 | 20 | ||||
226 | 19 | return $itemRecord; |
|||
227 | } |
||||
228 | |||||
229 | 20 | /** |
|||
230 | 20 | * Returns the overlayed item record. |
|||
231 | 20 | * |
|||
232 | 20 | * @param Item $item |
|||
233 | 20 | * @param int $language |
|||
234 | 20 | * @param string|null $systemLanguageContentOverlay |
|||
235 | * @return array|mixed|null |
||||
236 | 1 | */ |
|||
237 | protected function getItemRecordOverlayed(Item $item, $language) |
||||
238 | { |
||||
239 | 20 | $itemRecord = $item->getRecord(); |
|||
240 | 20 | ||||
241 | if ($language > 0) { |
||||
242 | $page = GeneralUtility::makeInstance(PageRepository::class); |
||||
243 | 20 | $itemRecord = $page->getLanguageOverlay($item->getType(), $itemRecord); |
|||
244 | } |
||||
245 | |||||
246 | if (!$itemRecord) { |
||||
247 | $itemRecord = null; |
||||
248 | } |
||||
249 | |||||
250 | return $itemRecord; |
||||
251 | } |
||||
252 | |||||
253 | /** |
||||
254 | 20 | * Gets the configuration how to process an item's fields for indexing. |
|||
255 | * |
||||
256 | 20 | * @param Item $item An index queue item |
|||
257 | * @param int $language Language ID |
||||
258 | 20 | * @throws \RuntimeException |
|||
259 | 6 | * @return array Configuration array from TypoScript |
|||
260 | 6 | */ |
|||
261 | 6 | protected function getItemTypeConfiguration(Item $item, $language = 0) |
|||
262 | { |
||||
263 | $indexConfigurationName = $item->getIndexingConfigurationName(); |
||||
264 | 20 | $fields = $this->getFieldConfigurationFromItemRecordPage($item, $language, $indexConfigurationName); |
|||
265 | if (!$this->isRootPageIdPartOfRootLine($item) || count($fields) === 0) { |
||||
266 | $fields = $this->getFieldConfigurationFromItemRootPage($item, $language, $indexConfigurationName); |
||||
267 | if (count($fields) === 0) { |
||||
268 | 20 | throw new \RuntimeException('The item indexing configuration "' . $item->getIndexingConfigurationName() . |
|||
269 | '" on root page uid ' . $item->getRootPageUid() . ' could not be found!', 1455530112); |
||||
270 | } |
||||
271 | } |
||||
272 | |||||
273 | return $fields; |
||||
274 | } |
||||
275 | |||||
276 | /** |
||||
277 | * The method retrieves the field configuration of the items record page id (pid). |
||||
278 | * |
||||
279 | 20 | * @param Item $item |
|||
280 | * @param integer $language |
||||
281 | 20 | * @param string $indexConfigurationName |
|||
282 | 20 | * @return array |
|||
283 | 20 | */ |
|||
284 | 1 | protected function getFieldConfigurationFromItemRecordPage(Item $item, $language, $indexConfigurationName) |
|||
285 | 1 | { |
|||
286 | try { |
||||
287 | $pageId = $this->getPageIdOfItem($item); |
||||
288 | $solrConfiguration = $this->frontendEnvironment->getSolrConfigurationFromPageId($pageId, $language); |
||||
289 | return $solrConfiguration->getIndexQueueFieldsConfigurationByConfigurationName($indexConfigurationName, []); |
||||
290 | } catch (\Exception $e) { |
||||
291 | 20 | return []; |
|||
292 | } |
||||
293 | } |
||||
294 | |||||
295 | /** |
||||
296 | * @param Item $item |
||||
297 | * @return int |
||||
298 | */ |
||||
299 | protected function getPageIdOfItem(Item $item): int |
||||
300 | { |
||||
301 | if ($item->getType() === 'pages') { |
||||
302 | 20 | return (int)$item->getRecordUid(); |
|||
303 | } |
||||
304 | return (int)$item->getRecordPageId(); |
||||
305 | 20 | } |
|||
306 | 19 | ||||
307 | 1 | /** |
|||
308 | 1 | * The method returns the field configuration of the items root page id (uid of the related root page). |
|||
309 | * |
||||
310 | * @param Item $item |
||||
311 | * @param integer $language |
||||
312 | * @param string $indexConfigurationName |
||||
313 | * @return array |
||||
314 | */ |
||||
315 | protected function getFieldConfigurationFromItemRootPage(Item $item, $language, $indexConfigurationName) |
||||
316 | { |
||||
317 | $solrConfiguration = $this->frontendEnvironment->getSolrConfigurationFromPageId($item->getRootPageUid(), $language); |
||||
318 | if (empty($solrConfiguration->getIndexQueueAdditionalPageIdsByConfigurationName($indexConfigurationName))) { |
||||
319 | return []; |
||||
320 | 1 | } |
|||
321 | |||||
322 | 1 | return $solrConfiguration->getIndexQueueFieldsConfigurationByConfigurationName($indexConfigurationName, []); |
|||
323 | 1 | } |
|||
324 | |||||
325 | /** |
||||
326 | * In case of additionalStoragePid config recordPageId can be outsite of siteroot. |
||||
327 | 1 | * In that case we should not read TS config of foreign siteroot. |
|||
328 | * |
||||
329 | * @return bool |
||||
330 | */ |
||||
331 | protected function isRootPageIdPartOfRootLine(Item $item) |
||||
332 | { |
||||
333 | $rootPageId = $item->getRootPageUid(); |
||||
334 | $buildRootlineWithPid = $this->getPageIdOfItem($item); |
||||
335 | $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $buildRootlineWithPid); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
336 | $rootline = $rootlineUtility->get(); |
||||
337 | |||||
338 | 20 | $pageInRootline = array_filter($rootline, function($page) use ($rootPageId) { |
|||
339 | return (int)$page['uid'] === $rootPageId; |
||||
340 | 20 | }); |
|||
341 | return !empty($pageInRootline); |
||||
342 | 20 | } |
|||
343 | 20 | ||||
344 | 20 | /** |
|||
345 | 20 | * Converts an item array (record) to a Solr document by mapping the |
|||
346 | 20 | * record's fields onto Solr document fields as configured in TypoScript. |
|||
347 | * |
||||
348 | * @param Item $item An index queue item |
||||
349 | 20 | * @param int $language Language Id |
|||
350 | * @return Document The Solr document converted from the record |
||||
351 | */ |
||||
352 | protected function itemToDocument(Item $item, $language = 0) |
||||
353 | { |
||||
354 | $document = null; |
||||
355 | if ($item->getType() === 'pages') { |
||||
356 | $this->frontendEnvironment->initializeTsfe($item->getRecordUid(), $language); |
||||
357 | } else { |
||||
358 | $this->frontendEnvironment->initializeTsfe($item->getRootPageUid(), $language); |
||||
359 | 20 | } |
|||
360 | |||||
361 | 20 | $itemRecord = $this->getFullItemRecord($item, $language); |
|||
362 | 20 | if (!is_null($itemRecord)) { |
|||
363 | 20 | $itemIndexingConfiguration = $this->getItemTypeConfiguration($item, $language); |
|||
364 | 20 | $document = $this->getBaseDocument($item, $itemRecord); |
|||
365 | $document = $this->addDocumentFieldsFromTyposcript($document, $itemIndexingConfiguration, $itemRecord); |
||||
366 | } |
||||
367 | |||||
368 | return $document; |
||||
369 | } |
||||
370 | |||||
371 | /** |
||||
372 | * Creates a Solr document with the basic / core fields set already. |
||||
373 | 20 | * |
|||
374 | * @param Item $item The item to index |
||||
375 | 20 | * @param array $itemRecord The record to use to build the base document |
|||
376 | 20 | * @return Document A basic Solr document |
|||
377 | */ |
||||
378 | protected function getBaseDocument(Item $item, array $itemRecord) |
||||
379 | { |
||||
380 | 20 | $type = $item->getType(); |
|||
381 | 1 | $rootPageUid = $item->getRootPageUid(); |
|||
382 | $accessRootLine = $this->getAccessRootline($item); |
||||
383 | 1 | return $this->documentBuilder->fromRecord($itemRecord, $type, $rootPageUid, $accessRootLine); |
|||
384 | } |
||||
385 | 1 | ||||
386 | /** |
||||
387 | * Generates an Access Rootline for an item. |
||||
388 | * |
||||
389 | 20 | * @param Item $item Index Queue item to index. |
|||
390 | * @return string The Access Rootline for the item |
||||
391 | */ |
||||
392 | protected function getAccessRootline(Item $item) |
||||
393 | { |
||||
394 | $accessRestriction = '0'; |
||||
395 | $itemRecord = $item->getRecord(); |
||||
396 | |||||
397 | // TODO support access restrictions set on storage page |
||||
398 | |||||
399 | if (isset($GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['fe_group'])) { |
||||
400 | 20 | $accessRestriction = $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['fe_group']]; |
|||
401 | |||||
402 | if (empty($accessRestriction)) { |
||||
403 | 20 | // public |
|||
404 | 20 | $accessRestriction = '0'; |
|||
405 | } |
||||
406 | } |
||||
407 | 20 | ||||
408 | 20 | return 'r:' . $accessRestriction; |
|||
409 | 20 | } |
|||
410 | |||||
411 | /** |
||||
412 | 20 | * Sends the documents to the field processing service which takes care of |
|||
413 | * manipulating fields as defined in the field's configuration. |
||||
414 | * |
||||
415 | * @param Item $item An index queue item |
||||
416 | * @param array $documents An array of Apache_Solr_Document objects to manipulate. |
||||
417 | * @return Document[] array Array of manipulated Document objects. |
||||
418 | */ |
||||
419 | protected function processDocuments(Item $item, array $documents) |
||||
420 | { |
||||
421 | // needs to respect the TS settings for the page the item is on, conditions may apply |
||||
422 | $solrConfiguration = $this->frontendEnvironment->getSolrConfigurationFromPageId($item->getRootPageUid()); |
||||
423 | $fieldProcessingInstructions = $solrConfiguration->getIndexFieldProcessingInstructionsConfiguration(); |
||||
424 | 24 | ||||
425 | // same as in the FE indexer |
||||
426 | 24 | if (is_array($fieldProcessingInstructions)) { |
|||
0 ignored issues
–
show
|
|||||
427 | $service = GeneralUtility::makeInstance(Service::class); |
||||
428 | 24 | $service->processDocuments($documents, $fieldProcessingInstructions); |
|||
429 | 4 | } |
|||
430 | 4 | ||||
431 | 2 | return $documents; |
|||
432 | } |
||||
433 | 2 | ||||
434 | 2 | /** |
|||
435 | 1 | * Allows third party extensions to provide additional documents which |
|||
436 | * should be indexed for the current item. |
||||
437 | 1 | * |
|||
438 | 1 | * @param Item $item The item currently being indexed. |
|||
439 | 1 | * @param int $language The language uid currently being indexed. |
|||
440 | * @param Document $itemDocument The document representing the item for the given language. |
||||
441 | * @return Document[] array An array of additional Document objects to index. |
||||
442 | 1 | */ |
|||
443 | 1 | protected function getAdditionalDocuments(Item $item, $language, Document $itemDocument) |
|||
444 | 2 | { |
|||
445 | $documents = []; |
||||
446 | |||||
447 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['indexItemAddDocuments'])) { |
||||
448 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['indexItemAddDocuments'] as $classReference) { |
||||
449 | 21 | if (!class_exists($classReference)) { |
|||
450 | throw new \InvalidArgumentException('Class does not exits' . $classReference, 1490363487); |
||||
451 | } |
||||
452 | $additionalIndexer = GeneralUtility::makeInstance($classReference); |
||||
453 | if ($additionalIndexer instanceof AdditionalIndexQueueItemIndexer) { |
||||
454 | $additionalDocuments = $additionalIndexer->getAdditionalItemDocuments($item, $language, $itemDocument); |
||||
455 | |||||
456 | if (is_array($additionalDocuments)) { |
||||
457 | $documents = array_merge($documents, |
||||
458 | $additionalDocuments); |
||||
459 | } |
||||
460 | } else { |
||||
461 | 20 | throw new \UnexpectedValueException( |
|||
462 | get_class($additionalIndexer) . ' must implement interface ' . AdditionalIndexQueueItemIndexer::class, |
||||
463 | 20 | 1326284551 |
|||
464 | ); |
||||
465 | } |
||||
466 | } |
||||
467 | } |
||||
468 | return $documents; |
||||
469 | } |
||||
470 | |||||
471 | /** |
||||
472 | * Provides a hook to manipulate documents right before they get added to |
||||
473 | * the Solr index. |
||||
474 | * |
||||
475 | * @param Item $item The item currently being indexed. |
||||
476 | * @param int $language The language uid of the documents |
||||
477 | * @param array $documents An array of documents to be indexed |
||||
478 | * @return array An array of modified documents |
||||
479 | */ |
||||
480 | protected function preAddModifyDocuments(Item $item, $language, array $documents) |
||||
481 | 20 | { |
|||
482 | if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments'])) { |
||||
483 | foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments'] as $classReference) { |
||||
484 | $documentsModifier = GeneralUtility::makeInstance($classReference); |
||||
485 | |||||
486 | if ($documentsModifier instanceof PageIndexerDocumentsModifier) { |
||||
487 | $documents = $documentsModifier->modifyDocuments($item, $language, $documents); |
||||
488 | } else { |
||||
489 | throw new \RuntimeException( |
||||
490 | 'The class "' . get_class($documentsModifier) |
||||
491 | . '" registered as document modifier in hook |
||||
492 | preAddModifyDocuments must implement interface |
||||
493 | ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerDocumentsModifier', |
||||
494 | 1309522677 |
||||
495 | 22 | ); |
|||
496 | } |
||||
497 | 22 | } |
|||
498 | } |
||||
499 | 22 | ||||
500 | 22 | return $documents; |
|||
501 | 2 | } |
|||
502 | |||||
503 | // Initialization |
||||
504 | |||||
505 | 22 | /** |
|||
506 | * Gets the Solr connections applicable for an item. |
||||
507 | 22 | * |
|||
508 | 22 | * The connections include the default connection and connections to be used |
|||
509 | 22 | * for translations of an item. |
|||
510 | 22 | * |
|||
511 | * @param Item $item An index queue item |
||||
512 | * @return array An array of ApacheSolrForTypo3\Solr\System\Solr\SolrConnection connections, the array's keys are the sys_language_uid of the language of the connection |
||||
513 | 22 | */ |
|||
514 | 22 | protected function getSolrConnectionsByItem(Item $item) |
|||
515 | { |
||||
516 | 22 | $solrConnections = []; |
|||
517 | 22 | ||||
518 | $pageId = $item->getRootPageUid(); |
||||
519 | 22 | ||||
520 | 21 | // Solr configurations possible for this item |
|||
521 | $site = $item->getSite(); |
||||
522 | $solrConfigurationsBySite = $site->getAllSolrConnectionConfigurations(); |
||||
523 | 22 | $siteLanguages = []; |
|||
524 | 7 | foreach ($solrConfigurationsBySite as $solrConfiguration) { |
|||
525 | $siteLanguages[] = $solrConfiguration['language']; |
||||
526 | 22 | } |
|||
527 | |||||
528 | $defaultLanguageUid = $this->getDefaultLanguageUid($item, $site->getRootPage(), $siteLanguages); |
||||
529 | $translationOverlays = $this->getTranslationOverlaysWithConfiguredSite((int)$pageId, $site, (array)$siteLanguages); |
||||
530 | |||||
531 | $defaultConnection = $this->connectionManager->getConnectionByPageId($pageId, 0, $item->getMountPointIdentifier()); |
||||
532 | $translationConnections = $this->getConnectionsForIndexableLanguages($translationOverlays); |
||||
533 | |||||
534 | if ($defaultLanguageUid == 0) { |
||||
535 | $solrConnections[0] = $defaultConnection; |
||||
536 | } |
||||
537 | |||||
538 | 22 | foreach ($translationConnections as $systemLanguageUid => $solrConnection) { |
|||
539 | $solrConnections[$systemLanguageUid] = $solrConnection; |
||||
540 | 22 | } |
|||
541 | return $solrConnections; |
||||
542 | 22 | } |
|||
543 | 7 | ||||
544 | 7 | /** |
|||
545 | * @param int $pageId |
||||
546 | * @param Site $site |
||||
547 | * @param array $siteLanguages |
||||
548 | 22 | * @return array |
|||
549 | */ |
||||
550 | protected function getTranslationOverlaysWithConfiguredSite(int $pageId, Site $site, array $siteLanguages): array |
||||
551 | { |
||||
552 | $translationOverlays = $this->pagesRepository->findTranslationOverlaysByPageId($pageId); |
||||
553 | $translatedLanguages = []; |
||||
554 | foreach ($translationOverlays as $key => $translationOverlay) { |
||||
555 | if (!in_array($translationOverlay['sys_language_uid'], $siteLanguages)) { |
||||
556 | unset($translationOverlays[$key]); |
||||
557 | } else { |
||||
558 | $translatedLanguages[] = (int)$translationOverlay['sys_language_uid']; |
||||
559 | 22 | } |
|||
560 | } |
||||
561 | 22 | ||||
562 | 22 | if (count($translationOverlays) + 1 !== count($siteLanguages)) { |
|||
563 | 1 | // not all Languages are translated |
|||
564 | 1 | // add Language Fallback |
|||
565 | 21 | foreach ($siteLanguages as $languageId) { |
|||
566 | if ($languageId !== 0 && !in_array((int)$languageId, $translatedLanguages, true)) { |
||||
567 | $fallbackLanguageIds = $this->getFallbackOrder($site, (int)$languageId, (int)$pageId); |
||||
568 | foreach ($fallbackLanguageIds as $fallbackLanguageId) { |
||||
569 | if ($fallbackLanguageId === 0 || in_array((int)$fallbackLanguageId, $translatedLanguages, true)) { |
||||
570 | 22 | $translationOverlay = [ |
|||
571 | 'pid' => $pageId, |
||||
572 | 'sys_language_uid' => $languageId, |
||||
573 | 'l10n_parent' => $pageId |
||||
574 | ]; |
||||
575 | $translationOverlays[] = $translationOverlay; |
||||
576 | continue 2; |
||||
577 | } |
||||
578 | } |
||||
579 | } |
||||
580 | } |
||||
581 | } |
||||
582 | return $translationOverlays; |
||||
583 | } |
||||
584 | |||||
585 | /** |
||||
586 | * @param Site $site |
||||
587 | 22 | * @param int $languageId |
|||
588 | * @param int $pageId |
||||
589 | 22 | * @return array |
|||
590 | 22 | */ |
|||
591 | protected function getFallbackOrder(Site $site, int $languageId, int $pageId): array |
||||
0 ignored issues
–
show
The parameter
$pageId is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
592 | 22 | { |
|||
593 | 22 | $fallbackChain = []; |
|||
594 | 22 | $siteFinder = GeneralUtility::makeInstance(SiteFinder::class); |
|||
595 | 22 | try { |
|||
596 | $site = $siteFinder->getSiteByRootPageId($site->getRootPageId()); |
||||
597 | 22 | $languageAspect = LanguageAspectFactory::createFromSiteLanguage($site->getLanguageById($languageId)); |
|||
598 | 6 | $fallbackChain = $languageAspect->getFallbackChain(); |
|||
599 | } catch (SiteNotFoundException $e) { |
||||
0 ignored issues
–
show
Coding Style
Comprehensibility
introduced
by
|
|||||
600 | |||||
601 | 16 | } |
|||
602 | 16 | return $fallbackChain; |
|||
603 | 15 | } |
|||
604 | 15 | ||||
605 | |||||
606 | 1 | /** |
|||
607 | 1 | * @param Item $item An index queue item |
|||
608 | 1 | * @param array $rootPage |
|||
609 | 1 | * @param array $siteLanguages |
|||
610 | * |
||||
611 | * @return int |
||||
612 | * @throws \RuntimeException |
||||
613 | */ |
||||
614 | 22 | private function getDefaultLanguageUid(Item $item, array $rootPage, array $siteLanguages) |
|||
615 | { |
||||
616 | $defaultLanguageUid = 0; |
||||
617 | if (($rootPage['l18n_cfg'] & 1) == 1 && count($siteLanguages) > 1) { |
||||
618 | unset($siteLanguages[array_search('0', $siteLanguages)]); |
||||
619 | $defaultLanguageUid = $siteLanguages[min(array_keys($siteLanguages))]; |
||||
620 | } elseif (($rootPage['l18n_cfg'] & 1) == 1 && count($siteLanguages) == 1) { |
||||
621 | $message = 'Root page ' . (int)$item->getRootPageUid() . ' is set to hide default translation, but no other language is configured!'; |
||||
622 | 15 | throw new \RuntimeException($message); |
|||
623 | } |
||||
624 | 15 | ||||
625 | return $defaultLanguageUid; |
||||
626 | } |
||||
627 | |||||
628 | /** |
||||
629 | * Checks for which languages connections have been configured and returns |
||||
630 | * these connections. |
||||
631 | * |
||||
632 | * @param array $translationOverlays An array of translation overlays to check for configured connections. |
||||
633 | * @return array An array of ApacheSolrForTypo3\Solr\System\Solr\SolrConnection connections. |
||||
634 | 22 | */ |
|||
635 | protected function getConnectionsForIndexableLanguages(array $translationOverlays) |
||||
636 | 22 | { |
|||
637 | $connections = []; |
||||
638 | 22 | ||||
639 | foreach ($translationOverlays as $translationOverlay) { |
||||
640 | 7 | $pageId = $translationOverlay['l10n_parent']; |
|||
641 | 7 | $languageId = $translationOverlay['sys_language_uid']; |
|||
642 | |||||
643 | try { |
||||
644 | 7 | $connection = $this->connectionManager->getConnectionByPageId($pageId, $languageId); |
|||
645 | 7 | $connections[$languageId] = $connection; |
|||
646 | 7 | } catch (NoSolrConnectionFoundException $e) { |
|||
647 | // ignore the exception as we seek only those connections |
||||
648 | // actually available |
||||
649 | } |
||||
650 | } |
||||
651 | |||||
652 | 22 | return $connections; |
|||
653 | } |
||||
654 | |||||
655 | // Utility methods |
||||
656 | |||||
657 | // FIXME extract log() and setLogging() to ApacheSolrForTypo3\Solr\IndexQueue\AbstractIndexer |
||||
658 | // FIXME extract an interface Tx_Solr_IndexQueue_ItemInterface |
||||
659 | |||||
660 | /** |
||||
661 | * Enables logging dependent on the configuration of the item's site |
||||
662 | * |
||||
663 | * @param Item $item An item being indexed |
||||
664 | * @return void |
||||
665 | */ |
||||
666 | 21 | protected function setLogging(Item $item) |
|||
667 | { |
||||
668 | 21 | $solrConfiguration = $this->frontendEnvironment->getSolrConfigurationFromPageId($item->getRootPageUid()); |
|||
669 | 21 | $this->loggingEnabled = $solrConfiguration->getLoggingIndexingQueueOperationsByConfigurationNameWithFallBack( |
|||
670 | 21 | $item->getIndexingConfigurationName() |
|||
671 | ); |
||||
672 | 21 | } |
|||
673 | |||||
674 | /** |
||||
675 | * Logs the item and what document was created from it |
||||
676 | * |
||||
677 | * @param Item $item The item that is being indexed. |
||||
678 | * @param array $itemDocuments An array of Solr documents created from the item's data |
||||
679 | * @param ResponseAdapter $response The Solr response for the particular index document |
||||
680 | */ |
||||
681 | 20 | protected function log(Item $item, array $itemDocuments, ResponseAdapter $response) |
|||
682 | { |
||||
683 | 20 | if (!$this->loggingEnabled) { |
|||
684 | 20 | return; |
|||
685 | } |
||||
686 | |||||
687 | $message = 'Index Queue indexing ' . $item->getType() . ':' . $item->getRecordUid() . ' - '; |
||||
688 | |||||
689 | // preparing data |
||||
690 | $documents = []; |
||||
691 | foreach ($itemDocuments as $document) { |
||||
692 | $documents[] = (array)$document; |
||||
693 | } |
||||
694 | |||||
695 | $logData = ['item' => (array)$item, 'documents' => $documents, 'response' => (array)$response]; |
||||
696 | |||||
697 | if ($response->getHttpStatus() == 200) { |
||||
698 | $severity = SolrLogManager::NOTICE; |
||||
699 | $message .= 'Success'; |
||||
700 | } else { |
||||
701 | $severity = SolrLogManager::ERROR; |
||||
702 | $message .= 'Failure'; |
||||
703 | |||||
704 | $logData['status'] = $response->getHttpStatus(); |
||||
705 | $logData['status message'] = $response->getHttpStatusMessage(); |
||||
706 | } |
||||
707 | |||||
708 | $this->logger->log($severity, $message, $logData); |
||||
709 | } |
||||
710 | } |
||||
711 |