[TASK] Re-work/simplify copyright header in PHP files - Part 9
[Packages/TYPO3.CMS.git] / typo3 / sysext / scheduler / Classes / Execution.php
1 <?php
2 namespace TYPO3\CMS\Scheduler;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 /**
17 * This class manages the logic of a particular execution of a task
18 *
19 * @author Fran├žois Suter <francois@typo3.org>
20 * @author Christian Jul Jensen <julle@typo3.org>
21 * @author Markus Friedrich <markus.friedrich@dkd.de>
22 */
23 class Execution {
24
25 /**
26 * Start date of a task (timestamp)
27 *
28 * @var integer $start
29 */
30 protected $start;
31
32 /**
33 * End date of a task (timestamp)
34 *
35 * @var integer $end
36 */
37 protected $end;
38
39 /**
40 * Interval between executions (in seconds)
41 *
42 * @var integer $interval
43 */
44 protected $interval;
45
46 /**
47 * Flag for concurrent executions: TRUE if allowed, FALSE otherwise (default)
48 *
49 * @var boolean $multiple
50 */
51 protected $multiple = FALSE;
52
53 /**
54 * The cron command string of this task,
55 *
56 * @var string $cronCmd
57 */
58 protected $cronCmd;
59
60 /**
61 * This flag is used to mark a new single execution
62 * See explanations in method setIsNewSingleExecution()
63 *
64 * @var boolean $isNewSingleExecution
65 * @see \TYPO3\CMS\Scheduler\Execution::setIsNewSingleExecution()
66 */
67 protected $isNewSingleExecution = FALSE;
68
69 /**********************************
70 * Setters and getters
71 **********************************/
72 /**
73 * This method is used to set the start date
74 *
75 * @param integer $start Start date (timestamp)
76 * @return void
77 */
78 public function setStart($start) {
79 $this->start = $start;
80 }
81
82 /**
83 * This method is used to get the start date
84 *
85 * @return integer Start date (timestamp)
86 */
87 public function getStart() {
88 return $this->start;
89 }
90
91 /**
92 * This method is used to set the end date
93 *
94 * @param integer $end End date (timestamp)
95 * @return void
96 */
97 public function setEnd($end) {
98 $this->end = $end;
99 }
100
101 /**
102 * This method is used to get the end date
103 *
104 * @return integer End date (timestamp)
105 */
106 public function getEnd() {
107 return $this->end;
108 }
109
110 /**
111 * This method is used to set the interval
112 *
113 * @param integer $interval Interval (in seconds)
114 * @return void
115 */
116 public function setInterval($interval) {
117 $this->interval = $interval;
118 }
119
120 /**
121 * This method is used to get the interval
122 *
123 * @return integer Interval (in seconds)
124 */
125 public function getInterval() {
126 return $this->interval;
127 }
128
129 /**
130 * This method is used to set the multiple execution flag
131 *
132 * @param boolean $multiple TRUE if concurrent executions are allowed, FALSE otherwise
133 * @return void
134 */
135 public function setMultiple($multiple) {
136 $this->multiple = $multiple;
137 }
138
139 /**
140 * This method is used to get the multiple execution flag
141 *
142 * @return boolean TRUE if concurrent executions are allowed, FALSE otherwise
143 */
144 public function getMultiple() {
145 return $this->multiple;
146 }
147
148 /**
149 * Set the value of the cron command
150 *
151 * @param string $cmd Cron command, using cron-like syntax
152 * @return void
153 */
154 public function setCronCmd($cmd) {
155 $this->cronCmd = $cmd;
156 }
157
158 /**
159 * Get the value of the cron command
160 *
161 * @return string Cron command, using cron-like syntax
162 */
163 public function getCronCmd() {
164 return $this->cronCmd;
165 }
166
167 /**
168 * Set whether this is a newly created single execution.
169 * This is necessary for the following reason: if a new single-running task
170 * is created and its start date is in the past (even for only a few seconds),
171 * the next run time calculation (which happens upon saving) will disable
172 * that task, because it was meant to run only once and is in the past.
173 * Setting this flag to TRUE preserves this task for a single run.
174 * Upon next execution, this flag is set to FALSE.
175 *
176 * @param boolean $isNewSingleExecution Is newly created single execution?
177 * @return void
178 * @see \TYPO3\CMS\Scheduler\Execution::getNextExecution()
179 */
180 public function setIsNewSingleExecution($isNewSingleExecution) {
181 $this->isNewSingleExecution = $isNewSingleExecution;
182 }
183
184 /**
185 * Get whether this is a newly created single execution
186 *
187 * @return boolean Is newly created single execution?
188 */
189 public function getIsNewSingleExecution() {
190 return $this->isNewSingleExecution;
191 }
192
193 /**********************************
194 * Execution calculations and logic
195 **********************************/
196 /**
197 * This method gets or calculates the next execution date
198 *
199 * @return integer Timestamp of the next execution
200 * @throws \OutOfBoundsException
201 */
202 public function getNextExecution() {
203 if ($this->getIsNewSingleExecution()) {
204 $this->setIsNewSingleExecution(FALSE);
205 return $this->start;
206 }
207 if (!$this->isEnded()) {
208 // If the schedule has not yet run out, find out the next date
209 if (!$this->isStarted()) {
210 // If the schedule hasn't started yet, next date is start date
211 $date = $this->start;
212 } else {
213 // If the schedule has already started, calculate next date
214 if ($this->cronCmd) {
215 // If it uses cron-like syntax, calculate next date
216 $date = $this->getNextCronExecution();
217 } elseif ($this->interval == 0) {
218 // If not and there's no interval either, it's a singe execution: use start date
219 $date = $this->start;
220 } else {
221 // Otherwise calculate date based on interval
222 $now = time();
223 $date = $now + $this->interval - ($now - $this->start) % $this->interval;
224 }
225 // If date is in the future, throw an exception
226 if (!empty($this->end) && $date > $this->end) {
227 throw new \OutOfBoundsException('Next execution date is past end date.', 1250715528);
228 }
229 }
230 } else {
231 // The event has ended, throw an exception
232 throw new \OutOfBoundsException('Task is past end date.', 1250715544);
233 }
234 return $date;
235 }
236
237 /**
238 * Calculates the next execution from a cron command
239 *
240 * @return integer Next execution (timestamp)
241 */
242 public function getNextCronExecution() {
243 /** @var $cronCmd \TYPO3\CMS\Scheduler\CronCommand\CronCommand */
244 $cronCmd = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Scheduler\\CronCommand\\CronCommand', $this->getCronCmd());
245 $cronCmd->calculateNextValue();
246 return $cronCmd->getTimestamp();
247 }
248
249 /**
250 * Checks if the schedule for a task is started or not
251 *
252 * @return boolean TRUE if the schedule is already active, FALSE otherwise
253 */
254 public function isStarted() {
255 return $this->start < time();
256 }
257
258 /**
259 * Checks if the schedule for a task is passed or not
260 *
261 * @return boolean TRUE if the schedule is not active anymore, FALSE otherwise
262 */
263 public function isEnded() {
264 if (empty($this->end)) {
265 // If no end is defined, the schedule never ends
266 $result = FALSE;
267 } else {
268 // Otherwise check if end is in the past
269 $result = $this->end < time();
270 }
271 return $result;
272 }
273
274 }