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