Passed
Pull Request — release-11.2.x (#3604)
by Markus
32:12 queued 27:55
created

IndexQueueModuleController   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 233
Duplicated Lines 0 %

Test Coverage

Coverage 13.13%

Importance

Changes 0
Metric Value
wmc 24
eloc 83
c 0
b 0
f 0
dl 0
loc 233
ccs 13
cts 99
cp 0.1313
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeView() 0 3 1
A setIndexQueue() 0 3 1
A initializeAction() 0 4 1
A addIndexQueueFlashMessage() 0 3 1
A doIndexingRunAction() 0 20 2
A initializeIndexQueueAction() 0 34 5
A requeueDocumentAction() 0 14 2
A resetLogErrorsAction() 0 14 2
A showErrorAction() 0 13 2
A getIndexQueueInitializationSelector() 0 6 1
A canQueueSelectedSite() 0 10 4
A indexAction() 0 11 2
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Controller\Backend\Search;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2013-2015 Ingo Renner <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Backend\IndexingConfigurationSelectorField;
29
use ApacheSolrForTypo3\Solr\Domain\Index\IndexService;
30
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
31
use TYPO3\CMS\Backend\View\BackendTemplateView;
32
use TYPO3\CMS\Core\Messaging\FlashMessage;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
35
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
36
37
/**
38
 * Index Queue Module
39
 *
40
 * @author Ingo Renner <[email protected]>
41
 * @property BackendTemplateView $view
42
 */
43
class IndexQueueModuleController extends AbstractModuleController
0 ignored issues
show
Bug introduced by
The type ApacheSolrForTypo3\Solr\...bstractModuleController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
44
{
45
46
    /**
47
     * Module name, used to identify a module f.e. in URL parameters.
48
     *
49
     * @var string
50
     */
51
    protected $moduleName = 'IndexQueue';
52
53
    /**
54
     * Module title, shows up in the module menu.
55
     *
56
     * @var string
57
     */
58
    protected $moduleTitle = 'Index Queue';
59
60
    /**
61
     * @var Queue
62
     */
63
    protected $indexQueue;
64
65
    /**
66
     * Initializes the controller before invoking an action method.
67
     */
68
    protected function initializeAction()
69
    {
70
        parent::initializeAction();
71
        $this->indexQueue = GeneralUtility::makeInstance(Queue::class);
72
    }
73
74
    /**
75
     * @param Queue $indexQueue
76
     */
77 2
    public function setIndexQueue(Queue $indexQueue)
78
    {
79 2
        $this->indexQueue = $indexQueue;
80 2
    }
81
82
    /**
83
     * Set up the doc header properly here
84
     *
85
     * @param ViewInterface $view
86
     */
87
    protected function initializeView(ViewInterface $view)
88
    {
89
        parent::initializeView($view);
90
    }
91
92
    /**
93
     * Lists the available indexing configurations
94
     */
95
    public function indexAction()
96
    {
97
        if (!$this->canQueueSelectedSite()) {
98
            $this->view->assign('can_not_proceed', true);
99
            return;
100
        }
101
102
        $statistics = $this->indexQueue->getStatisticsBySite($this->selectedSite);
103
        $this->view->assign('indexQueueInitializationSelector', $this->getIndexQueueInitializationSelector());
104
        $this->view->assign('indexqueue_statistics', $statistics);
105
        $this->view->assign('indexqueue_errors', $this->indexQueue->getErrorsBySite($this->selectedSite));
106
    }
107
108
    /**
109
     * Checks if selected site can be queued.
110
     *
111
     * @return bool
112
     */
113
    protected function canQueueSelectedSite()
114
    {
115
        if ($this->selectedSite === null || empty($this->solrConnectionManager->getConnectionsBySite($this->selectedSite))) {
116
            return false;
117
        }
118
        $enabledIndexQueueConfigurationNames = $this->selectedSite->getSolrConfiguration()->getEnabledIndexQueueConfigurationNames();
119
        if (empty($enabledIndexQueueConfigurationNames)) {
120
            return false;
121
        }
122
        return true;
123
    }
124
125
    /**
126
     * Renders a field to select which indexing configurations to initialize.
127
     *
128
     * Uses TCEforms.
129
     *
130
     * @return string Markup for the select field
131
     */
132
    protected function getIndexQueueInitializationSelector()
133
    {
134
        $selector = GeneralUtility::makeInstance(IndexingConfigurationSelectorField::class, /** @scrutinizer ignore-type */ $this->selectedSite);
135
        $selector->setFormElementName('tx_solr-index-queue-initialization');
136
137
        return $selector->render();
138
    }
139
140
    /**
141
     * Initializes the Index Queue for selected indexing configurations
142
     */
143
    public function initializeIndexQueueAction()
144
    {
145
        $initializedIndexingConfigurations = [];
146
147
        $indexingConfigurationsToInitialize = GeneralUtility::_POST('tx_solr-index-queue-initialization');
148
        if ((!empty($indexingConfigurationsToInitialize)) && (is_array($indexingConfigurationsToInitialize))) {
0 ignored issues
show
introduced by
The condition is_array($indexingConfigurationsToInitialize) is always false.
Loading history...
149
            // initialize selected indexing configuration
150
            $initializedIndexingConfigurations = $this->indexQueue->getInitializationService()->initializeBySiteAndIndexConfigurations($this->selectedSite, $indexingConfigurationsToInitialize);
151
        } else {
152
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.no_selection';
153
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.not_initialized.title';
154
            $this->addFlashMessage(
155
                LocalizationUtility::translate($messageLabel, 'Solr'),
156
                LocalizationUtility::translate($titleLabel, 'Solr'),
157
                FlashMessage::WARNING
158
            );
159
        }
160
        $messagesForConfigurations = [];
161
        foreach (array_keys($initializedIndexingConfigurations) as $indexingConfigurationName) {
162
            $itemCount = $this->indexQueue->getStatisticsBySite($this->selectedSite, $indexingConfigurationName)->getTotalCount();
163
            $messagesForConfigurations[] = $indexingConfigurationName . ' (' . $itemCount . ' records)';
164
        }
165
166
        if (!empty($initializedIndexingConfigurations)) {
167
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.success';
168
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.initialize.title';
169
            $this->addFlashMessage(
170
                LocalizationUtility::translate($messageLabel, 'Solr', [implode(', ', $messagesForConfigurations)]),
171
                LocalizationUtility::translate($titleLabel, 'Solr'),
172
                FlashMessage::OK
173
            );
174
        }
175
176
        $this->redirect('index');
177
    }
178
179
    /**
180
     * Removes all errors in the index queue list. So that the items can be indexed again.
181
     */
182
    public function resetLogErrorsAction()
183
    {
184
        $resetResult = $this->indexQueue->resetAllErrors();
185
186
        $label = 'solr.backend.index_queue_module.flashmessage.success.reset_errors';
187
        $severity = FlashMessage::OK;
188
        if (!$resetResult) {
189
            $label = 'solr.backend.index_queue_module.flashmessage.error.reset_errors';
190
            $severity = FlashMessage::ERROR;
191
        }
192
193
        $this->addIndexQueueFlashMessage($label, $severity);
194
195
        $this->redirect('index');
196
    }
197
198
    /**
199
     * ReQueues a single item in the indexQueue.
200
     *
201
     * @param string $type
202
     * @param int $uid
203
     */
204 2
    public function requeueDocumentAction(string $type, int $uid)
205
    {
206 2
        $label = 'solr.backend.index_queue_module.flashmessage.error.single_item_not_requeued';
207 2
        $severity = FlashMessage::ERROR;
208
209 2
        $updateCount = $this->indexQueue->updateItem($type, $uid, time());
210 2
        if ($updateCount > 0) {
211 1
            $label = 'solr.backend.index_queue_module.flashmessage.success.single_item_was_requeued';
212 1
            $severity = FlashMessage::OK;
213
        }
214
215 2
        $this->addIndexQueueFlashMessage($label, $severity);
216
217 2
        $this->redirect('index');
218 2
    }
219
220
    /**
221
     * Shows the error message for one queue item.
222
     *
223
     * @param int $indexQueueItemId
224
     */
225
    public function showErrorAction(int $indexQueueItemId)
226
    {
227
        if (is_null($indexQueueItemId)) {
0 ignored issues
show
introduced by
The condition is_null($indexQueueItemId) is always false.
Loading history...
228
            // add a flash message and quit
229
            $label = 'solr.backend.index_queue_module.flashmessage.error.no_queue_item_for_queue_error';
230
            $severity = FlashMessage::ERROR;
231
            $this->addIndexQueueFlashMessage($label, $severity);
232
233
            return;
234
        }
235
236
        $item = $this->indexQueue->getItem($indexQueueItemId);
237
        $this->view->assign('indexQueueItem', $item);
238
    }
239
240
    /**
241
     * Indexes a few documents with the index service.
242
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
243
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
244
     */
245
    public function doIndexingRunAction()
246
    {
247
        /** @var $indexService \ApacheSolrForTypo3\Solr\Domain\Index\IndexService */
248
        $indexService = GeneralUtility::makeInstance(IndexService::class, /** @scrutinizer ignore-type */ $this->selectedSite);
249
        $indexWithoutErrors = $indexService->indexItems(10);
250
251
        $label = 'solr.backend.index_queue_module.flashmessage.success.index_manual';
252
        $severity = FlashMessage::OK;
253
        if (!$indexWithoutErrors) {
254
            $label = 'solr.backend.index_queue_module.flashmessage.error.index_manual';
255
            $severity = FlashMessage::ERROR;
256
        }
257
258
        $this->addFlashMessage(
259
            LocalizationUtility::translate($label, 'Solr'),
260
            LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.index_manual', 'Solr'),
261
            $severity
262
        );
263
264
        $this->redirect('index');
265
    }
266
267
    /**
268
     * Adds a flash message for the index queue module.
269
     *
270
     * @param string $label
271
     * @param int $severity
272
     */
273
    protected function addIndexQueueFlashMessage($label, $severity)
274
    {
275
        $this->addFlashMessage(LocalizationUtility::translate($label, 'Solr'), LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.title', 'Solr'), $severity);
276
    }
277
}
278