Skip to content

Commit

Permalink
Merge pull request #217 from tpwd/feature/custom-table-indexer
Browse files Browse the repository at this point in the history
Feature: Custom table indexer, eg. for mask and bootstrap_package
  • Loading branch information
christianbltr authored Feb 23, 2024
2 parents c27dcc0 + 5fb1491 commit 1bef800
Show file tree
Hide file tree
Showing 18 changed files with 583 additions and 148 deletions.
85 changes: 64 additions & 21 deletions Classes/Domain/Repository/GenericRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use PDO;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\EndTimeRestriction;
use TYPO3\CMS\Core\Database\Query\Restriction\HiddenRestriction;
use TYPO3\CMS\Core\Database\Query\Restriction\StartTimeRestriction;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/***************************************************************
Expand Down Expand Up @@ -47,44 +50,37 @@ public function findByUidAndType($uid, string $type)
}

$row = false;
$tableName = '';
$table = '';
$type = (substr($type, 0, 4) == 'file') ? 'file' : $type;
switch ($type) {
case 'page':
$tableName = 'pages';
$table = 'pages';
break;
case 'news':
$tableName = 'tx_news_domain_model_news';
$table = 'tx_news_domain_model_news';
break;
case 'file':
$tableName = 'sys_file';
$table = 'sys_file';
break;
default:
// check if a table exists that matches the type name
$tableNameToCheck = strip_tags(htmlentities($type));
/** @var ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$connection = $connectionPool->getConnectionForTable($tableNameToCheck);
$statement = $connection->prepare('SHOW TABLES LIKE "' . $tableNameToCheck . '"');
$result = $statement->executeQuery();
if ($result->rowCount()) {
$tableName = $tableNameToCheck;
if ($this->tableExists($tableNameToCheck)) {
$table = $tableNameToCheck;
}
}
// hook to add a custom types
if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['ke_search']['GenericRepositoryTablename'] ?? null)) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['ke_search']['GenericRepositoryTablename'] as $_classRef) {
$_procObj = GeneralUtility::makeInstance($_classRef);
$tableName = $_procObj->getTableName($type);
$table = $_procObj->getTableName($type);
}
}
if (!empty($tableName)) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($tableName);
$row = $queryBuilder
if (!empty($table)) {
$queryBuilder = $this->getQueryBuilder($table);
$row = $queryBuilder
->select('*')
->from($tableName)
->from($table)
->where(
$queryBuilder->expr()->eq(
'uid',
Expand All @@ -110,9 +106,7 @@ public function findLangaugeOverlayByUidAndLanguage(string $table, int $uid, int
$languageField = $GLOBALS['TCA'][$table]['ctrl']['languageField'] ?? null;

if (!empty($transOrigPointerField) && !empty($languageField)) {
/** @var ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable($table);
$queryBuilder = $this->getQueryBuilder($table);
$overlayRecord = $queryBuilder
->select('*')
->from($table)
Expand All @@ -131,4 +125,53 @@ public function findLangaugeOverlayByUidAndLanguage(string $table, int $uid, int
}
return $overlayRecord;
}

public function getQueryBuilder(string $table, bool $includeHiddenAndTimeRestricted = false): QueryBuilder
{
/** @var ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable($table);
if ($includeHiddenAndTimeRestricted) {
$queryBuilder
->getRestrictions()
->removeByType(HiddenRestriction::class)
->removeByType(StartTimeRestriction::class)
->removeByType(EndTimeRestriction::class);
}
return $queryBuilder;
}

public function findByReferenceField(
string $table,
string $fieldName,
int $value,
bool $includeHiddenAndTimeRestricted = false
) {
$queryBuilder = $this->getQueryBuilder($table, $includeHiddenAndTimeRestricted);
return $queryBuilder
->select('*')
->from($table)
->where(
$queryBuilder->expr()->eq(
$fieldName,
$queryBuilder->createNamedParameter($value, PDO::PARAM_INT)
)
)
->executeQuery()
->fetchAllAssociative();
}

public function tableExists(string $table): bool
{
$table = strip_tags(htmlentities($table));
/** @var ConnectionPool $connectionPool */
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$connection = $connectionPool->getConnectionForTable($table);
$statement = $connection->prepare('SHOW TABLES LIKE "' . $table . '"');
$result = $statement->executeQuery();
if ($result->rowCount()) {
return true;
}
return false;
}
}
4 changes: 2 additions & 2 deletions Classes/Indexer/IndexerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Tpwd\KeSearch\Indexer\Types\File;
use Tpwd\KeSearch\Lib\Db;
use Tpwd\KeSearch\Lib\SearchHelper;
use Tpwd\KeSearch\Service\FileService;
use Tpwd\KeSearch\Utility\FileUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Database\Query\QueryHelper;
Expand Down Expand Up @@ -477,7 +477,7 @@ protected function getFilesToIndex($table, $fieldname, $uid, $language): array
$fileReference = $fileRepository->findFileReferenceByUid($relatedFile['uid']);
$file = $fileReference->getOriginalFile();
if ($file instanceof \TYPO3\CMS\Core\Resource\File
&& FileService::isFileIndexable($file, $this->indexerConfig)) {
&& FileUtility::isFileIndexable($file, $this->indexerConfig)) {
$filesToIndex[] = $fileReference;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Classes/Indexer/Types/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
use Tpwd\KeSearch\Indexer\IndexerRunner;
use Tpwd\KeSearch\Lib\Fileinfo;
use Tpwd\KeSearch\Lib\SearchHelper;
use Tpwd\KeSearch\Service\FileService;
use Tpwd\KeSearch\Utility\FileUtility;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Resource\Folder;
use TYPO3\CMS\Core\Resource\Index\MetaDataRepository;
Expand Down Expand Up @@ -179,7 +179,7 @@ public function getFilesFromFal(array &$files, array $directoryArray)
if (count($filesInFolder)) {
foreach ($filesInFolder as $file) {
if ($file instanceof \TYPO3\CMS\Core\Resource\File
&& FileService::isFileIndexable($file, $this->indexerConfig)) {
&& FileUtility::isFileIndexable($file, $this->indexerConfig)) {
$files[] = $file;
}
}
Expand Down
7 changes: 3 additions & 4 deletions Classes/Indexer/Types/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Tpwd\KeSearch\Indexer\IndexerRunner;
use Tpwd\KeSearch\Lib\Db;
use Tpwd\KeSearch\Lib\SearchHelper;
use Tpwd\KeSearch\Utility\ContentUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
Expand Down Expand Up @@ -532,18 +533,16 @@ public function getContentFromContentElements($contentElements)
// NOTE: There's no restriction to certain content element types .
// All attached content elements will be indexed. Only fields "header" and "bodytext" will be indexed.
// todo: As in the page indexer it should be possible to define which fields should be indexed from the tt_content row.
// todo: index files linked in content elements
if (count($contentElements)) {
/* @var $pageIndexerObject Page */
$pageIndexerObject = GeneralUtility::makeInstance(Page::class, $this->pObj);

foreach ($contentElements as $contentElement) {
// index header, add header only if not set to "hidden"
if ($contentElement['header_layout'] != 100) {
$content .= "\n" . strip_tags($contentElement['header']) . "\n";
}

// index bodytext (main content)
$content .= "\n" . $pageIndexerObject->getContentFromContentElement($contentElement);
$content .= "\n" . ContentUtility::getPlainContentFromContentRow($contentElement, 'bodytext');
}
}

Expand Down
Loading

0 comments on commit 1bef800

Please sign in to comment.