‪TYPO3CMS  10.4
TableGarbageCollectionTask.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Doctrine\DBAL\DBALException;
21 
33 {
37  public ‪$allTables = false;
38 
42  public ‪$numberOfDays = 180;
43 
47  public ‪$table = '';
48 
55  public function ‪execute()
56  {
57  $tableConfigurations = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][self::class]['options']['tables'];
58  $tableHandled = false;
59  foreach ($tableConfigurations as $tableName => $configuration) {
60  if ($this->allTables || $tableName === $this->table) {
61  $this->‪handleTable($tableName, $configuration);
62  $tableHandled = true;
63  }
64  }
65  if (!$tableHandled) {
66  throw new \RuntimeException(self::class . ' misconfiguration: ' . $this->table . ' does not exist in configuration', 1308354399);
67  }
68  return true;
69  }
70 
79  protected function ‪handleTable(‪$table, array $configuration)
80  {
81  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(‪$table);
82  $queryBuilder->delete(‪$table);
83  if (!empty($configuration['expireField'])) {
84  $field = $configuration['expireField'];
85  $dateLimit = ‪$GLOBALS['EXEC_TIME'];
86  // If expire field value is 0, do not delete
87  // Expire field = 0 means no expiration
88  $queryBuilder->where(
89  $queryBuilder->expr()->lte($field, $queryBuilder->createNamedParameter($dateLimit, \PDO::PARAM_INT)),
90  $queryBuilder->expr()->gt($field, $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
91  );
92  } elseif (!empty($configuration['dateField'])) {
93  if (!$this->allTables) {
94  $deleteTimestamp = strtotime('-' . $this->numberOfDays . 'days');
95  } else {
96  if (!isset($configuration['expirePeriod'])) {
97  throw new \RuntimeException(self::class . ' misconfiguration: No expirePeriod defined for table ' . ‪$table, 1308355095);
98  }
99  $deleteTimestamp = strtotime('-' . $configuration['expirePeriod'] . 'days');
100  }
101  $queryBuilder->where(
102  $queryBuilder->expr()->lt(
103  $configuration['dateField'],
104  $queryBuilder->createNamedParameter($deleteTimestamp, \PDO::PARAM_INT)
105  )
106  );
107  } else {
108  throw new \RuntimeException(self::class . ' misconfiguration: Either expireField or dateField must be defined for table ' . ‪$table, 1308355268);
109  }
110 
111  try {
112  $queryBuilder->execute();
113  } catch (DBALException $e) {
114  throw new \RuntimeException(self::class . ' failed for table ' . $this->table . ' with error: ' . $e->getMessage(), 1308255491);
115  }
116  return true;
117  }
118 
124  public function ‪getAdditionalInformation()
125  {
126  if ($this->allTables) {
127  $message = $this->‪getLanguageService()->‪sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.additionalInformationAllTables');
128  } else {
129  $message = sprintf($this->‪getLanguageService()->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.additionalInformationTable'), $this->table);
130  }
131  return $message;
132  }
133 }
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask
Definition: TableGarbageCollectionTask.php:33
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask\execute
‪bool execute()
Definition: TableGarbageCollectionTask.php:52
‪TYPO3\CMS\Scheduler\Task
Definition: AbstractTask.php:16
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask\getAdditionalInformation
‪string getAdditionalInformation()
Definition: TableGarbageCollectionTask.php:121
‪TYPO3\CMS\Core\Localization\LanguageService\sL
‪string sL($input)
Definition: LanguageService.php:194
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask\handleTable
‪bool handleTable($table, array $configuration)
Definition: TableGarbageCollectionTask.php:76
‪TYPO3\CMS\Scheduler\Task\AbstractTask\getLanguageService
‪LanguageService null getLanguageService()
Definition: AbstractTask.php:605
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:35
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask\$numberOfDays
‪int $numberOfDays
Definition: TableGarbageCollectionTask.php:40
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask\$table
‪string $table
Definition: TableGarbageCollectionTask.php:44
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask\$allTables
‪bool $allTables
Definition: TableGarbageCollectionTask.php:36
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46