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: TYPO3-Solr#2285
Ports: TYPO3-Solr#3076
  • Loading branch information
Christoph Lehmann authored and dkd-kaehm committed Oct 7, 2022
1 parent 09741ef commit 55bab1e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 4 deletions.
6 changes: 3 additions & 3 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,9 +531,9 @@ 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)) {
if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments'])) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['solr']['IndexQueueIndexer']['preAddModifyDocuments'] as $classReference) {
$documentsModifier = GeneralUtility::makeInstance($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 @@ -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;
}
}

0 comments on commit 55bab1e

Please sign in to comment.