|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the TYPO3 CMS project. |
|
5
|
|
|
* |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
7
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
8
|
|
|
* of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please read the |
|
11
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* The TYPO3 project - inspiring people to share! |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Index\Queue\GarbageRemover; |
|
17
|
|
|
|
|
18
|
|
|
use ApacheSolrForTypo3\Solr\ConnectionManager; |
|
19
|
|
|
use ApacheSolrForTypo3\Solr\GarbageCollectorPostProcessor; |
|
20
|
|
|
use ApacheSolrForTypo3\Solr\IndexQueue\Queue; |
|
21
|
|
|
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager; |
|
22
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection; |
|
23
|
|
|
use InvalidArgumentException; |
|
24
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* An implementation ob a garbage remover strategy is responsible to remove all garbage from the index queue and |
|
28
|
|
|
* the solr server for a certain table and uid combination. |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class AbstractStrategy |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var Queue |
|
34
|
|
|
*/ |
|
35
|
|
|
protected Queue $queue; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var ConnectionManager |
|
39
|
|
|
*/ |
|
40
|
|
|
protected ConnectionManager $connectionManager; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* AbstractStrategy constructor. |
|
44
|
|
|
* @param Queue|null $queue |
|
45
|
22 |
|
* @param ConnectionManager|null $connectionManager |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct( |
|
48
|
|
|
Queue $queue = null, |
|
49
|
22 |
|
ConnectionManager $connectionManager = null |
|
50
|
22 |
|
) { |
|
51
|
|
|
$this->queue = $queue ?? GeneralUtility::makeInstance(Queue::class); |
|
52
|
|
|
$this->connectionManager = $connectionManager ?? GeneralUtility::makeInstance(ConnectionManager::class); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Call's the removal of the strategy and afterwards the garbage-collector post-processing hook. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $table |
|
59
|
22 |
|
* @param int $uid |
|
60
|
|
|
*/ |
|
61
|
22 |
|
public function removeGarbageOf(string $table, int $uid) |
|
62
|
22 |
|
{ |
|
63
|
|
|
$this->removeGarbageOfByStrategy($table, $uid); |
|
64
|
|
|
$this->callPostProcessGarbageCollectorHook($table, $uid); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* An implementation of the GarbageCollection strategy is responsible to remove the garbage from |
|
69
|
|
|
* the indexqueue and from the solr server. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $table |
|
72
|
|
|
* @param int $uid |
|
73
|
|
|
*/ |
|
74
|
|
|
abstract protected function removeGarbageOfByStrategy(string $table, int $uid); |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Deletes a document from solr and from the index queue. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $table |
|
80
|
14 |
|
* @param int $uid |
|
81
|
|
|
*/ |
|
82
|
14 |
|
protected function deleteInSolrAndRemoveFromIndexQueue(string $table, int $uid) |
|
83
|
14 |
|
{ |
|
84
|
|
|
$this->deleteIndexDocuments($table, $uid); |
|
85
|
|
|
$this->queue->deleteItem($table, $uid); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Deletes a document from solr and updates the item in the index queue (e.g. on page content updates). |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $table |
|
92
|
8 |
|
* @param int $uid |
|
93
|
|
|
*/ |
|
94
|
8 |
|
protected function deleteInSolrAndUpdateIndexQueue(string $table, int $uid) |
|
95
|
8 |
|
{ |
|
96
|
|
|
$this->deleteIndexDocuments($table, $uid); |
|
97
|
|
|
$this->queue->updateItem($table, $uid); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Deletes index documents for a given record identification. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $table The record's table name. |
|
104
|
22 |
|
* @param int $uid The record's uid. |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function deleteIndexDocuments(string $table, int $uid, int $language = 0) |
|
107
|
22 |
|
{ |
|
108
|
22 |
|
// record can be indexed for multiple sites |
|
109
|
19 |
|
$indexQueueItems = $this->queue->getItems($table, $uid); |
|
110
|
19 |
|
foreach ($indexQueueItems as $indexQueueItem) { |
|
111
|
19 |
|
try { |
|
112
|
|
|
$site = $indexQueueItem->getSite(); |
|
113
|
19 |
|
} catch (InvalidArgumentException $e) { |
|
114
|
19 |
|
$this->queue->deleteItem($indexQueueItem->getType(), $indexQueueItem->getIndexQueueUid()); |
|
115
|
|
|
continue; |
|
116
|
|
|
} |
|
117
|
19 |
|
|
|
118
|
|
|
$enableCommitsSetting = $site->getSolrConfiguration()->getEnableCommits(); |
|
119
|
|
|
$siteHash = $site->getSiteHash(); |
|
120
|
|
|
// a site can have multiple connections (cores / languages) |
|
121
|
|
|
$solrConnections = $this->connectionManager->getConnectionsBySite($site); |
|
122
|
|
|
if ($language > 0 && isset($solrConnections[$language])) { |
|
123
|
|
|
$solrConnections = [$language => $solrConnections[$language]]; |
|
124
|
|
|
} |
|
125
|
|
|
$this->deleteRecordInAllSolrConnections($table, $uid, $solrConnections, $siteHash, $enableCommitsSetting); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
19 |
|
* Deletes the record in all solr connections from that site. |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $table |
|
133
|
|
|
* @param int $uid |
|
134
|
|
|
* @param SolrConnection[] $solrConnections |
|
135
|
|
|
* @param string $siteHash |
|
136
|
|
|
* @param bool $enableCommitsSetting |
|
137
|
19 |
|
*/ |
|
138
|
19 |
|
protected function deleteRecordInAllSolrConnections( |
|
139
|
19 |
|
string $table, |
|
140
|
19 |
|
int $uid, |
|
141
|
19 |
|
array $solrConnections, |
|
142
|
19 |
|
string $siteHash, |
|
143
|
|
|
bool $enableCommitsSetting |
|
144
|
|
|
) { |
|
145
|
|
|
foreach ($solrConnections as $solr) { |
|
146
|
|
|
$query = 'type:' . $table . ' AND uid:' . $uid . ' AND siteHash:' . $siteHash; |
|
147
|
|
|
$response = $solr->getWriteService()->deleteByQuery($query); |
|
148
|
|
|
|
|
149
|
|
|
if ($response->getHttpStatus() !== 200) { |
|
150
|
|
|
$logger = GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__); |
|
151
|
|
|
$logger->log( |
|
152
|
|
|
SolrLogManager::ERROR, |
|
153
|
22 |
|
'Couldn\'t delete index document', |
|
154
|
|
|
[ |
|
155
|
22 |
|
'status' => $response->getHttpStatus(), |
|
156
|
20 |
|
'msg' => $response->getHttpStatusMessage(), |
|
157
|
|
|
'core' => $solr->getWriteService()->getCorePath(), |
|
158
|
|
|
'query' => $query, |
|
159
|
2 |
|
] |
|
160
|
2 |
|
); |
|
161
|
|
|
|
|
162
|
2 |
|
// @todo: Ensure index is updated later on, e.g. via a new index queue status |
|
163
|
2 |
|
continue; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
if ($enableCommitsSetting) { |
|
167
|
|
|
$solr->getWriteService()->commit(false, false); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Calls the registered post-processing hooks after the garbageCollection. |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $table |
|
176
|
|
|
* @param int $uid |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function callPostProcessGarbageCollectorHook(string $table, int $uid) |
|
179
|
|
|
{ |
|
180
|
|
|
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessGarbageCollector'] ?? null)) { |
|
181
|
|
|
return; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessGarbageCollector'] as $classReference) { |
|
185
|
|
|
$garbageCollectorPostProcessor = GeneralUtility::makeInstance($classReference); |
|
186
|
|
|
|
|
187
|
|
|
if ($garbageCollectorPostProcessor instanceof GarbageCollectorPostProcessor) { |
|
188
|
|
|
$garbageCollectorPostProcessor->postProcessGarbageCollector($table, $uid); |
|
189
|
|
|
} else { |
|
190
|
|
|
$message = get_class($garbageCollectorPostProcessor) . ' must implement interface ' . |
|
191
|
|
|
GarbageCollectorPostProcessor::class; |
|
192
|
|
|
throw new \UnexpectedValueException($message, 1345807460); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|