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