From 2ec00d58511271bd4d5f18bdfafefac5d7aaf0e6 Mon Sep 17 00:00:00 2001 From: Ingo Pfennigstorf Date: Thu, 19 May 2022 12:19:27 +0200 Subject: [PATCH] Add method to return connection parameters This adds a method to the ServiceProviderInterface to return the current connection settings. `GeneralUtility::makeInstance(SolrServiceProvider::class)->getConnectionSettings()` will return a `Connection` object. Related: #176 --- Classes/Domain/Model/Connection.php | 87 ++++++++++++++++++++ Classes/Service/ServiceProviderInterface.php | 4 + Classes/Service/SolrServiceProvider.php | 67 +++++++++++---- 3 files changed, 140 insertions(+), 18 deletions(-) create mode 100644 Classes/Domain/Model/Connection.php diff --git a/Classes/Domain/Model/Connection.php b/Classes/Domain/Model/Connection.php new file mode 100644 index 00000000..25e8360c --- /dev/null +++ b/Classes/Domain/Model/Connection.php @@ -0,0 +1,87 @@ +searchProvider; + } + + public function setSearchProvider(string $searchProvider): Connection + { + $this->searchProvider = $searchProvider; + + return $this; + } + + public function getHost(): string + { + return $this->host; + } + + public function setHost(string $host): Connection + { + $this->host = $host; + + return $this; + } + + public function getPort(): int + { + return $this->port; + } + + public function setPort(int $port): Connection + { + $this->port = $port; + + return $this; + } + + public function getScheme(): string + { + return $this->scheme; + } + + public function setScheme(string $scheme): Connection + { + $this->scheme = $scheme; + + return $this; + } + + public function getCore(): string + { + return $this->core; + } + + public function setCore(string $core): Connection + { + $this->core = $core; + + return $this; + } + + public function getPath(): string + { + return $this->path; + } + + public function setPath(string $path): Connection + { + $this->path = $path; + + return $this; + } +} diff --git a/Classes/Service/ServiceProviderInterface.php b/Classes/Service/ServiceProviderInterface.php index 646ff2ba..15824d7d 100644 --- a/Classes/Service/ServiceProviderInterface.php +++ b/Classes/Service/ServiceProviderInterface.php @@ -27,6 +27,8 @@ * This copyright notice MUST APPEAR in all copies of the script! * ************************************************************* */ +use Subugoe\Find\Domain\Model\Connection; + /** * Interface for search engine provider. */ @@ -38,6 +40,8 @@ public function connect(); public function getConfiguration(); + public function getConnectionSettings(): Connection; + public function getDefaultQuery(); public function getDocumentById(string $id); diff --git a/Classes/Service/SolrServiceProvider.php b/Classes/Service/SolrServiceProvider.php index 08f6902d..3dc68706 100644 --- a/Classes/Service/SolrServiceProvider.php +++ b/Classes/Service/SolrServiceProvider.php @@ -32,6 +32,7 @@ use Solarium\Core\Client\Adapter\Http; use Solarium\Exception\HttpException; use Solarium\QueryType\Select\Query\Query; +use Subugoe\Find\Domain\Model\Connection; use Subugoe\Find\Utility\FrontendUtility; use Subugoe\Find\Utility\LoggerUtility; use Subugoe\Find\Utility\SettingsUtility; @@ -52,6 +53,8 @@ class SolrServiceProvider extends AbstractServiceProvider protected ?string $controllerExtensionKey = null; + protected Connection $connectionSettings; + protected Query $query; public function connect() @@ -62,14 +65,22 @@ public function connect() $currentConnectionSettings = UpgradeUtility::handleSolariumUpgrade($currentConnectionSettings); } + $this->connectionSettings = (new Connection()) + ->setCore($currentConnectionSettings['core']) + ->setHost($currentConnectionSettings['host']) + ->setPort((int) $currentConnectionSettings['port']) + ->setScheme($currentConnectionSettings['scheme']) + ->setPath($currentConnectionSettings['path']) + ->setSearchProvider(__CLASS__); + $connectionSettings = [ 'endpoint' => [ $this->connectionName => [ - 'host' => $currentConnectionSettings['host'], - 'port' => (int) $currentConnectionSettings['port'], - 'path' => $currentConnectionSettings['path'], - 'scheme' => $currentConnectionSettings['scheme'], - 'core' => $currentConnectionSettings['core'], + 'host' => $this->connectionSettings->getHost(), + 'port' => $this->connectionSettings->getPort(), + 'path' => $this->connectionSettings->getPath(), + 'scheme' => $this->connectionSettings->getScheme(), + 'core' => $this->connectionSettings->getCore(), ], ], ]; @@ -78,6 +89,7 @@ public function connect() $adapter = new Curl(); $eventDispatcher = new EventDispatcher(); $adapter->setTimeout((int) $currentConnectionSettings['timeout']); + // create a client instance $client = new Client($adapter, $eventDispatcher, $connectionSettings); @@ -338,7 +350,8 @@ protected function addFacetQueries(): void if (array_key_exists('id', $facetQuery) && array_key_exists('query', $facetQuery)) { $queryForFacet->createQuery($facetQuery['id'], $facetQuery['query']); } else { - $this->logger->error(sprintf('TypoScript facet »%s«, facetQuery %s does not have the required keys »id« and »query«. Ignoring this facetQuery.', $facetID, $facetQueryIndex), + $this->logger->error(sprintf('TypoScript facet »%s«, facetQuery %s does not have the required keys »id« and »query«. Ignoring this facetQuery.', + $facetID, $facetQueryIndex), [ 'facetQuery' => $facetQuery, 'facetConfiguration' => $facetConfiguration, @@ -358,7 +371,8 @@ protected function addFacetQueries(): void ->setSort($facet['sortOrder']); } } else { - $this->logger->warning(sprintf('TypoScript facet %s does not have the required key »id«. Ignoring this facet.', $key), + $this->logger->warning(sprintf('TypoScript facet %s does not have the required key »id«. Ignoring this facet.', + $key), [ 'facet' => $facet, 'facetConfiguration' => $facetConfiguration, @@ -542,7 +556,8 @@ protected function addSortOrdersToTemplate(array $arguments): void $sortOptions['default'] = $sortOption['sortCriteria']; } } else { - $this->logger->warning(sprintf('TypoScript sort option »%s« does not have the required keys »id« and »sortCriteria. Ignoring this setting.', $sortOptionIndex), + $this->logger->warning(sprintf('TypoScript sort option »%s« does not have the required keys »id« and »sortCriteria. Ignoring this setting.', + $sortOptionIndex), [ 'sortOption' => $sortOption, ] @@ -576,13 +591,15 @@ protected function addSortStringForQuery(string $sortString): void if ('desc' === $sortCriterionParts[1]) { $sortDirection = Query::SORT_DESC; } elseif ('asc' !== $sortCriterionParts[1]) { - $this->logger->warning(sprintf('sort criterion »%s«’s sort direction is »%s« It should be »asc« or »desc«. Ignoring it.', $sortCriterion, $sortCriterionParts[1])); + $this->logger->warning(sprintf('sort criterion »%s«’s sort direction is »%s« It should be »asc« or »desc«. Ignoring it.', + $sortCriterion, $sortCriterionParts[1])); continue; } $this->query->addSort($sortCriterionParts[0], $sortDirection); } else { - $this->logger->warning('sort criterion »%s« does not have the required form »fieldName [asc|desc]«. Ignoring it.', $sortCriterion); + $this->logger->warning('sort criterion »%s« does not have the required form »fieldName [asc|desc]«. Ignoring it.', + $sortCriterion); } } } @@ -794,7 +811,8 @@ protected function getFacetQuery(array $facetConfig, string $queryTerm): ?string } if (null === $queryString) { - $this->logger->info(sprintf('Results for Facet »%s« with facetQuery ID »%s« were requested, but this facetQuery is not configured. Building a generic facet query instead.', $facetConfig['id'], $queryTerm), + $this->logger->info(sprintf('Results for Facet »%s« with facetQuery ID »%s« were requested, but this facetQuery is not configured. Building a generic facet query instead.', + $facetConfig['id'], $queryTerm), [ 'requestArguments' => $this->requestArguments, 'facetConfig' => $facetConfig, @@ -881,12 +899,14 @@ protected function getRecordsWithUnderlyingQuery(array $assignments, array $inde $assignments['document-next-number'] = $index['nextIndex'] + 1; } } else { - $this->logger->error(sprintf('»detail« action query with underlying query could not retrieve record id »%d«.', $id), + $this->logger->error(sprintf('»detail« action query with underlying query could not retrieve record id »%d«.', + $id), ['arguments' => $arguments] ); } } else { - $this->logger->error('»detail« action query with underlying query returned no results.', ['arguments' => $arguments]); + $this->logger->error('»detail« action query with underlying query returned no results.', + ['arguments' => $arguments]); } } catch (HttpException $httpException) { $this->logger->error('Solr Exception (Timeout?)', @@ -922,7 +942,8 @@ protected function getTheRecordSpecified($id, $assignments) $resultSet = $selectResults->getDocuments(); $assignments['document'] = $resultSet[0]; } else { - $this->logger->error(sprintf('»detail« action query for id »%d« returned no results.', $id), ['arguments' => $this->getRequestArguments()]); + $this->logger->error(sprintf('»detail« action query for id »%d« returned no results.', $id), + ['arguments' => $this->getRequestArguments()]); } } catch (HttpException $httpException) { $this->logger->error('Solr Exception (Timeout?)', @@ -956,7 +977,8 @@ protected function queryComponentsForQueryParameters(array $queryParameters): ar $queryArguments = $queryParameters[$fieldID]; $queryAlternate = null; $queryTerms = null; - if (is_array($queryArguments) && array_key_exists('alternate', $queryArguments) && array_key_exists('queryAlternate', $fieldInfo)) { + if (is_array($queryArguments) && array_key_exists('alternate', + $queryArguments) && array_key_exists('queryAlternate', $fieldInfo)) { $queryAlternate = $queryArguments['alternate']; if (array_key_exists('term', $queryArguments)) { $queryTerms = $queryArguments['term']; @@ -1015,15 +1037,19 @@ protected function queryComponentsForQueryParameters(array $queryParameters): ar $magicFieldPrefix = ''; - if ((array_key_exists('luceneMatchVersionNumber', $this->settings) && (int) $this->settings['luceneMatchVersionNumber'] < 8) || (!array_key_exists('luceneMatchVersionNumber', $this->settings))) { + if ((array_key_exists('luceneMatchVersionNumber', + $this->settings) && (int) $this->settings['luceneMatchVersionNumber'] < 8) || (!array_key_exists('luceneMatchVersionNumber', + $this->settings))) { $magicFieldPrefix = '_query_:'; } if ($this->settings['features']['eDisMax']) { - $queryPart = $magicFieldPrefix.'{!edismax}'.$this->query->getHelper()->escapePhrase(vsprintf($queryFormat, $queryTerms)); + $queryPart = $magicFieldPrefix.'{!edismax}'.$this->query->getHelper()->escapePhrase(vsprintf($queryFormat, + $queryTerms)); $queryPart = str_replace('"', '', $queryPart); } else { - $queryPart = $magicFieldPrefix.$this->query->getHelper()->escapePhrase(vsprintf($queryFormat, $queryTerms)); + $queryPart = $magicFieldPrefix.$this->query->getHelper()->escapePhrase(vsprintf($queryFormat, + $queryTerms)); } if ('' !== $queryPart && '0' !== $queryPart) { @@ -1149,4 +1175,9 @@ protected function tagForFacet(string $facetID): string { return 'facet-'.$facetID; } + + public function getConnectionSettings(): Connection + { + return $this->connectionSettings; + } }