2021-04-17 00:26:33 +02:00
|
|
|
<?php
|
2021-04-17 21:20:54 +02:00
|
|
|
namespace WapplerSystems\Meilisearch\System\Meilisearch\Service;
|
2021-04-17 00:26:33 +02:00
|
|
|
|
|
|
|
/***************************************************************
|
|
|
|
* Copyright notice
|
|
|
|
*
|
|
|
|
* (c) 2009-2017 Timo Hund <timo.hund@dkd.de>
|
|
|
|
* All rights reserved
|
|
|
|
*
|
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is
|
|
|
|
* free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* The GNU General Public License can be found at
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html.
|
|
|
|
*
|
|
|
|
* This script is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* This copyright notice MUST APPEAR in all copies of the script!
|
|
|
|
***************************************************************/
|
|
|
|
|
2021-04-24 04:44:44 +02:00
|
|
|
use MeiliSearch\Client;
|
2021-04-17 00:26:33 +02:00
|
|
|
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
|
2021-04-17 21:20:54 +02:00
|
|
|
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
|
2021-04-24 04:44:44 +02:00
|
|
|
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchConnection;
|
2021-04-17 21:20:54 +02:00
|
|
|
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\SchemaParser;
|
|
|
|
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\StopWordParser;
|
|
|
|
use WapplerSystems\Meilisearch\System\Meilisearch\Parser\SynonymParser;
|
|
|
|
use WapplerSystems\Meilisearch\System\Meilisearch\ResponseAdapter;
|
|
|
|
use WapplerSystems\Meilisearch\System\Meilisearch\Schema\Schema;
|
2021-04-17 00:26:33 +02:00
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
|
|
|
|
|
|
/**
|
2021-04-17 21:20:54 +02:00
|
|
|
* Class MeilisearchAdminService
|
2021-04-17 00:26:33 +02:00
|
|
|
*/
|
2021-04-17 21:20:54 +02:00
|
|
|
class MeilisearchAdminService extends AbstractMeilisearchService
|
2021-04-17 00:26:33 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
protected $systemData = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SchemaParser
|
|
|
|
*/
|
|
|
|
protected $schemaParser = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Schema
|
|
|
|
*/
|
|
|
|
protected $schema;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_synonymsUrl;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_stopWordsUrl;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var SynonymParser
|
|
|
|
*/
|
|
|
|
protected $synonymParser = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var StopWordParser
|
|
|
|
*/
|
|
|
|
protected $stopWordParser = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2021-04-24 04:44:44 +02:00
|
|
|
* @param MeilisearchConnection $meilisearchConnection
|
|
|
|
* @param Client $client
|
|
|
|
* @param TypoScriptConfiguration|null $typoScriptConfiguration
|
|
|
|
* @param MeilisearchLogManager|null $logManager
|
|
|
|
* @param SynonymParser|null $synonymParser
|
|
|
|
* @param StopWordParser|null $stopWordParser
|
|
|
|
* @param SchemaParser|null $schemaParser
|
2021-04-17 00:26:33 +02:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2021-04-24 04:44:44 +02:00
|
|
|
MeilisearchConnection $meilisearchConnection,
|
2021-04-17 00:26:33 +02:00
|
|
|
Client $client,
|
|
|
|
TypoScriptConfiguration $typoScriptConfiguration = null,
|
2021-04-17 21:20:54 +02:00
|
|
|
MeilisearchLogManager $logManager = null,
|
2021-04-17 00:26:33 +02:00
|
|
|
SynonymParser $synonymParser = null,
|
|
|
|
StopWordParser $stopWordParser = null,
|
|
|
|
SchemaParser $schemaParser = null
|
|
|
|
)
|
|
|
|
{
|
2021-04-24 04:44:44 +02:00
|
|
|
parent::__construct($meilisearchConnection, $client, $typoScriptConfiguration);
|
2021-04-17 00:26:33 +02:00
|
|
|
|
|
|
|
$this->synonymParser = $synonymParser ?? GeneralUtility::makeInstance(SynonymParser::class);
|
|
|
|
$this->stopWordParser = $stopWordParser ?? GeneralUtility::makeInstance(StopWordParser::class);
|
|
|
|
$this->schemaParser = $schemaParser ?? GeneralUtility::makeInstance(SchemaParser::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-17 21:20:54 +02:00
|
|
|
* Gets information about the Meilisearch server
|
2021-04-17 00:26:33 +02:00
|
|
|
*
|
|
|
|
* @return ResponseAdapter
|
|
|
|
*/
|
|
|
|
public function getSystemInformation()
|
|
|
|
{
|
|
|
|
if (empty($this->systemData)) {
|
|
|
|
$systemInformation = $this->system();
|
|
|
|
|
|
|
|
// access a random property to trigger response parsing
|
|
|
|
$this->systemData = $systemInformation;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->systemData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get currently configured synonyms
|
|
|
|
*
|
|
|
|
* @param string $baseWord If given a base word, retrieves the synonyms for that word only
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSynonyms($baseWord = '')
|
|
|
|
{
|
|
|
|
$this->initializeSynonymsUrl();
|
|
|
|
$synonymsUrl = $this->_synonymsUrl;
|
|
|
|
if (!empty($baseWord)) {
|
|
|
|
$synonymsUrl .= '/' . $baseWord;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->_sendRawGet($synonymsUrl);
|
|
|
|
return $this->synonymParser->parseJson($baseWord, $response->getRawResponse());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add list of synonyms for base word to managed synonyms map
|
|
|
|
*
|
|
|
|
* @param string $baseWord
|
|
|
|
* @param array $synonyms
|
|
|
|
*
|
|
|
|
* @return ResponseAdapter
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException If $baseWord or $synonyms are empty
|
|
|
|
*/
|
|
|
|
public function addSynonym($baseWord, array $synonyms)
|
|
|
|
{
|
|
|
|
$this->initializeSynonymsUrl();
|
|
|
|
$json = $this->synonymParser->toJson($baseWord, $synonyms);
|
2021-04-24 04:44:44 +02:00
|
|
|
return $this->_sendRawPost($this->_synonymsUrl, $json, 'application/json');
|
2021-04-17 00:26:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a synonym from the synonyms map
|
|
|
|
*
|
|
|
|
* @param string $baseWord
|
|
|
|
* @return ResponseAdapter
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function deleteSynonym($baseWord)
|
|
|
|
{
|
|
|
|
$this->initializeSynonymsUrl();
|
|
|
|
if (empty($baseWord)) {
|
|
|
|
throw new \InvalidArgumentException('Must provide base word.');
|
|
|
|
}
|
|
|
|
|
2021-04-24 04:44:44 +02:00
|
|
|
return $this->_sendRawDelete($this->_synonymsUrl . '/' . urlencode($baseWord));
|
2021-04-17 00:26:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get currently configured stop words
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getStopWords()
|
|
|
|
{
|
|
|
|
$this->initializeStopWordsUrl();
|
|
|
|
$response = $this->_sendRawGet($this->_stopWordsUrl);
|
|
|
|
return $this->stopWordParser->parseJson($response->getRawResponse());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds stop words to the managed stop word list
|
|
|
|
*
|
|
|
|
* @param array|string $stopWords string for a single word, array for multiple words
|
|
|
|
* @return ResponseAdapter
|
|
|
|
* @throws \InvalidArgumentException If $stopWords is empty
|
|
|
|
*/
|
|
|
|
public function addStopWords($stopWords)
|
|
|
|
{
|
|
|
|
$this->initializeStopWordsUrl();
|
|
|
|
$json = $this->stopWordParser->toJson($stopWords);
|
|
|
|
return $this->_sendRawPost($this->_stopWordsUrl, $json, 'application/json');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a words from the managed stop word list
|
|
|
|
*
|
|
|
|
* @param string $stopWord stop word to delete
|
|
|
|
* @return ResponseAdapter
|
|
|
|
* @throws \InvalidArgumentException If $stopWords is empty
|
|
|
|
*/
|
|
|
|
public function deleteStopWord($stopWord)
|
|
|
|
{
|
|
|
|
$this->initializeStopWordsUrl();
|
|
|
|
if (empty($stopWord)) {
|
|
|
|
throw new \InvalidArgumentException('Must provide stop word.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_sendRawDelete($this->_stopWordsUrl . '/' . urlencode($stopWord));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function initializeSynonymsUrl()
|
|
|
|
{
|
|
|
|
if (trim($this->_synonymsUrl) !== '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->_synonymsUrl = $this->_constructUrl(self::SYNONYMS_SERVLET) . $this->getSchema()->getLanguage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function initializeStopWordsUrl()
|
|
|
|
{
|
|
|
|
if (trim($this->_stopWordsUrl) !== '') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_stopWordsUrl = $this->_constructUrl(self::STOPWORDS_SERVLET) . $this->getSchema()->getLanguage();
|
|
|
|
}
|
|
|
|
}
|