Skip to content

Commit

Permalink
!!![TASK] Pass pages to preAddModifyDocuments hook
Browse files Browse the repository at this point in the history
Now the preAddModifyDocuments hook is called for pages as well.
Also the indexPagePostProcessPageDocument hook is deprecated
and superseeded by:

```
 $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']
   ['preAddModifyDocuments']
```

Fixes: #2285
Ports: #3076
  • Loading branch information
Christoph Lehmann authored and dkd-kaehm committed Oct 8, 2022
1 parent 09741ef commit 32a8589
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 6 deletions.
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

0 comments on commit 32a8589

Please sign in to comment.