meilisearch/Classes/Domain/Index/Queue/GarbageRemover/AbstractStrategy.php

176 lines
6.3 KiB
PHP
Raw Normal View History

2021-04-17 00:26:33 +02:00
<?php
namespace WapplerSystems\Meilisearch\Domain\Index\Queue\GarbageRemover;
/***************************************************************
* Copyright notice
*
* (c) 2018 - Timo Hund <timo.hund@dkd.de>
* 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\ConnectionManager;
use WapplerSystems\Meilisearch\GarbageCollectorPostProcessor;
use WapplerSystems\Meilisearch\IndexQueue\Queue;
2021-04-17 21:20:54 +02:00
use WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchConnection;
2021-04-17 00:26:33 +02:00
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* An implementation ob a garbage remover strategy is responsible to remove all garbage from the index queue and
2021-04-17 21:20:54 +02:00
* the meilisearch server for a certain table and uid combination.
2021-04-17 00:26:33 +02:00
*/
abstract class AbstractStrategy
{
/**
* @var Queue
*/
protected $queue;
/**
* @var ConnectionManager
*/
protected $connectionManager;
/**
* AbstractStrategy constructor.
* @param Queue|null $queue
* @param ConnectionManager|null $connectionManager
*/
public function __construct(Queue $queue = null, ConnectionManager $connectionManager = null)
{
$this->queue = $queue ?? GeneralUtility::makeInstance(Queue::class);
$this->connectionManager = $connectionManager ?? GeneralUtility::makeInstance(ConnectionManager::class);
}
/**
* Call's the removal of the strategy and afterwards the garbagecollector post processing hook.
*
* @param string $table
* @param int $uid
* @return mixed
*/
public function removeGarbageOf($table, $uid)
{
$this->removeGarbageOfByStrategy($table, $uid);
$this->callPostProcessGarbageCollectorHook($table, $uid);
}
/**
* A implementation of the GarbageCollection strategy is responsible to remove the garbage from
2021-04-17 21:20:54 +02:00
* the indexqueue and from the meilisearch server.
2021-04-17 00:26:33 +02:00
*
* @param string $table
* @param int $uid
* @return mixed
*/
abstract protected function removeGarbageOfByStrategy($table, $uid);
/**
2021-04-17 21:20:54 +02:00
* Deletes a document from meilisearch and from the index queue.
2021-04-17 00:26:33 +02:00
*
* @param string $table
* @param integer $uid
*/
2021-04-17 21:20:54 +02:00
protected function deleteInMeilisearchAndRemoveFromIndexQueue($table, $uid)
2021-04-17 00:26:33 +02:00
{
$this->deleteIndexDocuments($table, $uid);
$this->queue->deleteItem($table, $uid);
}
/**
2021-04-17 21:20:54 +02:00
* Deletes a document from meilisearch and updates the item in the index queue (e.g. on page content updates).
2021-04-17 00:26:33 +02:00
*
* @param string $table
* @param integer $uid
*/
2021-04-17 21:20:54 +02:00
protected function deleteInMeilisearchAndUpdateIndexQueue($table, $uid)
2021-04-17 00:26:33 +02:00
{
$this->deleteIndexDocuments($table, $uid);
$this->queue->updateItem($table, $uid);
}
/**
* Deletes index documents for a given record identification.
*
* @param string $table The record's table name.
* @param int $uid The record's uid.
*/
protected function deleteIndexDocuments($table, $uid, $language = 0)
{
// record can be indexed for multiple sites
$indexQueueItems = $this->queue->getItems($table, $uid);
foreach ($indexQueueItems as $indexQueueItem) {
$site = $indexQueueItem->getSite();
2021-04-17 21:20:54 +02:00
$enableCommitsSetting = $site->getMeilisearchConfiguration()->getEnableCommits();
2021-04-17 00:26:33 +02:00
$siteHash = $site->getSiteHash();
// a site can have multiple connections (cores / languages)
2021-04-24 04:44:44 +02:00
$meilisearchConnections = $this->connectionManager->getConnectionBySite($site);
2021-04-17 00:26:33 +02:00
if ($language > 0) {
2021-04-17 21:20:54 +02:00
$meilisearchConnections = [$language => $meilisearchConnections[$language]];
2021-04-17 00:26:33 +02:00
}
2021-04-17 21:20:54 +02:00
$this->deleteRecordInAllMeilisearchConnections($table, $uid, $meilisearchConnections, $siteHash, $enableCommitsSetting);
2021-04-17 00:26:33 +02:00
}
}
/**
2021-04-17 21:20:54 +02:00
* Deletes the record in all meilisearch connections from that site.
2021-04-17 00:26:33 +02:00
*
* @param string $table
* @param int $uid
2021-04-17 21:20:54 +02:00
* @param MeilisearchConnection[] $meilisearchConnections
2021-04-17 00:26:33 +02:00
* @param string $siteHash
* @param boolean $enableCommitsSetting
*/
2021-04-17 21:20:54 +02:00
protected function deleteRecordInAllMeilisearchConnections($table, $uid, $meilisearchConnections, $siteHash, $enableCommitsSetting)
2021-04-17 00:26:33 +02:00
{
2021-04-17 21:20:54 +02:00
foreach ($meilisearchConnections as $meilisearch) {
$meilisearch->getWriteService()->deleteByQuery('type:' . $table . ' AND uid:' . (int)$uid . ' AND siteHash:' . $siteHash);
2021-04-17 00:26:33 +02:00
if ($enableCommitsSetting) {
2021-04-17 21:20:54 +02:00
$meilisearch->getWriteService()->commit(false, false);
2021-04-17 00:26:33 +02:00
}
}
}
/**
* Calls the registered post processing hooks after the garbageCollection.
*
* @param string $table
* @param int $uid
*/
protected function callPostProcessGarbageCollectorHook($table, $uid)
{
2021-04-17 21:20:54 +02:00
if (!is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['meilisearch']['postProcessGarbageCollector'])) {
2021-04-17 00:26:33 +02:00
return;
}
2021-04-17 21:20:54 +02:00
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['meilisearch']['postProcessGarbageCollector'] as $classReference) {
2021-04-17 00:26:33 +02:00
$garbageCollectorPostProcessor = GeneralUtility::makeInstance($classReference);
if ($garbageCollectorPostProcessor instanceof GarbageCollectorPostProcessor) {
$garbageCollectorPostProcessor->postProcessGarbageCollector($table, $uid);
} else {
$message = get_class($garbageCollectorPostProcessor) . ' must implement interface ' .
GarbageCollectorPostProcessor::class;
throw new \UnexpectedValueException($message, 1345807460);
}
}
}
}