Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Avoid using deprecated classes and methods #1014

Merged
merged 1 commit into from
Jun 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- Always drop empty values for `intExplode` calls (#1000)
- Discard invalid gender values (#996)
- Avoid some version-specific tests (#964, #967)
- Avoid deprecated `settingLanguage` calls (#955)
- Avoid calls to deprecated classes and methods (#955, #1014)
- Build a more complete fake frontend (#954)
- Use proper mocking with 11LTS in the storage-related tests (#951)
- Fix compatibility with newer DBAL versions (#950)
Expand Down
17 changes: 14 additions & 3 deletions Classes/Configuration/AbstractConfigurationCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,21 @@ protected function buildWarningStartWithKeyAndValue(string $key, $value): string
private function getDbColumnNames(string $tableName): array
{
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName);
$statement = $connection->query('SHOW FULL COLUMNS FROM `' . $tableName . '`');
$query = 'SHOW FULL COLUMNS FROM `' . $tableName . '`';
if (\method_exists($connection, 'executeQuery')) {
$statement = $connection->executeQuery($query);
} else {
$statement = $connection->query($query);
}
$columns = [];
foreach ($statement->fetchAll() as $row) {
$columns[] = $row['Field'];
if (\method_exists($statement, 'fetchAllAssociative')) {
foreach ($statement->fetchAllAssociative() as $row) {
$columns[] = $row['Field'];
}
} else {
foreach ($statement->fetchAll() as $row) {
$columns[] = $row['Field'];
}
}

return $columns;
Expand Down
16 changes: 12 additions & 4 deletions Classes/Domain/Repository/PageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,18 @@ private function findDirectSubpages(array $pageUids): array
$subpageUids = [];
$queryResult = $query->execute();
if ($queryResult instanceof ResultStatement) {
foreach ($queryResult->fetchAll() as $row) {
/** @var positive-int $uid */
$uid = (int)$row['uid'];
$subpageUids[] = $uid;
if (\method_exists($queryResult, 'fetchAllAssociative')) {
foreach ($queryResult->fetchAllAssociative() as $row) {
/** @var positive-int $uid */
$uid = (int)$row['uid'];
$subpageUids[] = $uid;
}
} else {
foreach ($queryResult->fetchAll() as $row) {
/** @var positive-int $uid */
$uid = (int)$row['uid'];
$subpageUids[] = $uid;
}
}
}
return $subpageUids;
Expand Down
30 changes: 26 additions & 4 deletions Classes/Mapper/AbstractDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,11 @@ protected function retrieveRecord(array $whereClauseParts): array
throw new \UnexpectedValueException('Expected ResultStatement, got int instead.', 1646321598);
}

$data = $result->fetch();
if (\method_exists($result, 'fetchAssociative')) {
$data = $result->fetchAssociative();
} else {
$data = $result->fetch();
}
if ($data === false) {
throw new NotFoundException(
'No records found in the table "' . $tableName . '" matching: ' . \json_encode($whereClauseParts)
Expand Down Expand Up @@ -1227,7 +1231,13 @@ public function findByPageUid($pageUids, string $sorting = ''): Collection
throw new \UnexpectedValueException('Expected ResultStatement, got int instead.', 1646321575);
}

return $this->getListOfModels($result->fetchAll());
if (\method_exists($result, 'fetchAllAssociative')) {
$modelData = $result->fetchAllAssociative();
} else {
$modelData = $result->fetchAll();
}

return $this->getListOfModels($modelData);
}

/**
Expand Down Expand Up @@ -1517,7 +1527,13 @@ public function findAllByRelation(
throw new \UnexpectedValueException('Expected ResultStatement, got int instead.', 1646321551);
}

return $this->getListOfModels($result->fetchAll());
if (\method_exists($result, 'fetchAllAssociative')) {
$modelData = $result->fetchAllAssociative();
} else {
$modelData = $result->fetchAll();
}

return $this->getListOfModels($modelData);
}

/**
Expand All @@ -1536,7 +1552,13 @@ public function countByPageUid(string $pageUids): int
throw new \UnexpectedValueException('Expected ResultStatement, got int instead.', 1646321386);
}

return (int)$result->fetchColumn();
if (\method_exists($result, 'fetchOne')) {
$count = (int)$result->fetchOne();
} else {
$count = (int)$result->fetchColumn();
}

return $count;
}

/**
Expand Down
14 changes: 12 additions & 2 deletions Classes/Mapper/FrontEndUserMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,18 @@ public function getGroupMembers($commaSeparatedGroupUids): Collection
$tableName = $this->getTableName();
$where = 'deleted = 0 AND disable = 0 AND usergroup REGEXP \'(^|,)(' . \implode('|', $groupUids) . ')($|,)\'';
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName);
$statement = $connection->query('SELECT * FROM `' . $tableName . '` WHERE ' . $where);
$query = 'SELECT * FROM `' . $tableName . '` WHERE ' . $where;
if (\method_exists($connection, 'executeQuery')) {
$statement = $connection->executeQuery($query);
} else {
$statement = $connection->query($query);
}
if (\method_exists($statement, 'fetchAllAssociative')) {
$modelData = $statement->fetchAllAssociative();
} else {
$modelData = $statement->fetchAll();
}

return $this->getListOfModels($statement->fetchAll());
return $this->getListOfModels($modelData);
}
}
4 changes: 2 additions & 2 deletions Classes/Testing/CacheNullifyer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace OliverKlee\Oelib\Testing;

use OliverKlee\Oelib\System\Typo3Version;
use TYPO3\CMS\Core\Cache\Backend\NullBackend;
use TYPO3\CMS\Core\Cache\Backend\SimpleFileBackend;
use TYPO3\CMS\Core\Cache\Backend\TransientMemoryBackend;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\Core\Cache\FluidTemplateCache;

Expand All @@ -24,7 +24,7 @@ final class CacheNullifyer
*/
public function setAllCoreCaches(): void
{
if (Typo3Version::isNotHigherThan(9)) {
if ((new Typo3Version())->getMajorVersion() <= 9) {
$this->disableCoreCachesForVersion9();
} else {
$this->disableCoreCachesForVersion10();
Expand Down
118 changes: 94 additions & 24 deletions Classes/Testing/TestingFramework.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use OliverKlee\Oelib\Mapper\FrontEndUserMapper;
use OliverKlee\Oelib\Mapper\MapperRegistry;
use OliverKlee\Oelib\Model\FrontEndUserGroup;
use OliverKlee\Oelib\System\Typo3Version;
use Psr\Log\NullLogger;
use TYPO3\CMS\Core\Configuration\SiteConfiguration;
use TYPO3\CMS\Core\Context\Context;
Expand All @@ -21,6 +20,7 @@
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Exception\Page\PageNotFoundException;
use TYPO3\CMS\Core\Http\Uri;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Localization\Locales;
use TYPO3\CMS\Core\Routing\PageArguments;
use TYPO3\CMS\Core\Site\Entity\Site;
Expand Down Expand Up @@ -860,12 +860,26 @@ private function retrieveColumnsForTable(string $table): void
return;
}

$connection = $this->getConnectionForTable($table);
$query = 'SHOW FULL COLUMNS FROM `' . $table . '`';
if (\method_exists($connection, 'executeQuery')) {
$queryResult = $connection->executeQuery($query);
} else {
$queryResult = $connection->query($query);
}
$columns = [];
$queryResult = $this->getConnectionForTable($table)->query('SHOW FULL COLUMNS FROM `' . $table . '`');
/** @var array<string, string> $fieldRow */
foreach ($queryResult->fetchAll() as $fieldRow) {
$field = $fieldRow['Field'];
$columns[$field] = $fieldRow;
if (\method_exists($queryResult, 'fetchAllAssociative')) {
/** @var array<string, string> $fieldRow */
foreach ($queryResult->fetchAllAssociative() as $fieldRow) {
$field = $fieldRow['Field'];
$columns[$field] = $fieldRow;
}
} else {
/** @var array<string, string> $fieldRow */
foreach ($queryResult->fetchAll() as $fieldRow) {
$field = $fieldRow['Field'];
$columns[$field] = $fieldRow;
}
}

self::$tableColumnCache[$table] = $columns;
Expand Down Expand Up @@ -1204,7 +1218,7 @@ public function createFakeFrontEnd(int $pageUid = 0): int
$frontEndUser->unpack_uc();
$frontEndUser->fetchGroupData();

if (Typo3Version::isAtLeast(10)) {
if ((new Typo3Version())->getMajorVersion() >= 10) {
if ($pageUid > 0) {
$this->createDummySite($pageUid);
$allSites = GeneralUtility::makeInstance(SiteConfiguration::class)->getAllExistingSites(false);
Expand Down Expand Up @@ -1253,7 +1267,7 @@ public function createFakeFrontEnd(int $pageUid = 0): int
$frontEnd->tmpl->runThroughTemplates($rootLine);
$frontEnd->tmpl->generateConfig();
$frontEnd->tmpl->loaded = true;
if (Typo3Version::isAtLeast(10)) {
if ((new Typo3Version())->getMajorVersion() >= 10) {
Locales::setSystemLocaleFromSiteLanguage($frontEnd->getLanguage());
} else {
$frontEnd->settingLocale();
Expand Down Expand Up @@ -1541,13 +1555,27 @@ private function retrieveTableNames(): void
}

$connection = $this->getConnectionPool()->getConnectionByName('Default');
$queryResult = $connection->query('SHOW TABLE STATUS FROM `' . $connection->getDatabase() . '`');
$query = 'SHOW TABLE STATUS FROM `' . $connection->getDatabase() . '`';
if (\method_exists($connection, 'executeQuery')) {
$queryResult = $connection->executeQuery($query);
} else {
$queryResult = $connection->query($query);
}
$tableNames = [];
/** @var array<string, string|int|null> $tableInformation */
foreach ($queryResult->fetchAll() as $tableInformation) {
/** @var non-empty-string $tableName */
$tableName = $tableInformation['Name'];
$tableNames[$tableName] = $tableInformation;
if (\method_exists($queryResult, 'fetchAllAssociative')) {
/** @var array<string, string|int|null> $tableInformation */
foreach ($queryResult->fetchAllAssociative() as $tableInformation) {
/** @var non-empty-string $tableName */
$tableName = $tableInformation['Name'];
$tableNames[$tableName] = $tableInformation;
}
} else {
/** @var array<string, string|int|null> $tableInformation */
foreach ($queryResult->fetchAll() as $tableInformation) {
/** @var non-empty-string $tableName */
$tableName = $tableInformation['Name'];
$tableNames[$tableName] = $tableInformation;
}
}

self::$tableNameCache = $tableNames;
Expand Down Expand Up @@ -1743,7 +1771,13 @@ public function count(string $table, array $criteria = []): int
throw new \UnexpectedValueException('Expected ResultStatement, got int instead.', 1646321756);
}

return (int)$result->fetchColumn();
if (\method_exists($result, 'fetchOne')) {
$count = (int)$result->fetchOne();
} else {
$count = (int)$result->fetchColumn();
}

return $count;
}

/**
Expand Down Expand Up @@ -1775,9 +1809,14 @@ public function existsRecordWithUid(string $table, int $uid): bool

$dummyColumn = $this->getDummyColumnName($table);
$queryResult = $this->getConnectionForTable($table)
->select(['*'], $table, ['uid' => $uid, $dummyColumn => 1])->fetchAll();
->select(['*'], $table, ['uid' => $uid, $dummyColumn => 1]);
if (\method_exists($queryResult, 'fetchAllAssociative')) {
$data = $queryResult->fetchAllAssociative();
} else {
$data = $queryResult->fetchAll();
}

return $queryResult !== [];
return $data !== [];
}

/**
Expand Down Expand Up @@ -1814,7 +1853,12 @@ public function resetAutoIncrement(string $table): void
// Updates the auto increment index for this table. The index will be
// set to one UID above the highest existing UID.
$connection = $this->getConnectionPool()->getConnectionByName('Default');
$connection->query('ALTER TABLE `' . $table . '` AUTO_INCREMENT=' . $newAutoIncrementValue . ';');
$query = 'ALTER TABLE `' . $table . '` AUTO_INCREMENT=' . $newAutoIncrementValue . ';';
if (\method_exists($connection, 'executeQuery')) {
$connection->executeQuery($query);
} else {
$connection->query($query);
}
}

/**
Expand Down Expand Up @@ -1887,10 +1931,20 @@ public function setResetAutoIncrementThreshold(int $threshold): void
*/
private function getMaximumUidFromTable(string $table): int
{
$queryResult = $this->getConnectionForTable($table)
->query('SELECT MAX(uid) AS uid FROM `' . $table . '`')->fetch();
$connection = $this->getConnectionForTable($table);
$query = 'SELECT MAX(uid) AS uid FROM `' . $table . '`';
if (\method_exists($connection, 'executeQuery')) {
$queryResult = $connection->executeQuery($query);
} else {
$queryResult = $connection->query($query);
}
if (\method_exists($queryResult, 'fetchAllAssociative')) {
$data = $queryResult->fetchAllAssociative();
} else {
$data = $queryResult->fetchAll();
}

return (int)$queryResult['uid'];
return (int)$data['uid'];
}

/**
Expand All @@ -1916,8 +1970,18 @@ public function getAutoIncrement(string $table): int
);
}

$connection = $this->getConnectionForTable($table);
$query = 'SHOW TABLE STATUS WHERE Name = \'' . $table . '\';';
$row = $this->getConnectionForTable($table)->query($query)->fetch();
if (\method_exists($connection, 'executeQuery')) {
$queryResult = $connection->executeQuery($query);
} else {
$queryResult = $connection->query($query);
}
if (\method_exists($queryResult, 'fetchAssociative')) {
$row = $queryResult->fetchAssociative();
} else {
$row = $queryResult->fetch();
}

$autoIncrement = $row['Auto_increment'];
if ($autoIncrement === null) {
Expand Down Expand Up @@ -2015,8 +2079,14 @@ private function increaseRelationCounter(string $tableName, int $uid, string $fi
);
}

$connection = $this->getConnectionForTable($tableName);
$query = 'UPDATE ' . $tableName . ' SET ' . $fieldName . '=' . $fieldName . '+1 WHERE uid=' . $uid;
$numberOfAffectedRows = $this->getConnectionForTable($tableName)->query($query)->rowCount();
if (\method_exists($connection, 'executeQuery')) {
$queryResult = $connection->executeQuery($query);
} else {
$queryResult = $connection->query($query);
}
$numberOfAffectedRows = $queryResult->rowCount();
if ($numberOfAffectedRows === 0) {
throw new \BadMethodCallException(
'The table ' . $tableName . ' does not contain a record with UID ' . $uid . '.',
Expand Down Expand Up @@ -2077,6 +2147,6 @@ private function getFrontEndController(): TypoScriptFrontendController
*/
public function disableCoreCaches(): void
{
$this->cacheNullifyer->disableCoreCaches();
$this->cacheNullifyer->setAllCoreCaches();
}
}