1 | <?php |
||
45 | class IndexService |
||
46 | { |
||
47 | /** |
||
48 | * @var TypoScriptConfiguration |
||
49 | */ |
||
50 | protected $configuration; |
||
51 | |||
52 | /** |
||
53 | * @var Site |
||
54 | */ |
||
55 | protected $site; |
||
56 | |||
57 | /** |
||
58 | * @var IndexQueueWorkerTask |
||
59 | */ |
||
60 | protected $contextTask; |
||
61 | |||
62 | /** |
||
63 | * @var Queue |
||
64 | */ |
||
65 | protected $indexQueue; |
||
66 | |||
67 | /** |
||
68 | * @var Dispatcher |
||
69 | */ |
||
70 | protected $signalSlotDispatcher; |
||
71 | |||
72 | /** |
||
73 | * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager |
||
74 | */ |
||
75 | protected $logger = null; |
||
76 | |||
77 | /** |
||
78 | * IndexService constructor. |
||
79 | * @param Site $site |
||
80 | * @param Queue|null $queue |
||
81 | * @param Dispatcher|null $dispatcher |
||
82 | */ |
||
83 | 8 | public function __construct(Site $site, Queue $queue = null, Dispatcher $dispatcher = null) |
|
90 | |||
91 | /** |
||
92 | * @param \ApacheSolrForTypo3\Solr\Task\IndexQueueWorkerTask $contextTask |
||
93 | */ |
||
94 | 2 | public function setContextTask($contextTask) |
|
98 | |||
99 | /** |
||
100 | * @return \ApacheSolrForTypo3\Solr\Task\IndexQueueWorkerTask |
||
101 | */ |
||
102 | 6 | public function getContextTask() |
|
106 | |||
107 | /** |
||
108 | * Indexes items from the Index Queue. |
||
109 | * |
||
110 | * @param int $limit |
||
111 | * @return bool |
||
112 | */ |
||
113 | 6 | public function indexItems($limit) |
|
114 | { |
||
115 | 6 | $errors = 0; |
|
116 | 6 | $indexRunId = uniqid(); |
|
117 | 6 | $configurationToUse = $this->site->getSolrConfiguration(); |
|
118 | 6 | $enableCommitsSetting = $configurationToUse->getEnableCommits(); |
|
119 | |||
120 | // get items to index |
||
121 | 6 | $itemsToIndex = $this->indexQueue->getItemsToIndex($this->site, $limit); |
|
122 | |||
123 | 6 | $this->emitSignal('beforeIndexItems', [$itemsToIndex, $this->getContextTask(), $indexRunId]); |
|
124 | |||
125 | 6 | foreach ($itemsToIndex as $itemToIndex) { |
|
126 | try { |
||
127 | // try indexing |
||
128 | 6 | $this->emitSignal('beforeIndexItem', [$itemToIndex, $this->getContextTask(), $indexRunId]); |
|
129 | 6 | $this->indexItem($itemToIndex, $configurationToUse); |
|
130 | 6 | $this->emitSignal('afterIndexItem', [$itemToIndex, $this->getContextTask(), $indexRunId]); |
|
131 | } catch (\Exception $e) { |
||
132 | $errors++; |
||
133 | $this->indexQueue->markItemAsFailed($itemToIndex, $e->getCode() . ': ' . $e->__toString()); |
||
134 | $this->generateIndexingErrorLog($itemToIndex, $e); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | 6 | $this->emitSignal('afterIndexItems', [$itemsToIndex, $this->getContextTask(), $indexRunId]); |
|
139 | |||
140 | 6 | if ($enableCommitsSetting && count($itemsToIndex) > 0) { |
|
141 | 5 | $solrServers = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionsBySite($this->site); |
|
142 | 5 | foreach ($solrServers as $solrServer) { |
|
143 | 5 | $solrServer->commit(false, false, false); |
|
144 | } |
||
145 | } |
||
146 | |||
147 | 6 | return ($errors === 0); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Generates a message in the error log when an error occured. |
||
152 | * |
||
153 | * @param Item $itemToIndex |
||
154 | * @param \Exception $e |
||
155 | */ |
||
156 | protected function generateIndexingErrorLog(Item $itemToIndex, \Exception $e) |
||
157 | { |
||
158 | $message = 'Failed indexing Index Queue item ' . $itemToIndex->getIndexQueueUid(); |
||
159 | $data = ['code' => $e->getCode(), 'message' => $e->getMessage(), 'trace' => $e->getTrace(), 'item' => (array)$itemToIndex]; |
||
160 | |||
161 | $this->logger->log( |
||
162 | SolrLogManager::ERROR, |
||
163 | $message, |
||
164 | $data |
||
165 | ); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Builds an emits a singal for the IndexService. |
||
170 | * |
||
171 | * @param string $name |
||
172 | * @param array $arguments |
||
173 | * @return mixed |
||
174 | */ |
||
175 | 6 | protected function emitSignal($name, $arguments) |
|
179 | |||
180 | /** |
||
181 | * Indexes an item from the Index Queue. |
||
182 | * |
||
183 | * @param Item $item An index queue item to index |
||
184 | * @param TypoScriptConfiguration $configuration |
||
185 | * @return bool TRUE if the item was successfully indexed, FALSE otherwise |
||
186 | */ |
||
187 | 5 | protected function indexItem(Item $item, TypoScriptConfiguration $configuration) |
|
213 | |||
214 | /** |
||
215 | * A factory method to get an indexer depending on an item's configuration. |
||
216 | * |
||
217 | * By default all items are indexed using the default indexer |
||
218 | * (ApacheSolrForTypo3\Solr\IndexQueue\Indexer) coming with EXT:solr. Pages by default are |
||
219 | * configured to be indexed through a dedicated indexer |
||
220 | * (ApacheSolrForTypo3\Solr\IndexQueue\PageIndexer). In all other cases a dedicated indexer |
||
221 | * can be specified through TypoScript if needed. |
||
222 | * |
||
223 | * @param string $indexingConfigurationName Indexing configuration name. |
||
224 | * @param TypoScriptConfiguration $configuration |
||
225 | * @return Indexer |
||
226 | */ |
||
227 | 5 | protected function getIndexerByItem($indexingConfigurationName, TypoScriptConfiguration $configuration) |
|
242 | |||
243 | /** |
||
244 | * Gets the indexing progress. |
||
245 | * |
||
246 | * @return float Indexing progress as a two decimal precision float. f.e. 44.87 |
||
247 | */ |
||
248 | 2 | public function getProgress() |
|
252 | |||
253 | /** |
||
254 | * Returns the amount of failed queue items for the current site. |
||
255 | * |
||
256 | * @return int |
||
257 | */ |
||
258 | 1 | public function getFailCount() |
|
262 | |||
263 | /** |
||
264 | * Initializes the $_SERVER['HTTP_HOST'] environment variable in CLI |
||
265 | * environments dependent on the Index Queue item's root page. |
||
266 | * |
||
267 | * When the Index Queue Worker task is executed by a cron job there is no |
||
268 | * HTTP_HOST since we are in a CLI environment. RealURL needs the host |
||
269 | * information to generate a proper URL though. Using the Index Queue item's |
||
270 | * root page information we can determine the correct host although being |
||
271 | * in a CLI environment. |
||
272 | * |
||
273 | * @param Item $item Index Queue item to use to determine the host. |
||
274 | * @param |
||
275 | */ |
||
276 | 5 | protected function initializeHttpServerEnvironment(Item $item) |
|
293 | } |
||
294 |