From ca4d7d3a016e20d0d43cd15c0ebd02697324f94b Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 17 Sep 2019 17:14:28 +0200 Subject: [PATCH] Add basic cleanup command Allows to remove all existing records regarding events. It will search for all pages providing either organizer or events, and will delete both types of records. As DataHandler is used for deletion, there is already logging within TYPO3, as well as recursive deletion for dates and sys_file_references. To speedup for large data sets, truncate organizer and dates, as they don't have recursive deletions, e.g. to sys_file_references. Also add keys to database. --- ...leanupCommand.php => RemoveAllCommand.php} | 16 ++-- Classes/Service/CleanupService.php | 95 +++++++++++++------ Configuration/Commands.php | 7 +- ext_tables.sql | 4 + 4 files changed, 81 insertions(+), 41 deletions(-) rename Classes/Command/{CleanupCommand.php => RemoveAllCommand.php} (76%) diff --git a/Classes/Command/CleanupCommand.php b/Classes/Command/RemoveAllCommand.php similarity index 76% rename from Classes/Command/CleanupCommand.php rename to Classes/Command/RemoveAllCommand.php index 054fb31..38306ab 100644 --- a/Classes/Command/CleanupCommand.php +++ b/Classes/Command/RemoveAllCommand.php @@ -4,19 +4,17 @@ namespace Wrm\Events\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; - -use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Core\Bootstrap; +use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Object\ObjectManager; - use Wrm\Events\Service\CleanupService; -class CleanupCommand extends Command { - +class RemoveAllCommand extends Command +{ public function configure() { - $this->setDescription('Cleanup Events'); - $this->setHelp('Events are cleaned up'); + $this->setDescription('Remove all event data'); + $this->setHelp('All events and associated data will be removed.'); } protected function execute(InputInterface $input, OutputInterface $output) @@ -25,6 +23,6 @@ class CleanupCommand extends Command { return GeneralUtility::makeInstance(ObjectManager::class) ->get(CleanupService::class) - ->doClean(); + ->deleteAllData(); } -} \ No newline at end of file +} diff --git a/Classes/Service/CleanupService.php b/Classes/Service/CleanupService.php index 88afad6..e4647ce 100644 --- a/Classes/Service/CleanupService.php +++ b/Classes/Service/CleanupService.php @@ -2,39 +2,74 @@ namespace Wrm\Events\Service; +use TYPO3\CMS\Core\DataHandling\DataHandler; +use TYPO3\CMS\Core\Database\Connection; +use TYPO3\CMS\Core\Database\ConnectionPool; +use TYPO3\CMS\Core\Database\Query\QueryBuilder; use TYPO3\CMS\Core\Utility\GeneralUtility; -use TYPO3\CMS\Core\Log\LogManager; -use TYPO3\CMS\Extbase\Configuration\ConfigurationManager; -use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; -use TYPO3\CMS\Extbase\Object\ObjectManager; -class CleanupService { +class CleanupService +{ + public function deleteAllData() + { + $this->truncateTables(... [ + 'tx_events_domain_model_date', + 'tx_events_domain_model_organizer', + ]); - /** - * Cleanup Service constructor. - * @param ConfigurationManager $configurationManager - * @param ObjectManager $objectManager - */ - public function __construct( - ConfigurationManager $configurationManager, - ObjectManager $objectManager - ) { - - // Get Typoscript Settings - $this->settings = $this->configurationManager->getConfiguration( - ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, - 'Events', - 'Pi1' - ); - - - $this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__); - $this->logger->info('Event Cleanup Service'); + $dataHandler = GeneralUtility::makeInstance(DataHandler::class); + /* @var DataHandler $dataHandler */ + $dataHandler->start([], $this->getDeletionStructure([ + 'tx_events_domain_model_event', + ])); + $dataHandler->process_cmdmap(); } - public function doCleanup() { - - // To be done - // Hmpf + private function truncateTables(string ...$tableNames): void + { + foreach ($tableNames as $tableName) { + GeneralUtility::makeInstance(ConnectionPool::class) + ->getConnectionForTable($tableName) + ->truncate($tableName); + } } -} \ No newline at end of file + + private function getDeletionStructure(array $tableNames): array + { + $structure = []; + + foreach ($tableNames as $tableName) { + $structure = array_merge($structure, $this->getDeletionStructureForTable($tableName)); + } + + return $structure; + } + + private function getDeletionStructureForTable(string $tableName): array + { + $dataStructure = [$tableName=> []]; + + foreach ($this->getRecordsToDelete($tableName) as $recordToDelete) { + $dataStructure[$tableName][$recordToDelete] = ['delete' => 1]; + } + + return $dataStructure; + } + + private function getRecordsToDelete(string $tableName): array + { + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) + ->getConnectionForTable($tableName) + ->createQueryBuilder(); + + /* @var QueryBuilder $queryBuilder */ + $records = $queryBuilder->select('uid') + ->from($tableName) + ->execute() + ->fetchAll(); + + return array_map(function (array $record) { + return $record['uid']; + }, $records); + } +} diff --git a/Configuration/Commands.php b/Configuration/Commands.php index 183f817..64ddda0 100644 --- a/Configuration/Commands.php +++ b/Configuration/Commands.php @@ -2,5 +2,8 @@ return [ 'events:destinationdataimport‚' => [ 'class' => \Wrm\Events\Command\DestinationDataImportCommand::class - ] -]; \ No newline at end of file + ], + 'events:removeAll' => [ + 'class' => \Wrm\Events\Command\RemoveAllCommand::class + ], +]; diff --git a/ext_tables.sql b/ext_tables.sql index 73c3c73..dfa2b6e 100644 --- a/ext_tables.sql +++ b/ext_tables.sql @@ -28,6 +28,7 @@ CREATE TABLE tx_events_domain_model_event ( organizer int(11) unsigned DEFAULT '0', region int(11) unsigned DEFAULT '0', + KEY dataHandler (l10n_parent, t3ver_oid, deleted, t3ver_wsid, t3ver_state) ); # @@ -44,6 +45,7 @@ CREATE TABLE tx_events_domain_model_organizer ( web varchar(255) DEFAULT '' NOT NULL, email varchar(255) DEFAULT '' NOT NULL, + KEY dataHandler (l10n_parent, sys_language_uid, deleted) ); # @@ -56,6 +58,8 @@ CREATE TABLE tx_events_domain_model_date ( start datetime DEFAULT NULL, end datetime DEFAULT NULL, + KEY event (event), + KEY dataHandler (event, t3ver_wsid, pid) ); #