Passed
Push — master ( 9d2dcb...4e6543 )
by Timo
45:55 queued 43:04
created

IndexAdministrationModuleController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 30.3%

Importance

Changes 0
Metric Value
wmc 15
eloc 57
c 0
b 0
f 0
dl 0
loc 152
ccs 20
cts 66
cp 0.303
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A redirectToReferrerModule() 0 15 2
A setSolrConnectionManager() 0 3 1
A initializeAction() 0 5 1
A clearIndexQueueAction() 0 8 1
A indexAction() 0 4 3
A reloadIndexConfigurationAction() 0 35 4
A emptyIndexAction() 0 21 3
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 3 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\ConnectionManager;
28
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
29
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
30
use ApacheSolrForTypo3\Solr\Util;
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\Mvc\Web\ReferringRequest;
35
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
36
37
/**
38
 * Index Administration Module
39
 *
40
 * @author Ingo Renner <[email protected]>
41
 */
42
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...
43
{
44
45
    /**
46
     * @var Queue
47
     */
48
    protected $indexQueue;
49
50
    /**
51
     * @var ConnectionManager
52
     */
53
    protected $solrConnectionManager = null;
54
55
    /**
56
     * @param ConnectionManager $solrConnectionManager
57
     */
58 1
    public function setSolrConnectionManager(ConnectionManager $solrConnectionManager)
59
    {
60 1
        $this->solrConnectionManager = $solrConnectionManager;
61 1
    }
62
63
    /**
64
     * Initializes the controller before invoking an action method.
65
     */
66
    protected function initializeAction()
67
    {
68
        parent::initializeAction();
69
        $this->indexQueue = GeneralUtility::makeInstance(Queue::class);
70
        $this->solrConnectionManager = GeneralUtility::makeInstance(ConnectionManager::class);
71
    }
72
73
    /**
74
     * Index action, shows an overview of available index maintenance operations.
75
     *
76
     * @return void
77
     */
78
    public function indexAction()
79
    {
80
        if ($this->selectedSite === null || empty($this->solrConnectionManager->getConnectionsBySite($this->selectedSite))) {
81
            $this->view->assign('can_not_proceed', true);
82
        }
83
    }
84
85
    /**
86
     * Empties the site's indexes.
87
     *
88
     * @return void
89
     */
90
    public function emptyIndexAction()
91
    {
92
        $siteHash = $this->selectedSite->getSiteHash();
93
94
        try {
95
            $affectedCores = [];
96
            $solrServers = $this->solrConnectionManager->getConnectionsBySite($this->selectedSite);
97
            foreach ($solrServers as $solrServer) {
98
                $writeService = $solrServer->getWriteService();
99
                /* @var $solrServer SolrConnection */
100
                $writeService->deleteByQuery('siteHash:' . $siteHash);
101
                $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

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