first commit

This commit is contained in:
Sven Wappler
2021-04-17 00:26:33 +02:00
commit 866c63cc63
813 changed files with 100696 additions and 0 deletions

View File

@@ -0,0 +1,156 @@
<?php
namespace WapplerSystems\Meilisearch\Domain\Search\LastSearches;
/***************************************************************
* Copyright notice
*
* (c) 2010-2017 dkd Internet Service GmbH <solr-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\System\Records\AbstractRepository;
class LastSearchesRepository extends AbstractRepository
{
/**
* @var string
*/
protected $table = 'tx_meilisearch_last_searches';
/**
* Finds the last searched keywords from the database
*
* @param int $limit
* @return array An array containing the last searches of the current user
*/
public function findAllKeywords($limit = 10) : array
{
$lastSearchesResultSet = $this->getLastSearchesResultSet($limit);
if (empty($lastSearchesResultSet)) {
return [];
}
$lastSearches = [];
foreach ($lastSearchesResultSet as $row) {
$lastSearches[] = html_entity_decode($row['keywords'], ENT_QUOTES, 'UTF-8');
}
return $lastSearches;
}
/**
* Returns all last searches
*
* @param $limit
* @return array
*/
protected function getLastSearchesResultSet($limit) : array
{
$queryBuilder = $this->getQueryBuilder();
return $queryBuilder
->select('keywords')
->addSelectLiteral(
$queryBuilder->expr()->max('tstamp','maxtstamp')
)
->from($this->table)
// There is no support for DISTINCT, a ->groupBy() has to be used instead.
->groupBy('keywords')
->orderBy('maxtstamp', 'DESC')
->setMaxResults($limit)->execute()->fetchAll();
}
/**
* Adds keywords to last searches or updates the oldest row by given limit.
*
* @param string $lastSearchesKeywords
* @param int $lastSearchesLimit
* @return void
*/
public function add(string $lastSearchesKeywords, int $lastSearchesLimit)
{
$nextSequenceId = $this->resolveNextSequenceIdForGivenLimit($lastSearchesLimit);
$rowsCount = $this->count();
if ($nextSequenceId < $rowsCount) {
$this->update([
'sequence_id' => $nextSequenceId,
'keywords' => $lastSearchesKeywords
]);
return;
}
$queryBuilder = $this->getQueryBuilder();
$queryBuilder
->insert($this->table)
->values([
'sequence_id' => $nextSequenceId,
'keywords' => $lastSearchesKeywords,
'tstamp' => time()
])
->execute();
}
/**
* Resolves next sequence id by given last searches limit.
*
* @param int $lastSearchesLimit
* @return int
*/
protected function resolveNextSequenceIdForGivenLimit(int $lastSearchesLimit) : int
{
$nextSequenceId = 0;
$queryBuilder = $this->getQueryBuilder();
$result = $queryBuilder->select('sequence_id')
->from($this->table)
->orderBy('tstamp', 'DESC')
->setMaxResults(1)
->execute()->fetch();
if (!empty($result)) {
$nextSequenceId = ($result['sequence_id'] + 1) % $lastSearchesLimit;
}
return $nextSequenceId;
}
/**
* Updates last searches row by using sequence_id from given $lastSearchesRow array
*
* @param array $lastSearchesRow
* @return void
* @throws \InvalidArgumentException
*/
protected function update(array $lastSearchesRow)
{
$queryBuilder = $this->getQueryBuilder();
$affectedRows = $queryBuilder
->update($this->table)
->where(
$queryBuilder->expr()->eq('sequence_id', $queryBuilder->createNamedParameter($lastSearchesRow['sequence_id']))
)
->set('tstamp', time())
->set('keywords', $lastSearchesRow['keywords'])
->execute();
if ($affectedRows < 1) {
throw new \InvalidArgumentException(vsprintf('By trying to update last searches row with values "%s" nothing was updated, make sure the given "sequence_id" exists in database.', [\json_encode($lastSearchesRow)]), 1502717923);
}
}
}

View File

@@ -0,0 +1,148 @@
<?php
namespace WapplerSystems\Meilisearch\Domain\Search\LastSearches;
/***************************************************************
* Copyright notice
*
* (c) 2015-2016 Timo Schmidt <timo.schmidt@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\System\Configuration\TypoScriptConfiguration;
use WapplerSystems\Meilisearch\System\Session\FrontendUserSession;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* The LastSearchesService is responsible to return the LastSearches from the session or database,
* depending on the configuration.
*
* @author Timo Schmidt <timo.schmidt@dkd.de>
*/
class LastSearchesService
{
/**
* @var TypoScriptConfiguration
*/
protected $configuration;
/**
* @var FrontendUserSession
*/
protected $session;
/**
* @var LastSearchesRepository
*/
protected $lastSearchesRepository;
/**
* @param TypoScriptConfiguration $typoscriptConfiguration
* @param FrontendUserSession|null $session
* @param LastSearchesRepository|null $lastSearchesRepository
*/
public function __construct(TypoScriptConfiguration $typoscriptConfiguration, FrontendUserSession $session = null, LastSearchesRepository $lastSearchesRepository = null)
{
$this->configuration = $typoscriptConfiguration;
$this->session = $session ?? GeneralUtility::makeInstance(FrontendUserSession::class);
$this->lastSearchesRepository = $lastSearchesRepository ?? GeneralUtility::makeInstance(LastSearchesRepository::class);
}
/**
* Retrieves the last searches from the session or database depending on the configuration.
*
* @return array
*/
public function getLastSearches()
{
$lastSearchesKeywords = [];
$mode = $this->configuration->getSearchLastSearchesMode();
$limit = $this->configuration->getSearchLastSearchesLimit();
switch ($mode) {
case 'user':
$lastSearchesKeywords = $this->getLastSearchesFromSession($limit);
break;
case 'global':
$lastSearchesKeywords = $this->lastSearchesRepository->findAllKeywords($limit);
break;
}
return $lastSearchesKeywords;
}
/**
* Saves the keywords to the last searches in the database or session depending on the configuration.
*
* @param string $keywords
* @throws \UnexpectedValueException
*/
public function addToLastSearches($keywords)
{
$mode = $this->configuration->getSearchLastSearchesMode();
switch ($mode) {
case 'user':
$this->storeKeywordsToSession($keywords);
break;
case 'global':
$this->lastSearchesRepository->add($keywords, (int)$this->configuration->getSearchLastSearchesLimit());
break;
default:
throw new \UnexpectedValueException(
'Unknown mode for plugin.tx_meilisearch.search.lastSearches.mode, valid modes are "user" or "global".',
1342456570
);
}
}
/**
* Gets the last searched keywords from the user's session
*
* @param int $limit
* @return array An array containing the last searches of the current user
*/
protected function getLastSearchesFromSession($limit)
{
$lastSearches = $this->session->getLastSearches();
$lastSearches = array_slice(array_reverse(array_unique($lastSearches)), 0, $limit);
return $lastSearches;
}
/**
* Stores the keywords from the current query to the user's session.
*
* @param string $keywords The current query's keywords
* @return void
*/
protected function storeKeywordsToSession($keywords)
{
$currentLastSearches = $this->session->getLastSearches();
$lastSearches = $currentLastSearches;
$newLastSearchesCount = array_push($lastSearches, $keywords);
while ($newLastSearchesCount > $this->configuration->getSearchLastSearchesLimit()) {
array_shift($lastSearches);
$newLastSearchesCount = count($lastSearches);
}
$this->session->setLastSearches($lastSearches);
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace WapplerSystems\Meilisearch\Domain\Search\LastSearches;
/***************************************************************
* Copyright notice
*
* (c) 2017 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\Domain\Search\LastSearches\LastSearchesService;
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\SearchResultSet;
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\SearchResultSetProcessor;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Writes the last searches
*
* @author Timo Hund <timo.hund@dkd.de>
*/
class LastSearchesWriterProcessor implements SearchResultSetProcessor
{
/**
* @param SearchResultSet $resultSet
* @return SearchResultSet
*/
public function process(SearchResultSet $resultSet) {
if ($resultSet->getAllResultCount() === 0) {
// when the search does not produce a result we do not store the last searches
return $resultSet;
}
if (!isset($GLOBALS['TSFE'])) {
return $resultSet;
}
$query = $resultSet->getUsedSearchRequest()->getRawUserQuery();
if (is_string($query)) {
$lastSearchesService = $this->getLastSearchesService($resultSet);
$lastSearchesService->addToLastSearches($query);
}
return $resultSet;
}
/**
* @param SearchResultSet $resultSet
* @return LastSearchesService
*/
protected function getLastSearchesService(SearchResultSet $resultSet) {
return GeneralUtility::makeInstance(LastSearchesService::class,
/** @scrutinizer ignore-type */ $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration());
}
}