From 8ed5bd9970249eed682e4c4a0e99eceb0a88b96a Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Thu, 19 Sep 2019 09:09:42 +0200 Subject: [PATCH] Add command to remove all past dates and events It will search for all dates within the past and remove them. Afterwards all events with no dates are searched and removed. As DataHandler is used for deletion of events, there is already logging within TYPO3, as well as recursive deletion for sys_file_references. To speedup for large data sets, deletion of dates is done without DataHandler. --- Classes/Command/RemovePastCommand.php | 28 +++++++++++ Classes/Service/CleanupService.php | 70 +++++++++++++++++++++++++++ Configuration/Commands.php | 3 ++ 3 files changed, 101 insertions(+) create mode 100644 Classes/Command/RemovePastCommand.php diff --git a/Classes/Command/RemovePastCommand.php b/Classes/Command/RemovePastCommand.php new file mode 100644 index 0000000..c661c39 --- /dev/null +++ b/Classes/Command/RemovePastCommand.php @@ -0,0 +1,28 @@ +setDescription('Remove past events'); + $this->setHelp('Past dates are removed, together with events that do not have any left dates.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + Bootstrap::initializeBackendAuthentication(); + + return GeneralUtility::makeInstance(ObjectManager::class) + ->get(CleanupService::class) + ->deletePastData(); + } +} diff --git a/Classes/Service/CleanupService.php b/Classes/Service/CleanupService.php index e4647ce..39f7f89 100644 --- a/Classes/Service/CleanupService.php +++ b/Classes/Service/CleanupService.php @@ -25,6 +25,16 @@ class CleanupService $dataHandler->process_cmdmap(); } + public function deletePastData() + { + $this->deleteDates(... $this->getPastDates()); + + $dataHandler = GeneralUtility::makeInstance(DataHandler::class); + /* @var DataHandler $dataHandler */ + $dataHandler->start([], $this->getDeletionStructureForEventsWithoutDates()); + $dataHandler->process_cmdmap(); + } + private function truncateTables(string ...$tableNames): void { foreach ($tableNames as $tableName) { @@ -72,4 +82,64 @@ class CleanupService return $record['uid']; }, $records); } + + private function getPastDates(): array + { + $midnightToday = new \DateTimeImmutable('midnight today'); + + /* @var QueryBuilder $queryBuilder */ + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) + ->getConnectionForTable('tx_events_domain_model_date') + ->createQueryBuilder(); + + $queryBuilder->getRestrictions()->removeAll(); + + $records = $queryBuilder->select('uid') + ->from('tx_events_domain_model_date') + ->where($queryBuilder->expr()->lte( + 'end', + $queryBuilder->createNamedParameter($midnightToday->format('Y-m-d H:i:s')) + )) + ->execute() + ->fetchAll(); + + return array_map(function (array $record) { + return $record['uid']; + }, $records); + } + + private function deleteDates(int ...$uids) + { + /* @var QueryBuilder $queryBuilder */ + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) + ->getQueryBuilderForTable('tx_events_domain_model_date'); + + $queryBuilder->delete('tx_events_domain_model_date') + ->where('uid in (:uids)') + ->setParameter(':uids', $uids, Connection::PARAM_INT_ARRAY) + ->execute(); + } + + private function getDeletionStructureForEventsWithoutDates(): array + { + $dataStructure = ['tx_events_domain_model_event' => []]; + /* @var QueryBuilder $queryBuilder */ + $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) + ->getConnectionForTable('tx_events_domain_model_event') + ->createQueryBuilder(); + + $queryBuilder->getRestrictions()->removeAll(); + + $records = $queryBuilder->select('event.uid') + ->from('tx_events_domain_model_event', 'event') + ->leftJoin('event', 'tx_events_domain_model_date', 'date', $queryBuilder->expr()->eq('date.event', 'event.uid')) + ->where($queryBuilder->expr()->isNull('date.uid')) + ->execute() + ->fetchAll(); + + foreach ($records as $record) { + $dataStructure['tx_events_domain_model_event'][$record['uid']] = ['delete' => 1]; + } + return $dataStructure; + } } diff --git a/Configuration/Commands.php b/Configuration/Commands.php index 64ddda0..91b6d7f 100644 --- a/Configuration/Commands.php +++ b/Configuration/Commands.php @@ -6,4 +6,7 @@ return [ 'events:removeAll' => [ 'class' => \Wrm\Events\Command\RemoveAllCommand::class ], + 'events:removePast' => [ + 'class' => \Wrm\Events\Command\RemovePastCommand::class + ], ];