Passed
Pull Request — release-11.0.x (#2992)
by Rafael
29:19
created

SolrAdminService::getStopWords()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 5
cp 0.8
rs 10
cc 1
nc 1
nop 0
crap 1.008
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
 */
40
class SolrAdminService extends AbstractSolrService
41
{
42
    const PLUGINS_SERVLET = 'admin/plugins';
43
    const LUKE_SERVLET = 'admin/luke';
44
    const SYSTEM_SERVLET = 'admin/system';
45
    const CORES_SERVLET = '../admin/cores';
46
    const FILE_SERVLET = 'admin/file';
47
    const SCHEMA_SERVLET = 'schema';
48
    const SYNONYMS_SERVLET = 'schema/analysis/synonyms/';
49
    const STOPWORDS_SERVLET = 'schema/analysis/stopwords/';
50
51
52
    /**
53
     * @var array
54
     */
55
    protected $lukeData = [];
56
57
    protected $systemData = null;
58
59
    protected $pluginsData = [];
60
61
    /**
62
     * @var string|null
63
     */
64
    protected $solrconfigName;
65
66
    /**
67
     * @var SchemaParser
68
     */
69
    protected $schemaParser = null;
70
71
    /**
72
     * @var Schema
73
     */
74
    protected $schema;
75
76
    /**
77
     * @var string
78
     */
79
    protected $_synonymsUrl;
80
    /**
81
     * @var string
82
     */
83
    protected $_stopWordsUrl;
84
85
    /**
86
     * @var SynonymParser
87
     */
88
    protected $synonymParser = null;
89
90
    /**
91
     * @var StopWordParser
92
     */
93
    protected $stopWordParser = null;
94
95
    /**
96
     * Constructor
97
     *
98
     * @param Client $client
99
     * @param TypoScriptConfiguration|null $typoScriptConfiguration
100
     * @param SolrLogManager|null $logManager
101
     * @param SynonymParser|null $synonymParser
102
     * @param StopWordParser|null $stopWordParser
103
     * @param SchemaParser|null $schemaParser
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 3
    public function getPluginsInformation()
137
    {
138 3
        if (count($this->pluginsData) == 0) {
139 3
            $url = $this->_constructUrl(self::PLUGINS_SERVLET, ['wt' => 'json']);
140 3
            $pluginsInformation = $this->_sendRawGet($url);
141
142
            // access a random property to trigger response parsing
143 3
            $pluginsInformation->responseHeader;
144 3
            $this->pluginsData = $pluginsInformation;
145
        }
146
147 3
        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
    public function getLukeMetaData($numberOfTerms = 0)
168
    {
169
        if (!isset($this->lukeData[$numberOfTerms])) {
170
            $lukeUrl = $this->_constructUrl(
171
                self::LUKE_SERVLET, ['numTerms' => $numberOfTerms, 'wt' => 'json', 'fl' => '*']
172
            );
173
174
            $this->lukeData[$numberOfTerms] = $this->_sendRawGet($lukeUrl);
175
        }
176
177
        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 2
    public function getSolrconfigName()
205
    {
206 2
        if (is_null($this->solrconfigName)) {
207 2
            $solrconfigXmlUrl = $this->_constructUrl(self::FILE_SERVLET, ['file' => 'solrconfig.xml']);
208 2
            $response = $this->_sendRawGet($solrconfigXmlUrl);
209 2
            $solrconfigXml = simplexml_load_string($response->getRawResponse());
210 2
            if ($solrconfigXml === false) {
211 1
                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 3
    public function getSolrServerVersion()
225
    {
226 3
        $systemInformation = $this->getSystemInformation();
227
        // don't know why $systemInformation->lucene->solr-spec-version won't work
228 3
        $luceneInformation = (array)$systemInformation->lucene;
229 3
        return $luceneInformation['solr-spec-version'];
230
    }
231
232
    /**
233
     * Reloads the current core
234
     *
235
     * @return ResponseAdapter
236
     */
237 11
    public function reloadCore()
238
    {
239 11
        $response = $this->reloadCoreByName($this->getPrimaryEndpoint()->getCore());
240 11
        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 11
    public function reloadCoreByName($coreName)
250
    {
251 11
        $coreAdminReloadUrl = $this->_constructUrl(self::CORES_SERVLET) . '?action=reload&core=' . $coreName;
252 11
        $response = $this->_sendRawGet($coreAdminReloadUrl);
253 11
        return $response;
254
    }
255
256
    /**
257
     * Get the configured schema for the current core.
258
     *
259
     * @return Schema
260
     */
261 13
    public function getSchema()
262
    {
263 13
        if ($this->schema !== null) {
264
            return $this->schema;
265
        }
266 13
        $response = $this->_sendRawGet($this->_constructUrl(self::SCHEMA_SERVLET));
267
268 13
        $this->schema = $this->schemaParser->parseJson($response->getRawResponse());
269 13
        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 7
    public function getSynonyms($baseWord = '')
279
    {
280 7
        $this->initializeSynonymsUrl();
281 7
        $synonymsUrl = $this->_synonymsUrl;
282 7
        if (!empty($baseWord)) {
283 7
            $synonymsUrl .= '/' . rawurlencode(rawurlencode($baseWord));
284
        }
285
286 7
        $response = $this->_sendRawGet($synonymsUrl);
287 7
        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 7
    public function addSynonym($baseWord, array $synonyms)
301
    {
302 7
        $this->initializeSynonymsUrl();
303 7
        $json = $this->synonymParser->toJson($baseWord, $synonyms);
304 7
        $response = $this->_sendRawPost($this->_synonymsUrl, $json, 'application/json');
305 7
        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 7
    public function deleteSynonym($baseWord)
316
    {
317 7
        $this->initializeSynonymsUrl();
318 7
        if (empty($baseWord)) {
319
            throw new \InvalidArgumentException('Must provide base word.');
320
        }
321
322 7
        $response = $this->_sendRawDelete($this->_synonymsUrl . '/' . rawurlencode(rawurlencode($baseWord)));
323 7
        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 . '/' . rawurlencode(rawurlencode($stopWord)));
367
    }
368
369
    /**
370
     * @return void
371
     */
372 7
    protected function initializeSynonymsUrl()
373
    {
374 7
        if (trim($this->_synonymsUrl) !== '') {
375 7
            return;
376
        }
377 7
        $this->_synonymsUrl = $this->_constructUrl(self::SYNONYMS_SERVLET) . $this->getSchema()->getLanguage();
378 7
    }
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