first commit
This commit is contained in:
405
Classes/IndexQueue/Initializer/AbstractInitializer.php
Normal file
405
Classes/IndexQueue/Initializer/AbstractInitializer.php
Normal file
@@ -0,0 +1,405 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\IndexQueue\Initializer;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2011-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.
|
||||
* A copy is found in the textfile GPL.txt and important notices to the license
|
||||
* from the author is found in LICENSE.txt distributed with these scripts.
|
||||
*
|
||||
*
|
||||
* 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\Domain\Index\Queue\QueueItemRepository;
|
||||
use WapplerSystems\Meilisearch\Domain\Site\Site;
|
||||
use WapplerSystems\Meilisearch\System\Logging\SolrLogManager;
|
||||
use Doctrine\DBAL\DBALException;
|
||||
use TYPO3\CMS\Backend\Utility\BackendUtility;
|
||||
use TYPO3\CMS\Core\Database\ConnectionPool;
|
||||
use TYPO3\CMS\Core\Messaging\FlashMessageService;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Abstract Index Queue initializer with implementation of methods for common
|
||||
* needs during Index Queue initialization.
|
||||
*
|
||||
* @author Ingo Renner <ingo@typo3.org>
|
||||
*/
|
||||
abstract class AbstractInitializer implements IndexQueueInitializer
|
||||
{
|
||||
|
||||
/**
|
||||
* Site to initialize
|
||||
*
|
||||
* @var Site
|
||||
*/
|
||||
protected $site;
|
||||
|
||||
/**
|
||||
* The type of items this initializer is handling.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* Index Queue configuration.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $indexingConfiguration;
|
||||
|
||||
/**
|
||||
* Indexing configuration name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $indexingConfigurationName;
|
||||
|
||||
/**
|
||||
* Flash message queue
|
||||
*
|
||||
* @var \TYPO3\CMS\Core\Messaging\FlashMessageQueue
|
||||
*/
|
||||
protected $flashMessageQueue;
|
||||
|
||||
/**
|
||||
* @var \WapplerSystems\Meilisearch\System\Logging\SolrLogManager
|
||||
*/
|
||||
protected $logger = null;
|
||||
|
||||
/**
|
||||
* @var QueueItemRepository
|
||||
*/
|
||||
protected $queueItemRepository;
|
||||
|
||||
/**
|
||||
* Constructor, prepares the flash message queue
|
||||
* @param QueueItemRepository|null $queueItemRepository
|
||||
*/
|
||||
public function __construct(QueueItemRepository $queueItemRepository = null)
|
||||
{
|
||||
$this->logger = GeneralUtility::makeInstance(SolrLogManager::class, /** @scrutinizer ignore-type */ __CLASS__);
|
||||
$flashMessageService = GeneralUtility::makeInstance(FlashMessageService::class);
|
||||
$this->flashMessageQueue = $flashMessageService->getMessageQueueByIdentifier('solr.queue.initializer');
|
||||
$this->queueItemRepository = $queueItemRepository ?? GeneralUtility::makeInstance(QueueItemRepository::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the site for the initializer.
|
||||
*
|
||||
* @param Site $site The site to initialize Index Queue items for.
|
||||
*/
|
||||
public function setSite(Site $site)
|
||||
{
|
||||
$this->site = $site;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the type (usually a Db table name) of items to initialize.
|
||||
*
|
||||
* @param string $type Type to initialize.
|
||||
*/
|
||||
public function setType($type)
|
||||
{
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the configuration for how to index a type of items.
|
||||
*
|
||||
* @param array $indexingConfiguration Indexing configuration from TypoScript
|
||||
*/
|
||||
public function setIndexingConfiguration(array $indexingConfiguration)
|
||||
{
|
||||
$this->indexingConfiguration = $indexingConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the name of the indexing configuration to initialize.
|
||||
*
|
||||
* @param string $indexingConfigurationName Indexing configuration name
|
||||
*/
|
||||
public function setIndexingConfigurationName($indexingConfigurationName)
|
||||
{
|
||||
$this->indexingConfigurationName = (string)$indexingConfigurationName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes Index Queue items for a certain site and indexing
|
||||
* configuration.
|
||||
*
|
||||
* @return bool TRUE if initialization was successful, FALSE on error.
|
||||
*/
|
||||
public function initialize()
|
||||
{
|
||||
/** @var ConnectionPool $connectionPool */
|
||||
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
|
||||
|
||||
$fetchItemsQuery = $this->buildSelectStatement() . ', "" as errors '
|
||||
. 'FROM ' . $this->type . ' '
|
||||
. 'WHERE '
|
||||
. $this->buildPagesClause()
|
||||
. $this->buildTcaWhereClause()
|
||||
. $this->buildUserWhereClause();
|
||||
|
||||
try {
|
||||
if ($connectionPool->getConnectionForTable($this->type)->getParams() === $connectionPool->getConnectionForTable('tx_meilisearch_indexqueue_item')->getParams()) {
|
||||
// If both tables are in the same DB, send only one query to copy all datas from one table to the other
|
||||
$initializationQuery = 'INSERT INTO tx_meilisearch_indexqueue_item (root, item_type, item_uid, indexing_configuration, indexing_priority, changed, errors) ' . $fetchItemsQuery;
|
||||
$logData = ['query' => $initializationQuery];
|
||||
$logData['rows'] = $this->queueItemRepository->initializeByNativeSQLStatement($initializationQuery);
|
||||
} else {
|
||||
// If tables are using distinct connections, start by fetching items matching criteria
|
||||
$logData = ['query' => $fetchItemsQuery];
|
||||
$items = $connectionPool->getConnectionForTable($this->type)->fetchAll($fetchItemsQuery);
|
||||
$logData['rows'] = count($items);
|
||||
|
||||
if (count($items)) {
|
||||
// Add items to the queue (if any)
|
||||
$logData['rows'] = $connectionPool
|
||||
->getConnectionForTable('tx_meilisearch_indexqueue_item')
|
||||
->bulkInsert('tx_meilisearch_indexqueue_item', $items, array_keys($items[0]));
|
||||
}
|
||||
}
|
||||
} catch (DBALException $DBALException) {
|
||||
$logData['error'] = $DBALException->getCode() . ': ' . $DBALException->getMessage();
|
||||
}
|
||||
|
||||
$this->logInitialization($logData);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the SELECT part of the Index Queue initialization query.
|
||||
*
|
||||
*/
|
||||
protected function buildSelectStatement()
|
||||
{
|
||||
$changedField = $GLOBALS['TCA'][$this->type]['ctrl']['tstamp'];
|
||||
if (!empty($GLOBALS['TCA'][$this->type]['ctrl']['enablecolumns']['starttime'])) {
|
||||
$changedField = 'GREATEST(' . $GLOBALS['TCA'][$this->type]['ctrl']['enablecolumns']['starttime'] . ',' . $GLOBALS['TCA'][$this->type]['ctrl']['tstamp'] . ')';
|
||||
}
|
||||
$select = 'SELECT '
|
||||
. '\'' . $this->site->getRootPageId() . '\' as root, '
|
||||
. '\'' . $this->type . '\' AS item_type, '
|
||||
. 'uid AS item_uid, '
|
||||
. '\'' . $this->indexingConfigurationName . '\' as indexing_configuration, '
|
||||
. $this->getIndexingPriority() . ' AS indexing_priority, '
|
||||
. $changedField . ' AS changed';
|
||||
|
||||
return $select;
|
||||
}
|
||||
|
||||
// initialization query building
|
||||
|
||||
/**
|
||||
* Reads the indexing priority for an indexing configuration.
|
||||
*
|
||||
* @return int Indexing priority
|
||||
*/
|
||||
protected function getIndexingPriority()
|
||||
{
|
||||
$priority = 0;
|
||||
|
||||
if (!empty($this->indexingConfiguration['indexingPriority'])) {
|
||||
$priority = (int)$this->indexingConfiguration['indexingPriority'];
|
||||
}
|
||||
|
||||
return $priority;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a part of the WHERE clause of the Index Queue initialization
|
||||
* query. This part selects the limits items to be selected from the pages
|
||||
* in a site only, plus additional pages that may have been configured.
|
||||
*
|
||||
*/
|
||||
protected function buildPagesClause()
|
||||
{
|
||||
$pages = $this->getPages();
|
||||
$pageIdField = ($this->type === 'pages') ? 'uid' : 'pid';
|
||||
|
||||
return $pageIdField . ' IN(' . implode(',', $pages) . ')';
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the pages in a site plus additional pages that may have been
|
||||
* configured.
|
||||
*
|
||||
* @return array A (sorted) array of page IDs in a site
|
||||
*/
|
||||
protected function getPages()
|
||||
{
|
||||
$pages = $this->site->getPages();
|
||||
$additionalPageIds = [];
|
||||
if (!empty($this->indexingConfiguration['additionalPageIds'])) {
|
||||
$additionalPageIds = GeneralUtility::intExplode(',', $this->indexingConfiguration['additionalPageIds']);
|
||||
}
|
||||
|
||||
$pages = array_merge($pages, $additionalPageIds);
|
||||
sort($pages, SORT_NUMERIC);
|
||||
|
||||
return $pages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the WHERE clauses of the Index Queue initialization query based
|
||||
* on TCA information for the type to be initialized.
|
||||
*
|
||||
* @return string Conditions to only add indexable items to the Index Queue
|
||||
*/
|
||||
protected function buildTcaWhereClause()
|
||||
{
|
||||
$tcaWhereClause = '';
|
||||
$conditions = [];
|
||||
|
||||
if (isset($GLOBALS['TCA'][$this->type]['ctrl']['delete'])) {
|
||||
$conditions['delete'] = $GLOBALS['TCA'][$this->type]['ctrl']['delete'] . ' = 0';
|
||||
}
|
||||
|
||||
if (isset($GLOBALS['TCA'][$this->type]['ctrl']['enablecolumns']['disabled'])) {
|
||||
$conditions['disabled'] = $GLOBALS['TCA'][$this->type]['ctrl']['enablecolumns']['disabled'] . ' = 0';
|
||||
}
|
||||
|
||||
if (isset($GLOBALS['TCA'][$this->type]['ctrl']['enablecolumns']['endtime'])) {
|
||||
// only include records with a future endtime or default value (0)
|
||||
$endTimeFieldName = $GLOBALS['TCA'][$this->type]['ctrl']['enablecolumns']['endtime'];
|
||||
$conditions['endtime'] = '(' . $endTimeFieldName . ' > ' . time() . ' OR ' . $endTimeFieldName . ' = 0)';
|
||||
}
|
||||
|
||||
if (BackendUtility::isTableLocalizable($this->type)) {
|
||||
$conditions['languageField'] = [
|
||||
$GLOBALS['TCA'][$this->type]['ctrl']['languageField'] . ' = 0',
|
||||
// default language
|
||||
$GLOBALS['TCA'][$this->type]['ctrl']['languageField'] . ' = -1'
|
||||
// all languages
|
||||
];
|
||||
if (isset($GLOBALS['TCA'][$this->type]['ctrl']['transOrigPointerField'])) {
|
||||
$conditions['languageField'][] = $GLOBALS['TCA'][$this->type]['ctrl']['transOrigPointerField'] . ' = 0'; // translations without original language source
|
||||
}
|
||||
$conditions['languageField'] = '(' . implode(' OR ',
|
||||
$conditions['languageField']) . ')';
|
||||
}
|
||||
|
||||
if (!empty($GLOBALS['TCA'][$this->type]['ctrl']['versioningWS'])) {
|
||||
// versioning is enabled for this table: exclude draft workspace records
|
||||
/* @see \TYPO3\CMS\Core\Database\Query\Restriction\WorkspaceRestriction::buildExpression */
|
||||
$conditions['versioningWS'] = 't3ver_wsid = 0';
|
||||
}
|
||||
|
||||
if (count($conditions)) {
|
||||
$tcaWhereClause = ' AND ' . implode(' AND ', $conditions);
|
||||
}
|
||||
|
||||
return $tcaWhereClause;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the WHERE clauses of the Index Queue initialization query based
|
||||
* on TypoScript configuration for the type to be initialized.
|
||||
*
|
||||
* @return string Conditions to add items to the Index Queue based on TypoScript configuration
|
||||
*/
|
||||
protected function buildUserWhereClause()
|
||||
{
|
||||
$condition = '';
|
||||
|
||||
// FIXME replace this with the mechanism described below
|
||||
if (isset($this->indexingConfiguration['additionalWhereClause'])) {
|
||||
$condition = ' AND ' . $this->indexingConfiguration['additionalWhereClause'];
|
||||
}
|
||||
|
||||
return $condition;
|
||||
|
||||
// TODO add a query builder implementation based on TypoScript configuration
|
||||
|
||||
/* example TypoScript
|
||||
|
||||
@see http://docs.jboss.org/drools/release/5.4.0.Final/drools-expert-docs/html_single/index.html
|
||||
@see The Java Rule Engine API (JSR94)
|
||||
|
||||
tt_news {
|
||||
|
||||
// RULES cObject provided by EXT:rules, simply evaluates to boolean TRUE or FALSE
|
||||
conditions = RULES
|
||||
conditions {
|
||||
|
||||
and {
|
||||
|
||||
10 {
|
||||
field = pid
|
||||
value = 2,3,5
|
||||
condition = in / equals / notEquals / greaterThan / lessThan / greaterThanOrEqual / lessThanOrEqual
|
||||
}
|
||||
|
||||
20 {
|
||||
field = ...
|
||||
value = ...
|
||||
condition = ...
|
||||
|
||||
or {
|
||||
10 {
|
||||
field = ...
|
||||
value = ...
|
||||
condition = ...
|
||||
}
|
||||
|
||||
20 {
|
||||
field = ...
|
||||
value = ...
|
||||
condition = ...
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fields {
|
||||
// field mapping
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the passed log data to the log.
|
||||
*
|
||||
* @param array $logData
|
||||
*/
|
||||
protected function logInitialization(array $logData)
|
||||
{
|
||||
if (!$this->site->getSolrConfiguration()->getLoggingIndexingIndexQueueInitialization()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$logSeverity = isset($logData['error']) ? SolrLogManager::ERROR : SolrLogManager::NOTICE;
|
||||
$logData = array_merge($logData, [
|
||||
'site' => $this->site->getLabel(),
|
||||
'indexing configuration name' => $this->indexingConfigurationName,
|
||||
'type' => $this->type,
|
||||
]);
|
||||
|
||||
$message = 'Index Queue initialized for indexing configuration ' . $this->indexingConfigurationName;
|
||||
$this->logger->log($logSeverity, $message, $logData);
|
||||
}
|
||||
}
|
75
Classes/IndexQueue/Initializer/IndexQueueInitializer.php
Normal file
75
Classes/IndexQueue/Initializer/IndexQueueInitializer.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\IndexQueue\Initializer;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2011-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.
|
||||
* A copy is found in the textfile GPL.txt and important notices to the license
|
||||
* from the author is found in LICENSE.txt distributed with these scripts.
|
||||
*
|
||||
*
|
||||
* 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\Domain\Site\Site;
|
||||
|
||||
/**
|
||||
* Interface to initialize items in the Index Queue.
|
||||
*
|
||||
* @author Ingo Renner <ingo@typo3.org>
|
||||
*/
|
||||
interface IndexQueueInitializer
|
||||
{
|
||||
|
||||
/**
|
||||
* Sets the site for the initializer.
|
||||
*
|
||||
* @param Site $site The site to initialize Index Queue items for.
|
||||
*/
|
||||
public function setSite(Site $site);
|
||||
|
||||
/**
|
||||
* Set the type (usually a Db table name) of items to initialize.
|
||||
*
|
||||
* @param string $type Type to initialize.
|
||||
*/
|
||||
public function setType($type);
|
||||
|
||||
/**
|
||||
* Sets the name of the indexing configuration to initialize.
|
||||
*
|
||||
* @param string $indexingConfigurationName Indexing configuration name
|
||||
*/
|
||||
public function setIndexingConfigurationName($indexingConfigurationName);
|
||||
|
||||
/**
|
||||
* Sets the configuration for how to index a type of items.
|
||||
*
|
||||
* @param array $indexingConfiguration Indexing configuration from TypoScript
|
||||
*/
|
||||
public function setIndexingConfiguration(array $indexingConfiguration);
|
||||
|
||||
/**
|
||||
* Initializes Index Queue items for a certain site and indexing
|
||||
* configuration.
|
||||
*
|
||||
* @return bool TRUE if initialization was successful, FALSE on error.
|
||||
*/
|
||||
public function initialize();
|
||||
}
|
342
Classes/IndexQueue/Initializer/Page.php
Normal file
342
Classes/IndexQueue/Initializer/Page.php
Normal file
@@ -0,0 +1,342 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\IndexQueue\Initializer;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2011-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.
|
||||
* A copy is found in the textfile GPL.txt and important notices to the license
|
||||
* from the author is found in LICENSE.txt distributed with these scripts.
|
||||
*
|
||||
*
|
||||
* 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\Domain\Index\Queue\QueueItemRepository;
|
||||
use WapplerSystems\Meilisearch\Domain\Site\SiteRepository;
|
||||
use WapplerSystems\Meilisearch\IndexQueue\Item;
|
||||
use WapplerSystems\Meilisearch\IndexQueue\Queue;
|
||||
use WapplerSystems\Meilisearch\System\Logging\SolrLogManager;
|
||||
use WapplerSystems\Meilisearch\System\Records\Pages\PagesRepository;
|
||||
use Doctrine\DBAL\DBALException;
|
||||
use TYPO3\CMS\Backend\Utility\BackendUtility;
|
||||
use TYPO3\CMS\Core\Database\Connection;
|
||||
use TYPO3\CMS\Core\Database\ConnectionPool;
|
||||
use TYPO3\CMS\Core\Messaging\FlashMessage;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
/**
|
||||
* Index Queue initializer for pages which also covers resolution of mount
|
||||
* pages.
|
||||
*
|
||||
* @author Ingo Renner <ingo@typo3.org>
|
||||
*/
|
||||
class Page extends AbstractInitializer
|
||||
{
|
||||
/**
|
||||
* The type of items this initializer is handling.
|
||||
* @var string
|
||||
*/
|
||||
protected $type = 'pages';
|
||||
|
||||
/**
|
||||
* @var PagesRepository
|
||||
*/
|
||||
protected $pagesRepository;
|
||||
|
||||
/**
|
||||
* Constructor, sets type and indexingConfigurationName to "pages".
|
||||
*
|
||||
* @param QueueItemRepository|null $queueItemRepository
|
||||
* @param PagesRepository|null $pagesRepository
|
||||
*/
|
||||
public function __construct(QueueItemRepository $queueItemRepository = null, PagesRepository $pagesRepository = null)
|
||||
{
|
||||
parent::__construct($queueItemRepository);
|
||||
$this->pagesRepository = $pagesRepository ?? GeneralUtility::makeInstance(PagesRepository::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides the general setType() implementation, forcing type to "pages".
|
||||
*
|
||||
* @param string $type Type to initialize (ignored).
|
||||
*/
|
||||
public function setType($type)
|
||||
{
|
||||
$this->type = 'pages';
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes Index Queue page items for a site. Includes regular pages
|
||||
* and mounted pages - no nested mount page structures though.
|
||||
*
|
||||
* @return bool TRUE if initialization was successful, FALSE on error.
|
||||
*/
|
||||
public function initialize()
|
||||
{
|
||||
$pagesInitialized = parent::initialize();
|
||||
$mountPagesInitialized = $this->initializeMountPages();
|
||||
|
||||
return ($pagesInitialized && $mountPagesInitialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize a single page that is part of a mounted tree.
|
||||
*
|
||||
* @param array $mountProperties Array of mount point properties mountPageSource, mountPageDestination, and mountPageOverlayed
|
||||
* @param int $mountPageId The ID of the mounted page
|
||||
*/
|
||||
public function initializeMountedPage(array $mountProperties, $mountPageId)
|
||||
{
|
||||
$mountedPages = [$mountPageId];
|
||||
|
||||
$this->addMountedPagesToIndexQueue($mountedPages, $mountProperties);
|
||||
$this->addIndexQueueItemIndexingProperties($mountProperties, $mountedPages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes Mount Pages to be indexed through the Index Queue. The Mount
|
||||
* Pages are searched and their mounted virtual sub-trees are then resolved
|
||||
* and added to the Index Queue as if they were actually present below the
|
||||
* Mount Page.
|
||||
*
|
||||
* @return bool TRUE if initialization of the Mount Pages was successful, FALSE otherwise
|
||||
*/
|
||||
protected function initializeMountPages()
|
||||
{
|
||||
$mountPagesInitialized = false;
|
||||
$mountPages = $this->pagesRepository->findAllMountPagesByWhereClause($this->buildPagesClause() . $this->buildTcaWhereClause() . ' AND doktype = 7 AND no_search = 0');
|
||||
|
||||
if (empty($mountPages)) {
|
||||
$mountPagesInitialized = true;
|
||||
return $mountPagesInitialized;
|
||||
}
|
||||
|
||||
$databaseConnection = $this->queueItemRepository->getConnectionForAllInTransactionInvolvedTables(
|
||||
'tx_meilisearch_indexqueue_item',
|
||||
'tx_meilisearch_indexqueue_indexing_property'
|
||||
);
|
||||
|
||||
foreach ($mountPages as $mountPage) {
|
||||
if (!$this->validateMountPage($mountPage)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$mountedPages = $this->resolveMountPageTree($mountPage);
|
||||
|
||||
// handling mount_pid_ol behavior
|
||||
if ($mountPage['mountPageOverlayed']) {
|
||||
// the page shows the mounted page's content
|
||||
$mountedPages[] = $mountPage['mountPageSource'];
|
||||
} else {
|
||||
// Add page like a regular page, as only the sub tree is
|
||||
// mounted. The page itself has its own content.
|
||||
$indexQueue = GeneralUtility::makeInstance(Queue::class);
|
||||
$indexQueue->updateItem($this->type, $mountPage['uid']);
|
||||
}
|
||||
|
||||
// This can happen when the mount point does not show the content of the
|
||||
// mounted page and the mounted page does not have any subpages.
|
||||
if (empty($mountedPages)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$databaseConnection->beginTransaction();
|
||||
try {
|
||||
$this->addMountedPagesToIndexQueue($mountedPages, $mountPage);
|
||||
$this->addIndexQueueItemIndexingProperties($mountPage, $mountedPages);
|
||||
|
||||
$databaseConnection->commit();
|
||||
$mountPagesInitialized = true;
|
||||
} catch (\Exception $e) {
|
||||
$databaseConnection->rollBack();
|
||||
|
||||
$this->logger->log(
|
||||
SolrLogManager::ERROR,
|
||||
'Index Queue initialization failed for mount pages',
|
||||
[
|
||||
$e->__toString()
|
||||
]
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $mountPagesInitialized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a Mount Page is properly configured.
|
||||
*
|
||||
* @param array $mountPage A mount page
|
||||
* @return bool TRUE if the Mount Page is OK, FALSE otherwise
|
||||
*/
|
||||
protected function validateMountPage(array $mountPage)
|
||||
{
|
||||
$isValidMountPage = true;
|
||||
|
||||
if (empty($mountPage['mountPageSource'])) {
|
||||
$isValidMountPage = false;
|
||||
|
||||
$flashMessage = GeneralUtility::makeInstance(
|
||||
FlashMessage::class,
|
||||
'Property "Mounted page" must not be empty. Invalid Mount Page configuration for page ID ' . $mountPage['uid'] . '.',
|
||||
'Failed to initialize Mount Page tree. ',
|
||||
FlashMessage::ERROR
|
||||
);
|
||||
// @extensionScannerIgnoreLine
|
||||
$this->flashMessageQueue->addMessage($flashMessage);
|
||||
}
|
||||
|
||||
if (!$this->mountedPageExists($mountPage['mountPageSource'])) {
|
||||
$isValidMountPage = false;
|
||||
|
||||
$flashMessage = GeneralUtility::makeInstance(
|
||||
FlashMessage::class,
|
||||
'The mounted page must be accessible in the frontend. '
|
||||
. 'Invalid Mount Page configuration for page ID '
|
||||
. $mountPage['uid'] . ', the mounted page with ID '
|
||||
. $mountPage['mountPageSource']
|
||||
. ' is not accessible in the frontend.',
|
||||
'Failed to initialize Mount Page tree. ',
|
||||
FlashMessage::ERROR
|
||||
);
|
||||
// @extensionScannerIgnoreLine
|
||||
$this->flashMessageQueue->addMessage($flashMessage);
|
||||
}
|
||||
|
||||
return $isValidMountPage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the mounted page (mount page source) exists. That is,
|
||||
* whether it accessible in the frontend. So the record must exist
|
||||
* (deleted = 0) and must not be hidden (hidden = 0).
|
||||
*
|
||||
* @param int $mountedPageId Mounted page ID
|
||||
* @return bool TRUE if the page is accessible in the frontend, FALSE otherwise.
|
||||
*/
|
||||
protected function mountedPageExists($mountedPageId)
|
||||
{
|
||||
$mountedPageExists = false;
|
||||
|
||||
$mountedPage = BackendUtility::getRecord('pages', $mountedPageId, 'uid', ' AND hidden = 0');
|
||||
if (!empty($mountedPage)) {
|
||||
$mountedPageExists = true;
|
||||
}
|
||||
|
||||
return $mountedPageExists;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the virtual / mounted pages to the Index Queue as if they would
|
||||
* belong to the same site where they are mounted.
|
||||
*
|
||||
* @param array $mountedPages An array of mounted page IDs
|
||||
* @param array $mountProperties Array with mount point properties (mountPageSource, mountPageDestination, mountPageOverlayed)
|
||||
*/
|
||||
protected function addMountedPagesToIndexQueue(array $mountedPages, array $mountProperties)
|
||||
{
|
||||
$mountPointIdentifier = $this->getMountPointIdentifier($mountProperties);
|
||||
$mountPointPageIsWithExistingQueueEntry = $this->queueItemRepository->findPageIdsOfExistingMountPagesByMountIdentifier($mountPointIdentifier);
|
||||
$mountedPagesThatNeedToBeAdded = array_diff($mountedPages, $mountPointPageIsWithExistingQueueEntry);
|
||||
|
||||
if (count($mountedPagesThatNeedToBeAdded) === 0) {
|
||||
//nothing to do
|
||||
return;
|
||||
}
|
||||
|
||||
/* @var Connection $connection */
|
||||
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('tx_meilisearch_indexqueue_item');
|
||||
|
||||
$mountIdentifier = $this->getMountPointIdentifier($mountProperties);
|
||||
$initializationQuery = 'INSERT INTO tx_meilisearch_indexqueue_item (root, item_type, item_uid, indexing_configuration, indexing_priority, changed, has_indexing_properties, pages_mountidentifier, errors) '
|
||||
. $this->buildSelectStatement() . ', 1, ' . $connection->quote($mountIdentifier, \PDO::PARAM_STR) . ',""'
|
||||
. 'FROM pages '
|
||||
. 'WHERE '
|
||||
. 'uid IN(' . implode(',', $mountedPagesThatNeedToBeAdded) . ') '
|
||||
. $this->buildTcaWhereClause()
|
||||
. $this->buildUserWhereClause();
|
||||
$logData = ['query' => $initializationQuery];
|
||||
|
||||
try {
|
||||
$logData['rows'] = $this->queueItemRepository->initializeByNativeSQLStatement($initializationQuery);
|
||||
} catch (DBALException $DBALException) {
|
||||
$logData['error'] = $DBALException->getCode() . ': ' . $DBALException->getMessage();
|
||||
}
|
||||
|
||||
$this->logInitialization($logData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds Index Queue item indexing properties for mounted pages. The page
|
||||
* indexer later needs to know that he's dealing with a mounted page, the
|
||||
* indexing properties will let make it possible for the indexer to
|
||||
* distinguish the mounted pages.
|
||||
*
|
||||
* @param array $mountPage An array with information about the root/destination Mount Page
|
||||
* @param array $mountedPages An array of mounted page IDs
|
||||
*/
|
||||
protected function addIndexQueueItemIndexingProperties(array $mountPage, array $mountedPages)
|
||||
{
|
||||
$mountIdentifier = $this->getMountPointIdentifier($mountPage);
|
||||
$mountPageItems = $this->queueItemRepository->findAllIndexQueueItemsByRootPidAndMountIdentifierAndMountedPids($this->site->getRootPageId(), $mountIdentifier, $mountedPages);
|
||||
|
||||
foreach ($mountPageItems as $mountPageItemRecord) {
|
||||
/* @var Item $mountPageItem */
|
||||
$mountPageItem = GeneralUtility::makeInstance(Item::class, /** @scrutinizer ignore-type */ $mountPageItemRecord);
|
||||
$mountPageItem->setIndexingProperty('mountPageSource', $mountPage['mountPageSource']);
|
||||
$mountPageItem->setIndexingProperty('mountPageDestination', $mountPage['mountPageDestination']);
|
||||
$mountPageItem->setIndexingProperty('isMountedPage', '1');
|
||||
|
||||
$mountPageItem->storeIndexingProperties();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds an identifier of the given mount point properties.
|
||||
*
|
||||
* @param array $mountProperties Array with mount point properties (mountPageSource, mountPageDestination, mountPageOverlayed)
|
||||
* @return string String consisting of mountPageSource-mountPageDestination-mountPageOverlayed
|
||||
*/
|
||||
protected function getMountPointIdentifier(array $mountProperties)
|
||||
{
|
||||
return $mountProperties['mountPageSource']
|
||||
. '-' . $mountProperties['mountPageDestination']
|
||||
. '-' . $mountProperties['mountPageOverlayed'];
|
||||
}
|
||||
|
||||
// Mount Page resolution
|
||||
/**
|
||||
* Gets all the pages from a mounted page tree.
|
||||
*
|
||||
* @param array $mountPage
|
||||
* @return array An array of page IDs in the mounted page tree
|
||||
*/
|
||||
protected function resolveMountPageTree($mountPage)
|
||||
{
|
||||
$mountPageSourceId = $mountPage['mountPageSource'];
|
||||
$mountPageIdentifier = $this->getMountPointIdentifier($mountPage);
|
||||
|
||||
$siteRepository = GeneralUtility::makeInstance(SiteRepository::class);
|
||||
/* @var $siteRepository SiteRepository */
|
||||
$mountedSite = $siteRepository->getSiteByPageId($mountPageSourceId, $mountPageIdentifier);
|
||||
|
||||
return $mountedSite ? $mountedSite->getPages($mountPageSourceId) : [];
|
||||
}
|
||||
}
|
40
Classes/IndexQueue/Initializer/Record.php
Normal file
40
Classes/IndexQueue/Initializer/Record.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\IndexQueue\Initializer;
|
||||
|
||||
/***************************************************************
|
||||
* Copyright notice
|
||||
*
|
||||
* (c) 2011-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.
|
||||
* A copy is found in the textfile GPL.txt and important notices to the license
|
||||
* from the author is found in LICENSE.txt distributed with these scripts.
|
||||
*
|
||||
*
|
||||
* 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!
|
||||
***************************************************************/
|
||||
|
||||
/**
|
||||
* Simple Index Queue initializer for records as found in tables configured
|
||||
* through TCA.
|
||||
*
|
||||
* @author Ingo Renner <ingo@typo3.org>
|
||||
*/
|
||||
class Record extends AbstractInitializer
|
||||
{
|
||||
|
||||
// just the default behavior as in the abstract class
|
||||
}
|
Reference in New Issue
Block a user