Passed
Push — release-11.2.x ( 9027bf...093924 )
by Markus
12:13
created

IndexQueueModuleController   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 244
Duplicated Lines 0 %

Test Coverage

Coverage 13.27%

Importance

Changes 0
Metric Value
wmc 24
eloc 83
dl 0
loc 244
ccs 13
cts 98
cp 0.1327
rs 10
c 0
b 0
f 0

12 Methods

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