[ Index ] |
PHP Cross Reference of MyBB |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * MyBB 1.6 4 * Copyright 2010 MyBB Group, All Rights Reserved 5 * 6 * Website: http://mybb.com 7 * License: http://mybb.com/about/license 8 * 9 * $Id: functions_task.php 5297 2010-12-28 22:01:14Z Tomm $ 10 */ 11 12 /** 13 * Execute a scheduled task. 14 * 15 * @param int The task ID. If none specified, the next task due to be ran is executed 16 * @return boolean True if successful, false on failure 17 */ 18 function run_task($tid=0) 19 { 20 global $db, $mybb, $cache, $plugins, $task, $lang; 21 22 // Run a specific task 23 if($tid > 0) 24 { 25 $query = $db->simple_select("tasks", "*", "tid='{$tid}'"); 26 $task = $db->fetch_array($query); 27 } 28 29 // Run the next task due to be run 30 else 31 { 32 $query = $db->simple_select("tasks", "*", "enabled=1 AND nextrun<='".TIME_NOW."'", array("order_by" => "nextrun", "order_dir" => "asc", "limit" => 1)); 33 $task = $db->fetch_array($query); 34 } 35 36 // No task? Return 37 if(!$task['tid']) 38 { 39 $cache->update_tasks(); 40 return false; 41 } 42 43 // Is this task still running and locked less than 5 minutes ago? Well don't run it now - clearly it isn't broken! 44 if($task['locked'] != 0 && $task['locked'] > TIME_NOW-300) 45 { 46 $cache->update_tasks(); 47 return false; 48 } 49 // Lock it! It' mine, all mine! 50 else 51 { 52 $db->update_query("tasks", array("locked" => TIME_NOW), "tid='{$task['tid']}'"); 53 } 54 55 // The task file does not exist 56 if(!file_exists(MYBB_ROOT."inc/tasks/{$task['file']}.php")) 57 { 58 if($task['logging'] == 1) 59 { 60 add_task_log($task, $lang->missing_task); 61 } 62 $cache->update_tasks(); 63 return false; 64 } 65 // Run the task 66 else 67 { 68 // Update the nextrun time now, so if the task causes a fatal error, it doesn't get stuck first in the queue 69 $nextrun = fetch_next_run($task); 70 $db->update_query("tasks", array("nextrun" => $nextrun), "tid='{$task['tid']}'"); 71 72 include_once MYBB_ROOT."inc/tasks/{$task['file']}.php"; 73 $function = "task_{$task['file']}"; 74 if(function_exists($function)) 75 { 76 $function($task); 77 } 78 } 79 80 $updated_task = array( 81 "lastrun" => TIME_NOW, 82 "locked" => 0 83 ); 84 $db->update_query("tasks", $updated_task, "tid='{$task['tid']}'"); 85 86 $cache->update_tasks(); 87 88 return true; 89 } 90 91 /** 92 * Adds information to the scheduled task log. 93 * 94 * @param int The task array to create the log entry for 95 * @param string The message to log 96 */ 97 function add_task_log($task, $message) 98 { 99 global $db; 100 101 if(!$task['logging']) 102 { 103 return; 104 } 105 106 $log_entry = array( 107 "tid" => intval($task['tid']), 108 "dateline" => TIME_NOW, 109 "data" => $db->escape_string($message) 110 ); 111 $db->insert_query("tasklog", $log_entry); 112 } 113 114 /** 115 * Generate the next run time for a particular task. 116 * 117 * @param array The task array as fetched from the database. 118 * @return int The next run time as a UNIX timestamp 119 */ 120 function fetch_next_run($task) 121 { 122 $time = TIME_NOW; 123 $next_minute = $current_minute = date("i", $time); 124 $next_hour = $current_hour = date("H", $time); 125 $next_day = $current_day = date("d", $time); 126 $next_weekday = $current_weekday = date("w", $time); 127 $next_month = $current_month = date("m", $time); 128 $next_year = $current_year = date("Y", $time); 129 130 if($task['minute'] == "*") 131 { 132 ++$next_minute; 133 if($next_minute > 59) 134 { 135 $reset_hour = 1; 136 $next_minute = 0; 137 } 138 } 139 else 140 { 141 if(build_next_run_bit($task['minute'], $current_minute) != false) 142 { 143 $next_minute = build_next_run_bit($task['minute'], $current_minute); 144 } 145 else 146 { 147 $next_minute = fetch_first_run_time($task['minute']); 148 } 149 if($next_minute <= $current_minute) 150 { 151 $reset_hour = 1; 152 } 153 } 154 155 if($reset_hour || !run_time_exists($task['hour'], $current_hour)) 156 { 157 if($task['hour'] == "*") 158 { 159 ++$next_hour; 160 if($next_hour > 23) 161 { 162 $reset_day = 1; 163 $next_hour = 0; 164 } 165 } 166 else 167 { 168 if(build_next_run_bit($task['hour'], $current_hour) != false) 169 { 170 $next_hour = build_next_run_bit($task['hour'], $current_hour); 171 } 172 else 173 { 174 $next_hour = fetch_first_run_time($task['hour']); 175 $reset_day = 1; 176 } 177 if($next_hour < $current_hour) 178 { 179 $reset_day = 1; 180 } 181 } 182 $next_minute = fetch_first_run_time($task['minute']); 183 } 184 185 if($reset_day || ($task['weekday'] == "*" && !run_time_exists($task['day'], $current_day) || $task['day'] == "*" && !run_time_exists($task['weekday'], $current_weekday))) 186 { 187 if($task['weekday'] == "*") 188 { 189 if($task['day'] == "*") 190 { 191 ++$next_day; 192 if($next_day > date("t", $time)) 193 { 194 $reset_month = 1; 195 $next_day = 1; 196 } 197 } 198 else 199 { 200 if(build_next_run_bit($task['day'], $current_day) != false) 201 { 202 $next_day = build_next_run_bit($task['day'], $current_day); 203 } 204 else 205 { 206 $next_day = fetch_first_run_time($task['day']); 207 $reset_month = 1; 208 } 209 if($next_day < $current_day) 210 { 211 $reset_month = 1; 212 } 213 } 214 } 215 else 216 { 217 if(build_next_run_bit($task['weekday'], $current_weekday) != false) 218 { 219 $next_weekday = build_next_run_bit($task['weekday'], $current_weekday); 220 } 221 else 222 { 223 $next_weekday = fetch_first_run_time($task['weekday']); 224 } 225 $next_day = $current_day + ($next_weekday-$current_weekday); 226 if($next_day <= $current_day) 227 { 228 $next_day += 7; 229 } 230 231 if($next_day > date("t", $time)) 232 { 233 $reset_month = 1; 234 } 235 } 236 $next_minute = fetch_first_run_time($task['minute']); 237 $next_hour = fetch_first_run_time($task['hour']); 238 if($next_day == $current_day && $next_hour < $current_hour) 239 { 240 $reset_month = 1; 241 } 242 } 243 244 if($reset_month || !run_time_exists($task['month'], $current_month)) 245 { 246 if($task['month'] == "*") 247 { 248 $next_month++; 249 if($next_month > 12) 250 { 251 $reset_year = 1; 252 $next_month = 1; 253 } 254 } 255 else 256 { 257 if(build_next_run_bit($task['month'], $current_month) != false) 258 { 259 $next_month = build_next_run_bit($task['month'], $current_month); 260 } 261 else 262 { 263 $next_month = fetch_first_run_time($task['month']); 264 $reset_year = 1; 265 } 266 if($next_month < $current_month) 267 { 268 $reset_year = 1; 269 } 270 } 271 $next_minute = fetch_first_run_time($task['minute']); 272 $next_hour = fetch_first_run_time($task['hour']); 273 if($task['weekday'] == "*") 274 { 275 $next_day = fetch_first_run_time($task['day']); 276 if($next_day == 0) $next_day = 1; 277 } 278 else 279 { 280 $next_weekday = fetch_first_run_time($task['weekday']); 281 $new_weekday = date("w", mktime($next_hour, $next_minute, 0, $next_month, 1, $next_year)); 282 $next_day = 1 + ($next_weekday-$new_weekday); 283 if($next_weekday < $new_weekday) 284 { 285 $next_day += 7; 286 } 287 } 288 if($next_month == $current_month && $next_day == $current_day && $new_hour < $current_hour) 289 { 290 $reset_year = 1; 291 } 292 } 293 294 if($reset_year) 295 { 296 $next_year++; 297 $next_minute = fetch_first_run_time($task['minute']); 298 $next_hour = fetch_first_run_time($task['hour']); 299 $next_month = fetch_first_run_time($task['month']); 300 if($next_month == 0) $next_month = 1; 301 if($task['weekday'] == "*") 302 { 303 $next_day = fetch_first_run_time($task['day']); 304 if($next_day == 0) $next_day = 1; 305 } 306 else 307 { 308 $next_weekday = fetch_first_run_time($task['weekday']); 309 $new_weekday = date("w", mktime($next_hour, $next_minute, 0, $next_month, 1, $next_year)); 310 $next_day = 1 + ($next_weekday-$new_weekday); 311 if($next_weekday < $new_weekday) 312 { 313 $next_day += 7; 314 } 315 } 316 } 317 return mktime($next_hour, $next_minute, 0, $next_month, $next_day, $next_year); 318 } 319 320 /** 321 * Builds the next run time bit for a particular item (day, hour, month etc). Used by fetch_next_run(). 322 * 323 * @param string A string containing the run timse for this particular item 324 * @param int The current value (be it current day etc) 325 * @return int The new or found value 326 */ 327 function build_next_run_bit($data, $bit) 328 { 329 if($data == "*") return $bit; 330 $data = explode(",", $data); 331 foreach($data as $thing) 332 { 333 if($thing > $bit) 334 { 335 return $thing; 336 } 337 } 338 return false; 339 } 340 341 /** 342 * Fetches the fist run bit for a particular item (day, hour, month etc). Used by fetch_next_run(). 343 * 344 * @param string A string containing the run times for this particular item 345 * @return int The first run time 346 */ 347 function fetch_first_run_time($data) 348 { 349 if($data == "*") return "0"; 350 $data = explode(",", $data); 351 return $data[0]; 352 } 353 354 /** 355 * Checks if a specific run time exists for a particular item (day, hour, month etc). Used by fetch_next_run(). 356 * 357 * @param string A string containing the run times for this particular item 358 * @param int The bit we're checking for 359 * @return boolean True if it exists, false if it does not 360 */ 361 function run_time_exists($data, $bit) 362 { 363 if($data == "*") return true; 364 $data = explode(",", $data); 365 if(in_array($bit, $data)) 366 { 367 return true; 368 } 369 return false; 370 } 371 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Oct 8 19:19:50 2013 | Cross-referenced by PHPXref 0.7.1 |