Completed
Pull Request — master (#1305)
by
unknown
36:36
created

Indexer::getBaseDocument()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7.3471

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 46
ccs 12
cts 22
cp 0.5455
rs 8.4751
cc 5
eloc 25
nc 8
nop 2
crap 7.3471
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 2 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 Apache_Solr_Document;
28
use Apache_Solr_Response;
29
use ApacheSolrForTypo3\Solr\ConnectionManager;
30
use ApacheSolrForTypo3\Solr\Domain\Variants\IdBuilder;
31
use ApacheSolrForTypo3\Solr\FieldProcessor\Service;
32
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
33
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
34
use ApacheSolrForTypo3\Solr\SolrService;
35
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
36
use ApacheSolrForTypo3\Solr\Util;
37
use TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider;
38
use TYPO3\CMS\Backend\Utility\BackendUtility;
39
use TYPO3\CMS\Core\Utility\GeneralUtility;
40
use TYPO3\CMS\Frontend\Page\PageRepository;
41
42
/**
43
 * A general purpose indexer to be used for indexing of any kind of regular
44
 * records like tt_news, tt_address, and so on.
45
 * Specialized indexers can extend this class to handle advanced stuff like
46
 * category resolution in tt_news or file indexing.
47
 *
48
 * @author Ingo Renner <[email protected]>
49
 */
50
class Indexer extends AbstractIndexer
51
{
52
53
    # TODO change to singular $document instead of plural $documents
54
55
    /**
56
     * A Solr service instance to interact with the Solr server
57
     *
58
     * @var SolrService
59
     */
60
    protected $solr;
61
62
    /**
63
     * @var ConnectionManager
64
     */
65
    protected $connectionManager;
66
67
    /**
68
     * Holds options for a specific indexer
69
     *
70
     * @var array
71
     */
72
    protected $options = [];
73
74
    /**
75
     * To log or not to log... #Shakespeare
76
     *
77
     * @var bool
78
     */
79
    protected $loggingEnabled = false;
80
81
    /**
82
     * @var IdBuilder
83
     */
84
    protected $variantIdBuilder;
85
86
    /**
87
     * Cache of the sys_language_overlay information
88
     *
89
     * @var array
90
     */
91
    protected static $sysLanguageOverlay = [];
92
93
    /**
94
     * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager
95
     */
96
    protected $logger = null;
97
98
    /**
99
     * Constructor
100
     *
101
     * @param array $options array of indexer options
102
     * @param IdBuilder $idBuilder
103
     */
104 16
    public function __construct(array $options = [], IdBuilder $idBuilder = null)
105
    {
106 16
        $this->logger = GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__);
107 16
        $this->options = $options;
108 16
        $this->connectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
109 16
        $this->variantIdBuilder = is_null($idBuilder) ? GeneralUtility::makeInstance(IdBuilder::class) : $idBuilder;
110 16
    }
111
112
    /**
113
     * Indexes an item from the indexing queue.
114
     *
115
     * @param Item $item An index queue item
116
     * @return bool returns true when indexed, false when not
117
     */
118 11
    public function index(Item $item)
119
    {
120 11
        $indexed = true;
121
122 11
        $this->type = $item->getType();
123 11
        $this->setLogging($item);
124
125 11
        $solrConnections = $this->getSolrConnectionsByItem($item);
126
127 11
        foreach ($solrConnections as $systemLanguageUid => $solrConnection) {
128 11
            $this->solr = $solrConnection;
129
130 11
            if (!$this->indexItem($item, $systemLanguageUid)) {
131
                /*
132
                 * A single language voting for "not indexed" should make the whole
133
                 * item count as being not indexed, even if all other languages are
134
                 * indexed.
135
                 * If there is no translation for a single language, this item counts
136
                 * as TRUE since it's not an error which that should make the item
137
                 * being reindexed during another index run.
138
                 */
139
                $indexed = false;
140
            }
141
        }
142
143 11
        return $indexed;
144
    }
145
146
    /**
147
     * Creates a single Solr Document for an item in a specific language.
148
     *
149
     * @param Item $item An index queue item to index.
150
     * @param int $language The language to use.
151
     * @return bool TRUE if item was indexed successfully, FALSE on failure
152
     */
153 11
    protected function indexItem(Item $item, $language = 0)
154
    {
155 11
        $itemIndexed = false;
156 11
        $documents = [];
157
158 11
        $itemDocument = $this->itemToDocument($item, $language);
159 11
        if (is_null($itemDocument)) {
160
            /*
161
             * If there is no itemDocument, this means there was no translation
162
             * for this record. This should not stop the current item to count as
163
             * being valid because not-indexing not-translated items is perfectly
164
             * fine.
165
             */
166
            return true;
167
        }
168
169 11
        $documents[] = $itemDocument;
170 11
        $documents = array_merge($documents, $this->getAdditionalDocuments($item, $language, $itemDocument));
171 11
        $documents = $this->processDocuments($item, $documents);
172 11
        $documents = $this->preAddModifyDocuments($item, $language, $documents);
173
174 11
        $response = $this->solr->addDocuments($documents);
175 11
        if ($response->getHttpStatus() == 200) {
176 11
            $itemIndexed = true;
177
        }
178
179 11
        $this->log($item, $documents, $response);
180
181 11
        return $itemIndexed;
182
    }
183
184
    /**
185
     * Gets the full item record.
186
     *
187
     * This general record indexer simply gets the record from the item. Other
188
     * more specialized indexers may provide more data for their specific item
189
     * types.
190
     *
191
     * @param Item $item The item to be indexed
192
     * @param int $language Language Id (sys_language.uid)
193
     * @return array|NULL The full record with fields of data to be used for indexing or NULL to prevent an item from being indexed
194
     */
195 11
    protected function getFullItemRecord(Item $item, $language = 0)
196
    {
197 11
        $rootPageUid = $item->getRootPageUid();
198 11
        $overlayIdentifier = $rootPageUid . '|' . $language;
199 11
        if (!isset(self::$sysLanguageOverlay[$overlayIdentifier])) {
200 11
            Util::initializeTsfe($rootPageUid, $language);
201 11
            self::$sysLanguageOverlay[$overlayIdentifier] = $GLOBALS['TSFE']->sys_language_contentOL;
202
        }
203
204 11
        $itemRecord = $item->getRecord();
205
206 11
        if ($language > 0) {
207 1
            $page = GeneralUtility::makeInstance(PageRepository::class);
208 1
            $page->init(false);
209
210 1
            $itemRecord = $page->getRecordOverlay(
211 1
                $item->getType(),
212
                $itemRecord,
213
                $language,
214 1
                self::$sysLanguageOverlay[$overlayIdentifier]
215
            );
216
        }
217
218 11
        if (!$itemRecord) {
219
            $itemRecord = null;
220
        }
221
222
        /*
223
         * Skip disabled records. This happens if the default language record
224
         * is hidden but a certain translation isn't. Then the default language
225
         * document appears here but must not be indexed.
226
         */
227 11
        if (!empty($GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['disabled'])
228 11
            && $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['disabled']]
229
        ) {
230
            $itemRecord = null;
231
        }
232
233
        /*
234
         * Skip translation mismatching records. Sometimes the requested language
235
         * doesn't fit the returned language. This might happen with content fallback
236
         * and is perfectly fine in general.
237
         * But if the requested language doesn't match the returned language and
238
         * the given record has no translation parent, the indexqueue_item most
239
         * probably pointed to a non-translated language record that is dedicated
240
         * to a very specific language. Now we have to avoid indexing this record
241
         * into all language cores.
242
         */
243 11
        $translationOriginalPointerField = 'l10n_parent';
244 11
        if (!empty($GLOBALS['TCA'][$item->getType()]['ctrl']['transOrigPointerField'])) {
245 10
            $translationOriginalPointerField = $GLOBALS['TCA'][$item->getType()]['ctrl']['transOrigPointerField'];
246
        }
247
248 11
        $languageField = $GLOBALS['TCA'][$item->getType()]['ctrl']['languageField'];
249 11
        if ($itemRecord[$translationOriginalPointerField] == 0
250 11
            && self::$sysLanguageOverlay[$overlayIdentifier] != 1
251
            && !empty($languageField)
252 10
            && $itemRecord[$languageField] != $language
253
            && $itemRecord[$languageField] != '-1'
254
        ) {
255
            $itemRecord = null;
256
        }
257
258 11
        if (!is_null($itemRecord)) {
259 11
            $itemRecord['__solr_index_language'] = $language;
260
        }
261
262 11
        return $itemRecord;
263
    }
264
265
    /**
266
     * Gets the configuration how to process an item's fields for indexing.
267
     *
268
     * @param Item $item An index queue item
269
     * @param int $language Language ID
270
     * @throws \RuntimeException
271
     * @return array Configuration array from TypoScript
272
     */
273 11
    protected function getItemTypeConfiguration(Item $item, $language = 0)
274
    {
275 11
        $indexConfigurationName = $item->getIndexingConfigurationName();
276 11
        $fields = $this->getFieldConfigurationFromItemRecordPage($item, $language, $indexConfigurationName);
277 11
        if (count($fields) === 0) {
278
            $fields = $this->getFieldConfigurationFromItemRootPage($item, $language, $indexConfigurationName);
279 11
            if (count($fields) === 0) {
280
                throw new \RuntimeException('The item indexing configuration "' . $item->getIndexingConfigurationName() .
281
                    '" on root page uid ' . $item->getRootPageUid() . ' could not be found!', 1455530112);
282
            }
283
        }
284 11
285
        return $fields;
286
    }
287
288
    /**
289
     * The method retrieves the field configuration of the items record page id (pid).
290
     *
291
     * @param Item $item
292
     * @param integer $language
293
     * @param string $indexConfigurationName
294
     * @return array
295 11
     */
296
    protected function getFieldConfigurationFromItemRecordPage(Item $item, $language, $indexConfigurationName)
297 11
    {
298
        try {
299 11
            $solrConfiguration = Util::getSolrConfigurationFromPageId($item->getRecordPageId(), true, $language);
300 11
            return $solrConfiguration->getIndexQueueFieldsConfigurationByConfigurationName($indexConfigurationName, []);
301 11
        } catch (\Exception $e) {
302 11
            return [];
303 11
        }
304
    }
305
306 11
    /**
307
     * The method returns the field configuration of the items root page id (uid of the related root page).
308
     *
309
     * @param Item $item
310
     * @param integer $language
311
     * @param string $indexConfigurationName
312
     * @return array
313
     */
314
    protected function getFieldConfigurationFromItemRootPage(Item $item, $language, $indexConfigurationName)
315
    {
316 11
        $solrConfiguration = Util::getSolrConfigurationFromPageId($item->getRootPageUid(), true, $language);
317
        if (empty($solrConfiguration->getIndexQueueAdditionalPageIdsByConfigurationName($indexConfigurationName))) {
318 11
            return [];
319 11
        }
320
321 11
        return $solrConfiguration->getIndexQueueFieldsConfigurationByConfigurationName($indexConfigurationName, []);
322
    }
323
324
    /**
325 11
     * Converts an item array (record) to a Solr document by mapping the
326 11
     * record's fields onto Solr document fields as configured in TypoScript.
327 11
     *
328 11
     * @param Item $item An index queue item
329
     * @param int $language Language Id
330 11
     * @return Apache_Solr_Document The Solr document converted from the record
331 11
     */
332
    protected function itemToDocument(Item $item, $language = 0)
333
    {
334 11
        $document = null;
335 11
336
        $itemRecord = $this->getFullItemRecord($item, $language);
337
        if (!is_null($itemRecord)) {
338 11
            $itemIndexingConfiguration = $this->getItemTypeConfiguration($item, $language);
339 11
            $document = $this->getBaseDocument($item, $itemRecord);
340
            $document = $this->addDocumentFieldsFromTyposcript($document, $itemIndexingConfiguration, $itemRecord);
341
        }
342 11
343 11
        return $document;
344
    }
345
346 11
    /**
347 11
     * Creates a Solr document with the basic / core fields set already.
348 11
     *
349
     * @param Item $item The item to index
350 11
     * @param array $itemRecord The record to use to build the base document
351 11
     * @return Apache_Solr_Document A basic Solr document
352 11
     */
353
    protected function getBaseDocument(Item $item, array $itemRecord)
354
    {
355
        $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
356 11
        $site = $siteRepository->getSiteByRootPageId($item->getRootPageUid());
357 11
358 11
        $document = GeneralUtility::makeInstance(Apache_Solr_Document::class);
359
        /* @var $document Apache_Solr_Document */
360
361
        // required fields
362
        $document->setField('id', Util::getDocumentId($item->getType(), $site->getRootPageId(), $itemRecord['uid']));
363
        $document->setField('type', $item->getType());
364 11
        $document->setField('appKey', 'EXT:solr');
365
366
        // site, siteHash
367
        $document->setField('site', $site->getDomain());
368
        $document->setField('siteHash', $site->getSiteHash());
369
370
        // uid, pid
371
        $document->setField('uid', $itemRecord['uid']);
372
        $document->setField('pid', $itemRecord['pid']);
373 11
374
        // variantId
375 11
        $variantId = $this->variantIdBuilder->buildFromTypeAndUid($item->getType(), $itemRecord['uid']);
376 11
        $document->setField('variantId', $variantId);
377
378
        // created, changed
379
        if (!empty($GLOBALS['TCA'][$item->getType()]['ctrl']['crdate'])) {
380 11
            $document->setField('created',
381 1
                $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['crdate']]);
382
        }
383 1
        if (!empty($GLOBALS['TCA'][$item->getType()]['ctrl']['tstamp'])) {
384
            $document->setField('changed',
385 1
                $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['tstamp']]);
386
        }
387
388
        // access, endtime
389 11
        $document->setField('access', $this->getAccessRootline($item));
390
        if (!empty($GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['endtime'])
391
            && $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['endtime']] != 0
392
        ) {
393
            $document->setField('endtime',
394
                $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['endtime']]);
395
        }
396
397
        return $document;
398
    }
399
400 11
    /**
401
     * Generates an Access Rootline for an item.
402
     *
403 11
     * @param Item $item Index Queue item to index.
404 11
     * @return string The Access Rootline for the item
405
     */
406
    protected function getAccessRootline(Item $item)
407 11
    {
408 11
        $accessRestriction = '0';
409 11
        $itemRecord = $item->getRecord();
410
411 11
        // TODO support access restrictions set on storage page
412
413
        if (isset($GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['fe_group'])) {
414
            $accessRestriction = $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['fe_group']];
415 11
416
            if (empty($accessRestriction)) {
417
                // public
418
                $accessRestriction = '0';
419
            }
420
        }
421
422
        return 'r:' . $accessRestriction;
423
    }
424
425
    /**
426
     * Sends the documents to the field processing service which takes care of
427 15
     * manipulating fields as defined in the field's configuration.
428
     *
429
     * @param Item $item An index queue item
430
     * @param array $documents An array of Apache_Solr_Document objects to manipulate.
431
     * @return array Array of manipulated Apache_Solr_Document objects.
432 15
     */
433
    protected function processDocuments(Item $item, array $documents)
434 15
    {
435 4
        // needs to respect the TS settings for the page the item is on, conditions may apply
436 4
        $solrConfiguration = Util::getSolrConfigurationFromPageId($item->getRootPageUid());
437 2
        $fieldProcessingInstructions = $solrConfiguration->getIndexFieldProcessingInstructionsConfiguration();
438
439 2
        // same as in the FE indexer
440 2
        if (is_array($fieldProcessingInstructions)) {
441 1
            $service = GeneralUtility::makeInstance(Service::class);
442 1
            $service->processDocuments(
443
                $documents,
444 1
                $fieldProcessingInstructions
445 1
            );
446 1
        }
447
448
        return $documents;
449 1
    }
450 1
451 1
    /**
452
     * Allows third party extensions to provide additional documents which
453
     * should be indexed for the current item.
454
     *
455
     * @param Item $item The item currently being indexed.
456 12
     * @param int $language The language uid currently being indexed.
457
     * @param Apache_Solr_Document $itemDocument The document representing the item for the given language.
458
     * @return array An array of additional Apache_Solr_Document objects to index.
459
     */
460
    protected function getAdditionalDocuments(
461
        Item $item,
462
        $language,
463
        Apache_Solr_Document $itemDocument
464
    ) {
465
        $documents = [];
466
467
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['indexItemAddDocuments'])) {
468 11
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['indexItemAddDocuments'] as $classReference) {
469
                if (!class_exists($classReference)) {
470
                    throw new \InvalidArgumentException('Class does not exits' . $classReference, 1490363487);
471
                }
472
                $additionalIndexer = GeneralUtility::makeInstance($classReference);
473 11
                if ($additionalIndexer instanceof AdditionalIndexQueueItemIndexer) {
474
                    $additionalDocuments = $additionalIndexer->getAdditionalItemDocuments($item,
475
                        $language, $itemDocument);
476
477
                    if (is_array($additionalDocuments)) {
478
                        $documents = array_merge($documents,
479
                            $additionalDocuments);
480
                    }
481
                } else {
482
                    throw new \UnexpectedValueException(
483
                        get_class($additionalIndexer) . ' must implement interface ' . AdditionalIndexQueueItemIndexer::class,
484
                        1326284551
485
                    );
486
                }
487
            }
488
        }
489
        return $documents;
490
    }
491
492 11
    /**
493
     * Provides a hook to manipulate documents right before they get added to
494
     * the Solr index.
495
     *
496
     * @param Item $item The item currently being indexed.
497
     * @param int $language The language uid of the documents
498
     * @param array $documents An array of documents to be indexed
499
     * @return array An array of modified documents
500
     */
501
    protected function preAddModifyDocuments(
502
        Item $item,
503
        $language,
504
        array $documents
505
    ) {
506 12
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments'])) {
507
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments'] as $classReference) {
508 12
                $documentsModifier = GeneralUtility::getUserObj($classReference);
509
510 12
                if ($documentsModifier instanceof PageIndexerDocumentsModifier) {
511 12
                    $documents = $documentsModifier->modifyDocuments($item,
512 2
                        $language, $documents);
513
                } else {
514
                    throw new \RuntimeException(
515
                        'The class "' . get_class($documentsModifier)
516 12
                        . '" registered as document modifier in hook
517 12
							preAddModifyDocuments must implement interface
518
							ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerDocumentsModifier',
519 12
                        1309522677
520 12
                    );
521 12
                }
522
            }
523
        }
524 12
525 12
        return $documents;
526 1
    }
527 1
528
    // Initialization
529
530
    /**
531
     * Gets the Solr connections applicable for an item.
532
     *
533 12
     * The connections include the default connection and connections to be used
534 12
     * for translations of an item.
535
     *
536 12
     * @param Item $item An index queue item
537 12
     * @return array An array of ApacheSolrForTypo3\Solr\SolrService connections, the array's keys are the sys_language_uid of the language of the connection
538 1
     */
539
    protected function getSolrConnectionsByItem(Item $item)
540
    {
541 12
        $solrConnections = [];
542
543
        $pageId = $item->getRootPageUid();
544
        if ($item->getType() == 'pages') {
545
            $pageId = $item->getRecordUid();
546
        }
547
548
        // Solr configurations possible for this item
549
        $site = $item->getSite();
550
        $solrConfigurationsBySite = $this->connectionManager->getConfigurationsBySite($site);
551
552
        $siteLanguages = [];
553
        foreach ($solrConfigurationsBySite as $solrConfiguration) {
554
            $siteLanguages[] = $solrConfiguration['language'];
555
        }
556
557
        $translationOverlays = $this->getTranslationOverlaysForPage($pageId, $site->getSysLanguageMode());
558 12
        foreach ($translationOverlays as $key => $translationOverlay) {
559
            if (!in_array($translationOverlay['sys_language_uid'],
560 12
                $siteLanguages)
561 12
            ) {
562
                unset($translationOverlays[$key]);
563 12
            }
564 12
        }
565 12
566 12
        $defaultConnection = $this->connectionManager->getConnectionByPageId($pageId, 0, $item->getMountPointIdentifier());
567
        $translationConnections = $this->getConnectionsForIndexableLanguages($translationOverlays);
568 12
569 1
        $solrConnections[0] = $defaultConnection;
570 1
        foreach ($translationConnections as $systemLanguageUid => $solrConnection) {
571 1
            $solrConnections[$systemLanguageUid] = $solrConnection;
572 1
        }
573 1
574 1
        return $solrConnections;
575
    }
576
577
    /**
578 11
     * Finds the alternative page language overlay records for a page based on
579
     * the sys_language_mode.
580 11
     *
581 11
     * Possible Language Modes:
582 11
     * 1) content_fallback --> all languages
583
     * 2) strict --> available languages with page overlay
584
     * 3) ignore --> available languages with page overlay
585
     * 4) unknown mode or blank --> all languages
586
     *
587
     * @param int $pageId Page ID.
588
     * @param string $languageMode
589
     * @return array An array of translation overlays (or fake overlays) found for the given page.
590
     */
591 12
    protected function getTranslationOverlaysForPage($pageId, $languageMode)
592
    {
593
        $translationOverlays = [];
594
        $pageId = intval($pageId);
595
596
        $languageModes = ['content_fallback', 'strict', 'ignore'];
597
        $hasOverlayMode = in_array($languageMode, $languageModes,
598
            true);
599 11
        $isContentFallbackMode = ($languageMode === 'content_fallback');
600
601 11
        if ($hasOverlayMode && !$isContentFallbackMode) {
602
            $translationOverlays = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
603
                'pid, sys_language_uid',
604
                'pages_language_overlay',
605
                'pid = ' . $pageId
606
                . BackendUtility::deleteClause('pages_language_overlay')
607
                . BackendUtility::BEenableFields('pages_language_overlay')
608
            );
609
        } else {
610
            // ! If no sys_language_mode is configured, all languages will be indexed !
611 12
            $languages = $this->getSystemLanguages();
612
613
            foreach ($languages as $language) {
614 12
                if ($language['uid'] <= 0) {
615
                    continue;
616 12
                }
617 1
                $translationOverlays[] = [
618 1
                    'pid' => $pageId,
619
                    'sys_language_uid' => $language['uid'],
620
                ];
621 1
            }
622 1
        }
623 1
624
        return $translationOverlays;
625
    }
626
627
    /**
628
     * Returns an array of system languages.
629
     *
630 12
     * @return array
631
     */
632
    protected function getSystemLanguages()
633
    {
634
        return GeneralUtility::makeInstance(TranslationConfigurationProvider::class)->getSystemLanguages();
635
    }
636
637
    /**
638
     * Checks for which languages connections have been configured and returns
639
     * these connections.
640
     *
641
     * @param array $translationOverlays An array of translation overlays to check for configured connections.
642
     * @return array An array of ApacheSolrForTypo3\Solr\SolrService connections.
643
     */
644 12
    protected function getConnectionsForIndexableLanguages(
645
        array $translationOverlays
646 12
    ) {
647 12
        $connections = [];
648 12
649
        foreach ($translationOverlays as $translationOverlay) {
650 12
            $pageId = $translationOverlay['pid'];
651
            $languageId = $translationOverlay['sys_language_uid'];
652
653
            try {
654
                $connection = $this->connectionManager->getConnectionByPageId($pageId,
655
                    $languageId);
656
                $connections[$languageId] = $connection;
657
            } catch (NoSolrConnectionFoundException $e) {
658
                // ignore the exception as we seek only those connections
659 11
                // actually available
660
            }
661
        }
662
663
        return $connections;
664 11
    }
665 11
666
    // Utility methods
667
668
    // FIXME extract log() and setLogging() to ApacheSolrForTypo3\Solr\IndexQueue\AbstractIndexer
669
    // FIXME extract an interface Tx_Solr_IndexQueue_ItemInterface
670
671
    /**
672
     * Enables logging dependent on the configuration of the item's site
673
     *
674
     * @param Item $item An item being indexed
675
     * @return    void
676
     */
677
    protected function setLogging(Item $item)
678
    {
679
        $solrConfiguration = Util::getSolrConfigurationFromPageId($item->getRootPageUid());
680
        $this->loggingEnabled = $solrConfiguration->getLoggingIndexingQueueOperationsByConfigurationNameWithFallBack(
681
            $item->getIndexingConfigurationName()
682
        );
683
    }
684
685
    /**
686
     * Logs the item and what document was created from it
687
     *
688
     * @param Item $item The item that is being indexed.
689
     * @param array $itemDocuments An array of Solr documents created from the item's data
690
     * @param Apache_Solr_Response $response The Solr response for the particular index document
691
     */
692
    protected function log(
693
        Item $item,
694
        array $itemDocuments,
695
        Apache_Solr_Response $response
696
    ) {
697
        if (!$this->loggingEnabled) {
698
            return;
699
        }
700
701
        $message = 'Index Queue indexing ' . $item->getType() . ':'
702
            . $item->getRecordUid() . ' - ';
703
704
        // preparing data
705
        $documents = [];
706
        foreach ($itemDocuments as $document) {
707
            $documents[] = (array)$document;
708
        }
709
710
        $logData = [
711
            'item' => (array)$item,
712
            'documents' => $documents,
713
            'response' => (array)$response
714
        ];
715
716
        if ($response->getHttpStatus() == 200) {
717
            $severity = SolrLogManager::NOTICE;
718
            $message .= 'Success';
719
        } else {
720
            $severity = SolrLogManager::ERROR;
721
            $message .= 'Failure';
722
723
            $logData['status'] = $response->getHttpStatus();
724
            $logData['status message'] = $response->getHttpStatusMessage();
725
        }
726
727
        $this->logger->log(
728
            $severity,
729
            $message,
730
            $logData
731
        );
732
    }
733
}
734