Passed
Pull Request — release-11.2.x (#3594)
by Markus
14:43 queued 10:47
created

IndexAdministrationModuleController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 53.23%

Importance

Changes 0
Metric Value
wmc 15
eloc 59
dl 0
loc 147
ccs 33
cts 62
cp 0.5323
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setSolrConnectionManager() 0 3 1
A redirectToReferrerModule() 0 15 2
A initializeAction() 0 5 1
A clearIndexQueueAction() 0 11 1
A indexAction() 0 4 3
A reloadIndexConfigurationAction() 0 35 4
A emptyIndexAction() 0 21 3
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Controller\Backend\Search;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2013-2015 Ingo Renner <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\ConnectionManager;
29
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
30
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
31
use TYPO3\CMS\Backend\Routing\UriBuilder as BackendUriBuilder;
32
use TYPO3\CMS\Core\Messaging\FlashMessage;
33
use TYPO3\CMS\Core\Utility\GeneralUtility;
34
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
35
36
/**
37
 * Index Administration Module
38
 *
39
 * @author Ingo Renner <[email protected]>
40
 */
41
class IndexAdministrationModuleController extends AbstractModuleController
0 ignored issues
show
Bug introduced by
The type ApacheSolrForTypo3\Solr\...bstractModuleController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
{
43
44
    /**
45
     * @var Queue
46
     */
47
    protected $indexQueue;
48
49
    /**
50
     * @var ConnectionManager
51
     */
52
    protected $solrConnectionManager;
53
54
    /**
55
     * @param ConnectionManager $solrConnectionManager
56
     */
57
    public function setSolrConnectionManager(ConnectionManager $solrConnectionManager)
58 3
    {
59
        $this->solrConnectionManager = $solrConnectionManager;
60 3
    }
61 3
62
    /**
63
     * Initializes the controller before invoking an action method.
64
     */
65
    protected function initializeAction()
66
    {
67
        parent::initializeAction();
68
        $this->indexQueue = GeneralUtility::makeInstance(Queue::class);
69
        $this->solrConnectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
70
    }
71
72
    /**
73
     * Index action, shows an overview of available index maintenance operations.
74
     */
75
    public function indexAction()
76
    {
77
        if ($this->selectedSite === null || empty($this->solrConnectionManager->getConnectionsBySite($this->selectedSite))) {
78
            $this->view->assign('can_not_proceed', true);
79
        }
80
    }
81
82
    /**
83
     * Empties the site's indexes.
84
     */
85
    public function emptyIndexAction()
86
    {
87
        $siteHash = $this->selectedSite->getSiteHash();
88
89
        try {
90 1
            $affectedCores = [];
91
            $solrServers = $this->solrConnectionManager->getConnectionsBySite($this->selectedSite);
92 1
            foreach ($solrServers as $solrServer) {
93
                $writeService = $solrServer->getWriteService();
94
                /* @var $solrServer SolrConnection */
95 1
                $writeService->deleteByQuery('siteHash:' . $siteHash);
96 1
                $writeService->commit(false, false, false);
0 ignored issues
show
Unused Code introduced by
The call to ApacheSolrForTypo3\Solr\...rWriteService::commit() has too many arguments starting with false. ( Ignorable by Annotation )

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

96
                $writeService->/** @scrutinizer ignore-call */ 
97
                               commit(false, false, false);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
97 1
                $affectedCores[] = $writeService->getPrimaryEndpoint()->getCore();
98 1
            }
99
            $message = LocalizationUtility::translate('solr.backend.index_administration.index_emptied_all', 'Solr', [$this->selectedSite->getLabel(), implode(', ', $affectedCores)]);
100 1
            $this->addFlashMessage($message);
101 1
        } catch (\Exception $e) {
102 1
            $this->addFlashMessage(LocalizationUtility::translate('solr.backend.index_administration.error.on_empty_index', 'Solr', [$e->__toString()]), '', FlashMessage::ERROR);
103
        }
104 1
105 1
        $this->redirect('index');
106
    }
107
108
    /**
109
     * Empties the Index Queue
110 1
     */
111 1
    public function clearIndexQueueAction()
112
    {
113
        $this->indexQueue->deleteItemsBySite($this->selectedSite);
114
        $this->addFlashMessage(
115
            LocalizationUtility::translate(
116
                'solr.backend.index_administration.success.queue_emptied',
117
                'Solr',
118
                [$this->selectedSite->getLabel()]
119
            )
120
        );
121
        $this->redirectToReferrerModule();
122
    }
123
124
    /**
125
     * Reloads the site's Solr cores.
126
     */
127
    public function reloadIndexConfigurationAction()
128
    {
129
        $coresReloaded = true;
130
        $reloadedCores = [];
131
        $solrServers = $this->solrConnectionManager->getConnectionsBySite($this->selectedSite);
132
133 2
        foreach ($solrServers as $solrServer) {
134
            /* @var $solrServer SolrConnection */
135 2
            $coreAdmin = $solrServer->getAdminService();
136 2
            $coreReloaded = $coreAdmin->reloadCore()->getHttpStatus() === 200;
137 2
138
            $coreName = $coreAdmin->getPrimaryEndpoint()->getCore();
139 2
            if (!$coreReloaded) {
140
                $coresReloaded = false;
141 2
142 2
                $this->addFlashMessage(
143
                    'Failed to reload index configuration for core "' . $coreName . '"',
144 2
                    '',
145 2
                    FlashMessage::ERROR
146
                );
147
                break;
148
            }
149
150
            $reloadedCores[] = $coreName;
151
        }
152
153
        if ($coresReloaded) {
154
            $this->addFlashMessage(
155
                'Core configuration reloaded (' . implode(', ', $reloadedCores) . ').',
156 2
                '',
157
                FlashMessage::OK
158
            );
159 2
        }
160 2
161 2
        $this->redirect('index');
162 2
    }
163 2
164
    /**
165
     * Redirects to the referrer module index Action.
166
     *
167 2
     * Fluids <f:form VH can not make urls to other modules properly.
168 2
     * The module name/key is not provided in the hidden fields __referrer by bulding form.
169
     * So this is currently the single way to make it possible.
170
     *
171
     * @todo: remove this method if f:form works properly between backend modules.
172
     */
173
    protected function redirectToReferrerModule()
174
    {
175
        $wasFromQueue = $this->request->hasArgument('fromQueue');
176
        if (!$wasFromQueue) {
177
            $this->redirect('index');
178
            return;
179
        }
180
181
        /* @var BackendUriBuilder $backendUriBuilder */
182
        $backendUriBuilder = GeneralUtility::makeInstance(BackendUriBuilder::class);
183
184
        $parameters =  ['id' => $this->selectedPageUID];
185
        $referringUri = $backendUriBuilder->buildUriFromRoute('searchbackend_SolrIndexqueue', $parameters);
186
187
        $this->redirectToUri($referringUri);
188
    }
189
}
190