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.
This commit is contained in:
Daniel Siepmann 2019-09-19 09:09:42 +02:00
parent ca4d7d3a01
commit 8ed5bd9970
3 changed files with 101 additions and 0 deletions

View file

@ -0,0 +1,28 @@
<?php
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\Core\Bootstrap;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use Wrm\Events\Service\CleanupService;
class RemovePastCommand extends Command
{
public function configure()
{
$this->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();
}
}

View file

@ -25,6 +25,16 @@ class CleanupService
$dataHandler->process_cmdmap(); $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 private function truncateTables(string ...$tableNames): void
{ {
foreach ($tableNames as $tableName) { foreach ($tableNames as $tableName) {
@ -72,4 +82,64 @@ class CleanupService
return $record['uid']; return $record['uid'];
}, $records); }, $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;
}
} }

View file

@ -6,4 +6,7 @@ return [
'events:removeAll' => [ 'events:removeAll' => [
'class' => \Wrm\Events\Command\RemoveAllCommand::class 'class' => \Wrm\Events\Command\RemoveAllCommand::class
], ],
'events:removePast' => [
'class' => \Wrm\Events\Command\RemovePastCommand::class
],
]; ];