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