‪TYPO3CMS  9.5
TableGarbageCollectionTask.php
Go to the documentation of this file.
1 <?php
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;
19 
31 {
35  public ‪$allTables = false;
36 
40  public ‪$numberOfDays = 180;
41 
45  public ‪$table = '';
46 
53  public function ‪execute()
54  {
55  $tableConfigurations = ‪$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][self::class]['options']['tables'];
56  $tableHandled = false;
57  foreach ($tableConfigurations as $tableName => $configuration) {
58  if ($this->allTables || $tableName === $this->table) {
59  $this->‪handleTable($tableName, $configuration);
60  $tableHandled = true;
61  }
62  }
63  if (!$tableHandled) {
64  throw new \RuntimeException(self::class . ' misconfiguration: ' . $this->table . ' does not exist in configuration', 1308354399);
65  }
66  return true;
67  }
68 
77  protected function ‪handleTable(‪$table, array $configuration)
78  {
79  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable(‪$table);
80  $queryBuilder->delete(‪$table);
81  if (!empty($configuration['expireField'])) {
82  $field = $configuration['expireField'];
83  $dateLimit = ‪$GLOBALS['EXEC_TIME'];
84  // If expire field value is 0, do not delete
85  // Expire field = 0 means no expiration
86  $queryBuilder->where(
87  $queryBuilder->expr()->lte($field, $queryBuilder->createNamedParameter($dateLimit, \PDO::PARAM_INT)),
88  $queryBuilder->expr()->gt($field, $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
89  );
90  } elseif (!empty($configuration['dateField'])) {
91  if (!$this->allTables) {
92  $deleteTimestamp = strtotime('-' . $this->numberOfDays . 'days');
93  } else {
94  if (!isset($configuration['expirePeriod'])) {
95  throw new \RuntimeException(self::class . ' misconfiguration: No expirePeriod defined for table ' . ‪$table, 1308355095);
96  }
97  $deleteTimestamp = strtotime('-' . $configuration['expirePeriod'] . 'days');
98  }
99  $queryBuilder->where(
100  $queryBuilder->expr()->lt(
101  $configuration['dateField'],
102  $queryBuilder->createNamedParameter($deleteTimestamp, \PDO::PARAM_INT)
103  )
104  );
105  } else {
106  throw new \RuntimeException(self::class . ' misconfiguration: Either expireField or dateField must be defined for table ' . ‪$table, 1308355268);
107  }
108 
109  try {
110  $queryBuilder->execute();
111  } catch (DBALException $e) {
112  throw new \RuntimeException(self::class . ' failed for table ' . $this->table . ' with error: ' . $e->getMessage(), 1308255491);
113  }
114  return true;
115  }
116 
122  public function ‪getAdditionalInformation()
123  {
124  if ($this->allTables) {
125  $message = ‪$GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.additionalInformationAllTables');
126  } else {
127  $message = sprintf(‪$GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.additionalInformationTable'), $this->table);
128  }
129  return $message;
130  }
131 }
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask
Definition: TableGarbageCollectionTask.php:31
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask\execute
‪bool execute()
Definition: TableGarbageCollectionTask.php:50
‪TYPO3\CMS\Scheduler\Task
Definition: AbstractTask.php:2
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask\getAdditionalInformation
‪string getAdditionalInformation()
Definition: TableGarbageCollectionTask.php:119
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask\handleTable
‪bool handleTable($table, array $configuration)
Definition: TableGarbageCollectionTask.php:74
‪TYPO3\CMS\Scheduler\Task\AbstractTask
Definition: AbstractTask.php:32
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask\$numberOfDays
‪int $numberOfDays
Definition: TableGarbageCollectionTask.php:38
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask\$table
‪string $table
Definition: TableGarbageCollectionTask.php:42
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask\$allTables
‪bool $allTables
Definition: TableGarbageCollectionTask.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45