Passed
Push — master ( e55157...ed38f6 )
by Timo
27:41
created

Queue::resetErrorByItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\Site;
34
use ApacheSolrForTypo3\Solr\System\Cache\TwoLevelCache;
35
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
36
use ApacheSolrForTypo3\Solr\Util;
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
     * Queue constructor.
80
     * @param RootPageResolver|null $rootPageResolver
81
     * @param ConfigurationAwareRecordService|null $recordService
82
     * @param QueueItemRepository|null $queueItemRepository
83
     * @param QueueStatisticsRepository|null $queueStatisticsRepository
84
     * @param QueueInitializationService|null $queueInitializationService
85
     */
86 110
    public function __construct(RootPageResolver $rootPageResolver = null, ConfigurationAwareRecordService $recordService = null, QueueItemRepository $queueItemRepository = null, QueueStatisticsRepository $queueStatisticsRepository = null, QueueInitializationService $queueInitializationService = null)
87
    {
88 110
        $this->logger = GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
89 110
        $this->rootPageResolver = $rootPageResolver ?? GeneralUtility::makeInstance(RootPageResolver::class);
90 110
        $this->recordService = $recordService ?? GeneralUtility::makeInstance(ConfigurationAwareRecordService::class);
91 110
        $this->queueItemRepository = $queueItemRepository ?? GeneralUtility::makeInstance(QueueItemRepository::class);
92 110
        $this->queueStatisticsRepository = $queueStatisticsRepository ??  GeneralUtility::makeInstance(QueueStatisticsRepository::class);
93 110
        $this->queueInitializationService = $queueInitializationService ?? GeneralUtility::makeInstance(QueueInitializationService::class, /** @scrutinizer ignore-type */ $this);
94 110
    }
95
96
    // FIXME some of the methods should be renamed to plural forms
97
    // FIXME singular form methods should deal with exactly one item only
98
99
    /**
100
     * Returns the timestamp of the last indexing run.
101
     *
102
     * @param int $rootPageId The root page uid for which to get
103
     *      the last indexed item id
104
     * @return int Timestamp of last index run.
105
     */
106 2
    public function getLastIndexTime($rootPageId)
107
    {
108 2
        $lastIndexTime = 0;
109
110 2
        $lastIndexedRow = $this->queueItemRepository->findLastIndexedRow($rootPageId);
111
112 2
        if ($lastIndexedRow[0]['indexed']) {
113 1
            $lastIndexTime = $lastIndexedRow[0]['indexed'];
114
        }
115
116 2
        return $lastIndexTime;
117
    }
118
119
    /**
120
     * Returns the uid of the last indexed item in the queue
121
     *
122
     * @param int $rootPageId The root page uid for which to get
123
     *      the last indexed item id
124
     * @return int The last indexed item's ID.
125
     */
126 3
    public function getLastIndexedItemId($rootPageId)
127
    {
128 3
        $lastIndexedItemId = 0;
129
130 3
        $lastIndexedItemRow = $this->queueItemRepository->findLastIndexedRow($rootPageId);
131 3
        if ($lastIndexedItemRow[0]['uid']) {
132 2
            $lastIndexedItemId = $lastIndexedItemRow[0]['uid'];
133
        }
134
135 3
        return $lastIndexedItemId;
136
    }
137
138
    /**
139
     * @return QueueInitializationService
140
     */
141 6
    public function getInitializationService()
142
    {
143 6
        return $this->queueInitializationService;
144
    }
145
146
    /**
147
     * Marks an item as needing (re)indexing.
148
     *
149
     * Like with Solr itself, there's no add method, just a simple update method
150
     * that handles the adds, too.
151
     *
152
     * The method creates or updates the index queue items for all related rootPageIds.
153
     *
154
     * @param string $itemType The item's type, usually a table name.
155
     * @param string $itemUid The item's uid, usually an integer uid, could be a different value for non-database-record types.
156
     * @param int $forcedChangeTime The change time for the item if set, otherwise value from getItemChangedTime() is used.
157
     * @return int Number of updated/created items
158
     */
159 62
    public function updateItem($itemType, $itemUid, $forcedChangeTime = 0)
160
    {
161 62
        $updateCount = $this->updateOrAddItemForAllRelatedRootPages($itemType, $itemUid, $forcedChangeTime);
162 61
        $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

162
        $updateCount = $this->postProcessIndexQueueUpdateItem($itemType, /** @scrutinizer ignore-type */ $itemUid, $updateCount, $forcedChangeTime);
Loading history...
163
164 61
        return $updateCount;
165
    }
166
167
    /**
168
     * Updates or add's the item for all relevant root pages.
169
     *
170
     * @param string $itemType The item's type, usually a table name.
171
     * @param string $itemUid The item's uid, usually an integer uid, could be a different value for non-database-record types.
172
     * @param int $forcedChangeTime The change time for the item if set, otherwise value from getItemChangedTime() is used.
173
     * @return int
174
     */
175 60
    protected function updateOrAddItemForAllRelatedRootPages($itemType, $itemUid, $forcedChangeTime): int
176
    {
177 60
        $updateCount = 0;
178 60
        $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

178
        $rootPageIds = $this->rootPageResolver->getResponsibleRootPageIds($itemType, /** @scrutinizer ignore-type */ $itemUid);
Loading history...
179 59
        foreach ($rootPageIds as $rootPageId) {
180 59
            $skipInvalidRootPage = $rootPageId === 0;
181 59
            if ($skipInvalidRootPage) {
182
                continue;
183
            }
184
185 59
            $solrConfiguration = Util::getSolrConfigurationFromPageId($rootPageId);
186 59
            $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

186
            $indexingConfiguration = $this->recordService->getIndexingConfigurationName($itemType, /** @scrutinizer ignore-type */ $itemUid, $solrConfiguration);
Loading history...
187 59
            $itemInQueueForRootPage = $this->containsItemWithRootPageId($itemType, $itemUid, $rootPageId);
188 59
            if ($itemInQueueForRootPage) {
189
                // update changed time if that item is in the queue already
190 10
                $changedTime = ($forcedChangeTime > 0) ? $forcedChangeTime : $this->getItemChangedTime($itemType, $itemUid);
191 10
                $updatedRows = $this->queueItemRepository->updateExistingItemByItemTypeAndItemUidAndRootPageId($itemType, $itemUid, $rootPageId, $changedTime, $indexingConfiguration);
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

191
                $updatedRows = $this->queueItemRepository->updateExistingItemByItemTypeAndItemUidAndRootPageId($itemType, /** @scrutinizer ignore-type */ $itemUid, $rootPageId, $changedTime, $indexingConfiguration);
Loading history...
192
            } else {
193
                // add the item since it's not in the queue yet
194 53
                $updatedRows = $this->addNewItem($itemType, $itemUid, $indexingConfiguration, $rootPageId);
195
            }
196
197 59
            $updateCount += $updatedRows;
198
        }
199
200 59
        return $updateCount;
201
    }
202
203
    /**
204
     * Executes the updateItem post processing hook.
205
     *
206
     * @param string $itemType
207
     * @param int $itemUid
208
     * @param int $updateCount
209
     * @param int $forcedChangeTime
210
     * @return int
211
     */
212 61
    protected function postProcessIndexQueueUpdateItem($itemType, $itemUid, $updateCount, $forcedChangeTime = 0)
213
    {
214 61
        if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueUpdateItem'])) {
215 60
            return $updateCount;
216
        }
217
218 1
        foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['postProcessIndexQueueUpdateItem'] as $classReference) {
219 1
            $updateHandler = $this->getHookImplementation($classReference);
220 1
            $updateCount = $updateHandler->postProcessIndexQueueUpdateItem($itemType, $itemUid, $updateCount, $forcedChangeTime);
221
        }
222
223 1
        return $updateCount;
224
    }
225
226
    /**
227
     * @param string $classReference
228
     * @return object
229
     */
230
    protected function getHookImplementation($classReference)
231
    {
232
        return GeneralUtility::makeInstance($classReference);
233
    }
234
235
    /**
236
     * Finds indexing errors for the current site
237
     *
238
     * @param Site $site
239
     * @return array Error items for the current site's Index Queue
240
     */
241 3
    public function getErrorsBySite(Site $site)
242
    {
243 3
        return $this->queueItemRepository->findErrorsBySite($site);
244
    }
245
246
    /**
247
     * Resets all the errors for all index queue items.
248
     *
249
     * @return mixed
250
     */
251 1
    public function resetAllErrors()
252
    {
253 1
        return $this->queueItemRepository->flushAllErrors();
254
    }
255
256
    /**
257
     * Resets the errors in the index queue for a specific site
258
     *
259
     * @param Site $site
260
     * @return mixed
261
     */
262 1
    public function resetErrorsBySite(Site $site)
263
    {
264 1
        return $this->queueItemRepository->flushErrorsBySite($site);
265
    }
266
267
    /**
268
     * Resets the error in the index queue for a specific item
269
     *
270
     * @param Item $item
271
     * @return mixed
272
     */
273 1
    public function resetErrorByItem(Item $item)
274
    {
275 1
        return $this->queueItemRepository->flushErrorByItem($item);
276
    }
277
278
    /**
279
     * Adds an item to the index queue.
280
     *
281
     * Not meant for public use.
282
     *
283
     * @param string $itemType The item's type, usually a table name.
284
     * @param string $itemUid The item's uid, usually an integer uid, could be a
285
     *      different value for non-database-record types.
286
     * @param string $indexingConfiguration The item's indexing configuration to use.
287
     *      Optional, overwrites existing / determined configuration.
288
     * @param $rootPageId
289
     * @return int
290
     */
291 53
    private function addNewItem($itemType, $itemUid, $indexingConfiguration, $rootPageId)
292
    {
293 53
        $additionalRecordFields = '';
294 53
        if ($itemType === 'pages') {
295 30
            $additionalRecordFields = ', doktype, uid';
296
        }
297
298 53
        $record = $this->getRecordCached($itemType, $itemUid, $additionalRecordFields);
299
300 53
        if (empty($record) || ($itemType === 'pages' && !Util::isAllowedPageType($record, $indexingConfiguration))) {
301 1
            return 0;
302
        }
303
304 52
        $changedTime = $this->getItemChangedTime($itemType, $itemUid);
305
306 52
        return $this->queueItemRepository->add($itemType, $itemUid, $rootPageId, $changedTime, $indexingConfiguration);
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

306
        return $this->queueItemRepository->add($itemType, /** @scrutinizer ignore-type */ $itemUid, $rootPageId, $changedTime, $indexingConfiguration);
Loading history...
307
    }
308
309
    /**
310
     * Get record to be added in addNewItem
311
     *
312
     * @param string $itemType The item's type, usually a table name.
313
     * @param string $itemUid The item's uid, usually an integer uid, could be a
314
     *      different value for non-database-record types.
315
     * @param string $additionalRecordFields for sql-query
316
     *
317
     * @return array|NULL
318
     */
319 53
    protected function getRecordCached($itemType, $itemUid, $additionalRecordFields)
320
    {
321 53
        $cache = GeneralUtility::makeInstance(TwoLevelCache::class, /** @scrutinizer ignore-type */ 'cache_runtime');
322 53
        $cacheId = md5('Queue' . ':' . 'getRecordCached' . ':' . $itemType . ':' . $itemUid . ':' . 'pid' . $additionalRecordFields);
323
324 53
        $record = $cache->get($cacheId);
325 53
        if (empty($record)) {
326 53
            $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

326
            $record = BackendUtility::getRecord($itemType, /** @scrutinizer ignore-type */ $itemUid, 'pid' . $additionalRecordFields);
Loading history...
327 53
            $cache->set($cacheId, $record);
328
        }
329
330 53
        return $record;
331
    }
332
333
    /**
334
     * Determines the time for when an item should be indexed. This timestamp
335
     * is then stored in the changed column in the Index Queue.
336
     *
337
     * The changed timestamp usually is now - time(). For records which are set
338
     * to published at a later time, this timestamp is the start time. So if a
339
     * future start time has been set, that will be used to delay indexing
340
     * of an item.
341
     *
342
     * @param string $itemType The item's table name.
343
     * @param string $itemUid The item's uid, usually an integer uid, could be a
344
     *      different value for non-database-record types.
345
     * @return int Timestamp of the item's changed time or future start time
346
     */
347 58
    protected function getItemChangedTime($itemType, $itemUid)
348
    {
349 58
        $itemTypeHasStartTimeColumn = false;
350 58
        $changedTimeColumns = $GLOBALS['TCA'][$itemType]['ctrl']['tstamp'];
351 58
        $startTime = 0;
352 58
        $pageChangedTime = 0;
353
354 58
        if (!empty($GLOBALS['TCA'][$itemType]['ctrl']['enablecolumns']['starttime'])) {
355 58
            $itemTypeHasStartTimeColumn = true;
356 58
            $changedTimeColumns .= ', ' . $GLOBALS['TCA'][$itemType]['ctrl']['enablecolumns']['starttime'];
357
        }
358 58
        if ($itemType === 'pages') {
359
            // does not carry time information directly, but needed to support
360
            // canonical pages
361 35
            $changedTimeColumns .= ', content_from_pid';
362
        }
363
364 58
        $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

364
        $record = BackendUtility::getRecord($itemType, /** @scrutinizer ignore-type */ $itemUid, $changedTimeColumns);
Loading history...
365 58
        $itemChangedTime = $record[$GLOBALS['TCA'][$itemType]['ctrl']['tstamp']];
366
367 58
        if ($itemTypeHasStartTimeColumn) {
368 58
            $startTime = $record[$GLOBALS['TCA'][$itemType]['ctrl']['enablecolumns']['starttime']];
369
        }
370
371 58
        if ($itemType === 'pages') {
372 35
            $record['uid'] = $itemUid;
373
            // overrule the page's last changed time with the most recent
374
            //content element change
375 35
            $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

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