Skip to content

Commit

Permalink
[TASK] Rewrite legacy database call
Browse files Browse the repository at this point in the history
The db call is rewritten from $GLOBALS['TYPO3_DB'] to QueryBuilder so we can get rid of the friendsoftypo3/typo3db-legacy package
  • Loading branch information
smichaelsen committed Dec 16, 2020
1 parent db7c398 commit 7376734
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 56 deletions.
86 changes: 32 additions & 54 deletions Classes/ContentObject/FolderContentObject.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
<?php

declare(strict_types=1);

namespace Smichaelsen\FolderCobj\ContentObject;

use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\AbstractContentObject;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Typo3DbLegacy\Database\DatabaseConnection;

class FolderContentObject extends AbstractContentObject
{

/**
* Renders the content object.
*
* @param array $conf
* @return string
*/
public function render($conf = [])
public function render($conf = []): string
{
$content = '';
$cObj = GeneralUtility::makeInstance(ContentObjectRenderer::class);
Expand All @@ -34,66 +31,58 @@ public function render($conf = [])
return $content;
}

/**
* @param array $conf
* @return \Generator
*/
protected function loadFolders(array $conf)
protected function loadFolders(array $conf): \Generator
{
$pageRepository = GeneralUtility::makeInstance(PageRepository::class);
$conf = $this->resolveStdWrapProperties(
$conf,
[
'containsModule',
'recursive',
'restrictToRootPage',
'doktypes',
'limit'
'limit',
]
);
if (!empty($conf['doktypes'] ?? null)) {
$doktypes = \implode(',', GeneralUtility::intExplode(',', $conf['doktypes']));
$constraints = [
'pages.doktype IN (' . $doktypes . ')',
];
} else {
$constraints = [
'pages.doktype = ' . PageRepository::DOKTYPE_SYSFOLDER,
];

$doktypes = GeneralUtility::intExplode(',', $conf['doktypes'] ?: PageRepository::DOKTYPE_SYSFOLDER);

$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages');

if ($conf['limit']) {
$queryBuilder->setMaxResults((int)$conf['limit']);
}

if ($conf['containsModule'] ?? false) {
$constraints[] = 'pages.module = ' . $this->getDatabaseConnection()->fullQuoteStr($conf['containsModule'], 'pages');
$constraints = [
$queryBuilder->expr()->in('doktype', $queryBuilder->createNamedParameter($doktypes, Connection::PARAM_INT_ARRAY)),
];
if ($conf['containsModule']) {
$constraints[] = $queryBuilder->expr()->eq('module', $queryBuilder->createNamedParameter($conf['containsModule'], \PDO::PARAM_STR));
}
if ($conf['restrictToRootPage'] ?? false) {
$rootPage = (int) $this->cObj->getData('leveluid:0');
if ($conf['restrictToRootPage']) {
$rootPage = (int)$this->cObj->getData('leveluid:0');
$pidList = [$rootPage];
if ((int)$conf['recursive'] > 0) {
$pidList = array_merge(
GeneralUtility::intExplode(',', $this->cObj->getTreeList($rootPage, $conf['recursive'])),
$pidList
);
}
$constraints[] = 'pages.pid IN (' . join(',', $pidList) . ')';
}
$where = join(' AND ', $constraints) . $pageRepository->enableFields('pages');
if (empty($conf['limit'])) {
$limit = '';
} else {
$limit = $conf['limit'];
$constraints[] = $queryBuilder->expr()->in('pid', $queryBuilder->createNamedParameter($pidList, Connection::PARAM_INT_ARRAY));
}
$res = $this->getDatabaseConnection()->exec_SELECTquery('*', 'pages', $where, '', '', $limit);
while ($folderRecord = $this->getDatabaseConnection()->sql_fetch_assoc($res)) {

$result = $queryBuilder->select('*')->from('pages')->where(...$constraints)->execute();
while ($folderRecord = $result->fetchAssociative()) {
yield $folderRecord;
}
}

protected function resolveStdWrapProperties(array $conf, array $propertyNames)
protected function resolveStdWrapProperties(array $conf, array $propertyNames): array
{
foreach ($propertyNames as $propertyName) {
$conf[$propertyName] = trim(isset($conf[$propertyName . '.'])
$conf[$propertyName] = trim(
isset($conf[$propertyName . '.'])
? $this->cObj->stdWrap($conf[$propertyName] ?? null, $conf[$propertyName . '.'])
: $conf[$propertyName] ?? null
: $conf[$propertyName] ?? ''
);
if ($conf[$propertyName] === '') {
unset($conf[$propertyName]);
Expand All @@ -106,18 +95,7 @@ protected function resolveStdWrapProperties(array $conf, array $propertyNames)
return $conf;
}

/**
* @return DatabaseConnection
*/
protected function getDatabaseConnection()
{
return $GLOBALS['TYPO3_DB'];
}

/**
* @return TypoScriptFrontendController
*/
protected function getTyposcriptFrontendController()
protected function getTyposcriptFrontendController(): TypoScriptFrontendController
{
return $GLOBALS['TSFE'];
}
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
}
},
"require": {
"typo3/cms-core": "^9.5 || ^10.4",
"friendsoftypo3/typo3db-legacy": "^1.1"
"typo3/cms-core": "^9.5 || ^10.4"
}
}

0 comments on commit 7376734

Please sign in to comment.