Passed
Push — master ( a5d0a9...8581cd )
by Timo
23:17
created

IndexService::indexItem()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.0585

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 15
cts 17
cp 0.8824
rs 8.7697
c 0
b 0
f 0
cc 6
nc 16
nop 2
crap 6.0585
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\Site;
33
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
34
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
35
use ApacheSolrForTypo3\Solr\Task\IndexQueueWorkerTask;
36
use TYPO3\CMS\Backend\Utility\BackendUtility;
37
use TYPO3\CMS\Core\Utility\GeneralUtility;
38
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
39
40
/**
41
 * Service to perform indexing operations
42
 *
43
 * @author Timo Hund <[email protected]>
44
 */
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
     * @param SolrLogManager|null $solrLogManager
83
     */
84 7
    public function __construct(Site $site, Queue $queue = null, Dispatcher $dispatcher = null, SolrLogManager $solrLogManager = null)
85
    {
86 7
        $this->site = $site;
87 7
        $this->indexQueue = $queue ?? GeneralUtility::makeInstance(Queue::class);
88 7
        $this->signalSlotDispatcher = $dispatcher ?? GeneralUtility::makeInstance(Dispatcher::class);
89 7
        $this->logger = $solrLogManager ?? GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
90 7
        define('EXT_SOLR_INDEXING_CONTEXT', true);
91 7
    }
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 5
    public function getContextTask()
105
    {
106 5
        return $this->contextTask;
107
    }
108
109
    /**
110
     * Indexes items from the Index Queue.
111
     *
112
     * @param int $limit
113
     * @return bool
114
     */
115 5
    public function indexItems($limit)
116
    {
117 5
        $errors     = 0;
118 5
        $indexRunId = uniqid();
119 5
        $configurationToUse = $this->site->getSolrConfiguration();
120 5
        $enableCommitsSetting = $configurationToUse->getEnableCommits();
121
122
        // get items to index
123 5
        $itemsToIndex = $this->indexQueue->getItemsToIndex($this->site, $limit);
124
125 5
        $this->emitSignal('beforeIndexItems', [$itemsToIndex, $this->getContextTask(), $indexRunId]);
126
127 5
        foreach ($itemsToIndex as $itemToIndex) {
128
            try {
129
                // try indexing
130 5
                $this->emitSignal('beforeIndexItem', [$itemToIndex, $this->getContextTask(), $indexRunId]);
131 5
                $this->indexItem($itemToIndex, $configurationToUse);
132 5
                $this->emitSignal('afterIndexItem', [$itemToIndex, $this->getContextTask(), $indexRunId]);
133
            } catch (\Exception $e) {
134
                $errors++;
135
                $this->indexQueue->markItemAsFailed($itemToIndex, $e->getCode() . ': ' . $e->__toString());
136 5
                $this->generateIndexingErrorLog($itemToIndex, $e);
137
            }
138
        }
139
140 5
        $this->emitSignal('afterIndexItems', [$itemsToIndex, $this->getContextTask(), $indexRunId]);
141
142 5
        if ($enableCommitsSetting && count($itemsToIndex) > 0) {
143 4
            $solrServers = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionsBySite($this->site);
144 4
            foreach ($solrServers as $solrServer) {
145 4
                $solrServer->getWriteService()->commit(false, false, false);
146
            }
147
        }
148
149 5
        return ($errors === 0);
150
    }
151
152
    /**
153
     * Generates a message in the error log when an error occured.
154
     *
155
     * @param Item $itemToIndex
156
     * @param \Exception  $e
157
     */
158
    protected function generateIndexingErrorLog(Item $itemToIndex, \Exception $e)
159
    {
160
        $message = 'Failed indexing Index Queue item ' . $itemToIndex->getIndexQueueUid();
161
        $data = ['code' => $e->getCode(), 'message' => $e->getMessage(), 'trace' => $e->getTraceAsString(), 'item' => (array)$itemToIndex];
162
163
        $this->logger->log(
164
            SolrLogManager::ERROR,
165
            $message,
166
            $data
167
        );
168
    }
169
170
    /**
171
     * Builds an emits a singal for the IndexService.
172
     *
173
     * @param string $name
174
     * @param array $arguments
175
     * @return mixed
176
     */
177 5
    protected function emitSignal($name, $arguments)
178
    {
179 5
        return $this->signalSlotDispatcher->dispatch(__CLASS__, $name, $arguments);
180
    }
181
182
    /**
183
     * Indexes an item from the Index Queue.
184
     *
185
     * @param Item $item An index queue item to index
186
     * @param TypoScriptConfiguration $configuration
187
     * @return bool TRUE if the item was successfully indexed, FALSE otherwise
188
     */
189 4
    protected function indexItem(Item $item, TypoScriptConfiguration $configuration)
190
    {
191 4
        $indexer = $this->getIndexerByItem($item->getIndexingConfigurationName(), $configuration);
192
193
        // Remember original http host value
194 4
        $originalHttpHost = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null;
195
196 4
        $itemChangedDate = $item->getChanged();
197 4
        $itemChangedDateAfterIndex = 0;
198
199 4
        $this->initializeHttpServerEnvironment($item);
200 4
        $itemIndexed = $indexer->index($item);
201
202
        // update IQ item so that the IQ can determine what's been indexed already
203 4
        if ($itemIndexed) {
204 4
            $this->indexQueue->updateIndexTimeByItem($item);
205 4
            $itemChangedDateAfterIndex = $item->getChanged();
206
        }
207
208 4
        if ($itemChangedDateAfterIndex > $itemChangedDate && $itemChangedDateAfterIndex > time()) {
209
            $this->indexQueue->setForcedChangeTimeByItem($item, $itemChangedDateAfterIndex);
210
        }
211
212 4
        if (!is_null($originalHttpHost)) {
213
            $_SERVER['HTTP_HOST'] = $originalHttpHost;
214
        } else {
215 4
            unset($_SERVER['HTTP_HOST']);
216
        }
217
218
        // needed since TYPO3 7.5
219 4
        GeneralUtility::flushInternalRuntimeCaches();
220
221 4
        return $itemIndexed;
222
    }
223
224
    /**
225
     * A factory method to get an indexer depending on an item's configuration.
226
     *
227
     * By default all items are indexed using the default indexer
228
     * (ApacheSolrForTypo3\Solr\IndexQueue\Indexer) coming with EXT:solr. Pages by default are
229
     * configured to be indexed through a dedicated indexer
230
     * (ApacheSolrForTypo3\Solr\IndexQueue\PageIndexer). In all other cases a dedicated indexer
231
     * can be specified through TypoScript if needed.
232
     *
233
     * @param string $indexingConfigurationName Indexing configuration name.
234
     * @param TypoScriptConfiguration $configuration
235
     * @return Indexer
236
     */
237 4
    protected function getIndexerByItem($indexingConfigurationName, TypoScriptConfiguration $configuration)
238
    {
239 4
        $indexerClass = $configuration->getIndexQueueIndexerByConfigurationName($indexingConfigurationName);
240 4
        $indexerConfiguration = $configuration->getIndexQueueIndexerConfigurationByConfigurationName($indexingConfigurationName);
241
242 4
        $indexer = GeneralUtility::makeInstance($indexerClass, /** @scrutinizer ignore-type */ $indexerConfiguration);
243 4
        if (!($indexer instanceof Indexer)) {
244
            throw new \RuntimeException(
245
                'The indexer class "' . $indexerClass . '" for indexing configuration "' . $indexingConfigurationName . '" is not a valid indexer. Must be a subclass of ApacheSolrForTypo3\Solr\IndexQueue\Indexer.',
246
                1260463206
247
            );
248
        }
249
250 4
        return $indexer;
251
    }
252
253
    /**
254
     * Gets the indexing progress.
255
     *
256
     * @return float Indexing progress as a two decimal precision float. f.e. 44.87
257
     */
258 1
    public function getProgress()
259
    {
260 1
        return $this->indexQueue->getStatisticsBySite($this->site)->getSuccessPercentage();
261
    }
262
263
    /**
264
     * Returns the amount of failed queue items for the current site.
265
     *
266
     * @return int
267
     */
268 1
    public function getFailCount()
269
    {
270 1
        return $this->indexQueue->getStatisticsBySite($this->site)->getFailedCount();
271
    }
272
273
    /**
274
     * Initializes the $_SERVER['HTTP_HOST'] environment variable in CLI
275
     * environments dependent on the Index Queue item's root page.
276
     *
277
     * When the Index Queue Worker task is executed by a cron job there is no
278
     * HTTP_HOST since we are in a CLI environment. RealURL needs the host
279
     * information to generate a proper URL though. Using the Index Queue item's
280
     * root page information we can determine the correct host although being
281
     * in a CLI environment.
282
     *
283
     * @param Item $item Index Queue item to use to determine the host.
284
     * @param
285
     */
286 4
    protected function initializeHttpServerEnvironment(Item $item)
287
    {
288 4
        static $hosts = [];
289 4
        $rootpageId = $item->getRootPageUid();
290 4
        $hostFound = !empty($hosts[$rootpageId]);
291
292 4
        if (!$hostFound) {
293 4
            $rootline = BackendUtility::BEgetRootLine($rootpageId);
294 4
            $host = BackendUtility::firstDomainRecord($rootline);
295 4
            $hosts[$rootpageId] = $host;
296
        }
297
298 4
        $_SERVER['HTTP_HOST'] = $hosts[$rootpageId];
299
300
        // needed since TYPO3 7.5
301 4
        GeneralUtility::flushInternalRuntimeCaches();
302 4
    }
303
}
304