1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2010-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\Domain\Index\Queue\GarbageRemover\StrategyFactory; |
28
|
|
|
use ApacheSolrForTypo3\Solr\IndexQueue\Queue; |
29
|
|
|
use ApacheSolrForTypo3\Solr\System\TCA\TCAService; |
30
|
|
|
use TYPO3\CMS\Backend\Utility\BackendUtility; |
31
|
|
|
use TYPO3\CMS\Core\DataHandling\DataHandler; |
32
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
33
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Garbage Collector, removes related documents from the index when a record is |
37
|
|
|
* set to hidden, is deleted or is otherwise made invisible to website visitors. |
38
|
|
|
* |
39
|
|
|
* Garbage collection will happen for online/LIVE workspaces only. |
40
|
|
|
* |
41
|
|
|
* @author Ingo Renner <[email protected]> |
42
|
|
|
* @author Timo Schmidt <[email protected]> |
43
|
|
|
*/ |
44
|
|
|
class GarbageCollector extends AbstractDataHandlerListener implements SingletonInterface |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $trackedRecords = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var TCAService |
53
|
|
|
*/ |
54
|
|
|
protected $tcaService; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* GarbageCollector constructor. |
58
|
|
|
* @param TCAService|null $TCAService |
59
|
|
|
*/ |
60
|
12 |
|
public function __construct(TCAService $TCAService = null) |
61
|
|
|
{ |
62
|
12 |
|
parent::__construct(); |
63
|
12 |
|
$this->tcaService = $TCAService ?? GeneralUtility::makeInstance(TCAService::class); |
64
|
12 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Hooks into TCE main and tracks record deletion commands. |
68
|
|
|
* |
69
|
|
|
* @param string $command The command. |
70
|
|
|
* @param string $table The table the record belongs to |
71
|
|
|
* @param int $uid The record's uid |
72
|
|
|
* @param string $value Not used |
73
|
|
|
* @param DataHandler $tceMain TYPO3 Core Engine parent object, not used |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
3 |
|
public function processCmdmap_preProcess($command, $table, $uid, $value, DataHandler $tceMain) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
// workspaces: collect garbage only for LIVE workspace |
79
|
3 |
|
if ($command === 'delete' && $GLOBALS['BE_USER']->workspace == 0) { |
80
|
3 |
|
$this->collectGarbage($table, $uid); |
81
|
|
|
|
82
|
3 |
|
if ($table === 'pages') { |
83
|
1 |
|
$this->getIndexQueue()->deleteItem($table, $uid); |
84
|
|
|
} |
85
|
|
|
} |
86
|
3 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Holds the configuration when a recursive page queing should be triggered. |
90
|
|
|
* |
91
|
|
|
* @var array |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
3 |
|
protected function getUpdateSubPagesRecursiveTriggerConfiguration() |
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
|
|
// the current page has the field "extendToSubpages" enabled and the field "hidden" was set to 1 |
98
|
3 |
|
'extendToSubpageEnabledAndHiddenFlagWasAdded' => [ |
99
|
|
|
'currentState' => ['extendToSubpages' => '1'], |
100
|
|
|
'changeSet' => ['hidden' => '1'] |
101
|
|
|
], |
102
|
|
|
// the current page has the field "hidden" enabled and the field "extendToSubpages" was set to 1 |
103
|
|
|
'hiddenIsEnabledAndExtendToSubPagesWasAdded' => [ |
104
|
|
|
'currentState' => ['hidden' => '1'], |
105
|
|
|
'changeSet' => ['extendToSubpages' => '1'] |
106
|
|
|
] |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Tracks down index documents belonging to a particular record or page and |
112
|
|
|
* removes them from the index and the Index Queue. |
113
|
|
|
* |
114
|
|
|
* @param string $table The record's table name. |
115
|
|
|
* @param int $uid The record's uid. |
116
|
|
|
* @throws \UnexpectedValueException if a hook object does not implement interface \ApacheSolrForTypo3\Solr\GarbageCollectorPostProcessor |
117
|
|
|
*/ |
118
|
11 |
|
public function collectGarbage($table, $uid) |
119
|
|
|
{ |
120
|
11 |
|
$garbageRemoverStrategy = StrategyFactory::getByTable($table); |
121
|
11 |
|
$garbageRemoverStrategy->removeGarbageOf($table, $uid); |
122
|
11 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $table |
126
|
|
|
* @param int $uid |
127
|
|
|
* @param array $changedFields |
128
|
|
|
*/ |
129
|
3 |
|
protected function deleteSubpagesWhenExtendToSubpagesIsSet($table, $uid, $changedFields) |
130
|
|
|
{ |
131
|
3 |
|
if (!$this->isRecursivePageUpdateRequired($uid, $changedFields)) { |
132
|
1 |
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// get affected subpages when "extendToSubpages" flag was set |
136
|
2 |
|
$pagesToDelete = $this->getSubPageIds($uid); |
137
|
|
|
// we need to at least remove this page |
138
|
2 |
|
foreach ($pagesToDelete as $pageToDelete) { |
139
|
2 |
|
$this->collectGarbage($table, $pageToDelete); |
140
|
|
|
} |
141
|
2 |
|
} |
142
|
|
|
|
143
|
|
|
// methods checking whether to trigger garbage collection |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Hooks into TCE main and tracks page move commands. |
147
|
|
|
* |
148
|
|
|
* @param string $command The command. |
149
|
|
|
* @param string $table The table the record belongs to |
150
|
|
|
* @param int $uid The record's uid |
151
|
|
|
* @param string $value Not used |
152
|
|
|
* @param DataHandler $tceMain TYPO3 Core Engine parent object, not used |
153
|
|
|
*/ |
154
|
3 |
|
public function processCmdmap_postProcess($command, $table, $uid, $value, DataHandler $tceMain) { |
|
|
|
|
155
|
|
|
// workspaces: collect garbage only for LIVE workspace |
156
|
3 |
|
if ($command === 'move' && $table === 'pages' && $GLOBALS['BE_USER']->workspace == 0) { |
157
|
|
|
// TODO the below comment is not valid anymore, pid has been removed from doc ID |
158
|
|
|
// ...still needed? |
159
|
|
|
|
160
|
|
|
// must be removed from index since the pid changes and |
161
|
|
|
// is part of the Solr document ID |
162
|
|
|
$this->collectGarbage($table, $uid); |
163
|
|
|
|
164
|
|
|
// now re-index with new properties |
165
|
|
|
$this->getIndexQueue()->updateItem($table, $uid); |
166
|
|
|
} |
167
|
3 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Hooks into TCE main and tracks changed records. In this case the current |
171
|
|
|
* record's values are stored to do a change comparison later on for fields |
172
|
|
|
* like fe_group. |
173
|
|
|
* |
174
|
|
|
* @param array $incomingFields An array of incoming fields, new or changed, not used |
175
|
|
|
* @param string $table The table the record belongs to |
176
|
|
|
* @param mixed $uid The record's uid, [integer] or [string] (like 'NEW...') |
177
|
|
|
* @param DataHandler $tceMain TYPO3 Core Engine parent object, not used |
178
|
|
|
*/ |
179
|
5 |
|
public function processDatamap_preProcessFieldArray($incomingFields, $table, $uid, DataHandler $tceMain) |
|
|
|
|
180
|
|
|
{ |
181
|
5 |
|
if (!is_int($uid)) { |
182
|
|
|
// a newly created record, skip |
183
|
|
|
return; |
184
|
|
|
} |
185
|
|
|
|
186
|
5 |
|
if (Util::isDraftRecord($table, $uid)) { |
187
|
|
|
// skip workspaces: collect garbage only for LIVE workspace |
188
|
|
|
return; |
189
|
|
|
} |
190
|
|
|
|
191
|
5 |
|
$hasConfiguredEnableColumnForFeGroup = $this->tcaService->isEnableColumn($table, 'fe_group'); |
192
|
5 |
|
if (!$hasConfiguredEnableColumnForFeGroup) { |
193
|
|
|
return; |
194
|
|
|
} |
195
|
|
|
|
196
|
5 |
|
$visibilityAffectingFields = $this->tcaService->getVisibilityAffectingFieldsByTable($table); |
197
|
5 |
|
$record = (array)BackendUtility::getRecord($table, $uid, $visibilityAffectingFields, '', false); |
198
|
|
|
// If no record could be found skip further processing |
199
|
5 |
|
if (empty($record)) { |
200
|
|
|
return; |
201
|
|
|
} |
202
|
|
|
|
203
|
5 |
|
$record = $this->tcaService->normalizeFrontendGroupField($table, $record); |
204
|
|
|
|
205
|
|
|
// keep previous state of important fields for later comparison |
206
|
5 |
|
$this->trackedRecords[$table][$uid] = $record; |
207
|
5 |
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Hooks into TCE Main and watches all record updates. If a change is |
211
|
|
|
* detected that would remove the record from the website, we try to find |
212
|
|
|
* related documents and remove them from the index. |
213
|
|
|
* |
214
|
|
|
* @param string $status Status of the current operation, 'new' or 'update' |
215
|
|
|
* @param string $table The table the record belongs to |
216
|
|
|
* @param mixed $uid The record's uid, [integer] or [string] (like 'NEW...') |
217
|
|
|
* @param array $fields The record's data, not used |
218
|
|
|
* @param DataHandler $tceMain TYPO3 Core Engine parent object, not used |
219
|
|
|
*/ |
220
|
7 |
|
public function processDatamap_afterDatabaseOperations($status, $table, $uid, array $fields, DataHandler $tceMain) |
|
|
|
|
221
|
|
|
{ |
222
|
7 |
|
if ($status === 'new') { |
223
|
|
|
// a newly created record, skip |
224
|
|
|
return; |
225
|
|
|
} |
226
|
|
|
|
227
|
7 |
|
if (Util::isDraftRecord($table, $uid)) { |
228
|
|
|
// skip workspaces: collect garbage only for LIVE workspace |
229
|
|
|
return; |
230
|
|
|
} |
231
|
|
|
|
232
|
7 |
|
$record = $this->getRecordWithFieldRelevantForGarbageCollection($table, $uid); |
233
|
|
|
|
234
|
|
|
// If no record could be found skip further processing |
235
|
7 |
|
if (empty($record)) { |
236
|
|
|
return; |
237
|
|
|
} |
238
|
|
|
|
239
|
7 |
|
$record = $this->tcaService->normalizeFrontendGroupField($table, $record); |
240
|
7 |
|
$isGarbage = $this->getIsGarbageRecord($table, $record); |
241
|
7 |
|
if (!$isGarbage) { |
242
|
1 |
|
return; |
243
|
|
|
} |
244
|
|
|
|
245
|
6 |
|
$this->collectGarbage($table, $uid); |
246
|
|
|
|
247
|
6 |
|
if ($table === 'pages') { |
248
|
3 |
|
$this->deleteSubpagesWhenExtendToSubpagesIsSet($table, $uid, $fields); |
249
|
|
|
} |
250
|
6 |
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Check if a record is getting invisible due to changes in start or endtime. In addition it is checked that the related |
254
|
|
|
* queue item was marked as indexed. |
255
|
|
|
* |
256
|
|
|
* @param string $table |
257
|
|
|
* @param array $record |
258
|
|
|
* @return bool |
259
|
|
|
*/ |
260
|
3 |
|
protected function isInvisibleByStartOrEndtime($table, $record) |
261
|
|
|
{ |
262
|
|
|
return ( |
263
|
3 |
|
($this->tcaService->isStartTimeInFuture($table, $record) || $this->tcaService->isEndTimeInPast($table, $record)) && |
264
|
3 |
|
$this->isRelatedQueueRecordMarkedAsIndexed($table, $record) |
265
|
|
|
); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Checks if the related index queue item is indexed. |
270
|
|
|
* |
271
|
|
|
* * For tt_content and pages_language_overlay the page from the pid is checked |
272
|
|
|
* * For all other records the table it's self is checked |
273
|
|
|
* |
274
|
|
|
* @param string $table The table name. |
275
|
|
|
* @param array $record An array with record fields that may affect visibility. |
276
|
|
|
* @return bool True if the record is marked as being indexed |
277
|
|
|
*/ |
278
|
2 |
|
protected function isRelatedQueueRecordMarkedAsIndexed($table, $record) |
279
|
|
|
{ |
280
|
|
|
//@todo check for pages_language_overlay can be dropped when TYPO3 8 compatibility is dropped. |
281
|
2 |
|
if ($table === 'tt_content' || $table === 'pages_language_overlay') { |
282
|
2 |
|
$table = 'pages'; |
283
|
2 |
|
$uid = $record['pid']; |
284
|
|
|
} else { |
285
|
|
|
$uid = $record['uid']; |
286
|
|
|
} |
287
|
|
|
|
288
|
2 |
|
return $this->getIndexQueue()->containsIndexedItem($table, $uid); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @return Queue |
293
|
|
|
*/ |
294
|
3 |
|
private function getIndexQueue() |
295
|
|
|
{ |
296
|
3 |
|
return GeneralUtility::makeInstance(Queue::class); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Checks whether the a frontend group field exists for the record and if so |
301
|
|
|
* whether groups have been removed from accessing the record thus making |
302
|
|
|
* the record invisible to at least some people. |
303
|
|
|
* |
304
|
|
|
* @param string $table The table name. |
305
|
|
|
* @param array $record An array with record fields that may affect visibility. |
306
|
|
|
* @return bool TRUE if frontend groups have been removed from access to the record, FALSE otherwise. |
307
|
|
|
*/ |
308
|
1 |
|
protected function hasFrontendGroupsRemoved($table, $record) |
309
|
|
|
{ |
310
|
1 |
|
if (!isset($GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['fe_group'])) { |
311
|
|
|
return false; |
312
|
|
|
} |
313
|
|
|
|
314
|
1 |
|
$frontendGroupsField = $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['fe_group']; |
315
|
|
|
|
316
|
1 |
|
$previousGroups = explode(',', (string)$this->trackedRecords[$table][$record['uid']][$frontendGroupsField]); |
317
|
1 |
|
$currentGroups = explode(',', (string)$record[$frontendGroupsField]); |
318
|
1 |
|
$removedGroups = array_diff($previousGroups, $currentGroups); |
319
|
|
|
|
320
|
1 |
|
return (boolean)count($removedGroups); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Checks whether the page has been excluded from searching. |
325
|
|
|
* |
326
|
|
|
* @param array $record An array with record fields that may affect visibility. |
327
|
|
|
* @return bool True if the page has been excluded from searching, FALSE otherwise |
328
|
|
|
*/ |
329
|
|
|
protected function isPageExcludedFromSearch($record) |
330
|
|
|
{ |
331
|
|
|
return (boolean)$record['no_search']; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Checks whether a page has a page type that can be indexed. |
336
|
|
|
* Currently standard pages and mount pages can be indexed. |
337
|
|
|
* |
338
|
|
|
* @param array $record A page record |
339
|
|
|
* @return bool TRUE if the page can be indexed according to its page type, FALSE otherwise |
340
|
|
|
*/ |
341
|
|
|
protected function isIndexablePageType(array $record) |
342
|
|
|
{ |
343
|
|
|
return Util::isAllowedPageType($record); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Determines if a record is garbage and can be deleted. |
348
|
|
|
* |
349
|
|
|
* @param string $table |
350
|
|
|
* @param array $record |
351
|
|
|
* @return bool |
352
|
|
|
*/ |
353
|
7 |
|
protected function getIsGarbageRecord($table, $record):bool |
354
|
|
|
{ |
355
|
7 |
|
return $this->tcaService->isHidden($table, $record) || |
356
|
3 |
|
$this->isInvisibleByStartOrEndtime($table, $record) || |
357
|
1 |
|
$this->hasFrontendGroupsRemoved($table, $record) || |
358
|
1 |
|
($table === 'pages' && $this->isPageExcludedFromSearch($record)) || |
359
|
7 |
|
($table === 'pages' && !$this->isIndexablePageType($record)); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Returns a record with all visibility affecting fields. |
364
|
|
|
* |
365
|
|
|
* @param string $table |
366
|
|
|
* @param int $uid |
367
|
|
|
* @return array |
368
|
|
|
*/ |
369
|
7 |
|
protected function getRecordWithFieldRelevantForGarbageCollection($table, $uid):array |
370
|
|
|
{ |
371
|
7 |
|
$garbageCollectionRelevantFields = $this->tcaService->getVisibilityAffectingFieldsByTable($table); |
372
|
7 |
|
$record = (array)BackendUtility::getRecord($table, $uid, $garbageCollectionRelevantFields, '', false); |
373
|
7 |
|
return $record; |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.