2019-09-19 12:11:21 +02:00
|
|
|
<?php
|
|
|
|
|
2023-11-27 10:04:42 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2023-11-09 10:27:43 +01:00
|
|
|
namespace WerkraumMedia\Events\Service\Cleanup;
|
2019-09-19 12:11:21 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-11-27 10:04:42 +01:00
|
|
|
use DateTimeImmutable;
|
|
|
|
use PDO;
|
2019-09-19 12:11:21 +02:00
|
|
|
use TYPO3\CMS\Core\Database\Connection;
|
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool;
|
|
|
|
|
2023-11-27 10:04:42 +01:00
|
|
|
final class Database
|
2019-09-19 12:11:21 +02:00
|
|
|
{
|
2021-09-07 09:49:03 +02:00
|
|
|
private const DATE_TABLE = 'tx_events_domain_model_date';
|
|
|
|
private const EVENT_TABLE = 'tx_events_domain_model_event';
|
|
|
|
private const ORGANIZER_TABLE = 'tx_events_domain_model_organizer';
|
|
|
|
|
|
|
|
public function __construct(
|
2023-11-27 10:04:42 +01:00
|
|
|
private readonly ConnectionPool $connectionPool
|
2021-09-07 09:49:03 +02:00
|
|
|
) {
|
|
|
|
}
|
2019-09-19 12:11:21 +02:00
|
|
|
|
2021-09-07 09:49:03 +02:00
|
|
|
public function truncateTables(): void
|
2019-09-19 12:11:21 +02:00
|
|
|
{
|
2021-09-07 09:49:03 +02:00
|
|
|
$tableNames = [
|
|
|
|
Database::DATE_TABLE,
|
|
|
|
Database::ORGANIZER_TABLE,
|
|
|
|
Database::EVENT_TABLE,
|
|
|
|
];
|
|
|
|
|
2019-09-19 12:11:21 +02:00
|
|
|
foreach ($tableNames as $tableName) {
|
2021-09-07 09:49:03 +02:00
|
|
|
$this->connectionPool
|
2019-09-19 12:11:21 +02:00
|
|
|
->getConnectionForTable($tableName)
|
2023-11-27 10:04:42 +01:00
|
|
|
->truncate($tableName)
|
|
|
|
;
|
2019-09-19 12:11:21 +02:00
|
|
|
}
|
2021-09-07 09:49:03 +02:00
|
|
|
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_category_record_mm');
|
2023-11-27 10:04:42 +01:00
|
|
|
$queryBuilder->delete('sys_category_record_mm')->where($queryBuilder->expr()->like(
|
|
|
|
'tablenames',
|
|
|
|
$queryBuilder->createNamedParameter('tx_events_domain_model_%')
|
|
|
|
))->executeStatement();
|
2019-09-19 12:11:21 +02:00
|
|
|
}
|
|
|
|
|
2021-12-20 09:45:25 +01:00
|
|
|
public function deletePastDates(): void
|
2019-09-19 12:11:21 +02:00
|
|
|
{
|
2021-09-07 09:49:03 +02:00
|
|
|
$queryBuilder = $this->connectionPool
|
2021-12-13 09:09:13 +01:00
|
|
|
->getConnectionForTable(self::DATE_TABLE)
|
2023-11-27 10:04:42 +01:00
|
|
|
->createQueryBuilder()
|
|
|
|
;
|
2019-09-19 12:11:21 +02:00
|
|
|
|
|
|
|
$queryBuilder->getRestrictions()->removeAll();
|
|
|
|
|
2023-11-27 10:04:42 +01:00
|
|
|
$midnightToday = new DateTimeImmutable('midnight today');
|
|
|
|
$queryBuilder->delete(self::DATE_TABLE)->where($queryBuilder->expr()->lte(
|
|
|
|
'end',
|
|
|
|
$queryBuilder->createNamedParameter($midnightToday->format('U'))
|
|
|
|
))->executeStatement();
|
2019-09-19 12:11:21 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 09:49:03 +02:00
|
|
|
public function deleteEventsWithoutDates(): void
|
2019-09-19 12:11:21 +02:00
|
|
|
{
|
2021-09-07 09:49:03 +02:00
|
|
|
$queryBuilder = $this->connectionPool
|
2021-12-13 09:09:13 +01:00
|
|
|
->getConnectionForTable(self::EVENT_TABLE)
|
2023-11-27 10:04:42 +01:00
|
|
|
->createQueryBuilder()
|
|
|
|
;
|
2019-09-19 12:11:21 +02:00
|
|
|
|
|
|
|
$queryBuilder->getRestrictions()->removeAll();
|
|
|
|
|
2021-09-07 09:49:03 +02:00
|
|
|
$recordUids = $queryBuilder->select('event.uid')
|
2021-12-13 09:09:13 +01:00
|
|
|
->from(self::EVENT_TABLE, 'event')
|
2023-11-27 10:04:42 +01:00
|
|
|
->leftJoin('event', self::DATE_TABLE, 'date', $queryBuilder->expr()->eq('date.event', 'event.uid'))->where($queryBuilder->expr()->isNull('date.uid'))->executeQuery()
|
|
|
|
->fetchAll(PDO::FETCH_COLUMN)
|
|
|
|
;
|
2019-09-19 12:11:21 +02:00
|
|
|
|
2021-12-20 09:45:25 +01:00
|
|
|
$queryBuilder = $this->connectionPool->getQueryBuilderForTable(self::EVENT_TABLE);
|
|
|
|
$queryBuilder->delete(self::EVENT_TABLE);
|
|
|
|
$queryBuilder->where($queryBuilder->expr()->in(
|
|
|
|
'uid',
|
|
|
|
$queryBuilder->createNamedParameter($recordUids, Connection::PARAM_INT_ARRAY)
|
|
|
|
));
|
2023-11-27 10:04:42 +01:00
|
|
|
$queryBuilder->executeStatement();
|
2021-09-07 09:49:03 +02:00
|
|
|
|
|
|
|
$queryBuilder = $this->connectionPool->getQueryBuilderForTable('sys_category_record_mm');
|
2023-11-27 10:04:42 +01:00
|
|
|
$queryBuilder->delete('sys_category_record_mm')->where($queryBuilder->expr()->and($queryBuilder->expr()->like(
|
|
|
|
'tablenames',
|
|
|
|
$queryBuilder->createNamedParameter('tx_events_domain_model_%')
|
|
|
|
), $queryBuilder->expr()->in(
|
|
|
|
'uid_foreign',
|
|
|
|
$queryBuilder->createNamedParameter($recordUids, Connection::PARAM_INT_ARRAY)
|
|
|
|
)))->executeStatement();
|
2019-09-19 12:11:21 +02:00
|
|
|
}
|
|
|
|
}
|