Passed
Push — release-11.5.x ( 002661...eb87e8 )
by Rafael
41:47
created

IndexQueueModuleController   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 237
Duplicated Lines 0 %

Test Coverage

Coverage 11.7%

Importance

Changes 0
Metric Value
wmc 24
eloc 95
dl 0
loc 237
ccs 11
cts 94
cp 0.117
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setIndexQueue() 0 3 1
A initializeAction() 0 4 1
A getIndexQueueInitializationSelector() 0 6 1
A canQueueSelectedSite() 0 10 4
A indexAction() 0 12 2
B initializeIndexQueueAction() 0 52 6
A doIndexingRunAction() 0 20 2
A resetLogErrorsAction() 0 14 2
A requeueDocumentAction() 0 14 2
A showErrorAction() 0 14 2
A addIndexQueueFlashMessage() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr\Controller\Backend\Search;
17
18
use ApacheSolrForTypo3\Solr\Backend\IndexingConfigurationSelectorField;
19
use ApacheSolrForTypo3\Solr\Domain\Index\IndexService;
20
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
21
use Psr\Http\Message\ResponseInterface;
22
use TYPO3\CMS\Backend\Form\Exception as BackendFormException;
23
use TYPO3\CMS\Core\Http\RedirectResponse;
24
use TYPO3\CMS\Core\Messaging\AbstractMessage;
25
use TYPO3\CMS\Core\Messaging\FlashMessage;
26
use TYPO3\CMS\Core\Utility\GeneralUtility;
27
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
28
29
/**
30
 * Index Queue Module
31
 *
32
 * @author Ingo Renner <[email protected]>
33
 */
34
class IndexQueueModuleController extends AbstractModuleController
35
{
36
    /**
37
     * @var Queue
38
     */
39
    protected Queue $indexQueue;
40
41
    /**
42
     * Initializes the controller before invoking an action method.
43
     */
44
    protected function initializeAction()
45
    {
46
        parent::initializeAction();
47
        $this->indexQueue = GeneralUtility::makeInstance(Queue::class);
48
    }
49
50
    /**
51
     * @param Queue $indexQueue
52
     */
53 2
    public function setIndexQueue(Queue $indexQueue)
54
    {
55 2
        $this->indexQueue = $indexQueue;
56
    }
57
58
    /**
59
     * Lists the available indexing configurations
60
     *
61
     * @return ResponseInterface
62
     * @throws BackendFormException
63
     */
64
    public function indexAction(): ResponseInterface
65
    {
66
        if (!$this->canQueueSelectedSite()) {
67
            $this->view->assign('can_not_proceed', true);
68
            return $this->getModuleTemplateResponse();
69
        }
70
71
        $statistics = $this->indexQueue->getStatisticsBySite($this->selectedSite);
72
        $this->view->assign('indexQueueInitializationSelector', $this->getIndexQueueInitializationSelector());
73
        $this->view->assign('indexqueue_statistics', $statistics);
74
        $this->view->assign('indexqueue_errors', $this->indexQueue->getErrorsBySite($this->selectedSite));
75
        return $this->getModuleTemplateResponse();
76
    }
77
78
    /**
79
     * Checks if selected site can be queued.
80
     *
81
     * @return bool
82
     */
83
    protected function canQueueSelectedSite(): bool
84
    {
85
        if ($this->selectedSite === null || empty($this->solrConnectionManager->getConnectionsBySite($this->selectedSite))) {
86
            return false;
87
        }
88
        $enabledIndexQueueConfigurationNames = $this->selectedSite->getSolrConfiguration()->getEnabledIndexQueueConfigurationNames();
89
        if (empty($enabledIndexQueueConfigurationNames)) {
90
            return false;
91
        }
92
        return true;
93
    }
94
95
    /**
96
     * Renders a field to select which indexing configurations to initialize.
97
     *
98
     * Uses TCEforms.
99
     *
100
     * @return string Markup for the select field
101
     * @throws BackendFormException
102
     */
103
    protected function getIndexQueueInitializationSelector(): string
104
    {
105
        $selector = GeneralUtility::makeInstance(IndexingConfigurationSelectorField::class, /** @scrutinizer ignore-type */ $this->selectedSite);
106
        $selector->setFormElementName('tx_solr-index-queue-initialization');
107
108
        return $selector->render();
109
    }
110
111
    /**
112
     * Initializes the Index Queue for selected indexing configurations
113
     *
114
     * @return ResponseInterface
115
     */
116
    public function initializeIndexQueueAction(): ResponseInterface
117
    {
118
        $initializedIndexingConfigurations = [];
119
120
        $indexingConfigurationsToInitialize = GeneralUtility::_POST('tx_solr-index-queue-initialization');
121
        if ((!empty($indexingConfigurationsToInitialize)) && (is_array($indexingConfigurationsToInitialize))) {
0 ignored issues
show
introduced by
The condition is_array($indexingConfigurationsToInitialize) is always false.
Loading history...
122
            // initialize selected indexing configuration
123
            try {
124
                $initializedIndexingConfigurations = $this->indexQueue->getInitializationService()->initializeBySiteAndIndexConfigurations($this->selectedSite, $indexingConfigurationsToInitialize);
125
            } catch (\Throwable $e) {
126
                $this->addFlashMessage(
127
                    sprintf(
128
                        LocalizationUtility::translate(
129
                            'solr.backend.index_queue_module.flashmessage.initialize_failure',
130
                            'Solr'
131
                        ),
132
                        $e->getMessage(),
133
                        $e->getCode()
134
                    ),
135
                    LocalizationUtility::translate(
136
                        'solr.backend.index_queue_module.flashmessage.initialize_failure.title',
137
                        'Solr'
138
                    ),
139
                    FlashMessage::ERROR
140
                );
141
            }
142
        } else {
143
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.no_selection';
144
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.not_initialized.title';
145
            $this->addFlashMessage(
146
                LocalizationUtility::translate($messageLabel, 'Solr'),
147
                LocalizationUtility::translate($titleLabel, 'Solr'),
148
                FlashMessage::WARNING
149
            );
150
        }
151
        $messagesForConfigurations = [];
152
        foreach (array_keys($initializedIndexingConfigurations) as $indexingConfigurationName) {
153
            $itemCount = $this->indexQueue->getStatisticsBySite($this->selectedSite, $indexingConfigurationName)->getTotalCount();
0 ignored issues
show
Bug introduced by
It seems like $this->selectedSite can also be of type null; however, parameter $site of ApacheSolrForTypo3\Solr\...::getStatisticsBySite() does only seem to accept ApacheSolrForTypo3\Solr\Domain\Site\Site, 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

153
            $itemCount = $this->indexQueue->getStatisticsBySite(/** @scrutinizer ignore-type */ $this->selectedSite, $indexingConfigurationName)->getTotalCount();
Loading history...
154
            $messagesForConfigurations[] = $indexingConfigurationName . ' (' . $itemCount . ' records)';
155
        }
156
157
        if (!empty($initializedIndexingConfigurations)) {
158
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.success';
159
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.initialize.title';
160
            $this->addFlashMessage(
161
                LocalizationUtility::translate($messageLabel, 'Solr', [implode(', ', $messagesForConfigurations)]),
162
                LocalizationUtility::translate($titleLabel, 'Solr'),
163
                FlashMessage::OK
164
            );
165
        }
166
167
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
168
    }
169
170
    /**
171
     * Removes all errors in the index queue list. So that the items can be indexed again.
172
     *
173
     * @return ResponseInterface
174
     */
175
    public function resetLogErrorsAction(): ResponseInterface
176
    {
177
        $resetResult = $this->indexQueue->resetAllErrors();
178
179
        $label = 'solr.backend.index_queue_module.flashmessage.success.reset_errors';
180
        $severity = FlashMessage::OK;
181
        if (!$resetResult) {
182
            $label = 'solr.backend.index_queue_module.flashmessage.error.reset_errors';
183
            $severity = FlashMessage::ERROR;
184
        }
185
186
        $this->addIndexQueueFlashMessage($label, $severity);
187
188
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
189
    }
190
191
    /**
192
     * ReQueues a single item in the indexQueue.
193
     *
194
     * @param string $type
195
     * @param int $uid
196
     * @return ResponseInterface
197
     */
198 2
    public function requeueDocumentAction(string $type, int $uid): ResponseInterface
199
    {
200 2
        $label = 'solr.backend.index_queue_module.flashmessage.error.single_item_not_requeued';
201 2
        $severity = AbstractMessage::ERROR;
202
203 2
        $updateCount = $this->indexQueue->updateItem($type, $uid, time());
204 2
        if ($updateCount > 0) {
205 1
            $label = 'solr.backend.index_queue_module.flashmessage.success.single_item_was_requeued';
206 1
            $severity = AbstractMessage::OK;
207
        }
208
209 2
        $this->addIndexQueueFlashMessage($label, $severity);
210
211 2
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
212
    }
213
214
    /**
215
     * Shows the error message for one queue item.
216
     *
217
     * @param int $indexQueueItemId
218
     * @return ResponseInterface
219
     */
220
    public function showErrorAction(int $indexQueueItemId): ResponseInterface
221
    {
222
        $item = $this->indexQueue->getItem($indexQueueItemId);
223
        if ($item === null) {
224
            // add a flash message and quit
225
            $label = 'solr.backend.index_queue_module.flashmessage.error.no_queue_item_for_queue_error';
226
            $severity = FlashMessage::ERROR;
227
            $this->addIndexQueueFlashMessage($label, $severity);
228
229
            return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
230
        }
231
232
        $this->view->assign('indexQueueItem', $item);
233
        return $this->getModuleTemplateResponse();
234
    }
235
236
    /**
237
     * Indexes a few documents with the index service.
238
     * @return ResponseInterface
239
     */
240
    public function doIndexingRunAction(): ResponseInterface
241
    {
242
        /* @var IndexService $indexService */
243
        $indexService = GeneralUtility::makeInstance(IndexService::class, /** @scrutinizer ignore-type */ $this->selectedSite);
244
        $indexWithoutErrors = $indexService->indexItems(1);
245
246
        $label = 'solr.backend.index_queue_module.flashmessage.success.index_manual';
247
        $severity = FlashMessage::OK;
248
        if (!$indexWithoutErrors) {
249
            $label = 'solr.backend.index_queue_module.flashmessage.error.index_manual';
250
            $severity = FlashMessage::ERROR;
251
        }
252
253
        $this->addFlashMessage(
254
            LocalizationUtility::translate($label, 'Solr'),
255
            LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.index_manual', 'Solr'),
256
            $severity
257
        );
258
259
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
260
    }
261
262
    /**
263
     * Adds a flash message for the index queue module.
264
     *
265
     * @param string $label
266
     * @param int $severity
267
     */
268
    protected function addIndexQueueFlashMessage(string $label, int $severity)
269
    {
270
        $this->addFlashMessage(LocalizationUtility::translate($label, 'Solr'), LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.title', 'Solr'), $severity);
271
    }
272
}
273