Passed
Pull Request — release-11.2.x (#3557)
by Markus
15:20 queued 12:06
created

Queue   B

Complexity

Total Complexity 51

Size/Duplication

Total Lines 570
Duplicated Lines 0 %

Test Coverage

Coverage 95.1%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 51
eloc 116
c 1
b 0
f 0
dl 0
loc 570
ccs 136
cts 143
cp 0.951
rs 7.92

32 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A getInitializationService() 0 3 1
A getLastIndexTime() 0 11 2
A getLastIndexedItemId() 0 10 2
A updateItem() 0 6 1
A getHookImplementation() 0 3 1
A getErrorsBySite() 0 3 1
A resetAllErrors() 0 3 1
A resetErrorsBySite() 0 3 1
A resetErrorByItem() 0 3 1
A getStatisticsBySite() 0 3 1
A markItemAsFailed() 0 3 1
A addNewItem() 0 21 5
A deleteItemsByType() 0 3 1
A deleteAllItems() 0 3 1
A deleteItem() 0 3 1
A containsIndexedItem() 0 3 1
A setForcedChangeTimeByItem() 0 3 1
A updateIndexTimeByItem() 0 3 1
A updateOrAddItemForAllRelatedRootPages() 0 30 6
A containsItemWithRootPageId() 0 3 1
A getRecordCached() 0 12 2
A deleteItemsBySite() 0 3 1
A postProcessIndexQueueUpdateItem() 0 12 3
A getPageItemChangedTime() 0 7 2
A getItem() 0 3 1
A getAllItems() 0 3 1
A getItems() 0 3 1
A getItemChangedTime() 0 44 5
A getAllItemsCount() 0 3 1
A getItemsToIndex() 0 3 1
A containsItem() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Queue often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Queue, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace ApacheSolrForTypo3\Solr\IndexQueue;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2009-2015 Ingo Renner <[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\Domain\Index\Queue\QueueInitializationService;
28
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\QueueItemRepository;
29
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\ConfigurationAwareRecordService;
30
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\RootPageResolver;
31
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\Statistic\QueueStatistic;
32
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\Statistic\QueueStatisticsRepository;
33
use ApacheSolrForTypo3\Solr\Domain\Site\Site;
34
use ApacheSolrForTypo3\Solr\FrontendEnvironment;
35
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache;
36
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
37
use TYPO3\CMS\Backend\Utility\BackendUtility;
38
use TYPO3\CMS\Core\Utility\GeneralUtility;
39
40
/**
41
 * The Indexing Queue. It allows us to decouple from frontend indexing and
42
 * reacting to changes faster.
43
 *
44
 * @author Ingo Renner <[email protected]>
45
 */
46
class Queue
47
{
48
    /**
49
     * @var RootPageResolver
50
     */
51
    protected $rootPageResolver;
52
53
    /**
54
     * @var ConfigurationAwareRecordService
55
     */
56
    protected $recordService;
57
58
    /**
59
     * @var \ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager
60
     */
61
    protected $logger = null;
62
63
    /**
64
     * @var QueueItemRepository
65
     */
66
    protected $queueItemRepository;
67
68
    /**
69
     * @var QueueStatisticsRepository
70
     */
71
    protected $queueStatisticsRepository;
72
73
    /**
74
     * @var QueueInitializationService
75
     */
76
    protected $queueInitializationService;
77
78
    /**
79
     * @var FrontendEnvironment
80
     */
81
    protected $frontendEnvironment = null;
82
83
    /**
84
     * Queue constructor.
85
     * @param RootPageResolver|null $rootPageResolver
86
     * @param ConfigurationAwareRecordService|null $recordService
87
     * @param QueueItemRepository|null $queueItemRepository
88
     * @param QueueStatisticsRepository|null $queueStatisticsRepository
89
     * @param QueueInitializationService|null $queueInitializationService
90
     */
91 150
    public function __construct(
92
        RootPageResolver $rootPageResolver = null,
93
        ConfigurationAwareRecordService $recordService = null,
94
        QueueItemRepository $queueItemRepository = null,
95
        QueueStatisticsRepository $queueStatisticsRepository = null,
96
        QueueInitializationService $queueInitializationService = null,
97
        FrontendEnvironment $frontendEnvironment = null
98
    )
99
    {
100 150
        $this->logger = GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
101 150
        $this->rootPageResolver = $rootPageResolver ?? GeneralUtility::makeInstance(RootPageResolver::class);
102 150
        $this->recordService = $recordService ?? GeneralUtility::makeInstance(ConfigurationAwareRecordService::class);
103 150
        $this->queueItemRepository = $queueItemRepository ?? GeneralUtility::makeInstance(QueueItemRepository::class);
104 150
        $this->queueStatisticsRepository = $queueStatisticsRepository ??  GeneralUtility::makeInstance(QueueStatisticsRepository::class);
105 150
        $this->queueInitializationService = $queueInitializationService ?? GeneralUtility::makeInstance(QueueInitializationService::class, /** @scrutinizer ignore-type */ $this);
106 150
        $this->frontendEnvironment = $frontendEnvironment ?? GeneralUtility::makeInstance(FrontendEnvironment::class);
107 150
    }
108
109
    // FIXME some of the methods should be renamed to plural forms
110
    // FIXME singular form methods should deal with exactly one item only
111
112
    /**
113
     * Returns the timestamp of the last indexing run.
114
     *
115
     * @param int $rootPageId The root page uid for which to get
116
     *      the last indexed item id
117
     * @return int Timestamp of last index run.
118
     */
119 2
    public function getLastIndexTime($rootPageId)
120
    {
121 2
        $lastIndexTime = 0;
122
123 2
        $lastIndexedRow = $this->queueItemRepository->findLastIndexedRow($rootPageId);
124
125 2
        if ($lastIndexedRow[0]['indexed']) {
126 1
            $lastIndexTime = $lastIndexedRow[0]['indexed'];
127
        }
128
129 2
        return $lastIndexTime;
130
    }
131
132
    /**
133
     * Returns the uid of the last indexed item in the queue
134
     *
135
     * @param int $rootPageId The root page uid for which to get
136
     *      the last indexed item id
137
     * @return int The last indexed item's ID.
138
     */
139 3
    public function getLastIndexedItemId($rootPageId)
140
    {
141 3
        $lastIndexedItemId = 0;
142
143 3
        $lastIndexedItemRow = $this->queueItemRepository->findLastIndexedRow($rootPageId);
144 3
        if ($lastIndexedItemRow[0]['uid']) {
145 2
            $lastIndexedItemId = $lastIndexedItemRow[0]['uid'];
146
        }
147
148 3
        return $lastIndexedItemId;
149
    }
150
151
    /**
152
     * @return QueueInitializationService
153
     */
154 6
    public function getInitializationService()
155
    {
156 6
        return $this->queueInitializationService;
157
    }
158
159
    /**
160
     * Marks an item as needing (re)indexing.
161
     *
162
     * Like with Solr itself, there's no add method, just a simple update method
163
     * that handles the adds, too.
164
     *
165
     * The method creates or updates the index queue items for all related rootPageIds.
166
     *
167
     * @param string $itemType The item's type, usually a table name.
168
     * @param string $itemUid The item's uid, usually an integer uid, could be a different value for non-database-record types.
169
     * @param int $forcedChangeTime The change time for the item if set, otherwise value from getItemChangedTime() is used.
170
     * @return int Number of updated/created items
171
     */
172 89
    public function updateItem($itemType, $itemUid, $forcedChangeTime = 0)
173
    {
174 89
        $updateCount = $this->updateOrAddItemForAllRelatedRootPages($itemType, $itemUid, $forcedChangeTime);
175 88
        $updateCount = $this->postProcessIndexQueueUpdateItem($itemType, $itemUid, $updateCount, $forcedChangeTime);
0 ignored issues
show
Bug introduced by
$itemUid of type string is incompatible with the type integer expected by parameter $itemUid of ApacheSolrForTypo3\Solr\...sIndexQueueUpdateItem(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

175
        $updateCount = $this->postProcessIndexQueueUpdateItem($itemType, /** @scrutinizer ignore-type */ $itemUid, $updateCount, $forcedChangeTime);
Loading history...
176
177 88
        return $updateCount;
178
    }
179
180
    /**
181
     * Updates or add's the item for all relevant root pages.
182
     *
183
     * @param string $itemType The item's type, usually a table name.
184
     * @param string $itemUid The item's uid, usually an integer uid, could be a different value for non-database-record types.
185
     * @param int $forcedChangeTime The change time for the item if set, otherwise value from getItemChangedTime() is used.
186
     * @return int
187
     */
188 87
    protected function updateOrAddItemForAllRelatedRootPages($itemType, $itemUid, $forcedChangeTime): int
189
    {
190 87
        $updateCount = 0;
191 87
        $rootPageIds = $this->rootPageResolver->getResponsibleRootPageIds($itemType, $itemUid);
0 ignored issues
show
Bug introduced by
$itemUid of type string is incompatible with the type integer expected by parameter $uid of ApacheSolrForTypo3\Solr\...esponsibleRootPageIds(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

191
        $rootPageIds = $this->rootPageResolver->getResponsibleRootPageIds($itemType, /** @scrutinizer ignore-type */ $itemUid);
Loading history...
192 86
        foreach ($rootPageIds as $rootPageId) {
193 86
            $skipInvalidRootPage = $rootPageId === 0;
194 86
            if ($skipInvalidRootPage) {
195
                continue;
196
            }
197
198 86
            $solrConfiguration = $this->frontendEnvironment->getSolrConfigurationFromPageId($rootPageId);
199 86
            $indexingConfiguration = $this->recordService->getIndexingConfigurationName($itemType, $itemUid, $solrConfiguration);
0 ignored issues
show
Bug introduced by
$itemUid of type string is incompatible with the type integer expected by parameter $recordUid of ApacheSolrForTypo3\Solr\...xingConfigurationName(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

199
            $indexingConfiguration = $this->recordService->getIndexingConfigurationName($itemType, /** @scrutinizer ignore-type */ $itemUid, $solrConfiguration);
Loading history...
200 86
            if ($indexingConfiguration === null) {
201 3
                continue;
202
            }
203 85
            $indexingPriority = $solrConfiguration->getIndexQueueIndexingPriorityByConfigurationName($indexingConfiguration);
204 85
            $itemInQueueForRootPage = $this->containsItemWithRootPageId($itemType, $itemUid, $rootPageId);
205 85
            if ($itemInQueueForRootPage) {
206
                // update changed time if that item is in the queue already
207 17
                $changedTime = ($forcedChangeTime > 0) ? $forcedChangeTime : $this->getItemChangedTime($itemType, $itemUid);
208 17
                $updatedRows = $this->queueItemRepository->updateExistingItemByItemTypeAndItemUidAndRootPageId($itemType, $itemUid, $rootPageId, $changedTime, $indexingConfiguration, $indexingPriority);
0 ignored issues
show
Bug introduced by
$itemUid of type string is incompatible with the type integer expected by parameter $itemUid of ApacheSolrForTypo3\Solr\...dItemUidAndRootPageId(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

208
                $updatedRows = $this->queueItemRepository->updateExistingItemByItemTypeAndItemUidAndRootPageId($itemType, /** @scrutinizer ignore-type */ $itemUid, $rootPageId, $changedTime, $indexingConfiguration, $indexingPriority);
Loading history...
209
            } else {
210
                // add the item since it's not in the queue yet
211 80
                $updatedRows = $this->addNewItem($itemType, $itemUid, $indexingConfiguration, $rootPageId, $indexingPriority);
212
            }
213
214 85
            $updateCount += $updatedRows;
215
        }
216
217 86
        return $updateCount;
218
    }
219
220
    /**
221
     * Executes the updateItem post processing hook.
222
     *
223
     * @param string $itemType
224
     * @param int $itemUid
225
     * @param int $updateCount
226
     * @param int $forcedChangeTime
227
     * @return int
228
     */
229 88
    protected function postProcessIndexQueueUpdateItem($itemType, $itemUid, $updateCount, $forcedChangeTime = 0)
230
    {
231 88
        if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueUpdateItem'])) {
232 87
            return $updateCount;
233
        }
234
235 1
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueUpdateItem'] as $classReference) {
236 1
            $updateHandler = $this->getHookImplementation($classReference);
237 1
            $updateCount = $updateHandler->postProcessIndexQueueUpdateItem($itemType, $itemUid, $updateCount, $forcedChangeTime);
238
        }
239
240 1
        return $updateCount;
241
    }
242
243
    /**
244
     * @param string $classReference
245
     * @return object
246
     */
247
    protected function getHookImplementation($classReference)
248
    {
249
        return GeneralUtility::makeInstance($classReference);
250
    }
251
252
    /**
253
     * Finds indexing errors for the current site
254
     *
255
     * @param Site $site
256
     * @return array Error items for the current site's Index Queue
257
     */
258 3
    public function getErrorsBySite(Site $site)
259
    {
260 3
        return $this->queueItemRepository->findErrorsBySite($site);
261
    }
262
263
    /**
264
     * Resets all the errors for all index queue items.
265
     *
266
     * @return mixed
267
     */
268 1
    public function resetAllErrors()
269
    {
270 1
        return $this->queueItemRepository->flushAllErrors();
271
    }
272
273
    /**
274
     * Resets the errors in the index queue for a specific site
275
     *
276
     * @param Site $site
277
     * @return mixed
278
     */
279 1
    public function resetErrorsBySite(Site $site)
280
    {
281 1
        return $this->queueItemRepository->flushErrorsBySite($site);
282
    }
283
284
    /**
285
     * Resets the error in the index queue for a specific item
286
     *
287
     * @param Item $item
288
     * @return mixed
289
     */
290 1
    public function resetErrorByItem(Item $item)
291
    {
292 1
        return $this->queueItemRepository->flushErrorByItem($item);
293
    }
294
295
    /**
296
     * Adds an item to the index queue.
297
     *
298
     * Not meant for public use.
299
     *
300
     * @param string $itemType The item's type, usually a table name.
301
     * @param string $itemUid The item's uid, usually an integer uid, could be a
302
     *      different value for non-database-record types.
303
     * @param string $indexingConfiguration The item's indexing configuration to use.
304
     *      Optional, overwrites existing / determined configuration.
305
     * @param $rootPageId
306
     * @param int $indexingPriority
307
     * @return int
308
     */
309 80
    private function addNewItem(
310
        $itemType,
311
        $itemUid,
312
        $indexingConfiguration,
313
        $rootPageId,
314
        int $indexingPriority = 0
315
    ): int {
316 80
        $additionalRecordFields = '';
317 80
        if ($itemType === 'pages') {
318 52
            $additionalRecordFields = ', doktype, uid';
319
        }
320
321 80
        $record = $this->getRecordCached($itemType, $itemUid, $additionalRecordFields);
322
323 80
        if (empty($record) || ($itemType === 'pages' && !$this->frontendEnvironment->isAllowedPageType($record, $indexingConfiguration))) {
324
            return 0;
325
        }
326
327 80
        $changedTime = $this->getItemChangedTime($itemType, $itemUid);
328
329 80
        return $this->queueItemRepository->add($itemType, $itemUid, $rootPageId, $changedTime, $indexingConfiguration, $indexingPriority);
0 ignored issues
show
Bug introduced by
$itemUid of type string is incompatible with the type integer expected by parameter $itemUid of ApacheSolrForTypo3\Solr\...ueItemRepository::add(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

329
        return $this->queueItemRepository->add($itemType, /** @scrutinizer ignore-type */ $itemUid, $rootPageId, $changedTime, $indexingConfiguration, $indexingPriority);
Loading history...
330
    }
331
332
    /**
333
     * Get record to be added in addNewItem
334
     *
335
     * @param string $itemType The item's type, usually a table name.
336
     * @param string $itemUid The item's uid, usually an integer uid, could be a
337
     *      different value for non-database-record types.
338
     * @param string $additionalRecordFields for sql-query
339
     *
340
     * @return array|NULL
341
     */
342 80
    protected function getRecordCached($itemType, $itemUid, $additionalRecordFields)
343
    {
344 80
        $cache = GeneralUtility::makeInstance(TwoLevelCache::class, /** @scrutinizer ignore-type */ 'runtime');
345 80
        $cacheId = md5('Queue' . ':' . 'getRecordCached' . ':' . $itemType . ':' . $itemUid . ':' . 'pid' . $additionalRecordFields);
346
347 80
        $record = $cache->get($cacheId);
348 80
        if (empty($record)) {
349 80
            $record = BackendUtility::getRecord($itemType, $itemUid, 'pid' . $additionalRecordFields);
0 ignored issues
show
Bug introduced by
$itemUid of type string is incompatible with the type integer expected by parameter $uid of TYPO3\CMS\Backend\Utilit...endUtility::getRecord(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

349
            $record = BackendUtility::getRecord($itemType, /** @scrutinizer ignore-type */ $itemUid, 'pid' . $additionalRecordFields);
Loading history...
350 80
            $cache->set($cacheId, $record);
351
        }
352
353 80
        return $record;
354
    }
355
356
    /**
357
     * Determines the time for when an item should be indexed. This timestamp
358
     * is then stored in the changed column in the Index Queue.
359
     *
360
     * The changed timestamp usually is now - time(). For records which are set
361
     * to published at a later time, this timestamp is the start time. So if a
362
     * future start time has been set, that will be used to delay indexing
363
     * of an item.
364
     *
365
     * @param string $itemType The item's table name.
366
     * @param string $itemUid The item's uid, usually an integer uid, could be a
367
     *      different value for non-database-record types.
368
     * @return int Timestamp of the item's changed time or future start time
369
     */
370 85
    protected function getItemChangedTime($itemType, $itemUid)
371
    {
372 85
        $itemTypeHasStartTimeColumn = false;
373 85
        $changedTimeColumns = $GLOBALS['TCA'][$itemType]['ctrl']['tstamp'];
374 85
        $startTime = 0;
375 85
        $pageChangedTime = 0;
376
377 85
        if (!empty($GLOBALS['TCA'][$itemType]['ctrl']['enablecolumns']['starttime'])) {
378 85
            $itemTypeHasStartTimeColumn = true;
379 85
            $changedTimeColumns .= ', ' . $GLOBALS['TCA'][$itemType]['ctrl']['enablecolumns']['starttime'];
380
        }
381 85
        if ($itemType === 'pages') {
382
            // does not carry time information directly, but needed to support
383
            // canonical pages
384 57
            $changedTimeColumns .= ', content_from_pid';
385
        }
386
387 85
        $record = BackendUtility::getRecord($itemType, $itemUid, $changedTimeColumns);
0 ignored issues
show
Bug introduced by
$itemUid of type string is incompatible with the type integer expected by parameter $uid of TYPO3\CMS\Backend\Utilit...endUtility::getRecord(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

387
        $record = BackendUtility::getRecord($itemType, /** @scrutinizer ignore-type */ $itemUid, $changedTimeColumns);
Loading history...
388 85
        $itemChangedTime = $record[$GLOBALS['TCA'][$itemType]['ctrl']['tstamp']];
389
390 85
        if ($itemTypeHasStartTimeColumn) {
391 85
            $startTime = $record[$GLOBALS['TCA'][$itemType]['ctrl']['enablecolumns']['starttime']];
392
        }
393
394 85
        if ($itemType === 'pages') {
395 57
            $record['uid'] = $itemUid;
396
            // overrule the page's last changed time with the most recent
397
            //content element change
398 57
            $pageChangedTime = $this->getPageItemChangedTime($record);
0 ignored issues
show
Bug introduced by
It seems like $record can also be of type null; however, parameter $page of ApacheSolrForTypo3\Solr\...etPageItemChangedTime() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

398
            $pageChangedTime = $this->getPageItemChangedTime(/** @scrutinizer ignore-type */ $record);
Loading history...
399
        }
400
401 85
        $localizationsChangedTime = $this->queueItemRepository->getLocalizableItemChangedTime($itemType, (int)$itemUid);
402
403
        // if start time exists and start time is higher than last changed timestamp
404
        // then set changed to the future start time to make the item
405
        // indexed at a later time
406 85
        $changedTime = max(
407 85
            $itemChangedTime,
408
            $pageChangedTime,
409
            $localizationsChangedTime,
410
            $startTime
411
        );
412
413 85
        return $changedTime;
414
    }
415
416
    /**
417
     * Gets the most recent changed time of a page's content elements
418
     *
419
     * @param array $page Partial page record
420
     * @return int Timestamp of the most recent content element change
421
     */
422 57
    protected function getPageItemChangedTime(array $page)
423
    {
424 57
        if (!empty($page['content_from_pid'])) {
425
            // canonical page, get the original page's last changed time
426
            return $this->queueItemRepository->getPageItemChangedTimeByPageUid((int)$page['content_from_pid']);
427
        }
428 57
        return $this->queueItemRepository->getPageItemChangedTimeByPageUid((int)$page['uid']);
429
    }
430
431
    /**
432
     * Checks whether the Index Queue contains a specific item.
433
     *
434
     * @param string $itemType The item's type, usually a table name.
435
     * @param string $itemUid The item's uid, usually an integer uid, could be a
436
     *      different value for non-database-record types.
437
     * @return bool TRUE if the item is found in the queue, FALSE otherwise
438
     */
439 10
    public function containsItem($itemType, $itemUid)
440
    {
441 10
        return $this->queueItemRepository->containsItem($itemType, (int)$itemUid);
442
    }
443
444
    /**
445
     * Checks whether the Index Queue contains a specific item.
446
     *
447
     * @param string $itemType The item's type, usually a table name.
448
     * @param string $itemUid The item's uid, usually an integer uid, could be a
449
     *      different value for non-database-record types.
450
     * @param integer $rootPageId
451
     * @return bool TRUE if the item is found in the queue, FALSE otherwise
452
     */
453 85
    public function containsItemWithRootPageId($itemType, $itemUid, $rootPageId)
454
    {
455 85
        return $this->queueItemRepository->containsItemWithRootPageId($itemType, (int)$itemUid, (int)$rootPageId);
456
    }
457
458
    /**
459
     * Checks whether the Index Queue contains a specific item that has been
460
     * marked as indexed.
461
     *
462
     * @param string $itemType The item's type, usually a table name.
463
     * @param string $itemUid The item's uid, usually an integer uid, could be a
464
     *      different value for non-database-record types.
465
     * @return bool TRUE if the item is found in the queue and marked as
466
     *      indexed, FALSE otherwise
467
     */
468 4
    public function containsIndexedItem($itemType, $itemUid)
469
    {
470 4
        return $this->queueItemRepository->containsIndexedItem($itemType, (int)$itemUid);
471
    }
472
473
    /**
474
     * Removes an item from the Index Queue.
475
     *
476
     * @param string $itemType The type of the item to remove, usually a table name.
477
     * @param int $itemUid The uid of the item to remove
478
     */
479 56
    public function deleteItem($itemType, $itemUid)
480
    {
481 56
        $this->queueItemRepository->deleteItem($itemType, (int)$itemUid);
482 56
    }
483
484
    /**
485
     * Removes all items of a certain type from the Index Queue.
486
     *
487
     * @param string $itemType The type of items to remove, usually a table name.
488
     */
489 1
    public function deleteItemsByType($itemType)
490
    {
491 1
        $this->queueItemRepository->deleteItemsByType($itemType);
492 1
    }
493
494
    /**
495
     * Removes all items of a certain site from the Index Queue. Accepts an
496
     * optional parameter to limit the deleted items by indexing configuration.
497
     *
498
     * @param Site $site The site to remove items for.
499
     * @param string $indexingConfigurationName Name of a specific indexing
500
     *      configuration
501
     */
502 6
    public function deleteItemsBySite(Site $site, $indexingConfigurationName = '')
503
    {
504 6
        $this->queueItemRepository->deleteItemsBySite($site, $indexingConfigurationName);
505 6
    }
506
507
    /**
508
     * Removes all items from the Index Queue.
509
     *
510
     */
511 1
    public function deleteAllItems()
512
    {
513 1
        $this->queueItemRepository->deleteAllItems();
514 1
    }
515
516
    /**
517
     * Gets a single Index Queue item by its uid.
518
     *
519
     * @param int $itemId Index Queue item uid
520
     * @return Item The request Index Queue item or NULL if no item with $itemId was found
521
     */
522 34
    public function getItem($itemId)
523
    {
524 34
        return $this->queueItemRepository->findItemByUid($itemId);
525
    }
526
527
    /**
528
     * Gets Index Queue items by type and uid.
529
     *
530
     * @param string $itemType item type, usually  the table name
531
     * @param int $itemUid item uid
532
     * @return Item[] An array of items matching $itemType and $itemUid
533
     */
534 50
    public function getItems($itemType, $itemUid)
535
    {
536 50
        return $this->queueItemRepository->findItemsByItemTypeAndItemUid($itemType, (int)$itemUid);
537
    }
538
539
    /**
540
     * Returns all items in the queue.
541
     *
542
     * @return Item[] An array of items
543
     */
544 4
    public function getAllItems()
545
    {
546 4
        return $this->queueItemRepository->findAll();
547
    }
548
549
    /**
550
     * Returns the number of items for all queues.
551
     *
552
     * @return int
553
     */
554 111
    public function getAllItemsCount()
555
    {
556 111
        return $this->queueItemRepository->count();
557
    }
558
559
    /**
560
     * Extracts the number of pending, indexed and erroneous items from the
561
     * Index Queue.
562
     *
563
     * @param Site $site
564
     * @param string $indexingConfigurationName
565
     *
566
     * @return QueueStatistic
567
     */
568 5
    public function getStatisticsBySite(Site $site, $indexingConfigurationName = '')
569
    {
570 5
        return $this->queueStatisticsRepository->findOneByRootPidAndOptionalIndexingConfigurationName($site->getRootPageId(), $indexingConfigurationName);
571
    }
572
573
    /**
574
     * Gets $limit number of items to index for a particular $site.
575
     *
576
     * @param Site $site TYPO3 site
577
     * @param int $limit Number of items to get from the queue
578
     * @return Item[] Items to index to the given solr server
579
     */
580 3
    public function getItemsToIndex(Site $site, $limit = 50)
581
    {
582 3
        return $this->queueItemRepository->findItemsToIndex($site, $limit);
583
    }
584
585
    /**
586
     * Marks an item as failed and causes the indexer to skip the item in the
587
     * next run.
588
     *
589
     * @param int|Item $item Either the item's Index Queue uid or the complete item
590
     * @param string $errorMessage Error message
591
     */
592 6
    public function markItemAsFailed($item, $errorMessage = '')
593
    {
594 6
        $this->queueItemRepository->markItemAsFailed($item, $errorMessage);
595 6
    }
596
597
    /**
598
     * Sets the timestamp of when an item last has been indexed.
599
     *
600
     * @param Item $item
601
     */
602 2
    public function updateIndexTimeByItem(Item $item)
603
    {
604 2
        $this->queueItemRepository->updateIndexTimeByItem($item);
605 2
    }
606
607
    /**
608
     * Sets the change timestamp of an item.
609
     *
610
     * @param Item $item
611
     * @param int $forcedChangeTime The change time for the item
612
     */
613
    public function setForcedChangeTimeByItem(Item $item, $forcedChangeTime)
614
    {
615
        $this->queueItemRepository->updateChangedTimeByItem($item, $forcedChangeTime);
616
    }
617
}
618