Passed
Push — master ( 0720cb...a95893 )
by Timo
02:36
created

IndexQueueModuleController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 209
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 0
loc 209
ccs 0
cts 119
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeAction() 0 5 1
A initializeView() 0 4 1
A indexAction() 0 15 2
A getIndexQueueInitializationSelector() 0 7 1
B initializeIndexQueueAction() 0 48 6
A resetLogErrorsAction() 0 19 2
A showErrorAction() 0 19 2
A doIndexingRunAction() 0 21 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 2 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
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
     * Set up the doc header properly here
75
     *
76
     * @param ViewInterface $view
77
     * @return void
78
     */
79
    protected function initializeView(ViewInterface $view)
80
    {
81
        parent::initializeView($view);
82
    }
83
84
    /**
85
     * Lists the available indexing configurations
86
     *
87
     * @return void
88
     */
89
    public function indexAction()
90
    {
91
        if ($this->selectedSite === null) {
92
            $this->view->assignMultiple([
93
                'can_not_proceed' => true,
94
                'pageUID' => $this->selectedPageUID
95
            ]);
96
            return;
97
        }
98
99
        $statistics = $this->indexQueue->getStatisticsBySite($this->selectedSite);
100
        $this->view->assign('indexQueueInitializationSelector', $this->getIndexQueueInitializationSelector());
101
        $this->view->assign('indexqueue_statistics', $statistics);
102
        $this->view->assign('indexqueue_errors', $this->indexQueue->getErrorsBySite($this->selectedSite));
103
    }
104
105
    /**
106
     * Renders a field to select which indexing configurations to initialize.
107
     *
108
     * Uses TCEforms.
109
     *
110
     * @return string Markup for the select field
111
     */
112
    protected function getIndexQueueInitializationSelector()
113
    {
114
        $selector = GeneralUtility::makeInstance(IndexingConfigurationSelectorField::class, $this->selectedSite);
115
        $selector->setFormElementName('tx_solr-index-queue-initialization');
116
117
        return $selector->render();
118
    }
119
120
    /**
121
     * Initializes the Index Queue for selected indexing configurations
122
     *
123
     * @return void
124
     */
125
    public function initializeIndexQueueAction()
126
    {
127
        $initializedIndexingConfigurations = [];
128
129
        $indexingConfigurationsToInitialize = GeneralUtility::_POST('tx_solr-index-queue-initialization');
130
        if ((!empty($indexingConfigurationsToInitialize)) && (is_array($indexingConfigurationsToInitialize))) {
131
            // initialize selected indexing configuration
132
            foreach ($indexingConfigurationsToInitialize as $indexingConfigurationName) {
133
                $initializedIndexingConfiguration = $this->indexQueue->initialize(
134
                    $this->selectedSite,
135
                    $indexingConfigurationName
136
                );
137
138
                // track initialized indexing configurations for the flash message
139
                $initializedIndexingConfigurations = array_merge(
140
                    $initializedIndexingConfigurations,
141
                    $initializedIndexingConfiguration
142
                );
143
            }
144
        } else {
145
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.no_selection';
146
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.not_initialized.title';
147
            $this->addFlashMessage(
148
                LocalizationUtility::translate($messageLabel, 'Solr'),
149
                LocalizationUtility::translate($titleLabel, 'Solr'),
150
                FlashMessage::WARNING
151
            );
152
        }
153
154
        $messagesForConfigurations = [];
155
        foreach (array_keys($initializedIndexingConfigurations) as $indexingConfigurationName) {
156
            $itemCount = $this->indexQueue->getStatisticsBySite($this->selectedSite, $indexingConfigurationName)->getTotalCount();
157
            $messagesForConfigurations[] = $indexingConfigurationName . ' (' . $itemCount . ' records)';
158
        }
159
160
        if (!empty($initializedIndexingConfigurations)) {
161
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.success';
162
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.initialize.title';
163
            $this->addFlashMessage(
164
                LocalizationUtility::translate($messageLabel, 'Solr',
165
                    [implode(', ', $messagesForConfigurations)]),
166
                LocalizationUtility::translate($titleLabel, 'Solr'),
167
                FlashMessage::OK
168
            );
169
        }
170
171
        $this->redirect('index');
172
    }
173
174
    /**
175
     * Removes all errors in the index queue list. So that the items can be indexed again.
176
     *
177
     * @return void
178
     */
179
    public function resetLogErrorsAction()
180
    {
181
        $resetResult = $this->indexQueue->resetAllErrors();
182
183
        $label = 'solr.backend.index_queue_module.flashmessage.success.reset_errors';
184
        $severity = FlashMessage::OK;
185
        if (!$resetResult) {
186
            $label = 'solr.backend.index_queue_module.flashmessage.error.reset_errors';
187
            $severity = FlashMessage::ERROR;
188
        }
189
190
        $this->addFlashMessage(
191
            LocalizationUtility::translate($label, 'Solr'),
192
            LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.title', 'Solr'),
193
            $severity
194
        );
195
196
        $this->redirect('index');
197
    }
198
199
    /**
200
     * Shows the error message for one queue item.
201
     *
202
     * @param int $indexQueueItemId
203
     * @return void
204
     */
205
    public function showErrorAction(int $indexQueueItemId)
206
    {
207
        if (is_null($indexQueueItemId)) {
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->addFlashMessage(
212
                LocalizationUtility::translate($label, 'Solr'),
213
                LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.title',
214
                    'Solr'),
215
                $severity
216
            );
217
218
            return;
219
        }
220
221
        $item = $this->indexQueue->getItem($indexQueueItemId);
222
        $this->view->assign('indexQueueItem', $item);
223
    }
224
225
    /**
226
     * Indexes a few documents with the index service.
227
     * @return void
228
     */
229
    public function doIndexingRunAction()
230
    {
231
        /** @var $indexService \ApacheSolrForTypo3\Solr\Domain\Index\IndexService */
232
        $indexService = GeneralUtility::makeInstance(IndexService::class, $this->selectedSite);
233
        $indexWithoutErrors = $indexService->indexItems(10);
234
235
        $label = 'solr.backend.index_queue_module.flashmessage.success.index_manual';
236
        $severity = FlashMessage::OK;
237
        if (!$indexWithoutErrors) {
238
            $label = 'solr.backend.index_queue_module.flashmessage.error.index_manual';
239
            $severity = FlashMessage::ERROR;
240
        }
241
242
        $this->addFlashMessage(
243
            LocalizationUtility::translate($label, 'Solr'),
244
            LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.index_manual', 'Solr'),
245
            $severity
246
        );
247
248
        $this->redirect('index');
249
    }
250
}
251