Skip to content

Commit

Permalink
[!!!][TASK] Remove deprecated Node class
Browse files Browse the repository at this point in the history
Removes deprecated class ApacheSolrForTypo3\Solr\System\Solr\Node
and it's usages.

- Replace deprecated Node class with Endpoint
- Replace getUsername/getPassword with getAuthentication()
- Change SolrConnection->getNode() to SolrConnection->getEndpoint

Resolves: #3630, #3043
Replaces: #3518
  • Loading branch information
dkd-friedrich authored and dkd-kaehm committed Oct 6, 2023
1 parent 81b8f2b commit 666529c
Show file tree
Hide file tree
Showing 19 changed files with 220 additions and 341 deletions.
41 changes: 33 additions & 8 deletions Classes/ConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
use ApacheSolrForTypo3\Solr\Domain\Site\Site;
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
use ApacheSolrForTypo3\Solr\Exception\InvalidArgumentException;
use ApacheSolrForTypo3\Solr\Exception\InvalidConnectionException;
use ApacheSolrForTypo3\Solr\System\Records\Pages\PagesRepository as PagesRepositoryAtExtSolr;
use ApacheSolrForTypo3\Solr\System\Solr\Node;
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
use ApacheSolrForTypo3\Solr\System\Util\SiteUtility;
use Doctrine\DBAL\Exception as DBALException;
use Solarium\Core\Client\Endpoint;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Site\Entity\Site as Typo3Site;

use TYPO3\CMS\Core\Utility\GeneralUtility;

use function json_encode;
Expand Down Expand Up @@ -57,25 +59,48 @@ public function __construct(
}

/**
* Creates a solr connection for read and write endpoints
* Creates a Solr connection for read and write endpoints
*
* @throw InvalidConnectionException
*/
public function getSolrConnectionForNodes(array $readNodeConfiguration, array $writeNodeConfiguration): SolrConnection
public function getSolrConnectionForEndpoints(array $readEndpointConfiguration, array $writeEndpointConfiguration): SolrConnection
{
$connectionHash = md5(json_encode($readNodeConfiguration) . json_encode($writeNodeConfiguration));
$connectionHash = md5(json_encode($readEndpointConfiguration) . json_encode($writeEndpointConfiguration));
if (!isset(self::$connections[$connectionHash])) {
$readNode = Node::fromArray($readNodeConfiguration);
$writeNode = Node::fromArray($writeNodeConfiguration);
self::$connections[$connectionHash] = GeneralUtility::makeInstance(SolrConnection::class, $readNode, $writeNode);
$readEndpoint = new Endpoint($readEndpointConfiguration);
if (!$this->isValidEndpoint($readEndpoint)) {
throw new InvalidConnectionException('Invalid read endpoint');
}

$writeEndpoint = new Endpoint($writeEndpointConfiguration);
if (!$this->isValidEndpoint($writeEndpoint)) {
throw new InvalidConnectionException('Invalid write endpoint');
}

self::$connections[$connectionHash] = GeneralUtility::makeInstance(SolrConnection::class, $readEndpoint, $writeEndpoint);
}

return self::$connections[$connectionHash];
}

/**
* Checks if endpoint is valid
*/
protected function isValidEndpoint(Endpoint $endpoint): bool
{
return
!empty($endpoint->getHost())
&& !empty($endpoint->getPort())
&& !empty($endpoint->getCore())
;
}

/**
* Creates a solr configuration from the configuration array and returns it.
*/
public function getConnectionFromConfiguration(array $solrConfiguration): SolrConnection
{
return $this->getSolrConnectionForNodes($solrConfiguration['read'], $solrConfiguration['write']);
return $this->getSolrConnectionForEndpoints($solrConfiguration['read'], $solrConfiguration['write']);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions Classes/Exception/InvalidConnectionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

namespace ApacheSolrForTypo3\Solr\Exception;

use ApacheSolrForTypo3\Solr\Exception;

/**
* Exception that is thrown if a Solr connection is invalid
*/
class InvalidConnectionException extends Exception {}
2 changes: 1 addition & 1 deletion Classes/IndexQueue/FrontendHelper/PageIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public function index(Item $indexQueueItem, TypoScriptFrontendController $tsfe):
$this->responseData['solrConnection'] = [
'rootPage' => $indexQueueItem->getRootPageUid(),
'sys_language_uid' => $tsfe->getLanguage()->getLanguageId(),
'solr' => (string)$this->solrConnection->getNode('write'),
'solr' => $this->solrConnection->getEndpoint('write')->getCoreBaseUri(),
];

foreach ($this->documentsSentToSolr as $document) {
Expand Down
2 changes: 1 addition & 1 deletion Classes/Report/SolrStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ protected function getConnectionStatus(Site $site, array $solrConnection): Statu
$this->responseStatus = ContextualFeedbackSeverity::OK;

$solrAdmin = $this->connectionManager
->getSolrConnectionForNodes($solrConnection['read'], $solrConnection['write'])
->getSolrConnectionForEndpoints($solrConnection['read'], $solrConnection['write'])
->getAdminService();

$solrVersion = $this->checkSolrVersion($solrAdmin);
Expand Down
144 changes: 0 additions & 144 deletions Classes/System/Solr/Node.php

This file was deleted.

28 changes: 14 additions & 14 deletions Classes/System/Solr/SolrConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Psr\Http\Message\StreamFactoryInterface;
use Solarium\Client;
use Solarium\Core\Client\Adapter\Psr18Adapter;
use Solarium\Core\Client\Endpoint;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
Expand All @@ -58,9 +59,9 @@ class SolrConnection
protected ?SchemaParser $schemaParser = null;

/**
* @var Node[]
* @var Endpoint[]
*/
protected array $nodes = [];
protected array $endpoints = [];

protected ?SolrLogManager $logger = null;

Expand All @@ -83,8 +84,8 @@ class SolrConnection
* @throws NotFoundExceptionInterface
*/
public function __construct(
Node $readNode,
Node $writeNode,
Endpoint $readEndpoint,
Endpoint $writeEndpoint,
TypoScriptConfiguration $configuration = null,
SynonymParser $synonymParser = null,
StopWordParser $stopWordParser = null,
Expand All @@ -95,9 +96,9 @@ public function __construct(
StreamFactoryInterface $streamFactory = null,
EventDispatcherInterface $eventDispatcher = null,
) {
$this->nodes['read'] = $readNode;
$this->nodes['write'] = $writeNode;
$this->nodes['admin'] = $writeNode;
$this->endpoints['read'] = $readEndpoint;
$this->endpoints['write'] = $writeEndpoint;
$this->endpoints['admin'] = $writeEndpoint;
$this->configuration = $configuration ?? Util::getSolrConfiguration();
$this->synonymParser = $synonymParser;
$this->stopWordParser = $stopWordParser;
Expand All @@ -112,9 +113,9 @@ public function __construct(
/**
* Returns Endpoint by key
*/
public function getNode(string $key): Node
public function getEndpoint(string $key): Endpoint
{
return $this->nodes[$key];
return $this->endpoints[$key];
}

/**
Expand Down Expand Up @@ -200,13 +201,12 @@ protected function buildWriteService(): SolrWriteService
*/
protected function initializeClient(Client $client, string $endpointKey): Client
{
if (trim($this->getNode($endpointKey)->getUsername()) === '') {
$authentication = $this->getEndpoint($endpointKey)->getAuthentication();
if (trim($authentication['username'] ?? '') === '') {
return $client;
}

$username = $this->getNode($endpointKey)->getUsername();
$password = $this->getNode($endpointKey)->getPassword();
$this->setAuthenticationOnAllEndpoints($client, $username, $password);
$this->setAuthenticationOnAllEndpoints($client, $authentication['username'], $authentication['password']);

return $client;
}
Expand Down Expand Up @@ -236,7 +236,7 @@ protected function getClient(string $endpointKey): Client
$client->getPlugin('postbigrequest');
$client->clearEndpoints();

$newEndpointOptions = $this->getNode($endpointKey)->getSolariumClientOptions();
$newEndpointOptions = $this->getEndpoint($endpointKey)->getOptions();
$newEndpointOptions['key'] = $endpointKey;
$client->createEndpoint($newEndpointOptions, true);

Expand Down
12 changes: 4 additions & 8 deletions Classes/System/Util/SiteUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,17 @@ public static function getSolrConnectionConfiguration(
'scheme' => self::getConnectionProperty($typo3Site, 'scheme', $languageUid, 'read', 'http'),
'host' => self::getConnectionProperty($typo3Site, 'host', $languageUid, 'read', 'localhost'),
'port' => (int)self::getConnectionProperty($typo3Site, 'port', $languageUid, 'read', 8983),
// @todo: transform core to path
'path' =>
self::getConnectionProperty($typo3Site, 'path', $languageUid, 'read', '/solr/') .
$solrReadCore . '/' ,
'path' => self::getConnectionProperty($typo3Site, 'path', $languageUid, 'read', ''),
'core' => $solrReadCore,
'username' => self::getConnectionProperty($typo3Site, 'username', $languageUid, 'read', ''),
'password' => self::getConnectionProperty($typo3Site, 'password', $languageUid, 'read', ''),
],
'write' => [
'scheme' => self::getConnectionProperty($typo3Site, 'scheme', $languageUid, 'write', 'http'),
'host' => self::getConnectionProperty($typo3Site, 'host', $languageUid, 'write', 'localhost'),
'port' => (int)self::getConnectionProperty($typo3Site, 'port', $languageUid, 'write', 8983),
// @todo: transform core to path
'path' =>
self::getConnectionProperty($typo3Site, 'path', $languageUid, 'write', '/solr/') .
$solrWriteCore . '/' ,
'path' => self::getConnectionProperty($typo3Site, 'path', $languageUid, 'write', ''),
'core' => $solrWriteCore,
'username' => self::getConnectionProperty($typo3Site, 'username', $languageUid, 'write', ''),
'password' => self::getConnectionProperty($typo3Site, 'password', $languageUid, 'write', ''),
],
Expand Down
2 changes: 1 addition & 1 deletion Configuration/SiteConfiguration/Overrides/sites.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

$GLOBALS['SiteConfiguration']['site']['columns']['solr_path_read'] = [
'label' => 'URL path to Apache Solr server',
'description' => 'I.e. if you use Hosted-Solr.com the path inside the admin panel. Should not contain "/solr/".',
'description' => 'Must not contain "/solr/"! Unlesss you have an additional "solr" segment in your path like "http://localhost:8983/solr/solr/core_en".',
'config' => [
'type' => 'input',
'eval' => 'trim',
Expand Down
Loading

0 comments on commit 666529c

Please sign in to comment.