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