From 890d5776491db87fbda8f47869b681cddaedf56b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Katarina=20Mio=C4=8Di=C4=87?= Date: Thu, 9 May 2024 12:28:42 +0200 Subject: [PATCH] NGSTACK-843 move getSiteConfigForContent method to new service SiteAccessConfigResolver --- .../PageTextExtractor/PageTextExtractor.php | 46 ++------------- .../Common/SiteAccessConfigResolver.php | 56 +++++++++++++++++++ .../BlockPageTextFieldMapper.php | 5 +- .../common/layouts_page_text_indexing.yaml | 10 +++- .../elasticsearch/document_factory.yaml | 1 + 5 files changed, 74 insertions(+), 44 deletions(-) create mode 100644 lib/Core/Search/Common/SiteAccessConfigResolver.php diff --git a/lib/Core/Search/Common/PageTextExtractor/PageTextExtractor.php b/lib/Core/Search/Common/PageTextExtractor/PageTextExtractor.php index dc6def7d..95e94efa 100644 --- a/lib/Core/Search/Common/PageTextExtractor/PageTextExtractor.php +++ b/lib/Core/Search/Common/PageTextExtractor/PageTextExtractor.php @@ -11,6 +11,7 @@ use Ibexa\Contracts\Core\Persistence\Content\Location\Handler as LocationHandler; use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException; use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException; +use Netgen\IbexaSearchExtra\Core\Search\Common\SiteAccessConfigResolver; use Netgen\IbexaSearchExtra\Exception\IndexPageUnavailableException; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; @@ -38,14 +39,11 @@ class PageTextExtractor extends \Netgen\IbexaSearchExtra\Core\Search\Common\Page private LoggerInterface $logger; - /** - * @param array $sitesConfig - */ public function __construct( private readonly ContentHandler $contentHandler, private readonly LocationHandler $locationHandler, private readonly RouterInterface $router, - private readonly array $sitesConfig + private readonly SiteAccessConfigResolver $siteAccessConfigResolver ) { $this->logger = new NullLogger(); } @@ -63,7 +61,7 @@ public function setLogger(LoggerInterface $logger): void */ public function extractPageText(int $contentId, string $languageCode): array { - $siteConfig = $this->getSiteConfigForContent($contentId); + $siteConfig = $this->siteAccessConfigResolver->getSiteConfigForContent($contentId); if (isset($this->cache[$contentId][$languageCode])) { return $this->cache[$contentId][$languageCode]; @@ -128,7 +126,7 @@ private function generateUrl(string $languageCode, int $contentId, array $siteCo private function resolveSiteAccess(ContentInfo $contentInfo, string $languageCode): string { - $siteConfig = $this->getSiteConfigForContent($contentInfo->id); + $siteConfig = $this->siteAccessConfigResolver->getSiteConfigForContent($contentInfo->id); if (!isset($siteConfig['languages_siteaccess_map'][$languageCode])) { throw new RuntimeException( @@ -177,7 +175,7 @@ private function recursiveExtractTextArray(DOMNode $node, array &$textArray, int private function getFieldName(DOMNode $node, int $contentId): null|string { - $siteConfig = $this->getSiteConfigForContent($contentId); + $siteConfig = $this->siteAccessConfigResolver->getSiteConfigForContent($contentId); $fields = $siteConfig['fields']; foreach ($fields as $level => $tags) { @@ -269,38 +267,4 @@ private function extractTextArray(string $html, int $contentId): array return $textArray; } - - public function getSiteConfigForContent(int $contentId): array - { - $contentInfo = $this->contentHandler->loadContentInfo($contentId); - - try { - $location = $this->locationHandler->load($contentInfo->mainLocationId); - } catch (NotFoundException) { - throw new RuntimeException( - sprintf( - 'Content #%d does not have a location', - $contentInfo->id, - ), - ); - } - - $pathString = $location->pathString; - $pathArray = explode('/', $pathString); - - foreach ($this->sitesConfig as $site => $siteConfig) { - if (in_array($siteConfig['tree_root_location_id'], $pathArray, false)) { - $siteConfig['site'] = $site; - return $siteConfig; - } - } - - throw new RuntimeException( - sprintf( - "Failed to match content ID %d to a siteaccess", - $contentInfo->id - ) - ); - - } } diff --git a/lib/Core/Search/Common/SiteAccessConfigResolver.php b/lib/Core/Search/Common/SiteAccessConfigResolver.php new file mode 100644 index 00000000..6ec4eac1 --- /dev/null +++ b/lib/Core/Search/Common/SiteAccessConfigResolver.php @@ -0,0 +1,56 @@ + $sitesConfig + */ + public function __construct( + private readonly ContentHandler $contentHandler, + private readonly LocationHandler $locationHandler, + private readonly array $sitesConfig + ) { + } + + public function getSiteConfigForContent(int $contentId): array + { + $contentInfo = $this->contentHandler->loadContentInfo($contentId); + + try { + $location = $this->locationHandler->load($contentInfo->mainLocationId); + } catch (NotFoundException) { + throw new RuntimeException( + sprintf( + 'Content #%d does not have a location', + $contentInfo->id, + ), + ); + } + + $pathString = $location->pathString; + $pathArray = explode('/', $pathString); + + foreach ($this->sitesConfig as $site => $siteConfig) { + if (in_array($siteConfig['tree_root_location_id'], $pathArray, false)) { + $siteConfig['site'] = $site; + return $siteConfig; + } + } + + throw new RuntimeException( + sprintf( + "Failed to match content ID %d to a siteaccess", + $contentInfo->id + ) + ); + + } +} diff --git a/lib/Core/Search/Elasticsearch/DocumentMapper/BlockTranslationFieldMapper/BlockPageTextFieldMapper.php b/lib/Core/Search/Elasticsearch/DocumentMapper/BlockTranslationFieldMapper/BlockPageTextFieldMapper.php index 1bde763b..7c5fa6fc 100644 --- a/lib/Core/Search/Elasticsearch/DocumentMapper/BlockTranslationFieldMapper/BlockPageTextFieldMapper.php +++ b/lib/Core/Search/Elasticsearch/DocumentMapper/BlockTranslationFieldMapper/BlockPageTextFieldMapper.php @@ -4,11 +4,11 @@ use Ibexa\Contracts\Core\Persistence\Content as SPIContent; use Ibexa\Contracts\Core\Persistence\Content\Type\Handler as ContentTypeHandler; -use Ibexa\Contracts\Core\Persistence\Content\Location\Handler as LocationHandler; use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException; use Ibexa\Contracts\Core\Search\Field; use Ibexa\Contracts\Core\Search\FieldType\FullTextField; use Netgen\IbexaSearchExtra\Core\Search\Common\PageTextExtractor; +use Netgen\IbexaSearchExtra\Core\Search\Common\SiteAccessConfigResolver; use Netgen\IbexaSearchExtra\Core\Search\Elasticsearch\DocumentMapper\BlockTranslationFieldMapper; class BlockPageTextFieldMapper extends BlockTranslationFieldMapper @@ -16,6 +16,7 @@ class BlockPageTextFieldMapper extends BlockTranslationFieldMapper public function __construct( private readonly PageTextExtractor $pageTextExtractor, private readonly ContentTypeHandler $contentTypeHandler, + private readonly SiteAccessConfigResolver $siteAccessConfigResolver ) {} public function accept(SPIContent $content, string $languageCode): bool @@ -28,7 +29,7 @@ public function accept(SPIContent $content, string $languageCode): bool */ public function mapFields(SPIContent $content, string $languageCode): array { - $siteConfig = $this->pageTextExtractor->getSiteConfigForContent($content->versionInfo->contentInfo->id); + $siteConfig = $this->siteAccessConfigResolver->getSiteConfigForContent($content->versionInfo->contentInfo->id); $fields = []; $contentType = $this->contentTypeHandler->load($content->versionInfo->contentInfo->contentTypeId); diff --git a/lib/Resources/config/search/common/layouts_page_text_indexing.yaml b/lib/Resources/config/search/common/layouts_page_text_indexing.yaml index 0c21bec7..3795e5fa 100644 --- a/lib/Resources/config/search/common/layouts_page_text_indexing.yaml +++ b/lib/Resources/config/search/common/layouts_page_text_indexing.yaml @@ -5,10 +5,18 @@ services: - '@Ibexa\Contracts\Core\Persistence\Content\Handler' - '@Ibexa\Contracts\Core\Persistence\Content\Location\Handler' - '@Ibexa\Bundle\Core\Routing\UrlAliasRouter' - - '%netgen_ibexa_search_extra.page_indexing.sites%' + - '@netgen.ibexa_search_extra.page_indexing.site_access_resolver' calls: - setLogger: ['@?logger'] + netgen.ibexa_search_extra.page_indexing.site_access_resolver: + class: Netgen\IbexaSearchExtra\Core\Search\Common\SiteAccessConfigResolver + arguments: + - '@Ibexa\Contracts\Core\Persistence\Content\Handler' + - '@Ibexa\Contracts\Core\Persistence\Content\Location\Handler' + - '%netgen_ibexa_search_extra.page_indexing.sites%' + + netgen.ibexa_search_extra.page_indexing: class: Netgen\IbexaSearchExtra\Command\IndexPageContentCommand tags: diff --git a/lib/Resources/config/search/elasticsearch/document_factory.yaml b/lib/Resources/config/search/elasticsearch/document_factory.yaml index 6b02b00c..b899cb70 100644 --- a/lib/Resources/config/search/elasticsearch/document_factory.yaml +++ b/lib/Resources/config/search/elasticsearch/document_factory.yaml @@ -11,6 +11,7 @@ services: arguments: - '@netgen.ibexa_search_extra.page_indexing.page_text_extractor' - '@Ibexa\Contracts\Core\Persistence\Content\Type\Handler' + - '@netgen.ibexa_search_extra.page_indexing.site_access_resolver' tags: - { name: netgen.ibexa_search_extra.elasticsearch.field_mapper.block_translation }