MDL-29509 add cron support for admin tools
[moodle.git] / lib / cronlib.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * Cron functions.
20  *
21  * @package    core
22  * @subpackage admin
23  * @copyright  1999 onwards Martin Dougiamas  http://dougiamas.com
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
27 function cron_run() {
28     global $DB, $CFG, $OUTPUT;
30     if (CLI_MAINTENANCE) {
31         echo "CLI maintenance mode active, cron execution suspended.\n";
32         exit(1);
33     }
35     if (moodle_needs_upgrading()) {
36         echo "Moodle upgrade pending, cron execution suspended.\n";
37         exit(1);
38     }
40     require_once($CFG->libdir.'/adminlib.php');
41     require_once($CFG->libdir.'/gradelib.php');
43     if (!empty($CFG->showcronsql)) {
44         $DB->set_debug(true);
45     }
46     if (!empty($CFG->showcrondebugging)) {
47         $CFG->debug = DEBUG_DEVELOPER;
48         $CFG->debugdisplay = true;
49     }
51     set_time_limit(0);
52     $starttime = microtime();
54 /// increase memory limit
55     raise_memory_limit(MEMORY_EXTRA);
57 /// emulate normal session
58     cron_setup_user();
60 /// Start output log
62     $timenow  = time();
64     mtrace("Server Time: ".date('r',$timenow)."\n\n");
67 /// Session gc
69     mtrace("Cleaning up stale sessions");
70     session_gc();
72 /// Run all cron jobs for each module
74     mtrace("Starting activity modules");
75     get_mailer('buffer');
76     if ($mods = $DB->get_records_select("modules", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) {
77         foreach ($mods as $mod) {
78             $libfile = "$CFG->dirroot/mod/$mod->name/lib.php";
79             if (file_exists($libfile)) {
80                 include_once($libfile);
81                 $cron_function = $mod->name."_cron";
82                 if (function_exists($cron_function)) {
83                     mtrace("Processing module function $cron_function ...", '');
84                     $pre_dbqueries = null;
85                     $pre_dbqueries = $DB->perf_get_queries();
86                     $pre_time      = microtime(1);
87                     if ($cron_function()) {
88                         $DB->set_field("modules", "lastcron", $timenow, array("id"=>$mod->id));
89                     }
90                     if (isset($pre_dbqueries)) {
91                         mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries");
92                         mtrace("... used " . (microtime(1) - $pre_time) . " seconds");
93                     }
94                 /// Reset possible changes by modules to time_limit. MDL-11597
95                     @set_time_limit(0);
96                     mtrace("done.");
97                 }
98             }
99         }
100     }
101     get_mailer('close');
102     mtrace("Finished activity modules");
104     mtrace("Starting blocks");
105     if ($blocks = $DB->get_records_select("block", "cron > 0 AND ((? - lastcron) > cron) AND visible = 1", array($timenow))) {
106         // we will need the base class.
107         require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
108         foreach ($blocks as $block) {
109             $blockfile = $CFG->dirroot.'/blocks/'.$block->name.'/block_'.$block->name.'.php';
110             if (file_exists($blockfile)) {
111                 require_once($blockfile);
112                 $classname = 'block_'.$block->name;
113                 $blockobj = new $classname;
114                 if (method_exists($blockobj,'cron')) {
115                     mtrace("Processing cron function for ".$block->name.'....','');
116                     if ($blockobj->cron()) {
117                         $DB->set_field('block', 'lastcron', $timenow, array('id'=>$block->id));
118                     }
119                 /// Reset possible changes by blocks to time_limit. MDL-11597
120                     @set_time_limit(0);
121                     mtrace('done.');
122                 }
123             }
125         }
126     }
127     mtrace('Finished blocks');
129     //now do plagiarism checks
130     require_once($CFG->libdir.'/plagiarismlib.php');
131     plagiarism_cron();
133     mtrace("Starting quiz reports");
134     if ($reports = $DB->get_records_select('quiz_reports', "cron > 0 AND ((? - lastcron) > cron)", array($timenow))) {
135         foreach ($reports as $report) {
136             $cronfile = "$CFG->dirroot/mod/quiz/report/$report->name/cron.php";
137             if (file_exists($cronfile)) {
138                 include_once($cronfile);
139                 $cron_function = 'quiz_report_'.$report->name."_cron";
140                 if (function_exists($cron_function)) {
141                     mtrace("Processing quiz report cron function $cron_function ...", '');
142                     $pre_dbqueries = null;
143                     $pre_dbqueries = $DB->perf_get_queries();
144                     $pre_time      = microtime(1);
145                     if ($cron_function()) {
146                         $DB->set_field('quiz_reports', "lastcron", $timenow, array("id"=>$report->id));
147                     }
148                     if (isset($pre_dbqueries)) {
149                         mtrace("... used " . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries");
150                         mtrace("... used " . (microtime(1) - $pre_time) . " seconds");
151                     }
152                     mtrace("done.");
153                 }
154             }
155         }
156     }
157     mtrace("Finished quiz reports");
159     mtrace('Starting admin reports');
160     cron_execute_plugin_type('report');
161     mtrace('Finished admin reports');
163     mtrace('Starting main gradebook job ...');
164     grade_cron();
165     mtrace('done.');
168     mtrace('Starting processing the event queue...');
169     events_cron();
170     mtrace('done.');
173     if ($CFG->enablecompletion) {
174         // Completion cron
175         mtrace('Starting the completion cron...');
176         require_once($CFG->libdir . '/completion/cron.php');
177         completion_cron();
178         mtrace('done');
179     }
182     if ($CFG->enableportfolios) {
183         // Portfolio cron
184         mtrace('Starting the portfolio cron...');
185         require_once($CFG->libdir . '/portfoliolib.php');
186         portfolio_cron();
187         mtrace('done');
188     }
190 /// Run all core cron jobs, but not every time since they aren't too important.
191 /// These don't have a timer to reduce load, so we'll use a random number
192 /// to randomly choose the percentage of times we should run these jobs.
194     srand ((double) microtime() * 10000000);
195     $random100 = rand(0,100);
197     if ($random100 < 20) {     // Approximately 20% of the time.
198         mtrace("Running clean-up tasks...");
200         /// Delete users who haven't confirmed within required period
202         if (!empty($CFG->deleteunconfirmed)) {
203             $cuttime = $timenow - ($CFG->deleteunconfirmed * 3600);
204             $rs = $DB->get_recordset_sql ("SELECT id, firstname, lastname
205                                              FROM {user}
206                                             WHERE confirmed = 0 AND firstaccess > 0
207                                                   AND firstaccess < ?", array($cuttime));
208             foreach ($rs as $user) {
209                 if ($DB->delete_records('user', array('id'=>$user->id))) {
210                     mtrace("Deleted unconfirmed user for ".fullname($user, true)." ($user->id)");
211                 }
212             }
213             $rs->close();
214         }
215         flush();
218         /// Delete users who haven't completed profile within required period
220         if (!empty($CFG->deleteincompleteusers)) {
221             $cuttime = $timenow - ($CFG->deleteincompleteusers * 3600);
222             $rs = $DB->get_recordset_sql ("SELECT *
223                                              FROM {user}
224                                             WHERE confirmed = 1 AND lastaccess > 0
225                                                   AND lastaccess < ? AND deleted = 0
226                                                   AND (lastname = '' OR firstname = '' OR email = '')",
227                                           array($cuttime));
228             foreach ($rs as $user) {
229                 if (delete_user($user)) {
230                     mtrace("Deleted not fully setup user $user->username ($user->id)");
231                 }
232             }
233             $rs->close();
234         }
235         flush();
238         /// Delete old logs to save space (this might need a timer to slow it down...)
240         if (!empty($CFG->loglifetime)) {  // value in days
241             $loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24);
242             if ($DB->delete_records_select("log", "time < ?", array($loglifetime))) {
243                 mtrace("Deleted old log records");
244             }
245         }
246         flush();
248         // Delete old backup_controllers and logs
250         if (!empty($CFG->loglifetime)) {  // value in days
251             $loglifetime = $timenow - ($CFG->loglifetime * 3600 * 24);
252             // Delete child records from backup_logs
253             $DB->execute("DELETE FROM {backup_logs}
254                            WHERE EXISTS (
255                                SELECT 'x'
256                                  FROM {backup_controllers} bc
257                                 WHERE bc.backupid = {backup_logs}.backupid
258                                   AND bc.timecreated < ?)", array($loglifetime));
259             // Delete records from backup_controllers
260             $DB->execute("DELETE FROM {backup_controllers}
261                           WHERE timecreated < ?", array($loglifetime));
262             mtrace("Deleted old backup records");
263         }
264         flush();
268         /// Delete old cached texts
270         if (!empty($CFG->cachetext)) {   // Defined in config.php
271             $cachelifetime = time() - $CFG->cachetext - 60;  // Add an extra minute to allow for really heavy sites
272             if ($DB->delete_records_select('cache_text', "timemodified < ?", array($cachelifetime))) {
273                 mtrace("Deleted old cache_text records");
274             }
275         }
276         flush();
278         if (!empty($CFG->notifyloginfailures)) {
279             notify_login_failures();
280             mtrace('Notified login failured');
281         }
282         flush();
284         //
285         // generate new password emails for users
286         //
287         mtrace('checking for create_password');
288         if ($DB->count_records('user_preferences', array('name'=>'create_password', 'value'=>'1'))) {
289             mtrace('creating passwords for new users');
290             $newusers = $DB->get_recordset_sql("SELECT u.id as id, u.email, u.firstname,
291                                                      u.lastname, u.username,
292                                                      p.id as prefid
293                                                 FROM {user} u
294                                                 JOIN {user_preferences} p ON u.id=p.userid
295                                                WHERE p.name='create_password' AND p.value='1' AND u.email !='' ");
297             foreach ($newusers as $newuser) {
298                 // email user
299                 if (setnew_password_and_mail($newuser)) {
300                     unset_user_preference('create_password', $newuser);
301                     set_user_preference('auth_forcepasswordchange', 1, $newuser);
302                 } else {
303                     trigger_error("Could not create and mail new user password!");
304                 }
305             }
306             $newusers->close();
307         }
309         if (!empty($CFG->usetags)) {
310             require_once($CFG->dirroot.'/tag/lib.php');
311             tag_cron();
312             mtrace ('Executed tag cron');
313         }
315         // Accesslib stuff
316         cleanup_contexts();
317         mtrace ('Cleaned up contexts');
318         gc_cache_flags();
319         mtrace ('Cleaned cache flags');
320         // If you suspect that the context paths are somehow corrupt
321         // replace the line below with: build_context_path(true);
322         build_context_path();
323         mtrace ('Built context paths');
325         if (!empty($CFG->messagingdeletereadnotificationsdelay)) {
326             $notificationdeletetime = time() - $CFG->messagingdeletereadnotificationsdelay;
327             $DB->delete_records_select('message_read', 'notification=1 AND timeread<:notificationdeletetime', array('notificationdeletetime'=>$notificationdeletetime));
328             mtrace('Cleaned up read notifications');
329         }
331         mtrace("Finished clean-up tasks...");
333     } // End of occasional clean-up tasks
335     // Run automated backups if required.
336     require_once($CFG->dirroot.'/backup/util/includes/backup_includes.php');
337     require_once($CFG->dirroot.'/backup/util/helper/backup_cron_helper.class.php');
338     backup_cron_automated_helper::run_automated_backup();
340 /// Run the auth cron, if any
341 /// before enrolments because it might add users that will be needed in enrol plugins
342     $auths = get_enabled_auth_plugins();
344     mtrace("Running auth crons if required...");
345     foreach ($auths as $auth) {
346         $authplugin = get_auth_plugin($auth);
347         if (method_exists($authplugin, 'cron')) {
348             mtrace("Running cron for auth/$auth...");
349             $authplugin->cron();
350             if (!empty($authplugin->log)) {
351                 mtrace($authplugin->log);
352             }
353         }
354         unset($authplugin);
355     }
357     mtrace("Running enrol crons if required...");
358     $enrols = enrol_get_plugins(true);
359     foreach($enrols as $ename=>$enrol) {
360         // do this for all plugins, disabled plugins might want to cleanup stuff such as roles
361         if (!$enrol->is_cron_required()) {
362             continue;
363         }
364         mtrace("Running cron for enrol_$ename...");
365         $enrol->cron();
366         $enrol->set_config('lastcron', time());
367     }
369     if (!empty($CFG->enablestats) and empty($CFG->disablestatsprocessing)) {
370         require_once($CFG->dirroot.'/lib/statslib.php');
371         // check we're not before our runtime
372         $timetocheck = stats_get_base_daily() + $CFG->statsruntimestarthour*60*60 + $CFG->statsruntimestartminute*60;
374         if (time() > $timetocheck) {
375             // process configured number of days as max (defaulting to 31)
376             $maxdays = empty($CFG->statsruntimedays) ? 31 : abs($CFG->statsruntimedays);
377             if (stats_cron_daily($maxdays)) {
378                 if (stats_cron_weekly()) {
379                     if (stats_cron_monthly()) {
380                         stats_clean_old();
381                     }
382                 }
383             }
384             @set_time_limit(0);
385         } else {
386             mtrace('Next stats run after:'. userdate($timetocheck));
387         }
388     }
391     mtrace('Starting course reports');
392     cron_execute_plugin_type('coursereport');
393     mtrace('Finished course reports');
395     // run gradebook import/export/report cron
396     mtrace('Starting gradebook plugins');
397     cron_execute_plugin_type('gradeimport');
398     cron_execute_plugin_type('gradeexport');
399     cron_execute_plugin_type('gradereport');
400     mtrace('Finished gradebook plugins');
402     // Run external blog cron if needed
403     if ($CFG->useexternalblogs) {
404         require_once($CFG->dirroot . '/blog/lib.php');
405         mtrace("Fetching external blog entries...", '');
406         $sql = "timefetched < ? OR timefetched = 0";
407         $externalblogs = $DB->get_records_select('blog_external', $sql, array(mktime() - $CFG->externalblogcrontime));
409         foreach ($externalblogs as $eb) {
410             blog_sync_external_entries($eb);
411         }
412         mtrace('done.');
413     }
415     // Run blog associations cleanup
416     if ($CFG->useblogassociations) {
417         require_once($CFG->dirroot . '/blog/lib.php');
418         // delete entries whose contextids no longer exists
419         mtrace("Deleting blog associations linked to non-existent contexts...", '');
420         $DB->delete_records_select('blog_association', 'contextid NOT IN (SELECT id FROM {context})');
421         mtrace('done.');
422     }
424     //Run registration updated cron
425     mtrace(get_string('siteupdatesstart', 'hub'));
426     require_once($CFG->dirroot . '/admin/registration/lib.php');
427     $registrationmanager = new registration_manager();
428     $registrationmanager->cron();
429     mtrace(get_string('siteupdatesend', 'hub'));
431     // cleanup file trash
432     $fs = get_file_storage();
433     $fs->cron();
435     //cleanup old session linked tokens
436     //deletes the session linked tokens that are over a day old.
437     mtrace("Deleting session linked tokens more than one day old...", '');
438     $DB->delete_records_select('external_tokens', 'lastaccess < :onedayago AND tokentype = :tokentype',
439                     array('onedayago' => time() - DAYSECS, 'tokentype' => EXTERNAL_TOKEN_EMBEDDED));
440     mtrace('done.');
442     // all other plugins
443     cron_execute_plugin_type('message', 'message plugins');
444     cron_execute_plugin_type('filter', 'filters');
445     cron_execute_plugin_type('editor', 'editors');
446     cron_execute_plugin_type('format', 'course formats');
447     cron_execute_plugin_type('profilefield', 'profile fields');
448     cron_execute_plugin_type('webservice', 'webservices');
449     // TODO: Repository lib.php files are messed up (include many other files, etc), so it is
450     // currently not possible to implement repository plugin cron using this infrastructure
451     // cron_execute_plugin_type('repository', 'repository plugins');
452     cron_execute_plugin_type('qtype', 'question types');
453     cron_execute_plugin_type('plagiarism', 'plagiarism plugins');
454     cron_execute_plugin_type('theme', 'themes');
455     cron_execute_plugin_type('tool', 'admin tools');
457     // and finally run any local cronjobs, if any
458     if ($locals = get_plugin_list('local')) {
459         mtrace('Processing customized cron scripts ...', '');
460         // new cron functions in lib.php first
461         cron_execute_plugin_type('local');
462         // legacy cron files are executed directly
463         foreach ($locals as $local => $localdir) {
464             if (file_exists("$localdir/cron.php")) {
465                 include("$localdir/cron.php");
466             }
467         }
468         mtrace('done.');
469     }
471     mtrace("Cron script completed correctly");
473     $difftime = microtime_diff($starttime, microtime());
474     mtrace("Execution took ".$difftime." seconds");
477 /**
478  * Executes cron functions for a specific type of plugin.
479  * @param string $plugintype Plugin type (e.g. 'report')
480  * @param string $description If specified, will display 'Starting (whatever)'
481  *   and 'Finished (whatever)' lines, otherwise does not display
482  */
483 function cron_execute_plugin_type($plugintype, $description = null) {
484     global $DB;
486     // Get list from plugin => function for all plugins
487     $plugins = get_plugin_list_with_function($plugintype, 'cron');
489     // Modify list for backward compatibility (different files/names)
490     $plugins = cron_bc_hack_plugin_functions($plugintype, $plugins);
492     // Return if no plugins with cron function to process
493     if (!$plugins) {
494         return;
495     }
497     if ($description) {
498         mtrace('Starting '.$description);
499     }
501     foreach ($plugins as $component=>$cronfunction) {
502         $dir = get_component_directory($component);
504         // Get cron period if specified in version.php, otherwise assume every cron
505         $cronperiod = 0;
506         if (file_exists("$dir/version.php")) {
507             $plugin = new stdClass();
508             include("$dir/version.php");
509             if (isset($plugin->cron)) {
510                 $cronperiod = $plugin->cron;
511             }
512         }
514         // Using last cron and cron period, don't run if it already ran recently
515         $lastcron = get_config($component, 'lastcron');
516         if ($cronperiod && $lastcron) {
517             if ($lastcron + $cronperiod > time()) {
518                 // do not execute cron yet
519                 continue;
520             }
521         }
523         mtrace('Processing cron function for ' . $component . '...');
524         $pre_dbqueries = $DB->perf_get_queries();
525         $pre_time = microtime(true);
527         $cronfunction();
529         mtrace("done. (" . ($DB->perf_get_queries() - $pre_dbqueries) . " dbqueries, " .
530                 round(microtime(true) - $pre_time, 2) . " seconds)");
532         set_config('lastcron', time(), $component);
533     }
535     if ($description) {
536         mtrace('Finished ' . $description);
537     }
540 /**
541  * Used to add in old-style cron functions within plugins that have not been converted to the
542  * new standard API. (The standard API is frankenstyle_name_cron() in lib.php; some types used
543  * cron.php and some used a different name.)
544  * @param string $plugintype Plugin type e.g. 'report'
545  * @param array $plugins Array from plugin name (e.g. 'report_frog') to function name (e.g.
546  *   'report_frog_cron') for plugin cron functions that were already found using the new API
547  * @return array Revised version of $plugins that adds in any extra plugin functions found by
548  *   looking in the older location
549  */
550 function cron_bc_hack_plugin_functions($plugintype, $plugins) {
551     global $CFG; // mandatory in case it is referenced by include()d PHP script
553     if ($plugintype === 'report') {
554         // Admin reports only - not course report because course report was
555         // never implemented before, so doesn't need BC
556         foreach (get_plugin_list($plugintype) as $pluginname=>$dir) {
557             $component = $plugintype . '_' . $pluginname;
558             if (isset($plugins[$component])) {
559                 // We already have detected the function using the new API
560                 continue;
561             }
562             if (!file_exists("$dir/cron.php")) {
563                 // No old style cron file present
564                 continue;
565             }
566             include_once("$dir/cron.php");
567             $cronfunction = $component . '_cron';
568             if (function_exists($cronfunction)) {
569                 $plugins[$component] = $cronfunction;
570             } else {
571                 debugging("Invalid legacy cron.php detected in $component, " .
572                         "please use lib.php instead");
573             }
574         }
575     } else if (strpos($plugintype, 'grade') === 0) {
576         // Detect old style cron function names
577         // Plugin gradeexport_frog used to use grade_export_frog_cron() instead of
578         // new standard API gradeexport_frog_cron(). Also applies to gradeimport, gradereport
579         foreach(get_plugin_list($plugintype) as $pluginname=>$dir) {
580             $component = $plugintype.'_'.$pluginname;
581             if (isset($plugins[$component])) {
582                 // We already have detected the function using the new API
583                 continue;
584             }
585             if (!file_exists("$dir/lib.php")) {
586                 continue;
587             }
588             include_once("$dir/lib.php");
589             $cronfunction = str_replace('grade', 'grade_', $plugintype) . '_' .
590                     $pluginname . '_cron';
591             if (function_exists($cronfunction)) {
592                 $plugins[$component] = $cronfunction;
593             }
594         }
595     }
597     return $plugins;