|
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 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
|
|
|
// 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\Util; |
|
33
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
34
|
|
|
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Indexer to add / overwrite page document fields as defined in |
|
38
|
|
|
* plugin.tx_solr.index.queue.pages.fields. |
|
39
|
|
|
* |
|
40
|
|
|
* @author Ingo Renner <[email protected]> |
|
41
|
|
|
*/ |
|
42
|
|
|
class PageFieldMappingIndexer implements SubstitutePageIndexer |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* @var \ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $configuration; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $pageIndexingConfigurationName = 'pages'; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param TypoScriptConfiguration $configuration |
|
56
|
|
|
*/ |
|
57
|
|
|
public function __construct(TypoScriptConfiguration $configuration = null) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->configuration = $configuration == null ? Util::getSolrConfiguration() : $configuration; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
/** |
|
63
|
|
|
* @param string $pageIndexingConfigurationName |
|
64
|
2 |
|
*/ |
|
65
|
2 |
|
public function setPageIndexingConfigurationName($pageIndexingConfigurationName) |
|
66
|
2 |
|
{ |
|
67
|
2 |
|
$this->pageIndexingConfigurationName = $pageIndexingConfigurationName; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns a substitute document for the currently being indexed page. |
|
72
|
|
|
* |
|
73
|
|
|
* Uses the original document and adds fields as defined in |
|
74
|
|
|
* plugin.tx_solr.index.queue.pages.fields. |
|
75
|
|
|
* |
|
76
|
|
|
* @param \Apache_Solr_Document $pageDocument The original page document. |
|
77
|
|
|
* @return \Apache_Solr_Document A Apache_Solr_Document object that replace the default page document |
|
78
|
2 |
|
*/ |
|
79
|
|
|
public function getPageDocument(\Apache_Solr_Document $pageDocument) |
|
80
|
2 |
|
{ |
|
81
|
2 |
|
$substitutePageDocument = clone $pageDocument; |
|
82
|
2 |
|
|
|
83
|
2 |
|
|
|
84
|
|
|
$mappedFields = $this->getMappedFields(); |
|
85
|
|
|
foreach ($mappedFields as $fieldName => $fieldValue) { |
|
86
|
|
|
if (isset($substitutePageDocument->{$fieldName})) { |
|
87
|
|
|
// reset = overwrite, especially important to not make fields |
|
88
|
|
|
// multi valued where they may not accept multiple values |
|
89
|
|
|
unset($substitutePageDocument->{$fieldName}); |
|
90
|
2 |
|
} |
|
91
|
2 |
|
|
|
92
|
|
|
// add new field / overwrite field if it was set before |
|
93
|
|
|
if ($fieldValue !== '' && $fieldValue !== null) { |
|
94
|
|
|
$substitutePageDocument->setField($fieldName, $fieldValue); |
|
95
|
2 |
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $substitutePageDocument; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Gets the mapped fields as an array mapping field names to values. |
|
103
|
|
|
* |
|
104
|
2 |
|
* @throws InvalidFieldNameException |
|
105
|
|
|
* @return array An array mapping field names to their values. |
|
106
|
2 |
|
*/ |
|
107
|
|
|
protected function getMappedFields() |
|
108
|
2 |
|
{ |
|
109
|
2 |
|
$fields = array(); |
|
110
|
|
|
|
|
111
|
|
|
$mappedFieldNames = $this->configuration->getIndexQueueMappedFieldsByConfigurationName($this->pageIndexingConfigurationName); |
|
112
|
|
|
|
|
113
|
|
|
foreach ($mappedFieldNames as $mappedFieldName) { |
|
114
|
|
|
if (!AbstractIndexer::isAllowedToOverrideField($mappedFieldName)) { |
|
115
|
2 |
|
throw new InvalidFieldNameException( |
|
116
|
|
|
'Must not overwrite field "type".', |
|
117
|
|
|
1435441863 |
|
118
|
2 |
|
); |
|
119
|
|
|
} |
|
120
|
|
|
$fields[$mappedFieldName] = $this->resolveFieldValue($mappedFieldName); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $fields; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Resolves a field mapping to its value depending on its configuration. |
|
128
|
|
|
* |
|
129
|
|
|
* Allows to put the page record through cObj processing if wanted / needed. |
|
130
|
2 |
|
* Otherwise the plain page record field value is used. |
|
131
|
|
|
* |
|
132
|
2 |
|
* @param string $solrFieldName The Solr field name to resolve the value from the item's record |
|
133
|
|
|
* @return string The resolved string value to be indexed |
|
134
|
2 |
|
*/ |
|
135
|
|
|
protected function resolveFieldValue($solrFieldName) |
|
136
|
1 |
|
{ |
|
137
|
1 |
|
$pageRecord = $GLOBALS['TSFE']->page; |
|
138
|
|
|
|
|
139
|
1 |
|
$pageIndexingConfiguration = $this->configuration->getIndexQueueFieldsConfigurationByConfigurationName($this->pageIndexingConfigurationName); |
|
140
|
1 |
|
|
|
141
|
1 |
|
if (isset($pageIndexingConfiguration[$solrFieldName . '.'])) { |
|
142
|
|
|
// configuration found => need to resolve a cObj |
|
143
|
|
|
$contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class); |
|
144
|
1 |
|
$contentObject->start($pageRecord, 'pages'); |
|
145
|
1 |
|
|
|
146
|
|
|
$fieldValue = $contentObject->cObjGetSingle( |
|
147
|
|
|
$pageIndexingConfiguration[$solrFieldName], |
|
148
|
2 |
|
$pageIndexingConfiguration[$solrFieldName . '.'] |
|
149
|
|
|
); |
|
150
|
|
|
|
|
151
|
2 |
|
if (AbstractIndexer::isSerializedValue($pageIndexingConfiguration, $solrFieldName)) { |
|
152
|
|
|
$fieldValue = unserialize($fieldValue); |
|
153
|
|
|
} |
|
154
|
|
|
} else { |
|
155
|
|
|
$fieldValue = $pageRecord[$pageIndexingConfiguration[$solrFieldName]]; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $fieldValue; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|