[TASK] Remove ext:dbal from installation steps
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / TableGarbageCollectionTask.php
1 <?php
2 namespace TYPO3\CMS\Scheduler\Task;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use Doctrine\DBAL\DBALException;
17 use TYPO3\CMS\Core\Database\ConnectionPool;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Remove old entries from tables.
22 *
23 * This task deletes rows from tables older than the given number of days.
24 *
25 * Available tables must be registered in
26 * $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask::class]['options']['tables']
27 * See ext_localconf.php of scheduler extension for an example
28 */
29 class TableGarbageCollectionTask extends AbstractTask
30 {
31 /**
32 * @var bool True if all tables should be cleaned up
33 */
34 public $allTables = false;
35
36 /**
37 * @var int Number of days
38 */
39 public $numberOfDays = 180;
40
41 /**
42 * @var string Table to clean up
43 */
44 public $table = '';
45
46 /**
47 * Execute garbage collection, called by scheduler.
48 *
49 * @throws \RuntimeException If configured table was not cleaned up
50 * @return bool TRUE if task run was successful
51 */
52 public function execute()
53 {
54 $tableConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][TableGarbageCollectionTask::class]['options']['tables'];
55 $tableHandled = false;
56 foreach ($tableConfigurations as $tableName => $configuration) {
57 if ($this->allTables || $tableName === $this->table) {
58 $this->handleTable($tableName, $configuration);
59 $tableHandled = true;
60 }
61 }
62 if (!$tableHandled) {
63 throw new \RuntimeException(TableGarbageCollectionTask::class . ' misconfiguration: ' . $this->table . ' does not exist in configuration', 1308354399);
64 }
65 return true;
66 }
67
68 /**
69 * Execute clean up of a specific table
70 *
71 * @throws \RuntimeException If table configuration is broken
72 * @param string $table The table to handle
73 * @param array $configuration Clean up configuration
74 * @return bool TRUE if cleanup was successful
75 */
76 protected function handleTable($table, array $configuration)
77 {
78 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table);
79 $queryBuilder->delete($table);
80 if (!empty($configuration['expireField'])) {
81 $field = $configuration['expireField'];
82 $dateLimit = $GLOBALS['EXEC_TIME'];
83 // If expire field value is 0, do not delete
84 // Expire field = 0 means no expiration
85 $queryBuilder->where(
86 $queryBuilder->expr()->lte($field, $dateLimit),
87 $queryBuilder->expr()->gt($field, 0)
88 );
89 } elseif (!empty($configuration['dateField'])) {
90 if (!$this->allTables) {
91 $deleteTimestamp = strtotime('-' . $this->numberOfDays . 'days');
92 } else {
93 if (!isset($configuration['expirePeriod'])) {
94 throw new \RuntimeException(TableGarbageCollectionTask::class . ' misconfiguration: No expirePeriod defined for table ' . $table, 1308355095);
95 }
96 $deleteTimestamp = strtotime('-' . $configuration['expirePeriod'] . 'days');
97 }
98 $queryBuilder->where(
99 $queryBuilder->expr()->lt($configuration['dateField'], $deleteTimestamp)
100 );
101 } else {
102 throw new \RuntimeException(TableGarbageCollectionTask::class . ' misconfiguration: Either expireField or dateField must be defined for table ' . $table, 1308355268);
103 }
104
105 try {
106 $queryBuilder->execute();
107 } catch (DBALException $e) {
108 throw new \RuntimeException(TableGarbageCollectionTask::class . ' failed for table ' . $this->table . ' with error: ' . $e->getMessage(), 1308255491);
109 }
110 return true;
111 }
112
113 /**
114 * This method returns the selected table as additional information
115 *
116 * @return string Information to display
117 */
118 public function getAdditionalInformation()
119 {
120 if ($this->allTables) {
121 $message = $GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.additionalInformationAllTables');
122 } else {
123 $message = sprintf($GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.additionalInformationTable'), $this->table);
124 }
125 return $message;
126 }
127 }