1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Index\Queue; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2010-2017 dkd Internet Service GmbH <[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\IndexQueue\InitializationPostProcessor; |
28
|
|
|
use ApacheSolrForTypo3\Solr\IndexQueue\Queue; |
29
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Site\Site; |
30
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The queue initialization service is responsible to run the initialization of the index queue for a combination of sites |
34
|
|
|
* and index queue configurations. |
35
|
|
|
* |
36
|
|
|
* @author Timo Hund <[email protected]> |
37
|
|
|
* @author Ingo Renner <[email protected]> |
38
|
|
|
*/ |
39
|
|
|
class QueueInitializationService { |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Queue |
43
|
|
|
*/ |
44
|
|
|
protected $queue; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* QueueInitializationService constructor. |
48
|
|
|
*/ |
49
|
117 |
|
public function __construct(Queue $queue) |
50
|
|
|
{ |
51
|
117 |
|
$this->queue = $queue; |
52
|
117 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Truncate and rebuild the tx_solr_indexqueue_item table. This is the most |
56
|
|
|
* complete way to force reindexing, or to build the Index Queue for the |
57
|
|
|
* first time. The Index Queue initialization is site-specific. |
58
|
|
|
* |
59
|
|
|
* @param Site $site The site to initialize |
60
|
|
|
* @param string $indexingConfigurationName Name of a specific indexing configuration, when * is passed any is used |
61
|
|
|
* @return array An array of booleans, each representing whether the |
62
|
|
|
* initialization for an indexing configuration was successful |
63
|
|
|
*/ |
64
|
4 |
|
public function initializeBySiteAndIndexConfiguration(Site $site, $indexingConfigurationName = '*'): array |
65
|
|
|
{ |
66
|
4 |
|
return $this->initializeBySiteAndIndexConfigurations($site, [$indexingConfigurationName]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Truncates and rebuilds the tx_solr_indexqueue_item table for a set of sites and a set of index configurations. |
71
|
|
|
* |
72
|
|
|
* @param array $sites The array of sites to initialize |
73
|
|
|
* @param array $indexingConfigurationNames the array of index configurations to initialize. |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function initializeBySitesAndConfigurations(array $sites, array $indexingConfigurationNames = ['*']): array |
77
|
|
|
{ |
78
|
|
|
$initializationStatesBySiteId = []; |
79
|
|
|
foreach($sites as $site) { |
80
|
|
|
/** @var Site $site */ |
81
|
|
|
$initializationResult = $this->initializeBySiteAndIndexConfigurations($site, $indexingConfigurationNames); |
82
|
|
|
$initializationStatesBySiteId[$site->getRootPageId()] = $initializationResult; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $initializationStatesBySiteId; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Initializes a set index configurations for a given site. |
90
|
|
|
* |
91
|
|
|
* @param Site $site |
92
|
|
|
* @param array $indexingConfigurationNames if one of the names is a * (wildcard) all configurations are used, |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
6 |
|
public function initializeBySiteAndIndexConfigurations(Site $site, array $indexingConfigurationNames): array |
96
|
|
|
{ |
97
|
6 |
|
$initializationStatus = []; |
98
|
|
|
|
99
|
6 |
|
$hasWildcardConfiguration = in_array('*', $indexingConfigurationNames); |
100
|
6 |
|
$indexingConfigurationNames = $hasWildcardConfiguration ? $site->getSolrConfiguration()->getEnabledIndexQueueConfigurationNames() : $indexingConfigurationNames; |
101
|
6 |
|
foreach ($indexingConfigurationNames as $indexingConfigurationName) { |
102
|
6 |
|
$initializationStatus[$indexingConfigurationName] = $this->applyInitialization($site, (string)$indexingConfigurationName); |
103
|
|
|
} |
104
|
|
|
|
105
|
6 |
|
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueInitialization'])) { |
106
|
6 |
|
return $initializationStatus; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueInitialization'] as $classReference) { |
110
|
|
|
$indexQueueInitializationPostProcessor = GeneralUtility::makeInstance($classReference); |
111
|
|
|
if ($indexQueueInitializationPostProcessor instanceof InitializationPostProcessor) { |
112
|
|
|
$indexQueueInitializationPostProcessor->postProcessIndexQueueInitialization($site, $indexingConfigurationNames, $initializationStatus); |
113
|
|
|
} else { |
114
|
|
|
throw new \UnexpectedValueException(get_class($indexQueueInitializationPostProcessor) . ' must implement interface ' . InitializationPostProcessor::class, 1345815561); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $initializationStatus; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Initializes the Index Queue for a specific indexing configuration. |
123
|
|
|
* |
124
|
|
|
* @param Site $site The site to initialize |
125
|
|
|
* @param string $indexingConfigurationName name of a specific |
126
|
|
|
* indexing configuration |
127
|
|
|
* @return bool TRUE if the initialization was successful, FALSE otherwise |
128
|
|
|
*/ |
129
|
6 |
|
protected function applyInitialization(Site $site, $indexingConfigurationName): bool |
130
|
|
|
{ |
131
|
|
|
// clear queue |
132
|
6 |
|
$this->queue->deleteItemsBySite($site, $indexingConfigurationName); |
133
|
|
|
|
134
|
6 |
|
$solrConfiguration = $site->getSolrConfiguration(); |
135
|
6 |
|
$tableToIndex = $solrConfiguration->getIndexQueueTableNameOrFallbackToConfigurationName($indexingConfigurationName); |
136
|
6 |
|
$initializerClass = $solrConfiguration->getIndexQueueInitializerClassByConfigurationName($indexingConfigurationName); |
137
|
6 |
|
$indexConfiguration = $solrConfiguration->getIndexQueueConfigurationByName($indexingConfigurationName); |
138
|
|
|
|
139
|
6 |
|
return $this->executeInitializer($site, $indexingConfigurationName, $initializerClass, $tableToIndex, $indexConfiguration); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param Site $site |
144
|
|
|
* @param string $indexingConfigurationName |
145
|
|
|
* @param string $initializerClass |
146
|
|
|
* @param string $tableToIndex |
147
|
|
|
* @param array $indexConfiguration |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
6 |
|
protected function executeInitializer(Site $site, $indexingConfigurationName, $initializerClass, $tableToIndex, $indexConfiguration): bool |
151
|
|
|
{ |
152
|
6 |
|
$initializer = GeneralUtility::makeInstance($initializerClass); |
153
|
|
|
/** @var $initializer \ApacheSolrForTypo3\Solr\IndexQueue\Initializer\AbstractInitializer */ |
154
|
6 |
|
$initializer->setSite($site); |
155
|
6 |
|
$initializer->setType($tableToIndex); |
156
|
6 |
|
$initializer->setIndexingConfigurationName($indexingConfigurationName); |
157
|
6 |
|
$initializer->setIndexingConfiguration($indexConfiguration); |
158
|
|
|
|
159
|
6 |
|
return $initializer->initialize(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
|