Passed
Push — master ( 48f77b...329461 )
by Timo
15:28
created

Indexer::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 7
cp 0.8571
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2.0116
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\Site;
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 12
    public function __construct(array $options = [], IdBuilder $idBuilder = null)
105
    {
106 12
        $this->logger = GeneralUtility::makeInstance(SolrLogManager::class, __CLASS__);
107 12
        $this->options = $options;
108 12
        $this->connectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
109 12
        $this->variantIdBuilder = is_null($idBuilder) ? GeneralUtility::makeInstance(IdBuilder::class) : $idBuilder;
110 12
    }
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 11
                $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 11
            && !empty($languageField)
252 11
            && $itemRecord[$languageField] != $language
253 11
            && $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
        $solrConfiguration = Util::getSolrConfigurationFromPageId($item->getRecordPageId(), true, $language);
276 11
        $indexConfigurationName = $item->getIndexingConfigurationName();
277 11
        $fields = $solrConfiguration->getIndexQueueFieldsConfigurationByConfigurationName($indexConfigurationName, []);
278
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
        return $fields;
285
    }
286
287
    /**
288
     * Converts an item array (record) to a Solr document by mapping the
289
     * record's fields onto Solr document fields as configured in TypoScript.
290
     *
291
     * @param Item $item An index queue item
292
     * @param int $language Language Id
293
     * @return Apache_Solr_Document The Solr document converted from the record
294
     */
295 11
    protected function itemToDocument(Item $item, $language = 0)
296
    {
297 11
        $document = null;
298
299 11
        $itemRecord = $this->getFullItemRecord($item, $language);
300 11
        if (!is_null($itemRecord)) {
301 11
            $itemIndexingConfiguration = $this->getItemTypeConfiguration($item, $language);
302 11
            $document = $this->getBaseDocument($item, $itemRecord);
303 11
            $document = $this->addDocumentFieldsFromTyposcript($document, $itemIndexingConfiguration, $itemRecord);
304
        }
305
306 11
        return $document;
307
    }
308
309
    /**
310
     * Creates a Solr document with the basic / core fields set already.
311
     *
312
     * @param Item $item The item to index
313
     * @param array $itemRecord The record to use to build the base document
314
     * @return Apache_Solr_Document A basic Solr document
315
     */
316 11
    protected function getBaseDocument(Item $item, array $itemRecord)
317
    {
318 11
        $site = GeneralUtility::makeInstance(Site::class, $item->getRootPageUid());
319 11
        $document = GeneralUtility::makeInstance(Apache_Solr_Document::class);
320
        /* @var $document Apache_Solr_Document */
321
322
        // required fields
323 11
        $document->setField('id', Util::getDocumentId(
324 11
            $item->getType(),
325 11
            $itemRecord['pid'],
326 11
            $itemRecord['uid']
327
        ));
328 11
        $document->setField('type', $item->getType());
329 11
        $document->setField('appKey', 'EXT:solr');
330
331
        // site, siteHash
332 11
        $document->setField('site', $site->getDomain());
333 11
        $document->setField('siteHash', $site->getSiteHash());
334
335
        // uid, pid
336 11
        $document->setField('uid', $itemRecord['uid']);
337 11
        $document->setField('pid', $itemRecord['pid']);
338
339
        // variantId
340 11
        $variantId = $this->variantIdBuilder->buildFromTypeAndUid($item->getType(), $itemRecord['uid']);
341 11
        $document->setField('variantId', $variantId);
342
343
        // created, changed
344 11
        if (!empty($GLOBALS['TCA'][$item->getType()]['ctrl']['crdate'])) {
345 11
            $document->setField('created',
346 11
                $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['crdate']]);
347
        }
348 11
        if (!empty($GLOBALS['TCA'][$item->getType()]['ctrl']['tstamp'])) {
349 11
            $document->setField('changed',
350 11
                $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['tstamp']]);
351
        }
352
353
        // access, endtime
354 11
        $document->setField('access', $this->getAccessRootline($item));
355 11
        if (!empty($GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['endtime'])
356 11
            && $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['endtime']] != 0
357
        ) {
358
            $document->setField('endtime',
359
                $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['endtime']]);
360
        }
361
362 11
        return $document;
363
    }
364
365
    /**
366
     * Generates an Access Rootline for an item.
367
     *
368
     * @param Item $item Index Queue item to index.
369
     * @return string The Access Rootline for the item
370
     */
371 11
    protected function getAccessRootline(Item $item)
372
    {
373 11
        $accessRestriction = '0';
374 11
        $itemRecord = $item->getRecord();
375
376
        // TODO support access restrictions set on storage page
377
378 11
        if (isset($GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['fe_group'])) {
379 1
            $accessRestriction = $itemRecord[$GLOBALS['TCA'][$item->getType()]['ctrl']['enablecolumns']['fe_group']];
380
381 1
            if (empty($accessRestriction)) {
382
                // public
383 1
                $accessRestriction = '0';
384
            }
385
        }
386
387 11
        return 'r:' . $accessRestriction;
388
    }
389
390
    /**
391
     * Sends the documents to the field processing service which takes care of
392
     * manipulating fields as defined in the field's configuration.
393
     *
394
     * @param Item $item An index queue item
395
     * @param array $documents An array of Apache_Solr_Document objects to manipulate.
396
     * @return array Array of manipulated Apache_Solr_Document objects.
397
     */
398 11
    protected function processDocuments(Item $item, array $documents)
399
    {
400
        // needs to respect the TS settings for the page the item is on, conditions may apply
401 11
        $solrConfiguration = Util::getSolrConfigurationFromPageId($item->getRootPageUid());
402 11
        $fieldProcessingInstructions = $solrConfiguration->getIndexFieldProcessingInstructionsConfiguration();
403
404
        // same as in the FE indexer
405 11
        if (is_array($fieldProcessingInstructions)) {
406 11
            $service = GeneralUtility::makeInstance(Service::class);
407 11
            $service->processDocuments(
408
                $documents,
409
                $fieldProcessingInstructions
410
            );
411
        }
412
413 11
        return $documents;
414
    }
415
416
    /**
417
     * Allows third party extensions to provide additional documents which
418
     * should be indexed for the current item.
419
     *
420
     * @param Item $item The item currently being indexed.
421
     * @param int $language The language uid currently being indexed.
422
     * @param Apache_Solr_Document $itemDocument The document representing the item for the given language.
423
     * @return array An array of additional Apache_Solr_Document objects to index.
424
     */
425 11
    protected function getAdditionalDocuments(
426
        Item $item,
427
        $language,
428
        Apache_Solr_Document $itemDocument
429
    ) {
430 11
        $documents = [];
431
432 11
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['indexItemAddDocuments'])) {
433
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['indexItemAddDocuments'] as $classReference) {
434
                $additionalIndexer = GeneralUtility::getUserObj($classReference);
435
436
                if ($additionalIndexer instanceof AdditionalIndexQueueItemIndexer) {
437
                    $additionalDocuments = $additionalIndexer->getAdditionalItemDocuments($item,
438
                        $language, $itemDocument);
439
440
                    if (is_array($additionalDocuments)) {
441
                        $documents = array_merge($documents,
442
                            $additionalDocuments);
443
                    }
444
                } else {
445
                    throw new \UnexpectedValueException(
446
                        get_class($additionalIndexer) . ' must implement interface ' . AdditionalIndexQueueItemIndexer::class,
447
                        1326284551
448
                    );
449
                }
450
            }
451
        }
452
453 11
        return $documents;
454
    }
455
456
    /**
457
     * Provides a hook to manipulate documents right before they get added to
458
     * the Solr index.
459
     *
460
     * @param Item $item The item currently being indexed.
461
     * @param int $language The language uid of the documents
462
     * @param array $documents An array of documents to be indexed
463
     * @return array An array of modified documents
464
     */
465 11
    protected function preAddModifyDocuments(
466
        Item $item,
467
        $language,
468
        array $documents
469
    ) {
470 11
        if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments'])) {
471
            foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments'] as $classReference) {
472
                $documentsModifier = &GeneralUtility::getUserObj($classReference);
473
474
                if ($documentsModifier instanceof PageIndexerDocumentsModifier) {
475
                    $documents = $documentsModifier->modifyDocuments($item,
476
                        $language, $documents);
477
                } else {
478
                    throw new \RuntimeException(
479
                        'The class "' . get_class($documentsModifier)
480
                        . '" registered as document modifier in hook
481
							preAddModifyDocuments must implement interface
482
							ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerDocumentsModifier',
483
                        1309522677
484
                    );
485
                }
486
            }
487
        }
488
489 11
        return $documents;
490
    }
491
492
    // Initialization
493
494
    /**
495
     * Gets the Solr connections applicable for an item.
496
     *
497
     * The connections include the default connection and connections to be used
498
     * for translations of an item.
499
     *
500
     * @param Item $item An index queue item
501
     * @return array An array of ApacheSolrForTypo3\Solr\SolrService connections, the array's keys are the sys_language_uid of the language of the connection
502
     */
503 12
    protected function getSolrConnectionsByItem(Item $item)
504
    {
505 12
        $solrConnections = [];
506
507 12
        $pageId = $item->getRootPageUid();
508 12
        if ($item->getType() == 'pages') {
509 2
            $pageId = $item->getRecordUid();
510
        }
511
512
        // Solr configurations possible for this item
513 12
        $site = $item->getSite();
514 12
        $solrConfigurationsBySite = $this->connectionManager->getConfigurationsBySite($site);
515
516 12
        $siteLanguages = [];
517 12
        foreach ($solrConfigurationsBySite as $solrConfiguration) {
518 12
            $siteLanguages[] = $solrConfiguration['language'];
519
        }
520
521 12
        $translationOverlays = $this->getTranslationOverlaysForPage($pageId, $site->getSysLanguageMode());
522 12
        foreach ($translationOverlays as $key => $translationOverlay) {
523 1
            if (!in_array($translationOverlay['sys_language_uid'],
524
                $siteLanguages)
525
            ) {
526 1
                unset($translationOverlays[$key]);
527
            }
528
        }
529
530 12
        $defaultConnection = $this->connectionManager->getConnectionByPageId($pageId);
531 12
        $translationConnections = $this->getConnectionsForIndexableLanguages($translationOverlays);
532
533 12
        $solrConnections[0] = $defaultConnection;
534 12
        foreach ($translationConnections as $systemLanguageUid => $solrConnection) {
535 1
            $solrConnections[$systemLanguageUid] = $solrConnection;
536
        }
537
538 12
        return $solrConnections;
539
    }
540
541
    /**
542
     * Finds the alternative page language overlay records for a page based on
543
     * the sys_language_mode.
544
     *
545
     * Possible Language Modes:
546
     * 1) content_fallback --> all languages
547
     * 2) strict --> available languages with page overlay
548
     * 3) ignore --> available languages with page overlay
549
     * 4) unknown mode or blank --> all languages
550
     *
551
     * @param int $pageId Page ID.
552
     * @param string $languageMode
553
     * @return array An array of translation overlays (or fake overlays) found for the given page.
554
     */
555 12
    protected function getTranslationOverlaysForPage($pageId, $languageMode)
556
    {
557 12
        $translationOverlays = [];
558 12
        $pageId = intval($pageId);
559
560 12
        $languageModes = ['content_fallback', 'strict', 'ignore'];
561 12
        $hasOverlayMode = in_array($languageMode, $languageModes,
562 12
            true);
563 12
        $isContentFallbackMode = ($languageMode === 'content_fallback');
564
565 12
        if ($hasOverlayMode && !$isContentFallbackMode) {
566 1
            $translationOverlays = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
567 1
                'pid, sys_language_uid',
568 1
                'pages_language_overlay',
569 1
                'pid = ' . $pageId
570 1
                . BackendUtility::deleteClause('pages_language_overlay')
571 1
                . BackendUtility::BEenableFields('pages_language_overlay')
572
            );
573
        } else {
574
            // ! If no sys_language_mode is configured, all languages will be indexed !
575 11
            $languages = $this->getSystemLanguages();
576
577 11
            foreach ($languages as $language) {
578 11
                if ($language['uid'] <= 0) {
579 11
                    continue;
580
                }
581
                $translationOverlays[] = [
582
                    'pid' => $pageId,
583
                    'sys_language_uid' => $language['uid'],
584
                ];
585
            }
586
        }
587
588 12
        return $translationOverlays;
589
    }
590
591
    /**
592
     * Returns an array of system languages.
593
     *
594
     * @return array
595
     */
596 11
    protected function getSystemLanguages()
597
    {
598 11
        return GeneralUtility::makeInstance(TranslationConfigurationProvider::class)->getSystemLanguages();
599
    }
600
601
    /**
602
     * Checks for which languages connections have been configured and returns
603
     * these connections.
604
     *
605
     * @param array $translationOverlays An array of translation overlays to check for configured connections.
606
     * @return array An array of ApacheSolrForTypo3\Solr\SolrService connections.
607
     */
608 12
    protected function getConnectionsForIndexableLanguages(
609
        array $translationOverlays
610
    ) {
611 12
        $connections = [];
612
613 12
        foreach ($translationOverlays as $translationOverlay) {
614 1
            $pageId = $translationOverlay['pid'];
615 1
            $languageId = $translationOverlay['sys_language_uid'];
616
617
            try {
618 1
                $connection = $this->connectionManager->getConnectionByPageId($pageId,
619
                    $languageId);
620 1
                $connections[$languageId] = $connection;
621 1
            } catch (NoSolrConnectionFoundException $e) {
622
                // ignore the exception as we seek only those connections
623
                // actually available
624
            }
625
        }
626
627 12
        return $connections;
628
    }
629
630
    // Utility methods
631
632
    // FIXME extract log() and setLogging() to ApacheSolrForTypo3\Solr\IndexQueue\AbstractIndexer
633
    // FIXME extract an interface Tx_Solr_IndexQueue_ItemInterface
634
635
    /**
636
     * Enables logging dependent on the configuration of the item's site
637
     *
638
     * @param Item $item An item being indexed
639
     * @return    void
640
     */
641 12
    protected function setLogging(Item $item)
642
    {
643 12
        $solrConfiguration = Util::getSolrConfigurationFromPageId($item->getRootPageUid());
644 12
        $this->loggingEnabled = $solrConfiguration->getLoggingIndexingQueueOperationsByConfigurationNameWithFallBack(
645 12
            $item->getIndexingConfigurationName()
646
        );
647 12
    }
648
649
    /**
650
     * Logs the item and what document was created from it
651
     *
652
     * @param Item $item The item that is being indexed.
653
     * @param array $itemDocuments An array of Solr documents created from the item's data
654
     * @param Apache_Solr_Response $response The Solr response for the particular index document
655
     */
656 11
    protected function log(
657
        Item $item,
658
        array $itemDocuments,
659
        Apache_Solr_Response $response
660
    ) {
661 11
        if (!$this->loggingEnabled) {
662 11
            return;
663
        }
664
665
        $message = 'Index Queue indexing ' . $item->getType() . ':'
666
            . $item->getRecordUid() . ' - ';
667
668
        // preparing data
669
        $documents = [];
670
        foreach ($itemDocuments as $document) {
671
            $documents[] = (array)$document;
672
        }
673
674
        $logData = [
675
            'item' => (array)$item,
676
            'documents' => $documents,
677
            'response' => (array)$response
678
        ];
679
680
        if ($response->getHttpStatus() == 200) {
681
            $severity = SolrLogManager::NOTICE;
682
            $message .= 'Success';
683
        } else {
684
            $severity = SolrLogManager::ERROR;
685
            $message .= 'Failure';
686
687
            $logData['status'] = $response->getHttpStatus();
688
            $logData['status message'] = $response->getHttpStatusMessage();
689
        }
690
691
        $this->logger->log(
692
            $severity,
693
            $message,
694
            $logData
695
        );
696
    }
697
}
698