MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[moodle.git] / admin / cron.php
CommitLineData
fce3ef94 1<?php // $Id$
f9903ed0 2
540bab47 3/// This script looks through all the module directories for cron.php files
4/// and runs them. These files can contain cleanup functions, email functions
5/// or anything that needs to be run on a regular basis.
6///
7/// This file is best run from cron on the host system (ie outside PHP).
8/// The script can either be invoked via the web server or via a standalone
9/// version of PHP compiled for CGI.
10///
11/// eg wget -q -O /dev/null 'http://moodle.somewhere.edu/admin/cron.php'
12/// or php /web/moodle/admin/cron.php
168df8f3 13 set_time_limit(0);
498d4032 14 $starttime = microtime();
f9f4d999 15
a91b910e 16/// this cron script might be considered to be a CLI script even when accessed over HTTP,
17/// we do not want HTML in output and there is no real session ;-)
18 define('CLI_SCRIPT', true);
19
11e7b506 20/// Following hack used to identify cron and other CLI scripts - use CLI_SCRIPT or $FULLME == "/$CFG->admin/cropn.php" instead
21 //define('FULLME', 'cron');
9152fc99 22
23/// Do not set moodle cookie because we do not need it here, it is better to emulate session
6800d78e 24 define('NO_MOODLE_COOKIES', true);
9152fc99 25
331ca297 26 require_once(dirname(__FILE__) . '/../config.php');
6e4dc10f 27 require_once($CFG->libdir.'/adminlib.php');
2650c51e 28 require_once($CFG->libdir.'/gradelib.php');
f9903ed0 29
63a11f3c 30/// Extra debugging (set in config.php)
31 if (!empty($CFG->showcronsql)) {
f33e1ed4 32 $DB->set_debug(true);
63a11f3c 33 }
34 if (!empty($CFG->showcrondebugging)) {
35 $CFG->debug = DEBUG_DEVELOPER;
7f51af40 36 $CFG->debugdisplay = true;
63a11f3c 37 }
38
9152fc99 39/// extra safety
dd9e22f8 40 session_get_instance()->write_close();
9152fc99 41
42/// check if execution allowed
e08df351 43 if (isset($_SERVER['REMOTE_ADDR'])) { // if the script is accessed via the web.
44 if (!empty($CFG->cronclionly)) {
45 // This script can only be run via the cli.
46 print_error('cronerrorclionly', 'admin');
47 exit;
48 }
49 // This script is being called via the web, so check the password if there is one.
50 if (!empty($CFG->cronremotepassword)) {
51 $pass = optional_param('password', '', PARAM_RAW);
52 if($pass != $CFG->cronremotepassword) {
53 // wrong password.
54 print_error('cronerrorpassword', 'admin');
55 exit;
56 }
57 }
58 }
59
afabdb14 60
9152fc99 61/// emulate normal session
e8b7114d 62 cron_setup_user();
5e601222 63
64/// send mime type and encoding
58e8f85b 65 if (check_browser_version('MSIE')) {
66 //ugly IE hack to work around downloading instead of viewing
810944af 67 @header('Content-Type: text/html; charset=utf-8');
58e8f85b 68 echo "<xmp>"; //<pre> is not good enough for us here
69 } else {
70 //send proper plaintext header
810944af 71 @header('Content-Type: text/plain; charset=utf-8');
58e8f85b 72 }
5e601222 73
9152fc99 74/// no more headers and buffers
75 while(@ob_end_flush());
76
4eefee97 77/// increase memory limit (PHP 5.2 does different calculation, we need more memory now)
78 @raise_memory_limit('128M');
79
428b6fec 80/// Start output log
f9903ed0 81
f9903ed0 82 $timenow = time();
428b6fec 83
defaac4c 84 mtrace("Server Time: ".date('r',$timenow)."\n\n");
f9903ed0 85
dd9e22f8 86
87/// Session gc
88
89 mtrace("Cleaning up stale sessions");
e8656bef 90 session_gc();
dd9e22f8 91
b2ce450a 92/// Run all cron jobs for each module
c88f014a 93
defaac4c 94 mtrace("Starting activity modules");
c2f6fe2f 95 get_mailer('buffer');
953d72db 96 if ($mods = $DB->get_records_select("modules", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) {
f9903ed0 97 foreach ($mods as $mod) {
673de292 98 $libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
99 if (file_exists($libfile)) {
100 include_once($libfile);
101 $cron_function = $mod->name."_cron";
102 if (function_exists($cron_function)) {
defaac4c 103 mtrace("Processing module function $cron_function ...", '');
d9b06327 104 $pre_dbqueries = null;
ec22e380 105 $pre_dbqueries = $DB->perf_get_queries();
ab130a0b 106 $pre_time = microtime(1);
673de292 107 if ($cron_function()) {
953d72db 108 if (!$DB->set_field("modules", "lastcron", $timenow, array("id"=>$mod->id))) {
defaac4c 109 mtrace("Error: could not update timestamp for $mod->fullname");
673de292 110 }
111 }
ff991c05 112 if (isset($pre_dbqueries)) {
ec22e380 113 mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries");
ff991c05 114 mtrace("... used " . (microtime(1) - $pre_time) . " seconds");
115 }
5e867b23 116 /// Reset possible changes by modules to time_limit. MDL-11597
117 @set_time_limit(0);
defaac4c 118 mtrace("done.");
f9903ed0 119 }
120 }
121 }
122 }
c2f6fe2f 123 get_mailer('close');
defaac4c 124 mtrace("Finished activity modules");
c88f014a 125
f9648e77 126 mtrace("Starting blocks");
953d72db 127 if ($blocks = $DB->get_records_select("block", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) {
f9648e77 128 // we will need the base class.
129 require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
130 foreach ($blocks as $block) {
131 $blockfile = $CFG->dirroot.'/blocks/'.$block->name.'/block_'.$block->name.'.php';
132 if (file_exists($blockfile)) {
133 require_once($blockfile);
134 $classname = 'block_'.$block->name;
135 $blockobj = new $classname;
136 if (method_exists($blockobj,'cron')) {
137 mtrace("Processing cron function for ".$block->name.'....','');
138 if ($blockobj->cron()) {
953d72db 139 if (!$DB->set_field('block', 'lastcron', $timenow, array('id'=>$block->id))) {
f9648e77 140 mtrace('Error: could not update timestamp for '.$block->name);
141 }
142 }
44853b81 143 /// Reset possible changes by blocks to time_limit. MDL-11597
144 @set_time_limit(0);
f9648e77 145 mtrace('done.');
146 }
147 }
148
149 }
150 }
0616d3e8 151 mtrace('Finished blocks');
f9648e77 152
17f1782c 153 mtrace("Starting quiz reports");
154 if ($reports = $DB->get_records_select('quiz_report', "cron > 0 AND ((? - lastcron) > cron)", array($timenow))) {
155 foreach ($reports as $report) {
156 $cronfile = "$CFG->dirroot/mod/quiz/report/$report->name/cron.php";
157 if (file_exists($cronfile)) {
158 include_once($cronfile);
159 $cron_function = 'quiz_report_'.$report->name."_cron";
160 if (function_exists($cron_function)) {
161 mtrace("Processing quiz report cron function $cron_function ...", '');
162 $pre_dbqueries = null;
163 $pre_dbqueries = $DB->perf_get_queries();
164 $pre_time = microtime(1);
165 if ($cron_function()) {
166 if (!$DB->set_field('quiz_report', "lastcron", $timenow, array("id"=>$report->id))) {
167 mtrace("Error: could not update timestamp for $report->name");
168 }
169 }
170 if (isset($pre_dbqueries)) {
171 mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries");
172 mtrace("... used " . (microtime(1) - $pre_time) . " seconds");
173 }
174 mtrace("done.");
175 }
176 }
177 }
178 }
179 mtrace("Finished quiz reports");
180
d9b06327 181 mtrace('Starting admin reports');
182 // Admin reports do not have a database table that lists them. Instead a
183 // report includes cron.php with function report_reportname_cron() if it wishes
184 // to be cronned. It is up to cron.php to handle e.g. if it only needs to
185 // actually do anything occasionally.
17da2e6f 186 $reports = get_plugin_list('report');
187 foreach($reports as $report => $reportdir) {
188 $cronfile = $reportdir.'/cron.php';
d9b06327 189 if (file_exists($cronfile)) {
190 require_once($cronfile);
40ad295c 191 $cronfunction = 'report_'.$report.'_cron';
d9b06327 192 mtrace('Processing cron function for '.$report.'...', '');
193 $pre_dbqueries = null;
ec22e380 194 $pre_dbqueries = $DB->perf_get_queries();
ab130a0b 195 $pre_time = microtime(true);
d9b06327 196 $cronfunction();
197 if (isset($pre_dbqueries)) {
ec22e380 198 mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries");
d9b06327 199 mtrace("... used " . round(microtime(true) - $pre_time, 2) . " seconds");
200 }
201 mtrace('done.');
202 }
203 }
204 mtrace('Finished admin reports');
205
d6ead3a2 206 if (!empty($CFG->langcache)) {
207 mtrace('Updating languages cache');
0a5ce9dd 208 get_list_of_languages(true);
d6ead3a2 209 }
210
0616d3e8 211 mtrace('Removing expired enrolments ...', ''); // See MDL-8785
212 $timenow = time();
cf057534 213 $somefound = false;
3dce78e1 214 // The preferred way saves memory, datablib
cf057534 215 // find courses where limited enrolment is enabled
953d72db 216 $sql = "SELECT ra.roleid, ra.userid, ra.contextid
217 FROM {course} c
218 JOIN {context} cx ON cx.instanceid = c.id
219 JOIN {role_assignments} ra ON ra.contextid = cx.id
220 WHERE cx.contextlevel = '".CONTEXT_COURSE."'
221 AND ra.timeend > 0
222 AND ra.timeend < ?
223 AND c.enrolperiod > 0";
224 if ($rs = $DB->get_recordset_sql($sql, array($timenow))) {
225 foreach ($rs as $oldenrolment) {
226 role_unassign($oldenrolment->roleid, $oldenrolment->userid, 0, $oldenrolment->contextid);
227 $somefound = true;
228 }
229 $rs->close();
cf057534 230 }
953d72db 231 if ($somefound) {
0616d3e8 232 mtrace('Done');
233 } else {
234 mtrace('none found');
235 }
236
d6ead3a2 237
d9b06327 238
1ee0df06 239 mtrace('Starting main gradebook job ...');
2650c51e 240 grade_cron();
1ee0df06 241 mtrace('done.');
2650c51e 242
243
a5d56dc1 244 mtrace('Starting processing the event queue...');
245 events_cron();
246 mtrace('done.');
247
90658eef 248 if ($CFG->enableportfolios) {
7e51bbce 249 // Portfolio cron
250 mtrace('Starting the portfolio cron...');
251 require_once($CFG->libdir . '/portfoliolib.php');
252 portfolio_cron();
253 mtrace('done');
254 }
a5d56dc1 255
b2ce450a 256/// Run all core cron jobs, but not every time since they aren't too important.
257/// These don't have a timer to reduce load, so we'll use a random number
258/// to randomly choose the percentage of times we should run these jobs.
c88f014a 259
b2ce450a 260 srand ((double) microtime() * 10000000);
261 $random100 = rand(0,100);
c88f014a 262
b2ce450a 263 if ($random100 < 20) { // Approximately 20% of the time.
defaac4c 264 mtrace("Running clean-up tasks...");
b2ce450a 265
266 /// Unenrol users who haven't logged in for $CFG->longtimenosee
267
268 if ($CFG->longtimenosee) { // value in days
fce3ef94 269 $cuttime = $timenow - ($CFG->longtimenosee * 3600 * 24);
953d72db 270 $rs = $DB->get_recordset_sql ("SELECT id, userid, courseid
271 FROM {user_lastaccess}
272 WHERE courseid != ".SITEID."
273 AND timeaccess < ?", array($cuttime));
274 foreach ($rs as $assign) {
fce3ef94 275 if ($context = get_context_instance(CONTEXT_COURSE, $assign->courseid)) {
276 if (role_unassign(0, $assign->userid, 0, $context->id)) {
ac7658aa 277 mtrace("removing user $assign->userid from course $assign->courseid as they have not accessed the course for over $CFG->longtimenosee days");
b2ce450a 278 }
279 }
280 }
953d72db 281 $rs->close();
d9420631 282 /// Execute the same query again, looking for remaining records and deleting them
283 /// if the user hasn't moodle/course:view in the CONTEXT_COURSE context (orphan records)
953d72db 284 $rs = $DB->get_recordset_sql ("SELECT id, userid, courseid
285 FROM {user_lastaccess}
286 WHERE courseid != ".SITEID."
287 AND timeaccess < ?", array($cuttime));
288 foreach ($rs as $assign) {
d9420631 289 if ($context = get_context_instance(CONTEXT_COURSE, $assign->courseid)) {
290 if (!has_capability('moodle/course:view', $context, $assign->userid)) {
953d72db 291 $DB->delete_records('user_lastaccess', array('userid'=>$assign->userid, 'courseid'=>$assign->courseid));
d9420631 292 mtrace("Deleted orphan user_lastaccess for user $assign->userid from course $assign->courseid");
293 }
294 }
295 }
953d72db 296 $rs->close();
b2ce450a 297 }
fce3ef94 298 flush();
299
300
6c61fd04 301 /// Delete users who haven't confirmed within required period
c6c43ae9 302
8c182006 303 if (!empty($CFG->deleteunconfirmed)) {
fce3ef94 304 $cuttime = $timenow - ($CFG->deleteunconfirmed * 3600);
953d72db 305 $rs = $DB->get_recordset_sql ("SELECT id, firstname, lastname
306 FROM {user}
307 WHERE confirmed = 0 AND firstaccess > 0
308 AND firstaccess < ?", array($cuttime));
309 foreach ($rs as $user) {
310 if ($DB->delete_records('user', array('id'=>$user->id))) {
fce3ef94 311 mtrace("Deleted unconfirmed user for ".fullname($user, true)." ($user->id)");
f282e631 312 }
6cb86b39 313 }
953d72db 314 $rs->close();
6cb86b39 315 }
69f78bcb 316 flush();
7b8cc339 317
318
7b8cc339 319 /// Delete users who haven't completed profile within required period
320
a4a57da9 321 if (!empty($CFG->deleteincompleteusers)) {
322 $cuttime = $timenow - ($CFG->deleteincompleteusers * 3600);
953d72db 323 $rs = $DB->get_recordset_sql ("SELECT id, username
324 FROM {user}
325 WHERE confirmed = 1 AND lastaccess > 0
326 AND lastaccess < ? AND deleted = 0
327 AND (lastname = '' OR firstname = '' OR email = '')",
328 array($cuttime));
329 foreach ($rs as $user) {
a4a57da9 330 if (delete_user($user)) {
fce3ef94 331 mtrace("Deleted not fully setup user $user->username ($user->id)");
7b8cc339 332 }
333 }
953d72db 334 $rs->close();
7b8cc339 335 }
336 flush();
337
338
b2ce450a 339 /// Delete old logs to save space (this might need a timer to slow it down...)
fce3ef94 340
b2ce450a 341 if (!empty($CFG->loglifetime)) { // value in days
342 $loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24);
953d72db 343 if ($DB->delete_records_select("log", "time < ?", array($loglifetime))) {
fce3ef94 344 mtrace("Deleted old log records");
345 }
b2ce450a 346 }
69f78bcb 347 flush();
cb3a4484 348
fce3ef94 349
cb3a4484 350 /// Delete old cached texts
351
352 if (!empty($CFG->cachetext)) { // Defined in config.php
a9743837 353 $cachelifetime = time() - $CFG->cachetext - 60; // Add an extra minute to allow for really heavy sites
953d72db 354 if ($DB->delete_records_select('cache_text', "timemodified < ?", array($cachelifetime))) {
fce3ef94 355 mtrace("Deleted old cache_text records");
356 }
cb3a4484 357 }
69f78bcb 358 flush();
d846e50a 359
360 if (!empty($CFG->notifyloginfailures)) {
361 notify_login_failures();
f1dc7614 362 mtrace('Notified login failured');
d846e50a 363 }
69f78bcb 364 flush();
d846e50a 365
b61efafb 366 sync_metacourses();
f1dc7614 367 mtrace('Synchronised metacourses');
b61efafb 368
e3b6b2b0 369 //
370 // generate new password emails for users
371 //
372 mtrace('checking for create_password');
953d72db 373 if ($DB->count_records('user_preferences', array('name'=>'create_password', 'value'=>'1'))) {
e3b6b2b0 374 mtrace('creating passwords for new users');
953d72db 375 $newusers = $DB->get_records_sql("SELECT u.id as id, u.email, u.firstname,
376 u.lastname, u.username,
377 p.id as prefid
378 FROM {user} u
379 JOIN {user_preferences} p ON u.id=p.userid
32426545 380 WHERE p.name='create_password' AND p.value='1' AND u.email !='' ");
e3b6b2b0 381
382 foreach ($newusers as $newuserid => $newuser) {
383 $newuser->emailstop = 0; // send email regardless
384 // email user
385 if (setnew_password_and_mail($newuser)) {
386 // remove user pref
953d72db 387 $DB->delete_records('user_preferences', array('id'=>$newuser->prefid));
e3b6b2b0 388 } else {
389 trigger_error("Could not create and mail new user password!");
390 }
391 }
392 }
7ea9b095 393
2cb5f03a 394 if (!empty($CFG->usetags)) {
19e41f75 395 require_once($CFG->dirroot.'/tag/lib.php');
396 tag_cron();
f1dc7614 397 mtrace ('Executed tag cron');
19e41f75 398 }
399
ad72d0d7 400 // Accesslib stuff
401 cleanup_contexts();
f1dc7614 402 mtrace ('Cleaned up contexts');
fce3ef94 403 gc_cache_flags();
f1dc7614 404 mtrace ('Cleaned cache flags');
ad72d0d7 405 // If you suspect that the context paths are somehow corrupt
406 // replace the line below with: build_context_path(true);
407 build_context_path();
f1dc7614 408 mtrace ('Built context paths');
409
410 mtrace("Finished clean-up tasks...");
ad72d0d7 411
d846e50a 412 } // End of occasional clean-up tasks
540bab47 413
750eeaeb 414
50999a0b 415 if (empty($CFG->disablescheduledbackups)) { // Defined in config.php
74736e49 416 //Execute backup's cron
417 //Perhaps a long time and memory could help in large sites
a71bfa1c 418 @set_time_limit(0);
8891e81c 419 @raise_memory_limit("192M");
a2ce7344 420 if (function_exists('apache_child_terminate')) {
421 // if we are running from Apache, give httpd a hint that
422 // it can recycle the process after it's done. Apache's
423 // memory management is truly awful but we can help it.
424 @apache_child_terminate();
425 }
74736e49 426 if (file_exists("$CFG->dirroot/backup/backup_scheduled.php") and
427 file_exists("$CFG->dirroot/backup/backuplib.php") and
e91e2b6a 428 file_exists("$CFG->dirroot/backup/lib.php") and
429 file_exists("$CFG->libdir/blocklib.php")) {
74736e49 430 include_once("$CFG->dirroot/backup/backup_scheduled.php");
431 include_once("$CFG->dirroot/backup/backuplib.php");
432 include_once("$CFG->dirroot/backup/lib.php");
defaac4c 433 mtrace("Running backups if required...");
74736e49 434
435 if (! schedule_backup_cron()) {
04ddd06c 436 mtrace("ERROR: Something went wrong while performing backup tasks!!!");
74736e49 437 } else {
defaac4c 438 mtrace("Backup tasks finished.");
74736e49 439 }
e6d273eb 440 }
d3159192 441 }
442
8adcb49f 443 if (!empty($CFG->enablerssfeeds)) { //Defined in admin/variables page
3c02d134 444 include_once("$CFG->libdir/rsslib.php");
445 mtrace("Running rssfeeds if required...");
8adcb49f 446
3c02d134 447 if ( ! cron_rss_feeds()) {
448 mtrace("Something went wrong while generating rssfeeds!!!");
449 } else {
450 mtrace("Rssfeeds finished");
8adcb49f 451 }
452 }
453
d14383e0 454/// Run the auth cron, if any
1619d109 455/// before enrolments because it might add users that will be needed in enrol plugins
c7b10b5f 456 $auths = get_enabled_auth_plugins();
457
d14383e0 458 mtrace("Running auth crons if required...");
459 foreach ($auths as $auth) {
460 $authplugin = get_auth_plugin($auth);
461 if (method_exists($authplugin, 'cron')) {
462 mtrace("Running cron for auth/$auth...");
463 $authplugin->cron();
464 if (!empty($authplugin->log)) {
465 mtrace($authplugin->log);
466 }
467 }
468 unset($authplugin);
469 }
470
1619d109 471/// Run the enrolment cron, if any
472 if (!($plugins = explode(',', $CFG->enrol_plugins_enabled))) {
473 $plugins = array($CFG->enrol);
474 }
475 require_once($CFG->dirroot .'/enrol/enrol.class.php');
476 foreach ($plugins as $p) {
477 $enrol = enrolment_factory::factory($p);
478 if (method_exists($enrol, 'cron')) {
479 $enrol->cron();
480 }
481 if (!empty($enrol->log)) {
482 mtrace($enrol->log);
483 }
484 unset($enrol);
485 }
486
72df7e1e 487 if (!empty($CFG->enablestats) and empty($CFG->disablestatsprocessing)) {
61460dd6 488 require_once($CFG->dirroot.'/lib/statslib.php');
4900433c 489 // check we're not before our runtime
61460dd6 490 $timetocheck = stats_get_base_daily() + $CFG->statsruntimestarthour*60*60 + $CFG->statsruntimestartminute*60;
4900433c 491
492 if (time() > $timetocheck) {
5e7206a8 493 // process configured number of days as max (defaulting to 31)
494 $maxdays = empty($CFG->statsruntimedays) ? 31 : abs($CFG->statsruntimedays);
495 if (stats_cron_daily($maxdays)) {
61460dd6 496 if (stats_cron_weekly()) {
497 if (stats_cron_monthly()) {
498 stats_clean_old();
499 }
4900433c 500 }
f3221af9 501 }
61460dd6 502 @set_time_limit(0);
6fdde1d2 503 } else {
5a4b2e8e 504 mtrace('Next stats run after:'. userdate($timetocheck));
f3221af9 505 }
506 }
1ee0df06 507
ce34ed3a 508 // run gradebook import/export/report cron
17da2e6f 509 if ($gradeimports = get_plugin_list('gradeimport')) {
510 foreach ($gradeimports as $gradeimport => $plugindir) {
511 if (file_exists($plugindir.'/lib.php')) {
512 require_once($plugindir.'/lib.php');
1ee0df06 513 $cron_function = 'grade_import_'.$gradeimport.'_cron';
ce34ed3a 514 if (function_exists($cron_function)) {
515 mtrace("Processing gradebook import function $cron_function ...", '');
516 $cron_function;
517 }
518 }
519 }
520 }
521
17da2e6f 522 if ($gradeexports = get_plugin_list('gradeexport')) {
523 foreach ($gradeexports as $gradeexport => $plugindir) {
524 if (file_exists($plugindir.'/lib.php')) {
525 require_once($plugindir.'/lib.php');
1ee0df06 526 $cron_function = 'grade_export_'.$gradeexport.'_cron';
ce34ed3a 527 if (function_exists($cron_function)) {
528 mtrace("Processing gradebook export function $cron_function ...", '');
529 $cron_function;
530 }
531 }
532 }
533 }
1ee0df06 534
17da2e6f 535 if ($gradereports = get_plugin_list('gradereport')) {
536 foreach ($gradereports as $gradereport => $plugindir) {
537 if (file_exists($plugindir.'/lib.php')) {
538 require_once($plugindir.'/lib.php');
1ee0df06 539 $cron_function = 'grade_report_'.$gradereport.'_cron';
ce34ed3a 540 if (function_exists($cron_function)) {
541 mtrace("Processing gradebook report function $cron_function ...", '');
542 $cron_function;
543 }
544 }
545 }
546 }
cae83708 547
548 // Run external blog cron if needed
549 if ($CFG->useexternalblogs) {
550 require_once($CFG->dirroot . '/blog/lib.php');
ec22ccd5
NC
551 mtrace("Fetching external blog entries...", '');
552 $sql = "timefetched < ? - ? OR timefetched = 0";
553 $externalblogs = $DB->get_records_select('blog_external', $sql, array(mktime(), $CFG->externalblogcrontime));
cae83708 554
555 foreach ($external_blogs as $eb) {
ec22ccd5 556 blog_sync_external_entries($eb);
cae83708 557 }
558 }
1aa01caf 559
ec22ccd5
NC
560 // Run blog associations cleanup
561 if ($CFG->useblogassociations) {
562 require_once($CFG->dirroot . '/blog/lib.php');
563 // delete entries whose contextids no longer exists
564 mtrace("Deleting blog associations linked to non-existent contexts...", '');
565 $DB->delete_records_select('blog_association', 'contextid NOT IN (SELECT id FROM {context})');
566 }
567
1aa01caf 568 // cleanup file trash
569 $fs = get_file_storage();
570 $fs->cron();
571
ba9b44f9 572 // run any customized cronjobs, if any
17da2e6f 573 if ($locals = get_plugin_list('local')) {
574 mtrace('Processing customized cron scripts ...', '');
575 foreach ($locals as $local => $localdir) {
576 if (file_exists("$localdir/cron.php")) {
577 include("$localdir/cron.php");
578 }
579 }
ba9b44f9 580 mtrace('done.');
581 }
582
f3221af9 583
defaac4c 584 mtrace("Cron script completed correctly");
f9903ed0 585
498d4032 586 $difftime = microtime_diff($starttime, microtime());
defaac4c 587 mtrace("Execution took ".$difftime." seconds");
498d4032 588
28a082f1 589/// finish the IE hack
58e8f85b 590 if (check_browser_version('MSIE')) {
591 echo "</xmp>";
592 }
593
1afb11e7 594?>