Passed
Push — master ( 1b8200...cdf526 )
by Timo
21:46
created

IndexQueueModuleController::canQueueSelectedSite()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 0
crap 12
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->canQueueSelectedSite()) {
92
            $this->view->assign('can_not_proceed', true);
93
            return;
94
        }
95
96
        $statistics = $this->indexQueue->getStatisticsBySite($this->selectedSite);
97
        $this->view->assign('indexQueueInitializationSelector', $this->getIndexQueueInitializationSelector());
98
        $this->view->assign('indexqueue_statistics', $statistics);
99
        $this->view->assign('indexqueue_errors', $this->indexQueue->getErrorsBySite($this->selectedSite));
100
    }
101
102
    /**
103
     * Checks if selected site can be queued.
104
     *
105
     * @return bool
106
     */
107
    protected function canQueueSelectedSite()
108
    {
109
        if ($this->selectedSite === null) {
110
            return false;
111
        }
112
        $enabledIndexQueueConfigurationNames = $this->selectedSite->getSolrConfiguration()->getEnabledIndexQueueConfigurationNames();
113
        if (empty($enabledIndexQueueConfigurationNames)) {
114
            return false;
115
        }
116
        return true;
117
    }
118
119
    /**
120
     * Renders a field to select which indexing configurations to initialize.
121
     *
122
     * Uses TCEforms.
123
     *
124
     * @return string Markup for the select field
125
     */
126
    protected function getIndexQueueInitializationSelector()
127
    {
128
        $selector = GeneralUtility::makeInstance(IndexingConfigurationSelectorField::class, $this->selectedSite);
129
        $selector->setFormElementName('tx_solr-index-queue-initialization');
130
131
        return $selector->render();
132
    }
133
134
    /**
135
     * Initializes the Index Queue for selected indexing configurations
136
     *
137
     * @return void
138
     */
139
    public function initializeIndexQueueAction()
140
    {
141
        $initializedIndexingConfigurations = [];
142
143
        $indexingConfigurationsToInitialize = GeneralUtility::_POST('tx_solr-index-queue-initialization');
144
        if ((!empty($indexingConfigurationsToInitialize)) && (is_array($indexingConfigurationsToInitialize))) {
145
            // initialize selected indexing configuration
146
            foreach ($indexingConfigurationsToInitialize as $indexingConfigurationName) {
147
                $initializedIndexingConfiguration = $this->indexQueue->initialize(
148
                    $this->selectedSite,
149
                    $indexingConfigurationName
150
                );
151
152
                // track initialized indexing configurations for the flash message
153
                $initializedIndexingConfigurations = array_merge(
154
                    $initializedIndexingConfigurations,
155
                    $initializedIndexingConfiguration
156
                );
157
            }
158
        } else {
159
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.no_selection';
160
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.not_initialized.title';
161
            $this->addFlashMessage(
162
                LocalizationUtility::translate($messageLabel, 'Solr'),
163
                LocalizationUtility::translate($titleLabel, 'Solr'),
164
                FlashMessage::WARNING
165
            );
166
        }
167
168
        $messagesForConfigurations = [];
169
        foreach (array_keys($initializedIndexingConfigurations) as $indexingConfigurationName) {
170
            $itemCount = $this->indexQueue->getStatisticsBySite($this->selectedSite, $indexingConfigurationName)->getTotalCount();
171
            $messagesForConfigurations[] = $indexingConfigurationName . ' (' . $itemCount . ' records)';
172
        }
173
174
        if (!empty($initializedIndexingConfigurations)) {
175
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.success';
176
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.initialize.title';
177
            $this->addFlashMessage(
178
                LocalizationUtility::translate($messageLabel, 'Solr',
179
                    [implode(', ', $messagesForConfigurations)]),
180
                LocalizationUtility::translate($titleLabel, 'Solr'),
181
                FlashMessage::OK
182
            );
183
        }
184
185
        $this->redirect('index');
186
    }
187
188
    /**
189
     * Removes all errors in the index queue list. So that the items can be indexed again.
190
     *
191
     * @return void
192
     */
193
    public function resetLogErrorsAction()
194
    {
195
        $resetResult = $this->indexQueue->resetAllErrors();
196
197
        $label = 'solr.backend.index_queue_module.flashmessage.success.reset_errors';
198
        $severity = FlashMessage::OK;
199
        if (!$resetResult) {
200
            $label = 'solr.backend.index_queue_module.flashmessage.error.reset_errors';
201
            $severity = FlashMessage::ERROR;
202
        }
203
204
        $this->addFlashMessage(
205
            LocalizationUtility::translate($label, 'Solr'),
206
            LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.title', 'Solr'),
207
            $severity
208
        );
209
210
        $this->redirect('index');
211
    }
212
213
    /**
214
     * Shows the error message for one queue item.
215
     *
216
     * @param int $indexQueueItemId
217
     * @return void
218
     */
219
    public function showErrorAction(int $indexQueueItemId)
220
    {
221
        if (is_null($indexQueueItemId)) {
222
            // add a flash message and quit
223
            $label = 'solr.backend.index_queue_module.flashmessage.error.no_queue_item_for_queue_error';
224
            $severity = FlashMessage::ERROR;
225
            $this->addFlashMessage(
226
                LocalizationUtility::translate($label, 'Solr'),
227
                LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.title',
228
                    'Solr'),
229
                $severity
230
            );
231
232
            return;
233
        }
234
235
        $item = $this->indexQueue->getItem($indexQueueItemId);
236
        $this->view->assign('indexQueueItem', $item);
237
    }
238
239
    /**
240
     * Indexes a few documents with the index service.
241
     * @return void
242
     */
243
    public function doIndexingRunAction()
244
    {
245
        /** @var $indexService \ApacheSolrForTypo3\Solr\Domain\Index\IndexService */
246
        $indexService = GeneralUtility::makeInstance(IndexService::class, $this->selectedSite);
247
        $indexWithoutErrors = $indexService->indexItems(10);
248
249
        $label = 'solr.backend.index_queue_module.flashmessage.success.index_manual';
250
        $severity = FlashMessage::OK;
251
        if (!$indexWithoutErrors) {
252
            $label = 'solr.backend.index_queue_module.flashmessage.error.index_manual';
253
            $severity = FlashMessage::ERROR;
254
        }
255
256
        $this->addFlashMessage(
257
            LocalizationUtility::translate($label, 'Solr'),
258
            LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.index_manual', 'Solr'),
259
            $severity
260
        );
261
262
        $this->redirect('index');
263
    }
264
}
265