meilisearch/Classes/IndexQueue/Typo3PageIndexer.php

509 lines
17 KiB
PHP
Raw Permalink Normal View History

2021-04-17 00:26:33 +02:00
<?php
2021-04-29 18:33:05 +02:00
namespace WapplerSystems\Meilisearch\IndexQueue;
2021-04-17 00:26:33 +02:00
/***************************************************************
* Copyright notice
*
* (c) 2009-2015 Ingo Renner <ingo@typo3.org>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use WapplerSystems\Meilisearch\Access\Rootline;
2021-04-29 18:33:05 +02:00
use WapplerSystems\Meilisearch\AdditionalPageIndexer;
use WapplerSystems\Meilisearch\ConnectionManager;
2021-04-24 04:44:44 +02:00
use WapplerSystems\Meilisearch\Domain\Search\MeilisearchDocument\Builder;
2021-04-17 00:26:33 +02:00
use WapplerSystems\Meilisearch\FieldProcessor\Service;
use WapplerSystems\Meilisearch\IndexQueue\FrontendHelper\PageFieldMappingIndexer;
use WapplerSystems\Meilisearch\IndexQueue\Item;
2021-04-29 18:33:05 +02:00
use WapplerSystems\Meilisearch\SubstitutePageIndexer;
2021-04-17 00:26:33 +02:00
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
use WapplerSystems\Meilisearch\System\Meilisearch\Document\Document;
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchConnection;
2021-04-17 00:26:33 +02:00
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
2021-04-29 18:33:05 +02:00
use WapplerSystems\Meilisearch\Typo3PageContentExtractor;
use WapplerSystems\Meilisearch\Util;
2021-04-17 00:26:33 +02:00
/**
* Page Indexer to index TYPO3 pages used by the Index Queue.
*
* @author Ingo Renner <ingo@typo3.org>
* @author Daniel Poetzinger <poetzinger@aoemedia.de>
* @author Timo Schmidt <schmidt@aoemedia.de>
*/
class Typo3PageIndexer
{
/**
2021-04-17 21:20:54 +02:00
* ID of the current page's Meilisearch document.
2021-04-17 00:26:33 +02:00
*
* @var string
*/
2021-04-17 21:20:54 +02:00
protected static $pageMeilisearchDocumentId = '';
2021-04-29 18:33:05 +02:00
2021-04-17 00:26:33 +02:00
/**
2021-04-29 18:33:05 +02:00
* @var array
2021-04-17 00:26:33 +02:00
*/
2021-04-29 18:33:05 +02:00
private static $pageMeilisearchDocument;
2021-04-17 00:26:33 +02:00
/**
* The mount point parameter used in the Frontend controller.
*
* @var string
*/
protected $mountPointParameter;
/**
2021-04-17 21:20:54 +02:00
* Meilisearch server connection.
2021-04-17 00:26:33 +02:00
*
2021-04-17 21:20:54 +02:00
* @var MeilisearchConnection
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
protected $meilisearchConnection = null;
2021-04-17 00:26:33 +02:00
/**
* Frontend page object (TSFE).
*
* @var TypoScriptFrontendController
*/
protected $page = null;
/**
* Content extractor to extract content from TYPO3 pages
*
* @var Typo3PageContentExtractor
*/
protected $contentExtractor = null;
/**
* URL to be indexed as the page's URL
*
* @var string
*/
protected $pageUrl = '';
/**
* The page's access rootline
*
* @var Rootline
*/
protected $pageAccessRootline = null;
/**
2021-04-17 21:20:54 +02:00
* Documents that have been sent to Meilisearch
2021-04-17 00:26:33 +02:00
*
* @var array
*/
2021-04-17 21:20:54 +02:00
protected $documentsSentToMeilisearch = [];
2021-04-17 00:26:33 +02:00
/**
* @var TypoScriptConfiguration
*/
protected $configuration;
/**
* @var Item
*/
protected $indexQueueItem;
/**
2021-04-17 21:20:54 +02:00
* @var \WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager
2021-04-17 00:26:33 +02:00
*/
protected $logger = null;
/**
* Constructor
*
* @param TypoScriptFrontendController $page The page to index
*/
public function __construct(TypoScriptFrontendController $page)
{
2021-04-17 21:20:54 +02:00
$this->logger = GeneralUtility::makeInstance(MeilisearchLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
2021-04-17 00:26:33 +02:00
$this->page = $page;
$this->pageUrl = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
2021-04-17 21:20:54 +02:00
$this->configuration = Util::getMeilisearchConfiguration();
2021-04-17 00:26:33 +02:00
try {
2021-04-17 21:20:54 +02:00
$this->initializeMeilisearchConnection();
2021-04-17 00:26:33 +02:00
} catch (\Exception $e) {
$this->logger->log(
2021-04-17 21:20:54 +02:00
MeilisearchLogManager::ERROR,
2021-04-17 00:26:33 +02:00
$e->getMessage() . ' Error code: ' . $e->getCode()
);
// TODO extract to a class "ExceptionLogger"
if ($this->configuration->getLoggingExceptions()) {
$this->logger->log(
2021-04-17 21:20:54 +02:00
MeilisearchLogManager::ERROR,
2021-04-17 00:26:33 +02:00
'Exception while trying to index a page',
[
$e->__toString()
]
);
}
}
$this->pageAccessRootline = GeneralUtility::makeInstance(Rootline::class, /** @scrutinizer ignore-type */ '');
}
/**
* @param Item $indexQueueItem
*/
public function setIndexQueueItem($indexQueueItem)
{
$this->indexQueueItem = $indexQueueItem;
}
/**
2021-04-17 21:20:54 +02:00
* Initializes the Meilisearch server connection.
2021-04-17 00:26:33 +02:00
*
2021-04-17 21:20:54 +02:00
* @throws \Exception when no Meilisearch connection can be established.
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
protected function initializeMeilisearchConnection()
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
$meilisearch = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($this->page->id, Util::getLanguageUid());
2021-04-17 00:26:33 +02:00
// do not continue if no server is available
2021-04-17 21:20:54 +02:00
if (!$meilisearch->getWriteService()->ping()) {
2021-04-17 00:26:33 +02:00
throw new \Exception(
2021-04-17 21:20:54 +02:00
'No Meilisearch instance available while trying to index a page.',
2021-04-17 00:26:33 +02:00
1234790825
);
}
2021-04-17 21:20:54 +02:00
$this->meilisearchConnection = $meilisearch;
2021-04-17 00:26:33 +02:00
}
/**
2021-04-17 21:20:54 +02:00
* Gets the current page's Meilisearch document ID.
2021-04-17 00:26:33 +02:00
*
2021-04-17 21:20:54 +02:00
* @return string|NULL The page's Meilisearch document ID or NULL in case no document was generated yet.
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
public static function getPageMeilisearchDocumentId()
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
return self::$pageMeilisearchDocumentId;
2021-04-17 00:26:33 +02:00
}
/**
2021-04-17 21:20:54 +02:00
* Gets the Meilisearch document generated for the current page.
2021-04-17 00:26:33 +02:00
*
2021-04-29 18:33:05 +02:00
* @return array|NULL The page's Meilisearch document or NULL if it has not been generated yet.
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
public static function getPageMeilisearchDocument()
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
return self::$pageMeilisearchDocument;
2021-04-17 00:26:33 +02:00
}
/**
2021-04-17 21:20:54 +02:00
* Allows to provide a Meilisearch server connection other than the one
2021-04-17 00:26:33 +02:00
* initialized by the constructor.
*
2021-04-17 21:20:54 +02:00
* @param MeilisearchConnection $meilisearchConnection Meilisearch connection
* @throws \Exception if the Meilisearch server cannot be reached
2021-04-17 00:26:33 +02:00
*/
2021-04-17 21:20:54 +02:00
public function setMeilisearchConnection(MeilisearchConnection $meilisearchConnection)
2021-04-17 00:26:33 +02:00
{
2021-04-29 18:33:05 +02:00
if (!$meilisearchConnection->getService()->ping()) {
2021-04-17 00:26:33 +02:00
throw new \Exception(
2021-04-17 21:20:54 +02:00
'Could not connect to Meilisearch server.',
2021-04-17 00:26:33 +02:00
1323946472
);
}
2021-04-17 21:20:54 +02:00
$this->meilisearchConnection = $meilisearchConnection;
2021-04-17 00:26:33 +02:00
}
/**
* Indexes a page.
*
* @return bool TRUE after successfully indexing the page, FALSE on error
* @throws \UnexpectedValueException if a page document post processor fails to implement interface WapplerSystems\Meilisearch\PageDocumentPostProcessor
*/
public function indexPage()
{
$pageIndexed = false;
$documents = []; // this will become useful as soon as when starting to index individual records instead of whole pages
2021-04-17 21:20:54 +02:00
if (is_null($this->meilisearchConnection)) {
2021-04-17 00:26:33 +02:00
// intended early return as it doesn't make sense to continue
2021-04-17 21:20:54 +02:00
// and waste processing time if the meilisearch server isn't available
2021-04-17 00:26:33 +02:00
// anyways
// FIXME use an exception
return $pageIndexed;
}
$pageDocument = $this->getPageDocument();
$pageDocument = $this->substitutePageDocument($pageDocument);
$this->applyIndexPagePostProcessors($pageDocument);
2021-04-17 21:20:54 +02:00
self::$pageMeilisearchDocument = $pageDocument;
2021-04-17 00:26:33 +02:00
$documents[] = $pageDocument;
$documents = $this->getAdditionalDocuments($pageDocument, $documents);
$this->processDocuments($documents);
2021-04-17 21:20:54 +02:00
$pageIndexed = $this->addDocumentsToMeilisearchIndex($documents);
$this->documentsSentToMeilisearch = $documents;
2021-04-17 00:26:33 +02:00
return $pageIndexed;
}
/**
* Applies the configured post processors (indexPagePostProcessPageDocument)
*
2021-04-29 18:33:05 +02:00
* @param array $pageDocument
2021-04-17 00:26:33 +02:00
*/
protected function applyIndexPagePostProcessors($pageDocument)
{
2021-04-17 21:20:54 +02:00
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['meilisearch']['Indexer']['indexPagePostProcessPageDocument'])) {
2021-04-17 00:26:33 +02:00
return;
}
2021-04-17 21:20:54 +02:00
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['meilisearch']['Indexer']['indexPagePostProcessPageDocument'] as $classReference) {
2021-04-17 00:26:33 +02:00
$postProcessor = GeneralUtility::makeInstance($classReference);
if (!$postProcessor instanceof PageDocumentPostProcessor) {
throw new \UnexpectedValueException(get_class($pageDocument) . ' must implement interface ' . PageDocumentPostProcessor::class, 1397739154);
}
$postProcessor->postProcessPageDocument($pageDocument, $this->page);
}
}
/**
2021-04-17 21:20:54 +02:00
* Builds the Meilisearch document for the current page.
2021-04-17 00:26:33 +02:00
*
2021-04-29 18:33:05 +02:00
* @return array A document representing the page
2021-04-17 00:26:33 +02:00
*/
protected function getPageDocument()
{
2021-04-29 18:33:05 +02:00
/** @var Builder $documentBuilder */
2021-04-17 00:26:33 +02:00
$documentBuilder = GeneralUtility::makeInstance(Builder::class);
$document = $documentBuilder->fromPage($this->page, $this->pageUrl, $this->pageAccessRootline, (string)$this->mountPointParameter);
2021-04-17 21:20:54 +02:00
self::$pageMeilisearchDocumentId = $document['id'];
2021-04-17 00:26:33 +02:00
return $document;
}
// Logging
// TODO replace by a central logger
/**
* Gets the mount point parameter that is used in the Frontend controller.
*
* @return string
*/
public function getMountPointParameter()
{
return $this->mountPointParameter;
}
// Misc
/**
* Sets the mount point parameter that is used in the Frontend controller.
*
* @param string $mountPointParameter
*/
public function setMountPointParameter($mountPointParameter)
{
$this->mountPointParameter = (string)$mountPointParameter;
}
/**
* Allows third party extensions to replace or modify the page document
* created by this indexer.
*
* @param Document $pageDocument The page document created by this indexer.
2021-04-17 21:20:54 +02:00
* @return Document An Meilisearch document representing the currently indexed page
2021-04-17 00:26:33 +02:00
*/
protected function substitutePageDocument(Document $pageDocument)
{
2021-04-17 21:20:54 +02:00
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['meilisearch']['Indexer']['indexPageSubstitutePageDocument'])) {
2021-04-17 00:26:33 +02:00
return $pageDocument;
}
$indexConfigurationName = $this->getIndexConfigurationNameForCurrentPage();
2021-04-17 21:20:54 +02:00
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['meilisearch']['Indexer']['indexPageSubstitutePageDocument'] as $classReference) {
2021-04-17 00:26:33 +02:00
$substituteIndexer = GeneralUtility::makeInstance($classReference);
if (!$substituteIndexer instanceof SubstitutePageIndexer) {
$message = get_class($substituteIndexer) . ' must implement interface ' . SubstitutePageIndexer::class;
throw new \UnexpectedValueException($message, 1310491001);
}
if ($substituteIndexer instanceof PageFieldMappingIndexer) {
$substituteIndexer->setPageIndexingConfigurationName($indexConfigurationName);
}
$substituteDocument = $substituteIndexer->getPageDocument($pageDocument);
if (!$substituteDocument instanceof Document) {
$message = 'The document returned by ' . get_class($substituteIndexer) . ' is not a valid Document object.';
throw new \UnexpectedValueException($message, 1310490952);
}
$pageDocument = $substituteDocument;
}
return $pageDocument;
}
/**
* Retrieves the indexConfigurationName from the related queueItem, or falls back to pages when no queue item set.
*
* @return string
*/
protected function getIndexConfigurationNameForCurrentPage()
{
return isset($this->indexQueueItem) ? $this->indexQueueItem->getIndexingConfigurationName() : 'pages';
}
/**
* Allows third party extensions to provide additional documents which
* should be indexed for the current page.
*
* @param Document $pageDocument The main document representing this page.
* @param Document[] $existingDocuments An array of documents already created for this page.
* @return array An array of additional Document objects to index
*/
protected function getAdditionalDocuments(Document $pageDocument, array $existingDocuments)
{
$documents = $existingDocuments;
2021-04-17 21:20:54 +02:00
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['meilisearch']['Indexer']['indexPageAddDocuments'])) {
2021-04-17 00:26:33 +02:00
return $documents;
}
2021-04-17 21:20:54 +02:00
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['meilisearch']['Indexer']['indexPageAddDocuments'] as $classReference) {
2021-04-17 00:26:33 +02:00
$additionalIndexer = GeneralUtility::makeInstance($classReference);
if (!$additionalIndexer instanceof AdditionalPageIndexer) {
$message = get_class($additionalIndexer) . ' must implement interface ' . AdditionalPageIndexer::class;
throw new \UnexpectedValueException($message, 1310491024);
}
$additionalDocuments = $additionalIndexer->getAdditionalPageDocuments($pageDocument, $documents);
if (is_array($additionalDocuments)) {
$documents = array_merge($documents, $additionalDocuments);
}
}
return $documents;
}
/**
* Sends the given documents to the field processing service which takes
* care of manipulating fields as defined in the field's configuration.
*
* @param array $documents An array of documents to manipulate
*/
protected function processDocuments(array $documents)
{
$processingInstructions = $this->configuration->getIndexFieldProcessingInstructionsConfiguration();
if (count($processingInstructions) > 0) {
$service = GeneralUtility::makeInstance(Service::class);
$service->processDocuments($documents, $processingInstructions);
}
}
/**
2021-04-17 21:20:54 +02:00
* Adds the collected documents to the Meilisearch index.
2021-04-17 00:26:33 +02:00
*
* @param array $documents An array of Document objects.
* @return bool TRUE if documents were added successfully, FALSE otherwise
*/
2021-04-17 21:20:54 +02:00
protected function addDocumentsToMeilisearchIndex(array $documents)
2021-04-17 00:26:33 +02:00
{
$documentsAdded = false;
if (!count($documents)) {
return $documentsAdded;
}
try {
2021-04-17 21:20:54 +02:00
$this->logger->log(MeilisearchLogManager::INFO, 'Adding ' . count($documents) . ' documents.', $documents);
2021-04-17 00:26:33 +02:00
// chunk adds by 20
$documentChunks = array_chunk($documents, 20);
foreach ($documentChunks as $documentChunk) {
2021-04-29 18:33:05 +02:00
$response = $this->meilisearchConnection->getService()->addDocuments($documentChunk);
2021-04-17 00:26:33 +02:00
if ($response->getHttpStatus() != 200) {
2021-04-17 21:20:54 +02:00
throw new \RuntimeException('Meilisearch Request failed.', 1331834983);
2021-04-17 00:26:33 +02:00
}
}
$documentsAdded = true;
} catch (\Exception $e) {
2021-04-17 21:20:54 +02:00
$this->logger->log(MeilisearchLogManager::ERROR, $e->getMessage() . ' Error code: ' . $e->getCode());
2021-04-17 00:26:33 +02:00
if ($this->configuration->getLoggingExceptions()) {
2021-04-17 21:20:54 +02:00
$this->logger->log(MeilisearchLogManager::ERROR, 'Exception while adding documents', [$e->__toString()]);
2021-04-17 00:26:33 +02:00
}
}
return $documentsAdded;
}
/**
* Gets the current page's URL.
*
* @return string URL of the current page.
*/
public function getPageUrl()
{
return $this->pageUrl;
}
/**
* Sets the URL to use for the page document.
*
* @param string $url The page's URL.
*/
public function setPageUrl($url)
{
$this->pageUrl = $url;
}
/**
* Gets the page's access rootline.
*
* @return Rootline The page's access rootline
*/
public function getPageAccessRootline()
{
return $this->pageAccessRootline;
}
/**
* Sets the page's access rootline.
*
* @param Rootline $accessRootline The page's access rootline
*/
public function setPageAccessRootline(Rootline $accessRootline)
{
$this->pageAccessRootline = $accessRootline;
}
/**
2021-04-17 21:20:54 +02:00
* Gets the documents that have been sent to Meilisearch
2021-04-17 00:26:33 +02:00
*
* @return array An array of Document objects
*/
2021-04-17 21:20:54 +02:00
public function getDocumentsSentToMeilisearch()
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
return $this->documentsSentToMeilisearch;
2021-04-17 00:26:33 +02:00
}
}