[TASK] Re-work/simplify copyright header in PHP files - Part 9
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Task / TableGarbageCollectionTask.php
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
17 /**
18 * Remove old entries from tables.
19 *
20 * This task deletes rows from tables older than the given number of days.
21 *
22 * Available tables must be registered in
23 * $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask']['options']['tables']
24 * See ext_localconf.php of scheduler extension for an example
25 *
26 * @author Christian Kuhn <lolli@schwarzbu.ch>
27 */
28 class TableGarbageCollectionTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask {
29
30 /**
31 * @var boolean True if all tables should be cleaned up
32 */
33 public $allTables = FALSE;
34
35 /**
36 * @var integer Number of days
37 */
38 public $numberOfDays = 180;
39
40 /**
41 * @var string Table to clean up
42 */
43 public $table = '';
44
45 /**
46 * Execute garbage collection, called by scheduler.
47 *
48 * @throws \RuntimeException if configured table was not cleaned up
49 * @return boolean TRUE if task run was successful
50 */
51 public function execute() {
52 $tableConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask']['options']['tables'];
53 $tableHandled = FALSE;
54 foreach ($tableConfigurations as $tableName => $configuration) {
55 if ($this->allTables || $tableName === $this->table) {
56 $this->handleTable($tableName, $configuration);
57 $tableHandled = TRUE;
58 }
59 }
60 if (!$tableHandled) {
61 throw new \RuntimeException('TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask misconfiguration: ' . $this->table . ' does not exist in configuration', 1308354399);
62 }
63 return TRUE;
64 }
65
66 /**
67 * Execute clean up of a specific table
68 *
69 * @throws \RuntimeException If table configuration is broken
70 * @param string $table The table to handle
71 * @param array $configuration Clean up configuration
72 * @return boolean TRUE if cleanup was successful
73 */
74 protected function handleTable($table, array $configuration) {
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 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 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 failed for table ' . $this->table . ' with error: ' . $error, 1308255491);
98 }
99 return TRUE;
100 }
101
102 /**
103 * This method returns the selected table as additional information
104 *
105 * @return string Information to display
106 */
107 public function getAdditionalInformation() {
108 if ($this->allTables) {
109 $message = $GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xlf:label.tableGarbageCollection.additionalInformationAllTables');
110 } else {
111 $message = sprintf($GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xlf:label.tableGarbageCollection.additionalInformationTable'), $this->table);
112 }
113 return $message;
114 }
115
116 }