Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[!!!][FEATURE] Add new PSR-14 Event instead of SubstitutePageIndexer #3660

Merged
merged 1 commit into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions Classes/Domain/Search/ApacheSolrDocument/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use ApacheSolrForTypo3\Solr\Typo3PageContentExtractor;
use ApacheSolrForTypo3\Solr\Util;
use Doctrine\DBAL\Exception as DBALException;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

Expand All @@ -45,21 +44,19 @@ public function __construct(

/**
* This method can be used to build a Document from a TYPO3 page.
*
* @throws AspectNotFoundException
* @throws DBALException
*/
public function fromPage(
TypoScriptFrontendController $page,
string $url,
Rootline $pageAccessRootline,
string $mountPointParameter = '',
): Document {
/** @var Document $document */
$document = GeneralUtility::makeInstance(Document::class);
$site = $this->getSiteByPageId($page->id);
$pageId = $page->id;
$pageRecord = $page->page;

$document = GeneralUtility::makeInstance(Document::class);
$site = $this->getSiteByPageId($pageId);

$accessGroups = $this->getDocumentIdGroups($pageAccessRootline);
$documentId = $this->getPageDocumentId($page, $accessGroups, $mountPointParameter);

Expand All @@ -70,18 +67,18 @@ public function fromPage(
$document->setField('type', 'pages');

// system fields
$document->setField('uid', $page->id);
$document->setField('uid', $pageId);
$document->setField('pid', $pageRecord['pid']);

// variantId
$variantId = $this->variantIdBuilder->buildFromTypeAndUid('pages', $page->id);
$variantId = $this->variantIdBuilder->buildFromTypeAndUid('pages', $pageId);
$document->setField('variantId', $variantId);

$document->setField('typeNum', (int)$page->getPageArguments()->getPageType());
$document->setField('created', $pageRecord['crdate']);
$document->setField('changed', $pageRecord['SYS_LASTCHANGED']);

$rootline = $this->getRootLineFieldValue($page->id, $mountPointParameter);
$rootline = $this->getRootLineFieldValue($pageId, $mountPointParameter);
$document->setField('rootline', $rootline);

// access
Expand Down Expand Up @@ -157,12 +154,11 @@ public function fromRecord(array $itemRecord, string $type, int $rootPageUid, st
}

/**
* @throws AspectNotFoundException
* @throws DBALException
*/
protected function getPageDocumentId(TypoScriptFrontendController $frontendController, string $accessGroups, string $mountPointParameter): string
{
return Util::getPageDocumentId($frontendController->id, (int)$frontendController->getPageArguments()->getPageType(), Util::getLanguageUid(), $accessGroups, $mountPointParameter);
return Util::getPageDocumentId($frontendController->id, (int)$frontendController->getPageArguments()->getPageType(), $frontendController->getLanguage()->getLanguageId(), $accessGroups, $mountPointParameter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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\Event\Indexing;

use ApacheSolrForTypo3\Solr\IndexQueue\Item;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;

/**
* Allows third party extensions to replace or modify the page document
* created by the indexer.
*
* Previously used with
* $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['Indexer']['indexPageSubstitutePageDocument']
* with SubstitutePageIndexer and PageFieldMappingIndexer screens
*/
final class AfterPageDocumentIsCreatedForIndexingEvent
{
public function __construct(
private Document $document,
private readonly Item $indexQueueItem,
private readonly Site $site,
private readonly SiteLanguage $siteLanguage,
private readonly array $record,
private readonly TypoScriptConfiguration $configuration
) {
}

public function getDocument(): Document
{
return $this->document;
}

public function overrideDocument(Document $document): void
{
$this->document = $document;
}

public function getIndexQueueItem(): Item
{
return $this->indexQueueItem;
}

public function getIndexingConfigurationName(): string
{
return $this->indexQueueItem->getIndexingConfigurationName();
}

public function getSite(): Site
{
return $this->site;
}

public function getSiteLanguage(): SiteLanguage
{
return $this->siteLanguage;
}

public function getRecord(): array
{
return $this->record;
}

public function getConfiguration(): TypoScriptConfiguration
{
return $this->configuration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
* The TYPO3 project - inspiring people to share!
*/

namespace ApacheSolrForTypo3\Solr;
namespace ApacheSolrForTypo3\Solr\EventListener\PageIndexer;

use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use ApacheSolrForTypo3\Solr\Event\Indexing\AfterPageDocumentIsCreatedForIndexingEvent;
use ApacheSolrForTypo3\Solr\System\ContentObject\ContentObjectService;
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
Expand All @@ -32,31 +31,28 @@
*
* @author Ingo Renner <ingo@typo3.org>
*/
class AdditionalFieldsIndexer implements SubstitutePageIndexer
class AdditionalFieldsForPageIndexing
{
protected TypoScriptConfiguration $configuration;

protected array $additionalIndexingFields = [];

protected array $additionalFieldNames = [];

protected ContentObjectService $contentObjectService;

public function __construct(
TypoScriptConfiguration $configuration = null,
ContentObjectService $contentObjectService = null
) {
$this->configuration = $configuration ?? Util::getSolrConfiguration();
$this->additionalIndexingFields = $this->configuration->getIndexAdditionalFieldsConfiguration();
$this->additionalFieldNames = $this->configuration->getIndexMappedAdditionalFieldNames();
public function __construct(ContentObjectService $contentObjectService = null)
{
$this->contentObjectService = $contentObjectService ?? GeneralUtility::makeInstance(ContentObjectService::class);
}

/**
* Returns a substituted document for the currently being indexed page.
*/
public function getPageDocument(Document $originalPageDocument): Document
public function __invoke(AfterPageDocumentIsCreatedForIndexingEvent $event): void
{
$this->additionalIndexingFields = $event->getConfiguration()->getIndexAdditionalFieldsConfiguration();
$this->additionalFieldNames = $event->getConfiguration()->getIndexMappedAdditionalFieldNames();

$originalPageDocument = $event->getDocument();
$substitutePageDocument = clone $originalPageDocument;
$additionalFields = $this->getAdditionalFields();

Expand All @@ -66,8 +62,7 @@ public function getPageDocument(Document $originalPageDocument): Document
$substitutePageDocument->setField($fieldName, $fieldValue);
}
}

return $substitutePageDocument;
$event->overrideDocument($substitutePageDocument);
}

/**
Expand Down
42 changes: 12 additions & 30 deletions Classes/IndexQueue/FrontendHelper/PageFieldMappingIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
namespace ApacheSolrForTypo3\Solr\IndexQueue\FrontendHelper;

// TODO use/extend ApacheSolrForTypo3\Solr\IndexQueue\AbstractIndexer
use ApacheSolrForTypo3\Solr\Event\Indexing\AfterPageDocumentIsCreatedForIndexingEvent;
use ApacheSolrForTypo3\Solr\IndexQueue\AbstractIndexer;
use ApacheSolrForTypo3\Solr\IndexQueue\InvalidFieldNameException;
use ApacheSolrForTypo3\Solr\SubstitutePageIndexer;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
use ApacheSolrForTypo3\Solr\Util;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

Expand All @@ -33,37 +32,24 @@
*
* @author Ingo Renner <ingo@typo3.org>
*/
class PageFieldMappingIndexer implements SubstitutePageIndexer
class PageFieldMappingIndexer
{
protected TypoScriptConfiguration $configuration;

protected string $pageIndexingConfigurationName = 'pages';

public function __construct(TypoScriptConfiguration $configuration = null)
{
$this->configuration = $configuration == null ? Util::getSolrConfiguration() : $configuration;
}

public function setPageIndexingConfigurationName(string $pageIndexingConfigurationName): void
{
$this->pageIndexingConfigurationName = $pageIndexingConfigurationName;
}

/**
* Returns a substitute document for the currently being indexed page.
* Builds a substitute document for the currently being indexed page.
*
* Uses the original document and adds fields as defined in
* plugin.tx_solr.index.queue.pages.fields.
*
* @param Document $originalPageDocument The original page document.
*
* @return Document A Apache Solr Document object that replace the default page document
*/
public function getPageDocument(Document $originalPageDocument): Document
public function __invoke(AfterPageDocumentIsCreatedForIndexingEvent $event): void
{
$substitutePageDocument = clone $originalPageDocument;
$substitutePageDocument = clone $event->getDocument();
$this->configuration = $event->getConfiguration();
$this->pageIndexingConfigurationName = $event->getIndexingConfigurationName();

$mappedFields = $this->getMappedFields($originalPageDocument);
$mappedFields = $this->getMappedFields($event->getDocument(), $event->getRecord());
foreach ($mappedFields as $fieldName => $fieldValue) {
if (isset($substitutePageDocument->{$fieldName})) {
// reset = overwrite, especially important to not make fields
Expand All @@ -77,19 +63,18 @@ public function getPageDocument(Document $originalPageDocument): Document
}
}

return $substitutePageDocument;
$event->overrideDocument($substitutePageDocument);
}

/**
* Gets the mapped fields as an array mapping field names to values.
*
* @param Document $pageDocument The original page document.
*
* @return array An array mapping field names to their values.
*
* @throws InvalidFieldNameException
*/
protected function getMappedFields(Document $pageDocument): array
protected function getMappedFields(Document $pageDocument, array $pageRecord): array
{
$fields = [];

Expand All @@ -102,7 +87,7 @@ protected function getMappedFields(Document $pageDocument): array
1435441863
);
}
$fields[$mappedFieldName] = $this->resolveFieldValue($mappedFieldName, $pageDocument);
$fields[$mappedFieldName] = $this->resolveFieldValue($mappedFieldName, $pageDocument, $pageRecord);
}

return $fields;
Expand All @@ -115,13 +100,10 @@ protected function getMappedFields(Document $pageDocument): array
* Otherwise, the plain page record field value is used.
*
* @param string $solrFieldName The Solr field name to resolve the value from the item's record
*
* @return string|array The resolved value to be indexed
*/
protected function resolveFieldValue(string $solrFieldName, Document $pageDocument): array|string
protected function resolveFieldValue(string $solrFieldName, Document $pageDocument, array $pageRecord): array|string
{
$pageRecord = $GLOBALS['TSFE']->page;

$pageIndexingConfiguration = $this->configuration->getIndexQueueFieldsConfigurationByConfigurationName($this->pageIndexingConfigurationName);

if (isset($pageIndexingConfiguration[$solrFieldName . '.'])) {
Expand Down
Loading