[TASK] Wrong phpdoc in TableGarbageCollectionTask
[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-2013 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 /**
28 * Remove old entries from tables.
29 *
30 * This task deletes rows from tables older than the given number of days.
31 *
32 * Available tables must be registered in
33 * $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks']['TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask']['options']['tables']
34 * See ext_localconf.php of scheduler extension for an example
35 *
36 * @author Christian Kuhn <lolli@schwarzbu.ch>
37 */
38 class TableGarbageCollectionTask extends \TYPO3\CMS\Scheduler\Task\AbstractTask {
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']['TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask']['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 if (!$tableHandled) {
71 throw new \RuntimeException('TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask misconfiguration: ' . $this->table . ' does not exist in configuration', 1308354399);
72 }
73 return TRUE;
74 }
75
76 /**
77 * Execute clean up of a specific table
78 *
79 * @throws \RuntimeException If table configuration is broken
80 * @param string $table The table to handle
81 * @param array $configuration Clean up configuration
82 * @return boolean TRUE if cleanup was successful
83 */
84 protected function handleTable($table, array $configuration) {
85 if (!empty($configuration['expireField'])) {
86 $field = $configuration['expireField'];
87 $dateLimit = $GLOBALS['EXEC_TIME'];
88 // If expire field value is 0, do not delete
89 // Expire field = 0 means no expiration
90 $where = $field . ' <= \'' . $dateLimit . '\' AND ' . $field . ' > \'0\'';
91 } elseif (!empty($configuration['dateField'])) {
92 $field = $configuration['dateField'];
93 if (!$this->allTables) {
94 $deleteTimestamp = strtotime('-' . $this->numberOfDays . 'days');
95 } else {
96 if (!isset($configuration['expirePeriod'])) {
97 throw new \RuntimeException('TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask misconfiguration: No expirePeriod defined for table ' . $table, 1308355095);
98 }
99 $deleteTimestamp = strtotime('-' . $configuration['expirePeriod'] . 'days');
100 }
101 $where = $configuration['dateField'] . ' < ' . $deleteTimestamp;
102 } else {
103 throw new \RuntimeException('TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask misconfiguration: Either expireField or dateField must be defined for table ' . $table, 1308355268);
104 }
105 $GLOBALS['TYPO3_DB']->exec_DELETEquery($table, $where);
106 $error = $GLOBALS['TYPO3_DB']->sql_error();
107 if ($error) {
108 throw new \RuntimeException('TYPO3\\CMS\\Scheduler\\Task\\TableGarbageCollectionTask failed for table ' . $this->table . ' with error: ' . $error, 1308255491);
109 }
110 return TRUE;
111 }
112
113 /**
114 * This method returns the selected table as additional information
115 *
116 * @return string Information to display
117 */
118 public function getAdditionalInformation() {
119 if ($this->allTables) {
120 $message = $GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xlf:label.tableGarbageCollection.additionalInformationAllTables');
121 } else {
122 $message = sprintf($GLOBALS['LANG']->sL('LLL:EXT:scheduler/mod1/locallang.xlf:label.tableGarbageCollection.additionalInformationTable'), $this->table);
123 }
124 return $message;
125 }
126
127 }