meilisearch/Classes/Controller/Backend/Search/IndexQueueModuleController.php

288 lines
9.9 KiB
PHP
Raw Permalink Normal View History

2021-04-17 00:26:33 +02:00
<?php
namespace WapplerSystems\Meilisearch\Controller\Backend\Search;
/***************************************************************
* Copyright notice
*
* (c) 2013-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\Backend\IndexingConfigurationSelectorField;
use WapplerSystems\Meilisearch\Domain\Index\IndexService;
use WapplerSystems\Meilisearch\IndexQueue\Queue;
use TYPO3\CMS\Backend\View\BackendTemplateView;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
/**
* Index Queue Module
*
* @author Ingo Renner <ingo@typo3.org>
* @property BackendTemplateView $view
*/
class IndexQueueModuleController extends AbstractModuleController
{
/**
* Module name, used to identify a module f.e. in URL parameters.
*
* @var string
*/
protected $moduleName = 'IndexQueue';
/**
* Module title, shows up in the module menu.
*
* @var string
*/
protected $moduleTitle = 'Index Queue';
/**
* @var Queue
*/
protected $indexQueue;
/**
* Initializes the controller before invoking an action method.
*/
protected function initializeAction()
{
parent::initializeAction();
$this->indexQueue = GeneralUtility::makeInstance(Queue::class);
}
/**
* @param Queue $indexQueue
*/
public function setIndexQueue(Queue $indexQueue)
{
$this->indexQueue = $indexQueue;
}
/**
* Set up the doc header properly here
*
* @param ViewInterface $view
* @return void
*/
protected function initializeView(ViewInterface $view)
{
parent::initializeView($view);
}
/**
* Lists the available indexing configurations
*
* @return void
*/
public function indexAction()
{
if (!$this->canQueueSelectedSite()) {
$this->view->assign('can_not_proceed', true);
return;
}
$statistics = $this->indexQueue->getStatisticsBySite($this->selectedSite);
$this->view->assign('indexQueueInitializationSelector', $this->getIndexQueueInitializationSelector());
$this->view->assign('indexqueue_statistics', $statistics);
$this->view->assign('indexqueue_errors', $this->indexQueue->getErrorsBySite($this->selectedSite));
}
/**
* Checks if selected site can be queued.
*
* @return bool
*/
protected function canQueueSelectedSite()
{
2021-04-24 04:44:44 +02:00
if ($this->selectedSite === null || empty($this->meilisearchConnectionManager->getConnectionBySite($this->selectedSite))) {
2021-04-17 00:26:33 +02:00
return false;
}
2021-04-17 21:20:54 +02:00
$enabledIndexQueueConfigurationNames = $this->selectedSite->getMeilisearchConfiguration()->getEnabledIndexQueueConfigurationNames();
2021-04-17 00:26:33 +02:00
if (empty($enabledIndexQueueConfigurationNames)) {
return false;
}
return true;
}
/**
* Renders a field to select which indexing configurations to initialize.
*
* Uses TCEforms.
*
* @return string Markup for the select field
*/
protected function getIndexQueueInitializationSelector()
{
$selector = GeneralUtility::makeInstance(IndexingConfigurationSelectorField::class, /** @scrutinizer ignore-type */ $this->selectedSite);
$selector->setFormElementName('tx_meilisearch-index-queue-initialization');
return $selector->render();
}
/**
* Initializes the Index Queue for selected indexing configurations
*
* @return void
*/
public function initializeIndexQueueAction()
{
$initializedIndexingConfigurations = [];
$indexingConfigurationsToInitialize = GeneralUtility::_POST('tx_meilisearch-index-queue-initialization');
if ((!empty($indexingConfigurationsToInitialize)) && (is_array($indexingConfigurationsToInitialize))) {
// initialize selected indexing configuration
$initializedIndexingConfigurations = $this->indexQueue->getInitializationService()->initializeBySiteAndIndexConfigurations($this->selectedSite, $indexingConfigurationsToInitialize);
} else {
2021-04-17 21:20:54 +02:00
$messageLabel = 'meilisearch.backend.index_queue_module.flashmessage.initialize.no_selection';
$titleLabel = 'meilisearch.backend.index_queue_module.flashmessage.not_initialized.title';
2021-04-17 00:26:33 +02:00
$this->addFlashMessage(
2021-04-17 21:20:54 +02:00
LocalizationUtility::translate($messageLabel, 'Meilisearch'),
LocalizationUtility::translate($titleLabel, 'Meilisearch'),
2021-04-17 00:26:33 +02:00
FlashMessage::WARNING
);
}
$messagesForConfigurations = [];
foreach (array_keys($initializedIndexingConfigurations) as $indexingConfigurationName) {
$itemCount = $this->indexQueue->getStatisticsBySite($this->selectedSite, $indexingConfigurationName)->getTotalCount();
$messagesForConfigurations[] = $indexingConfigurationName . ' (' . $itemCount . ' records)';
}
if (!empty($initializedIndexingConfigurations)) {
2021-04-17 21:20:54 +02:00
$messageLabel = 'meilisearch.backend.index_queue_module.flashmessage.initialize.success';
$titleLabel = 'meilisearch.backend.index_queue_module.flashmessage.initialize.title';
2021-04-17 00:26:33 +02:00
$this->addFlashMessage(
2021-04-17 21:20:54 +02:00
LocalizationUtility::translate($messageLabel, 'Meilisearch', [implode(', ', $messagesForConfigurations)]),
LocalizationUtility::translate($titleLabel, 'Meilisearch'),
2021-04-17 00:26:33 +02:00
FlashMessage::OK
);
}
$this->redirect('index');
}
/**
* Removes all errors in the index queue list. So that the items can be indexed again.
*
* @return void
*/
public function resetLogErrorsAction()
{
$resetResult = $this->indexQueue->resetAllErrors();
2021-04-17 21:20:54 +02:00
$label = 'meilisearch.backend.index_queue_module.flashmessage.success.reset_errors';
2021-04-17 00:26:33 +02:00
$severity = FlashMessage::OK;
if (!$resetResult) {
2021-04-17 21:20:54 +02:00
$label = 'meilisearch.backend.index_queue_module.flashmessage.error.reset_errors';
2021-04-17 00:26:33 +02:00
$severity = FlashMessage::ERROR;
}
$this->addIndexQueueFlashMessage($label, $severity);
$this->redirect('index');
}
/**
* ReQueues a single item in the indexQueue.
*
* @param string $type
* @param int $uid
*
* @return void
*/
public function requeueDocumentAction(string $type, int $uid)
{
2021-04-17 21:20:54 +02:00
$label = 'meilisearch.backend.index_queue_module.flashmessage.error.single_item_not_requeued';
2021-04-17 00:26:33 +02:00
$severity = FlashMessage::ERROR;
$updateCount = $this->indexQueue->updateItem($type, $uid, time());
if ($updateCount > 0) {
2021-04-17 21:20:54 +02:00
$label = 'meilisearch.backend.index_queue_module.flashmessage.success.single_item_was_requeued';
2021-04-17 00:26:33 +02:00
$severity = FlashMessage::OK;
}
$this->addIndexQueueFlashMessage($label, $severity);
$this->redirect('index');
}
/**
* Shows the error message for one queue item.
*
* @param int $indexQueueItemId
* @return void
*/
public function showErrorAction(int $indexQueueItemId)
{
if (is_null($indexQueueItemId)) {
// add a flash message and quit
2021-04-17 21:20:54 +02:00
$label = 'meilisearch.backend.index_queue_module.flashmessage.error.no_queue_item_for_queue_error';
2021-04-17 00:26:33 +02:00
$severity = FlashMessage::ERROR;
$this->addIndexQueueFlashMessage($label, $severity);
return;
}
$item = $this->indexQueue->getItem($indexQueueItemId);
$this->view->assign('indexQueueItem', $item);
}
/**
* Indexes a few documents with the index service.
* @return void
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\UnsupportedRequestTypeException
*/
public function doIndexingRunAction()
{
/** @var $indexService \WapplerSystems\Meilisearch\Domain\Index\IndexService */
$indexService = GeneralUtility::makeInstance(IndexService::class, /** @scrutinizer ignore-type */ $this->selectedSite);
$indexWithoutErrors = $indexService->indexItems(10);
2021-04-17 21:20:54 +02:00
$label = 'meilisearch.backend.index_queue_module.flashmessage.success.index_manual';
2021-04-17 00:26:33 +02:00
$severity = FlashMessage::OK;
if (!$indexWithoutErrors) {
2021-04-17 21:20:54 +02:00
$label = 'meilisearch.backend.index_queue_module.flashmessage.error.index_manual';
2021-04-17 00:26:33 +02:00
$severity = FlashMessage::ERROR;
}
$this->addFlashMessage(
2021-04-17 21:20:54 +02:00
LocalizationUtility::translate($label, 'Meilisearch'),
LocalizationUtility::translate('meilisearch.backend.index_queue_module.flashmessage.index_manual', 'Meilisearch'),
2021-04-17 00:26:33 +02:00
$severity
);
$this->redirect('index');
}
/**
* Adds a flash message for the index queue module.
*
* @param string $label
* @param int $severity
*/
protected function addIndexQueueFlashMessage($label, $severity)
{
2021-04-17 21:20:54 +02:00
$this->addFlashMessage(LocalizationUtility::translate($label, 'Meilisearch'), LocalizationUtility::translate('meilisearch.backend.index_queue_module.flashmessage.title', 'Meilisearch'), $severity);
2021-04-17 00:26:33 +02:00
}
}