Passed
Push — task/3376-TYPO3_12_compatibili... ( b3b363...4d8ea7 )
by Rafael
44:55
created

IndexQueueModuleController::canQueueSelectedSite()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 4
nc 3
nop 0
1
<?php
2
3
/*
4
 * This file is part of the TYPO3 CMS project.
5
 *
6
 * It is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License, either version 2
8
 * of the License, or any later version.
9
 *
10
 * For the full copyright and license information, please read the
11
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
 * The TYPO3 project - inspiring people to share!
14
 */
15
16
namespace ApacheSolrForTypo3\Solr\Controller\Backend\Search;
17
18
use ApacheSolrForTypo3\Solr\Backend\IndexingConfigurationSelectorField;
19
use ApacheSolrForTypo3\Solr\Domain\Index\IndexService;
20
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
21
use Psr\Http\Message\ResponseInterface;
22
use TYPO3\CMS\Backend\Form\Exception as BackendFormException;
23
use TYPO3\CMS\Core\Http\RedirectResponse;
24
use TYPO3\CMS\Core\Messaging\AbstractMessage;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
27
28
/**
29
 * Index Queue Module
30
 *
31
 * @author Ingo Renner <[email protected]>
32
 */
33
class IndexQueueModuleController extends AbstractModuleController
34
{
35
    /**
36
     * @param Queue $indexQueue
37
     */
38
    public function setIndexQueue(Queue $indexQueue)
39
    {
40
        $this->indexQueue = $indexQueue;
41
    }
42
43
    /**
44
     * Lists the available indexing configurations
45
     *
46
     * @return ResponseInterface
47
     * @throws BackendFormException
48
     */
49
    public function indexAction(): ResponseInterface
50
    {
51
        $this->initializeAction();
52
        if (!$this->canQueueSelectedSite()) {
53
            $this->view->assign('can_not_proceed', true);
54
            return $this->getModuleTemplateResponse();
55
        }
56
57
        $statistics = $this->indexQueue->getStatisticsBySite($this->selectedSite);
58
        $this->view->assign('indexQueueInitializationSelector', $this->getIndexQueueInitializationSelector());
59
        $this->view->assign('indexqueue_statistics', $statistics);
60
        $this->view->assign('indexqueue_errors', $this->indexQueue->getErrorsBySite($this->selectedSite));
61
        return $this->getModuleTemplateResponse();
62
    }
63
64
    /**
65
     * Checks if selected site can be queued.
66
     *
67
     * @return bool
68
     */
69
    protected function canQueueSelectedSite(): bool
70
    {
71
        if ($this->selectedSite === null || empty($this->solrConnectionManager->getConnectionsBySite($this->selectedSite))) {
72
            return false;
73
        }
74
        $enabledIndexQueueConfigurationNames = $this->selectedSite->getSolrConfiguration()->getEnabledIndexQueueConfigurationNames();
75
        if (empty($enabledIndexQueueConfigurationNames)) {
76
            return false;
77
        }
78
        return true;
79
    }
80
81
    /**
82
     * Renders a field to select which indexing configurations to initialize.
83
     *
84
     * Uses TCEforms.
85
     *
86
     * @return string Markup for the select field
87
     * @throws BackendFormException
88
     */
89
    protected function getIndexQueueInitializationSelector(): string
90
    {
91
        $selector = GeneralUtility::makeInstance(IndexingConfigurationSelectorField::class, /** @scrutinizer ignore-type */ $this->selectedSite);
92
        $selector->setFormElementName('tx_solr-index-queue-initialization');
93
94
        return $selector->render();
95
    }
96
97
    /**
98
     * Initializes the Index Queue for selected indexing configurations
99
     *
100
     * @return ResponseInterface
101
     */
102
    public function initializeIndexQueueAction(): ResponseInterface
103
    {
104
        $initializedIndexingConfigurations = [];
105
106
        $indexingConfigurationsToInitialize = GeneralUtility::_POST('tx_solr-index-queue-initialization');
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Core\Utility\GeneralUtility::_POST() has been deprecated: since TYPO3 v12.2. will be removed in TYPO3 v13.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

106
        $indexingConfigurationsToInitialize = /** @scrutinizer ignore-deprecated */ GeneralUtility::_POST('tx_solr-index-queue-initialization');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
107
        if ((!empty($indexingConfigurationsToInitialize)) && (is_array($indexingConfigurationsToInitialize))) {
0 ignored issues
show
introduced by
The condition is_array($indexingConfigurationsToInitialize) is always false.
Loading history...
108
            // initialize selected indexing configuration
109
            try {
110
                $initializedIndexingConfigurations = $this->indexQueue->getInitializationService()->initializeBySiteAndIndexConfigurations($this->selectedSite, $indexingConfigurationsToInitialize);
111
            } catch (\Throwable $e) {
112
                $this->addFlashMessage(
113
                    sprintf(
114
                        LocalizationUtility::translate(
115
                            'solr.backend.index_queue_module.flashmessage.initialize_failure',
116
                            'Solr'
117
                        ),
118
                        $e->getMessage(),
119
                        $e->getCode()
120
                    ),
121
                    LocalizationUtility::translate(
122
                        'solr.backend.index_queue_module.flashmessage.initialize_failure.title',
123
                        'Solr'
124
                    ),
125
                    AbstractMessage::ERROR
126
                );
127
            }
128
        } else {
129
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.no_selection';
130
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.not_initialized.title';
131
            $this->addFlashMessage(
132
                LocalizationUtility::translate($messageLabel, 'Solr'),
133
                LocalizationUtility::translate($titleLabel, 'Solr'),
134
                AbstractMessage::WARNING
0 ignored issues
show
Deprecated Code introduced by
The constant TYPO3\CMS\Core\Messaging\AbstractMessage::WARNING has been deprecated: Use \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::WARNING instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

134
                /** @scrutinizer ignore-deprecated */ AbstractMessage::WARNING

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
135
            );
136
        }
137
        $messagesForConfigurations = [];
138
        foreach (array_keys($initializedIndexingConfigurations) as $indexingConfigurationName) {
139
            $itemCount = $this->indexQueue->getStatisticsBySite($this->selectedSite, $indexingConfigurationName)->getTotalCount();
0 ignored issues
show
Bug introduced by
It seems like $this->selectedSite can also be of type null; however, parameter $site of ApacheSolrForTypo3\Solr\...::getStatisticsBySite() does only seem to accept ApacheSolrForTypo3\Solr\Domain\Site\Site, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
            $itemCount = $this->indexQueue->getStatisticsBySite(/** @scrutinizer ignore-type */ $this->selectedSite, $indexingConfigurationName)->getTotalCount();
Loading history...
140
            $messagesForConfigurations[] = $indexingConfigurationName . ' (' . $itemCount . ' records)';
141
        }
142
143
        if (!empty($initializedIndexingConfigurations)) {
144
            $messageLabel = 'solr.backend.index_queue_module.flashmessage.initialize.success';
145
            $titleLabel = 'solr.backend.index_queue_module.flashmessage.initialize.title';
146
            $this->addFlashMessage(
147
                LocalizationUtility::translate($messageLabel, 'Solr', [implode(', ', $messagesForConfigurations)]),
148
                LocalizationUtility::translate($titleLabel, 'Solr'),
149
                AbstractMessage::OK
0 ignored issues
show
Deprecated Code introduced by
The constant TYPO3\CMS\Core\Messaging\AbstractMessage::OK has been deprecated: Use \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::OK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

149
                /** @scrutinizer ignore-deprecated */ AbstractMessage::OK

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
150
            );
151
        }
152
153
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
154
    }
155
156
    /**
157
     * Removes all errors in the index queue list. So that the items can be indexed again.
158
     *
159
     * @return ResponseInterface
160
     */
161
    public function resetLogErrorsAction(): ResponseInterface
162
    {
163
        $resetResult = $this->indexQueue->resetAllErrors();
164
165
        $label = 'solr.backend.index_queue_module.flashmessage.success.reset_errors';
166
        $severity = AbstractMessage::OK;
0 ignored issues
show
Deprecated Code introduced by
The constant TYPO3\CMS\Core\Messaging\AbstractMessage::OK has been deprecated: Use \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::OK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

166
        $severity = /** @scrutinizer ignore-deprecated */ AbstractMessage::OK;

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
167
        if (!$resetResult) {
168
            $label = 'solr.backend.index_queue_module.flashmessage.error.reset_errors';
169
            $severity = AbstractMessage::ERROR;
0 ignored issues
show
Deprecated Code introduced by
The constant TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR has been deprecated: Use \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::ERROR instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

169
            $severity = /** @scrutinizer ignore-deprecated */ AbstractMessage::ERROR;

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
170
        }
171
172
        $this->addIndexQueueFlashMessage($label, $severity);
173
174
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
175
    }
176
177
    /**
178
     * ReQueues a single item in the indexQueue.
179
     *
180
     * @param string $type
181
     * @param int $uid
182
     * @return ResponseInterface
183
     */
184
    public function requeueDocumentAction(string $type, int $uid): ResponseInterface
185
    {
186
        $label = 'solr.backend.index_queue_module.flashmessage.error.single_item_not_requeued';
187
        $severity = AbstractMessage::ERROR;
0 ignored issues
show
Deprecated Code introduced by
The constant TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR has been deprecated: Use \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::ERROR instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

187
        $severity = /** @scrutinizer ignore-deprecated */ AbstractMessage::ERROR;

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
188
189
        $updateCount = $this->indexQueue->updateItem($type, $uid, time());
190
        if ($updateCount > 0) {
191
            $label = 'solr.backend.index_queue_module.flashmessage.success.single_item_was_requeued';
192
            $severity = AbstractMessage::OK;
0 ignored issues
show
Deprecated Code introduced by
The constant TYPO3\CMS\Core\Messaging\AbstractMessage::OK has been deprecated: Use \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::OK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

192
            $severity = /** @scrutinizer ignore-deprecated */ AbstractMessage::OK;

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
193
        }
194
195
        $this->addIndexQueueFlashMessage($label, $severity);
196
197
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
198
    }
199
200
    /**
201
     * Shows the error message for one queue item.
202
     *
203
     * @param int $indexQueueItemId
204
     * @return ResponseInterface
205
     */
206
    public function showErrorAction(int $indexQueueItemId): ResponseInterface
207
    {
208
        $item = $this->indexQueue->getItem($indexQueueItemId);
209
        if ($item === null) {
210
            // add a flash message and quit
211
            $label = 'solr.backend.index_queue_module.flashmessage.error.no_queue_item_for_queue_error';
212
            $severity = AbstractMessage::ERROR;
0 ignored issues
show
Deprecated Code introduced by
The constant TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR has been deprecated: Use \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::ERROR instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

212
            $severity = /** @scrutinizer ignore-deprecated */ AbstractMessage::ERROR;

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
213
            $this->addIndexQueueFlashMessage($label, $severity);
214
215
            return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
216
        }
217
218
        $this->view->assign('indexQueueItem', $item);
219
        return $this->getModuleTemplateResponse();
220
    }
221
222
    /**
223
     * Indexes a few documents with the index service.
224
     * @return ResponseInterface
225
     */
226
    public function doIndexingRunAction(): ResponseInterface
227
    {
228
        /* @var IndexService $indexService */
229
        $indexService = GeneralUtility::makeInstance(IndexService::class, /** @scrutinizer ignore-type */ $this->selectedSite);
230
        $indexWithoutErrors = $indexService->indexItems(1);
231
232
        $label = 'solr.backend.index_queue_module.flashmessage.success.index_manual';
233
        $severity = AbstractMessage::OK;
0 ignored issues
show
Deprecated Code introduced by
The constant TYPO3\CMS\Core\Messaging\AbstractMessage::OK has been deprecated: Use \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::OK instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

233
        $severity = /** @scrutinizer ignore-deprecated */ AbstractMessage::OK;

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
234
        if (!$indexWithoutErrors) {
235
            $label = 'solr.backend.index_queue_module.flashmessage.error.index_manual';
236
            $severity = AbstractMessage::ERROR;
0 ignored issues
show
Deprecated Code introduced by
The constant TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR has been deprecated: Use \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::ERROR instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

236
            $severity = /** @scrutinizer ignore-deprecated */ AbstractMessage::ERROR;

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
237
        }
238
239
        $this->addFlashMessage(
240
            LocalizationUtility::translate($label, 'Solr'),
241
            LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.index_manual', 'Solr'),
242
            $severity
243
        );
244
245
        return new RedirectResponse($this->uriBuilder->uriFor('index'), 303);
246
    }
247
248
    /**
249
     * Adds a flash message for the index queue module.
250
     *
251
     * @param string $label
252
     * @param int $severity
253
     */
254
    protected function addIndexQueueFlashMessage(string $label, int $severity)
255
    {
256
        $this->addFlashMessage(LocalizationUtility::translate($label, 'Solr'), LocalizationUtility::translate('solr.backend.index_queue_module.flashmessage.title', 'Solr'), $severity);
257
    }
258
}
259