Failed Conditions
Push — task/2976_TYPO3.11_compatibili... ( 14c9f4...2d3a36 )
by Rafael
23:17
created

IndexQueueModuleController::doIndexingRunAction()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 20
ccs 0
cts 12
cp 0
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Controller\Backend\Search;
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
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 2
    }
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
            $this->moduleTemplate->setContent($this->view->render());
69
            return $this->htmlResponse($this->moduleTemplate->renderContent());
70
        }
71
72
        $statistics = $this->indexQueue->getStatisticsBySite($this->selectedSite);
73
        $this->view->assign('indexQueueInitializationSelector', $this->getIndexQueueInitializationSelector());
74
        $this->view->assign('indexqueue_statistics', $statistics);
75
        $this->view->assign('indexqueue_errors', $this->indexQueue->getErrorsBySite($this->selectedSite));
76
        $this->moduleTemplate->setContent($this->view->render());
77
        return $this->htmlResponse($this->moduleTemplate->renderContent());
78
    }
79
80
    /**
81
     * Checks if selected site can be queued.
82
     *
83
     * @return bool
84
     */
85
    protected function canQueueSelectedSite(): bool
86
    {
87
        if ($this->selectedSite === null || empty($this->solrConnectionManager->getConnectionsBySite($this->selectedSite))) {
0 ignored issues
show
Bug introduced by
The method getConnectionsBySite() does not exist on null. ( Ignorable by Annotation )

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

87
        if ($this->selectedSite === null || empty($this->solrConnectionManager->/** @scrutinizer ignore-call */ getConnectionsBySite($this->selectedSite))) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
            return false;
89
        }
90
        $enabledIndexQueueConfigurationNames = $this->selectedSite->getSolrConfiguration()->getEnabledIndexQueueConfigurationNames();
91
        if (empty($enabledIndexQueueConfigurationNames)) {
92
            return false;
93
        }
94
        return true;
95
    }
96
97
    /**
98
     * Renders a field to select which indexing configurations to initialize.
99
     *
100
     * Uses TCEforms.
101
     *
102
     * @return string Markup for the select field
103
     * @throws BackendFormException
104
     */
105
    protected function getIndexQueueInitializationSelector(): string
106
    {
107
        $selector = GeneralUtility::makeInstance(IndexingConfigurationSelectorField::class, /** @scrutinizer ignore-type */ $this->selectedSite);
108
        $selector->setFormElementName('tx_solr-index-queue-initialization');
109
110
        return $selector->render();
111
    }
112
113
    /**
114
     * Initializes the Index Queue for selected indexing configurations
115
     *
116
     * @return ResponseInterface
117
     */
118
    public function initializeIndexQueueAction(): ResponseInterface
119
    {
120
        $initializedIndexingConfigurations = [];
121
122
        $indexingConfigurationsToInitialize = GeneralUtility::_POST('tx_solr-index-queue-initialization');
123
        if ((!empty($indexingConfigurationsToInitialize)) && (is_array($indexingConfigurationsToInitialize))) {
0 ignored issues
show
introduced by
The condition is_array($indexingConfigurationsToInitialize) is always false.
Loading history...
124
            // initialize selected indexing configuration
125
            $initializedIndexingConfigurations = $this->indexQueue->getInitializationService()->initializeBySiteAndIndexConfigurations($this->selectedSite, $indexingConfigurationsToInitialize);
126
        } else {
127
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.no_selection';
128
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.not_initialized.title';
129
            $this->addFlashMessage(
130
                LocalizationUtility::translate($messageLabel, 'Solr'),
131
                LocalizationUtility::translate($titleLabel, 'Solr'),
132
                FlashMessage::WARNING
133
            );
134
        }
135
        $messagesForConfigurations = [];
136
        foreach (array_keys($initializedIndexingConfigurations) as $indexingConfigurationName) {
137
            $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

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