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