Skip to content

Commit

Permalink
[BUGFIX] 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 authored and dkd-kaehm committed Dec 22, 2022
1 parent 870f6aa commit dbbbecb
Show file tree
Hide file tree
Showing 15 changed files with 291 additions and 119 deletions.
30 changes: 30 additions & 0 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
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
62 changes: 27 additions & 35 deletions Classes/Domain/Site/SiteHashService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +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\Utility\GeneralUtility;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
use TYPO3\CMS\Core\Site\SiteFinder;

/**
* SiteHashService
Expand All @@ -30,6 +32,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 +61,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 +97,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,37 +119,14 @@ 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;
}

/**
* @return Site[]
* @throws DBALDriverException
* @throws Throwable
*/
protected function getAvailableSites(): array
{
return $this->getSiteRepository()->getAvailableSites();
}

/**
* @param int $pageId
* @return SiteInterface
*/
protected function getSiteByPageId(int $pageId): SiteInterface
{
return $this->getSiteRepository()->getSiteByPageId($pageId);
}

/**
* Get a reference to SiteRepository
*
* @return SiteRepository
*/
protected function getSiteRepository(): SiteRepository
{
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 dbbbecb

Please sign in to comment.