diff --git a/Classes/Update/AddSites/AbstractSiteUpgrade.php b/Classes/Update/AddSites/AbstractSiteUpgrade.php index 32719814..3842b467 100644 --- a/Classes/Update/AddSites/AbstractSiteUpgrade.php +++ b/Classes/Update/AddSites/AbstractSiteUpgrade.php @@ -5,6 +5,10 @@ namespace In2code\Lux\Update\AddSites; use In2code\Lux\Domain\Service\SiteService; +use In2code\Lux\Utility\DatabaseUtility; +use TYPO3\CMS\Core\Database\ConnectionPool; +use TYPO3\CMS\Core\Database\Query\QueryBuilder; +use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction; use TYPO3\CMS\Core\Utility\GeneralUtility; abstract class AbstractSiteUpgrade @@ -25,6 +29,36 @@ public function __construct() $this->siteService = GeneralUtility::makeInstance(SiteService::class); } + public function run(): void + { + $connection = $this->getConnectionPool()->getConnectionForTable(static::TABLE_NAME); + + $queryBuilder = $this->getPreparedQueryBuilder(); + $result = $queryBuilder + ->select('page') + ->groupBy('page') + ->where( + $queryBuilder->expr()->eq('site', $queryBuilder->createNamedParameter('')) + ) + ->executeQuery(); + while ($record = $result->fetchAssociative()) { + $siteIdentifier = $this->getSiteIdentifierFromPage($record['page']); + if ($siteIdentifier !== '') { + $connection->update( + static::TABLE_NAME, + ['site' => $siteIdentifier], + ['page' => (int)$record['page']] + ); + } else { + $connection->update( + static::TABLE_NAME, + ['deleted' => 1], + ['page' => (int)$record['page']] + ); + } + } + } + protected function getSiteIdentifierFromPage(int $pageIdentifier): string { if (array_key_exists($pageIdentifier, $this->mapping)) { @@ -38,4 +72,17 @@ protected function getSiteIdentifierFromPage(int $pageIdentifier): string $this->mapping[$pageIdentifier] = $siteIdentifier; return $siteIdentifier; } + + protected function getPreparedQueryBuilder(): QueryBuilder + { + $queryBuilder = $this->getConnectionPool()->getQueryBuilderForTable(static::TABLE_NAME); + $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class)); + $queryBuilder->from(static::TABLE_NAME); + return $queryBuilder; + } + + protected function getConnectionPool(): ConnectionPool + { + return GeneralUtility::makeInstance(ConnectionPool::class); + } } diff --git a/Classes/Update/AddSites/AddSitesForDownloads.php b/Classes/Update/AddSites/AddSitesForDownloads.php index 475402db..fcbd0664 100644 --- a/Classes/Update/AddSites/AddSitesForDownloads.php +++ b/Classes/Update/AddSites/AddSitesForDownloads.php @@ -9,17 +9,5 @@ class AddSitesForDownloads extends AbstractSiteUpgrade { - public function run(): void - { - $connection = DatabaseUtility::getConnectionForTable(Download::TABLE_NAME); - $records = $connection - ->executeQuery('select * from ' . Download::TABLE_NAME . ' where deleted=0') - ->fetchAllAssociative(); - foreach ($records as $record) { - $siteIdentifier = $this->getSiteIdentifierFromPage($record['page']); - $connection->executeQuery( - 'update ' . Download::TABLE_NAME . ' set site = "' . $siteIdentifier . '" where uid=' . $record['uid'] - ); - } - } + protected const TABLE_NAME = Download::TABLE_NAME; } diff --git a/Classes/Update/AddSites/AddSitesForLinkclick.php b/Classes/Update/AddSites/AddSitesForLinkclick.php index 240f61d2..76925ff7 100644 --- a/Classes/Update/AddSites/AddSitesForLinkclick.php +++ b/Classes/Update/AddSites/AddSitesForLinkclick.php @@ -9,17 +9,5 @@ class AddSitesForLinkclick extends AbstractSiteUpgrade { - public function run(): void - { - $connection = DatabaseUtility::getConnectionForTable(Linkclick::TABLE_NAME); - $records = $connection - ->executeQuery('select * from ' . Linkclick::TABLE_NAME . ' where deleted=0') - ->fetchAllAssociative(); - foreach ($records as $record) { - $siteIdentifier = $this->getSiteIdentifierFromPage($record['page']); - $connection->executeQuery( - 'update ' . Linkclick::TABLE_NAME . ' set site = "' . $siteIdentifier . '" where uid=' . $record['uid'] - ); - } - } + protected const TABLE_NAME = Linkclick::TABLE_NAME; } diff --git a/Classes/Update/AddSites/AddSitesForPagevisits.php b/Classes/Update/AddSites/AddSitesForPagevisits.php index 9844d220..7daa669b 100644 --- a/Classes/Update/AddSites/AddSitesForPagevisits.php +++ b/Classes/Update/AddSites/AddSitesForPagevisits.php @@ -9,17 +9,5 @@ class AddSitesForPagevisits extends AbstractSiteUpgrade { - public function run(): void - { - $connection = DatabaseUtility::getConnectionForTable(Pagevisit::TABLE_NAME); - $records = $connection - ->executeQuery('select * from ' . Pagevisit::TABLE_NAME . ' where deleted=0') - ->fetchAllAssociative(); - foreach ($records as $record) { - $siteIdentifier = $this->getSiteIdentifierFromPage($record['page']); - $connection->executeQuery( - 'update ' . Pagevisit::TABLE_NAME . ' set site = "' . $siteIdentifier . '" where uid=' . $record['uid'] - ); - } - } + protected const TABLE_NAME = Pagevisit::TABLE_NAME; } diff --git a/Classes/Utility/DatabaseUtility.php b/Classes/Utility/DatabaseUtility.php index e7c92587..e836ea61 100644 --- a/Classes/Utility/DatabaseUtility.php +++ b/Classes/Utility/DatabaseUtility.php @@ -7,6 +7,7 @@ use TYPO3\CMS\Core\Database\Connection; use TYPO3\CMS\Core\Database\ConnectionPool; use TYPO3\CMS\Core\Database\Query\QueryBuilder; +use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction; use TYPO3\CMS\Core\Utility\GeneralUtility; class DatabaseUtility @@ -107,12 +108,14 @@ public static function isFieldInTableFilled(string $fieldName, string $tableName public static function isAnyFieldFilledInTable(string $fieldName, string $tableName): bool { $queryBuilder = self::getQueryBuilderForTable($tableName); - return $queryBuilder + $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class)); + $result = $queryBuilder ->select($fieldName) ->from($tableName) - ->where($fieldName . ' != \'\'') + ->where($fieldName . ' = \'\'') ->setMaxResults(1) ->executeQuery() - ->fetchOne() !== false; + ->fetchOne(); + return $result === false; } }