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

!!![TASK] Use preAddModifyDocuments hook for pages #3076

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
4 changes: 2 additions & 2 deletions Classes/IndexQueue/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ protected function indexItem(Item $item, int $language = 0): bool
$documents[] = $itemDocument;
$documents = array_merge($documents, $this->getAdditionalDocuments($item, $language, $itemDocument));
$documents = $this->processDocuments($item, $documents);
$documents = $this->preAddModifyDocuments($item, $language, $documents);
$documents = self::preAddModifyDocuments($item, $language, $documents);

try {
$response = $this->solr->getWriteService()->addDocuments($documents);
Expand Down Expand Up @@ -531,7 +531,7 @@ protected function getAdditionalDocuments(Item $item, int $language, Document $i
* @param array $documents An array of documents to be indexed
* @return array An array of modified documents
*/
protected function preAddModifyDocuments(Item $item, int $language, array $documents): array
public static function preAddModifyDocuments(Item $item, int $language, array $documents): array
{
if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments'] ?? null)) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments'] as $classReference) {
Expand Down
2 changes: 2 additions & 0 deletions Classes/PageDocumentPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* have been put together, but not yet submitted to Solr.
*
* @author Steffen Ritter <steffen.ritter@typo3.org>
*
* @deprecated in favor of PageIndexerDocumentsModifier
*/
interface PageDocumentPostProcessor
{
Expand Down
13 changes: 13 additions & 0 deletions Classes/Typo3PageIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ApacheSolrForTypo3\Solr\Domain\Search\ApacheSolrDocument\Builder;
use ApacheSolrForTypo3\Solr\FieldProcessor\Service;
use ApacheSolrForTypo3\Solr\IndexQueue\FrontendHelper\PageFieldMappingIndexer;
use ApacheSolrForTypo3\Solr\IndexQueue\Indexer;
use ApacheSolrForTypo3\Solr\IndexQueue\Item;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use ApacheSolrForTypo3\Solr\System\Logging\SolrLogManager;
Expand Down Expand Up @@ -258,6 +259,11 @@ public function indexPage(): bool
$documents[] = $pageDocument;
$documents = $this->getAdditionalDocuments($pageDocument, $documents);
$this->processDocuments($documents);
$documents = Indexer::preAddModifyDocuments(
$this->indexQueueItem,
$this->page->getLanguage()->getLanguageId(),
$documents
);

$pageIndexed = $this->addDocumentsToSolrIndex($documents);
$this->documentsSentToSolr = $documents;
Expand All @@ -268,6 +274,8 @@ public function indexPage(): bool
/**
* Applies the configured post processors (indexPagePostProcessPageDocument)
*
* @deprecated
*
* @param Document $pageDocument
*/
protected function applyIndexPagePostProcessors(Document $pageDocument)
Expand All @@ -276,6 +284,11 @@ protected function applyIndexPagePostProcessors(Document $pageDocument)
return;
}

trigger_error(
"The hook indexPagePostProcessPageDocument has been superseded by \$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments']",
E_USER_DEPRECATED
);

foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['Indexer']['indexPagePostProcessPageDocument'] as $classReference) {
$postProcessor = GeneralUtility::makeInstance($classReference);
if (!$postProcessor instanceof PageDocumentPostProcessor) {
Expand Down
11 changes: 11 additions & 0 deletions Documentation/Development/Indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,23 @@ Required Interface: SubstitutePageIndexer
indexPagePostProcessPageDocument
--------------------------------

This is deprecated in favor of preAddModifyDocuments.

Registered classes can be used to post process a Solr document of a page.

Registration with: $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['Indexer']['indexPagePostProcessPageDocument']
Required Interface: PageDocumentPostProcessor


preAddModifyDocuments
---------------------

Registered classes can be used to process Solr documents (pages and records) before they are added to index.

Registration with: $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments']
Required Interface: PageIndexerDocumentsModifier


Independent indexer
===================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use ApacheSolrForTypo3\Solr\FrontendEnvironment\Exception\Exception as SolrFrontendEnvironmentException;
use ApacheSolrForTypo3\Solr\FrontendEnvironment\Tsfe;
use ApacheSolrForTypo3\Solr\IndexQueue\Item;
use ApacheSolrForTypo3\Solr\Tests\Integration\IntegrationTest;
use ApacheSolrForTypo3\Solr\Typo3PageIndexer;
use Doctrine\DBAL\DBALException;
Expand Down Expand Up @@ -75,6 +76,11 @@ protected function indexPages($importPageIds)

/* @var $pageIndexer Typo3PageIndexer */
$pageIndexer = GeneralUtility::makeInstance(Typo3PageIndexer::class, $fakeTSFE);
$indexQueueItemMock = $this->createMock(Item::class);
$indexQueueItemMock->expects(self::any())
->method('getIndexingConfigurationName')
->willReturn('pages');
$pageIndexer->setIndexQueueItem($indexQueueItemMock);
$pageIndexer->indexPage();
}
$this->waitToBeVisibleInSolr();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function canIndexPageIntoSolrWithAdditionalFieldsFromRootLine()
*/
public function canExecutePostProcessor()
{
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['Indexer']['indexPagePostProcessPageDocument']['TestPostProcessor'] = TestPostProcessor::class;
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments']['TestPageIndexerDocumentsModifier'] = TestPageIndexerDocumentsModifier::class;

$this->importDataSetFromFixture('can_index_into_solr.xml');
$this->executePageIndexer();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace ApacheSolrForTypo3\Solr\Tests\Integration\IndexQueue\FrontendHelper;

use ApacheSolrForTypo3\Solr\IndexQueue\Item;
use ApacheSolrForTypo3\Solr\IndexQueue\PageIndexerDocumentsModifier;
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;

class TestPageIndexerDocumentsModifier implements PageIndexerDocumentsModifier
{
/**
* Allows Modification of the Documents before they go into index
*
* @param Item $item
* @param int $language
* @param Document[] $documents
* @return array|void
*/
public function modifyDocuments(Item $item, int $language, array $documents)
{
foreach ($documents as $document) {
$document->addField('postProcessorField_stringS', 'postprocessed');
}

return $documents;
}
}
8 changes: 7 additions & 1 deletion Tests/Integration/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace ApacheSolrForTypo3\Solr\Tests\Integration;

use ApacheSolrForTypo3\Solr\Access\Rootline;
use ApacheSolrForTypo3\Solr\IndexQueue\Item;
use ApacheSolrForTypo3\Solr\Tests\Unit\Helper\FakeObjectManager;
use ApacheSolrForTypo3\Solr\Typo3PageIndexer;
use Doctrine\DBAL\DBALException;
Expand Down Expand Up @@ -340,8 +341,13 @@ protected function indexPageIds(array $importPageIds, array $feUserGroupArray =
foreach ($importPageIds as $importPageId) {
$fakeTSFE = $this->fakeTSFE($importPageId, $feUserGroupArray);

/** @var $pageIndexer Typo3PageIndexer */
/* @var Typo3PageIndexer $pageIndexer */
$pageIndexer = GeneralUtility::makeInstance(Typo3PageIndexer::class, $fakeTSFE);
$indexQueueItemMock = $this->createMock(Item::class);
$indexQueueItemMock->expects(self::any())
->method('getIndexingConfigurationName')
->willReturn('pages');
$pageIndexer->setIndexQueueItem($indexQueueItemMock);
$pageIndexer->setPageAccessRootline(Rootline::getAccessRootlineByPageId($importPageId));
$pageIndexer->indexPage();
}
Expand Down
15 changes: 13 additions & 2 deletions Tests/Integration/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\Slops;
use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\TrigramPhraseFields;
use ApacheSolrForTypo3\Solr\Domain\Search\Query\QueryBuilder;
use ApacheSolrForTypo3\Solr\IndexQueue\Item;
use ApacheSolrForTypo3\Solr\Search;
use ApacheSolrForTypo3\Solr\System\Configuration\ConfigurationManager;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
Expand Down Expand Up @@ -71,8 +72,13 @@ public function canSearchForADocument()
$fakeTSFE = $this->getConfiguredTSFE();
$GLOBALS['TSFE'] = $fakeTSFE;

/** @var $pageIndexer \ApacheSolrForTypo3\Solr\Typo3PageIndexer */
/* @var Typo3PageIndexer $pageIndexer */
$pageIndexer = GeneralUtility::makeInstance(Typo3PageIndexer::class, $fakeTSFE);
$indexQueueItemMock = $this->createMock(Item::class);
$indexQueueItemMock->expects(self::any())
->method('getIndexingConfigurationName')
->willReturn('pages');
$pageIndexer->setIndexQueueItem($indexQueueItemMock);
$pageIndexer->indexPage();

$this->waitToBeVisibleInSolr();
Expand Down Expand Up @@ -435,8 +441,13 @@ protected function fillIndexForPhraseSearchTests(string $fixture = 'phrase_searc
$fakeTSFE = $this->getConfiguredTSFE($i);
$GLOBALS['TSFE'] = $fakeTSFE;

/** @var $pageIndexer \ApacheSolrForTypo3\Solr\Typo3PageIndexer */
/* @var Typo3PageIndexer $pageIndexer */
$pageIndexer = GeneralUtility::makeInstance(Typo3PageIndexer::class, $fakeTSFE);
$indexQueueItemMock = $this->createMock(Item::class);
$indexQueueItemMock->expects(self::any())
->method('getIndexingConfigurationName')
->willReturn('pages');
$pageIndexer->setIndexQueueItem($indexQueueItemMock);
$pageIndexer->indexPage();
}
$this->waitToBeVisibleInSolr();
Expand Down