[TASK] TYPO3 code must not use the else if construct
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / class.tx_scheduler_execution.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2005 Christian Jul Jensen (julle@typo3.org)
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 /**
27 * This class manages the logic of a particular execution of a task
28 *
29 * @author Fran├žois Suter <francois@typo3.org>
30 * @author Christian Jul Jensen <julle@typo3.org>
31 * @author Markus Friedrich <markus.friedrich@dkd.de>
32 *
33 * @package TYPO3
34 * @subpackage tx_scheduler
35 */
36 class tx_scheduler_Execution {
37
38 /**
39 * Start date of a task (timestamp)
40 *
41 * @var integer $start
42 */
43 protected $start;
44
45 /**
46 * End date of a task (timestamp)
47 *
48 * @var integer $end
49 */
50 protected $end;
51
52 /**
53 * Interval between executions (in seconds)
54 *
55 * @var integer $interval
56 */
57 protected $interval;
58
59 /**
60 * Flag for concurrent executions: true if allowed, false otherwise (default)
61 *
62 * @var boolean $multiple
63 */
64 protected $multiple = FALSE;
65
66 /**
67 * The cron command string of this task,
68 *
69 * @var string $cronCmd
70 */
71 protected $cronCmd;
72
73 /**
74 * This flag is used to mark a new single execution
75 * See explanations in method setIsNewSingleExecution()
76 *
77 * @var boolean $isNewSingleExecution
78 * @see tx_scheduler_Execution::setIsNewSingleExecution()
79 */
80 protected $isNewSingleExecution = FALSE;
81
82
83 /**********************************
84 * Setters and getters
85 **********************************/
86
87 /**
88 * This method is used to set the start date
89 *
90 * @param integer $start: start date (timestamp)
91 * @return void
92 */
93 public function setStart($start) {
94 $this->start = $start;
95 }
96
97 /**
98 * This method is used to get the start date
99 *
100 * @return integer start date (timestamp)
101 */
102 public function getStart() {
103 return $this->start;
104 }
105
106 /**
107 * This method is used to set the end date
108 *
109 * @param integer $end: end date (timestamp)
110 * @return void
111 */
112 public function setEnd($end) {
113 $this->end = $end;
114 }
115
116 /**
117 * This method is used to get the end date
118 *
119 * @return integer end date (timestamp)
120 */
121 public function getEnd() {
122 return $this->end;
123 }
124
125 /**
126 * This method is used to set the interval
127 *
128 * @param integer $interval: interval (in seconds)
129 * @return void
130 */
131 public function setInterval($interval) {
132 $this->interval = $interval;
133 }
134
135 /**
136 * This method is used to get the interval
137 *
138 * @return integer interval (in seconds)
139 */
140 public function getInterval() {
141 return $this->interval;
142 }
143
144 /**
145 * This method is used to set the multiple execution flag
146 *
147 * @param boolean $multiple: true if concurrent executions are allowed, false otherwise
148 * @return void
149 */
150 public function setMultiple($multiple) {
151 $this->multiple = $multiple;
152 }
153
154 /**
155 * This method is used to get the multiple execution flag
156 *
157 * @return boolean true if concurrent executions are allowed, false otherwise
158 */
159 public function getMultiple() {
160 return $this->multiple;
161 }
162
163 /**
164 * Set the value of the cron command
165 *
166 * @param string $cmd: cron command, using cron-like syntax
167 * @return void
168 */
169 public function setCronCmd($cmd) {
170 $this->cronCmd = $cmd;
171 }
172
173 /**
174 * Get the value of the cron command
175 *
176 * @return string cron command, using cron-like syntax
177 */
178 public function getCronCmd() {
179 return $this->cronCmd;
180 }
181
182 /**
183 * Set whether this is a newly created single execution.
184 * This is necessary for the following reason: if a new single-running task
185 * is created and its start date is in the past (even for only a few seconds),
186 * the next run time calculation (which happens upon saving) will disable
187 * that task, because it was meant to run only once and is in the past.
188 * Setting this flag to true preserves this task for a single run.
189 * Upon next execution, this flag is set to false.
190 *
191 * @param boolean Is newly created single execution?
192 * @return void
193 * @see tx_scheduler_Execution::getNextExecution()
194 */
195 public function setIsNewSingleExecution($isNewSingleExecution) {
196 $this->isNewSingleExecution = $isNewSingleExecution;
197 }
198
199 /**
200 * Get whether this is a newly created single execution
201 *
202 * @return boolean Is newly created single execution?
203 */
204 public function getIsNewSingleExecution() {
205 return $this->isNewSingleExecution;
206 }
207
208 /**********************************
209 * Execution calculations and logic
210 **********************************/
211
212 /**
213 * This method gets or calculates the next execution date
214 *
215 * @return integer Timestamp of the next execution
216 */
217 public function getNextExecution() {
218
219 if ($this->getIsNewSingleExecution()) {
220 $this->setIsNewSingleExecution(FALSE);
221 return $this->start;
222 }
223
224 if (!$this->isEnded()) {
225 // If the schedule has not yet run out, find out the next date
226
227 if (!$this->isStarted()) {
228 // If the schedule hasn't started yet, next date is start date
229 $date = $this->start;
230 } else {
231 // If the schedule has already started, calculate next date
232
233 if ($this->cronCmd) {
234 // If it uses cron-like syntax, calculate next date
235 $date = $this->getNextCronExecution();
236 } elseif ($this->interval == 0) {
237 // If not and there's no interval either, it's a singe execution: use start date
238 $date = $this->start;
239 } else {
240 // Otherwise calculate date based on interval
241 $now = time();
242 $date = $now + $this->interval - (($now - $this->start) % $this->interval);
243 }
244 // If date is in the future, throw an exception
245 if (!empty($this->end) && $date > $this->end) {
246 throw new OutOfBoundsException('Next execution date is past end date.', 1250715528);
247 }
248 }
249 } else {
250 // The event has ended, throw an exception
251 throw new OutOfBoundsException('Task is past end date.', 1250715544);
252 }
253
254 return $date;
255 }
256
257 /**
258 * Calulates the next execution from a cron command
259 *
260 * @return integer Next execution (timestamp)
261 */
262 public function getNextCronExecution() {
263 $cronCmd = t3lib_div::makeInstance('tx_scheduler_CronCmd', $this->getCronCmd());
264 $cronCmd->calculateNextValue();
265
266 return $cronCmd->getTimestamp();
267 }
268
269 /**
270 * Checks if the schedule for a task is started or not
271 *
272 * @return boolean True if the schedule is already active, false otherwise
273 */
274 public function isStarted() {
275 return $this->start < time();
276 }
277
278 /**
279 * Checks if the schedule for a task is passed or not
280 *
281 * @return boolean True if the schedule is not active anymore, false otherwise
282 */
283 public function isEnded() {
284 if (empty($this->end)) {
285 // If no end is defined, the schedule never ends
286 $result = FALSE;
287 } else {
288 // Otherwise check if end is in the past
289 $result = $this->end < time();
290 }
291
292 return $result;
293 }
294 }
295
296 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/scheduler/class.tx_scheduler_execution.php'])) {
297 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/scheduler/class.tx_scheduler_execution.php']);
298 }
299
300 ?>