TYPO3  7.6
TableGarbageCollectionTask.php
Go to the documentation of this file.
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 
27 {
31  public $allTables = false;
32 
36  public $numberOfDays = 180;
37 
41  public $table = '';
42 
49  public function execute()
50  {
51  $tableConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask::class]['options']['tables'];
52  $tableHandled = false;
53  foreach ($tableConfigurations as $tableName => $configuration) {
54  if ($this->allTables || $tableName === $this->table) {
55  $this->handleTable($tableName, $configuration);
56  $tableHandled = true;
57  }
58  }
59  if (!$tableHandled) {
60  throw new \RuntimeException(\TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask::class . ' misconfiguration: ' . $this->table . ' does not exist in configuration', 1308354399);
61  }
62  return true;
63  }
64 
73  protected function handleTable($table, array $configuration)
74  {
75  if (!empty($configuration['expireField'])) {
76  $field = $configuration['expireField'];
77  $dateLimit = $GLOBALS['EXEC_TIME'];
78  // If expire field value is 0, do not delete
79  // Expire field = 0 means no expiration
80  $where = $field . ' <= \'' . $dateLimit . '\' AND ' . $field . ' > \'0\'';
81  } elseif (!empty($configuration['dateField'])) {
82  if (!$this->allTables) {
83  $deleteTimestamp = strtotime('-' . $this->numberOfDays . 'days');
84  } else {
85  if (!isset($configuration['expirePeriod'])) {
86  throw new \RuntimeException(\TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask::class . ' misconfiguration: No expirePeriod defined for table ' . $table, 1308355095);
87  }
88  $deleteTimestamp = strtotime('-' . $configuration['expirePeriod'] . 'days');
89  }
90  $where = $configuration['dateField'] . ' < ' . $deleteTimestamp;
91  } else {
92  throw new \RuntimeException(\TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask::class . ' misconfiguration: Either expireField or dateField must be defined for table ' . $table, 1308355268);
93  }
94  $GLOBALS['TYPO3_DB']->exec_DELETEquery($table, $where);
95  $error = $GLOBALS['TYPO3_DB']->sql_error();
96  if ($error) {
97  throw new \RuntimeException(\TYPO3\CMS\Scheduler\Task\TableGarbageCollectionTask::class . ' failed for table ' . $this->table . ' with error: ' . $error, 1308255491);
98  }
99  return true;
100  }
101 
107  public function getAdditionalInformation()
108  {
109  if ($this->allTables) {
110  $message = $GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.additionalInformationAllTables');
111  } else {
112  $message = sprintf($GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.additionalInformationTable'), $this->table);
113  }
114  return $message;
115  }
116 }