Passed
Pull Request — master (#1308)
by Timo
22:26
created

CoreOptimizationModuleController   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 0
loc 153
ccs 0
cts 104
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeView() 0 10 1
A indexAction() 0 23 3
B addSynonymsAction() 0 25 3
A deleteSynonymsAction() 0 21 3
C saveStopWordsAction() 0 38 7
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Controller\Backend\Search;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2010-2017 dkd Internet Service GmbH <[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 TYPO3\CMS\Backend\Template\ModuleTemplate;
28
use TYPO3\CMS\Core\Messaging\FlashMessage;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
31
32
/**
33
 * Manage Synonyms and Stop words in Backend Module
34
 */
35
class CoreOptimizationModuleController extends AbstractModuleController
36
{
37
    /**
38
     * Set up the doc header properly here
39
     *
40
     * @param ViewInterface $view
41
     * @return void
42
     */
43
    protected function initializeView(ViewInterface $view)
44
    {
45
        parent::initializeView($view);
46
47
        $this->generateCoreSelectorMenuUsingPageTree();
48
        /* @var ModuleTemplate $module */ // holds the state of chosen tab
49
        $module = $this->objectManager->get(ModuleTemplate::class);
50
        $coreOptimizationTabs = $module->getDynamicTabMenu([], 'coreOptimization');
51
        $this->view->assign('tabs', $coreOptimizationTabs);
52
    }
53
54
    /**
55
     * Gets synonyms and stopwords for the currently selected core
56
     *
57
     * @return void
58
     */
59
    public function indexAction()
60
    {
61
        if ($this->selectedSolrCoreConnection === null) {
62
            $this->view->assignMultiple([
63
                'can_not_proceed' => true,
64
                'pageUID' => $this->selectedPageUID
65
            ]);
66
            return;
67
        }
68
69
        $synonyms = [];
70
        $rawSynonyms = $this->selectedSolrCoreConnection->getSynonyms();
71
        foreach ($rawSynonyms as $baseWord => $synonymList) {
72
            $synonyms[$baseWord] = implode(', ', $synonymList);
73
        }
74
75
        $stopWords = $this->selectedSolrCoreConnection->getStopWords();
76
        $this->view->assignMultiple([
77
            'synonyms' => $synonyms,
78
            'stopWords' => implode(PHP_EOL, $stopWords),
79
            'stopWordsCount' => count($stopWords)
80
        ]);
81
    }
82
83
    /**
84
     * Add synonyms to selected core
85
     *
86
     * @param string $baseWord
87
     * @param string $synonyms
88
     * @return void
89
     */
90
    public function addSynonymsAction(string $baseWord, string $synonyms)
91
    {
92
        if (empty($baseWord) || empty($synonyms)) {
93
            $this->addFlashMessage(
94
                'Please provide a base word and synonyms.',
95
                'Missing parameter',
96
                FlashMessage::ERROR
97
            );
98
        } else {
99
            $baseWord = $this->stringUtility->toLower($baseWord);
100
            $synonyms = $this->stringUtility->toLower($synonyms);
101
102
            $this->selectedSolrCoreConnection->addSynonym(
103
                $baseWord,
104
                GeneralUtility::trimExplode(',', $synonyms, true)
105
            );
106
            $this->selectedSolrCoreConnection->reloadCore();
107
108
            $this->addFlashMessage(
109
                '"' . $synonyms . '" added as synonyms for base word "' . $baseWord . '"'
110
            );
111
        }
112
113
        $this->redirect('index');
114
    }
115
116
    /**
117
     * Deletes a synonym mapping by its base word.
118
     *
119
     * @param string $baseWord Synonym mapping base word
120
     */
121
    public function deleteSynonymsAction($baseWord)
122
    {
123
        $deleteResponse = $this->selectedSolrCoreConnection->deleteSynonym($baseWord);
124
        $reloadResponse = $this->selectedSolrCoreConnection->reloadCore();
125
126
        if ($deleteResponse->getHttpStatus() == 200
127
            && $reloadResponse->getHttpStatus() == 200
128
        ) {
129
            $this->addFlashMessage(
130
                'Synonym removed.'
131
            );
132
        } else {
133
            $this->addFlashMessage(
134
                'Failed to remove synonym.',
135
                'An error occurred',
136
                FlashMessage::ERROR
137
            );
138
        }
139
140
        $this->redirect('index');
141
    }
142
143
    /**
144
     * Saves the edited stop word list to Solr
145
     *
146
     * @param string $stopWords
147
     * @return void
148
     */
149
    public function saveStopWordsAction(string $stopWords)
150
    {
151
        // lowercase stopword before saving because terms get lowercased before stopword filtering
152
        $newStopWords = $this->stringUtility->toLower($stopWords);
153
        $newStopWords = GeneralUtility::trimExplode("\n", $newStopWords, true);
154
        $oldStopWords = $this->selectedSolrCoreConnection->getStopWords();
155
156
        $wordsRemoved = true;
157
        $removedStopWords = array_diff($oldStopWords, $newStopWords);
158
        foreach ($removedStopWords as $word) {
159
            $response = $this->selectedSolrCoreConnection->deleteStopWord($word);
160
            if ($response->getHttpStatus() != 200) {
161
                $wordsRemoved = false;
162
                $this->addFlashMessage(
163
                    'Failed to remove stop word "' . $word . '".',
164
                    'An error occurred',
165
                    FlashMessage::ERROR
166
                );
167
                break;
168
            }
169
        }
170
171
        $wordsAdded = true;
172
        $addedStopWords = array_diff($newStopWords, $oldStopWords);
173
        if (!empty($addedStopWords)) {
174
            $wordsAddedResponse = $this->selectedSolrCoreConnection->addStopWords($addedStopWords);
175
            $wordsAdded = ($wordsAddedResponse->getHttpStatus() == 200);
176
        }
177
178
        $reloadResponse = $this->selectedSolrCoreConnection->reloadCore();
179
        if ($wordsRemoved && $wordsAdded && $reloadResponse->getHttpStatus() == 200) {
180
            $this->addFlashMessage(
181
                'Stop Words Updated.'
182
            );
183
        }
184
185
        $this->redirect('index');
186
    }
187
}
188