Passed
Push — master ( ab955c...03080d )
by Timo
10:19
created

Indexer::itemToDocument()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 17
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 3
nc 4
nop 2
crap 3
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
    /**
106
     * Constructor
107
     *
108 34
     * @param array $options array of indexer options
109
     * @param PagesRepository|null $pagesRepository
110 34
     * @param Builder|null $documentBuilder
111 34
     * @param SolrLogManager|null $logger
112 34
     * @param ConnectionManager|null $connectionManager
113 34
     * @param FrontendEnvironment|null $frontendEnvironment
114 34
     */
115 34
    public function __construct(
116
        array $options = [],
117
        PagesRepository $pagesRepository = null,
118
        Builder $documentBuilder = null,
119
        SolrLogManager $logger = null,
120
        ConnectionManager $connectionManager = null,
121
        FrontendEnvironment $frontendEnvironment = null
122
    )
123 18
    {
124
        $this->options = $options;
125 18
        $this->pagesRepository = $pagesRepository ?? GeneralUtility::makeInstance(PagesRepository::class);
126
        $this->documentBuilder = $documentBuilder ?? GeneralUtility::makeInstance(Builder::class);
127 18
        $this->logger = $logger ?? GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
128 18
        $this->connectionManager = $connectionManager ?? GeneralUtility::makeInstance(ConnectionManager::class);
129
        $this->frontendEnvironment = $frontendEnvironment ?? GeneralUtility::makeInstance(FrontendEnvironment::class);
130 18
    }
131 18
132 18
    /**
133
     * Indexes an item from the indexing queue.
134 18
     *
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
        $indexed = true;
141
142
        $this->type = $item->getType();
143
        $this->setLogging($item);
144
145
        $solrConnections = $this->getSolrConnectionsByItem($item);
146
        foreach ($solrConnections as $systemLanguageUid => $solrConnection) {
147 18
            $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
                 * 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
                 * being reindexed during another index run.
157 18
                 */
158
                $indexed = false;
159 18
            }
160 18
        }
161
162 18
        return $indexed;
163 18
    }
164
165
    /**
166
     * Creates a single Solr Document for an item in a specific language.
167
     *
168
     * @param Item $item An index queue item to index.
169
     * @param int $language The language to use.
170 1
     * @return bool TRUE if item was indexed successfully, FALSE on failure
171
     */
172
    protected function indexItem(Item $item, $language = 0)
173 18
    {
174 18
        $itemIndexed = false;
175 18
        $documents = [];
176 18
177
        $itemDocument = $this->itemToDocument($item, $language);
178
        if (is_null($itemDocument)) {
179 18
            /*
180 18
             * If there is no itemDocument, this means there was no translation
181 18
             * for this record. This should not stop the current item to count as
182
             * being valid because not-indexing not-translated items is perfectly
183
             * fine.
184
             */
185
            return true;
186
        }
187 18
188
        $documents[] = $itemDocument;
189 18
        $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
                $itemIndexed = true;
197
            }
198
        } catch (HttpException $e) {
199
            $response = new ResponseAdapter($e->getBody(), $httpStatus = 500, $e->getStatusMessage());
200
        }
201
202
        $this->log($item, $documents, $response);
203 18
204
        return $itemIndexed;
205 18
    }
206
207 18
    /**
208
     * Gets the full item record.
209 18
     *
210 18
     * 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 18
     */
218 18
    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
        }
225
226
        return $itemRecord;
227
    }
228
229
    /**
230
     * Returns the overlayed item record.
231
     *
232
     * @param Item $item
233 18
     * @param int $language
234 18
     * @param string|null $systemLanguageContentOverlay
235 18
     * @return array|mixed|null
236
     */
237
    protected function getItemRecordOverlayed(Item $item, $language)
238 18
    {
239 18
        $itemRecord = $item->getRecord();
240 18
241 18
        if ($language > 0) {
242 18
            $page = GeneralUtility::makeInstance(PageRepository::class);
243 18
            $itemRecord = $page->getLanguageOverlay($item->getType(), $itemRecord);
244
        }
245 1
246
        if (!$itemRecord) {
247
            $itemRecord = null;
248 18
        }
249 18
250
        return $itemRecord;
251
    }
252 18
253
    /**
254
     * Gets the configuration how to process an item's fields for indexing.
255
     *
256
     * @param Item $item An index queue item
257
     * @param int $language Language ID
258
     * @throws \RuntimeException
259
     * @return array Configuration array from TypoScript
260
     */
261
    protected function getItemTypeConfiguration(Item $item, $language = 0)
262
    {
263 18
        $indexConfigurationName = $item->getIndexingConfigurationName();
264
        $fields = $this->getFieldConfigurationFromItemRecordPage($item, $language, $indexConfigurationName);
265 18
        if (!$this->isRootPageIdPartOfRootLine($item) || count($fields) === 0) {
266
            $fields = $this->getFieldConfigurationFromItemRootPage($item, $language, $indexConfigurationName);
267 18
            if (count($fields) === 0) {
268 6
                throw new \RuntimeException('The item indexing configuration "' . $item->getIndexingConfigurationName() .
269 6
                    '" on root page uid ' . $item->getRootPageUid() . ' could not be found!', 1455530112);
270 6
            }
271
        }
272
273 18
        return $fields;
274 1
    }
275
276
    /**
277 18
     * The method retrieves the field configuration of the items record page id (pid).
278
     *
279
     * @param Item $item
280
     * @param integer $language
281
     * @param string $indexConfigurationName
282
     * @return array
283
     */
284
    protected function getFieldConfigurationFromItemRecordPage(Item $item, $language, $indexConfigurationName)
285
    {
286
        try {
287
            $pageId = $this->getPageIdOfItem($item);
288 18
            $solrConfiguration = $this->frontendEnvironment->getSolrConfigurationFromPageId($pageId, $language);
289
            return $solrConfiguration->getIndexQueueFieldsConfigurationByConfigurationName($indexConfigurationName, []);
290 18
        } catch (\Exception $e) {
291 18
            return [];
292 18
        }
293 2
    }
294 2
295
    /**
296
     * @param Item $item
297
     * @return int
298
     */
299
    protected function getPageIdOfItem(Item $item): int
300 18
    {
301
        if ($item->getType() === 'pages') {
302
            return (int)$item->getRecordUid();
303
        }
304
        return (int)$item->getRecordPageId();
305
    }
306
307
    /**
308
     * The method returns the field configuration of the items root page id (uid of the related root page).
309
     *
310
     * @param Item $item
311 18
     * @param integer $language
312
     * @param string $indexConfigurationName
313
     * @return array
314 18
     */
315 17
    protected function getFieldConfigurationFromItemRootPage(Item $item, $language, $indexConfigurationName)
316 1
    {
317 1
        $solrConfiguration = $this->frontendEnvironment->getSolrConfigurationFromPageId($item->getRootPageUid(), $language);
318
        if (empty($solrConfiguration->getIndexQueueAdditionalPageIdsByConfigurationName($indexConfigurationName))) {
319
            return [];
320
        }
321
322
        return $solrConfiguration->getIndexQueueFieldsConfigurationByConfigurationName($indexConfigurationName, []);
323
    }
324
325
    /**
326
     * In case of additionalStoragePid config recordPageId can be outsite of siteroot.
327
     * In that case we should not read TS config of foreign siteroot.
328
     *
329 2
     * @return bool
330
     */
331 2
    protected function isRootPageIdPartOfRootLine(Item $item)
332 2
    {
333
        $rootPageId = $item->getRootPageUid();
334
        $buildRootlineWithPid = $this->getPageIdOfItem($item);
335
        $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $buildRootlineWithPid);
0 ignored issues
show
Bug introduced by
$buildRootlineWithPid of type integer is incompatible with the type array|array<mixed,mixed> expected by parameter $constructorArguments of TYPO3\CMS\Core\Utility\G...Utility::makeInstance(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

335
        $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, /** @scrutinizer ignore-type */ $buildRootlineWithPid);
Loading history...
336 2
        $rootline = $rootlineUtility->get();
337
338
        $pageInRootline = array_filter($rootline, function($page) use ($rootPageId) {
339
            return (int)$page['uid'] === $rootPageId;
340
        });
341
        return !empty($pageInRootline);
342
    }
343
344
    /**
345 18
     * Converts an item array (record) to a Solr document by mapping the
346
     * record's fields onto Solr document fields as configured in TypoScript.
347 18
     *
348 18
     * @param Item $item An index queue item
349 18
     * @param int $language Language Id
350 1
     * @return Document The Solr document converted from the record
351
     */
352 18
    protected function itemToDocument(Item $item, $language = 0)
353 18
    {
354
        $document = null;
355 18
        if ($item->getType() === 'pages') {
356 18
            $this->frontendEnvironment->initializeTsfe($item->getRecordUid(), $language);
357 18
        } else {
358 18
            $this->frontendEnvironment->initializeTsfe($item->getRootPageUid(), $language);
359
        }
360
361
        $itemRecord = $this->getFullItemRecord($item, $language);
362
        if (!is_null($itemRecord)) {
363
            $itemIndexingConfiguration = $this->getItemTypeConfiguration($item, $language);
364
            $document = $this->getBaseDocument($item, $itemRecord);
365
            $document = $this->addDocumentFieldsFromTyposcript($document, $itemIndexingConfiguration, $itemRecord);
366
        }
367
368
        return $document;
369 18
    }
370
371 18
    /**
372
     * Creates a Solr document with the basic / core fields set already.
373 18
     *
374 18
     * @param Item $item The item to index
375 18
     * @param array $itemRecord The record to use to build the base document
376 18
     * @return Document A basic Solr document
377 18
     */
378
    protected function getBaseDocument(Item $item, array $itemRecord)
379
    {
380 18
        $type = $item->getType();
381
        $rootPageUid = $item->getRootPageUid();
382
        $accessRootLine = $this->getAccessRootline($item);
383
        return $this->documentBuilder->fromRecord($itemRecord, $type, $rootPageUid, $accessRootLine);
384
    }
385
386
    /**
387
     * Generates an Access Rootline for an item.
388
     *
389
     * @param Item $item Index Queue item to index.
390 18
     * @return string The Access Rootline for the item
391
     */
392 18
    protected function getAccessRootline(Item $item)
393 18
    {
394 18
        $accessRestriction = '0';
395 18
        $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
            $accessRestriction = $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['fe_group']];
401
402
            if (empty($accessRestriction)) {
403
                // public
404 18
                $accessRestriction = '0';
405
            }
406 18
        }
407 18
408
        return 'r:' . $accessRestriction;
409
    }
410
411 18
    /**
412 1
     * Sends the documents to the field processing service which takes care of
413
     * manipulating fields as defined in the field's configuration.
414 1
     *
415
     * @param Item $item An index queue item
416 1
     * @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 18
    {
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
425
        // same as in the FE indexer
426
        if (is_array($fieldProcessingInstructions)) {
0 ignored issues
show
introduced by
The condition is_array($fieldProcessingInstructions) is always true.
Loading history...
427
            $service = GeneralUtility::makeInstance(Service::class);
428
            $service->processDocuments($documents, $fieldProcessingInstructions);
429
        }
430
431 18
        return $documents;
432
    }
433
434 18
    /**
435 18
     * Allows third party extensions to provide additional documents which
436
     * should be indexed for the current item.
437
     *
438 18
     * @param Item $item The item currently being indexed.
439 18
     * @param int $language The language uid currently being indexed.
440 18
     * @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
     */
443 18
    protected function getAdditionalDocuments(Item $item, $language, Document $itemDocument)
444
    {
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
                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 22
456
                    if (is_array($additionalDocuments)) {
457 22
                        $documents = array_merge($documents,
458
                            $additionalDocuments);
459 22
                    }
460 4
                } else {
461 4
                    throw new \UnexpectedValueException(
462 2
                        get_class($additionalIndexer) . ' must implement interface ' . AdditionalIndexQueueItemIndexer::class,
463
                        1326284551
464 2
                    );
465 2
                }
466 1
            }
467
        }
468 1
        return $documents;
469 1
    }
470 1
471
    /**
472
     * Provides a hook to manipulate documents right before they get added to
473 1
     * the Solr index.
474 1
     *
475 1
     * @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 19
    protected function preAddModifyDocuments(Item $item, $language, array $documents)
481
    {
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 18
							preAddModifyDocuments must implement interface
493
							ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerDocumentsModifier',
494 18
                        1309522677
495
                    );
496
                }
497
            }
498
        }
499
500
        return $documents;
501
    }
502
503
    // Initialization
504
505
    /**
506
     * Gets the Solr connections applicable for an item.
507
     *
508
     * The connections include the default connection and connections to be used
509
     * for translations of an item.
510
     *
511
     * @param Item $item An index queue item
512 18
     * @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
     */
514
    protected function getSolrConnectionsByItem(Item $item)
515
    {
516
        $solrConnections = [];
517
518
        $pageId = $item->getRootPageUid();
519
520
        // Solr configurations possible for this item
521
        $site = $item->getSite();
522
        $solrConfigurationsBySite = $site->getAllSolrConnectionConfigurations();
523
        $siteLanguages = [];
524
        foreach ($solrConfigurationsBySite as $solrConfiguration) {
525
            $siteLanguages[] = $solrConfiguration['language'];
526 20
        }
527
528 20
        $defaultLanguageUid = $this->getDefaultLanguageUid($item, $site->getRootPage(), $siteLanguages);
529
        $translationOverlays = $this->getTranslationOverlaysWithConfiguredSite((int)$pageId, $site, (array)$siteLanguages);
530 20
531 20
        $defaultConnection = $this->connectionManager->getConnectionByPageId($pageId, 0, $item->getMountPointIdentifier());
532 2
        $translationConnections = $this->getConnectionsForIndexableLanguages($translationOverlays);
533
534
        if ($defaultLanguageUid == 0) {
535
            $solrConnections[0] = $defaultConnection;
536 20
        }
537 20
538 20
        foreach ($translationConnections as $systemLanguageUid => $solrConnection) {
539 20
            $solrConnections[$systemLanguageUid] = $solrConnection;
540 20
        }
541
        return $solrConnections;
542
    }
543 20
544 20
    /**
545
     * @param int $pageId
546 20
     * @param Site $site
547 20
     * @param array $siteLanguages
548
     * @return array
549 20
     */
550 19
    protected function getTranslationOverlaysWithConfiguredSite(int $pageId, Site $site, array $siteLanguages): array
551
    {
552
        $translationOverlays = $this->pagesRepository->findTranslationOverlaysByPageId($pageId);
553 20
        $translatedLanguages = [];
554 7
        foreach ($translationOverlays as $key => $translationOverlay) {
555
            if (!in_array($translationOverlay['sys_language_uid'], $siteLanguages)) {
556 20
                unset($translationOverlays[$key]);
557
            } else {
558
                $translatedLanguages[] = (int)$translationOverlay['sys_language_uid'];
559
            }
560
        }
561
562
        if (count($translationOverlays) + 1 !== count($siteLanguages)) {
563
            // not all Languages are translated
564
            // add Language Fallback
565 20
            foreach ($siteLanguages as $languageId) {
566
                if ($languageId !== 0 && !in_array((int)$languageId, $translatedLanguages, true)) {
567 20
                    $fallbackLanguageIds = $this->getFallbackOrder($site, (int)$languageId, (int)$pageId);
568 20
                    foreach ($fallbackLanguageIds as $fallbackLanguageId) {
569 20
                        if ($fallbackLanguageId === 0 || in_array((int)$fallbackLanguageId, $translatedLanguages, true)) {
570 7
                            $translationOverlay = [
571
                                'pid' => $pageId,
572
                                'sys_language_uid' => $languageId,
573 7
                                'l10n_parent' => $pageId
574
                            ];
575
                            $translationOverlays[] = $translationOverlay;
576
                            continue 2;
577 20
                        }
578
                    }
579
                }
580 19
            }
581 19
        }
582 19
        return $translationOverlays;
583 19
    }
584 19
585
    /**
586
     * @param Site $site
587
     * @param int $languageId
588
     * @param int $pageId
589
     * @return array
590
     */
591
    protected function getFallbackOrder(Site $site,  int $languageId, int $pageId): array
0 ignored issues
show
Unused Code introduced by
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 ignore-unused  annotation

591
    protected function getFallbackOrder(Site $site,  int $languageId, /** @scrutinizer ignore-unused */ int $pageId): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
592
    {
593
        $fallbackChain = [];
594
        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
595
        try {
596
            $site = $siteFinder->getSiteByRootPageId($site->getRootPageId());
597 20
            $languageAspect = LanguageAspectFactory::createFromSiteLanguage($site->getLanguageById($languageId));
598
            $fallbackChain = $languageAspect->getFallbackChain();
599
        } catch (SiteNotFoundException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
600
601
        }
602
        return $fallbackChain;
603
    }
604
605
606
    /**
607
     * @param Item $item An index queue item
608
     * @param array $rootPage
609 20
     * @param array $siteLanguages
610
     *
611 20
     * @return int
612 20
     * @throws \RuntimeException
613 1
     */
614 1
    private function getDefaultLanguageUid(Item $item, array $rootPage, array $siteLanguages)
615 19
    {
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 20
        } 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
            throw new \RuntimeException($message);
623
        }
624
625
        return $defaultLanguageUid;
626
    }
627
628
    /**
629
     * Checks for which languages connections have been configured and returns
630 20
     * these connections.
631
     *
632 20
     * @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 20
     */
635 7
    protected function getConnectionsForIndexableLanguages(array $translationOverlays)
636 7
    {
637
        $connections = [];
638
639 7
        foreach ($translationOverlays as $translationOverlay) {
640 7
            $pageId = $translationOverlay['l10n_parent'];
641
            $languageId = $translationOverlay['sys_language_uid'];
642
643
            try {
644
                $connection = $this->connectionManager->getConnectionByPageId($pageId, $languageId);
645
                $connections[$languageId] = $connection;
646
            } catch (NoSolrConnectionFoundException $e) {
647 20
                // ignore the exception as we seek only those connections
648
                // actually available
649
            }
650
        }
651
652
        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 19
     * Enables logging dependent on the configuration of the item's site
662
     *
663 19
     * @param Item $item An item being indexed
664 19
     * @return    void
665 19
     */
666
    protected function setLogging(Item $item)
667 19
    {
668
        $solrConfiguration = $this->frontendEnvironment->getSolrConfigurationFromPageId($item->getRootPageUid());
669
        $this->loggingEnabled = $solrConfiguration->getLoggingIndexingQueueOperationsByConfigurationNameWithFallBack(
670
            $item->getIndexingConfigurationName()
671
        );
672
    }
673
674
    /**
675
     * Logs the item and what document was created from it
676 18
     *
677
     * @param Item $item The item that is being indexed.
678 18
     * @param array $itemDocuments An array of Solr documents created from the item's data
679 18
     * @param ResponseAdapter $response The Solr response for the particular index document
680
     */
681
    protected function log(Item $item, array $itemDocuments, ResponseAdapter $response)
682
    {
683
        if (!$this->loggingEnabled) {
684
            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