|
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 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
|
|
|
if ($table === 'tt_content') { |
|
281
|
2 |
|
$table = 'pages'; |
|
282
|
2 |
|
$uid = $record['pid']; |
|
283
|
2 |
|
} else { |
|
284
|
|
|
$uid = $record['uid']; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
return $this->getIndexQueue()->containsIndexedItem($table, $uid); |
|
288
|
2 |
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* @return Queue |
|
292
|
|
|
*/ |
|
293
|
|
|
private function getIndexQueue() |
|
294
|
3 |
|
{ |
|
295
|
|
|
return GeneralUtility::makeInstance(Queue::class); |
|
296
|
3 |
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Checks whether the a frontend group field exists for the record and if so |
|
300
|
|
|
* whether groups have been removed from accessing the record thus making |
|
301
|
|
|
* the record invisible to at least some people. |
|
302
|
|
|
* |
|
303
|
|
|
* @param string $table The table name. |
|
304
|
|
|
* @param array $record An array with record fields that may affect visibility. |
|
305
|
|
|
* @return bool TRUE if frontend groups have been removed from access to the record, FALSE otherwise. |
|
306
|
|
|
*/ |
|
307
|
|
|
protected function hasFrontendGroupsRemoved($table, $record) |
|
308
|
1 |
|
{ |
|
309
|
|
|
if (!isset($GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['fe_group'])) { |
|
310
|
1 |
|
return false; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
$frontendGroupsField = $GLOBALS['TCA'][$table]['ctrl']['enablecolumns']['fe_group']; |
|
314
|
1 |
|
|
|
315
|
|
|
$previousGroups = explode(',', (string)$this->trackedRecords[$table][$record['uid']][$frontendGroupsField]); |
|
316
|
1 |
|
$currentGroups = explode(',', (string)$record[$frontendGroupsField]); |
|
317
|
1 |
|
$removedGroups = array_diff($previousGroups, $currentGroups); |
|
318
|
1 |
|
|
|
319
|
|
|
return (boolean)count($removedGroups); |
|
320
|
1 |
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Checks whether the page has been excluded from searching. |
|
324
|
|
|
* |
|
325
|
|
|
* @param array $record An array with record fields that may affect visibility. |
|
326
|
|
|
* @return bool True if the page has been excluded from searching, FALSE otherwise |
|
327
|
|
|
*/ |
|
328
|
|
|
protected function isPageExcludedFromSearch($record) |
|
329
|
|
|
{ |
|
330
|
|
|
return (boolean)$record['no_search']; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Checks whether a page has a page type that can be indexed. |
|
335
|
|
|
* Currently standard pages and mount pages can be indexed. |
|
336
|
|
|
* |
|
337
|
|
|
* @param array $record A page record |
|
338
|
|
|
* @return bool TRUE if the page can be indexed according to its page type, FALSE otherwise |
|
339
|
|
|
*/ |
|
340
|
|
|
protected function isIndexablePageType(array $record) |
|
341
|
|
|
{ |
|
342
|
|
|
return $this->frontendEnvironment->isAllowedPageType($record); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Determines if a record is garbage and can be deleted. |
|
347
|
|
|
* |
|
348
|
|
|
* @param string $table |
|
349
|
|
|
* @param array $record |
|
350
|
|
|
* @return bool |
|
351
|
|
|
*/ |
|
352
|
|
|
protected function getIsGarbageRecord($table, $record):bool |
|
353
|
7 |
|
{ |
|
354
|
|
|
return $this->tcaService->isHidden($table, $record) || |
|
355
|
7 |
|
$this->isInvisibleByStartOrEndtime($table, $record) || |
|
356
|
3 |
|
$this->hasFrontendGroupsRemoved($table, $record) || |
|
357
|
1 |
|
($table === 'pages' && $this->isPageExcludedFromSearch($record)) || |
|
358
|
1 |
|
($table === 'pages' && !$this->isIndexablePageType($record)); |
|
359
|
7 |
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* Returns a record with all visibility affecting fields. |
|
363
|
|
|
* |
|
364
|
|
|
* @param string $table |
|
365
|
|
|
* @param int $uid |
|
366
|
|
|
* @return array |
|
367
|
|
|
*/ |
|
368
|
|
|
protected function getRecordWithFieldRelevantForGarbageCollection($table, $uid):array |
|
369
|
7 |
|
{ |
|
370
|
|
|
$garbageCollectionRelevantFields = $this->tcaService->getVisibilityAffectingFieldsByTable($table); |
|
371
|
7 |
|
$record = (array)BackendUtility::getRecord($table, $uid, $garbageCollectionRelevantFields, '', false); |
|
372
|
7 |
|
return $record; |
|
373
|
7 |
|
} |
|
374
|
|
|
} |
|
375
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.