Completed
Push — master ( ac874d...15a874 )
by Timo
48:30 queued 44:41
created

IndexService::generateIndexingErrorLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Index;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2015-2016 Timo Hund <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\ConnectionManager;
29
use ApacheSolrForTypo3\Solr\IndexQueue\Indexer;
30
use ApacheSolrForTypo3\Solr\IndexQueue\Item;
31
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
32
use ApacheSolrForTypo3\Solr\Domain\Site\Site;
33
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
34
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
35
use ApacheSolrForTypo3\Solr\Task\IndexQueueWorkerTask;
36
use Solarium\Exception\HttpException;
37
use TYPO3\CMS\Backend\Utility\BackendUtility;
38
use TYPO3\CMS\Core\Utility\GeneralUtility;
39
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
40
41
/**
42
 * Service to perform indexing operations
43
 *
44
 * @author Timo Hund <[email protected]>
45
 */
46
class IndexService
47
{
48
    /**
49
     * @var TypoScriptConfiguration
50
     */
51
    protected $configuration;
52
53
    /**
54
     * @var Site
55
     */
56
    protected $site;
57
58
    /**
59
     * @var IndexQueueWorkerTask
60
     */
61
    protected $contextTask;
62
63
    /**
64
     * @var Queue
65
     */
66
    protected $indexQueue;
67
68
    /**
69
     * @var Dispatcher
70
     */
71
    protected $signalSlotDispatcher;
72
73
    /**
74
     * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager
75
     */
76
    protected $logger = null;
77
78
    /**
79
     * IndexService constructor.
80
     * @param Site $site
81
     * @param Queue|null $queue
82
     * @param Dispatcher|null $dispatcher
83
     * @param SolrLogManager|null $solrLogManager
84
     */
85 5
    public function __construct(Site $site, Queue $queue = null, Dispatcher $dispatcher = null, SolrLogManager $solrLogManager = null)
86
    {
87 5
        $this->site = $site;
88 5
        $this->indexQueue = $queue ?? GeneralUtility::makeInstance(Queue::class);
89 5
        $this->signalSlotDispatcher = $dispatcher ?? GeneralUtility::makeInstance(Dispatcher::class);
90 5
        $this->logger = $solrLogManager ?? GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
91 5
    }
92
93
    /**
94
     * @param \ApacheSolrForTypo3\Solr\Task\IndexQueueWorkerTask $contextTask
95
     */
96 1
    public function setContextTask($contextTask)
97
    {
98 1
        $this->contextTask = $contextTask;
99 1
    }
100
101
    /**
102
     * @return \ApacheSolrForTypo3\Solr\Task\IndexQueueWorkerTask
103
     */
104 3
    public function getContextTask()
105
    {
106 3
        return $this->contextTask;
107
    }
108
109
    /**
110
     * Indexes items from the Index Queue.
111
     *
112
     * @param int $limit
113
     * @return bool
114
     */
115 3
    public function indexItems($limit)
116
    {
117 3
        $errors     = 0;
118 3
        $indexRunId = uniqid();
119 3
        $configurationToUse = $this->site->getSolrConfiguration();
120 3
        $enableCommitsSetting = $configurationToUse->getEnableCommits();
121
122
        // get items to index
123 3
        $itemsToIndex = $this->indexQueue->getItemsToIndex($this->site, $limit);
124
125 3
        $this->emitSignal('beforeIndexItems', [$itemsToIndex, $this->getContextTask(), $indexRunId]);
126
127 3
        foreach ($itemsToIndex as $itemToIndex) {
128
            try {
129
                // try indexing
130 3
                $this->emitSignal('beforeIndexItem', [$itemToIndex, $this->getContextTask(), $indexRunId]);
131 3
                $this->indexItem($itemToIndex, $configurationToUse);
132 2
                $this->emitSignal('afterIndexItem', [$itemToIndex, $this->getContextTask(), $indexRunId]);
133 1
            } catch (\Exception $e) {
134 1
                $errors++;
135 1
                $this->indexQueue->markItemAsFailed($itemToIndex, $e->getCode() . ': ' . $e->__toString());
136 1
                $this->generateIndexingErrorLog($itemToIndex, $e);
137
            }
138
        }
139
140 3
        $this->emitSignal('afterIndexItems', [$itemsToIndex, $this->getContextTask(), $indexRunId]);
141
142 3
        if ($enableCommitsSetting && count($itemsToIndex) > 0) {
143
            $solrServers = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionsBySite($this->site);
144
            foreach ($solrServers as $solrServer) {
145
                try {
146
                    $solrServer->getWriteService()->commit(false, false, false);
147
                } catch (HttpException $e) {
148
                    $errors++;
149
                }
150
            }
151
        }
152
153 3
        return ($errors === 0);
154
    }
155
156
    /**
157
     * Generates a message in the error log when an error occured.
158
     *
159
     * @param Item $itemToIndex
160
     * @param \Exception  $e
161
     */
162 1
    protected function generateIndexingErrorLog(Item $itemToIndex, \Exception $e)
163
    {
164 1
        $message = 'Failed indexing Index Queue item ' . $itemToIndex->getIndexQueueUid();
165 1
        $data = ['code' => $e->getCode(), 'message' => $e->getMessage(), 'trace' => $e->getTraceAsString(), 'item' => (array)$itemToIndex];
166
167 1
        $this->logger->log(
168 1
            SolrLogManager::ERROR,
169
            $message,
170
            $data
171
        );
172 1
    }
173
174
    /**
175
     * Builds an emits a singal for the IndexService.
176
     *
177
     * @param string $name
178
     * @param array $arguments
179
     * @return mixed
180
     */
181 3
    protected function emitSignal($name, $arguments)
182
    {
183 3
        return $this->signalSlotDispatcher->dispatch(__CLASS__, $name, $arguments);
184
    }
185
186
    /**
187
     * Indexes an item from the Index Queue.
188
     *
189
     * @param Item $item An index queue item to index
190
     * @param TypoScriptConfiguration $configuration
191
     * @return bool TRUE if the item was successfully indexed, FALSE otherwise
192
     */
193 2
    protected function indexItem(Item $item, TypoScriptConfiguration $configuration)
194
    {
195 2
        $indexer = $this->getIndexerByItem($item->getIndexingConfigurationName(), $configuration);
196
197
        // Remember original http host value
198 2
        $originalHttpHost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null;
199
200 2
        $itemChangedDate = $item->getChanged();
201 2
        $itemChangedDateAfterIndex = 0;
202
203
        try {
204 2
            $this->initializeHttpServerEnvironment($item);
205 2
            $itemIndexed = $indexer->index($item);
206
207
            // update IQ item so that the IQ can determine what's been indexed already
208 1
            if ($itemIndexed) {
209 1
                $this->indexQueue->updateIndexTimeByItem($item);
210 1
                $itemChangedDateAfterIndex = $item->getChanged();
211
            }
212
213 1
            if ($itemChangedDateAfterIndex > $itemChangedDate && $itemChangedDateAfterIndex > time()) {
214 1
                $this->indexQueue->setForcedChangeTimeByItem($item, $itemChangedDateAfterIndex);
215
            }
216 1
        } catch (\Exception $e) {
217 1
            $this->restoreOriginalHttpHost($originalHttpHost);
218 1
            throw $e;
219
        }
220
221 1
        $this->restoreOriginalHttpHost($originalHttpHost);
222
223 1
        return $itemIndexed;
224
    }
225
226
    /**
227
     * A factory method to get an indexer depending on an item's configuration.
228
     *
229
     * By default all items are indexed using the default indexer
230
     * (ApacheSolrForTypo3\Solr\IndexQueue\Indexer) coming with EXT:solr. Pages by default are
231
     * configured to be indexed through a dedicated indexer
232
     * (ApacheSolrForTypo3\Solr\IndexQueue\PageIndexer). In all other cases a dedicated indexer
233
     * can be specified through TypoScript if needed.
234
     *
235
     * @param string $indexingConfigurationName Indexing configuration name.
236
     * @param TypoScriptConfiguration $configuration
237
     * @return Indexer
238
     */
239
    protected function getIndexerByItem($indexingConfigurationName, TypoScriptConfiguration $configuration)
240
    {
241
        $indexerClass = $configuration->getIndexQueueIndexerByConfigurationName($indexingConfigurationName);
242
        $indexerConfiguration = $configuration->getIndexQueueIndexerConfigurationByConfigurationName($indexingConfigurationName);
243
244
        $indexer = GeneralUtility::makeInstance($indexerClass, /** @scrutinizer ignore-type */ $indexerConfiguration);
245
        if (!($indexer instanceof Indexer)) {
246
            throw new \RuntimeException(
247
                'The indexer class "' . $indexerClass . '" for indexing configuration "' . $indexingConfigurationName . '" is not a valid indexer. Must be a subclass of ApacheSolrForTypo3\Solr\IndexQueue\Indexer.',
248
                1260463206
249
            );
250
        }
251
252
        return $indexer;
253
    }
254
255
    /**
256
     * Gets the indexing progress.
257
     *
258
     * @return float Indexing progress as a two decimal precision float. f.e. 44.87
259
     */
260 1
    public function getProgress()
261
    {
262 1
        return $this->indexQueue->getStatisticsBySite($this->site)->getSuccessPercentage();
263
    }
264
265
    /**
266
     * Returns the amount of failed queue items for the current site.
267
     *
268
     * @return int
269
     */
270 1
    public function getFailCount()
271
    {
272 1
        return $this->indexQueue->getStatisticsBySite($this->site)->getFailedCount();
273
    }
274
275
    /**
276
     * Initializes the $_SERVER['HTTP_HOST'] environment variable in CLI
277
     * environments dependent on the Index Queue item's root page.
278
     *
279
     * When the Index Queue Worker task is executed by a cron job there is no
280
     * HTTP_HOST since we are in a CLI environment. RealURL needs the host
281
     * information to generate a proper URL though. Using the Index Queue item's
282
     * root page information we can determine the correct host although being
283
     * in a CLI environment.
284
     *
285
     * @param Item $item Index Queue item to use to determine the host.
286
     * @param
287
     */
288 2
    protected function initializeHttpServerEnvironment(Item $item)
289
    {
290 2
        static $hosts = [];
291 2
        $rootpageId = $item->getRootPageUid();
292 2
        $hostFound = !empty($hosts[$rootpageId]);
293
294 2
        if (!$hostFound) {
295 2
            $hosts[$rootpageId] = $item->getSite()->getDomain();
296
        }
297
298 2
        $_SERVER['HTTP_HOST'] = $hosts[$rootpageId];
299
300
        // needed since TYPO3 7.5
301 2
        GeneralUtility::flushInternalRuntimeCaches();
302 2
    }
303
304
    /**
305
     * @param string|null $originalHttpHost
306
     */
307 1
    protected function restoreOriginalHttpHost($originalHttpHost)
308
    {
309 1
        if (!is_null($originalHttpHost)) {
310 1
            $_SERVER['HTTP_HOST'] = $originalHttpHost;
311
        } else {
312
            unset($_SERVER['HTTP_HOST']);
313
        }
314
315
        // needed since TYPO3 7.5
316 1
        GeneralUtility::flushInternalRuntimeCaches();
317 1
    }
318
}
319