Passed
Push — master ( 771259...180e9a )
by Timo
25:15 queued 04:34
created

IndexQueueModuleController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 205
ccs 0
cts 115
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A clearIndexQueueAction() 0 5 1
A __construct() 0 5 2
A indexAction() 0 7 1
A getIndexQueueInitializationSelector() 0 7 1
B initializeIndexQueueAction() 0 50 6
A resetLogErrorsAction() 0 19 2
A showErrorAction() 0 21 2
A doIndexingRunAction() 0 21 2
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Backend\SolrModule;
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\Core\Database\DatabaseConnection;
31
use TYPO3\CMS\Core\Messaging\FlashMessage;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
34
35
/**
36
 * Index Queue Module
37
 *
38
 * @author Ingo Renner <[email protected]>
39
 */
40
class IndexQueueModuleController extends AbstractModuleController
41
{
42
43
    /**
44
     * Module name, used to identify a module f.e. in URL parameters.
45
     *
46
     * @var string
47
     */
48
    protected $moduleName = 'IndexQueue';
49
50
    /**
51
     * Module title, shows up in the module menu.
52
     *
53
     * @var string
54
     */
55
    protected $moduleTitle = 'Index Queue';
56
57
    /**
58
     * @var Queue
59
     */
60
    protected $indexQueue;
61
62
    /**
63
     * IndexQueueModuleController constructor.
64
     * @param Queue $indexQueue
65
     */
66
    public function __construct(Queue $indexQueue = null)
67
    {
68
        parent::__construct();
69
        $this->indexQueue = is_null($indexQueue) ? GeneralUtility::makeInstance(Queue::class) : $indexQueue;
70
    }
71
72
    /**
73
     * Lists the available indexing configurations
74
     *
75
     * @return void
76
     */
77
    public function indexAction()
78
    {
79
        $statistics = $this->indexQueue->getStatisticsBySite($this->site);
80
        $this->view->assign('indexQueueInitializationSelector', $this->getIndexQueueInitializationSelector());
81
        $this->view->assign('indexqueue_statistics', $statistics);
82
        $this->view->assign('indexqueue_errors', $this->indexQueue->getErrorsBySite($this->site));
83
    }
84
85
    /**
86
     * Renders a field to select which indexing configurations to initialize.
87
     *
88
     * Uses TCEforms.
89
     *
90
     * @return string Markup for the select field
91
     */
92
    protected function getIndexQueueInitializationSelector()
93
    {
94
        $selector = GeneralUtility::makeInstance(IndexingConfigurationSelectorField::class, $this->site);
95
        $selector->setFormElementName('tx_solr-index-queue-initialization');
96
97
        return $selector->render();
98
    }
99
100
    /**
101
     * Initializes the Index Queue for selected indexing configurations
102
     *
103
     * @return void
104
     */
105
    public function initializeIndexQueueAction()
106
    {
107
        $initializedIndexingConfigurations = [];
108
109
        $indexingConfigurationsToInitialize = GeneralUtility::_POST('tx_solr-index-queue-initialization');
110
        if ((!empty($indexingConfigurationsToInitialize)) && (is_array($indexingConfigurationsToInitialize))) {
111
            // initialize selected indexing configuration
112
            foreach ($indexingConfigurationsToInitialize as $indexingConfigurationName) {
113
                $initializedIndexingConfiguration = $this->indexQueue->initialize(
114
                    $this->site,
115
                    $indexingConfigurationName
116
                );
117
118
                // track initialized indexing configurations for the flash message
119
                $initializedIndexingConfigurations = array_merge(
120
                    $initializedIndexingConfigurations,
121
                    $initializedIndexingConfiguration
122
                );
123
            }
124
        } else {
125
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.no_selection';
126
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.not_initialized.title';
127
            $this->addFlashMessage(
128
                LocalizationUtility::translate($messageLabel, 'Solr'),
129
                LocalizationUtility::translate($titleLabel, 'Solr'),
130
                FlashMessage::WARNING
131
            );
132
        }
133
134
        $messagesForConfigurations = [];
135
        foreach (array_keys($initializedIndexingConfigurations) as $indexingConfigurationName) {
136
            $itemCount = $this->indexQueue->getItemsCountBySite($this->site, $indexingConfigurationName);
137
            $messagesForConfigurations[] = $indexingConfigurationName . ' (' . $itemCount . ' records)';
138
        }
139
140
        if (!empty($initializedIndexingConfigurations)) {
141
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.success';
142
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.initialize.title';
143
            $this->addFlashMessage(
144
                LocalizationUtility::translate($messageLabel, 'Solr',
145
                    [implode(', ', $messagesForConfigurations)]),
146
                LocalizationUtility::translate($titleLabel, 'Solr'),
147
                FlashMessage::OK
148
            );
149
150
            $this->addFlashMessagesByQueueIdentifier('solr.queue.initializer');
151
        }
152
153
        $this->forward('index');
154
    }
155
156
    /**
157
     * Empties the Index Queue
158
     *
159
     * @return void
160
     */
161
    public function clearIndexQueueAction()
162
    {
163
        $this->indexQueue->deleteItemsBySite($this->site);
164
        $this->forward('index');
165
    }
166
167
    /**
168
     * Removes all errors in the index queue list. So that the items can be indexed again.
169
     *
170
     * @return void
171
     */
172
    public function resetLogErrorsAction()
173
    {
174
        $resetResult = $this->indexQueue->resetAllErrors();
175
176
        $label = 'solr.backend.index_queue_module.flashmessage.success.reset_errors';
177
        $severity = FlashMessage::OK;
178
        if (!$resetResult) {
179
            $label = 'solr.backend.index_queue_module.flashmessage.error.reset_errors';
180
            $severity = FlashMessage::ERROR;
181
        }
182
183
        $this->addFlashMessage(
184
            LocalizationUtility::translate($label, 'Solr'),
185
            LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.title', 'Solr'),
186
            $severity
187
        );
188
189
        $this->forward('index');
190
    }
191
192
    /**
193
     * Shows the error message for one queue item.
194
     *
195
     * @return void
196
     */
197
    public function showErrorAction()
198
    {
199
        $queueItemId = $this->getRequestArgumentOrDefaultValue('indexQueueItemId', null);
200
201
        if (is_null($queueItemId)) {
202
            // add a flash message and quit
203
            $label = 'solr.backend.index_queue_module.flashmessage.error.no_queue_item_for_queue_error';
204
            $severity = FlashMessage::ERROR;
205
            $this->addFlashMessage(
206
                LocalizationUtility::translate($label, 'Solr'),
207
                LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.title',
208
                    'Solr'),
209
                $severity
210
            );
211
212
            return;
213
        }
214
215
        $item = $this->indexQueue->getItem($queueItemId);
216
        $this->view->assign('indexQueueItem', $item);
217
    }
218
219
    /**
220
     * Indexes a few documents with the index service.
221
     * @return void
222
     */
223
    public function doIndexingRunAction()
224
    {
225
        /** @var $indexService \ApacheSolrForTypo3\Solr\Domain\Index\IndexService */
226
        $indexService = GeneralUtility::makeInstance(IndexService::class, $this->site);
227
        $indexWithoutErrors = $indexService->indexItems(10);
228
229
        $label = 'solr.backend.index_queue_module.flashmessage.success.index_manual';
230
        $severity = FlashMessage::OK;
231
        if (!$indexWithoutErrors) {
232
            $label = 'solr.backend.index_queue_module.flashmessage.error.index_manual';
233
            $severity = FlashMessage::ERROR;
234
        }
235
236
        $this->addFlashMessage(
237
            LocalizationUtility::translate($label, 'Solr'),
238
            LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.index_manual', 'Solr'),
239
            $severity
240
        );
241
242
        $this->forward('index');
243
    }
244
}
245