Skip to content

Commit

Permalink
[BUGFIX:BP:11.5] Fix frontend Solr connection initialization
Browse files Browse the repository at this point in the history
Solr plugins are initializing Solr connections for all available
languages, this causes wrong language initializations in the
PageRenderer, as all created TypoScriptFrontendController instances
will overwrite the language setting.

By allowing to fetch the Solr configuration by TYPO3 site and a
specific language this issue is solved and the initialization
overhead is reduced.

Resolves: #3423
  • Loading branch information
dkd-friedrich committed Dec 16, 2022
1 parent 4fb31c6 commit 40d5062
Show file tree
Hide file tree
Showing 15 changed files with 316 additions and 91 deletions.
39 changes: 37 additions & 2 deletions Classes/ConnectionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
use ApacheSolrForTypo3\Solr\System\Records\SystemLanguage\SystemLanguageRepository;
use ApacheSolrForTypo3\Solr\System\Solr\Node;
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection;
use ApacheSolrForTypo3\Solr\System\Util\SiteUtility;
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
use InvalidArgumentException;
use function json_encode;
use Throwable;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Site\Entity\Site as Typo3Site;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
Expand Down Expand Up @@ -127,6 +129,34 @@ public function getConnectionByPageId(int $pageId, int $language = 0, string $mo
}
}

/**
* Gets a Solr connection for a TYPO3 site and language
*
* @param Typo3Site $typo3Site
* @param int $languageUid
* @return SolrConnection A Solr connection.
* @throws NoSolrConnectionFoundException
*/
public function getConnectionByTypo3Site(Typo3Site $typo3Site, int $languageUid = 0): SolrConnection
{
$config = SiteUtility::getSolrConnectionConfiguration($typo3Site, $languageUid);
if ($config === null) {
throw $this->buildNoConnectionExceptionForPageAndLanguage(
$typo3Site->getRootPageId(),
$languageUid
);
}

try {
return $this->getConnectionFromConfiguration($config);
} catch (InvalidArgumentException $e) {
throw $this->buildNoConnectionExceptionForPageAndLanguage(
$typo3Site->getRootPageId(),
$languageUid
);
}
}

/**
* Gets a Solr connection for a root page ID.
*
Expand Down Expand Up @@ -189,11 +219,16 @@ public function getConnectionsBySite(Site $site): array
*
* @param array $connection Connection configuration
* @return string Connection label
* @todo Remove, since not used, or take used.
* @deprecated since v11.5 and will be removed in v11.6, as unused since v11.0.0
*/
protected function buildConnectionLabel(array $connection): string
{
return $connection['rootPageTitle']
trigger_error(
'ConnectionManager->buildConnectionLabel is deprecated since v11.5 and will be removed in v11.6, as unsed since v11.0.0',
E_USER_DEPRECATED
);

return $connection['connectionKey']
. ' (pid: ' . $connection['rootPageUid']
. ', language: ' . $this->systemLanguageRepository->findOneLanguageTitleByLanguageId($connection['language'])
. ') - Read node: '
Expand Down
8 changes: 5 additions & 3 deletions Classes/Controller/AbstractBaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
use ApacheSolrForTypo3\Solr\System\Service\ConfigurationService;
use ApacheSolrForTypo3\Solr\Util;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand Down Expand Up @@ -223,9 +222,12 @@ protected function initializeSettings()
*/
protected function initializeSearch()
{
/** @var ConnectionManager $solrConnection */
try {
$solrConnection = $this->objectManager->get(ConnectionManager::class)->getConnectionByPageId($this->typoScriptFrontendController->id, Util::getLanguageUid(), $this->typoScriptFrontendController->MP);
$solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByTypo3Site(
$this->typoScriptFrontendController->getSite(),
$this->typoScriptFrontendController->getLanguage()->getLanguageId()
);

$search = $this->objectManager->get(Search::class, $solrConnection);

/** @noinspection PhpParamsInspection */
Expand Down
50 changes: 45 additions & 5 deletions Classes/Domain/Site/SiteHashService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

namespace ApacheSolrForTypo3\Solr\Domain\Site;

use ApacheSolrForTypo3\Solr\System\Util\SiteUtility;
use Doctrine\DBAL\Driver\Exception as DBALDriverException;
use Throwable;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
Expand All @@ -30,6 +33,16 @@
*/
class SiteHashService
{
/**
* SiteFinder
*/
protected SiteFinder $siteFinder;

public function __construct(SiteFinder $siteFinder)
{
$this->siteFinder = $siteFinder;
}

/**
* Resolves magic keywords in allowed sites configuration.
* Supported keywords:
Expand All @@ -49,7 +62,7 @@ public function getAllowedSitesForPageIdAndAllowedSitesConfiguration(
?string $allowedSitesConfiguration = ''
): string {
if ($allowedSitesConfiguration === '__all') {
return $this->getDomainListOfAllSites();
return $this->getDomainListOfAllSites();
}
if ($allowedSitesConfiguration === '*') {
return '*';
Expand Down Expand Up @@ -85,10 +98,13 @@ public function getSiteHashForDomain(string $domain): string
*/
protected function getDomainListOfAllSites(): string
{
$sites = $this->getAvailableSites();
$sites = $this->siteFinder->getAllSites();
$domains = [];
foreach ($sites as $site) {
$domains[] = $site->getDomain();
foreach ($sites as $typo3Site) {
$connections = SiteUtility::getAllSolrConnectionConfigurations($typo3Site);
if (!empty($connections)) {
$domains[] = $typo3Site->getBase()->getHost();
}
}

return implode(',', $domains);
Expand All @@ -104,7 +120,13 @@ protected function getDomainListOfAllSites(): string
*/
protected function getDomainByPageIdAndReplaceMarkers(int $pageId, string $allowedSitesConfiguration): string
{
$domainOfPage = $this->getSiteByPageId($pageId)->getDomain();
try {
$typo3Site = $this->siteFinder->getSiteByPageId($pageId);
$domainOfPage = $typo3Site->getBase()->getHost();
} catch (SiteNotFoundException $e) {
return '';
}

$allowedSites = str_replace(['__solr_current_site', '__current_site'], $domainOfPage, $allowedSitesConfiguration);
return (string)$allowedSites;
}
Expand All @@ -113,28 +135,46 @@ protected function getDomainByPageIdAndReplaceMarkers(int $pageId, string $allow
* @return Site[]
* @throws DBALDriverException
* @throws Throwable
* @deprecated since v11.5 and will be removed in v11.6, SiteHashService no longer requires/uses Solr sites
*/
protected function getAvailableSites(): array
{
trigger_error(
'SiteHashService no longer requires/uses Solr sites ' . __METHOD__ . ' of class ' . __CLASS__ . ' is deprecated since v11.5 and will be removed in v11.6. Use SiteFinder instead or initizalize own objects',
E_USER_DEPRECATED
);

return $this->getSiteRepository()->getAvailableSites();
}

/**
* @param int $pageId
* @return SiteInterface
* @deprecated since v11.5 and will be removed in v11.6, SiteHashService no longer requires/uses Solr sites
*/
protected function getSiteByPageId(int $pageId): SiteInterface
{
trigger_error(
'SiteHashService no longer requires/uses Solr sites ' . __METHOD__ . ' of class ' . __CLASS__ . ' is deprecated since v11.5 and will be removed in v11.6. Use SiteFinder instead or initizalize own objects',
E_USER_DEPRECATED
);

return $this->getSiteRepository()->getSiteByPageId($pageId);
}

/**
* Get a reference to SiteRepository
*
* @return SiteRepository
* @deprecated since v11.5 and will be removed in v11.6, SiteHashService no longer requires/uses Solr sites
*/
protected function getSiteRepository(): SiteRepository
{
trigger_error(
'SiteHashService no longer requires/uses Solr sites ' . __METHOD__ . ' of class ' . __CLASS__ . ' is deprecated since v11.5 and will be removed in v11.6. Use SiteFinder instead or initizalize own objects',
E_USER_DEPRECATED
);

return GeneralUtility::makeInstance(SiteRepository::class);
}
}
36 changes: 3 additions & 33 deletions Classes/Domain/Site/SiteRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,39 +290,9 @@ protected function buildTypo3ManagedSite(array $rootPageRecord): ?Site
$solrConnectionConfigurations = [];

foreach ($availableLanguageIds as $languageUid) {
$solrEnabled = SiteUtility::getConnectionProperty($typo3Site, 'enabled', $languageUid, 'read', true);
$solrReadCore = SiteUtility::getConnectionProperty($typo3Site, 'core', $languageUid, 'read');
$solrWriteCore = SiteUtility::getConnectionProperty($typo3Site, 'core', $languageUid, 'write');
if ($solrEnabled && !empty($solrReadCore) && !empty($solrWriteCore)) {
$solrConnectionConfigurations[$languageUid] = [
'connectionKey' => $rootPageRecord['uid'] . '|' . $languageUid,
'rootPageTitle' => $rootPageRecord['title'],
'rootPageUid' => $rootPageRecord['uid'],
'read' => [
'scheme' => SiteUtility::getConnectionProperty($typo3Site, 'scheme', $languageUid, 'read', 'http'),
'host' => SiteUtility::getConnectionProperty($typo3Site, 'host', $languageUid, 'read', 'localhost'),
'port' => (int)SiteUtility::getConnectionProperty($typo3Site, 'port', $languageUid, 'read', 8983),
// @todo: transform core to path
'path' =>
SiteUtility::getConnectionProperty($typo3Site, 'path', $languageUid, 'read', '/solr/') .
$solrReadCore . '/' ,
'username' => SiteUtility::getConnectionProperty($typo3Site, 'username', $languageUid, 'read', ''),
'password' => SiteUtility::getConnectionProperty($typo3Site, 'password', $languageUid, 'read', ''),
],
'write' => [
'scheme' => SiteUtility::getConnectionProperty($typo3Site, 'scheme', $languageUid, 'write', 'http'),
'host' => SiteUtility::getConnectionProperty($typo3Site, 'host', $languageUid, 'write', 'localhost'),
'port' => (int)SiteUtility::getConnectionProperty($typo3Site, 'port', $languageUid, 'write', 8983),
// @todo: transform core to path
'path' =>
SiteUtility::getConnectionProperty($typo3Site, 'path', $languageUid, 'write', '/solr/') .
$solrWriteCore . '/' ,
'username' => SiteUtility::getConnectionProperty($typo3Site, 'username', $languageUid, 'write', ''),
'password' => SiteUtility::getConnectionProperty($typo3Site, 'password', $languageUid, 'write', ''),
],

'language' => $languageUid,
];
$solrConnection = SiteUtility::getSolrConnectionConfiguration($typo3Site, $languageUid, );
if ($solrConnection !== null) {
$solrConnectionConfigurations[$languageUid] = $solrConnection;
}
}

Expand Down
66 changes: 66 additions & 0 deletions Classes/System/Util/SiteUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,72 @@ public static function getConnectionProperty(
return $value;
}

/**
* Builds the Solr connection configuration
*
* @param Site $typo3Site
* @param int $languageUid
* @return array|null
*/
public static function getSolrConnectionConfiguration(Site $typo3Site, int $languageUid): ?array
{
$solrEnabled = self::getConnectionProperty($typo3Site, 'enabled', $languageUid, 'read', true);
$solrReadCore = self::getConnectionProperty($typo3Site, 'core', $languageUid, 'read');
$solrWriteCore = self::getConnectionProperty($typo3Site, 'core', $languageUid, 'write');
if (!$solrEnabled || empty($solrReadCore) || empty($solrWriteCore)) {
return null;
}

$rootPageUid = $typo3Site->getRootPageId();
return [
'connectionKey' => $rootPageUid . '|' . $languageUid,
'rootPageUid' => $rootPageUid,
'read' => [
'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 . '/' ,
'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 . '/' ,
'username' => self::getConnectionProperty($typo3Site, 'username', $languageUid, 'write', ''),
'password' => self::getConnectionProperty($typo3Site, 'password', $languageUid, 'write', ''),
],

'language' => $languageUid,
];
}

/**
* Builds the Solr connection configuration for all languages of given TYPO3 site
*
* @param Site $typo3Site
* @return array
*/
public static function getAllSolrConnectionConfigurations(Site $typo3Site): array
{
$connections = [];
foreach ($typo3Site->getLanguages() as $language) {
$connection = self::getSolrConnectionConfiguration($typo3Site, $language->getLanguageId());
if ($connection !== null) {
$connections[$language->getLanguageId()] = $connection;
}
}

return $connections;
}

/**
* Resolves site configuration properties.
* Language context properties have precedence over global settings.
Expand Down
5 changes: 5 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ services:
$frontendEnvironment: '@ApacheSolrForTypo3\Solr\FrontendEnvironment'
$tcaService: '@ApacheSolrForTypo3\Solr\System\TCA\TCAService'
$indexQueue: '@ApacheSolrForTypo3\Solr\IndexQueue\Queue'

ApacheSolrForTypo3\Solr\Domain\Site\SiteHashService:
public: true
autowire: true

ApacheSolrForTypo3\Solr\EventListener\EnhancedRouting\CachedUrlModifier:
tags:
- name: event.listener
Expand Down
17 changes: 15 additions & 2 deletions Tests/Unit/Domain/Search/ApacheSolrDocument/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ class RepositoryTest extends UnitTest
public function findOneByPageIdAndByLanguageIdReturnsFirstFoundDocument()
{
$apacheSolrDocumentCollection = [new Document(), new Document()];
$apacheSolrDocumentRepository = $this->getAccessibleMock(Repository::class, ['findByPageIdAndByLanguageId']);
$apacheSolrDocumentRepository = $this->getAccessibleMock(
Repository::class,
['findByPageIdAndByLanguageId'],
[],
'',
false
);

$apacheSolrDocumentRepository
->expects(self::exactly(1))
->method('findByPageIdAndByLanguageId')
Expand Down Expand Up @@ -93,7 +100,13 @@ public function findByPageIdAndByLanguageIdThrowsInvalidArgumentExceptionIfLangu
public function findByPageIdAndByLanguageIdReturnsEmptyCollectionIfConnectionToSolrServerCanNotBeEstablished()
{
/* @var $apacheSolrDocumentRepository Repository */
$apacheSolrDocumentRepository = $this->getAccessibleMock(Repository::class, ['initializeSearch']);
$apacheSolrDocumentRepository = $this->getAccessibleMock(
Repository::class,
['initializeSearch'],
[],
'',
false
);
$apacheSolrDocumentRepository
->expects(self::exactly(1))
->method('initializeSearch')
Expand Down
Loading

0 comments on commit 40d5062

Please sign in to comment.