Refactor code

Split large code base into separate classes.
Use Service as a controller, in order to define tasks to execute.
This commit is contained in:
Daniel Siepmann 2019-09-19 12:11:21 +02:00
parent 412b59d77f
commit 208c0cfbfa
3 changed files with 275 additions and 205 deletions

View file

@ -0,0 +1,131 @@
<?php
namespace Wrm\Events\Service\Cleanup;
/*
* Copyright (C) 2019 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
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;
class Database
{
const DATE_TABLE = 'tx_events_domain_model_date';
const EVENT_TABLE = 'tx_events_domain_model_event';
const ORGANIZER_TABLE = 'tx_events_domain_model_organizer';
public function truncateTables(string ...$tableNames): void
{
foreach ($tableNames as $tableName) {
GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable($tableName)
->truncate($tableName);
}
}
public function getDeletionStructureForEvents(): array
{
$dataStructure = [static::EVENT_TABLE => []];
foreach ($this->getAllRecords(static::EVENT_TABLE) as $recordToDelete) {
$dataStructure[static::EVENT_TABLE][$recordToDelete] = ['delete' => 1];
}
return $dataStructure;
}
private function getAllRecords(string $tableName): array
{
/* @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable($tableName)
->createQueryBuilder();
$records = $queryBuilder->select('uid')
->from($tableName)
->execute()
->fetchAll();
return array_map(function (array $record) {
return $record['uid'];
}, $records);
}
public function getPastDates(): array
{
$midnightToday = new \DateTimeImmutable('midnight today');
/* @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable(static::DATE_TABLE)
->createQueryBuilder();
$queryBuilder->getRestrictions()->removeAll();
$records = $queryBuilder->select('uid')
->from(static::DATE_TABLE)
->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);
}
public function deleteDates(int ...$uids)
{
/* @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable(static::DATE_TABLE);
$queryBuilder->delete(static::DATE_TABLE)
->where('uid in (:uids)')
->setParameter(':uids', $uids, Connection::PARAM_INT_ARRAY)
->execute();
}
public function getDeletionStructureForEventsWithoutDates(): array
{
$dataStructure = [static::EVENT_TABLE => []];
/* @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable(static::EVENT_TABLE)
->createQueryBuilder();
$queryBuilder->getRestrictions()->removeAll();
$records = $queryBuilder->select('event.uid')
->from(static::EVENT_TABLE, 'event')
->leftJoin('event', static::DATE_TABLE, 'date', $queryBuilder->expr()->eq('date.event', 'event.uid'))
->where($queryBuilder->expr()->isNull('date.uid'))
->execute()
->fetchAll();
foreach ($records as $record) {
$dataStructure[static::EVENT_TABLE][$record['uid']] = ['delete' => 1];
}
return $dataStructure;
}
}

View file

@ -0,0 +1,115 @@
<?php
namespace Wrm\Events\Service\Cleanup;
/*
* Copyright (C) 2019 Daniel Siepmann <coding@daniel-siepmann.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
use TYPO3\CMS\Core\Database\Connection;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
use TYPO3\CMS\Core\Resource\ResourceStorage;
use TYPO3\CMS\Core\Resource\StorageRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
class Files
{
public function deleteAll()
{
$this->delete($this->getFilesFromDb());
}
public function deleteDangling()
{
$this->delete($this->getFilesFromDb(function (QueryBuilder $queryBuilder) {
$queryBuilder->leftJoin(
'file',
'sys_file_reference',
'reference',
$queryBuilder->expr()->eq('file.uid', $queryBuilder->quoteIdentifier('reference.uid_local'))
);
$queryBuilder->andWhere(
$queryBuilder->expr()->orX(
$queryBuilder->expr()->isNull('reference.uid'),
$queryBuilder->expr()->eq('reference.deleted', 1),
$queryBuilder->expr()->eq('reference.hidden', 1)
)
);
}));
}
private function delete(array $filesToDelete)
{
$uidsToRemove = [];
foreach ($filesToDelete as $fileToDelete) {
$this->deleteFromFal($fileToDelete['storage'], $fileToDelete['identifier']);
$uidsToRemove[] = $fileToDelete['uid'];
}
$this->deleteFromDb(... $uidsToRemove);
}
private function getFilesFromDb(callable $whereGenerator = null): array
{
/* @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('sys_file');
$queryBuilder->getRestrictions()->removeAll();
$queryBuilder->select('file.identifier', 'file.storage', 'file.uid')
->from('sys_file', 'file')
->where($queryBuilder->expr()->like(
'file.identifier',
$queryBuilder->createNamedParameter('/staedte/%/events/%')
));
if ($whereGenerator !== null) {
$whereGenerator($queryBuilder);
}
return $queryBuilder->execute()->fetchAll();
}
private function deleteFromFal(int $storageUid, string $filePath)
{
/* @var ResourceStorage $storage */
$storage = GeneralUtility::makeInstance(StorageRepository::class)
->findByUid($storageUid);
if ($storage->hasFile($filePath) === false) {
return;
}
$storage->deleteFile($storage->getFile($filePath));
}
private function deleteFromDb(int ...$uids)
{
/* @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('sys_file');
$queryBuilder->delete('sys_file')
->where('uid in (:uids)')
->setParameter(':uids', $uids, Connection::PARAM_INT_ARRAY)
->execute();
}
}

View file

@ -3,223 +3,47 @@
namespace Wrm\Events\Service; namespace Wrm\Events\Service;
use TYPO3\CMS\Core\DataHandling\DataHandler; 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\Resource\ResourceStorage;
use TYPO3\CMS\Core\Resource\StorageRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Core\Utility\GeneralUtility;
use Wrm\Events\Service\Cleanup\Database;
use Wrm\Events\Service\Cleanup\Files;
class CleanupService class CleanupService
{ {
/**
* @var Database
*/
private $database;
/**
* @var Files
*/
private $files;
public function __construct(Database $database, Files $files)
{
$this->database = $database;
$this->files = $files;
}
public function deleteAllData() public function deleteAllData()
{ {
$this->truncateTables(... [ $this->database->truncateTables(... [Database::DATE_TABLE, Database::ORGANIZER_TABLE]);
'tx_events_domain_model_date', $this->removeViaDataHandler($this->database->getDeletionStructureForEvents());
'tx_events_domain_model_organizer', $this->files->deleteAll();
]);
$dataHandler = GeneralUtility::makeInstance(DataHandler::class);
/* @var DataHandler $dataHandler */
$dataHandler->start([], $this->getDeletionStructure([
'tx_events_domain_model_event',
]));
$dataHandler->process_cmdmap();
$this->deleteAllFiles();
} }
public function deletePastData() public function deletePastData()
{ {
$this->deleteDates(... $this->getPastDates()); $this->database->deleteDates(... $this->database->getPastDates());
$this->removeViaDataHandler($this->database->getDeletionStructureForEventsWithoutDates());
$this->files->deleteDangling();
}
$dataHandler = GeneralUtility::makeInstance(DataHandler::class); private function removeViaDataHandler(array $structure)
{
/* @var DataHandler $dataHandler */ /* @var DataHandler $dataHandler */
$dataHandler->start([], $this->getDeletionStructureForEventsWithoutDates()); $dataHandler = GeneralUtility::makeInstance(DataHandler::class);
$dataHandler->start([], $structure);
$dataHandler->process_cmdmap(); $dataHandler->process_cmdmap();
$this->deleteDanglingFiles();
}
private function truncateTables(string ...$tableNames): void
{
foreach ($tableNames as $tableName) {
GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable($tableName)
->truncate($tableName);
}
}
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
{
/* @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable($tableName)
->createQueryBuilder();
$records = $queryBuilder->select('uid')
->from($tableName)
->execute()
->fetchAll();
return array_map(function (array $record) {
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;
}
private function deleteAllFiles()
{
$this->deleteFiles($this->getRelatedFileInformation());
}
private function deleteDanglingFiles()
{
$this->deleteFiles($this->getRelatedFileInformation(function (QueryBuilder $queryBuilder) {
$queryBuilder->leftJoin(
'file',
'sys_file_reference',
'reference',
$queryBuilder->expr()->eq('file.uid', 'reference.local_uid')
);
$queryBuilder->addWhere($queryBuilder->expr()->isNull('reference.uid'));
}));
}
private function deleteFiles(array $files)
{
$uidsToRemove = [];
foreach ($filesToDelete as $fileToDelete) {
$this->deleteFileFromFilesystem($fileToDelete['storage'], $fileToDelete['identifier']);
$uidsToRemove[] = $fileToDelete['uid'];
}
$this->deleteFileRecords(... $uidsToRemove);
}
private function getRelatedFileInformation(callable $whereGenerator = null): array
{
/* @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('sys_file');
$queryBuilder->select('file.identifier', 'file.storage', 'file.uid')
->from('sys_file', 'file')
->where($queryBuilder->expr()->like(
'file.identifier',
$queryBuilder->createNamedParameter('/staedte/%/events/%')
));
if ($whereGenerator !== null) {
$whereGenerator($queryBuilder);
}
return $queryBuilder->execute()->fetchAll();
}
private function deleteFileFromFilesystem(int $storageUid, string $filePath)
{
/* @var ResourceStorage $storage */
$storage = GeneralUtility::makeInstance(StorageRepository::class)
->findByUid($storageUid);
if ($storage->hasFile($filePath) === false) {
return;
}
$storage->deleteFile($storage->getFile($filePath));
}
private function deleteFileRecords(int ...$uids)
{
/* @var QueryBuilder $queryBuilder */
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('sys_file');
$queryBuilder->delete('sys_file')
->where('uid in (:uids)')
->setParameter(':uids', $uids, Connection::PARAM_INT_ARRAY)
->execute();
} }
} }