1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\System\Solr\Service; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2009-2017 Timo Hund <[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\System\Configuration\TypoScriptConfiguration; |
28
|
|
|
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager; |
29
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\Parser\SchemaParser; |
30
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\Parser\StopWordParser; |
31
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\Parser\SynonymParser; |
32
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter; |
33
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\Schema\Schema; |
34
|
|
|
use InvalidArgumentException; |
35
|
|
|
use Solarium\Client; |
36
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Class SolrAdminService |
40
|
|
|
*/ |
41
|
|
|
class SolrAdminService extends AbstractSolrService |
42
|
|
|
{ |
43
|
|
|
const PLUGINS_SERVLET = 'admin/plugins'; |
44
|
|
|
const LUKE_SERVLET = 'admin/luke'; |
45
|
|
|
const SYSTEM_SERVLET = 'admin/system'; |
46
|
|
|
const CORES_SERVLET = '../admin/cores'; |
47
|
|
|
const FILE_SERVLET = 'admin/file'; |
48
|
|
|
const SCHEMA_SERVLET = 'schema'; |
49
|
|
|
const SYNONYMS_SERVLET = 'schema/analysis/synonyms/'; |
50
|
|
|
const STOPWORDS_SERVLET = 'schema/analysis/stopwords/'; |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $lukeData = []; |
57
|
|
|
|
58
|
|
|
protected $systemData = null; |
59
|
|
|
|
60
|
|
|
protected $pluginsData = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string|null |
64
|
|
|
*/ |
65
|
|
|
protected $solrconfigName; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var SchemaParser |
69
|
|
|
*/ |
70
|
|
|
protected $schemaParser = null; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var Schema |
74
|
|
|
*/ |
75
|
|
|
protected $schema; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var string |
79
|
|
|
*/ |
80
|
|
|
protected $_synonymsUrl; |
81
|
|
|
/** |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
protected $_stopWordsUrl; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @var SynonymParser |
88
|
|
|
*/ |
89
|
|
|
protected $synonymParser = null; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @var StopWordParser |
93
|
|
|
*/ |
94
|
|
|
protected $stopWordParser = null; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Constructor |
98
|
|
|
* |
99
|
|
|
* @param Client $client |
100
|
|
|
* @param TypoScriptConfiguration|null $typoScriptConfiguration |
101
|
|
|
* @param SolrLogManager|null $logManager |
102
|
|
|
* @param SynonymParser|null $synonymParser |
103
|
|
|
* @param StopWordParser|null $stopWordParser |
104
|
|
|
* @param SchemaParser|null $schemaParser |
105
|
|
|
*/ |
106
|
30 |
|
public function __construct( |
107
|
|
|
Client $client, |
108
|
|
|
TypoScriptConfiguration $typoScriptConfiguration = null, |
109
|
|
|
SolrLogManager $logManager = null, |
110
|
|
|
SynonymParser $synonymParser = null, |
111
|
|
|
StopWordParser $stopWordParser = null, |
112
|
|
|
SchemaParser $schemaParser = null |
113
|
|
|
) |
114
|
|
|
{ |
115
|
30 |
|
parent::__construct($client, $typoScriptConfiguration); |
116
|
|
|
|
117
|
30 |
|
$this->synonymParser = $synonymParser ?? GeneralUtility::makeInstance(SynonymParser::class); |
118
|
30 |
|
$this->stopWordParser = $stopWordParser ?? GeneralUtility::makeInstance(StopWordParser::class); |
119
|
30 |
|
$this->schemaParser = $schemaParser ?? GeneralUtility::makeInstance(SchemaParser::class); |
120
|
30 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Call the /admin/system servlet and retrieve system information about Solr |
124
|
|
|
* |
125
|
|
|
* @return ResponseAdapter |
126
|
|
|
*/ |
127
|
7 |
|
public function system() |
128
|
|
|
{ |
129
|
7 |
|
return $this->_sendRawGet($this->_constructUrl(self::SYSTEM_SERVLET, ['wt' => 'json'])); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Gets information about the plugins installed in Solr |
134
|
|
|
* |
135
|
|
|
* @return array A nested array of plugin data. |
136
|
|
|
*/ |
137
|
5 |
|
public function getPluginsInformation() |
138
|
|
|
{ |
139
|
5 |
|
if (count($this->pluginsData) == 0) { |
140
|
5 |
|
$url = $this->_constructUrl(self::PLUGINS_SERVLET, ['wt' => 'json']); |
141
|
5 |
|
$pluginsInformation = $this->_sendRawGet($url); |
142
|
|
|
|
143
|
|
|
// access a random property to trigger response parsing |
144
|
5 |
|
$pluginsInformation->responseHeader; |
145
|
5 |
|
$this->pluginsData = $pluginsInformation; |
146
|
|
|
} |
147
|
|
|
|
148
|
5 |
|
return $this->pluginsData; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* get field meta data for the index |
153
|
|
|
* |
154
|
|
|
* @param int $numberOfTerms Number of top terms to fetch for each field |
155
|
|
|
* @return \stdClass |
156
|
|
|
*/ |
157
|
|
|
public function getFieldsMetaData($numberOfTerms = 0) |
158
|
|
|
{ |
159
|
|
|
return $this->getLukeMetaData($numberOfTerms)->fields; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Retrieves meta data about the index from the luke request handler |
164
|
|
|
* |
165
|
|
|
* @param int $numberOfTerms Number of top terms to fetch for each field |
166
|
|
|
* @return ResponseAdapter Index meta data |
167
|
|
|
*/ |
168
|
1 |
|
public function getLukeMetaData($numberOfTerms = 0) |
169
|
|
|
{ |
170
|
1 |
|
if (!isset($this->lukeData[$numberOfTerms])) { |
171
|
1 |
|
$lukeUrl = $this->_constructUrl( |
172
|
1 |
|
self::LUKE_SERVLET, ['numTerms' => $numberOfTerms, 'wt' => 'json', 'fl' => '*'] |
173
|
|
|
); |
174
|
|
|
|
175
|
1 |
|
$this->lukeData[$numberOfTerms] = $this->_sendRawGet($lukeUrl); |
176
|
|
|
} |
177
|
|
|
|
178
|
1 |
|
return $this->lukeData[$numberOfTerms]; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Gets information about the Solr server |
183
|
|
|
* |
184
|
|
|
* @return ResponseAdapter |
185
|
|
|
*/ |
186
|
7 |
|
public function getSystemInformation() |
187
|
|
|
{ |
188
|
7 |
|
if (empty($this->systemData)) { |
189
|
7 |
|
$systemInformation = $this->system(); |
190
|
|
|
|
191
|
|
|
// access a random property to trigger response parsing |
192
|
7 |
|
$systemInformation->responseHeader; |
193
|
7 |
|
$this->systemData = $systemInformation; |
194
|
|
|
} |
195
|
|
|
|
196
|
7 |
|
return $this->systemData; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Gets the name of the solrconfig.xml file installed and in use on the Solr |
201
|
|
|
* server. |
202
|
|
|
* |
203
|
|
|
* @return string Name of the active solrconfig.xml |
204
|
|
|
*/ |
205
|
4 |
|
public function getSolrconfigName() |
206
|
|
|
{ |
207
|
4 |
|
if (is_null($this->solrconfigName)) { |
208
|
4 |
|
$solrconfigXmlUrl = $this->_constructUrl(self::FILE_SERVLET, ['file' => 'solrconfig.xml']); |
209
|
4 |
|
$response = $this->_sendRawGet($solrconfigXmlUrl); |
210
|
4 |
|
$solrconfigXml = simplexml_load_string($response->getRawResponse()); |
|
|
|
|
211
|
4 |
|
if ($solrconfigXml === false) { |
212
|
1 |
|
throw new InvalidArgumentException('No valid xml response from schema file: ' . $solrconfigXmlUrl); |
213
|
|
|
} |
214
|
3 |
|
$this->solrconfigName = (string)$solrconfigXml->attributes()->name; |
215
|
|
|
} |
216
|
|
|
|
217
|
3 |
|
return $this->solrconfigName; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Gets the Solr server's version number. |
222
|
|
|
* |
223
|
|
|
* @return string Solr version number |
224
|
|
|
*/ |
225
|
5 |
|
public function getSolrServerVersion(): string |
226
|
|
|
{ |
227
|
5 |
|
$systemInformation = $this->getSystemInformation(); |
228
|
|
|
// don't know why $systemInformation->lucene->solr-spec-version won't work |
229
|
5 |
|
$luceneInformation = (array)$systemInformation->lucene; |
230
|
5 |
|
return $luceneInformation['solr-spec-version'] ?? ''; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Reloads the current core |
235
|
|
|
* |
236
|
|
|
* @return ResponseAdapter |
237
|
|
|
*/ |
238
|
11 |
|
public function reloadCore() |
239
|
|
|
{ |
240
|
11 |
|
return $this->reloadCoreByName($this->getPrimaryEndpoint()->getCore()); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Reloads a core of the connection by a given corename. |
245
|
|
|
* |
246
|
|
|
* @param string $coreName |
247
|
|
|
* @return ResponseAdapter |
248
|
|
|
*/ |
249
|
11 |
|
public function reloadCoreByName($coreName) |
250
|
|
|
{ |
251
|
11 |
|
$coreAdminReloadUrl = $this->_constructUrl(self::CORES_SERVLET) . '?action=reload&core=' . $coreName; |
252
|
11 |
|
return $this->_sendRawGet($coreAdminReloadUrl); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get the configured schema for the current core. |
257
|
|
|
* |
258
|
|
|
* @return Schema |
259
|
|
|
*/ |
260
|
14 |
|
public function getSchema() |
261
|
|
|
{ |
262
|
14 |
|
if ($this->schema !== null) { |
263
|
2 |
|
return $this->schema; |
264
|
|
|
} |
265
|
14 |
|
$response = $this->_sendRawGet($this->_constructUrl(self::SCHEMA_SERVLET)); |
266
|
|
|
|
267
|
14 |
|
$this->schema = $this->schemaParser->parseJson($response->getRawResponse()); |
|
|
|
|
268
|
13 |
|
return $this->schema; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Get currently configured synonyms |
273
|
|
|
* |
274
|
|
|
* @param string $baseWord If given a base word, retrieves the synonyms for that word only |
275
|
|
|
* @return array |
276
|
|
|
*/ |
277
|
7 |
|
public function getSynonyms($baseWord = '') |
278
|
|
|
{ |
279
|
7 |
|
$this->initializeSynonymsUrl(); |
280
|
7 |
|
$synonymsUrl = $this->_synonymsUrl; |
281
|
7 |
|
if (!empty($baseWord)) { |
282
|
7 |
|
$synonymsUrl .= '/' . rawurlencode(rawurlencode($baseWord)); |
283
|
|
|
} |
284
|
|
|
|
285
|
7 |
|
$response = $this->_sendRawGet($synonymsUrl); |
286
|
7 |
|
return $this->synonymParser->parseJson($baseWord, $response->getRawResponse()); |
|
|
|
|
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Add list of synonyms for base word to managed synonyms map |
291
|
|
|
* |
292
|
|
|
* @param string $baseWord |
293
|
|
|
* @param array $synonyms |
294
|
|
|
* |
295
|
|
|
* @return ResponseAdapter |
296
|
|
|
*/ |
297
|
7 |
|
public function addSynonym(string $baseWord, array $synonyms): ResponseAdapter |
298
|
|
|
{ |
299
|
7 |
|
$this->initializeSynonymsUrl(); |
300
|
7 |
|
$json = $this->synonymParser->toJson($baseWord, $synonyms); |
301
|
7 |
|
return $this->_sendRawPost($this->_synonymsUrl, $json, 'application/json'); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Remove a synonym from the synonyms map |
306
|
|
|
* |
307
|
|
|
* @param string $baseWord |
308
|
|
|
* @return ResponseAdapter |
309
|
|
|
*/ |
310
|
7 |
|
public function deleteSynonym(string $baseWord): ResponseAdapter |
311
|
|
|
{ |
312
|
7 |
|
$this->initializeSynonymsUrl(); |
313
|
7 |
|
return $this->_sendRawDelete($this->_synonymsUrl . '/' . rawurlencode(rawurlencode($baseWord))); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Get currently configured stop words |
318
|
|
|
* |
319
|
|
|
* @return array |
320
|
|
|
*/ |
321
|
3 |
|
public function getStopWords(): array |
322
|
|
|
{ |
323
|
3 |
|
$this->initializeStopWordsUrl(); |
324
|
3 |
|
$response = $this->_sendRawGet($this->_stopWordsUrl); |
325
|
3 |
|
return $this->stopWordParser->parseJson($response->getRawResponse()); |
|
|
|
|
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Adds stop words to the managed stop word list |
330
|
|
|
* |
331
|
|
|
* @param array|string $stopWords string for a single word, array for multiple words |
332
|
|
|
* @return ResponseAdapter |
333
|
|
|
* @throws InvalidArgumentException If $stopWords is empty |
334
|
|
|
*/ |
335
|
2 |
|
public function addStopWords($stopWords): ResponseAdapter |
336
|
|
|
{ |
337
|
2 |
|
$this->initializeStopWordsUrl(); |
338
|
2 |
|
$json = $this->stopWordParser->toJson($stopWords); |
339
|
2 |
|
return $this->_sendRawPost($this->_stopWordsUrl, $json, 'application/json'); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Deletes a words from the managed stop word list |
344
|
|
|
* |
345
|
|
|
* @param string $stopWord stop word to delete |
346
|
|
|
* @return ResponseAdapter |
347
|
|
|
* @throws InvalidArgumentException If $stopWords is empty |
348
|
|
|
*/ |
349
|
2 |
|
public function deleteStopWord($stopWord) |
350
|
|
|
{ |
351
|
2 |
|
$this->initializeStopWordsUrl(); |
352
|
2 |
|
if (empty($stopWord)) { |
353
|
|
|
throw new InvalidArgumentException('Must provide stop word.'); |
354
|
|
|
} |
355
|
|
|
|
356
|
2 |
|
return $this->_sendRawDelete($this->_stopWordsUrl . '/' . rawurlencode(rawurlencode($stopWord))); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @return void |
361
|
|
|
*/ |
362
|
7 |
|
protected function initializeSynonymsUrl() |
363
|
|
|
{ |
364
|
7 |
|
if (trim($this->_synonymsUrl) !== '') { |
365
|
7 |
|
return; |
366
|
|
|
} |
367
|
7 |
|
$this->_synonymsUrl = $this->_constructUrl(self::SYNONYMS_SERVLET) . $this->getSchema()->getManagedResourceId(); |
368
|
7 |
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @return void |
372
|
|
|
*/ |
373
|
3 |
|
protected function initializeStopWordsUrl() |
374
|
|
|
{ |
375
|
3 |
|
if (trim($this->_stopWordsUrl) !== '') { |
376
|
2 |
|
return; |
377
|
|
|
} |
378
|
|
|
|
379
|
3 |
|
$this->_stopWordsUrl = $this->_constructUrl(self::STOPWORDS_SERVLET) . $this->getSchema()->getManagedResourceId(); |
380
|
3 |
|
} |
381
|
|
|
} |
382
|
|
|
|