1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\IndexQueue; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2012-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
|
|
|
use ApacheSolrForTypo3\Solr\ContentObject\Classification; |
28
|
|
|
use ApacheSolrForTypo3\Solr\ContentObject\Multivalue; |
29
|
|
|
use ApacheSolrForTypo3\Solr\ContentObject\Relation; |
30
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document; |
31
|
|
|
use TYPO3\CMS\Core\Core\Environment; |
32
|
|
|
use TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser; |
33
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
34
|
|
|
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* An abstract indexer class to collect a few common methods shared with other |
38
|
|
|
* indexers. |
39
|
|
|
* |
40
|
|
|
* @author Ingo Renner <[email protected]> |
41
|
|
|
*/ |
42
|
|
|
abstract class AbstractIndexer |
43
|
|
|
{ |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Holds the type of the data to be indexed, usually that is the table name. |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $type = ''; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Holds field names that are denied to overwrite in thy indexing configuration. |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected static $unAllowedOverrideFields = ['type']; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $solrFieldName |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
67 |
|
public static function isAllowedToOverrideField($solrFieldName) |
64
|
|
|
{ |
65
|
67 |
|
return !in_array($solrFieldName, static::$unAllowedOverrideFields); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Adds fields to the document as defined in $indexingConfiguration |
70
|
|
|
* |
71
|
|
|
* @param Document $document base document to add fields to |
72
|
|
|
* @param array $indexingConfiguration Indexing configuration / mapping |
73
|
|
|
* @param array $data Record data |
74
|
|
|
* @return Document Modified document with added fields |
75
|
|
|
*/ |
76
|
18 |
|
protected function addDocumentFieldsFromTyposcript(Document $document, array $indexingConfiguration, array $data) { |
77
|
18 |
|
$data = static::addVirtualContentFieldToRecord($document, $data); |
78
|
|
|
|
79
|
|
|
// mapping of record fields => solr document fields, resolving cObj |
80
|
18 |
|
foreach ($indexingConfiguration as $solrFieldName => $recordFieldName) { |
81
|
18 |
|
if (is_array($recordFieldName)) { |
82
|
|
|
// configuration for a content object, skipping |
83
|
15 |
|
continue; |
84
|
|
|
} |
85
|
|
|
|
86
|
18 |
|
if (!static::isAllowedToOverrideField($solrFieldName)) { |
87
|
|
|
throw new InvalidFieldNameException( |
88
|
|
|
'Must not overwrite field .' . $solrFieldName, |
89
|
|
|
1435441863 |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
18 |
|
$fieldValue = $this->resolveFieldValue($indexingConfiguration, $solrFieldName, $data); |
94
|
|
|
|
95
|
18 |
|
if (is_array($fieldValue)) { |
96
|
|
|
// multi value |
97
|
9 |
|
$document->setField($solrFieldName, $fieldValue); |
98
|
|
|
} else { |
99
|
18 |
|
if ($fieldValue !== '' && $fieldValue !== null) { |
100
|
17 |
|
$document->setField($solrFieldName, $fieldValue); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
18 |
|
return $document; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Add's the content of the field 'content' from the solr document as virtual field __solr_content in the record, |
111
|
|
|
* to have it available in typoscript. |
112
|
|
|
* |
113
|
|
|
* @param Document $document |
114
|
|
|
* @param array $data |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
60 |
|
public static function addVirtualContentFieldToRecord(Document $document, array $data): array |
118
|
|
|
{ |
119
|
60 |
|
if (isset($document['content'])) { |
120
|
42 |
|
$data['__solr_content'] = $document['content']; |
121
|
42 |
|
return $data; |
122
|
|
|
} |
123
|
18 |
|
return $data; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Resolves a field to its value depending on its configuration. |
128
|
|
|
* |
129
|
|
|
* This enables you to configure the indexer to put the item/record through |
130
|
|
|
* cObj processing if wanted/needed. Otherwise the plain item/record value |
131
|
|
|
* is taken. |
132
|
|
|
* |
133
|
|
|
* @param array $indexingConfiguration Indexing configuration as defined in plugin.tx_solr_index.queue.[indexingConfigurationName].fields |
134
|
|
|
* @param string $solrFieldName A Solr field name that is configured in the indexing configuration |
135
|
|
|
* @param array $data A record or item's data |
136
|
|
|
* @return string The resolved string value to be indexed |
137
|
|
|
*/ |
138
|
18 |
|
protected function resolveFieldValue( |
139
|
|
|
array $indexingConfiguration, |
140
|
|
|
$solrFieldName, |
141
|
|
|
array $data |
142
|
|
|
) { |
143
|
18 |
|
$contentObject = GeneralUtility::makeInstance(ContentObjectRenderer::class); |
144
|
|
|
|
145
|
18 |
|
if (isset($indexingConfiguration[$solrFieldName . '.'])) { |
146
|
|
|
// configuration found => need to resolve a cObj |
147
|
|
|
|
148
|
|
|
// need to change directory to make IMAGE content objects work in BE context |
149
|
|
|
// see http://blog.netzelf.de/lang/de/tipps-und-tricks/tslib_cobj-image-im-backend |
150
|
15 |
|
$backupWorkingDirectory = getcwd(); |
151
|
15 |
|
chdir(Environment::getPublicPath() . '/'); |
152
|
|
|
|
153
|
15 |
|
$contentObject->start($data, $this->type); |
154
|
15 |
|
$fieldValue = $contentObject->cObjGetSingle( |
155
|
15 |
|
$indexingConfiguration[$solrFieldName], |
156
|
15 |
|
$indexingConfiguration[$solrFieldName . '.'] |
157
|
|
|
); |
158
|
|
|
|
159
|
15 |
|
chdir($backupWorkingDirectory); |
160
|
|
|
|
161
|
15 |
|
if ($this->isSerializedValue($indexingConfiguration, |
162
|
|
|
$solrFieldName) |
163
|
|
|
) { |
164
|
15 |
|
$fieldValue = unserialize($fieldValue); |
165
|
|
|
} |
166
|
18 |
|
} elseif (substr($indexingConfiguration[$solrFieldName], 0, |
167
|
18 |
|
1) === '<' |
168
|
|
|
) { |
169
|
|
|
$referencedTsPath = trim(substr($indexingConfiguration[$solrFieldName], |
170
|
|
|
1)); |
171
|
|
|
$typoScriptParser = GeneralUtility::makeInstance(TypoScriptParser::class); |
172
|
|
|
// $name and $conf is loaded with the referenced values. |
173
|
|
|
list($name, $conf) = $typoScriptParser->getVal($referencedTsPath, |
174
|
|
|
$GLOBALS['TSFE']->tmpl->setup); |
175
|
|
|
|
176
|
|
|
// need to change directory to make IMAGE content objects work in BE context |
177
|
|
|
// see http://blog.netzelf.de/lang/de/tipps-und-tricks/tslib_cobj-image-im-backend |
178
|
|
|
$backupWorkingDirectory = getcwd(); |
179
|
|
|
chdir(Environment::getPublicPath() . '/'); |
180
|
|
|
|
181
|
|
|
$contentObject->start($data, $this->type); |
182
|
|
|
$fieldValue = $contentObject->cObjGetSingle($name, $conf); |
183
|
|
|
|
184
|
|
|
chdir($backupWorkingDirectory); |
185
|
|
|
|
186
|
|
|
if ($this->isSerializedValue($indexingConfiguration, |
187
|
|
|
$solrFieldName) |
188
|
|
|
) { |
189
|
|
|
$fieldValue = unserialize($fieldValue); |
190
|
|
|
} |
191
|
|
|
} else { |
192
|
18 |
|
$fieldValue = $data[$indexingConfiguration[$solrFieldName]]; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// detect and correct type for dynamic fields |
196
|
|
|
|
197
|
|
|
// find last underscore, substr from there, cut off last character (S/M) |
198
|
18 |
|
$fieldType = substr($solrFieldName, strrpos($solrFieldName, '_') + 1, |
199
|
18 |
|
-1); |
200
|
18 |
|
if (is_array($fieldValue)) { |
201
|
9 |
|
foreach ($fieldValue as $key => $value) { |
202
|
9 |
|
$fieldValue[$key] = $this->ensureFieldValueType($value, |
203
|
|
|
$fieldType); |
204
|
|
|
} |
205
|
|
|
} else { |
206
|
18 |
|
$fieldValue = $this->ensureFieldValueType($fieldValue, $fieldType); |
207
|
|
|
} |
208
|
|
|
|
209
|
18 |
|
return $fieldValue; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// Utility methods |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Uses a field's configuration to detect whether its value returned by a |
216
|
|
|
* content object is expected to be serialized and thus needs to be |
217
|
|
|
* unserialized. |
218
|
|
|
* |
219
|
|
|
* @param array $indexingConfiguration Current item's indexing configuration |
220
|
|
|
* @param string $solrFieldName Current field being indexed |
221
|
|
|
* @return bool TRUE if the value is expected to be serialized, FALSE otherwise |
222
|
|
|
*/ |
223
|
60 |
|
public static function isSerializedValue(array $indexingConfiguration, $solrFieldName) |
224
|
|
|
{ |
225
|
60 |
|
$isSerialized = static::isSerializedResultFromRegisteredHook($indexingConfiguration, $solrFieldName); |
226
|
59 |
|
if ($isSerialized === true) { |
227
|
1 |
|
return $isSerialized; |
228
|
|
|
} |
229
|
|
|
|
230
|
58 |
|
$isSerialized = static::isSerializedResultFromCustomContentElement($indexingConfiguration, $solrFieldName); |
231
|
58 |
|
return $isSerialized; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Checks if the response comes from a custom content element that returns a serialized value. |
236
|
|
|
* |
237
|
|
|
* @param array $indexingConfiguration |
238
|
|
|
* @param string $solrFieldName |
239
|
|
|
* @return bool |
240
|
|
|
*/ |
241
|
58 |
|
protected static function isSerializedResultFromCustomContentElement(array $indexingConfiguration, $solrFieldName): bool |
242
|
|
|
{ |
243
|
58 |
|
$isSerialized = false; |
244
|
|
|
|
245
|
|
|
// SOLR_CLASSIFICATION - always returns serialized array |
246
|
58 |
|
if ($indexingConfiguration[$solrFieldName] == Classification::CONTENT_OBJECT_NAME) { |
247
|
1 |
|
$isSerialized = true; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// SOLR_MULTIVALUE - always returns serialized array |
251
|
58 |
|
if ($indexingConfiguration[$solrFieldName] == Multivalue::CONTENT_OBJECT_NAME) { |
252
|
2 |
|
$isSerialized = true; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
// SOLR_RELATION - returns serialized array if multiValue option is set |
256
|
58 |
|
if ($indexingConfiguration[$solrFieldName] == Relation::CONTENT_OBJECT_NAME && !empty($indexingConfiguration[$solrFieldName . '.']['multiValue'])) { |
257
|
46 |
|
$isSerialized = true; |
258
|
|
|
} |
259
|
|
|
|
260
|
58 |
|
return $isSerialized; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Checks registered hooks if a SerializedValueDetector detects a serialized response. |
265
|
|
|
* |
266
|
|
|
* @param array $indexingConfiguration |
267
|
|
|
* @param string $solrFieldName |
268
|
|
|
* @return bool |
269
|
|
|
*/ |
270
|
60 |
|
protected static function isSerializedResultFromRegisteredHook(array $indexingConfiguration, $solrFieldName) |
271
|
|
|
{ |
272
|
60 |
|
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['detectSerializedValue'])) { |
273
|
57 |
|
return false; |
274
|
|
|
} |
275
|
|
|
|
276
|
3 |
|
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['detectSerializedValue'] as $classReference) { |
277
|
2 |
|
$serializedValueDetector = GeneralUtility::makeInstance($classReference); |
278
|
2 |
|
if (!$serializedValueDetector instanceof SerializedValueDetector) { |
279
|
1 |
|
$message = get_class($serializedValueDetector) . ' must implement interface ' . SerializedValueDetector::class; |
280
|
1 |
|
throw new \UnexpectedValueException($message, 1404471741); |
281
|
|
|
} |
282
|
|
|
|
283
|
1 |
|
$isSerialized = (boolean)$serializedValueDetector->isSerializedValue($indexingConfiguration, $solrFieldName); |
284
|
1 |
|
if ($isSerialized) { |
285
|
1 |
|
return true; |
286
|
|
|
} |
287
|
|
|
} |
288
|
1 |
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Makes sure a field's value matches a (dynamic) field's type. |
292
|
|
|
* |
293
|
|
|
* @param mixed $value Value to be added to a document |
294
|
|
|
* @param string $fieldType The dynamic field's type |
295
|
|
|
* @return mixed Returns the value in the correct format for the field type |
296
|
|
|
*/ |
297
|
18 |
|
protected function ensureFieldValueType($value, $fieldType) |
298
|
|
|
{ |
299
|
18 |
|
switch ($fieldType) { |
300
|
18 |
|
case 'int': |
301
|
18 |
|
case 'tInt': |
302
|
|
|
$value = intval($value); |
303
|
|
|
break; |
304
|
|
|
|
305
|
18 |
|
case 'float': |
306
|
18 |
|
case 'tFloat': |
307
|
|
|
$value = floatval($value); |
308
|
|
|
break; |
309
|
|
|
|
310
|
|
|
// long and double do not exist in PHP |
311
|
|
|
// simply make sure it somehow looks like a number |
312
|
|
|
// <insert PHP rant here> |
313
|
18 |
|
case 'long': |
314
|
18 |
|
case 'tLong': |
315
|
|
|
// remove anything that's not a number or negative/minus sign |
316
|
|
|
$value = preg_replace('/[^0-9\\-]/', '', $value); |
317
|
|
|
if (trim($value) === '') { |
318
|
|
|
$value = 0; |
319
|
|
|
} |
320
|
|
|
break; |
321
|
18 |
|
case 'double': |
322
|
18 |
|
case 'tDouble': |
323
|
18 |
|
case 'tDouble4': |
324
|
|
|
// as long as it's numeric we'll take it, int or float doesn't matter |
325
|
|
|
if (!is_numeric($value)) { |
326
|
|
|
$value = 0; |
327
|
|
|
} |
328
|
|
|
break; |
329
|
|
|
|
330
|
|
|
default: |
331
|
|
|
// assume things are correct for non-dynamic fields |
332
|
|
|
} |
333
|
|
|
|
334
|
18 |
|
return $value; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|