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,130 @@
<?php
namespace WapplerSystems\Meilisearch\System\Records;
/***************************************************************
* 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 TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Repository class to encapsulate the database access for records used in solr.
*
* @author Timo Hund <timo.hund@dkd.de>
*/
abstract class AbstractRepository
{
/**
* @var string
*/
protected $table = '';
/**
* Retrieves a single row from the database by a given uid
*
* @param string $fields
* @param string $uid
* @return mixed
*/
protected function getOneRowByUid($fields, $uid)
{
$queryBuilder = $this->getQueryBuilder();
return $queryBuilder
->select($fields)
->from($this->table)
->where($queryBuilder->expr()->eq('uid', intval($uid)))
->execute()->fetch();
}
/**
* Returns QueryBuilder for Doctrine DBAL
*
* @return QueryBuilder
*/
protected function getQueryBuilder()
{
/** @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
return $queryBuilder;
}
/**
* Returns current count of last searches
*
* @return int
*/
public function count() : int
{
return (int)$this->getQueryBuilder()
->count('*')
->from($this->table)
->execute()->fetchColumn(0);
}
/**
* Returns connection for all in transaction involved tables.
*
* Note: Rollback will not work in case of different connections.
*
* @param string[] ...$tableNames
* @return Connection
*/
public function getConnectionForAllInTransactionInvolvedTables(string ...$tableNames) : Connection
{
if (empty($tableNames) || count($tableNames) < 2) {
throw new \InvalidArgumentException(__METHOD__ . ' requires at least 2 table names.', 1504801512);
}
if (!$this->isConnectionForAllTablesTheSame(...$tableNames)) {
throw new \RuntimeException(
vsprintf('The tables "%s" using different database connections. Transaction needs same database connection ' .
'for all tables, please reconfigure the database settings for involved tables properly.', [implode('", "', $tableNames)]
), 1504866142
);
}
return GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable(array_shift($tableNames));
}
/**
* Checks whether all table involved in transaction using same connection.
*
* @param string[] ...$tableNames
* @return bool
*/
protected function isConnectionForAllTablesTheSame(string ...$tableNames) : bool
{
/** @var ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$connection = $connectionPool->getConnectionForTable(array_shift($tableNames));
foreach ($tableNames as $tableName) {
$connectionForTable = $connectionPool->getConnectionForTable($tableName);
if ($connection !== $connectionForTable) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,296 @@
<?php
namespace WapplerSystems\Meilisearch\System\Records\Pages;
/***************************************************************
* Copyright notice
*
* (c) 2010-2017 dkd Internet Service GmbH <solr-eb-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\Cache\TwoLevelCache;
use WapplerSystems\Meilisearch\System\Records\AbstractRepository;
use WapplerSystems\Meilisearch\Util;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* PagesRepository to encapsulate the database access.
*/
class PagesRepository extends AbstractRepository
{
/**
* @var string
*/
protected $table = 'pages';
/**
* @var TwoLevelCache
*/
protected $transientVariableCache;
/**
* PagesRepository constructor.
*
* @param TwoLevelCache|null $transientVariableCache
*/
public function __construct(TwoLevelCache $transientVariableCache = null)
{
$this->transientVariableCache = $transientVariableCache ?? GeneralUtility::makeInstance(TwoLevelCache::class, /** @scrutinizer ignore-type */ 'cache_runtime');
}
/**
* Gets the site's root pages. The "Is root of website" flag must be set,
* which usually is the case for pages with pid = 0.
*
* @return array An array of (partial) root page records, containing the uid and title fields
*/
public function findAllRootPages()
{
$queryBuilder = $this->getQueryBuilder();
$queryBuilder
->select('uid', 'title')
->from($this->table)
->where(
$queryBuilder->expr()->neq('pid', -1),
$queryBuilder->expr()->eq('is_siteroot', 1)
);
$this->addDefaultLanguageUidConstraint($queryBuilder);
return $queryBuilder->execute()->fetchAll();
}
/**
* Finds the MountPointProperties array for mount points(destinations) by mounted page UID(source) or by the rootline array of mounted page.
*
* @param int $mountedPageUid
* @param array $rootLineParentPageIds
* @return array
*/
public function findMountPointPropertiesByPageIdOrByRootLineParentPageIds(int $mountedPageUid, array $rootLineParentPageIds = []) : array
{
if (array_filter($rootLineParentPageIds, 'is_int') !== $rootLineParentPageIds) {
throw new \InvalidArgumentException('Given $rootLineParentPageIds array is not valid. Allowed only the arrays with the root line page UIDs as integers.', 1502459711);
}
$queryBuilder = $this->getQueryBuilder();
$queryBuilder->select('uid', 'uid AS mountPageDestination', 'mount_pid AS mountPageSource', 'mount_pid_ol AS mountPageOverlayed')->from($this->table);
$queryBuilder = $this->addWhereClauseForMountpointDestinationProperties($queryBuilder, $mountedPageUid, $rootLineParentPageIds);
$result = $queryBuilder->execute()->fetchAll();
return $result;
}
/**
* This methods builds the where clause for the mountpoint destinations. It retrieves all records where the mount_pid = $mountedPageUid or the mount_pid is
* in the rootLineParentPageIds.
*
* @param QueryBuilder $queryBuilder
* @param int $mountedPageUid
* @param array $rootLineParentPageIds
* @return QueryBuilder
*/
protected function addWhereClauseForMountpointDestinationProperties(QueryBuilder $queryBuilder, $mountedPageUid, array $rootLineParentPageIds) : QueryBuilder
{
if (empty($rootLineParentPageIds)) {
$queryBuilder->andWhere(
$queryBuilder->expr()->eq('doktype', 7),
$queryBuilder->expr()->eq('no_search', 0),
$queryBuilder->expr()->eq('mount_pid', $mountedPageUid),
$queryBuilder->expr()->eq('mount_pid_ol', 1)
);
} else {
$queryBuilder->andWhere(
$queryBuilder->expr()->eq('doktype', 7),
$queryBuilder->expr()->eq('no_search', 0),
$queryBuilder->expr()->orX(
$queryBuilder->expr()->andX(
$queryBuilder->expr()->eq('mount_pid', $mountedPageUid),
$queryBuilder->expr()->eq('mount_pid_ol', 1)
),
$queryBuilder->expr()->in('mount_pid', $rootLineParentPageIds)
)
);
}
$this->addDefaultLanguageUidConstraint($queryBuilder);
return $queryBuilder;
}
/**
* Generates a list of page IDs in this site.
* Attention: Includes all page types except Deleted pages!
*
* @param int $rootPageId Page ID from where to start collection sub pages
* @param int $maxDepth Maximum depth to descend into the site tree
* @param string $initialPagesAdditionalWhereClause
* @return array Array of pages (IDs) in this site
*/
public function findAllSubPageIdsByRootPage(int $rootPageId, int $maxDepth = 999, string $initialPagesAdditionalWhereClause = '') : array
{
$pageIds = [];
$recursionRootPageId = $rootPageId;
// when we have a cached value, we can return it.
$cacheIdentifier = sha1('getPages' . (string)$rootPageId);
if ($this->transientVariableCache->get($cacheIdentifier) !== false) {
return $this->transientVariableCache->get($cacheIdentifier);
}
if ($maxDepth <= 0) {
// exiting the recursion loop, may write to cache now
$this->transientVariableCache->set($cacheIdentifier, $pageIds);
return $pageIds;
}
// get the page ids of the current level and if needed call getPages recursive
$pageIds = $this->getPageIdsFromCurrentDepthAndCallRecursive($maxDepth, $recursionRootPageId, $pageIds, $initialPagesAdditionalWhereClause);
// exiting the recursion loop, may write to cache now
$this->transientVariableCache->set($cacheIdentifier, $pageIds);
return $pageIds;
}
/**
* This method retrieves the pages ids from the current tree level an calls getPages recursive,
* when the maxDepth has not been reached.
*
* @param int $maxDepth
* @param int $recursionRootPageId
* @param array $pageIds
* @param string $initialPagesAdditionalWhereClause
* @return array
*/
protected function getPageIdsFromCurrentDepthAndCallRecursive(int $maxDepth, int $recursionRootPageId, array $pageIds, string $initialPagesAdditionalWhereClause = '')
{
$queryBuilder = $this->getQueryBuilder();
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$queryBuilder
->select('uid')
->from($this->table)
->where(
$queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($recursionRootPageId, \PDO::PARAM_INT))
);
$this->addDefaultLanguageUidConstraint($queryBuilder);
if (!empty($initialPagesAdditionalWhereClause)) {
$queryBuilder->andWhere($initialPagesAdditionalWhereClause);
}
$resultSet = $queryBuilder->execute();
while ($page = $resultSet->fetch()) {
$pageIds[] = $page['uid'];
if ($maxDepth > 1) {
$pageIds = array_merge($pageIds, $this->findAllSubPageIdsByRootPage($page['uid'], $maxDepth - 1));
}
}
return $pageIds;
}
/**
* Finds translation overlays by given page Id.
*
* @param int $pageId
* @return array
*/
public function findTranslationOverlaysByPageId(int $pageId) : array
{
$queryBuilder = $this->getQueryBuilder();
$queryBuilder->getRestrictions()->removeAll();
$queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
return $queryBuilder
->select('pid', 'l10n_parent', 'sys_language_uid')
->from('pages')
->add('where',
$queryBuilder->expr()->eq('l10n_parent', $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT))
. BackendUtility::BEenableFields('pages')
)->execute()->fetchAll();
}
/**
* Finds Pages, which are showing content from the page currently being updated.
*
* @param int $pageId UID of the page currently being updated
* @return array with page Uids from pages, which are showing contents from given Page Id
*/
public function findPageUidsWithContentsFromPid(int $pageId) : array
{
$queryBuilder = $this->getQueryBuilder();
$queryBuilder->getRestrictions()->removeAll();
$queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$queryBuilder
->select('uid')
->from($this->table)
->add('where',
$queryBuilder->expr()->eq('content_from_pid', $queryBuilder->createNamedParameter($pageId, \PDO::PARAM_INT))
);
$this->addDefaultLanguageUidConstraint($queryBuilder);
return $queryBuilder->execute()->fetchAll();
}
/**
* Finds all pages by given where clause
*
* @param string $whereClause
* @return array
*/
public function findAllMountPagesByWhereClause(string $whereClause) : array
{
$queryBuilder = $this->getQueryBuilder();
$queryBuilder->getRestrictions()->removeAll();
$queryBuilder
->select(
'uid',
'mount_pid AS mountPageSource',
'uid AS mountPageDestination',
'mount_pid_ol AS mountPageOverlayed')
->from($this->table)
->add('where', $whereClause);
$this->addDefaultLanguageUidConstraint($queryBuilder);
return $queryBuilder->execute()->fetchAll();
}
/**
* Limits the pages to the sys_language_uid = 0 (default language)
*
* @param $queryBuilder
*/
protected function addDefaultLanguageUidConstraint($queryBuilder)
{
$queryBuilder->andWhere($queryBuilder->expr()->eq('sys_language_uid', 0));
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace WapplerSystems\Meilisearch\System\Records\SystemCategory;
/***************************************************************
* 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\System\Records\AbstractRepository;
/**
* Repository class for sys_category items of the TYPO3 system.
*
* @author Timo Hund <timo.hund@dkd.de>
*/
class SystemCategoryRepository extends AbstractRepository
{
/**
* @var string
*/
protected $table = 'sys_category';
/**
* @param int $uid
* @param string $limitFields
* @return array
*/
public function findOneByUid($uid = 0, $limitFields = '*')
{
return $this->getOneRowByUid($limitFields, $uid);
}
}

View File

@@ -0,0 +1,91 @@
<?php
namespace WapplerSystems\Meilisearch\System\Records\SystemLanguage;
/***************************************************************
* Copyright notice
*
* (c) 2010-2017 dkd Internet Service GmbH <solr-eb-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;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* SystemLanguageRepository to encapsulate the database access for records used in solr.
*
*/
class SystemLanguageRepository extends AbstractRepository implements SingletonInterface
{
/**
* @var string
*/
protected $table = 'sys_language';
/**
* Finds the language name for a given language ID.
*
* @param int $languageId language ID
* @return string Language name
*/
public function findOneLanguageTitleByLanguageId(int $languageId) : string
{
$queryBuilder = $this->getQueryBuilder();
$result = $queryBuilder->select('title')
->from($this->table)
->where($queryBuilder->expr()->eq('uid', $languageId))
->execute()->fetch();
if ($result == false && $languageId == 0) {
return 'default';
}
return isset($result['title']) ? $result['title'] : '';
}
/**
* Finds the system's configured languages.
*
* @return array An array of language UIDs
*/
public function findSystemLanguages()
{
$languages = [0];
$queryBuilder = $this->getQueryBuilder();
$queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(HiddenRestriction::class));
$languageRecords = $queryBuilder->select('uid')
->from($this->table)
->execute()->fetchAll();
if ($languageRecords == false) {
return $languages;
}
foreach ($languageRecords as $languageRecord) {
$languages[] = $languageRecord['uid'];
}
return $languages;
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace WapplerSystems\Meilisearch\System\Records\SystemTemplate;
/***************************************************************
* Copyright notice
*
* (c) 2010-2017 dkd Internet Service GmbH <solr-eb-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;
/**
* SystemTemplateRepository to encapsulate the database access for records used in solr.
*
*/
class SystemTemplateRepository extends AbstractRepository
{
/**
* @var string
*/
protected $table = 'sys_template';
/**
* Finds a first closest page id with active template.
*
* This method expects one startPageId, which must be inside the root line and does not check if it is one in the root line.
*
* @param array $rootLine
* @return int
*/
public function findOneClosestPageIdWithActiveTemplateByRootLine(array $rootLine)
{
$rootLinePageIds = [0];
foreach ($rootLine as $rootLineItem) {
$rootLinePageIds[] = (int)$rootLineItem['uid'];
}
$queryBuilder = $this->getQueryBuilder();
$result = $queryBuilder
->select('uid', 'pid')
->from($this->table)
->where($queryBuilder->expr()->in('pid', $rootLinePageIds))
->execute()->fetch();
return isset($result['pid']) ? $result['pid'] : 0;
}
}