Passed
Push — master ( 42d2d3...8f9ec7 )
by Timo
69:32 queued 48:37
created

Indexer::index()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 10
cts 14
cp 0.7143
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 10
nc 3
nop 1
crap 3.2098
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\Search\ApacheSolrDocument\Builder;
31
use ApacheSolrForTypo3\Solr\FieldProcessor\Service;
32
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException;
33
use ApacheSolrForTypo3\Solr\Site;
34
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
35
use ApacheSolrForTypo3\Solr\System\Records\Pages\PagesRepository;
36
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
37
use ApacheSolrForTypo3\Solr\Util;
38
use TYPO3\CMS\Backend\Configuration\TranslationConfigurationProvider;
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 SolrConnection
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 \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager
83
     */
84
    protected $logger = null;
85
86
    /**
87
     * @var PagesRepository
88
     */
89
    protected $pagesRepository;
90
91
    /**
92
     * @var Builder
93
     */
94
    protected $documentBuilder;
95
96
    /**
97
     * Constructor
98
     *
99
     * @param array $options array of indexer options
100
     * @param PagesRepository|null $pagesRepository
101
     * @param Builder|null $documentBuilder
102
     */
103 25
    public function __construct(array $options = [], PagesRepository $pagesRepository = null, Builder $documentBuilder = null)
104
    {
105 25
        $this->logger = GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__);
106 25
        $this->options = $options;
107 25
        $this->connectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
108 25
        $this->pagesRepository = isset($pagesRepository) ? $pagesRepository : GeneralUtility::makeInstance(PagesRepository::class);
109 25
        $this->documentBuilder = isset($documentBuilder) ? $documentBuilder : GeneralUtility::makeInstance(Builder::class);
110 25
    }
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 19
    public function index(Item $item)
119
    {
120 19
        $indexed = true;
121
122 19
        $this->type = $item->getType();
123 19
        $this->setLogging($item);
124
125 19
        $solrConnections = $this->getSolrConnectionsByItem($item);
126
127 19
        foreach ($solrConnections as $systemLanguageUid => $solrConnection) {
128 19
            $this->solr = $solrConnection;
129
130 19
            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 19
                $indexed = false;
140
            }
141
        }
142
143 19
        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 19
    protected function indexItem(Item $item, $language = 0)
154
    {
155 19
        $itemIndexed = false;
156 19
        $documents = [];
157
158 19
        $itemDocument = $this->itemToDocument($item, $language);
159 19
        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 1
            return true;
167
        }
168
169 19
        $documents[] = $itemDocument;
170 19
        $documents = array_merge($documents, $this->getAdditionalDocuments($item, $language, $itemDocument));
171 19
        $documents = $this->processDocuments($item, $documents);
172 19
        $documents = $this->preAddModifyDocuments($item, $language, $documents);
173
174 19
        $response = $this->solr->getWriteService()->addDocuments($documents);
175 19
        if ($response->getHttpStatus() == 200) {
176 19
            $itemIndexed = true;
177
        }
178
179 19
        $this->log($item, $documents, $response);
180
181 19
        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 19
    protected function getFullItemRecord(Item $item, $language = 0)
196
    {
197 19
        Util::initializeTsfe($item->getRootPageUid(), $language);
198
199 19
        $systemLanguageContentOverlay = $GLOBALS['TSFE']->sys_language_contentOL;
200 19
        $itemRecord = $this->getItemRecordOverlayed($item, $language, $systemLanguageContentOverlay);
201
202
        /*
203
         * Skip disabled records. This happens if the default language record
204
         * is hidden but a certain translation isn't. Then the default language
205
         * document appears here but must not be indexed.
206
         */
207 19
        if (!empty($GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['disabled'])
208 19
            && $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['disabled']]
209
        ) {
210
            $itemRecord = null;
211
        }
212
213
        /*
214
         * Skip translation mismatching records. Sometimes the requested language
215
         * doesn't fit the returned language. This might happen with content fallback
216
         * and is perfectly fine in general.
217
         * But if the requested language doesn't match the returned language and
218
         * the given record has no translation parent, the indexqueue_item most
219
         * probably pointed to a non-translated language record that is dedicated
220
         * to a very specific language. Now we have to avoid indexing this record
221
         * into all language cores.
222
         */
223 19
        $translationOriginalPointerField = 'l10n_parent';
224 19
        if (!empty($GLOBALS['TCA'][$item->getType()]['ctrl']['transOrigPointerField'])) {
225 18
            $translationOriginalPointerField = $GLOBALS['TCA'][$item->getType()]['ctrl']['transOrigPointerField'];
226
        }
227
228 19
        $languageField = $GLOBALS['TCA'][$item->getType()]['ctrl']['languageField'];
229 19
        if ($itemRecord[$translationOriginalPointerField] == 0
230 19
            && $systemLanguageContentOverlay != 1
231 19
            && !empty($languageField)
232 19
            && $itemRecord[$languageField] != $language
233 19
            && $itemRecord[$languageField] != '-1'
234
        ) {
235 1
            $itemRecord = null;
236
        }
237
238 19
        if (!is_null($itemRecord)) {
239 19
            $itemRecord['__solr_index_language'] = $language;
240
        }
241
242 19
        return $itemRecord;
243
    }
244
245
    /**
246
     * Returns the overlayed item record.
247
     *
248
     * @param Item $item
249
     * @param int $language
250
     * @param string|null $systemLanguageContentOverlay
251
     * @return array|mixed|null
252
     */
253 19
    protected function getItemRecordOverlayed(Item $item, $language, $systemLanguageContentOverlay)
254
    {
255 19
        $itemRecord = $item->getRecord();
256
257 19
        if ($language > 0) {
258 6
            $page = GeneralUtility::makeInstance(PageRepository::class);
259 6
            $page->init(false);
260 6
            $itemRecord = $page->getRecordOverlay($item->getType(), $itemRecord, $language, $systemLanguageContentOverlay);
261
        }
262
263 19
        if (!$itemRecord) {
264
            $itemRecord = null;
265
        }
266
267 19
        return $itemRecord;
268
    }
269
270
    /**
271
     * Gets the configuration how to process an item's fields for indexing.
272
     *
273
     * @param Item $item An index queue item
274
     * @param int $language Language ID
275
     * @throws \RuntimeException
276
     * @return array Configuration array from TypoScript
277
     */
278 19
    protected function getItemTypeConfiguration(Item $item, $language = 0)
279
    {
280 19
        $indexConfigurationName = $item->getIndexingConfigurationName();
281 19
        $fields = $this->getFieldConfigurationFromItemRecordPage($item, $language, $indexConfigurationName);
282 19
        if (count($fields) === 0) {
283 1
            $fields = $this->getFieldConfigurationFromItemRootPage($item, $language, $indexConfigurationName);
284 1
            if (count($fields) === 0) {
285
                throw new \RuntimeException('The item indexing configuration "' . $item->getIndexingConfigurationName() .
286
                    '" on root page uid ' . $item->getRootPageUid() . ' could not be found!', 1455530112);
287
            }
288
        }
289
290 19
        return $fields;
291
    }
292
293
    /**
294
     * The method retrieves the field configuration of the items record page id (pid).
295
     *
296
     * @param Item $item
297
     * @param integer $language
298
     * @param string $indexConfigurationName
299
     * @return array
300
     */
301 19
    protected function getFieldConfigurationFromItemRecordPage(Item $item, $language, $indexConfigurationName)
302
    {
303
        try {
304 19
            $solrConfiguration = Util::getSolrConfigurationFromPageId($item->getRecordPageId(), true, $language);
305 18
            return $solrConfiguration->getIndexQueueFieldsConfigurationByConfigurationName($indexConfigurationName, []);
306 1
        } catch (\Exception $e) {
307 1
            return [];
308
        }
309
    }
310
311
    /**
312
     * The method returns the field configuration of the items root page id (uid of the related root page).
313
     *
314
     * @param Item $item
315
     * @param integer $language
316
     * @param string $indexConfigurationName
317
     * @return array
318
     */
319 1
    protected function getFieldConfigurationFromItemRootPage(Item $item, $language, $indexConfigurationName)
320
    {
321 1
        $solrConfiguration = Util::getSolrConfigurationFromPageId($item->getRootPageUid(), true, $language);
322 1
        if (empty($solrConfiguration->getIndexQueueAdditionalPageIdsByConfigurationName($indexConfigurationName))) {
323
            return [];
324
        }
325
326 1
        return $solrConfiguration->getIndexQueueFieldsConfigurationByConfigurationName($indexConfigurationName, []);
327
    }
328
329
    /**
330
     * Converts an item array (record) to a Solr document by mapping the
331
     * record's fields onto Solr document fields as configured in TypoScript.
332
     *
333
     * @param Item $item An index queue item
334
     * @param int $language Language Id
335
     * @return Apache_Solr_Document The Solr document converted from the record
336
     */
337 19
    protected function itemToDocument(Item $item, $language = 0)
338
    {
339 19
        $document = null;
340
341 19
        $itemRecord = $this->getFullItemRecord($item, $language);
342 19
        if (!is_null($itemRecord)) {
343 19
            $itemIndexingConfiguration = $this->getItemTypeConfiguration($item, $language);
344 19
            $document = $this->getBaseDocument($item, $itemRecord);
345 19
            $document = $this->addDocumentFieldsFromTyposcript($document, $itemIndexingConfiguration, $itemRecord);
346
        }
347
348 19
        return $document;
349
    }
350
351
    /**
352
     * Creates a Solr document with the basic / core fields set already.
353
     *
354
     * @param Item $item The item to index
355
     * @param array $itemRecord The record to use to build the base document
356
     * @return Apache_Solr_Document A basic Solr document
357
     */
358 19
    protected function getBaseDocument(Item $item, array $itemRecord)
359
    {
360 19
        $type = $item->getType();
361 19
        $rootPageUid = $item->getRootPageUid();
362 19
        $accessRootLine = $this->getAccessRootline($item);
363 19
        return $this->documentBuilder->fromRecord($itemRecord, $type, $rootPageUid, $accessRootLine);
364
    }
365
366
    /**
367
     * Generates an Access Rootline for an item.
368
     *
369
     * @param Item $item Index Queue item to index.
370
     * @return string The Access Rootline for the item
371
     */
372 19
    protected function getAccessRootline(Item $item)
373
    {
374 19
        $accessRestriction = '0';
375 19
        $itemRecord = $item->getRecord();
376
377
        // TODO support access restrictions set on storage page
378
379 19
        if (isset($GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['fe_group'])) {
380 1
            $accessRestriction = $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['fe_group']];
381
382 1
            if (empty($accessRestriction)) {
383
                // public
384 1
                $accessRestriction = '0';
385
            }
386
        }
387
388 19
        return 'r:' . $accessRestriction;
389
    }
390
391
    /**
392
     * Sends the documents to the field processing service which takes care of
393
     * manipulating fields as defined in the field's configuration.
394
     *
395
     * @param Item $item An index queue item
396
     * @param array $documents An array of Apache_Solr_Document objects to manipulate.
397
     * @return array Array of manipulated Apache_Solr_Document objects.
398
     */
399 19
    protected function processDocuments(Item $item, array $documents)
400
    {
401
        // needs to respect the TS settings for the page the item is on, conditions may apply
402 19
        $solrConfiguration = Util::getSolrConfigurationFromPageId($item->getRootPageUid());
403 19
        $fieldProcessingInstructions = $solrConfiguration->getIndexFieldProcessingInstructionsConfiguration();
404
405
        // same as in the FE indexer
406 19
        if (is_array($fieldProcessingInstructions)) {
407 19
            $service = GeneralUtility::makeInstance(Service::class);
408 19
            $service->processDocuments($documents, $fieldProcessingInstructions);
409
        }
410
411 19
        return $documents;
412
    }
413
414
    /**
415
     * Allows third party extensions to provide additional documents which
416
     * should be indexed for the current item.
417
     *
418
     * @param Item $item The item currently being indexed.
419
     * @param int $language The language uid currently being indexed.
420
     * @param Apache_Solr_Document $itemDocument The document representing the item for the given language.
421
     * @return array An array of additional Apache_Solr_Document objects to index.
422
     */
423 23
    protected function getAdditionalDocuments(Item $item, $language, Apache_Solr_Document $itemDocument)
424
    {
425 23
        $documents = [];
426
427 23
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['indexItemAddDocuments'])) {
428 4
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['indexItemAddDocuments'] as $classReference) {
429 4
                if (!class_exists($classReference)) {
430 2
                    throw new \InvalidArgumentException('Class does not exits' . $classReference, 1490363487);
431
                }
432 2
                $additionalIndexer = GeneralUtility::makeInstance($classReference);
433 2
                if ($additionalIndexer instanceof AdditionalIndexQueueItemIndexer) {
434 1
                    $additionalDocuments = $additionalIndexer->getAdditionalItemDocuments($item, $language, $itemDocument);
435
436 1
                    if (is_array($additionalDocuments)) {
437 1
                        $documents = array_merge($documents,
438 1
                            $additionalDocuments);
439
                    }
440
                } else {
441 1
                    throw new \UnexpectedValueException(
442 1
                        get_class($additionalIndexer) . ' must implement interface ' . AdditionalIndexQueueItemIndexer::class,
443 2
                        1326284551
444
                    );
445
                }
446
            }
447
        }
448 20
        return $documents;
449
    }
450
451
    /**
452
     * Provides a hook to manipulate documents right before they get added to
453
     * the Solr index.
454
     *
455
     * @param Item $item The item currently being indexed.
456
     * @param int $language The language uid of the documents
457
     * @param array $documents An array of documents to be indexed
458
     * @return array An array of modified documents
459
     */
460 19
    protected function preAddModifyDocuments(Item $item, $language, array $documents)
461
    {
462 19
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments'])) {
463
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments'] as $classReference) {
464
                $documentsModifier = GeneralUtility::getUserObj($classReference);
465
466
                if ($documentsModifier instanceof PageIndexerDocumentsModifier) {
467
                    $documents = $documentsModifier->modifyDocuments($item, $language, $documents);
468
                } else {
469
                    throw new \RuntimeException(
470
                        'The class "' . get_class($documentsModifier)
471
                        . '" registered as document modifier in hook
472
							preAddModifyDocuments must implement interface
473
							ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerDocumentsModifier',
474
                        1309522677
475
                    );
476
                }
477
            }
478
        }
479
480 19
        return $documents;
481
    }
482
483
    // Initialization
484
485
    /**
486
     * Gets the Solr connections applicable for an item.
487
     *
488
     * The connections include the default connection and connections to be used
489
     * for translations of an item.
490
     *
491
     * @param Item $item An index queue item
492
     * @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
493
     */
494 21
    protected function getSolrConnectionsByItem(Item $item)
495
    {
496 21
        $solrConnections = [];
497
498 21
        $pageId = $item->getRootPageUid();
499 21
        if ($item->getType() === 'pages') {
500 3
            $pageId = $item->getRecordUid();
501
        }
502
503
        // Solr configurations possible for this item
504 21
        $site = $item->getSite();
505
506 21
        $solrConfigurationsBySite = $this->connectionManager->getConfigurationsBySite($site);
507 21
        $siteLanguages = [];
508 21
        foreach ($solrConfigurationsBySite as $solrConfiguration) {
509 21
            $siteLanguages[] = $solrConfiguration['language'];
510
        }
511
512 21
        $defaultLanguageUid = $this->getDefaultLanguageUid($item, $site->getRootPage(), $siteLanguages);
513 21
        $translationOverlays = $this->getTranslationOverlaysWithConfiguredSite($pageId, $site, $defaultLanguageUid, $siteLanguages);
514
515 21
        $defaultConnection = $this->connectionManager->getConnectionByPageId($pageId, 0, $item->getMountPointIdentifier());
516 21
        $translationConnections = $this->getConnectionsForIndexableLanguages($translationOverlays);
517
518 21
        if ($defaultLanguageUid == 0) {
519 20
            $solrConnections[0] = $defaultConnection;
520
        }
521
522 21
        foreach ($translationConnections as $systemLanguageUid => $solrConnection) {
523 7
            $solrConnections[$systemLanguageUid] = $solrConnection;
524
        }
525
526 21
        return $solrConnections;
527
    }
528
529
    /**
530
     * Retrieves only translation overlays where a solr site is configured.
531
     *
532
     * @param int $pageId
533
     * @param Site $site
534
     * @param int $defaultLanguageUid
535
     * @param $siteLanguages
536
     * @return array
537
     */
538 21
    protected function getTranslationOverlaysWithConfiguredSite($pageId, Site $site, $defaultLanguageUid, $siteLanguages)
539
    {
540 21
        $translationOverlays = $this->getTranslationOverlaysForPage($pageId, $site->getSysLanguageMode($defaultLanguageUid));
541
542 21
        foreach ($translationOverlays as $key => $translationOverlay) {
543 7
            if (!in_array($translationOverlay['sys_language_uid'], $siteLanguages)) {
544 7
                unset($translationOverlays[$key]);
545
            }
546
        }
547
548 21
        return $translationOverlays;
549
    }
550
551
    /**
552
     * @param Item $item An index queue item
553
     * @param array $rootPage
554
     * @param array $siteLanguages
555
     *
556
     * @return int
557
     * @throws \Apache_Solr_Exception
558
     */
559 21
    private function getDefaultLanguageUid(Item $item, array $rootPage, array $siteLanguages)
560
    {
561 21
        $defaultLanguageUid = 0;
562 21
        if (($rootPage['l18n_cfg'] & 1) == 1 && count($siteLanguages) > 1) {
563 1
            unset($siteLanguages[array_search('0', $siteLanguages)]);
564 1
            $defaultLanguageUid = $siteLanguages[min(array_keys($siteLanguages))];
565 20
        } elseif (($rootPage['l18n_cfg'] & 1) == 1 && count($siteLanguages) == 1) {
566
            $message = 'Root page ' . (int)$item->getRootPageUid() . ' is set to hide default translation, but no other language is configured!';
567
            throw new \Apache_Solr_Exception($message);
568
        }
569
570 21
        return $defaultLanguageUid;
571
    }
572
573
    /**
574
     * Finds the alternative page language overlay records for a page based on
575
     * the sys_language_mode.
576
     *
577
     * Possible Language Modes:
578
     * 1) content_fallback --> all languages
579
     * 2) strict --> available languages with page overlay
580
     * 3) ignore --> available languages with page overlay
581
     * 4) unknown mode or blank --> all languages
582
     *
583
     * @param int $pageId Page ID.
584
     * @param string $languageMode
585
     * @return array An array of translation overlays (or fake overlays) found for the given page.
586
     */
587 21
    protected function getTranslationOverlaysForPage($pageId, $languageMode)
588
    {
589 21
        $translationOverlays = [];
590 21
        $pageId = intval($pageId);
591
592 21
        $languageModes = ['content_fallback', 'strict', 'ignore'];
593 21
        $hasOverlayMode = in_array($languageMode, $languageModes,
594 21
            true);
595 21
        $isContentFallbackMode = ($languageMode === 'content_fallback');
596
597 21
        if ($hasOverlayMode && !$isContentFallbackMode) {
598 6
            $translationOverlays = $this->pagesRepository->findTranslationOverlaysByPageId($pageId);
599
        } else {
600
            // ! If no sys_language_mode is configured, all languages will be indexed !
601 15
            $languages = $this->getSystemLanguages();
602 15
            foreach ($languages as $language) {
603 15
                if ($language['uid'] <= 0) {
604 15
                    continue;
605
                }
606 1
                $translationOverlays[] = [
607 1
                    'pid' => $pageId,
608 1
                    'sys_language_uid' => $language['uid'],
609
                ];
610
            }
611
        }
612
613 21
        return $translationOverlays;
614
    }
615
616
    /**
617
     * Returns an array of system languages.
618
     *
619
     * @return array
620
     */
621 15
    protected function getSystemLanguages()
622
    {
623 15
        return GeneralUtility::makeInstance(TranslationConfigurationProvider::class)->getSystemLanguages();
624
    }
625
626
    /**
627
     * Checks for which languages connections have been configured and returns
628
     * these connections.
629
     *
630
     * @param array $translationOverlays An array of translation overlays to check for configured connections.
631
     * @return array An array of ApacheSolrForTypo3\Solr\System\Solr\SolrConnection connections.
632
     */
633 21
    protected function getConnectionsForIndexableLanguages(array $translationOverlays)
634
    {
635 21
        $connections = [];
636
637 21
        foreach ($translationOverlays as $translationOverlay) {
638 7
            $pageId = $translationOverlay['pid'];
639 7
            $languageId = $translationOverlay['sys_language_uid'];
640
641
            try {
642 7
                $connection = $this->connectionManager->getConnectionByPageId($pageId, $languageId);
643 7
                $connections[$languageId] = $connection;
644 7
            } catch (NoSolrConnectionFoundException $e) {
645
                // ignore the exception as we seek only those connections
646
                // actually available
647
            }
648
        }
649
650 21
        return $connections;
651
    }
652
653
    // Utility methods
654
655
    // FIXME extract log() and setLogging() to ApacheSolrForTypo3\Solr\IndexQueue\AbstractIndexer
656
    // FIXME extract an interface Tx_Solr_IndexQueue_ItemInterface
657
658
    /**
659
     * Enables logging dependent on the configuration of the item's site
660
     *
661
     * @param Item $item An item being indexed
662
     * @return    void
663
     */
664 20
    protected function setLogging(Item $item)
665
    {
666 20
        $solrConfiguration = Util::getSolrConfigurationFromPageId($item->getRootPageUid());
667 20
        $this->loggingEnabled = $solrConfiguration->getLoggingIndexingQueueOperationsByConfigurationNameWithFallBack(
668 20
            $item->getIndexingConfigurationName()
669
        );
670 20
    }
671
672
    /**
673
     * Logs the item and what document was created from it
674
     *
675
     * @param Item $item The item that is being indexed.
676
     * @param array $itemDocuments An array of Solr documents created from the item's data
677
     * @param Apache_Solr_Response $response The Solr response for the particular index document
678
     */
679 19
    protected function log(Item $item, array $itemDocuments, Apache_Solr_Response $response)
680
    {
681 19
        if (!$this->loggingEnabled) {
682 19
            return;
683
        }
684
685
        $message = 'Index Queue indexing ' . $item->getType() . ':' . $item->getRecordUid() . ' - ';
686
687
        // preparing data
688
        $documents = [];
689
        foreach ($itemDocuments as $document) {
690
            $documents[] = (array)$document;
691
        }
692
693
        $logData = ['item' => (array)$item, 'documents' => $documents, 'response' => (array)$response];
694
695
        if ($response->getHttpStatus() == 200) {
696
            $severity = SolrLogManager::NOTICE;
697
            $message .= 'Success';
698
        } else {
699
            $severity = SolrLogManager::ERROR;
700
            $message .= 'Failure';
701
702
            $logData['status'] = $response->getHttpStatus();
703
            $logData['status message'] = $response->getHttpStatusMessage();
704
        }
705
706
        $this->logger->log($severity, $message, $logData);
707
    }
708
}
709