TYPO3 CMS  TYPO3_8-7
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  */
19 
30 {
34  public $allTables = false;
35 
39  public $numberOfDays = 180;
40 
44  public $table = '';
45 
52  public function execute()
53  {
54  $tableConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][self::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(self::class . ' misconfiguration: ' . $this->table . ' does not exist in configuration', 1308354399);
64  }
65  return true;
66  }
67 
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, $queryBuilder->createNamedParameter($dateLimit, \PDO::PARAM_INT)),
87  $queryBuilder->expr()->gt($field, $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT))
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(self::class . ' misconfiguration: No expirePeriod defined for table ' . $table, 1308355095);
95  }
96  $deleteTimestamp = strtotime('-' . $configuration['expirePeriod'] . 'days');
97  }
98  $queryBuilder->where(
99  $queryBuilder->expr()->lt(
100  $configuration['dateField'],
101  $queryBuilder->createNamedParameter($deleteTimestamp, \PDO::PARAM_INT)
102  )
103  );
104  } else {
105  throw new \RuntimeException(self::class . ' misconfiguration: Either expireField or dateField must be defined for table ' . $table, 1308355268);
106  }
107 
108  try {
109  $queryBuilder->execute();
110  } catch (DBALException $e) {
111  throw new \RuntimeException(self::class . ' failed for table ' . $this->table . ' with error: ' . $e->getMessage(), 1308255491);
112  }
113  return true;
114  }
115 
121  public function getAdditionalInformation()
122  {
123  if ($this->allTables) {
124  $message = $GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.additionalInformationAllTables');
125  } else {
126  $message = sprintf($GLOBALS['LANG']->sL('LLL:EXT:scheduler/Resources/Private/Language/locallang.xlf:label.tableGarbageCollection.additionalInformationTable'), $this->table);
127  }
128  return $message;
129  }
130 }
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']