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