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 ApacheSolrForTypo3\Solr\Utility\ManagedResourcesUtility; |
28
|
|
|
use TYPO3\CMS\Backend\Template\ModuleTemplate; |
29
|
|
|
use TYPO3\CMS\Core\Messaging\FlashMessage; |
30
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
31
|
|
|
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Manage Synonyms and Stop words in Backend Module |
35
|
|
|
* @property \TYPO3\CMS\Extbase\Mvc\Web\Response $response |
36
|
|
|
*/ |
37
|
|
|
class CoreOptimizationModuleController extends AbstractModuleController |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* Set up the doc header properly here |
41
|
|
|
* |
42
|
|
|
* @param ViewInterface $view |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
protected function initializeView(ViewInterface $view) |
46
|
|
|
{ |
47
|
|
|
parent::initializeView($view); |
48
|
|
|
|
49
|
|
|
$this->generateCoreSelectorMenuUsingPageTree(); |
50
|
|
|
/* @var ModuleTemplate $module */ // holds the state of chosen tab |
51
|
|
|
$module = $this->objectManager->get(ModuleTemplate::class); |
52
|
|
|
$coreOptimizationTabs = $module->getDynamicTabMenu([], 'coreOptimization'); |
53
|
|
|
$this->view->assign('tabs', $coreOptimizationTabs); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Gets synonyms and stopwords for the currently selected core |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function indexAction() |
62
|
|
|
{ |
63
|
|
|
if ($this->selectedSolrCoreConnection === null) { |
64
|
|
|
$this->view->assign('can_not_proceed', true); |
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$synonyms = []; |
69
|
|
|
$rawSynonyms = $this->selectedSolrCoreConnection->getSynonyms(); |
70
|
|
|
foreach ($rawSynonyms as $baseWord => $synonymList) { |
71
|
|
|
$synonyms[$baseWord] = implode(', ', $synonymList); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$stopWords = $this->selectedSolrCoreConnection->getStopWords(); |
75
|
|
|
$this->view->assignMultiple([ |
76
|
|
|
'synonyms' => $synonyms, |
77
|
|
|
'stopWords' => implode(PHP_EOL, $stopWords), |
78
|
|
|
'stopWordsCount' => count($stopWords) |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Add synonyms to selected core |
84
|
|
|
* |
85
|
|
|
* @param string $baseWord |
86
|
|
|
* @param string $synonyms |
87
|
|
|
* @param bool $overrideExisting |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function addSynonymsAction(string $baseWord, string $synonyms, $overrideExisting) |
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 = mb_strtolower($baseWord); |
100
|
|
|
$synonyms = mb_strtolower($synonyms); |
101
|
|
|
|
102
|
|
|
if ($overrideExisting && $this->selectedSolrCoreConnection->getSynonyms($baseWord)) { |
103
|
|
|
$this->selectedSolrCoreConnection->deleteSynonym($baseWord); |
104
|
|
|
} |
105
|
|
|
$this->selectedSolrCoreConnection->addSynonym( |
106
|
|
|
$baseWord, |
107
|
|
|
GeneralUtility::trimExplode(',', $synonyms, true) |
108
|
|
|
); |
109
|
|
|
$this->selectedSolrCoreConnection->reloadCore(); |
110
|
|
|
|
111
|
|
|
$this->addFlashMessage( |
112
|
|
|
'"' . $synonyms . '" added as synonyms for base word "' . $baseWord . '"' |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->redirect('index'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $fileFormat |
121
|
|
|
* @return void |
122
|
|
|
*/ |
123
|
|
|
public function exportStopWordsAction($fileFormat = 'txt') |
124
|
|
|
{ |
125
|
|
|
$this->exportFile( |
126
|
|
|
implode(PHP_EOL, $this->selectedSolrCoreConnection->getStopWords()), |
127
|
|
|
'stopwords', |
128
|
|
|
$fileFormat |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Exports synonyms to a download file. |
134
|
|
|
* |
135
|
|
|
* @param string $fileFormat |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function exportSynonymsAction($fileFormat = 'txt') |
139
|
|
|
{ |
140
|
|
|
$synonyms = $this->selectedSolrCoreConnection->getSynonyms(); |
141
|
|
|
return $this->exportFile(ManagedResourcesUtility::exportSynonymsToTxt($synonyms), 'synonyms', $fileFormat); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param array $synonymFileUpload |
146
|
|
|
* @param bool $overrideExisting |
147
|
|
|
* @param bool $deleteSynonymsBefore |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
public function importSynonymListAction(array $synonymFileUpload, $overrideExisting, $deleteSynonymsBefore) |
151
|
|
|
{ |
152
|
|
|
if ($deleteSynonymsBefore) { |
153
|
|
|
$this->deleteAllSynonyms(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$fileLines = ManagedResourcesUtility::importSynonymsFromPlainTextContents($synonymFileUpload); |
157
|
|
|
$synonymCount = 0; |
158
|
|
|
foreach ($fileLines as $baseWord => $synonyms) { |
159
|
|
|
if (!isset($baseWord) || empty($synonyms)) { |
160
|
|
|
continue; |
161
|
|
|
} |
162
|
|
|
$this->deleteExistingSynonym($overrideExisting, $deleteSynonymsBefore, $baseWord); |
163
|
|
|
$this->selectedSolrCoreConnection->addSynonym( |
164
|
|
|
$baseWord, |
165
|
|
|
$synonyms |
166
|
|
|
); |
167
|
|
|
$synonymCount++; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$this->selectedSolrCoreConnection->reloadCore(); |
171
|
|
|
$this->addFlashMessage( |
172
|
|
|
$synonymCount . ' synonyms imported.' |
173
|
|
|
); |
174
|
|
|
$this->redirect('index'); |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param array $stopwordsFileUpload |
180
|
|
|
* @param bool $replaceStopwords |
181
|
|
|
* @return void |
182
|
|
|
*/ |
183
|
|
|
public function importStopWordListAction(array $stopwordsFileUpload, $replaceStopwords) |
184
|
|
|
{ |
185
|
|
|
$this->saveStopWordsAction( |
186
|
|
|
ManagedResourcesUtility::importStopwordsFromPlainTextContents($stopwordsFileUpload), |
187
|
|
|
$replaceStopwords |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Delete complete synonym list |
193
|
|
|
* |
194
|
|
|
* @return void |
195
|
|
|
*/ |
196
|
|
|
public function deleteAllSynonymsAction() |
197
|
|
|
{ |
198
|
|
|
$allSynonymsCouldBeDeleted = $this->deleteAllSynonyms(); |
199
|
|
|
|
200
|
|
|
$reloadResponse = $this->selectedSolrCoreConnection->reloadCore(); |
201
|
|
|
|
202
|
|
|
if ($allSynonymsCouldBeDeleted |
203
|
|
|
&& $reloadResponse->getHttpStatus() == 200 |
204
|
|
|
) { |
205
|
|
|
$this->addFlashMessage( |
206
|
|
|
'All synonym removed.' |
207
|
|
|
); |
208
|
|
|
} else { |
209
|
|
|
$this->addFlashMessage( |
210
|
|
|
'Failed to remove all synonyms.', |
211
|
|
|
'An error occurred', |
212
|
|
|
FlashMessage::ERROR |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
$this->redirect('index'); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Deletes a synonym mapping by its base word. |
220
|
|
|
* |
221
|
|
|
* @param string $baseWord Synonym mapping base word |
222
|
|
|
*/ |
223
|
|
|
public function deleteSynonymsAction($baseWord) |
224
|
|
|
{ |
225
|
|
|
$deleteResponse = $this->selectedSolrCoreConnection->deleteSynonym($baseWord); |
226
|
|
|
$reloadResponse = $this->selectedSolrCoreConnection->reloadCore(); |
227
|
|
|
|
228
|
|
|
if ($deleteResponse->getHttpStatus() == 200 |
229
|
|
|
&& $reloadResponse->getHttpStatus() == 200 |
230
|
|
|
) { |
231
|
|
|
$this->addFlashMessage( |
232
|
|
|
'Synonym removed.' |
233
|
|
|
); |
234
|
|
|
} else { |
235
|
|
|
$this->addFlashMessage( |
236
|
|
|
'Failed to remove synonym.', |
237
|
|
|
'An error occurred', |
238
|
|
|
FlashMessage::ERROR |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
$this->redirect('index'); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Saves the edited stop word list to Solr |
247
|
|
|
* |
248
|
|
|
* @param string $stopWords |
249
|
|
|
* @param bool $replaceStopwords |
250
|
|
|
* @return void |
251
|
|
|
*/ |
252
|
|
|
public function saveStopWordsAction(string $stopWords, $replaceStopwords = true) |
253
|
|
|
{ |
254
|
|
|
// lowercase stopword before saving because terms get lowercased before stopword filtering |
255
|
|
|
$newStopWords = mb_strtolower($stopWords); |
256
|
|
|
$newStopWords = GeneralUtility::trimExplode("\n", $newStopWords, true); |
257
|
|
|
$oldStopWords = $this->selectedSolrCoreConnection->getStopWords(); |
258
|
|
|
|
259
|
|
|
if ($replaceStopwords) { |
260
|
|
|
$removedStopWords = array_diff($oldStopWords, $newStopWords); |
261
|
|
|
$wordsRemoved = $this->removeStopsWordsFromIndex($removedStopWords); |
262
|
|
|
} else { |
263
|
|
|
$wordsRemoved = true; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
$wordsAdded = true; |
267
|
|
|
$addedStopWords = array_diff($newStopWords, $oldStopWords); |
268
|
|
|
if (!empty($addedStopWords)) { |
269
|
|
|
$wordsAddedResponse = $this->selectedSolrCoreConnection->addStopWords($addedStopWords); |
270
|
|
|
$wordsAdded = ($wordsAddedResponse->getHttpStatus() == 200); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
$reloadResponse = $this->selectedSolrCoreConnection->reloadCore(); |
274
|
|
|
if ($wordsRemoved && $wordsAdded && $reloadResponse->getHttpStatus() == 200) { |
275
|
|
|
$this->addFlashMessage( |
276
|
|
|
'Stop Words Updated.' |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
$this->redirect('index'); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @param string $content |
285
|
|
|
* @param string $type |
286
|
|
|
* @param string $fileExtension |
287
|
|
|
* @return string |
288
|
|
|
*/ |
289
|
|
|
protected function exportFile($content, $type = 'synonyms', $fileExtension = 'txt') : string |
290
|
|
|
{ |
291
|
|
|
$this->response->setHeader('Content-type', 'text/plain', true); |
292
|
|
|
$this->response->setHeader('Cache-control', 'public', true); |
293
|
|
|
$this->response->setHeader('Content-Description', 'File transfer', true); |
294
|
|
|
$this->response->setHeader( |
295
|
|
|
'Content-disposition', |
296
|
|
|
'attachment; filename =' . $type . '_' . |
297
|
|
|
$this->selectedSolrCoreConnection->getCoreName() . '.' . $fileExtension, |
298
|
|
|
true |
299
|
|
|
); |
300
|
|
|
return $content; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Delete complete synonym list form solr |
305
|
|
|
* |
306
|
|
|
* @return bool |
307
|
|
|
*/ |
308
|
|
|
protected function deleteAllSynonyms() : bool |
309
|
|
|
{ |
310
|
|
|
$synonyms = $this->selectedSolrCoreConnection->getSynonyms(); |
311
|
|
|
$allSynonymsCouldBeDeleted = true; |
312
|
|
|
|
313
|
|
|
foreach ($synonyms as $baseWord => $synonym) { |
314
|
|
|
$deleteResponse = $this->selectedSolrCoreConnection->deleteSynonym($baseWord); |
315
|
|
|
$allSynonymsCouldBeDeleted = $allSynonymsCouldBeDeleted && $deleteResponse->getHttpStatus() == 200; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
return $allSynonymsCouldBeDeleted; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @param $stopwordsToRemove |
323
|
|
|
* @return bool |
324
|
|
|
*/ |
325
|
|
|
protected function removeStopsWordsFromIndex($stopwordsToRemove) : bool |
326
|
|
|
{ |
327
|
|
|
$wordsRemoved = true; |
328
|
|
|
foreach ($stopwordsToRemove as $word) { |
329
|
|
|
$response = $this->selectedSolrCoreConnection->deleteStopWord($word); |
330
|
|
|
if ($response->getHttpStatus() != 200) { |
331
|
|
|
$wordsRemoved = false; |
332
|
|
|
$this->addFlashMessage( |
333
|
|
|
'Failed to remove stop word "' . $word . '".', |
334
|
|
|
'An error occurred', |
335
|
|
|
FlashMessage::ERROR |
336
|
|
|
); |
337
|
|
|
break; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
return $wordsRemoved; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Delete synonym entry if selceted before |
346
|
|
|
* @param bool $overrideExisting |
347
|
|
|
* @param bool $deleteSynonymsBefore |
348
|
|
|
* @param string $baseWord |
349
|
|
|
*/ |
350
|
|
|
protected function deleteExistingSynonym($overrideExisting, $deleteSynonymsBefore, $baseWord) |
351
|
|
|
{ |
352
|
|
|
if (!$deleteSynonymsBefore && |
353
|
|
|
$overrideExisting && |
354
|
|
|
$this->selectedSolrCoreConnection->getSynonyms($baseWord) |
355
|
|
|
) { |
356
|
|
|
$this->selectedSolrCoreConnection->deleteSynonym($baseWord); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|