Passed
Push — task/2976_TYPO3.11_compatibili... ( 9f9205...0f123d )
by Rafael
27:04
created

SolrAdminService::reloadCore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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());
0 ignored issues
show
Bug introduced by
It seems like $response->getRawResponse() can also be of type null; however, parameter $data of simplexml_load_string() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

210
            $solrconfigXml = simplexml_load_string(/** @scrutinizer ignore-type */ $response->getRawResponse());
Loading history...
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()
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
        $response = $this->reloadCoreByName($this->getPrimaryEndpoint()->getCore());
241 11
        return $response;
242
    }
243
244
    /**
245
     * Reloads a core of the connection by a given corename.
246
     *
247
     * @param string $coreName
248
     * @return ResponseAdapter
249
     */
250 11
    public function reloadCoreByName($coreName)
251
    {
252 11
        $coreAdminReloadUrl = $this->_constructUrl(self::CORES_SERVLET) . '?action=reload&core=' . $coreName;
253 11
        $response = $this->_sendRawGet($coreAdminReloadUrl);
254 11
        return $response;
255
    }
256
257
    /**
258
     * Get the configured schema for the current core.
259
     *
260
     * @return Schema
261
     */
262 14
    public function getSchema()
263
    {
264 14
        if ($this->schema !== null) {
265 2
            return $this->schema;
266
        }
267 14
        $response = $this->_sendRawGet($this->_constructUrl(self::SCHEMA_SERVLET));
268
269 14
        $this->schema = $this->schemaParser->parseJson($response->getRawResponse());
0 ignored issues
show
Bug introduced by
It seems like $response->getRawResponse() can also be of type null; however, parameter $jsonString of ApacheSolrForTypo3\Solr\...hemaParser::parseJson() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

269
        $this->schema = $this->schemaParser->parseJson(/** @scrutinizer ignore-type */ $response->getRawResponse());
Loading history...
270 13
        return $this->schema;
271
    }
272
273
    /**
274
     * Get currently configured synonyms
275
     *
276
     * @param string $baseWord If given a base word, retrieves the synonyms for that word only
277
     * @return array
278
     */
279 7
    public function getSynonyms($baseWord = '')
280
    {
281 7
        $this->initializeSynonymsUrl();
282 7
        $synonymsUrl = $this->_synonymsUrl;
283 7
        if (!empty($baseWord)) {
284 7
            $synonymsUrl .= '/' . rawurlencode(rawurlencode($baseWord));
285
        }
286
287 7
        $response = $this->_sendRawGet($synonymsUrl);
288 7
        return $this->synonymParser->parseJson($baseWord, $response->getRawResponse());
0 ignored issues
show
Bug introduced by
It seems like $response->getRawResponse() can also be of type null; however, parameter $jsonString of ApacheSolrForTypo3\Solr\...onymParser::parseJson() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

288
        return $this->synonymParser->parseJson($baseWord, /** @scrutinizer ignore-type */ $response->getRawResponse());
Loading history...
289
    }
290
291
    /**
292
     * Add list of synonyms for base word to managed synonyms map
293
     *
294
     * @param string $baseWord
295
     * @param array $synonyms
296
     *
297
     * @return ResponseAdapter
298
     */
299 7
    public function addSynonym(string $baseWord, array $synonyms): ResponseAdapter
300
    {
301 7
        $this->initializeSynonymsUrl();
302 7
        $json = $this->synonymParser->toJson($baseWord, $synonyms);
303 7
        return $this->_sendRawPost($this->_synonymsUrl, $json, 'application/json');
304
    }
305
306
    /**
307
     * Remove a synonym from the synonyms map
308
     *
309
     * @param string $baseWord
310
     * @return ResponseAdapter
311
     */
312 7
    public function deleteSynonym(string $baseWord): ResponseAdapter
313
    {
314 7
        $this->initializeSynonymsUrl();
315 7
        return $this->_sendRawDelete($this->_synonymsUrl . '/' . rawurlencode(rawurlencode($baseWord)));
316
    }
317
318
    /**
319
     * Get currently configured stop words
320
     *
321
     * @return array
322
     */
323 3
    public function getStopWords(): array
324
    {
325 3
        $this->initializeStopWordsUrl();
326 3
        $response = $this->_sendRawGet($this->_stopWordsUrl);
327 3
        return $this->stopWordParser->parseJson($response->getRawResponse());
0 ignored issues
show
Bug introduced by
It seems like $response->getRawResponse() can also be of type null; however, parameter $jsonString of ApacheSolrForTypo3\Solr\...WordParser::parseJson() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

327
        return $this->stopWordParser->parseJson(/** @scrutinizer ignore-type */ $response->getRawResponse());
Loading history...
328
    }
329
330
    /**
331
     * Adds stop words to the managed stop word list
332
     *
333
     * @param array|string $stopWords string for a single word, array for multiple words
334
     * @return ResponseAdapter
335
     * @throws InvalidArgumentException If $stopWords is empty
336
     */
337 2
    public function addStopWords($stopWords): ResponseAdapter
338
    {
339 2
        $this->initializeStopWordsUrl();
340 2
        $json = $this->stopWordParser->toJson($stopWords);
341 2
        return $this->_sendRawPost($this->_stopWordsUrl, $json, 'application/json');
342
    }
343
344
    /**
345
     * Deletes a words from the managed stop word list
346
     *
347
     * @param string $stopWord stop word to delete
348
     * @return ResponseAdapter
349
     * @throws InvalidArgumentException If $stopWords is empty
350
     */
351 2
    public function deleteStopWord($stopWord)
352
    {
353 2
        $this->initializeStopWordsUrl();
354 2
        if (empty($stopWord)) {
355
            throw new InvalidArgumentException('Must provide stop word.');
356
        }
357
358 2
        return $this->_sendRawDelete($this->_stopWordsUrl . '/' . rawurlencode(rawurlencode($stopWord)));
359
    }
360
361
    /**
362
     * @return void
363
     */
364 7
    protected function initializeSynonymsUrl()
365
    {
366 7
        if (trim($this->_synonymsUrl) !== '') {
367 7
            return;
368
        }
369 7
        $this->_synonymsUrl = $this->_constructUrl(self::SYNONYMS_SERVLET) . $this->getSchema()->getManagedResourceId();
370 7
    }
371
372
    /**
373
     * @return void
374
     */
375 3
    protected function initializeStopWordsUrl()
376
    {
377 3
        if (trim($this->_stopWordsUrl) !== '') {
378 2
            return;
379
        }
380
381 3
        $this->_stopWordsUrl = $this->_constructUrl(self::STOPWORDS_SERVLET) . $this->getSchema()->getManagedResourceId();
382 3
    }
383
}
384