[FEATURE] Add table garbage collection scheduler task
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / tasks / class.tx_scheduler_tablegarbagecollection.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2011 Christian Kuhn <lolli@schwarzbu.ch>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Remove old entries from tables.
27 *
28 * This task deletes rows from tables older than the given number of days.
29 *
30 * Available tables must be registered in
31 * $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['tx_scheduler_TableGarbageCollection']['options']['tables']
32 * See ext_localconf.php of scheduler extension for an example
33 *
34 * @author Christian Kuhn <lolli@schwarzbu.ch>
35 * @package TYPO3
36 * @subpackage scheduler
37 */
38 class tx_scheduler_TableGarbageCollection extends tx_scheduler_Task {
39
40 /**
41 * @var boolean True if all tables should be cleaned up
42 */
43 public $allTables = FALSE;
44
45 /**
46 * @var integer Number of days
47 */
48 public $numberOfDays = 180;
49
50 /**
51 * @var string Table to clean up
52 */
53 public $table = '';
54
55 /**
56 * Execute garbage collection, called by scheduler.
57 *
58 * @throws RuntimeException if configured table was not cleaned up
59 * @return boolean TRUE if task run was successful
60 */
61 public function execute() {
62 $tableConfigurations = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['tx_scheduler_TableGarbageCollection']['options']['tables'];
63 $tableHandled = FALSE;
64 foreach ($tableConfigurations as $tableName => $configuration) {
65 if ($this->allTables || $tableName === $this->table) {
66 $this->handleTable($tableName, $configuration);
67 $tableHandled = TRUE;
68 }
69 }
70
71 if (!$tableHandled) {
72 throw new RuntimeException(
73 'tx_scheduler_TableGarbageCollection misconfiguration: ' . $this->table . ' does not exist in configuration',
74 1308354399
75 );
76 }
77
78 return TRUE;
79 }
80
81 /**
82 * Execute clean up of a specific table
83 *
84 * @throws RuntimeException If table configuration is broken
85 * @param string $table The table to handle
86 * @param array $configuration Clean up configuration
87 * @return void
88 */
89 protected function handleTable($table, array $configuration) {
90 if (isset($configuration['expireField'])) {
91 $field = $configuration['expireField'];
92 $dateLimit = $GLOBALS['EXEC_TIME'];
93 // If expire field value is 0, do not delete
94 // Expire field = 0 means no expiration
95 $where = $field . " <= '" . $dateLimit . "' AND " . $field . " > '0'";
96 } elseif (isset($configuration['dateField'])) {
97 $field = $configuration['dateField'];
98 if (!$this->allTables) {
99 $deleteTimestamp = strtotime('-' . $this->numberOfDays . 'days');
100 } else {
101 if (!isset($configuration['expirePeriod'])) {
102 throw new RuntimeException(
103 'tx_scheduler_TableGarbageCollection misconfiguration: No expirePeriod defined for table ' . $table,
104 1308355095
105 );
106 }
107 $deleteTimestamp = strtotime('-' . $configuration['expirePeriod'] . 'days');
108 }
109 $where = $configuration['dateField'] . ' < ' . $deleteTimestamp;
110 } else {
111 throw new RuntimeException(
112 'tx_scheduler_TableGarbageCollection misconfiguration: Either expireField or dateField must be defined for table ' . $table,
113 1308355268
114 );
115 }
116
117 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
118 $table,
119 $where
120 );
121
122 $error = $GLOBALS['TYPO3_DB']->sql_error();
123 if ($error) {
124 throw new Exception(
125 'tx_scheduler_TableGarbageCollection failed for table ' . $this->table . ' with error: ' . $error,
126 1308255491
127 );
128 }
129
130 return TRUE;
131 }
132
133 /**
134 * This method returns the selected table as additional information
135 *
136 * @return string Information to display
137 */
138 public function getAdditionalInformation() {
139 if ($this->allTables) {
140 $message = $GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xml:label.tableGarbageCollection.additionalInformationAllTables');
141 } else {
142 $message = sprintf(
143 $GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xml:label.tableGarbageCollection.additionalInformationTable'),
144 $this->table
145 );
146 }
147
148 return $message;
149 }
150 }
151
152 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/scheduler/tasks/class.tx_scheduler_tablegarbagecollection.php'])) {
153 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/scheduler/tasks/class.tx_scheduler_tablegarbagecollection.php']);
154 }
155
156 ?>