ew_base/Classes/Updates/ParentChildToContainerMigration.php
Sebastian Fischer 5319bd35a5 First commit
2024-02-09 17:13:28 +01:00

112 lines
3.2 KiB
PHP
Executable File

<?php
declare(strict_types=1);
namespace Evoweb\EwBase\Updates;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
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\Log\LogManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Install\Attribute\UpgradeWizard;
use TYPO3\CMS\Install\Updates\DatabaseUpdatedPrerequisite;
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;
/**
* Migrate parent child elements to container elements
*/
#[UpgradeWizard('parentChildToContainer')]
class ParentChildToContainerMigration implements UpgradeWizardInterface
{
private const TABLE_NAME = 'tt_content';
protected LoggerInterface $logger;
public function __construct(
protected ParentChildToContainerService $service,
protected LogManager $logManager,
) {
$this->logger = $logManager->getLogger(self::class);
}
public function getIdentifier(): string
{
return 'parentChildToContainer';
}
public function getTitle(): string
{
return 'Migrate parent/child to container';
}
public function getDescription(): string
{
return 'Migrates content elements that are type parent/child to corresponding container elements';
}
public function getPrerequisites(): array
{
return [
DatabaseUpdatedPrerequisite::class
];
}
public function updateNecessary(): bool
{
return $this->hasRecordsToUpdate();
}
public function executeUpdate(): bool
{
$updateSuccessful = true;
try {
$this->service->migrate();
} catch (\Exception $exception) {
$this->logger->log(LogLevel::ERROR, $exception->getMessage());
$updateSuccessful = false;
}
return $updateSuccessful;
}
protected function hasRecordsToUpdate(): bool
{
$queryBuilder = $this->getPreparedQueryBuilder();
$tableColumns = $queryBuilder
->getConnection()
->createSchemaManager()
->listTableColumns(self::TABLE_NAME);
return isset($tableColumns['parent'])
&& $queryBuilder
->count('uid')
->executeQuery()
->fetchOne() > 0;
}
protected function getPreparedQueryBuilder(): QueryBuilder
{
$queryBuilder = $this->getConnectionPool()->getQueryBuilderForTable(self::TABLE_NAME);
$queryBuilder->getRestrictions()
->removeByType(HiddenRestriction::class)
->removeByType(StartTimeRestriction::class)
->removeByType(EndTimeRestriction::class);
$queryBuilder
->from(self::TABLE_NAME)
->where(
// check if there are still records that have parent instead of tx_container_parent
$queryBuilder->expr()->gt('parent', 0)
);
return $queryBuilder;
}
protected function getConnectionPool(): ConnectionPool
{
return GeneralUtility::makeInstance(ConnectionPool::class);
}
}