Passed
Pull Request — release-11.5.x (#3206)
by Michael
41:07
created

PageFieldMappingIndexer::getPageDocument()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 1
crap 5
1
<?php
2
namespace ApacheSolrForTypo3\Solr\IndexQueue\FrontendHelper;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
// TODO use/extend ApacheSolrForTypo3\Solr\IndexQueue\AbstractIndexer
28
use ApacheSolrForTypo3\Solr\IndexQueue\AbstractIndexer;
29
use ApacheSolrForTypo3\Solr\IndexQueue\InvalidFieldNameException;
30
use ApacheSolrForTypo3\Solr\SubstitutePageIndexer;
31
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
32
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
33
use ApacheSolrForTypo3\Solr\Util;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
36
37
/**
38
 * Indexer to add / overwrite page document fields as defined in
39
 * plugin.tx_solr.index.queue.pages.fields.
40
 *
41
 * @author Ingo Renner <[email protected]>
42
 */
43
class PageFieldMappingIndexer implements SubstitutePageIndexer
44
{
45
    /**
46
     * @var \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration
47
     */
48
    protected $configuration;
49
50
    /**
51
     * @var string
52
     */
53
    protected $pageIndexingConfigurationName = 'pages';
54
55
    /**
56
     * @param TypoScriptConfiguration $configuration
57
     */
58 40
    public function __construct(TypoScriptConfiguration $configuration = null)
59
    {
60 40
        $this->configuration = $configuration == null ? Util::getSolrConfiguration() : $configuration;
61
    }
62
63
    /**
64
     * @param string $pageIndexingConfigurationName
65
     */
66 40
    public function setPageIndexingConfigurationName($pageIndexingConfigurationName)
67
    {
68 40
        $this->pageIndexingConfigurationName = $pageIndexingConfigurationName;
69
    }
70
71
    /**
72
     * Returns a substitute document for the currently being indexed page.
73
     *
74
     * Uses the original document and adds fields as defined in
75
     * plugin.tx_solr.index.queue.pages.fields.
76
     *
77
     * @param Document $pageDocument The original page document.
78
     * @return Document A Apache Solr Document object that replace the default page document
79
     */
80 40
    public function getPageDocument(Document $pageDocument)
81
    {
82 40
        $substitutePageDocument = clone $pageDocument;
83
84
85 40
        $mappedFields = $this->getMappedFields($pageDocument);
86 40
        foreach ($mappedFields as $fieldName => $fieldValue) {
87 40
            if (isset($substitutePageDocument->{$fieldName})) {
88
                // reset = overwrite, especially important to not make fields
89
                // multi valued where they may not accept multiple values
90 2
                unset($substitutePageDocument->{$fieldName});
91
            }
92
93
            // add new field / overwrite field if it was set before
94 40
            if ($fieldValue !== '' && $fieldValue !== null) {
95 37
                $substitutePageDocument->setField($fieldName, $fieldValue);
96
            }
97
        }
98
99 40
        return $substitutePageDocument;
100
    }
101
102
    /**
103
     * Gets the mapped fields as an array mapping field names to values.
104
     *
105
     * @throws InvalidFieldNameException
106
     * @param Document $pageDocument The original page document.
107
     * @return array An array mapping field names to their values.
108
     */
109 40
    protected function getMappedFields(Document $pageDocument)
110
    {
111 40
        $fields = [];
112
113 40
        $mappedFieldNames = $this->configuration->getIndexQueueMappedFieldsByConfigurationName($this->pageIndexingConfigurationName);
114
115 40
        foreach ($mappedFieldNames as $mappedFieldName) {
116 40
            if (!AbstractIndexer::isAllowedToOverrideField($mappedFieldName)) {
117
                throw new InvalidFieldNameException(
118
                    'Must not overwrite field "type".',
119
                    1435441863
120
                );
121
            }
122 40
            $fields[$mappedFieldName] = $this->resolveFieldValue($mappedFieldName, $pageDocument);
123
        }
124
125 40
        return $fields;
126
    }
127
128
    /**
129
     * Resolves a field mapping to its value depending on its configuration.
130
     *
131
     * Allows to put the page record through cObj processing if wanted / needed.
132
     * Otherwise the plain page record field value is used.
133
     *
134
     * @param string $solrFieldName The Solr field name to resolve the value from the item's record
135
     * @return string The resolved string value to be indexed
136
     */
137 40
    protected function resolveFieldValue($solrFieldName, Document $pageDocument)
138
    {
139 40
        $pageRecord = $GLOBALS['TSFE']->page;
140
141 40
        $pageIndexingConfiguration = $this->configuration->getIndexQueueFieldsConfigurationByConfigurationName($this->pageIndexingConfigurationName);
142
143 40
        if (isset($pageIndexingConfiguration[$solrFieldName . '.'])) {
144 8
            $pageRecord = AbstractIndexer::addVirtualContentFieldToRecord($pageDocument, $pageRecord);
145
146
            // configuration found => need to resolve a cObj
147 8
            $contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class, $GLOBALS['TSFE']);
148 8
            $contentObject->start($pageRecord, 'pages');
149
150 8
            $fieldValue = $contentObject->cObjGetSingle(
151
                $pageIndexingConfiguration[$solrFieldName],
152 8
                $pageIndexingConfiguration[$solrFieldName . '.']
153
            );
154
155 8
            if (AbstractIndexer::isSerializedValue($pageIndexingConfiguration, $solrFieldName)) {
156 8
                $fieldValue = unserialize($fieldValue);
157
            }
158
        } else {
159 39
            $fieldValue = $pageRecord[$pageIndexingConfiguration[$solrFieldName]];
160
        }
161
162 40
        return $fieldValue;
163
    }
164
}
165