first commit
This commit is contained in:
@@ -15,14 +15,13 @@ namespace WapplerSystems\Meilisearch\System\Meilisearch\Document;
|
||||
*/
|
||||
|
||||
use RuntimeException;
|
||||
use Solarium\QueryType\Update\Query\Document as SolariumDocument;
|
||||
|
||||
/**
|
||||
* Document representing the update query document
|
||||
*
|
||||
* @author Timo Hund <timo.hund@dkd.de>
|
||||
*/
|
||||
class Document extends SolariumDocument
|
||||
class Document
|
||||
{
|
||||
/**
|
||||
* Magic call method used to emulate getters as used by the template engine.
|
||||
@@ -33,13 +32,12 @@ class Document extends SolariumDocument
|
||||
*/
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
if (substr($name, 0, 3) == 'get') {
|
||||
if (substr($name, 0, 3) === 'get') {
|
||||
$field = substr($name, 3);
|
||||
$field = strtolower($field[0]) . substr($field, 1);
|
||||
return $this->fields[$field] ?? null;
|
||||
} else {
|
||||
throw new RuntimeException('Call to undefined method. Supports magic getters only.', 1311006605);
|
||||
}
|
||||
throw new RuntimeException('Call to undefined method. Supports magic getters only.', 1311006605);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -69,6 +69,11 @@ class MeilisearchConnection
|
||||
*/
|
||||
protected $configuration;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $siteConfiguration;
|
||||
|
||||
/**
|
||||
* @var SynonymParser
|
||||
*/
|
||||
@@ -85,9 +90,9 @@ class MeilisearchConnection
|
||||
protected $schemaParser = null;
|
||||
|
||||
/**
|
||||
* @var Client[]
|
||||
* @var Client
|
||||
*/
|
||||
protected $nodes = [];
|
||||
protected $client ;
|
||||
|
||||
/**
|
||||
* @var MeilisearchLogManager
|
||||
@@ -104,15 +109,6 @@ class MeilisearchConnection
|
||||
*/
|
||||
protected $psr7Client;
|
||||
|
||||
/**
|
||||
* @var RequestFactoryInterface
|
||||
*/
|
||||
protected $requestFactory;
|
||||
|
||||
/**
|
||||
* @var StreamFactoryInterface
|
||||
*/
|
||||
protected $streamFactory;
|
||||
|
||||
/**
|
||||
* @var EventDispatcherInterface
|
||||
@@ -122,8 +118,8 @@ class MeilisearchConnection
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Client $readNode
|
||||
* @param Client $writeNode
|
||||
* @param Client $client
|
||||
* @param array $siteConfiguration
|
||||
* @param ?TypoScriptConfiguration $configuration
|
||||
* @param ?SynonymParser $synonymParser
|
||||
* @param ?StopWordParser $stopWordParser
|
||||
@@ -138,8 +134,8 @@ class MeilisearchConnection
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function __construct(
|
||||
Client $readNode,
|
||||
Client $writeNode,
|
||||
Client $client,
|
||||
array $siteConfiguration,
|
||||
TypoScriptConfiguration $configuration = null,
|
||||
SynonymParser $synonymParser = null,
|
||||
StopWordParser $stopWordParser = null,
|
||||
@@ -148,9 +144,8 @@ class MeilisearchConnection
|
||||
ClientInterface $psr7Client = null,
|
||||
EventDispatcherInterface $eventDispatcher = null
|
||||
) {
|
||||
$this->nodes['read'] = $readNode;
|
||||
$this->nodes['write'] = $writeNode;
|
||||
$this->nodes['admin'] = $writeNode;
|
||||
$this->client = $client;
|
||||
$this->siteConfiguration = $siteConfiguration;
|
||||
$this->configuration = $configuration ?? Util::getMeilisearchConfiguration();
|
||||
$this->synonymParser = $synonymParser;
|
||||
$this->stopWordParser = $stopWordParser;
|
||||
@@ -160,14 +155,6 @@ class MeilisearchConnection
|
||||
$this->eventDispatcher = $eventDispatcher ?? GeneralUtility::getContainer()->get(EventDispatcherInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return Client
|
||||
*/
|
||||
public function getNode(string $key): Client
|
||||
{
|
||||
return $this->nodes[$key];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MeilisearchAdminService
|
||||
@@ -187,9 +174,7 @@ class MeilisearchConnection
|
||||
*/
|
||||
protected function buildAdminService(): MeilisearchAdminService
|
||||
{
|
||||
$endpointKey = 'admin';
|
||||
$client = $this->getClient($endpointKey);
|
||||
return GeneralUtility::makeInstance(MeilisearchAdminService::class, $client, $this->configuration, $this->logger, $this->synonymParser, $this->stopWordParser, $this->schemaParser);
|
||||
return GeneralUtility::makeInstance(MeilisearchAdminService::class, $this, $this->client, $this->configuration, $this->logger, $this->synonymParser, $this->stopWordParser, $this->schemaParser);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -210,9 +195,7 @@ class MeilisearchConnection
|
||||
*/
|
||||
protected function buildReadService(): MeilisearchReadService
|
||||
{
|
||||
$endpointKey = 'read';
|
||||
$client = $this->getClient($endpointKey);
|
||||
return GeneralUtility::makeInstance(MeilisearchReadService::class, $client);
|
||||
return GeneralUtility::makeInstance(MeilisearchReadService::class, $this->client);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -233,9 +216,7 @@ class MeilisearchConnection
|
||||
*/
|
||||
protected function buildWriteService(): MeilisearchWriteService
|
||||
{
|
||||
$endpointKey = 'write';
|
||||
$client = $this->getClient($endpointKey);
|
||||
return GeneralUtility::makeInstance(MeilisearchWriteService::class, $client);
|
||||
return GeneralUtility::makeInstance(MeilisearchWriteService::class, $this->client);
|
||||
}
|
||||
|
||||
|
||||
@@ -247,4 +228,13 @@ class MeilisearchConnection
|
||||
{
|
||||
$this->clients[$endpointKey] = $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getSiteConfiguration(): array
|
||||
{
|
||||
return $this->siteConfiguration;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -24,27 +24,18 @@ namespace WapplerSystems\Meilisearch\System\Meilisearch\Service;
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
use WapplerSystems\Meilisearch\PingFailedException;
|
||||
use MeiliSearch\Client;
|
||||
use MeiliSearch\Exceptions\CommunicationException;
|
||||
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
|
||||
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchConnection;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\ResponseAdapter;
|
||||
use WapplerSystems\Meilisearch\Util;
|
||||
use Solarium\Client;
|
||||
use Solarium\Core\Client\Endpoint;
|
||||
use Solarium\Core\Client\Request;
|
||||
use Solarium\Core\Query\QueryInterface;
|
||||
use Solarium\Exception\HttpException;
|
||||
use TYPO3\CMS\Core\Http\Uri;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
abstract class AbstractMeilisearchService
|
||||
{
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected static $pingCache = [];
|
||||
|
||||
/**
|
||||
* @var TypoScriptConfiguration
|
||||
*/
|
||||
@@ -60,26 +51,22 @@ abstract class AbstractMeilisearchService
|
||||
*/
|
||||
protected $client = null;
|
||||
|
||||
/**
|
||||
* @var MeilisearchConnection
|
||||
*/
|
||||
protected $meilisearchConnection = null;
|
||||
|
||||
/**
|
||||
* MeilisearchReadService constructor.
|
||||
*/
|
||||
public function __construct(Client $client, $typoScriptConfiguration = null, $logManager = null)
|
||||
public function __construct(MeilisearchConnection $meilisearchConnection, Client $client, $typoScriptConfiguration = null, $logManager = null)
|
||||
{
|
||||
$this->meilisearchConnection = $meilisearchConnection;
|
||||
$this->client = $client;
|
||||
$this->configuration = $typoScriptConfiguration ?? Util::getMeilisearchConfiguration();
|
||||
$this->logger = $logManager ?? GeneralUtility::makeInstance(MeilisearchLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path to the core meilisearch path + core path.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getCorePath()
|
||||
{
|
||||
$endpoint = $this->getPrimaryEndpoint();
|
||||
return is_null($endpoint) ? '' : $endpoint->getPath() .'/'. $endpoint->getCore();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Solarium client
|
||||
@@ -92,158 +79,28 @@ abstract class AbstractMeilisearchService
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a valid http URL given this server's host, port and path and a provided servlet name
|
||||
*
|
||||
* @param string $servlet
|
||||
* @param array $params
|
||||
* @return string
|
||||
* @return MeilisearchConnection
|
||||
*/
|
||||
protected function _constructUrl($servlet, $params = [])
|
||||
public function getMeilisearchConnection(): ?MeilisearchConnection
|
||||
{
|
||||
$queryString = count($params) ? '?' . http_build_query($params, null, '&') : '';
|
||||
return $this->__toString() . $servlet . $queryString;
|
||||
return $this->meilisearchConnection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string representation of the Meilisearch connection. Specifically
|
||||
* will return the Meilisearch URL.
|
||||
*
|
||||
* @return string The Meilisearch URL.
|
||||
* @TODO: Add support for API version 2
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$endpoint = $this->getPrimaryEndpoint();
|
||||
if (!$endpoint instanceof Endpoint) {
|
||||
return '';
|
||||
}
|
||||
|
||||
try {
|
||||
return $endpoint->getCoreBaseUri();
|
||||
} catch (\Exception $exception) {
|
||||
}
|
||||
return $endpoint->getScheme(). '://' . $endpoint->getHost() . ':' . $endpoint->getPort() . $endpoint->getPath() . '/' . $endpoint->getCore() . '/';
|
||||
$siteConfiguration = $this->meilisearchConnection->getSiteConfiguration();
|
||||
$strConnection = $siteConfiguration['schema'].$siteConfiguration['host'];
|
||||
|
||||
if (!$this->ping()) return $strConnection;
|
||||
|
||||
return $strConnection . ', ' . implode(',',$this->client->version());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Endpoint|null
|
||||
*/
|
||||
public function getPrimaryEndpoint()
|
||||
{
|
||||
return is_array($this->client->getEndpoints()) ? reset($this->client->getEndpoints()) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Central method for making a get operation against this Meilisearch Server
|
||||
*
|
||||
* @param string $url
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
protected function _sendRawGet($url)
|
||||
{
|
||||
return $this->_sendRawRequest($url, Request::METHOD_GET);
|
||||
}
|
||||
|
||||
/**
|
||||
* Central method for making a HTTP DELETE operation against the Meilisearch server
|
||||
*
|
||||
* @param string $url
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
protected function _sendRawDelete($url)
|
||||
{
|
||||
return $this->_sendRawRequest($url, Request::METHOD_DELETE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Central method for making a post operation against this Meilisearch Server
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $rawPost
|
||||
* @param string $contentType
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
protected function _sendRawPost($url, $rawPost, $contentType = 'text/xml; charset=UTF-8')
|
||||
{
|
||||
$initializeRequest = function(Request $request) use ($rawPost, $contentType) {
|
||||
$request->setRawData($rawPost);
|
||||
$request->addHeader('Content-Type: ' . $contentType);
|
||||
return $request;
|
||||
};
|
||||
|
||||
return $this->_sendRawRequest($url, Request::METHOD_POST, $rawPost, $initializeRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* Method that performs an http request with the solarium client.
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $method
|
||||
* @param string $body
|
||||
* @param ?\Closure $initializeRequest
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
protected function _sendRawRequest(
|
||||
string $url,
|
||||
$method = Request::METHOD_GET,
|
||||
$body = '',
|
||||
\Closure $initializeRequest = null
|
||||
) {
|
||||
$logSeverity = MeilisearchLogManager::INFO;
|
||||
$exception = null;
|
||||
$url = $this->reviseUrl($url);
|
||||
try {
|
||||
$request = $this->buildSolariumRequestFromUrl($url, $method);
|
||||
if($initializeRequest !== null) {
|
||||
$request = $initializeRequest($request);
|
||||
}
|
||||
$response = $this->executeRequest($request);
|
||||
} catch (HttpException $exception) {
|
||||
$logSeverity = MeilisearchLogManager::ERROR;
|
||||
$response = new ResponseAdapter($exception->getBody(), $exception->getCode(), $exception->getMessage());
|
||||
}
|
||||
|
||||
if ($this->configuration->getLoggingQueryRawPost() || $response->getHttpStatus() != 200) {
|
||||
$message = 'Querying Meilisearch using '.$method;
|
||||
$this->writeLog($logSeverity, $message, $url, $response, $exception, $body);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Revise url
|
||||
* - Resolve relative paths
|
||||
*
|
||||
* @param string $url
|
||||
* @return string
|
||||
*/
|
||||
protected function reviseUrl(string $url): string
|
||||
{
|
||||
/* @var Uri $uri */
|
||||
$uri = GeneralUtility::makeInstance(Uri::class, $url);
|
||||
|
||||
if ((string)$uri->getPath() === '') {
|
||||
return $url;
|
||||
}
|
||||
|
||||
$path = trim($uri->getPath(), '/');
|
||||
$pathsCurrent = explode('/', $path);
|
||||
$pathNew = [];
|
||||
foreach ($pathsCurrent as $pathCurrent) {
|
||||
if ($pathCurrent === '..') {
|
||||
array_pop($pathNew);
|
||||
continue;
|
||||
}
|
||||
if ($pathCurrent === '.') {
|
||||
continue;
|
||||
}
|
||||
$pathNew[] = $pathCurrent;
|
||||
}
|
||||
|
||||
$uri = $uri->withPath(implode('/', $pathNew));
|
||||
return (string)$uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the log data and writes the message to the log
|
||||
@@ -281,90 +138,29 @@ abstract class AbstractMeilisearchService
|
||||
if (!empty($e)) {
|
||||
$logData['exception'] = $e->__toString();
|
||||
return $logData;
|
||||
} else {
|
||||
// trigger data parsing
|
||||
// @extensionScannerIgnoreLine
|
||||
$meilisearchResponse->response;
|
||||
$logData['response data'] = print_r($meilisearchResponse, true);
|
||||
return $logData;
|
||||
}
|
||||
// trigger data parsing
|
||||
// @extensionScannerIgnoreLine
|
||||
$meilisearchResponse->response;
|
||||
$logData['response data'] = print_r($meilisearchResponse, true);
|
||||
return $logData;
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Call the /admin/ping servlet, can be used to quickly tell if a connection to the
|
||||
* server is available.
|
||||
*
|
||||
* Simply overrides the MeilisearchPhpClient implementation, changing ping from a
|
||||
* HEAD to a GET request, see http://forge.typo3.org/issues/44167
|
||||
*
|
||||
* Also does not report the time, see https://forge.typo3.org/issues/64551
|
||||
*
|
||||
* @param boolean $useCache indicates if the ping result should be cached in the instance or not
|
||||
* @return bool TRUE if Meilisearch can be reached, FALSE if not
|
||||
* @return bool
|
||||
*/
|
||||
public function ping($useCache = true)
|
||||
public function ping()
|
||||
{
|
||||
try {
|
||||
$httpResponse = $this->performPingRequest($useCache);
|
||||
} catch (HttpException $exception) {
|
||||
$health = $this->client->health();
|
||||
} catch (CommunicationException $ex) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return ($httpResponse->getHttpStatus() === 200);
|
||||
return is_array($health) && $health['status'] === 'available';
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the /admin/ping servlet, can be used to get the runtime of a ping request.
|
||||
*
|
||||
* @param boolean $useCache indicates if the ping result should be cached in the instance or not
|
||||
* @return double runtime in milliseconds
|
||||
* @throws \WapplerSystems\Meilisearch\PingFailedException
|
||||
*/
|
||||
public function getPingRoundTripRuntime($useCache = true)
|
||||
{
|
||||
try {
|
||||
$start = $this->getMilliseconds();
|
||||
$httpResponse = $this->performPingRequest($useCache);
|
||||
$end = $this->getMilliseconds();
|
||||
} catch (HttpException $e) {
|
||||
$message = 'Meilisearch ping failed with unexpected response code: ' . $e->getCode();
|
||||
/** @var $exception \WapplerSystems\Meilisearch\PingFailedException */
|
||||
$exception = GeneralUtility::makeInstance(PingFailedException::class, /** @scrutinizer ignore-type */ $message);
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
if ($httpResponse->getHttpStatus() !== 200) {
|
||||
$message = 'Meilisearch ping failed with unexpected response code: ' . $httpResponse->getHttpStatus();
|
||||
/** @var $exception \WapplerSystems\Meilisearch\PingFailedException */
|
||||
$exception = GeneralUtility::makeInstance(PingFailedException::class, /** @scrutinizer ignore-type */ $message);
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
return $end - $start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a ping request and returns the result.
|
||||
*
|
||||
* @param boolean $useCache indicates if the ping result should be cached in the instance or not
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
protected function performPingRequest($useCache = true)
|
||||
{
|
||||
$cacheKey = (string)($this);
|
||||
if ($useCache && isset(static::$pingCache[$cacheKey])) {
|
||||
return static::$pingCache[$cacheKey];
|
||||
}
|
||||
|
||||
$pingQuery = $this->client->createPing();
|
||||
$pingResult = $this->createAndExecuteRequest($pingQuery);
|
||||
|
||||
if ($useCache) {
|
||||
static::$pingCache[$cacheKey] = $pingResult;
|
||||
}
|
||||
|
||||
return $pingResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current time in milliseconds.
|
||||
@@ -376,76 +172,5 @@ abstract class AbstractMeilisearchService
|
||||
return GeneralUtility::milliseconds();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param QueryInterface $query
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
protected function createAndExecuteRequest(QueryInterface $query): ResponseAdapter
|
||||
{
|
||||
$request = $this->client->createRequest($query);
|
||||
return $this->executeRequest($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $request
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
protected function executeRequest($request): ResponseAdapter
|
||||
{
|
||||
$result = $this->client->executeRequest($request);
|
||||
return new ResponseAdapter($result->getBody(), $result->getStatusCode(), $result->getStatusMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the request for Solarium.
|
||||
*
|
||||
* Important: The endpoint already contains the API information.
|
||||
* The internal Solarium will append the information including the core if set.
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $httpMethod
|
||||
* @return Request
|
||||
*/
|
||||
protected function buildSolariumRequestFromUrl(string $url, $httpMethod = Request::METHOD_GET): Request
|
||||
{
|
||||
$params = [];
|
||||
parse_str(parse_url($url, PHP_URL_QUERY), $params);
|
||||
$request = new Request();
|
||||
$path = parse_url($url, PHP_URL_PATH);
|
||||
$endpoint = $this->getPrimaryEndpoint();
|
||||
$api = $request->getApi() === Request::API_V1 ? 'meilisearch' : 'api';
|
||||
$coreBasePath = $endpoint->getPath() . '/' . $api . '/' . $endpoint->getCore() . '/';
|
||||
|
||||
$handler = $this->buildRelativePath($coreBasePath, $path);
|
||||
$request->setMethod($httpMethod);
|
||||
$request->setParams($params);
|
||||
$request->setHandler($handler);
|
||||
return $request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a relative path from base path to target path.
|
||||
* Required since Solarium contains the core information
|
||||
*
|
||||
* @param string $basePath
|
||||
* @param string $targetPath
|
||||
* @return string
|
||||
*/
|
||||
protected function buildRelativePath(string $basePath, string $targetPath): string
|
||||
{
|
||||
$basePath = trim($basePath, '/');
|
||||
$targetPath = trim($targetPath, '/');
|
||||
$baseElements = explode('/', $basePath);
|
||||
$targetElements = explode('/', $targetPath);
|
||||
$targetSegment = array_pop($targetElements);
|
||||
foreach ($baseElements as $i => $segment) {
|
||||
if (isset($targetElements[$i]) && $segment === $targetElements[$i]) {
|
||||
unset($baseElements[$i], $targetElements[$i]);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$targetElements[] = $targetSegment;
|
||||
return str_repeat('../', count($baseElements)) . implode('/', $targetElements);
|
||||
}
|
||||
}
|
||||
|
@@ -24,14 +24,15 @@ namespace WapplerSystems\Meilisearch\System\Meilisearch\Service;
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
use MeiliSearch\Client;
|
||||
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
|
||||
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchConnection;
|
||||
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;
|
||||
use Solarium\Client;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
@@ -39,30 +40,9 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
*/
|
||||
class MeilisearchAdminService extends AbstractMeilisearchService
|
||||
{
|
||||
const PLUGINS_SERVLET = 'admin/plugins';
|
||||
const LUKE_SERVLET = 'admin/luke';
|
||||
const SYSTEM_SERVLET = 'admin/system';
|
||||
const CORES_SERVLET = '../admin/cores';
|
||||
const FILE_SERVLET = 'admin/file';
|
||||
const SCHEMA_SERVLET = 'schema';
|
||||
const SYNONYMS_SERVLET = 'schema/analysis/synonyms/';
|
||||
const STOPWORDS_SERVLET = 'schema/analysis/stopwords/';
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $lukeData = [];
|
||||
|
||||
protected $systemData = null;
|
||||
|
||||
protected $pluginsData = [];
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $meilisearchconfigName;
|
||||
|
||||
/**
|
||||
* @var SchemaParser
|
||||
*/
|
||||
@@ -95,13 +75,16 @@ class MeilisearchAdminService extends AbstractMeilisearchService
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param TypoScriptConfiguration $typoScriptConfiguration
|
||||
* @param SynonymParser $synonymParser
|
||||
* @param StopWordParser $stopWordParser
|
||||
* @param SchemaParser $schemaParser
|
||||
* @param MeilisearchLogManager $logManager
|
||||
* @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
|
||||
*/
|
||||
public function __construct(
|
||||
MeilisearchConnection $meilisearchConnection,
|
||||
Client $client,
|
||||
TypoScriptConfiguration $typoScriptConfiguration = null,
|
||||
MeilisearchLogManager $logManager = null,
|
||||
@@ -110,71 +93,13 @@ class MeilisearchAdminService extends AbstractMeilisearchService
|
||||
SchemaParser $schemaParser = null
|
||||
)
|
||||
{
|
||||
parent::__construct($client, $typoScriptConfiguration);
|
||||
parent::__construct($meilisearchConnection, $client, $typoScriptConfiguration);
|
||||
|
||||
$this->synonymParser = $synonymParser ?? GeneralUtility::makeInstance(SynonymParser::class);
|
||||
$this->stopWordParser = $stopWordParser ?? GeneralUtility::makeInstance(StopWordParser::class);
|
||||
$this->schemaParser = $schemaParser ?? GeneralUtility::makeInstance(SchemaParser::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call the /admin/system servlet and retrieve system information about Meilisearch
|
||||
*
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
public function system()
|
||||
{
|
||||
return $this->_sendRawGet($this->_constructUrl(self::SYSTEM_SERVLET, ['wt' => 'json']));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets information about the plugins installed in Meilisearch
|
||||
*
|
||||
* @return array A nested array of plugin data.
|
||||
*/
|
||||
public function getPluginsInformation()
|
||||
{
|
||||
if (count($this->pluginsData) == 0) {
|
||||
$url = $this->_constructUrl(self::PLUGINS_SERVLET, ['wt' => 'json']);
|
||||
$pluginsInformation = $this->_sendRawGet($url);
|
||||
|
||||
// access a random property to trigger response parsing
|
||||
$pluginsInformation->responseHeader;
|
||||
$this->pluginsData = $pluginsInformation;
|
||||
}
|
||||
|
||||
return $this->pluginsData;
|
||||
}
|
||||
|
||||
/**
|
||||
* get field meta data for the index
|
||||
*
|
||||
* @param int $numberOfTerms Number of top terms to fetch for each field
|
||||
* @return \stdClass
|
||||
*/
|
||||
public function getFieldsMetaData($numberOfTerms = 0)
|
||||
{
|
||||
return $this->getLukeMetaData($numberOfTerms)->fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves meta data about the index from the luke request handler
|
||||
*
|
||||
* @param int $numberOfTerms Number of top terms to fetch for each field
|
||||
* @return ResponseAdapter Index meta data
|
||||
*/
|
||||
public function getLukeMetaData($numberOfTerms = 0)
|
||||
{
|
||||
if (!isset($this->lukeData[$numberOfTerms])) {
|
||||
$lukeUrl = $this->_constructUrl(
|
||||
self::LUKE_SERVLET, ['numTerms' => $numberOfTerms, 'wt' => 'json', 'fl' => '*']
|
||||
);
|
||||
|
||||
$this->lukeData[$numberOfTerms] = $this->_sendRawGet($lukeUrl);
|
||||
}
|
||||
|
||||
return $this->lukeData[$numberOfTerms];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets information about the Meilisearch server
|
||||
@@ -187,86 +112,13 @@ class MeilisearchAdminService extends AbstractMeilisearchService
|
||||
$systemInformation = $this->system();
|
||||
|
||||
// access a random property to trigger response parsing
|
||||
$systemInformation->responseHeader;
|
||||
$this->systemData = $systemInformation;
|
||||
}
|
||||
|
||||
return $this->systemData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the meilisearchconfig.xml file installed and in use on the Meilisearch
|
||||
* server.
|
||||
*
|
||||
* @return string Name of the active meilisearchconfig.xml
|
||||
*/
|
||||
public function getMeilisearchconfigName()
|
||||
{
|
||||
if (is_null($this->meilisearchconfigName)) {
|
||||
$meilisearchconfigXmlUrl = $this->_constructUrl(self::FILE_SERVLET, ['file' => 'meilisearchconfig.xml']);
|
||||
$response = $this->_sendRawGet($meilisearchconfigXmlUrl);
|
||||
$meilisearchconfigXml = simplexml_load_string($response->getRawResponse());
|
||||
if ($meilisearchconfigXml === false) {
|
||||
throw new \InvalidArgumentException('No valid xml response from schema file: ' . $meilisearchconfigXmlUrl);
|
||||
}
|
||||
$this->meilisearchconfigName = (string)$meilisearchconfigXml->attributes()->name;
|
||||
}
|
||||
|
||||
return $this->meilisearchconfigName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Meilisearch server's version number.
|
||||
*
|
||||
* @return string Meilisearch version number
|
||||
*/
|
||||
public function getMeilisearchServerVersion()
|
||||
{
|
||||
$systemInformation = $this->getSystemInformation();
|
||||
// don't know why $systemInformation->lucene->meilisearch-spec-version won't work
|
||||
$luceneInformation = (array)$systemInformation->lucene;
|
||||
return $luceneInformation['meilisearch-spec-version'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads the current core
|
||||
*
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
public function reloadCore()
|
||||
{
|
||||
$response = $this->reloadCoreByName($this->getPrimaryEndpoint()->getCore());
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads a core of the connection by a given corename.
|
||||
*
|
||||
* @param string $coreName
|
||||
* @return ResponseAdapter
|
||||
*/
|
||||
public function reloadCoreByName($coreName)
|
||||
{
|
||||
$coreAdminReloadUrl = $this->_constructUrl(self::CORES_SERVLET) . '?action=reload&core=' . $coreName;
|
||||
$response = $this->_sendRawGet($coreAdminReloadUrl);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the configured schema for the current core.
|
||||
*
|
||||
* @return Schema
|
||||
*/
|
||||
public function getSchema()
|
||||
{
|
||||
if ($this->schema !== null) {
|
||||
return $this->schema;
|
||||
}
|
||||
$response = $this->_sendRawGet($this->_constructUrl(self::SCHEMA_SERVLET));
|
||||
|
||||
$this->schema = $this->schemaParser->parseJson($response->getRawResponse());
|
||||
return $this->schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get currently configured synonyms
|
||||
@@ -300,8 +152,7 @@ class MeilisearchAdminService extends AbstractMeilisearchService
|
||||
{
|
||||
$this->initializeSynonymsUrl();
|
||||
$json = $this->synonymParser->toJson($baseWord, $synonyms);
|
||||
$response = $this->_sendRawPost($this->_synonymsUrl, $json, 'application/json');
|
||||
return $response;
|
||||
return $this->_sendRawPost($this->_synonymsUrl, $json, 'application/json');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -318,8 +169,7 @@ class MeilisearchAdminService extends AbstractMeilisearchService
|
||||
throw new \InvalidArgumentException('Must provide base word.');
|
||||
}
|
||||
|
||||
$response = $this->_sendRawDelete($this->_synonymsUrl . '/' . urlencode($baseWord));
|
||||
return $response;
|
||||
return $this->_sendRawDelete($this->_synonymsUrl . '/' . urlencode($baseWord));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -24,12 +24,10 @@ namespace WapplerSystems\Meilisearch\System\Meilisearch\Service;
|
||||
* This copyright notice MUST APPEAR in all copies of the script!
|
||||
***************************************************************/
|
||||
|
||||
use WapplerSystems\Meilisearch\Domain\Search\Query\Query;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\ResponseAdapter;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchCommunicationException;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchInternalServerErrorException;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchUnavailableException;
|
||||
use Solarium\Exception\HttpException;
|
||||
|
||||
/**
|
||||
* Class MeilisearchReadService
|
||||
@@ -47,26 +45,6 @@ class MeilisearchReadService extends AbstractMeilisearchService
|
||||
*/
|
||||
protected $responseCache = null;
|
||||
|
||||
/**
|
||||
* Performs a search.
|
||||
*
|
||||
* @param Query $query
|
||||
* @return ResponseAdapter Meilisearch response
|
||||
* @throws \RuntimeException if Meilisearch returns a HTTP status code other than 200
|
||||
*/
|
||||
public function search($query)
|
||||
{
|
||||
try {
|
||||
$request = $this->client->createRequest($query);
|
||||
$response = $this->executeRequest($request);
|
||||
$this->hasSearched = true;
|
||||
$this->responseCache = $response;
|
||||
} catch (HttpException $e) {
|
||||
$this->handleErrorResponses($e);
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a search has been executed or not.
|
||||
*
|
||||
|
@@ -26,7 +26,6 @@ namespace WapplerSystems\Meilisearch\System\Meilisearch\Service;
|
||||
|
||||
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
|
||||
use WapplerSystems\Meilisearch\System\Meilisearch\ResponseAdapter;
|
||||
use Solarium\QueryType\Extract\Query;
|
||||
|
||||
/**
|
||||
* Class MeilisearchWriteService
|
||||
|
@@ -58,30 +58,16 @@ class SiteUtility
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is used to retrieve the connection configuration from the TYPO3 site configuration.
|
||||
*
|
||||
* Note: Language context properties have precedence over global settings.
|
||||
*
|
||||
* The configuration is done in the globals configuration of a site, and be extended in the language specific configuration
|
||||
* of a site.
|
||||
*
|
||||
* Typically everything except the core name is configured on the global level and the core name differs for each language.
|
||||
*
|
||||
* In addition every property can be defined for the ```read``` and ```write``` scope.
|
||||
*
|
||||
* The convention for property keys is "meilisearch_{propertyName}_{scope}". With the configuration "meilisearch_host_read" you define the host
|
||||
* for the meilisearch read connection.
|
||||
*
|
||||
* @param Site $typo3Site
|
||||
* @param string $property
|
||||
* @param int $languageId
|
||||
* @param string $scope
|
||||
* @param string $defaultValue
|
||||
* @return string
|
||||
*/
|
||||
public static function getConnectionProperty(Site $typo3Site, string $property, int $languageId, string $scope, string $defaultValue = null): string
|
||||
public static function getConnectionProperty(Site $typo3Site, string $property, string $defaultValue = null): string
|
||||
{
|
||||
$value = self::getConnectionPropertyOrFallback($typo3Site, $property, $languageId, $scope);
|
||||
$value = self::getConnectionPropertyOrFallback($typo3Site, $property);
|
||||
if ($value === null) {
|
||||
return $defaultValue;
|
||||
}
|
||||
@@ -94,72 +80,31 @@ class SiteUtility
|
||||
*
|
||||
* @param Site $typo3Site
|
||||
* @param string $property
|
||||
* @param int $languageId
|
||||
* @param string $scope
|
||||
* @return mixed
|
||||
*/
|
||||
protected static function getConnectionPropertyOrFallback(Site $typo3Site, string $property, int $languageId, string $scope)
|
||||
protected static function getConnectionPropertyOrFallback(Site $typo3Site, string $property)
|
||||
{
|
||||
if ($scope === 'write' && !self::writeConnectionIsEnabled($typo3Site, $languageId)) {
|
||||
$scope = 'read';
|
||||
}
|
||||
|
||||
// convention key meilisearch_$property_$scope
|
||||
$keyToCheck = 'meilisearch_' . $property . '_' . $scope;
|
||||
|
||||
// convention fallback key meilisearch_$property_read
|
||||
$fallbackKey = 'meilisearch_' . $property . '_read';
|
||||
|
||||
// try to find language specific setting if found return it
|
||||
$languageSpecificConfiguration = $typo3Site->getLanguageById($languageId)->toArray();
|
||||
$value = self::getValueOrFallback($languageSpecificConfiguration, $keyToCheck, $fallbackKey);
|
||||
if ($value !== null) {
|
||||
return $value;
|
||||
}
|
||||
$keyToCheck = 'meilisearch_' . $property;
|
||||
|
||||
// if not found check global configuration
|
||||
$siteBaseConfiguration = $typo3Site->getConfiguration();
|
||||
return self::getValueOrFallback($siteBaseConfiguration, $keyToCheck, $fallbackKey);
|
||||
return self::getValueOrFallback($siteBaseConfiguration, $keyToCheck);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether write connection is enabled.
|
||||
* Language context properties have precedence over global settings.
|
||||
*
|
||||
* @param Site $typo3Site
|
||||
* @param int $languageId
|
||||
* @return bool
|
||||
*/
|
||||
protected static function writeConnectionIsEnabled(Site $typo3Site, int $languageId): bool
|
||||
{
|
||||
$languageSpecificConfiguration = $typo3Site->getLanguageById($languageId)->toArray();
|
||||
$value = self::getValueOrFallback($languageSpecificConfiguration, 'meilisearch_use_write_connection', 'meilisearch_use_write_connection');
|
||||
if ($value !== null) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$siteBaseConfiguration = $typo3Site->getConfiguration();
|
||||
$value = self::getValueOrFallback($siteBaseConfiguration, 'meilisearch_use_write_connection', 'meilisearch_use_write_connection');
|
||||
if ($value !== null) {
|
||||
return $value;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param string $keyToCheck
|
||||
* @param string $fallbackKey
|
||||
* @return string|bool|null
|
||||
*/
|
||||
protected static function getValueOrFallback(array $data, string $keyToCheck, string $fallbackKey)
|
||||
protected static function getValueOrFallback(array $data, string $keyToCheck)
|
||||
{
|
||||
$value = $data[$keyToCheck] ?? null;
|
||||
if ($value === '0' || $value === 0 || !empty($value)) {
|
||||
return self::evaluateConfigurationData($value);
|
||||
}
|
||||
|
||||
return self::evaluateConfigurationData($data[$fallbackKey] ?? null);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -176,7 +121,8 @@ class SiteUtility
|
||||
{
|
||||
if ($value === 'true') {
|
||||
return true;
|
||||
} elseif ($value === 'false') {
|
||||
}
|
||||
if ($value === 'false') {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user