Passed
Push — release-11.5.x ( 39fc07...8ccd81 )
by Markus
34:52 queued 29:33
created

Queue   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 634
Duplicated Lines 0 %

Test Coverage

Coverage 95.17%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 52
eloc 121
c 1
b 0
f 0
dl 0
loc 634
ccs 138
cts 145
cp 0.9517
rs 7.44

32 Methods

Rating   Name   Duplication   Size   Complexity  
A getLastIndexTime() 0 11 2
A getStatisticsBySite() 0 6 1
A markItemAsFailed() 0 3 1
A addNewItem() 0 20 5
A deleteItemsByType() 0 3 1
A __construct() 0 15 1
A getInitializationService() 0 3 1
A getHookImplementation() 0 3 1
A deleteAllItems() 0 3 1
A deleteItem() 0 3 1
A getLastIndexedItemId() 0 10 2
A containsIndexedItem() 0 3 1
A setForcedChangeTimeByItem() 0 3 1
A updateIndexTimeByItem() 0 3 1
B updateOrAddItemForAllRelatedRootPages() 0 37 7
A getErrorsBySite() 0 3 1
A resetAllErrors() 0 3 1
A containsItemWithRootPageId() 0 3 1
A getRecordCached() 0 12 2
A deleteItemsBySite() 0 3 1
A postProcessIndexQueueUpdateItem() 0 16 3
A getPageItemChangedTime() 0 7 2
A getItem() 0 3 1
A getAllItems() 0 3 1
A getItems() 0 3 1
A updateItem() 0 4 1
A getItemChangedTime() 0 41 5
A getAllItemsCount() 0 3 1
A resetErrorsBySite() 0 3 1
A getItemsToIndex() 0 3 1
A resetErrorByItem() 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
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace ApacheSolrForTypo3\Solr\IndexQueue;
19
20
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\QueueInitializationService;
21
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\QueueItemRepository;
22
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\ConfigurationAwareRecordService;
23
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\RecordMonitor\Helper\RootPageResolver;
24
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\Statistic\QueueStatistic;
25
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\Statistic\QueueStatisticsRepository;
26
use ApacheSolrForTypo3\Solr\Domain\Site\Site;
27
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
28
use ApacheSolrForTypo3\Solr\FrontendEnvironment;
29
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache;
30
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
31
use Doctrine\DBAL\ConnectionException;
32
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
33
use Doctrine\DBAL\Exception as DBALException;
34
use InvalidArgumentException;
35
use Throwable;
36
use TYPO3\CMS\Backend\Utility\BackendUtility;
37
use TYPO3\CMS\Core\Utility\GeneralUtility;
38
39
/**
40
 * The Indexing Queue. It allows us to decouple from frontend indexing and
41
 * reacting to the changes faster.
42
 *
43
 * @author Ingo Renner <[email protected]>
44
 */
45
class Queue
46
{
47
    /**
48
     * @var RootPageResolver
49
     */
50
    protected RootPageResolver $rootPageResolver;
51
52
    /**
53
     * @var ConfigurationAwareRecordService
54
     */
55
    protected ConfigurationAwareRecordService $recordService;
56
57
    /**
58
     * @var SolrLogManager
59
     */
60
    protected SolrLogManager $logger;
61
62
    /**
63
     * @var QueueItemRepository
64
     */
65
    protected QueueItemRepository $queueItemRepository;
66
67
    /**
68
     * @var QueueStatisticsRepository
69
     */
70
    protected QueueStatisticsRepository $queueStatisticsRepository;
71
72
    /**
73
     * @var QueueInitializationService
74
     */
75
    protected QueueInitializationService $queueInitializationService;
76
77
    /**
78
     * @var FrontendEnvironment
79
     */
80
    protected FrontendEnvironment $frontendEnvironment;
81
82
    /**
83
     * Queue constructor.
84
     * @param RootPageResolver|null $rootPageResolver
85
     * @param ConfigurationAwareRecordService|null $recordService
86
     * @param QueueItemRepository|null $queueItemRepository
87
     * @param QueueStatisticsRepository|null $queueStatisticsRepository
88
     * @param QueueInitializationService|null $queueInitializationService
89
     */
90 156
    public function __construct(
91
        RootPageResolver $rootPageResolver = null,
92
        ConfigurationAwareRecordService $recordService = null,
93
        QueueItemRepository $queueItemRepository = null,
94
        QueueStatisticsRepository $queueStatisticsRepository = null,
95
        QueueInitializationService $queueInitializationService = null,
96
        FrontendEnvironment $frontendEnvironment = null
97
    ) {
98 156
        $this->logger = GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
99 156
        $this->rootPageResolver = $rootPageResolver ?? GeneralUtility::makeInstance(RootPageResolver::class);
100 156
        $this->recordService = $recordService ?? GeneralUtility::makeInstance(ConfigurationAwareRecordService::class);
101 156
        $this->queueItemRepository = $queueItemRepository ?? GeneralUtility::makeInstance(QueueItemRepository::class);
102 156
        $this->queueStatisticsRepository = $queueStatisticsRepository ??  GeneralUtility::makeInstance(QueueStatisticsRepository::class);
103 156
        $this->queueInitializationService = $queueInitializationService ?? GeneralUtility::makeInstance(QueueInitializationService::class, /** @scrutinizer ignore-type */ $this);
104 156
        $this->frontendEnvironment = $frontendEnvironment ?? GeneralUtility::makeInstance(FrontendEnvironment::class);
105
    }
106
107
    // FIXME some of the methods should be renamed to plural forms
108
    // FIXME singular form methods should deal with exactly one item only
109
110
    /**
111
     * Returns the timestamp of the last indexing run.
112
     *
113
     * @param int $rootPageId The root page uid for which to get
114
     *      the last indexed item id
115
     * @return int Timestamp of last index run.
116
     * @throws DBALDriverException
117
     * @throws DBALException|\Doctrine\DBAL\DBALException
118
     */
119 2
    public function getLastIndexTime(int $rootPageId): int
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
     * @throws DBALDriverException
139
     * @throws DBALException|\Doctrine\DBAL\DBALException
140
     */
141 3
    public function getLastIndexedItemId(int $rootPageId): int
142
    {
143 3
        $lastIndexedItemId = 0;
144
145 3
        $lastIndexedItemRow = $this->queueItemRepository->findLastIndexedRow($rootPageId);
146 3
        if ($lastIndexedItemRow[0]['uid']) {
147 2
            $lastIndexedItemId = $lastIndexedItemRow[0]['uid'];
148
        }
149
150 3
        return $lastIndexedItemId;
151
    }
152
153
    /**
154
     * @return QueueInitializationService
155
     */
156 6
    public function getInitializationService(): QueueInitializationService
157
    {
158 6
        return $this->queueInitializationService;
159
    }
160
161
    /**
162
     * Marks an item as needing (re)indexing.
163
     *
164
     * Like with Solr itself, there's no add method, just a simple update method
165
     * that handles the adds, too.
166
     *
167
     * The method creates or updates the index queue items for all related rootPageIds.
168
     *
169
     * @param string $itemType The item's type, usually a table name.
170
     * @param int|string $itemUid The item's uid, usually an integer uid, could be a different value for non-database-record types.
171
     * @param int $forcedChangeTime The change time for the item if set, otherwise value from getItemChangedTime() is used.
172
     * @return int Number of updated/created items
173
     * @throws DBALDriverException
174
     * @throws DBALException|\Doctrine\DBAL\DBALException
175
     * @throws Throwable
176
     */
177 94
    public function updateItem(string $itemType, $itemUid, int $forcedChangeTime = 0): int
178
    {
179 94
        $updateCount = $this->updateOrAddItemForAllRelatedRootPages($itemType, $itemUid, $forcedChangeTime);
180 94
        return $this->postProcessIndexQueueUpdateItem($itemType, $itemUid, $updateCount, $forcedChangeTime);
181
    }
182
183
    /**
184
     * Updates or adds the item for all relevant root pages.
185
     *
186
     * @param string $itemType The item's type, usually a table name.
187
     * @param int|string $itemUid The item's uid, usually an integer uid, could be a different value for non-database-record types.
188
     * @param int $forcedChangeTime The change time for the item if set, otherwise value from getItemChangedTime() is used.
189
     * @return int
190
     * @throws DBALDriverException
191
     * @throws DBALException|\Doctrine\DBAL\DBALException
192
     * @throws Throwable
193
     */
194 92
    protected function updateOrAddItemForAllRelatedRootPages(string $itemType, $itemUid, int $forcedChangeTime): int
195
    {
196 92
        $updateCount = 0;
197
        try {
198 92
            $rootPageIds = $this->rootPageResolver->getResponsibleRootPageIds($itemType, $itemUid);
0 ignored issues
show
Bug introduced by
It seems like $itemUid can also be of type string; however, parameter $uid of ApacheSolrForTypo3\Solr\...esponsibleRootPageIds() does only seem to accept integer, 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

198
            $rootPageIds = $this->rootPageResolver->getResponsibleRootPageIds($itemType, /** @scrutinizer ignore-type */ $itemUid);
Loading history...
199 4
        } catch (InvalidArgumentException $e) {
200 4
            $this->deleteItem($itemType, $itemUid);
201 4
            return 0;
202
        }
203
204 88
        foreach ($rootPageIds as $rootPageId) {
205 88
            $skipInvalidRootPage = $rootPageId === 0;
206 88
            if ($skipInvalidRootPage) {
207
                continue;
208
            }
209
210
            /* @var SiteRepository $siteRepository */
211 88
            $siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
212 88
            $solrConfiguration = $siteRepository->getSiteByRootPageId($rootPageId)->getSolrConfiguration();
213 88
            $indexingConfiguration = $this->recordService->getIndexingConfigurationName($itemType, $itemUid, $solrConfiguration);
0 ignored issues
show
Bug introduced by
It seems like $itemUid can also be of type string; however, parameter $recordUid of ApacheSolrForTypo3\Solr\...xingConfigurationName() does only seem to accept integer, 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

213
            $indexingConfiguration = $this->recordService->getIndexingConfigurationName($itemType, /** @scrutinizer ignore-type */ $itemUid, $solrConfiguration);
Loading history...
214 88
            if ($indexingConfiguration === null) {
215 3
                continue;
216
            }
217 87
            $itemInQueueForRootPage = $this->containsItemWithRootPageId($itemType, $itemUid, $rootPageId);
218 87
            if ($itemInQueueForRootPage) {
219
                // update changed time if that item is in the queue already
220 19
                $changedTime = ($forcedChangeTime > 0) ? $forcedChangeTime : $this->getItemChangedTime($itemType, $itemUid);
221 19
                $updatedRows = $this->queueItemRepository->updateExistingItemByItemTypeAndItemUidAndRootPageId($itemType, $itemUid, $rootPageId, $changedTime, $indexingConfiguration);
0 ignored issues
show
Bug introduced by
It seems like $itemUid can also be of type string; however, parameter $itemUid of ApacheSolrForTypo3\Solr\...dItemUidAndRootPageId() does only seem to accept integer, 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

221
                $updatedRows = $this->queueItemRepository->updateExistingItemByItemTypeAndItemUidAndRootPageId($itemType, /** @scrutinizer ignore-type */ $itemUid, $rootPageId, $changedTime, $indexingConfiguration);
Loading history...
222
            } else {
223
                // add the item since it's not in the queue yet
224 80
                $updatedRows = $this->addNewItem($itemType, $itemUid, $indexingConfiguration, $rootPageId);
225
            }
226
227 87
            $updateCount += $updatedRows;
228
        }
229
230 88
        return $updateCount;
231
    }
232
233
    /**
234
     * Executes the updateItem post-processing hook.
235
     *
236
     * @param string $itemType
237
     * @param int|string $itemUid The item's uid, usually an integer uid, could be a different value for non-database-record types.
238
     * @param int $updateCount
239
     * @param int $forcedChangeTime
240
     * @return int
241
     */
242 94
    protected function postProcessIndexQueueUpdateItem(
243
        string $itemType,
244
        $itemUid,
245
        int $updateCount,
246
        int $forcedChangeTime = 0
247
    ): int {
248 94
        if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueUpdateItem'] ?? null)) {
249 93
            return $updateCount;
250
        }
251
252 1
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueUpdateItem'] as $classReference) {
253 1
            $updateHandler = $this->getHookImplementation($classReference);
254 1
            $updateCount = $updateHandler->postProcessIndexQueueUpdateItem($itemType, $itemUid, $updateCount, $forcedChangeTime);
255
        }
256
257 1
        return $updateCount;
258
    }
259
260
    /**
261
     * @param string $classReference
262
     * @return object
263
     */
264
    protected function getHookImplementation(string $classReference): object
265
    {
266
        return GeneralUtility::makeInstance($classReference);
267
    }
268
269
    /**
270
     * Finds indexing errors for the current site
271
     *
272
     * @param Site $site
273
     * @return array Error items for the current site's Index Queue
274
     * @throws DBALDriverException
275
     * @throws DBALException|\Doctrine\DBAL\DBALException
276
     */
277 3
    public function getErrorsBySite(Site $site): array
278
    {
279 3
        return $this->queueItemRepository->findErrorsBySite($site);
280
    }
281
282
    /**
283
     * Resets all the errors for all index queue items.
284
     *
285
     * @return mixed
286
     * @throws DBALException|\Doctrine\DBAL\DBALException
287
     */
288 1
    public function resetAllErrors()
289
    {
290 1
        return $this->queueItemRepository->flushAllErrors();
291
    }
292
293
    /**
294
     * Resets the errors in the index queue for a specific site
295
     *
296
     * @param Site $site
297
     * @return mixed
298
     * @throws DBALException|\Doctrine\DBAL\DBALException
299
     */
300 1
    public function resetErrorsBySite(Site $site)
301
    {
302 1
        return $this->queueItemRepository->flushErrorsBySite($site);
303
    }
304
305
    /**
306
     * Resets the error in the index queue for a specific item
307
     *
308
     * @param Item $item
309
     * @return mixed
310
     * @throws DBALException|\Doctrine\DBAL\DBALException
311
     */
312 1
    public function resetErrorByItem(Item $item)
313
    {
314 1
        return $this->queueItemRepository->flushErrorByItem($item);
315
    }
316
317
    /**
318
     * Adds an item to the index queue.
319
     *
320
     * Not meant for public use.
321
     *
322
     * @param string $itemType The item's type, usually a table name.
323
     * @param int|string $itemUid The item's uid, usually an integer uid, could be a
324
     *      different value for non-database-record types.
325
     * @param string $indexingConfiguration The item's indexing configuration to use.
326
     *      Optional, overwrites existing / determined configuration.
327
     * @param int $rootPageId
328
     * @return int
329
     * @throws DBALDriverException
330
     * @throws DBALException|\Doctrine\DBAL\DBALException
331
     */
332 80
    private function addNewItem(
333
        string $itemType,
334
        $itemUid,
335
        string $indexingConfiguration,
336
        int $rootPageId
337
    ): int {
338 80
        $additionalRecordFields = '';
339 80
        if ($itemType === 'pages') {
340 52
            $additionalRecordFields = ', doktype, uid';
341
        }
342
343 80
        $record = $this->getRecordCached($itemType, $itemUid, $additionalRecordFields);
344
345 80
        if (empty($record) || ($itemType === 'pages' && !$this->frontendEnvironment->isAllowedPageType($record, $indexingConfiguration))) {
346
            return 0;
347
        }
348
349 80
        $changedTime = $this->getItemChangedTime($itemType, $itemUid);
350
351 80
        return $this->queueItemRepository->add($itemType, $itemUid, $rootPageId, $changedTime, $indexingConfiguration);
0 ignored issues
show
Bug introduced by
It seems like $itemUid can also be of type string; however, parameter $itemUid of ApacheSolrForTypo3\Solr\...ueItemRepository::add() does only seem to accept integer, 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

351
        return $this->queueItemRepository->add($itemType, /** @scrutinizer ignore-type */ $itemUid, $rootPageId, $changedTime, $indexingConfiguration);
Loading history...
352
    }
353
354
    /**
355
     * Get record to be added in addNewItem
356
     *
357
     * @param string $itemType The item's type, usually a table name.
358
     * @param int|string $itemUid The item's uid, usually an integer uid, could be a
359
     *      different value for non-database-record types.
360
     * @param string $additionalRecordFields for sql-query
361
     *
362
     * @return array|null
363
     */
364 80
    protected function getRecordCached(string $itemType, $itemUid, string $additionalRecordFields): ?array
365
    {
366 80
        $cache = GeneralUtility::makeInstance(TwoLevelCache::class, /** @scrutinizer ignore-type */ 'runtime');
367 80
        $cacheId = md5('Queue' . ':' . 'getRecordCached' . ':' . $itemType . ':' . $itemUid . ':' . 'pid' . $additionalRecordFields);
368
369 80
        $record = $cache->get($cacheId);
370 80
        if (empty($record)) {
371 80
            $record = BackendUtility::getRecord($itemType, $itemUid, 'pid' . $additionalRecordFields);
372 80
            $cache->set($cacheId, $record);
373
        }
374
375 80
        return $record;
376
    }
377
378
    /**
379
     * Determines the time for when an item should be indexed. This timestamp
380
     * is then stored in the changed column in the Index Queue.
381
     *
382
     * The changed timestamp usually is now - time(). For records which are set
383
     * to published at a later time, this timestamp is the start time. So if a
384
     * future start time has been set, that will be used to delay indexing
385
     * of an item.
386
     *
387
     * @param string $itemType The item's table name.
388
     * @param int|string $itemUid The item's uid, usually an integer uid, could be a
389
     *      different value for non-database-record types.
390
     * @return int Timestamp of the item's changed time or future start time
391
     * @throws DBALDriverException
392
     * @throws DBALException|\Doctrine\DBAL\DBALException
393
     */
394 87
    protected function getItemChangedTime(string $itemType, $itemUid): int
395
    {
396 87
        $itemTypeHasStartTimeColumn = false;
397 87
        $changedTimeColumns = $GLOBALS['TCA'][$itemType]['ctrl']['tstamp'];
398 87
        $startTime = 0;
399 87
        $pageChangedTime = 0;
400
401 87
        if (!empty($GLOBALS['TCA'][$itemType]['ctrl']['enablecolumns']['starttime'])) {
402 87
            $itemTypeHasStartTimeColumn = true;
403 87
            $changedTimeColumns .= ', ' . $GLOBALS['TCA'][$itemType]['ctrl']['enablecolumns']['starttime'];
404
        }
405 87
        if ($itemType === 'pages') {
406
            // does not carry time information directly, but needed to support
407
            // canonical pages
408 59
            $changedTimeColumns .= ', content_from_pid';
409
        }
410
411 87
        $record = BackendUtility::getRecord($itemType, $itemUid, $changedTimeColumns);
412 87
        $itemChangedTime = $record[$GLOBALS['TCA'][$itemType]['ctrl']['tstamp']];
413
414 87
        if ($itemTypeHasStartTimeColumn) {
415 87
            $startTime = $record[$GLOBALS['TCA'][$itemType]['ctrl']['enablecolumns']['starttime']];
416
        }
417
418 87
        if ($itemType === 'pages') {
419 59
            $record['uid'] = $itemUid;
420
            // overrule the page's last changed time with the most recent
421
            //content element change
422 59
            $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

422
            $pageChangedTime = $this->getPageItemChangedTime(/** @scrutinizer ignore-type */ $record);
Loading history...
423
        }
424
425 87
        $localizationsChangedTime = $this->queueItemRepository->getLocalizableItemChangedTime($itemType, (int)$itemUid);
426
427
        // if start time exists and start time is higher than last changed timestamp
428
        // then set changed to the future start time to make the item
429
        // indexed at a later time
430 87
        return (int)max(
431 87
            $itemChangedTime,
432 87
            $pageChangedTime,
433 87
            $localizationsChangedTime,
434 87
            $startTime
435 87
        );
436
    }
437
438
    /**
439
     * Gets the most recent changed time of a page's content elements
440
     *
441
     * @param array $page Partial page record
442
     * @return int Timestamp of the most recent content element change
443
     * @throws DBALDriverException
444
     * @throws DBALException|\Doctrine\DBAL\DBALException
445
     */
446 59
    protected function getPageItemChangedTime(array $page): int
447
    {
448 59
        if (!empty($page['content_from_pid'])) {
449
            // canonical page, get the original page's last changed time
450
            return $this->queueItemRepository->getPageItemChangedTimeByPageUid((int)$page['content_from_pid']);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->queueItemR...ge['content_from_pid']) could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
451
        }
452 59
        return $this->queueItemRepository->getPageItemChangedTimeByPageUid((int)$page['uid']) ?? 0;
453
    }
454
455
    /**
456
     * Checks whether the Index Queue contains a specific item.
457
     *
458
     * @param string $itemType The item's type, usually a table name.
459
     * @param int|string $itemUid The item's uid, usually an integer uid, could be a
460
     *      different value for non-database-record types.
461
     * @return bool TRUE if the item is found in the queue, FALSE otherwise
462
     * @throws DBALDriverException
463
     * @throws DBALException|\Doctrine\DBAL\DBALException
464
     */
465 10
    public function containsItem(string $itemType, $itemUid): bool
466
    {
467 10
        return $this->queueItemRepository->containsItem($itemType, (int)$itemUid);
468
    }
469
470
    /**
471
     * Checks whether the Index Queue contains a specific item.
472
     *
473
     * @param string $itemType The item's type, usually a table name.
474
     * @param int|string $itemUid The item's uid, usually an integer uid, could be a
475
     *      different value for non-database-record types.
476
     * @param int $rootPageId
477
     * @return bool TRUE if the item is found in the queue, FALSE otherwise
478
     * @throws DBALDriverException
479
     * @throws DBALException|\Doctrine\DBAL\DBALException
480
     */
481 87
    public function containsItemWithRootPageId(string $itemType, $itemUid, int $rootPageId): bool
482
    {
483 87
        return $this->queueItemRepository->containsItemWithRootPageId($itemType, (int)$itemUid, $rootPageId);
484
    }
485
486
    /**
487
     * Checks whether the Index Queue contains a specific item that has been
488
     * marked as indexed.
489
     *
490
     * @param string $itemType The item's type, usually a table name.
491
     * @param int|string $itemUid The item's uid, usually an integer uid, could be a
492
     *      different value for non-database-record types.
493
     * @return bool TRUE if the item is found in the queue and marked as
494
     *      indexed, FALSE otherwise
495
     * @throws DBALDriverException
496
     * @throws DBALException|\Doctrine\DBAL\DBALException
497
     */
498 4
    public function containsIndexedItem(string $itemType, $itemUid): bool
499
    {
500 4
        return $this->queueItemRepository->containsIndexedItem($itemType, (int)$itemUid);
501
    }
502
503
    /**
504
     * Removes an item from the Index Queue.
505
     *
506
     * @param string $itemType The type of the item to remove, usually a table name.
507
     * @param int|string $itemUid The uid of the item to remove
508
     * @throws ConnectionException
509
     * @throws DBALException
510
     * @throws Throwable
511
     */
512 61
    public function deleteItem(string $itemType, $itemUid)
513
    {
514 61
        $this->queueItemRepository->deleteItem($itemType, (int)$itemUid);
515
    }
516
517
    /**
518
     * Removes all items of a certain type from the Index Queue.
519
     *
520
     * @param string $itemType The type of items to remove, usually a table name.
521
     * @throws ConnectionException
522
     * @throws DBALException
523
     * @throws Throwable
524
     */
525 1
    public function deleteItemsByType(string $itemType)
526
    {
527 1
        $this->queueItemRepository->deleteItemsByType($itemType);
528
    }
529
530
    /**
531
     * Removes all items of a certain site from the Index Queue. Accepts an
532
     * optional parameter to limit the deleted items by indexing configuration.
533
     *
534
     * @param Site $site The site to remove items for.
535
     * @param string $indexingConfigurationName Name of a specific indexing
536
     *      configuration
537
     * @throws ConnectionException
538
     * @throws \Doctrine\DBAL\DBALException
539
     * @throws Throwable
540
     */
541 6
    public function deleteItemsBySite(Site $site, string $indexingConfigurationName = '')
542
    {
543 6
        $this->queueItemRepository->deleteItemsBySite($site, $indexingConfigurationName);
544
    }
545
546
    /**
547
     * Removes all items from the Index Queue.
548
     */
549 1
    public function deleteAllItems()
550
    {
551 1
        $this->queueItemRepository->deleteAllItems();
552
    }
553
554
    /**
555
     * Gets a single Index Queue item by its uid.
556
     *
557
     * @param int $itemId Index Queue item uid
558
     * @return Item|null The request Index Queue item or NULL if no item with $itemId was found
559
     * @throws DBALDriverException
560
     * @throws DBALException|\Doctrine\DBAL\DBALException
561
     */
562 34
    public function getItem(int $itemId): ?Item
563
    {
564 34
        return $this->queueItemRepository->findItemByUid($itemId);
565
    }
566
567
    /**
568
     * Gets Index Queue items by type and uid.
569
     *
570
     * @param string $itemType item type, usually  the table name
571
     * @param int|string $itemUid item uid
572
     * @return Item[] An array of items matching $itemType and $itemUid
573
     * @throws ConnectionException
574
     * @throws DBALDriverException
575
     * @throws DBALException
576
     * @throws Throwable
577
     */
578 52
    public function getItems(string $itemType, $itemUid): array
579
    {
580 52
        return $this->queueItemRepository->findItemsByItemTypeAndItemUid($itemType, (int)$itemUid);
581
    }
582
583
    /**
584
     * Returns all items in the queue.
585
     *
586
     * @return Item[] An array of items
587
     * @throws ConnectionException
588
     * @throws DBALDriverException
589
     * @throws DBALException
590
     * @throws Throwable
591
     */
592 4
    public function getAllItems(): array
593
    {
594 4
        return $this->queueItemRepository->findAll();
595
    }
596
597
    /**
598
     * Returns the number of items for all queues.
599
     *
600
     * @return int
601
     * @throws DBALDriverException
602
     * @throws DBALException
603
     */
604 112
    public function getAllItemsCount(): int
605
    {
606 112
        return $this->queueItemRepository->count();
607
    }
608
609
    /**
610
     * Extracts the number of pending, indexed and erroneous items from the
611
     * Index Queue.
612
     *
613
     * @param Site $site
614
     * @param string $indexingConfigurationName
615
     *
616
     * @return QueueStatistic
617
     * @throws DBALDriverException
618
     * @throws DBALException
619
     */
620 5
    public function getStatisticsBySite(Site $site, string $indexingConfigurationName = ''): QueueStatistic
621
    {
622 5
        return $this->queueStatisticsRepository
623 5
            ->findOneByRootPidAndOptionalIndexingConfigurationName(
624 5
                $site->getRootPageId(),
625 5
                $indexingConfigurationName
626 5
            );
627
    }
628
629
    /**
630
     * Gets $limit number of items to index for a particular $site.
631
     *
632
     * @param Site $site TYPO3 site
633
     * @param int $limit Number of items to get from the queue
634
     * @return Item[] Items to index to the given solr server
635
     * @throws ConnectionException
636
     * @throws DBALDriverException
637
     * @throws DBALException
638
     * @throws Throwable
639
     */
640 3
    public function getItemsToIndex(Site $site, int $limit = 50): array
641
    {
642 3
        return $this->queueItemRepository->findItemsToIndex($site, $limit);
643
    }
644
645
    /**
646
     * Marks an item as failed and causes the indexer to skip the item in the
647
     * next run.
648
     *
649
     * @param int|Item $item Either the item's Index Queue uid or the complete item
650
     * @param string $errorMessage Error message
651
     * @throws DBALException|\Doctrine\DBAL\DBALException
652
     */
653 6
    public function markItemAsFailed($item, string $errorMessage = '')
654
    {
655 6
        $this->queueItemRepository->markItemAsFailed($item, $errorMessage);
656
    }
657
658
    /**
659
     * Sets the timestamp of when an item last has been indexed.
660
     *
661
     * @param Item $item
662
     * @throws DBALException|\Doctrine\DBAL\DBALException
663
     */
664 2
    public function updateIndexTimeByItem(Item $item)
665
    {
666 2
        $this->queueItemRepository->updateIndexTimeByItem($item);
667
    }
668
669
    /**
670
     * Sets the change timestamp of an item.
671
     *
672
     * @param Item $item
673
     * @param int $forcedChangeTime The change time for the item
674
     * @throws DBALException|\Doctrine\DBAL\DBALException
675
     */
676
    public function setForcedChangeTimeByItem(Item $item, int $forcedChangeTime = 0)
677
    {
678
        $this->queueItemRepository->updateChangedTimeByItem($item, $forcedChangeTime);
679
    }
680
}
681