Passed
Push — master ( 390232...cefe26 )
by Timo
04:51
created

IndexQueueModuleController::setIndexQueue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 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
     * @param Queue $indexQueue
75
     */
76
    public function setIndexQueue(Queue $indexQueue)
77
    {
78
        $this->indexQueue = $indexQueue;
79
    }
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) {
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, $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))) {
153
            // initialize selected indexing configuration
154
            foreach ($indexingConfigurationsToInitialize as $indexingConfigurationName) {
155
                $initializedIndexingConfiguration = $this->indexQueue->initialize(
156
                    $this->selectedSite,
157
                    $indexingConfigurationName
158
                );
159
160
                // track initialized indexing configurations for the flash message
161
                $initializedIndexingConfigurations = array_merge(
162
                    $initializedIndexingConfigurations,
163
                    $initializedIndexingConfiguration
164
                );
165
            }
166
        } else {
167
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.no_selection';
168
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.not_initialized.title';
169
            $this->addFlashMessage(
170
                LocalizationUtility::translate($messageLabel, 'Solr'),
171
                LocalizationUtility::translate($titleLabel, 'Solr'),
172
                FlashMessage::WARNING
173
            );
174
        }
175
176
        $messagesForConfigurations = [];
177
        foreach (array_keys($initializedIndexingConfigurations) as $indexingConfigurationName) {
178
            $itemCount = $this->indexQueue->getStatisticsBySite($this->selectedSite, $indexingConfigurationName)->getTotalCount();
179
            $messagesForConfigurations[] = $indexingConfigurationName . ' (' . $itemCount . ' records)';
180
        }
181
182
        if (!empty($initializedIndexingConfigurations)) {
183
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.success';
184
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.initialize.title';
185
            $this->addFlashMessage(
186
                LocalizationUtility::translate($messageLabel, 'Solr',
187
                    [implode(', ', $messagesForConfigurations)]),
188
                LocalizationUtility::translate($titleLabel, 'Solr'),
189
                FlashMessage::OK
190
            );
191
        }
192
193
        $this->redirect('index');
194
    }
195
196
    /**
197
     * Removes all errors in the index queue list. So that the items can be indexed again.
198
     *
199
     * @return void
200
     */
201
    public function resetLogErrorsAction()
202
    {
203
        $resetResult = $this->indexQueue->resetAllErrors();
204
205
        $label = 'solr.backend.index_queue_module.flashmessage.success.reset_errors';
206
        $severity = FlashMessage::OK;
207
        if (!$resetResult) {
208
            $label = 'solr.backend.index_queue_module.flashmessage.error.reset_errors';
209
            $severity = FlashMessage::ERROR;
210
        }
211
212
        $this->addIndexQueueFlashMessage($label, $severity);
213
214
        $this->redirect('index');
215
    }
216
217
    /**
218
     * ReQueues a single item in the indexQueue.
219
     *
220
     * @param string $type
221
     * @param int $uid
222
     *
223
     * @return void
224
     */
225
    public function requeueDocumentAction(string $type, int $uid)
226
    {
227
        $label = 'solr.backend.index_queue_module.flashmessage.error.single_item_not_requeued';
228
        $severity = FlashMessage::ERROR;
229
230
        $updateCount = $this->indexQueue->updateItem($type, $uid, time());
231
        if ($updateCount > 0) {
232
            $label = 'solr.backend.index_queue_module.flashmessage.success.single_item_was_requeued';
233
            $severity = FlashMessage::OK;
234
        }
235
236
        $this->addIndexQueueFlashMessage($label, $severity);
237
238
        $this->redirect('index');
239
    }
240
241
    /**
242
     * Shows the error message for one queue item.
243
     *
244
     * @param int $indexQueueItemId
245
     * @return void
246
     */
247
    public function showErrorAction(int $indexQueueItemId)
248
    {
249
        if (is_null($indexQueueItemId)) {
250
            // add a flash message and quit
251
            $label = 'solr.backend.index_queue_module.flashmessage.error.no_queue_item_for_queue_error';
252
            $severity = FlashMessage::ERROR;
253
            $this->addIndexQueueFlashMessage($label, $severity);
254
255
            return;
256
        }
257
258
        $item = $this->indexQueue->getItem($indexQueueItemId);
259
        $this->view->assign('indexQueueItem', $item);
260
    }
261
262
    /**
263
     * Indexes a few documents with the index service.
264
     * @return void
265
     */
266
    public function doIndexingRunAction()
267
    {
268
        /** @var $indexService \ApacheSolrForTypo3\Solr\Domain\Index\IndexService */
269
        $indexService = GeneralUtility::makeInstance(IndexService::class, $this->selectedSite);
270
        $indexWithoutErrors = $indexService->indexItems(10);
271
272
        $label = 'solr.backend.index_queue_module.flashmessage.success.index_manual';
273
        $severity = FlashMessage::OK;
274
        if (!$indexWithoutErrors) {
275
            $label = 'solr.backend.index_queue_module.flashmessage.error.index_manual';
276
            $severity = FlashMessage::ERROR;
277
        }
278
279
        $this->addFlashMessage(
280
            LocalizationUtility::translate($label, 'Solr'),
281
            LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.index_manual', 'Solr'),
282
            $severity
283
        );
284
285
        $this->redirect('index');
286
    }
287
288
    /**
289
     * Adds a flash message for the index queue module.
290
     *
291
     * @param string $label
292
     * @param int $severity
293
     */
294
    protected function addIndexQueueFlashMessage($label, $severity)
295
    {
296
        $this->addFlashMessage(LocalizationUtility::translate($label, 'Solr'), LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.title', 'Solr'), $severity);
297
    }
298
}
299