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

@ -1,4 +1,5 @@
<?php
namespace WapplerSystems\Meilisearch;
/***************************************************************
@ -66,9 +67,9 @@ class ConnectionManager implements SingletonInterface
/**
* @param SystemLanguageRepository $systemLanguageRepository
* @param SystemLanguageRepository|null $systemLanguageRepository
* @param PagesRepositoryAtExtMeilisearch|null $pagesRepositoryAtExtMeilisearch
* @param SiteRepository $siteRepository
* @param SiteRepository|null $siteRepository
*/
public function __construct(
SystemLanguageRepository $systemLanguageRepository = null,
@ -77,24 +78,22 @@ class ConnectionManager implements SingletonInterface
)
{
$this->systemLanguageRepository = $systemLanguageRepository ?? GeneralUtility::makeInstance(SystemLanguageRepository::class);
$this->siteRepository = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class);
$this->siteRepository = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class);
$this->pagesRepositoryAtExtMeilisearch = $pagesRepositoryAtExtMeilisearch ?? GeneralUtility::makeInstance(PagesRepositoryAtExtMeilisearch::class);
}
/**
* Creates a meilisearch connection for read and write endpoints
*
* @param array $readNodeConfiguration
* @param array $writeNodeConfiguration
* @param array $siteConfiguration
* @return MeilisearchConnection|object
*/
public function getMeilisearchConnectionForNodes(array $readNodeConfiguration, array $writeNodeConfiguration)
public function getMeilisearchConnectionForNode(array $siteConfiguration)
{
$connectionHash = md5(json_encode($readNodeConfiguration) . json_encode($writeNodeConfiguration));
$connectionHash = md5(json_encode($siteConfiguration));
if (!isset(self::$connections[$connectionHash])) {
$readNode = $this->createClientFromArray($readNodeConfiguration);
$writeNode = $this->createClientFromArray($writeNodeConfiguration);
self::$connections[$connectionHash] = GeneralUtility::makeInstance(MeilisearchConnection::class, $readNode, $writeNode);
$node = $this->createClientFromArray($siteConfiguration);
self::$connections[$connectionHash] = GeneralUtility::makeInstance(MeilisearchConnection::class, $node, $siteConfiguration);
}
return self::$connections[$connectionHash];
}
@ -107,11 +106,11 @@ class ConnectionManager implements SingletonInterface
*/
public function getConnectionFromConfiguration(array $config)
{
if(empty($config['read']) && !empty($config['meilisearchHost'])) {
if (empty($config) && !empty($config['meilisearchHost'])) {
throw new InvalidArgumentException('Invalid registry data please re-initialize your meilisearch connections');
}
return $this->getMeilisearchConnectionForNodes($config['read'], $config['write']);
return $this->getMeilisearchConnectionForNode($config);
}
/**
@ -131,7 +130,7 @@ class ConnectionManager implements SingletonInterface
$config = $site->getMeilisearchConnectionConfiguration($language);
$meilisearchConnection = $this->getConnectionFromConfiguration($config);
return $meilisearchConnection;
} catch(InvalidArgumentException $e) {
} catch (InvalidArgumentException $e) {
$noMeilisearchConnectionException = $this->buildNoConnectionExceptionForPageAndLanguage($pageId, $language);
throw $noMeilisearchConnectionException;
}
@ -170,7 +169,7 @@ class ConnectionManager implements SingletonInterface
{
$meilisearchConnections = [];
foreach ($this->siteRepository->getAvailableSites() as $site) {
foreach ($site->getAllMeilisearchConnectionConfigurations() as $meilisearchConfiguration) {
foreach ($site->getMeilisearchConnectionConfigurations() as $meilisearchConfiguration) {
$meilisearchConnections[] = $this->getConnectionFromConfiguration($meilisearchConfiguration);
}
}
@ -182,18 +181,11 @@ class ConnectionManager implements SingletonInterface
* Gets all connections configured for a given site.
*
* @param Site $site A TYPO3 site
* @return MeilisearchConnection[] An array of Meilisearch connection objects (WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchConnection)
* @throws NoMeilisearchConnectionFoundException
* @return MeilisearchConnection An array of Meilisearch connection objects (WapplerSystems\Meilisearch\System\Meilisearch\MeilisearchConnection)
*/
public function getConnectionsBySite(Site $site)
public function getConnectionBySite(Site $site)
{
$connections = [];
foreach ($site->getAllMeilisearchConnectionConfigurations() as $languageId => $meilisearchConnectionConfiguration) {
$connections[$languageId] = $this->getConnectionFromConfiguration($meilisearchConnectionConfiguration);
}
return $connections;
return $this->getConnectionFromConfiguration($site->getMeilisearchConnectionConfiguration());
}
/**
@ -211,7 +203,7 @@ class ConnectionManager implements SingletonInterface
. $connection['read']['host'] . ':'
. $connection['read']['port']
. $connection['read']['path']
.' - Write node: '
. ' - Write node: '
. $connection['write']['host'] . ':'
. $connection['write']['port']
. $connection['write']['path'];
@ -266,8 +258,9 @@ class ConnectionManager implements SingletonInterface
}
private function createClientFromArray(array $configuration) {
return new Client(($configuration['scheme'] ?? 'http') . '://'.$configuration['host'].':'.$configuration['port'], $configuration['apiKey'] ?? null, new \TYPO3\CMS\Core\Http\Client(\TYPO3\CMS\Core\Http\Client\GuzzleClientFactory::getClient()));
private function createClientFromArray(array $configuration)
{
return new Client(($configuration['scheme'] ?? 'http') . '://' . $configuration['host'] . ':' . $configuration['port'], $configuration['apiKey'] ?? null, new \TYPO3\CMS\Core\Http\Client(\TYPO3\CMS\Core\Http\Client\GuzzleClientFactory::getClient()));
}

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();

View File

@ -33,8 +33,6 @@ use WapplerSystems\Meilisearch\Domain\Site\Site;
use WapplerSystems\Meilisearch\System\Configuration\TypoScriptConfiguration;
use WapplerSystems\Meilisearch\System\Logging\MeilisearchLogManager;
use WapplerSystems\Meilisearch\Task\IndexQueueWorkerTask;
use Solarium\Exception\HttpException;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
@ -140,7 +138,7 @@ class IndexService
$this->emitSignal('afterIndexItems', [$itemsToIndex, $this->getContextTask(), $indexRunId]);
if ($enableCommitsSetting && count($itemsToIndex) > 0) {
$meilisearchServers = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionsBySite($this->site);
$meilisearchServers = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionBySite($this->site);
foreach ($meilisearchServers as $meilisearchServer) {
try {
$meilisearchServer->getWriteService()->commit(false, false, false);

View File

@ -121,7 +121,7 @@ abstract class AbstractStrategy
$enableCommitsSetting = $site->getMeilisearchConfiguration()->getEnableCommits();
$siteHash = $site->getSiteHash();
// a site can have multiple connections (cores / languages)
$meilisearchConnections = $this->connectionManager->getConnectionsBySite($site);
$meilisearchConnections = $this->connectionManager->getConnectionBySite($site);
if ($language > 0) {
$meilisearchConnections = [$language => $meilisearchConnections[$language]];
}

View File

@ -1,5 +1,5 @@
<?php
namespace WapplerSystems\Meilisearch\Domain\Search\ApacheMeilisearchDocument;
namespace WapplerSystems\Meilisearch\Domain\Search\MeilisearchDocument;
/***************************************************************
* Copyright notice
@ -35,7 +35,7 @@ use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
/**
* Builder class to build an ApacheMeilisearchDocument
* Builder class to build an MeilisearchDocument
*
* Responsible to build \WapplerSystems\Meilisearch\System\Meilisearch\Document\Document
*
@ -64,38 +64,37 @@ class Builder
* @param string $url
* @param Rootline $pageAccessRootline
* @param string $mountPointParameter
* @return Document|object
* @return array
*/
public function fromPage(TypoScriptFrontendController $page, $url, Rootline $pageAccessRootline, $mountPointParameter): Document
public function fromPage(TypoScriptFrontendController $page, $url, Rootline $pageAccessRootline, $mountPointParameter): array
{
/* @var $document Document */
$document = GeneralUtility::makeInstance(Document::class);
$document = [];
$site = $this->getSiteByPageId($page->id);
$pageRecord = $page->page;
$accessGroups = $this->getDocumentIdGroups($pageAccessRootline);
$documentId = $this->getPageDocumentId($page, $accessGroups, $mountPointParameter);
$document->setField('id', $documentId);
$document->setField('site', $site->getDomain());
$document->setField('siteHash', $site->getSiteHash());
$document->setField('appKey', 'EXT:meilisearch');
$document->setField('type', 'pages');
$document['id'] = $documentId;
$document['site'] = $site->getDomain();
$document['siteHash'] = $site->getSiteHash();
$document['appKey'] = 'EXT:meilisearch';
$document['type'] = 'pages';
// system fields
$document->setField('uid', $page->id);
$document->setField('pid', $pageRecord['pid']);
$document['uid'] = $page->id;
$document['pid'] = $pageRecord['pid'];
// variantId
$variantId = $this->variantIdBuilder->buildFromTypeAndUid('pages', $page->id);
$document->setField('variantId', $variantId);
$document['variantId'] = $variantId;
$document->setField('typeNum', $page->type);
$document->setField('created', $pageRecord['crdate']);
$document->setField('changed', $pageRecord['SYS_LASTCHANGED']);
$document['typeNum'] = $page->type;
$document['created'] = $pageRecord['crdate'];
$document['changed'] = $pageRecord['SYS_LASTCHANGED'];
$rootline = $this->getRootLineFieldValue($page->id, $mountPointParameter);
$document->setField('rootline', $rootline);
$document['rootline'] = $rootline;
// access
$this->addAccessField($document, $pageAccessRootline);
@ -104,14 +103,14 @@ class Builder
// content
// @extensionScannerIgnoreLine
$contentExtractor = $this->getExtractorForPageContent($page->content);
$document->setField('title', $contentExtractor->getPageTitle());
$document->setField('subTitle', $pageRecord['subtitle']);
$document->setField('navTitle', $pageRecord['nav_title']);
$document->setField('author', $pageRecord['author']);
$document->setField('description', $pageRecord['description']);
$document->setField('abstract', $pageRecord['abstract']);
$document->setField('content', $contentExtractor->getIndexableContent());
$document->setField('url', $url);
$document['title'] = $contentExtractor->getPageTitle();
$document['subTitle'] = $pageRecord['subtitle'];
$document['navTitle'] = $pageRecord['nav_title'];
$document['author'] = $pageRecord['author'];
$document['description'] = $pageRecord['description'];
$document['abstract'] = $pageRecord['abstract'];
$document['content'] = $contentExtractor->getIndexableContent();
$document['url'] = $url;
$this->addKeywordsField($document, $pageRecord);
$this->addTagContentFields($document, $contentExtractor->getTagContent());
@ -127,9 +126,9 @@ class Builder
* @param string $type
* @param int $rootPageUid
* @param string $accessRootLine
* @return Document
* @return array
*/
public function fromRecord(array $itemRecord, string $type, int $rootPageUid, string $accessRootLine): Document
public function fromRecord(array $itemRecord, string $type, int $rootPageUid, string $accessRootLine): array
{
/* @var $document Document */
$document = GeneralUtility::makeInstance(Document::class);
@ -139,36 +138,36 @@ class Builder
$documentId = $this->getDocumentId($type, $site->getRootPageId(), $itemRecord['uid']);
// required fields
$document->setField('id', $documentId);
$document->setField('type', $type);
$document->setField('appKey', 'EXT:meilisearch');
$document['id'] = $documentId;
$document['type'] = $type;
$document['appKey'] = 'EXT:meilisearch';
// site, siteHash
$document->setField('site', $site->getDomain());
$document->setField('siteHash', $site->getSiteHash());
$document['site'] = $site->getDomain();
$document['siteHash'] = $site->getSiteHash();
// uid, pid
$document->setField('uid', $itemRecord['uid']);
$document->setField('pid', $itemRecord['pid']);
$document['uid'] = $itemRecord['uid'];
$document['pid'] = $itemRecord['pid'];
// variantId
$variantId = $this->variantIdBuilder->buildFromTypeAndUid($type, $itemRecord['uid']);
$document->setField('variantId', $variantId);
$document['variantId'] = $variantId;
// created, changed
if (!empty($GLOBALS['TCA'][$type]['ctrl']['crdate'])) {
$document->setField('created', $itemRecord[$GLOBALS['TCA'][$type]['ctrl']['crdate']]);
$document['created'] = $itemRecord[$GLOBALS['TCA'][$type]['ctrl']['crdate']];
}
if (!empty($GLOBALS['TCA'][$type]['ctrl']['tstamp'])) {
$document->setField('changed', $itemRecord[$GLOBALS['TCA'][$type]['ctrl']['tstamp']]);
$document['changed'] = $itemRecord[$GLOBALS['TCA'][$type]['ctrl']['tstamp']];
}
// access, endtime
$document->setField('access', $accessRootLine);
$document['access'] = $accessRootLine;
if (!empty($GLOBALS['TCA'][$type]['ctrl']['enablecolumns']['endtime'])
&& $itemRecord[$GLOBALS['TCA'][$type]['ctrl']['enablecolumns']['endtime']] != 0
) {
$document->setField('endtime', $itemRecord[$GLOBALS['TCA'][$type]['ctrl']['enablecolumns']['endtime']]);
$document['endtime'] = $itemRecord[$GLOBALS['TCA'][$type]['ctrl']['enablecolumns']['endtime']];
}
return $document;
@ -255,37 +254,37 @@ class Builder
/**
* Adds the access field to the document if needed.
*
* @param Document $document
* @param array $document
* @param Rootline $pageAccessRootline
*/
protected function addAccessField(Document $document, Rootline $pageAccessRootline)
protected function addAccessField(array &$document, Rootline $pageAccessRootline)
{
$access = (string)$pageAccessRootline;
if (trim($access) !== '') {
$document->setField('access', $access);
$document['access'] = $access;
}
}
/**
* Adds the endtime field value to the Document.
*
* @param Document $document
* @param array $document
* @param array $pageRecord
*/
protected function addEndtimeField(Document $document, $pageRecord)
protected function addEndtimeField(array &$document, $pageRecord)
{
if ($pageRecord['endtime']) {
$document->setField('endtime', $pageRecord['endtime']);
$document['endtime'] = $pageRecord['endtime'];
}
}
/**
* Adds keywords, multi valued.
*
* @param Document $document
* @param array $document
* @param array $pageRecord
*/
protected function addKeywordsField(Document $document, $pageRecord)
protected function addKeywordsField(array &$document, $pageRecord)
{
if (!isset($pageRecord['keywords'])) {
return;
@ -293,20 +292,20 @@ class Builder
$keywords = array_unique(GeneralUtility::trimExplode(',', $pageRecord['keywords'], true));
foreach ($keywords as $keyword) {
$document->addField('keywords', $keyword);
$document['keywords'] = $keyword;
}
}
/**
* Add content from several tags like headers, anchors, ...
*
* @param Document $document
* @param array $document
* @param array $tagContent
*/
protected function addTagContentFields(Document $document, $tagContent = [])
protected function addTagContentFields(array &$document, $tagContent = [])
{
foreach ($tagContent as $fieldName => $fieldValue) {
$document->setField($fieldName, $fieldValue);
$document[$fieldName] = $fieldValue;
}
}
}

View File

@ -1,5 +1,5 @@
<?php
namespace WapplerSystems\Meilisearch\Domain\Search\ApacheMeilisearchDocument;
namespace WapplerSystems\Meilisearch\Domain\Search\MeilisearchDocument;
/***************************************************************
* Copyright notice

View File

@ -1,47 +0,0 @@
<?php
namespace WapplerSystems\Meilisearch\Domain\Search\Query;
/***************************************************************
* Copyright notice
*
* (c) 2010-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 Solarium\QueryType\Extract\Query as SolariumExtractQuery;
/**
* Specialized query for content extraction using Meilisearch Cell
*
*/
class ExtractingQuery extends SolariumExtractQuery
{
/**
* Constructor
*
* @param string $file Absolute path to the file to extract content and meta data from.
*/
public function __construct($file)
{
parent::__construct();
$this->setFile($file);
$this->addParam('extractFormat', 'text');
}
}

View File

@ -1,47 +0,0 @@
<?php
namespace WapplerSystems\Meilisearch\Domain\Search\Query;
/***************************************************************
* 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 Solarium\QueryType\Select\Query\Query as SolariumQuery;
class Query extends SolariumQuery {
/**
* Returns the query parameters that should be used.
*
* @return array
*/
public function getQueryParameters() {
return $this->getParams();
}
/**
* @return string
*/
public function __toString()
{
return $this->getQuery();
}
}

View File

@ -62,7 +62,7 @@ class DefaultResultParser extends AbstractResultParser {
}
foreach ($documents as $searchResult) {
$searchResultObject = $this->searchResultBuilder->fromApacheMeilisearchDocument($searchResult);
$searchResultObject = $this->searchResultBuilder->fromMeilisearchDocument($searchResult);
$searchResults[] = $searchResultObject;
}

View File

@ -42,7 +42,7 @@ class SearchResultBuilder {
* @throws \InvalidArgumentException
* @return SearchResult
*/
public function fromApacheMeilisearchDocument(Document $originalDocument)
public function fromMeilisearchDocument(Document $originalDocument)
{
$searchResultClassName = $this->getResultClassName();

View File

@ -431,7 +431,7 @@ class SearchResultSetService
throw new \UnexpectedValueException("Response did not contain a valid Document object");
}
return $this->searchResultBuilder->fromApacheMeilisearchDocument($resultDocument);
return $this->searchResultBuilder->fromMeilisearchDocument($resultDocument);
}
/**

View File

@ -239,28 +239,14 @@ abstract class Site implements SiteInterface
return $rootPageIds;
}
/**
* @return array
* @throws NoMeilisearchConnectionFoundException
*/
public function getAllMeilisearchConnectionConfigurations(): array {
$configs = [];
foreach ($this->getAvailableLanguageIds() as $languageId) {
try {
$configs[$languageId] = $this->getMeilisearchConnectionConfiguration($languageId);
} catch (NoMeilisearchConnectionFoundException $e) {}
}
return $configs;
}
public function isEnabled(): bool
{
return !empty($this->getAllMeilisearchConnectionConfigurations());
return !empty($this->getMeilisearchConnectionConfiguration());
}
/**
* @param int $languageId
* @return array
*/
abstract function getMeilisearchConnectionConfiguration(int $language = 0): array;
abstract function getMeilisearchConnectionConfiguration(): array;
}

View File

@ -110,19 +110,12 @@ interface SiteInterface
*/
public function getTitle();
/**
* @param int $language
* @return array
* @throws NoMeilisearchConnectionFoundException
*/
public function getMeilisearchConnectionConfiguration(int $language = 0): array;
/**
* @return array
* @throws NoMeilisearchConnectionFoundException
*/
public function getAllMeilisearchConnectionConfigurations(): array;
public function getMeilisearchConnectionConfiguration(): array;
public function isEnabled(): bool;
}

View File

@ -85,7 +85,7 @@ class SiteRepository
* @param TwoLevelCache|null $twoLevelCache
* @param Registry|null $registry
* @param SiteFinder|null $siteFinder
* @param ExtensionConfiguration| null
* @param ExtensionConfiguration|null
*/
public function __construct(
RootPageResolver $rootPageResolver = null,
@ -230,8 +230,7 @@ class SiteRepository
{
/** @var $siteHashService SiteHashService */
$siteHashService = GeneralUtility::makeInstance(SiteHashService::class);
$siteHash = $siteHashService->getSiteHashForDomain($domain);
return $siteHash;
return $siteHashService->getSiteHashForDomain($domain);
}
/**
@ -272,37 +271,21 @@ class SiteRepository
$domain = $typo3Site->getBase()->getHost();
$siteHash = $this->getSiteHashForDomain($domain);
$defaultLanguage = $typo3Site->getDefaultLanguage()->getLanguageId();
$pageRepository = GeneralUtility::makeInstance(PagesRepository::class);
$availableLanguageIds = array_map(function($language) {
return $language->getLanguageId();
}, $typo3Site->getLanguages());
$meilisearchConnectionConfigurations = [];
$meilisearchConnectionConfiguration = [];
foreach ($availableLanguageIds as $languageUid) {
$meilisearchEnabled = SiteUtility::getConnectionProperty($typo3Site, 'enabled', $languageUid, 'read', true);
if ($meilisearchEnabled) {
$meilisearchConnectionConfigurations[$languageUid] = [
'connectionKey' => $rootPageRecord['uid'] . '|' . $languageUid,
'rootPageTitle' => $rootPageRecord['title'],
'rootPageUid' => $rootPageRecord['uid'],
'read' => [
'scheme' => SiteUtility::getConnectionProperty($typo3Site, 'scheme', $languageUid, 'read', 'http'),
'host' => SiteUtility::getConnectionProperty($typo3Site, 'host', $languageUid, 'read', 'localhost'),
'port' => (int)SiteUtility::getConnectionProperty($typo3Site, 'port', $languageUid, 'read', 7700),
'apiKey' => SiteUtility::getConnectionProperty($typo3Site, 'apiKey', $languageUid, 'read', ''),
],
'write' => [
'scheme' => SiteUtility::getConnectionProperty($typo3Site, 'scheme', $languageUid, 'write', 'http'),
'host' => SiteUtility::getConnectionProperty($typo3Site, 'host', $languageUid, 'write', 'localhost'),
'port' => (int)SiteUtility::getConnectionProperty($typo3Site, 'port', $languageUid, 'write', 7700),
'apiKey' => SiteUtility::getConnectionProperty($typo3Site, 'apiKey', $languageUid, 'write', ''),
],
'language' => $languageUid
];
}
$meilisearchEnabled = SiteUtility::getConnectionProperty($typo3Site, 'enabled', true);
if ($meilisearchEnabled) {
$meilisearchConnectionConfiguration = [
'connectionKey' => $rootPageRecord['uid'] . '|',
'rootPageTitle' => $rootPageRecord['title'],
'rootPageUid' => $rootPageRecord['uid'],
'scheme' => SiteUtility::getConnectionProperty($typo3Site, 'scheme', 'http'),
'host' => SiteUtility::getConnectionProperty($typo3Site, 'host', 'localhost'),
'port' => (int)SiteUtility::getConnectionProperty($typo3Site, 'port',7700),
'apiKey' => SiteUtility::getConnectionProperty($typo3Site, 'apiKey', ''),
];
}
return GeneralUtility::makeInstance(
@ -318,11 +301,7 @@ class SiteRepository
/** @scrutinizer ignore-type */
$pageRepository,
/** @scrutinizer ignore-type */
$defaultLanguage,
/** @scrutinizer ignore-type */
$availableLanguageIds,
/** @scrutinizer ignore-type */
$meilisearchConnectionConfigurations,
$meilisearchConnectionConfiguration,
/** @scrutinizer ignore-type */
$typo3Site
);

View File

@ -47,32 +47,29 @@ class Typo3ManagedSite extends Site
/**
* @var array
*/
protected $meilisearchConnectionConfigurations;
protected $meilisearchConnectionConfiguration;
public function __construct(
TypoScriptConfiguration $configuration,
array $page, $domain, $siteHash, PagesRepository $pagesRepository = null, $defaultLanguageId = 0, $availableLanguageIds = [], array $meilisearchConnectionConfigurations = [], Typo3Site $typo3SiteObject = null)
array $page, $domain, $siteHash, PagesRepository $pagesRepository = null, array $meilisearchConnectionConfiguration = [], Typo3Site $typo3SiteObject = null)
{
$this->configuration = $configuration;
$this->rootPage = $page;
$this->domain = $domain;
$this->siteHash = $siteHash;
$this->pagesRepository = $pagesRepository ?? GeneralUtility::makeInstance(PagesRepository::class);
$this->defaultLanguageId = $defaultLanguageId;
$this->availableLanguageIds = $availableLanguageIds;
$this->meilisearchConnectionConfigurations = $meilisearchConnectionConfigurations;
$this->meilisearchConnectionConfiguration = $meilisearchConnectionConfiguration;
$this->typo3SiteObject = $typo3SiteObject;
}
/**
* @param int $language
* @return array
* @throws NoMeilisearchConnectionFoundException
*/
public function getMeilisearchConnectionConfiguration(int $language = 0): array
public function getMeilisearchConnectionConfiguration(): array
{
if (!is_array($this->meilisearchConnectionConfigurations[$language])) {
if (!is_array($this->meilisearchConnectionConfiguration)) {
/* @var $noMeilisearchConnectionException NoMeilisearchConnectionFoundException */
$noMeilisearchConnectionException = GeneralUtility::makeInstance(
NoMeilisearchConnectionFoundException::class,
@ -80,12 +77,11 @@ class Typo3ManagedSite extends Site
/** @scrutinizer ignore-type */ 1552491117
);
$noMeilisearchConnectionException->setRootPageId($this->getRootPageId());
$noMeilisearchConnectionException->setLanguageId($language);
throw $noMeilisearchConnectionException;
}
return $this->meilisearchConnectionConfigurations[$language];
return $this->meilisearchConnectionConfiguration;
}
/**
@ -97,4 +93,5 @@ class Typo3ManagedSite extends Site
{
return $this->typo3SiteObject;
}
}