first commit

This commit is contained in:
Sven Wappler
2021-04-24 04:44:44 +02:00
parent cadcc8edb4
commit 2c9e27b3b7
55 changed files with 333 additions and 3877 deletions

View File

@@ -245,7 +245,7 @@ abstract class AbstractModuleController extends ActionController
}
$this->initializeSelectedMeilisearchCoreConnection();
$cores = $this->meilisearchConnectionManager->getConnectionsBySite($site);
$cores = $this->meilisearchConnectionManager->getConnectionBySite($site);
foreach ($cores as $core) {
$coreAdmin = $core->getAdminService();
$menuItem = $this->coreSelectorMenu->makeMenuItem();
@@ -295,7 +295,7 @@ abstract class AbstractModuleController extends ActionController
{
$moduleData = $this->moduleDataStorageService->loadModuleData();
$meilisearchCoreConnections = $this->meilisearchConnectionManager->getConnectionsBySite($this->selectedSite);
$meilisearchCoreConnections = $this->meilisearchConnectionManager->getConnectionBySite($this->selectedSite);
$currentMeilisearchCorePath = $moduleData->getCore();
if (empty($currentMeilisearchCorePath)) {
$this->initializeFirstAvailableMeilisearchCoreConnection($meilisearchCoreConnections, $moduleData);

View File

@@ -1,386 +0,0 @@
<?php
namespace WapplerSystems\Meilisearch\Controller\Backend\Search;
/***************************************************************
* Copyright notice
*
* (c) 2010-2017 dkd Internet Service GmbH <meilisearchs-support@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\Utility\ManagedResourcesUtility;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
/**
* Manage Synonyms and Stop words in Backend Module
* @property \TYPO3\CMS\Extbase\Mvc\Web\Response $response
*/
class CoreOptimizationModuleController extends AbstractModuleController
{
/**
* Set up the doc header properly here
*
* @param ViewInterface $view
* @return void
*/
protected function initializeView(ViewInterface $view)
{
parent::initializeView($view);
$this->generateCoreSelectorMenuUsingPageTree();
/* @var ModuleTemplate $module */ // holds the state of chosen tab
$module = $this->objectManager->get(ModuleTemplate::class);
$coreOptimizationTabs = $module->getDynamicTabMenu([], 'coreOptimization');
$this->view->assign('tabs', $coreOptimizationTabs);
}
/**
* Gets synonyms and stopwords for the currently selected core
*
* @return void
*/
public function indexAction()
{
if ($this->selectedMeilisearchCoreConnection === null) {
$this->view->assign('can_not_proceed', true);
return;
}
$synonyms = [];
$coreAdmin = $this->selectedMeilisearchCoreConnection->getAdminService();
$rawSynonyms = $coreAdmin->getSynonyms();
foreach ($rawSynonyms as $baseWord => $synonymList) {
$synonyms[$baseWord] = implode(', ', $synonymList);
}
$stopWords = $coreAdmin->getStopWords();
$this->view->assignMultiple([
'synonyms' => $synonyms,
'stopWords' => implode(PHP_EOL, $stopWords),
'stopWordsCount' => count($stopWords)
]);
}
/**
* Add synonyms to selected core
*
* @param string $baseWord
* @param string $synonyms
* @param bool $overrideExisting
* @return void
*/
public function addSynonymsAction(string $baseWord, string $synonyms, $overrideExisting)
{
if (empty($baseWord) || empty($synonyms)) {
$this->addFlashMessage(
'Please provide a base word and synonyms.',
'Missing parameter',
FlashMessage::ERROR
);
} else {
$baseWord = mb_strtolower($baseWord);
$synonyms = mb_strtolower($synonyms);
$coreAdmin = $this->selectedMeilisearchCoreConnection->getAdminService();
if ($overrideExisting && $coreAdmin->getSynonyms($baseWord)) {
$coreAdmin->deleteSynonym($baseWord);
}
$coreAdmin->addSynonym($baseWord, GeneralUtility::trimExplode(',', $synonyms, true));
$coreAdmin->reloadCore();
$this->addFlashMessage(
'"' . $synonyms . '" added as synonyms for base word "' . $baseWord . '"'
);
}
$this->redirect('index');
}
/**
* @param string $fileFormat
* @return void
*/
public function exportStopWordsAction($fileFormat = 'txt')
{
$coreAdmin = $this->selectedMeilisearchCoreConnection->getAdminService();
$this->exportFile(
implode(PHP_EOL, $coreAdmin->getStopWords()),
'stopwords',
$fileFormat
);
}
/**
* Exports synonyms to a download file.
*
* @param string $fileFormat
* @return string
*/
public function exportSynonymsAction($fileFormat = 'txt')
{
$coreAdmin = $this->selectedMeilisearchCoreConnection->getAdminService();
$synonyms = $coreAdmin->getSynonyms();
return $this->exportFile(ManagedResourcesUtility::exportSynonymsToTxt($synonyms), 'synonyms', $fileFormat);
}
/**
* @param array $synonymFileUpload
* @param bool $overrideExisting
* @param bool $deleteSynonymsBefore
* @return void
*/
public function importSynonymListAction(array $synonymFileUpload, $overrideExisting, $deleteSynonymsBefore)
{
if ($deleteSynonymsBefore) {
$this->deleteAllSynonyms();
}
$fileLines = ManagedResourcesUtility::importSynonymsFromPlainTextContents($synonymFileUpload);
$synonymCount = 0;
$coreAdmin = $this->selectedMeilisearchCoreConnection->getAdminService();
foreach ($fileLines as $baseWord => $synonyms) {
if (!isset($baseWord) || empty($synonyms)) {
continue;
}
$this->deleteExistingSynonym($overrideExisting, $deleteSynonymsBefore, $baseWord);
$coreAdmin->addSynonym($baseWord, $synonyms);
$synonymCount++;
}
$coreAdmin->reloadCore();
$this->addFlashMessage(
$synonymCount . ' synonyms imported.'
);
$this->redirect('index');
}
/**
* @param array $stopwordsFileUpload
* @param bool $replaceStopwords
* @return void
*/
public function importStopWordListAction(array $stopwordsFileUpload, $replaceStopwords)
{
$this->saveStopWordsAction(
ManagedResourcesUtility::importStopwordsFromPlainTextContents($stopwordsFileUpload),
$replaceStopwords
);
}
/**
* Delete complete synonym list
*
* @return void
*/
public function deleteAllSynonymsAction()
{
$allSynonymsCouldBeDeleted = $this->deleteAllSynonyms();
$coreAdmin = $this->selectedMeilisearchCoreConnection->getAdminService();
$reloadResponse = $coreAdmin->reloadCore();
if ($allSynonymsCouldBeDeleted
&& $reloadResponse->getHttpStatus() == 200
) {
$this->addFlashMessage(
'All synonym removed.'
);
} else {
$this->addFlashMessage(
'Failed to remove all synonyms.',
'An error occurred',
FlashMessage::ERROR
);
}
$this->redirect('index');
}
/**
* Deletes a synonym mapping by its base word.
*
* @param string $baseWord Synonym mapping base word
*/
public function deleteSynonymsAction($baseWord)
{
$coreAdmin = $this->selectedMeilisearchCoreConnection->getAdminService();
$deleteResponse = $coreAdmin->deleteSynonym($baseWord);
$reloadResponse = $coreAdmin->reloadCore();
if ($deleteResponse->getHttpStatus() == 200
&& $reloadResponse->getHttpStatus() == 200
) {
$this->addFlashMessage(
'Synonym removed.'
);
} else {
$this->addFlashMessage(
'Failed to remove synonym.',
'An error occurred',
FlashMessage::ERROR
);
}
$this->redirect('index');
}
/**
* Saves the edited stop word list to Meilisearch
*
* @param string $stopWords
* @param bool $replaceStopwords
* @return void
*/
public function saveStopWordsAction(string $stopWords, $replaceStopwords = true)
{
// lowercase stopword before saving because terms get lowercased before stopword filtering
$newStopWords = mb_strtolower($stopWords);
$newStopWords = GeneralUtility::trimExplode("\n", $newStopWords, true);
$coreAdmin = $this->selectedMeilisearchCoreConnection->getAdminService();
$oldStopWords = $coreAdmin->getStopWords();
if ($replaceStopwords) {
$removedStopWords = array_diff($oldStopWords, $newStopWords);
$wordsRemoved = $this->removeStopsWordsFromIndex($removedStopWords);
} else {
$wordsRemoved = true;
}
$wordsAdded = true;
$addedStopWords = array_diff($newStopWords, $oldStopWords);
if (!empty($addedStopWords)) {
$wordsAddedResponse = $coreAdmin->addStopWords($addedStopWords);
$wordsAdded = ($wordsAddedResponse->getHttpStatus() == 200);
}
$reloadResponse = $coreAdmin->reloadCore();
if ($wordsRemoved && $wordsAdded && $reloadResponse->getHttpStatus() == 200) {
$this->addFlashMessage(
'Stop Words Updated.'
);
}
$this->redirect('index');
}
/**
* @param string $content
* @param string $type
* @param string $fileExtension
* @return string
*/
protected function exportFile($content, $type = 'synonyms', $fileExtension = 'txt') : string
{
$coreAdmin = $this->selectedMeilisearchCoreConnection->getAdminService();
$this->response->setHeader('Content-type', 'text/plain', true);
$this->response->setHeader('Cache-control', 'public', true);
$this->response->setHeader('Content-Description', 'File transfer', true);
$this->response->setHeader(
'Content-disposition',
'attachment; filename =' . $type . '_' .
$coreAdmin->getPrimaryEndpoint()->getCore() . '.' . $fileExtension,
true
);
$this->response->setContent($content);
$this->sendFileResponse();
}
/**
* This method send the headers and content and does an exit, since without the exit TYPO3 produces and error.
* @return void
*/
protected function sendFileResponse()
{
$this->response->sendHeaders();
$this->response->send();
$this->response->shutdown();
exit();
}
/**
* Delete complete synonym list form meilisearch
*
* @return bool
*/
protected function deleteAllSynonyms() : bool
{
$coreAdmin = $this->selectedMeilisearchCoreConnection->getAdminService();
$synonyms = $coreAdmin->getSynonyms();
$allSynonymsCouldBeDeleted = true;
foreach ($synonyms as $baseWord => $synonym) {
$deleteResponse = $coreAdmin->deleteSynonym($baseWord);
$allSynonymsCouldBeDeleted = $allSynonymsCouldBeDeleted && $deleteResponse->getHttpStatus() == 200;
}
return $allSynonymsCouldBeDeleted;
}
/**
* @param $stopwordsToRemove
* @return bool
*/
protected function removeStopsWordsFromIndex($stopwordsToRemove) : bool
{
$wordsRemoved = true;
$coreAdmin = $this->selectedMeilisearchCoreConnection->getAdminService();
foreach ($stopwordsToRemove as $word) {
$response = $coreAdmin->deleteStopWord($word);
if ($response->getHttpStatus() != 200) {
$wordsRemoved = false;
$this->addFlashMessage(
'Failed to remove stop word "' . $word . '".',
'An error occurred',
FlashMessage::ERROR
);
break;
}
}
return $wordsRemoved;
}
/**
* Delete synonym entry if selceted before
* @param bool $overrideExisting
* @param bool $deleteSynonymsBefore
* @param string $baseWord
*/
protected function deleteExistingSynonym($overrideExisting, $deleteSynonymsBefore, $baseWord)
{
$coreAdmin = $this->selectedMeilisearchCoreConnection->getAdminService();
if (!$deleteSynonymsBefore &&
$overrideExisting &&
$coreAdmin->getSynonyms($baseWord)
) {
$coreAdmin->deleteSynonym($baseWord);
}
}
}

View File

@@ -77,7 +77,7 @@ class IndexAdministrationModuleController extends AbstractModuleController
*/
public function indexAction()
{
if ($this->selectedSite === null || empty($this->meilisearchConnectionManager->getConnectionsBySite($this->selectedSite))) {
if ($this->selectedSite === null || empty($this->meilisearchConnectionManager->getConnectionBySite($this->selectedSite))) {
$this->view->assign('can_not_proceed', true);
}
}
@@ -93,7 +93,7 @@ class IndexAdministrationModuleController extends AbstractModuleController
try {
$affectedCores = [];
$meilisearchServers = $this->meilisearchConnectionManager->getConnectionsBySite($this->selectedSite);
$meilisearchServers = $this->meilisearchConnectionManager->getConnectionBySite($this->selectedSite);
foreach ($meilisearchServers as $meilisearchServer) {
$writeService = $meilisearchServer->getWriteService();
/* @var $meilisearchServer MeilisearchConnection */
@@ -134,7 +134,7 @@ class IndexAdministrationModuleController extends AbstractModuleController
{
$coresReloaded = true;
$reloadedCores = [];
$meilisearchServers = $this->meilisearchConnectionManager->getConnectionsBySite($this->selectedSite);
$meilisearchServers = $this->meilisearchConnectionManager->getConnectionBySite($this->selectedSite);
foreach ($meilisearchServers as $meilisearchServer) {
/* @var $meilisearchServer MeilisearchConnection */

View File

@@ -114,7 +114,7 @@ class IndexQueueModuleController extends AbstractModuleController
*/
protected function canQueueSelectedSite()
{
if ($this->selectedSite === null || empty($this->meilisearchConnectionManager->getConnectionsBySite($this->selectedSite))) {
if ($this->selectedSite === null || empty($this->meilisearchConnectionManager->getConnectionBySite($this->selectedSite))) {
return false;
}
$enabledIndexQueueConfigurationNames = $this->selectedSite->getMeilisearchConfiguration()->getEnabledIndexQueueConfigurationNames();

View File

@@ -24,10 +24,11 @@ namespace WapplerSystems\Meilisearch\Controller\Backend\Search;
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
use TYPO3\CMS\Core\Utility\DebugUtility;
use WapplerSystems\Meilisearch\Api;
use WapplerSystems\Meilisearch\ConnectionManager;
use WapplerSystems\Meilisearch\Domain\Search\Statistics\StatisticsRepository;
use WapplerSystems\Meilisearch\Domain\Search\ApacheMeilisearchDocument\Repository;
use WapplerSystems\Meilisearch\Domain\Search\MeilisearchDocument\Repository;
use WapplerSystems\Meilisearch\System\Meilisearch\ResponseAdapter;
use WapplerSystems\Meilisearch\System\Validator\Path;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
@@ -93,8 +94,8 @@ class InfoModuleController extends AbstractModuleController
$this->collectConnectionInfos();
$this->collectStatistics();
$this->collectIndexFieldsInfo();
$this->collectIndexInspectorInfo();
//$this->collectIndexFieldsInfo();
//$this->collectIndexInspectorInfo();
}
/**
@@ -122,30 +123,22 @@ class InfoModuleController extends AbstractModuleController
$missingHosts = [];
$invalidPaths = [];
/* @var Path $path */
$path = GeneralUtility::makeInstance(Path::class);
$connections = $this->meilisearchConnectionManager->getConnectionsBySite($this->selectedSite);
$connection = $this->meilisearchConnectionManager->getConnectionBySite($this->selectedSite);
if (empty($connections)) {
if (empty($connection)) {
$this->view->assign('can_not_proceed', true);
return;
}
foreach ($connections as $connection) {
$coreAdmin = $connection->getAdminService();
$coreUrl = (string)$coreAdmin;
$coreAdmin = $connection->getAdminService();
if ($coreAdmin->ping()) {
$connectedHosts[] = $coreUrl;
} else {
$missingHosts[] = $coreUrl;
}
if (!$path->isValidMeilisearchPath($coreAdmin->getCorePath())) {
$invalidPaths[] = $coreAdmin->getCorePath();
}
if ($coreAdmin->ping()) {
$connectedHosts[] = $coreAdmin;
} else {
$missingHosts[] = $coreAdmin;
}
$this->view->assignMultiple([
'site' => $this->selectedSite,
'apiKey' => Api::getApiKey(),
@@ -204,36 +197,15 @@ class InfoModuleController extends AbstractModuleController
{
$indexFieldsInfoByCorePaths = [];
$meilisearchCoreConnections = $this->meilisearchConnectionManager->getConnectionsBySite($this->selectedSite);
foreach ($meilisearchCoreConnections as $meilisearchCoreConnection) {
$meilisearchCoreConnections = $this->meilisearchConnectionManager->getConnectionBySite($this->selectedSite);
foreach ($meilisearchCoreConnections as $i => $meilisearchCoreConnection) {
$coreAdmin = $meilisearchCoreConnection->getAdminService();
$indexFieldsInfo = [
'corePath' => $coreAdmin->getCorePath()
];
if ($coreAdmin->ping()) {
$lukeData = $coreAdmin->getLukeMetaData();
/* @var Registry $registry */
$registry = GeneralUtility::makeInstance(Registry::class);
$limit = $registry->get('tx_meilisearch', 'luke.limit', 20000);
$limitNote = '';
if (isset($lukeData->index->numDocs) && $lukeData->index->numDocs > $limit) {
$limitNote = '<em>Too many terms</em>';
} elseif (isset($lukeData->index->numDocs)) {
$limitNote = 'Nothing indexed';
// below limit, so we can get more data
// Note: we use 2 since 1 fails on Ubuntu Hardy.
$lukeData = $coreAdmin->getLukeMetaData(2);
}
$fields = $this->getFields($lukeData, $limitNote);
$coreMetrics = $this->getCoreMetrics($lukeData, $fields);
$indexFieldsInfo['noError'] = 'OK';
$indexFieldsInfo['fields'] = $fields;
$indexFieldsInfo['coreMetrics'] = $coreMetrics;
$indexFieldsInfo['coreMetrics'] = 'dedede';
} else {
$indexFieldsInfo['noError'] = null;
@@ -243,7 +215,7 @@ class InfoModuleController extends AbstractModuleController
FlashMessage::ERROR
);
}
$indexFieldsInfoByCorePaths[$coreAdmin->getCorePath()] = $indexFieldsInfo;
$indexFieldsInfoByCorePaths[$i] = $indexFieldsInfo;
}
$this->view->assign('indexFieldsInfoByCorePaths', $indexFieldsInfoByCorePaths);
}
@@ -255,7 +227,7 @@ class InfoModuleController extends AbstractModuleController
*/
protected function collectIndexInspectorInfo()
{
$meilisearchCoreConnections = $this->meilisearchConnectionManager->getConnectionsBySite($this->selectedSite);
$meilisearchCoreConnections = $this->meilisearchConnectionManager->getConnectionBySite($this->selectedSite);
$documentsByCoreAndType = [];
foreach ($meilisearchCoreConnections as $languageId => $meilisearchCoreConnection) {
$coreAdmin = $meilisearchCoreConnection->getAdminService();