|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the TYPO3 CMS project. |
|
7
|
|
|
* |
|
8
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
9
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
10
|
|
|
* of the License, or any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please read the |
|
13
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
14
|
|
|
* |
|
15
|
|
|
* The TYPO3 project - inspiring people to share! |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Index\Queue; |
|
19
|
|
|
|
|
20
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Site\Site; |
|
21
|
|
|
use ApacheSolrForTypo3\Solr\IndexQueue\InitializationPostProcessor; |
|
22
|
|
|
use ApacheSolrForTypo3\Solr\IndexQueue\Initializer\AbstractInitializer; |
|
23
|
|
|
use ApacheSolrForTypo3\Solr\IndexQueue\Queue; |
|
24
|
|
|
use Doctrine\DBAL\ConnectionException; |
|
25
|
|
|
use Doctrine\DBAL\Exception as DBALException; |
|
26
|
|
|
use Throwable; |
|
27
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
28
|
|
|
use UnexpectedValueException; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The queue initialization service is responsible to run the initialization of the index queue for a combination of sites |
|
32
|
|
|
* and index queue configurations. |
|
33
|
|
|
* |
|
34
|
|
|
* @author Timo Hund <[email protected]> |
|
35
|
|
|
* @author Ingo Renner <[email protected]> |
|
36
|
|
|
*/ |
|
37
|
|
|
class QueueInitializationService |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* @var Queue |
|
41
|
|
|
*/ |
|
42
|
|
|
protected Queue $queue; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* QueueInitializationService constructor. |
|
46
|
|
|
*/ |
|
47
|
154 |
|
public function __construct(Queue $queue) |
|
48
|
|
|
{ |
|
49
|
154 |
|
$this->queue = $queue; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Truncate and rebuild the tx_solr_indexqueue_item table. This is the most |
|
54
|
|
|
* complete way to force reindexing, or to build the Index Queue for the |
|
55
|
|
|
* first time. The Index Queue initialization is site-specific. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Site $site The site to initialize |
|
58
|
|
|
* @param string $indexingConfigurationName Name of a specific indexing configuration, when * is passed any is used |
|
59
|
|
|
* @return array An array of booleans, each representing whether the |
|
60
|
|
|
* initialization for an indexing configuration was successful |
|
61
|
|
|
* @throws ConnectionException |
|
62
|
|
|
* @throws DBALException |
|
63
|
|
|
* @throws Throwable |
|
64
|
|
|
*/ |
|
65
|
5 |
|
public function initializeBySiteAndIndexConfiguration(Site $site, string $indexingConfigurationName = '*'): array |
|
66
|
|
|
{ |
|
67
|
5 |
|
return $this->initializeBySiteAndIndexConfigurations($site, [$indexingConfigurationName]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Truncates and rebuilds the tx_solr_indexqueue_item table for a set of sites and a set of index configurations. |
|
72
|
|
|
* |
|
73
|
|
|
* @param array $sites The array of sites to initialize |
|
74
|
|
|
* @param array $indexingConfigurationNames the array of index configurations to initialize. |
|
75
|
|
|
* @return array |
|
76
|
|
|
* @throws ConnectionException |
|
77
|
|
|
* @throws DBALException |
|
78
|
|
|
* @throws Throwable |
|
79
|
|
|
*/ |
|
80
|
|
|
public function initializeBySitesAndConfigurations(array $sites, array $indexingConfigurationNames = ['*']): array |
|
81
|
|
|
{ |
|
82
|
|
|
$initializationStatesBySiteId = []; |
|
83
|
|
|
foreach ($sites as $site) { |
|
84
|
|
|
/** @var Site $site */ |
|
85
|
|
|
$initializationResult = $this->initializeBySiteAndIndexConfigurations($site, $indexingConfigurationNames); |
|
86
|
|
|
$initializationStatesBySiteId[$site->getRootPageId()] = $initializationResult; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $initializationStatesBySiteId; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Initializes a set index configurations for a given site. |
|
94
|
|
|
* |
|
95
|
|
|
* @param Site $site |
|
96
|
|
|
* @param array $indexingConfigurationNames if one of the names is a * (wildcard) all configurations are used, |
|
97
|
|
|
* @return array |
|
98
|
|
|
* @throws ConnectionException |
|
99
|
|
|
* @throws Throwable |
|
100
|
|
|
* @throws DBALException |
|
101
|
|
|
*/ |
|
102
|
7 |
|
public function initializeBySiteAndIndexConfigurations(Site $site, array $indexingConfigurationNames): array |
|
103
|
|
|
{ |
|
104
|
7 |
|
$initializationStatus = []; |
|
105
|
|
|
|
|
106
|
7 |
|
$hasWildcardConfiguration = in_array('*', $indexingConfigurationNames); |
|
107
|
7 |
|
$indexingConfigurationNames = $hasWildcardConfiguration ? $site->getSolrConfiguration()->getEnabledIndexQueueConfigurationNames() : $indexingConfigurationNames; |
|
108
|
7 |
|
foreach ($indexingConfigurationNames as $indexingConfigurationName) { |
|
109
|
7 |
|
$initializationStatus[$indexingConfigurationName] = $this->applyInitialization($site, (string)$indexingConfigurationName); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
7 |
|
if (!isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueInitialization'])) { |
|
113
|
7 |
|
return $initializationStatus; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueInitialization'] as $classReference) { |
|
117
|
|
|
$indexQueueInitializationPostProcessor = GeneralUtility::makeInstance($classReference); |
|
118
|
|
|
if ($indexQueueInitializationPostProcessor instanceof InitializationPostProcessor) { |
|
119
|
|
|
$indexQueueInitializationPostProcessor->postProcessIndexQueueInitialization($site, $indexingConfigurationNames, $initializationStatus); |
|
120
|
|
|
} else { |
|
121
|
|
|
throw new UnexpectedValueException(get_class($indexQueueInitializationPostProcessor) . ' must implement interface ' . InitializationPostProcessor::class, 1345815561); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $initializationStatus; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Initializes the Index Queue for a specific indexing configuration. |
|
130
|
|
|
* |
|
131
|
|
|
* @param Site $site The site to initialize |
|
132
|
|
|
* @param string $indexingConfigurationName name of a specific |
|
133
|
|
|
* indexing configuration |
|
134
|
|
|
* @return bool TRUE if the initialization was successful, FALSE otherwise |
|
135
|
|
|
* @throws ConnectionException |
|
136
|
|
|
* @throws DBALException |
|
137
|
|
|
* @throws Throwable |
|
138
|
|
|
*/ |
|
139
|
7 |
|
protected function applyInitialization(Site $site, string $indexingConfigurationName): bool |
|
140
|
|
|
{ |
|
141
|
|
|
// clear queue |
|
142
|
7 |
|
$this->queue->deleteItemsBySite($site, $indexingConfigurationName); |
|
143
|
|
|
|
|
144
|
7 |
|
$solrConfiguration = $site->getSolrConfiguration(); |
|
145
|
7 |
|
$tableToIndex = $solrConfiguration->getIndexQueueTableNameOrFallbackToConfigurationName($indexingConfigurationName); |
|
146
|
7 |
|
$initializerClass = $solrConfiguration->getIndexQueueInitializerClassByConfigurationName($indexingConfigurationName); |
|
147
|
7 |
|
$indexConfiguration = $solrConfiguration->getIndexQueueConfigurationByName($indexingConfigurationName); |
|
148
|
|
|
|
|
149
|
7 |
|
return $this->executeInitializer($site, $indexingConfigurationName, $initializerClass, $tableToIndex, $indexConfiguration); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param Site $site |
|
154
|
|
|
* @param string $indexingConfigurationName |
|
155
|
|
|
* @param string $initializerClass |
|
156
|
|
|
* @param string $tableToIndex |
|
157
|
|
|
* @param array $indexConfiguration |
|
158
|
|
|
* @return bool |
|
159
|
|
|
*/ |
|
160
|
6 |
|
protected function executeInitializer( |
|
161
|
|
|
Site $site, |
|
162
|
|
|
string $indexingConfigurationName, |
|
163
|
|
|
string $initializerClass, |
|
164
|
|
|
string $tableToIndex, |
|
165
|
|
|
array $indexConfiguration |
|
166
|
|
|
): bool { |
|
167
|
6 |
|
$initializer = GeneralUtility::makeInstance($initializerClass); |
|
168
|
|
|
/* @var AbstractInitializer $initializer */ |
|
169
|
6 |
|
$initializer->setSite($site); |
|
170
|
6 |
|
$initializer->setType($tableToIndex); |
|
171
|
6 |
|
$initializer->setIndexingConfigurationName($indexingConfigurationName); |
|
172
|
6 |
|
$initializer->setIndexingConfiguration($indexConfiguration); |
|
173
|
|
|
|
|
174
|
6 |
|
return $initializer->initialize(); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|