Merge branch 'MDL-40255_M25' of git://github.com/lazydaisy/moodle into MOODLE_25_STABLE
[moodle.git] / lib / upgradelib.php
blobc1ada2eb91af16cdce5d5c275be4d6e1cdca7de2
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.
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 * Various upgrade/install related functions and classes.
21 * @package core
22 * @subpackage upgrade
23 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 /** UPGRADE_LOG_NORMAL = 0 */
30 define('UPGRADE_LOG_NORMAL', 0);
31 /** UPGRADE_LOG_NOTICE = 1 */
32 define('UPGRADE_LOG_NOTICE', 1);
33 /** UPGRADE_LOG_ERROR = 2 */
34 define('UPGRADE_LOG_ERROR', 2);
36 /**
37 * Exception indicating unknown error during upgrade.
39 * @package core
40 * @subpackage upgrade
41 * @copyright 2009 Petr Skoda {@link http://skodak.org}
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class upgrade_exception extends moodle_exception {
45 function __construct($plugin, $version, $debuginfo=NULL) {
46 global $CFG;
47 $a = (object)array('plugin'=>$plugin, 'version'=>$version);
48 parent::__construct('upgradeerror', 'admin', "$CFG->wwwroot/$CFG->admin/index.php", $a, $debuginfo);
52 /**
53 * Exception indicating downgrade error during upgrade.
55 * @package core
56 * @subpackage upgrade
57 * @copyright 2009 Petr Skoda {@link http://skodak.org}
58 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
60 class downgrade_exception extends moodle_exception {
61 function __construct($plugin, $oldversion, $newversion) {
62 global $CFG;
63 $plugin = is_null($plugin) ? 'moodle' : $plugin;
64 $a = (object)array('plugin'=>$plugin, 'oldversion'=>$oldversion, 'newversion'=>$newversion);
65 parent::__construct('cannotdowngrade', 'debug', "$CFG->wwwroot/$CFG->admin/index.php", $a);
69 /**
70 * @package core
71 * @subpackage upgrade
72 * @copyright 2009 Petr Skoda {@link http://skodak.org}
73 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
75 class upgrade_requires_exception extends moodle_exception {
76 function __construct($plugin, $pluginversion, $currentmoodle, $requiremoodle) {
77 global $CFG;
78 $a = new stdClass();
79 $a->pluginname = $plugin;
80 $a->pluginversion = $pluginversion;
81 $a->currentmoodle = $currentmoodle;
82 $a->requiremoodle = $requiremoodle;
83 parent::__construct('pluginrequirementsnotmet', 'error', "$CFG->wwwroot/$CFG->admin/index.php", $a);
87 /**
88 * @package core
89 * @subpackage upgrade
90 * @copyright 2009 Petr Skoda {@link http://skodak.org}
91 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
93 class plugin_defective_exception extends moodle_exception {
94 function __construct($plugin, $details) {
95 global $CFG;
96 parent::__construct('detectedbrokenplugin', 'error', "$CFG->wwwroot/$CFG->admin/index.php", $plugin, $details);
101 * @package core
102 * @subpackage upgrade
103 * @copyright 2009 Petr Skoda {@link http://skodak.org}
104 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
106 class plugin_misplaced_exception extends moodle_exception {
107 function __construct($component, $expected, $current) {
108 global $CFG;
109 $a = new stdClass();
110 $a->component = $component;
111 $a->expected = $expected;
112 $a->current = $current;
113 parent::__construct('detectedmisplacedplugin', 'core_plugin', "$CFG->wwwroot/$CFG->admin/index.php", $a);
118 * Sets maximum expected time needed for upgrade task.
119 * Please always make sure that upgrade will not run longer!
121 * The script may be automatically aborted if upgrade times out.
123 * @category upgrade
124 * @param int $max_execution_time in seconds (can not be less than 60 s)
126 function upgrade_set_timeout($max_execution_time=300) {
127 global $CFG;
129 if (!isset($CFG->upgraderunning) or $CFG->upgraderunning < time()) {
130 $upgraderunning = get_config(null, 'upgraderunning');
131 } else {
132 $upgraderunning = $CFG->upgraderunning;
135 if (!$upgraderunning) {
136 if (CLI_SCRIPT) {
137 // never stop CLI upgrades
138 $upgraderunning = 0;
139 } else {
140 // web upgrade not running or aborted
141 print_error('upgradetimedout', 'admin', "$CFG->wwwroot/$CFG->admin/");
145 if ($max_execution_time < 60) {
146 // protection against 0 here
147 $max_execution_time = 60;
150 $expected_end = time() + $max_execution_time;
152 if ($expected_end < $upgraderunning + 10 and $expected_end > $upgraderunning - 10) {
153 // no need to store new end, it is nearly the same ;-)
154 return;
157 if (CLI_SCRIPT) {
158 // there is no point in timing out of CLI scripts, admins can stop them if necessary
159 set_time_limit(0);
160 } else {
161 set_time_limit($max_execution_time);
163 set_config('upgraderunning', $expected_end); // keep upgrade locked until this time
167 * Upgrade savepoint, marks end of each upgrade block.
168 * It stores new main version, resets upgrade timeout
169 * and abort upgrade if user cancels page loading.
171 * Please do not make large upgrade blocks with lots of operations,
172 * for example when adding tables keep only one table operation per block.
174 * @category upgrade
175 * @param bool $result false if upgrade step failed, true if completed
176 * @param string or float $version main version
177 * @param bool $allowabort allow user to abort script execution here
178 * @return void
180 function upgrade_main_savepoint($result, $version, $allowabort=true) {
181 global $CFG;
183 //sanity check to avoid confusion with upgrade_mod_savepoint usage.
184 if (!is_bool($allowabort)) {
185 $errormessage = 'Parameter type mismatch. Are you mixing up upgrade_main_savepoint() and upgrade_mod_savepoint()?';
186 throw new coding_exception($errormessage);
189 if (!$result) {
190 throw new upgrade_exception(null, $version);
193 if ($CFG->version >= $version) {
194 // something really wrong is going on in main upgrade script
195 throw new downgrade_exception(null, $CFG->version, $version);
198 set_config('version', $version);
199 upgrade_log(UPGRADE_LOG_NORMAL, null, 'Upgrade savepoint reached');
201 // reset upgrade timeout to default
202 upgrade_set_timeout();
204 // this is a safe place to stop upgrades if user aborts page loading
205 if ($allowabort and connection_aborted()) {
206 die;
211 * Module upgrade savepoint, marks end of module upgrade blocks
212 * It stores module version, resets upgrade timeout
213 * and abort upgrade if user cancels page loading.
215 * @category upgrade
216 * @param bool $result false if upgrade step failed, true if completed
217 * @param string or float $version main version
218 * @param string $modname name of module
219 * @param bool $allowabort allow user to abort script execution here
220 * @return void
222 function upgrade_mod_savepoint($result, $version, $modname, $allowabort=true) {
223 global $DB;
225 if (!$result) {
226 throw new upgrade_exception("mod_$modname", $version);
229 if (!$module = $DB->get_record('modules', array('name'=>$modname))) {
230 print_error('modulenotexist', 'debug', '', $modname);
233 if ($module->version >= $version) {
234 // something really wrong is going on in upgrade script
235 throw new downgrade_exception("mod_$modname", $module->version, $version);
237 $module->version = $version;
238 $DB->update_record('modules', $module);
239 upgrade_log(UPGRADE_LOG_NORMAL, "mod_$modname", 'Upgrade savepoint reached');
241 // reset upgrade timeout to default
242 upgrade_set_timeout();
244 // this is a safe place to stop upgrades if user aborts page loading
245 if ($allowabort and connection_aborted()) {
246 die;
251 * Blocks upgrade savepoint, marks end of blocks upgrade blocks
252 * It stores block version, resets upgrade timeout
253 * and abort upgrade if user cancels page loading.
255 * @category upgrade
256 * @param bool $result false if upgrade step failed, true if completed
257 * @param string or float $version main version
258 * @param string $blockname name of block
259 * @param bool $allowabort allow user to abort script execution here
260 * @return void
262 function upgrade_block_savepoint($result, $version, $blockname, $allowabort=true) {
263 global $DB;
265 if (!$result) {
266 throw new upgrade_exception("block_$blockname", $version);
269 if (!$block = $DB->get_record('block', array('name'=>$blockname))) {
270 print_error('blocknotexist', 'debug', '', $blockname);
273 if ($block->version >= $version) {
274 // something really wrong is going on in upgrade script
275 throw new downgrade_exception("block_$blockname", $block->version, $version);
277 $block->version = $version;
278 $DB->update_record('block', $block);
279 upgrade_log(UPGRADE_LOG_NORMAL, "block_$blockname", 'Upgrade savepoint reached');
281 // reset upgrade timeout to default
282 upgrade_set_timeout();
284 // this is a safe place to stop upgrades if user aborts page loading
285 if ($allowabort and connection_aborted()) {
286 die;
291 * Plugins upgrade savepoint, marks end of blocks upgrade blocks
292 * It stores plugin version, resets upgrade timeout
293 * and abort upgrade if user cancels page loading.
295 * @category upgrade
296 * @param bool $result false if upgrade step failed, true if completed
297 * @param string or float $version main version
298 * @param string $type name of plugin
299 * @param string $dir location of plugin
300 * @param bool $allowabort allow user to abort script execution here
301 * @return void
303 function upgrade_plugin_savepoint($result, $version, $type, $plugin, $allowabort=true) {
304 $component = $type.'_'.$plugin;
306 if (!$result) {
307 throw new upgrade_exception($component, $version);
310 $installedversion = get_config($component, 'version');
311 if ($installedversion >= $version) {
312 // Something really wrong is going on in the upgrade script
313 throw new downgrade_exception($component, $installedversion, $version);
315 set_config('version', $version, $component);
316 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
318 // Reset upgrade timeout to default
319 upgrade_set_timeout();
321 // This is a safe place to stop upgrades if user aborts page loading
322 if ($allowabort and connection_aborted()) {
323 die;
328 * Detect if there are leftovers in PHP source files.
330 * During main version upgrades administrators MUST move away
331 * old PHP source files and start from scratch (or better
332 * use git).
334 * @return bool true means borked upgrade, false means previous PHP files were properly removed
336 function upgrade_stale_php_files_present() {
337 global $CFG;
339 $someexamplesofremovedfiles = array(
340 // removed in 2.5dev
341 '/backup/lib.php',
342 '/backup/bb/README.txt',
343 '/lib/excel/test.php',
344 // removed in 2.4dev
345 '/admin/tool/unittest/simpletestlib.php',
346 // removed in 2.3dev
347 '/lib/minify/builder/',
348 // removed in 2.2dev
349 '/lib/yui/3.4.1pr1/',
350 // removed in 2.2
351 '/search/cron_php5.php',
352 '/course/report/log/indexlive.php',
353 '/admin/report/backups/index.php',
354 '/admin/generator.php',
355 // removed in 2.1
356 '/lib/yui/2.8.0r4/',
357 // removed in 2.0
358 '/blocks/admin/block_admin.php',
359 '/blocks/admin_tree/block_admin_tree.php',
362 foreach ($someexamplesofremovedfiles as $file) {
363 if (file_exists($CFG->dirroot.$file)) {
364 return true;
368 return false;
372 * Upgrade plugins
373 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
374 * return void
376 function upgrade_plugins($type, $startcallback, $endcallback, $verbose) {
377 global $CFG, $DB;
379 /// special cases
380 if ($type === 'mod') {
381 return upgrade_plugins_modules($startcallback, $endcallback, $verbose);
382 } else if ($type === 'block') {
383 return upgrade_plugins_blocks($startcallback, $endcallback, $verbose);
386 $plugs = get_plugin_list($type);
388 foreach ($plugs as $plug=>$fullplug) {
389 // Reset time so that it works when installing a large number of plugins
390 set_time_limit(600);
391 $component = clean_param($type.'_'.$plug, PARAM_COMPONENT); // standardised plugin name
393 // check plugin dir is valid name
394 if (empty($component)) {
395 throw new plugin_defective_exception($type.'_'.$plug, 'Invalid plugin directory name.');
398 if (!is_readable($fullplug.'/version.php')) {
399 continue;
402 $plugin = new stdClass();
403 $module = new stdClass(); // Prevent some notices when plugin placed in wrong directory.
404 require($fullplug.'/version.php'); // defines $plugin with version etc
406 if (!isset($plugin->version) and isset($module->version)) {
407 $plugin = $module;
410 // if plugin tells us it's full name we may check the location
411 if (isset($plugin->component)) {
412 if ($plugin->component !== $component) {
413 $current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullplug);
414 $expected = str_replace($CFG->dirroot, '$CFG->dirroot', get_component_directory($plugin->component));
415 throw new plugin_misplaced_exception($component, $expected, $current);
419 if (empty($plugin->version)) {
420 throw new plugin_defective_exception($component, 'Missing version value in version.php');
423 $plugin->name = $plug;
424 $plugin->fullname = $component;
427 if (!empty($plugin->requires)) {
428 if ($plugin->requires > $CFG->version) {
429 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
430 } else if ($plugin->requires < 2010000000) {
431 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
435 // try to recover from interrupted install.php if needed
436 if (file_exists($fullplug.'/db/install.php')) {
437 if (get_config($plugin->fullname, 'installrunning')) {
438 require_once($fullplug.'/db/install.php');
439 $recover_install_function = 'xmldb_'.$plugin->fullname.'_install_recovery';
440 if (function_exists($recover_install_function)) {
441 $startcallback($component, true, $verbose);
442 $recover_install_function();
443 unset_config('installrunning', $plugin->fullname);
444 update_capabilities($component);
445 log_update_descriptions($component);
446 external_update_descriptions($component);
447 events_update_definition($component);
448 message_update_providers($component);
449 if ($type === 'message') {
450 message_update_processors($plug);
452 upgrade_plugin_mnet_functions($component);
453 $endcallback($component, true, $verbose);
458 $installedversion = get_config($plugin->fullname, 'version');
459 if (empty($installedversion)) { // new installation
460 $startcallback($component, true, $verbose);
462 /// Install tables if defined
463 if (file_exists($fullplug.'/db/install.xml')) {
464 $DB->get_manager()->install_from_xmldb_file($fullplug.'/db/install.xml');
467 /// store version
468 upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false);
470 /// execute post install file
471 if (file_exists($fullplug.'/db/install.php')) {
472 require_once($fullplug.'/db/install.php');
473 set_config('installrunning', 1, $plugin->fullname);
474 $post_install_function = 'xmldb_'.$plugin->fullname.'_install';
475 $post_install_function();
476 unset_config('installrunning', $plugin->fullname);
479 /// Install various components
480 update_capabilities($component);
481 log_update_descriptions($component);
482 external_update_descriptions($component);
483 events_update_definition($component);
484 message_update_providers($component);
485 if ($type === 'message') {
486 message_update_processors($plug);
488 upgrade_plugin_mnet_functions($component);
489 cache_helper::purge_all(true);
490 purge_all_caches();
491 $endcallback($component, true, $verbose);
493 } else if ($installedversion < $plugin->version) { // upgrade
494 /// Run the upgrade function for the plugin.
495 $startcallback($component, false, $verbose);
497 if (is_readable($fullplug.'/db/upgrade.php')) {
498 require_once($fullplug.'/db/upgrade.php'); // defines upgrading function
500 $newupgrade_function = 'xmldb_'.$plugin->fullname.'_upgrade';
501 $result = $newupgrade_function($installedversion);
502 } else {
503 $result = true;
506 $installedversion = get_config($plugin->fullname, 'version');
507 if ($installedversion < $plugin->version) {
508 // store version if not already there
509 upgrade_plugin_savepoint($result, $plugin->version, $type, $plug, false);
512 /// Upgrade various components
513 update_capabilities($component);
514 log_update_descriptions($component);
515 external_update_descriptions($component);
516 events_update_definition($component);
517 message_update_providers($component);
518 if ($type === 'message') {
519 message_update_processors($plug);
521 upgrade_plugin_mnet_functions($component);
522 cache_helper::purge_all(true);
523 purge_all_caches();
524 $endcallback($component, false, $verbose);
526 } else if ($installedversion > $plugin->version) {
527 throw new downgrade_exception($component, $installedversion, $plugin->version);
533 * Find and check all modules and load them up or upgrade them if necessary
535 * @global object
536 * @global object
538 function upgrade_plugins_modules($startcallback, $endcallback, $verbose) {
539 global $CFG, $DB;
541 $mods = get_plugin_list('mod');
543 foreach ($mods as $mod=>$fullmod) {
545 if ($mod === 'NEWMODULE') { // Someone has unzipped the template, ignore it
546 continue;
549 $component = clean_param('mod_'.$mod, PARAM_COMPONENT);
551 // check module dir is valid name
552 if (empty($component)) {
553 throw new plugin_defective_exception('mod_'.$mod, 'Invalid plugin directory name.');
556 if (!is_readable($fullmod.'/version.php')) {
557 throw new plugin_defective_exception($component, 'Missing version.php');
560 $module = new stdClass();
561 $plugin = new stdClass(); // Prevent some notices when plugin placed in wrong directory.
562 require($fullmod .'/version.php'); // defines $module with version etc
564 if (!isset($module->version) and isset($plugin->version)) {
565 $module = $plugin;
568 // if plugin tells us it's full name we may check the location
569 if (isset($module->component)) {
570 if ($module->component !== $component) {
571 $current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullmod);
572 $expected = str_replace($CFG->dirroot, '$CFG->dirroot', get_component_directory($module->component));
573 throw new plugin_misplaced_exception($component, $expected, $current);
577 if (empty($module->version)) {
578 if (isset($module->version)) {
579 // Version is empty but is set - it means its value is 0 or ''. Let us skip such module.
580 // This is intended for developers so they can work on the early stages of the module.
581 continue;
583 throw new plugin_defective_exception($component, 'Missing version value in version.php');
586 if (!empty($module->requires)) {
587 if ($module->requires > $CFG->version) {
588 throw new upgrade_requires_exception($component, $module->version, $CFG->version, $module->requires);
589 } else if ($module->requires < 2010000000) {
590 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
594 if (empty($module->cron)) {
595 $module->cron = 0;
598 // all modules must have en lang pack
599 if (!is_readable("$fullmod/lang/en/$mod.php")) {
600 throw new plugin_defective_exception($component, 'Missing mandatory en language pack.');
603 $module->name = $mod; // The name MUST match the directory
605 $currmodule = $DB->get_record('modules', array('name'=>$module->name));
607 if (file_exists($fullmod.'/db/install.php')) {
608 if (get_config($module->name, 'installrunning')) {
609 require_once($fullmod.'/db/install.php');
610 $recover_install_function = 'xmldb_'.$module->name.'_install_recovery';
611 if (function_exists($recover_install_function)) {
612 $startcallback($component, true, $verbose);
613 $recover_install_function();
614 unset_config('installrunning', $module->name);
615 // Install various components too
616 update_capabilities($component);
617 log_update_descriptions($component);
618 external_update_descriptions($component);
619 events_update_definition($component);
620 message_update_providers($component);
621 upgrade_plugin_mnet_functions($component);
622 $endcallback($component, true, $verbose);
627 if (empty($currmodule->version)) {
628 $startcallback($component, true, $verbose);
630 /// Execute install.xml (XMLDB) - must be present in all modules
631 $DB->get_manager()->install_from_xmldb_file($fullmod.'/db/install.xml');
633 /// Add record into modules table - may be needed in install.php already
634 $module->id = $DB->insert_record('modules', $module);
636 /// Post installation hook - optional
637 if (file_exists("$fullmod/db/install.php")) {
638 require_once("$fullmod/db/install.php");
639 // Set installation running flag, we need to recover after exception or error
640 set_config('installrunning', 1, $module->name);
641 $post_install_function = 'xmldb_'.$module->name.'_install';
642 $post_install_function();
643 unset_config('installrunning', $module->name);
646 /// Install various components
647 update_capabilities($component);
648 log_update_descriptions($component);
649 external_update_descriptions($component);
650 events_update_definition($component);
651 message_update_providers($component);
652 upgrade_plugin_mnet_functions($component);
654 purge_all_caches();
655 $endcallback($component, true, $verbose);
657 } else if ($currmodule->version < $module->version) {
658 /// If versions say that we need to upgrade but no upgrade files are available, notify and continue
659 $startcallback($component, false, $verbose);
661 if (is_readable($fullmod.'/db/upgrade.php')) {
662 require_once($fullmod.'/db/upgrade.php'); // defines new upgrading function
663 $newupgrade_function = 'xmldb_'.$module->name.'_upgrade';
664 $result = $newupgrade_function($currmodule->version, $module);
665 } else {
666 $result = true;
669 $currmodule = $DB->get_record('modules', array('name'=>$module->name));
670 if ($currmodule->version < $module->version) {
671 // store version if not already there
672 upgrade_mod_savepoint($result, $module->version, $mod, false);
675 // update cron flag if needed
676 if ($currmodule->cron != $module->cron) {
677 $DB->set_field('modules', 'cron', $module->cron, array('name' => $module->name));
680 // Upgrade various components
681 update_capabilities($component);
682 log_update_descriptions($component);
683 external_update_descriptions($component);
684 events_update_definition($component);
685 message_update_providers($component);
686 upgrade_plugin_mnet_functions($component);
688 purge_all_caches();
690 $endcallback($component, false, $verbose);
692 } else if ($currmodule->version > $module->version) {
693 throw new downgrade_exception($component, $currmodule->version, $module->version);
700 * This function finds all available blocks and install them
701 * into blocks table or do all the upgrade process if newer.
703 * @global object
704 * @global object
706 function upgrade_plugins_blocks($startcallback, $endcallback, $verbose) {
707 global $CFG, $DB;
709 require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
711 $blocktitles = array(); // we do not want duplicate titles
713 //Is this a first install
714 $first_install = null;
716 $blocks = get_plugin_list('block');
718 foreach ($blocks as $blockname=>$fullblock) {
720 if (is_null($first_install)) {
721 $first_install = ($DB->count_records('block_instances') == 0);
724 if ($blockname === 'NEWBLOCK') { // Someone has unzipped the template, ignore it
725 continue;
728 $component = clean_param('block_'.$blockname, PARAM_COMPONENT);
730 // check block dir is valid name
731 if (empty($component)) {
732 throw new plugin_defective_exception('block_'.$blockname, 'Invalid plugin directory name.');
735 if (!is_readable($fullblock.'/version.php')) {
736 throw new plugin_defective_exception('block/'.$blockname, 'Missing version.php file.');
738 $plugin = new stdClass();
739 $module = new stdClass(); // Prevent some notices when module placed in wrong directory.
740 $plugin->version = NULL;
741 $plugin->cron = 0;
742 include($fullblock.'/version.php');
743 if (!isset($plugin->version) and isset($module->version)) {
744 $plugin = $module;
746 $block = $plugin;
748 // if plugin tells us it's full name we may check the location
749 if (isset($block->component)) {
750 if ($block->component !== $component) {
751 $current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullblock);
752 $expected = str_replace($CFG->dirroot, '$CFG->dirroot', get_component_directory($block->component));
753 throw new plugin_misplaced_exception($component, $expected, $current);
757 if (!empty($plugin->requires)) {
758 if ($plugin->requires > $CFG->version) {
759 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
760 } else if ($plugin->requires < 2010000000) {
761 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
765 if (!is_readable($fullblock.'/block_'.$blockname.'.php')) {
766 throw new plugin_defective_exception('block/'.$blockname, 'Missing main block class file.');
768 include_once($fullblock.'/block_'.$blockname.'.php');
770 $classname = 'block_'.$blockname;
772 if (!class_exists($classname)) {
773 throw new plugin_defective_exception($component, 'Can not load main class.');
776 $blockobj = new $classname; // This is what we'll be testing
777 $blocktitle = $blockobj->get_title();
779 // OK, it's as we all hoped. For further tests, the object will do them itself.
780 if (!$blockobj->_self_test()) {
781 throw new plugin_defective_exception($component, 'Self test failed.');
784 $block->name = $blockname; // The name MUST match the directory
786 if (empty($block->version)) {
787 throw new plugin_defective_exception($component, 'Missing block version.');
790 $currblock = $DB->get_record('block', array('name'=>$block->name));
792 if (file_exists($fullblock.'/db/install.php')) {
793 if (get_config('block_'.$blockname, 'installrunning')) {
794 require_once($fullblock.'/db/install.php');
795 $recover_install_function = 'xmldb_block_'.$blockname.'_install_recovery';
796 if (function_exists($recover_install_function)) {
797 $startcallback($component, true, $verbose);
798 $recover_install_function();
799 unset_config('installrunning', 'block_'.$blockname);
800 // Install various components
801 update_capabilities($component);
802 log_update_descriptions($component);
803 external_update_descriptions($component);
804 events_update_definition($component);
805 message_update_providers($component);
806 upgrade_plugin_mnet_functions($component);
807 $endcallback($component, true, $verbose);
812 if (empty($currblock->version)) { // block not installed yet, so install it
813 $conflictblock = array_search($blocktitle, $blocktitles);
814 if ($conflictblock !== false) {
815 // Duplicate block titles are not allowed, they confuse people
816 // AND PHP's associative arrays ;)
817 throw new plugin_defective_exception($component, get_string('blocknameconflict', 'error', (object)array('name'=>$block->name, 'conflict'=>$conflictblock)));
819 $startcallback($component, true, $verbose);
821 if (file_exists($fullblock.'/db/install.xml')) {
822 $DB->get_manager()->install_from_xmldb_file($fullblock.'/db/install.xml');
824 $block->id = $DB->insert_record('block', $block);
826 if (file_exists($fullblock.'/db/install.php')) {
827 require_once($fullblock.'/db/install.php');
828 // Set installation running flag, we need to recover after exception or error
829 set_config('installrunning', 1, 'block_'.$blockname);
830 $post_install_function = 'xmldb_block_'.$blockname.'_install';
831 $post_install_function();
832 unset_config('installrunning', 'block_'.$blockname);
835 $blocktitles[$block->name] = $blocktitle;
837 // Install various components
838 update_capabilities($component);
839 log_update_descriptions($component);
840 external_update_descriptions($component);
841 events_update_definition($component);
842 message_update_providers($component);
843 upgrade_plugin_mnet_functions($component);
845 purge_all_caches();
846 $endcallback($component, true, $verbose);
848 } else if ($currblock->version < $block->version) {
849 $startcallback($component, false, $verbose);
851 if (is_readable($fullblock.'/db/upgrade.php')) {
852 require_once($fullblock.'/db/upgrade.php'); // defines new upgrading function
853 $newupgrade_function = 'xmldb_block_'.$blockname.'_upgrade';
854 $result = $newupgrade_function($currblock->version, $block);
855 } else {
856 $result = true;
859 $currblock = $DB->get_record('block', array('name'=>$block->name));
860 if ($currblock->version < $block->version) {
861 // store version if not already there
862 upgrade_block_savepoint($result, $block->version, $block->name, false);
865 if ($currblock->cron != $block->cron) {
866 // update cron flag if needed
867 $DB->set_field('block', 'cron', $block->cron, array('id' => $currblock->id));
870 // Upgrade various components
871 update_capabilities($component);
872 log_update_descriptions($component);
873 external_update_descriptions($component);
874 events_update_definition($component);
875 message_update_providers($component);
876 upgrade_plugin_mnet_functions($component);
878 purge_all_caches();
879 $endcallback($component, false, $verbose);
881 } else if ($currblock->version > $block->version) {
882 throw new downgrade_exception($component, $currblock->version, $block->version);
887 // Finally, if we are in the first_install of BLOCKS setup frontpage and admin page blocks
888 if ($first_install) {
889 //Iterate over each course - there should be only site course here now
890 if ($courses = $DB->get_records('course')) {
891 foreach ($courses as $course) {
892 blocks_add_default_course_blocks($course);
896 blocks_add_default_system_blocks();
902 * Log_display description function used during install and upgrade.
904 * @param string $component name of component (moodle, mod_assignment, etc.)
905 * @return void
907 function log_update_descriptions($component) {
908 global $DB;
910 $defpath = get_component_directory($component).'/db/log.php';
912 if (!file_exists($defpath)) {
913 $DB->delete_records('log_display', array('component'=>$component));
914 return;
917 // load new info
918 $logs = array();
919 include($defpath);
920 $newlogs = array();
921 foreach ($logs as $log) {
922 $newlogs[$log['module'].'-'.$log['action']] = $log; // kind of unique name
924 unset($logs);
925 $logs = $newlogs;
927 $fields = array('module', 'action', 'mtable', 'field');
928 // update all log fist
929 $dblogs = $DB->get_records('log_display', array('component'=>$component));
930 foreach ($dblogs as $dblog) {
931 $name = $dblog->module.'-'.$dblog->action;
933 if (empty($logs[$name])) {
934 $DB->delete_records('log_display', array('id'=>$dblog->id));
935 continue;
938 $log = $logs[$name];
939 unset($logs[$name]);
941 $update = false;
942 foreach ($fields as $field) {
943 if ($dblog->$field != $log[$field]) {
944 $dblog->$field = $log[$field];
945 $update = true;
948 if ($update) {
949 $DB->update_record('log_display', $dblog);
952 foreach ($logs as $log) {
953 $dblog = (object)$log;
954 $dblog->component = $component;
955 $DB->insert_record('log_display', $dblog);
960 * Web service discovery function used during install and upgrade.
961 * @param string $component name of component (moodle, mod_assignment, etc.)
962 * @return void
964 function external_update_descriptions($component) {
965 global $DB, $CFG;
967 $defpath = get_component_directory($component).'/db/services.php';
969 if (!file_exists($defpath)) {
970 require_once($CFG->dirroot.'/lib/externallib.php');
971 external_delete_descriptions($component);
972 return;
975 // load new info
976 $functions = array();
977 $services = array();
978 include($defpath);
980 // update all function fist
981 $dbfunctions = $DB->get_records('external_functions', array('component'=>$component));
982 foreach ($dbfunctions as $dbfunction) {
983 if (empty($functions[$dbfunction->name])) {
984 $DB->delete_records('external_functions', array('id'=>$dbfunction->id));
985 // do not delete functions from external_services_functions, beacuse
986 // we want to notify admins when functions used in custom services disappear
988 //TODO: this looks wrong, we have to delete it eventually (skodak)
989 continue;
992 $function = $functions[$dbfunction->name];
993 unset($functions[$dbfunction->name]);
994 $function['classpath'] = empty($function['classpath']) ? null : $function['classpath'];
996 $update = false;
997 if ($dbfunction->classname != $function['classname']) {
998 $dbfunction->classname = $function['classname'];
999 $update = true;
1001 if ($dbfunction->methodname != $function['methodname']) {
1002 $dbfunction->methodname = $function['methodname'];
1003 $update = true;
1005 if ($dbfunction->classpath != $function['classpath']) {
1006 $dbfunction->classpath = $function['classpath'];
1007 $update = true;
1009 $functioncapabilities = array_key_exists('capabilities', $function)?$function['capabilities']:'';
1010 if ($dbfunction->capabilities != $functioncapabilities) {
1011 $dbfunction->capabilities = $functioncapabilities;
1012 $update = true;
1014 if ($update) {
1015 $DB->update_record('external_functions', $dbfunction);
1018 foreach ($functions as $fname => $function) {
1019 $dbfunction = new stdClass();
1020 $dbfunction->name = $fname;
1021 $dbfunction->classname = $function['classname'];
1022 $dbfunction->methodname = $function['methodname'];
1023 $dbfunction->classpath = empty($function['classpath']) ? null : $function['classpath'];
1024 $dbfunction->component = $component;
1025 $dbfunction->capabilities = array_key_exists('capabilities', $function)?$function['capabilities']:'';
1026 $dbfunction->id = $DB->insert_record('external_functions', $dbfunction);
1028 unset($functions);
1030 // now deal with services
1031 $dbservices = $DB->get_records('external_services', array('component'=>$component));
1032 foreach ($dbservices as $dbservice) {
1033 if (empty($services[$dbservice->name])) {
1034 $DB->delete_records('external_tokens', array('externalserviceid'=>$dbservice->id));
1035 $DB->delete_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
1036 $DB->delete_records('external_services_users', array('externalserviceid'=>$dbservice->id));
1037 $DB->delete_records('external_services', array('id'=>$dbservice->id));
1038 continue;
1040 $service = $services[$dbservice->name];
1041 unset($services[$dbservice->name]);
1042 $service['enabled'] = empty($service['enabled']) ? 0 : $service['enabled'];
1043 $service['requiredcapability'] = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1044 $service['restrictedusers'] = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
1045 $service['downloadfiles'] = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
1046 $service['shortname'] = !isset($service['shortname']) ? null : $service['shortname'];
1048 $update = false;
1049 if ($dbservice->requiredcapability != $service['requiredcapability']) {
1050 $dbservice->requiredcapability = $service['requiredcapability'];
1051 $update = true;
1053 if ($dbservice->restrictedusers != $service['restrictedusers']) {
1054 $dbservice->restrictedusers = $service['restrictedusers'];
1055 $update = true;
1057 if ($dbservice->downloadfiles != $service['downloadfiles']) {
1058 $dbservice->downloadfiles = $service['downloadfiles'];
1059 $update = true;
1061 //if shortname is not a PARAM_ALPHANUMEXT, fail (tested here for service update and creation)
1062 if (isset($service['shortname']) and
1063 (clean_param($service['shortname'], PARAM_ALPHANUMEXT) != $service['shortname'])) {
1064 throw new moodle_exception('installserviceshortnameerror', 'webservice', '', $service['shortname']);
1066 if ($dbservice->shortname != $service['shortname']) {
1067 //check that shortname is unique
1068 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
1069 $existingservice = $DB->get_record('external_services',
1070 array('shortname' => $service['shortname']));
1071 if (!empty($existingservice)) {
1072 throw new moodle_exception('installexistingserviceshortnameerror', 'webservice', '', $service['shortname']);
1075 $dbservice->shortname = $service['shortname'];
1076 $update = true;
1078 if ($update) {
1079 $DB->update_record('external_services', $dbservice);
1082 $functions = $DB->get_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
1083 foreach ($functions as $function) {
1084 $key = array_search($function->functionname, $service['functions']);
1085 if ($key === false) {
1086 $DB->delete_records('external_services_functions', array('id'=>$function->id));
1087 } else {
1088 unset($service['functions'][$key]);
1091 foreach ($service['functions'] as $fname) {
1092 $newf = new stdClass();
1093 $newf->externalserviceid = $dbservice->id;
1094 $newf->functionname = $fname;
1095 $DB->insert_record('external_services_functions', $newf);
1097 unset($functions);
1099 foreach ($services as $name => $service) {
1100 //check that shortname is unique
1101 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
1102 $existingservice = $DB->get_record('external_services',
1103 array('shortname' => $service['shortname']));
1104 if (!empty($existingservice)) {
1105 throw new moodle_exception('installserviceshortnameerror', 'webservice');
1109 $dbservice = new stdClass();
1110 $dbservice->name = $name;
1111 $dbservice->enabled = empty($service['enabled']) ? 0 : $service['enabled'];
1112 $dbservice->requiredcapability = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1113 $dbservice->restrictedusers = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
1114 $dbservice->downloadfiles = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
1115 $dbservice->shortname = !isset($service['shortname']) ? null : $service['shortname'];
1116 $dbservice->component = $component;
1117 $dbservice->timecreated = time();
1118 $dbservice->id = $DB->insert_record('external_services', $dbservice);
1119 foreach ($service['functions'] as $fname) {
1120 $newf = new stdClass();
1121 $newf->externalserviceid = $dbservice->id;
1122 $newf->functionname = $fname;
1123 $DB->insert_record('external_services_functions', $newf);
1129 * upgrade logging functions
1131 function upgrade_handle_exception($ex, $plugin = null) {
1132 global $CFG;
1134 // rollback everything, we need to log all upgrade problems
1135 abort_all_db_transactions();
1137 $info = get_exception_info($ex);
1139 // First log upgrade error
1140 upgrade_log(UPGRADE_LOG_ERROR, $plugin, 'Exception: ' . get_class($ex), $info->message, $info->backtrace);
1142 // Always turn on debugging - admins need to know what is going on
1143 $CFG->debug = DEBUG_DEVELOPER;
1145 default_exception_handler($ex, true, $plugin);
1149 * Adds log entry into upgrade_log table
1151 * @param int $type UPGRADE_LOG_NORMAL, UPGRADE_LOG_NOTICE or UPGRADE_LOG_ERROR
1152 * @param string $plugin frankenstyle component name
1153 * @param string $info short description text of log entry
1154 * @param string $details long problem description
1155 * @param string $backtrace string used for errors only
1156 * @return void
1158 function upgrade_log($type, $plugin, $info, $details=null, $backtrace=null) {
1159 global $DB, $USER, $CFG;
1161 if (empty($plugin)) {
1162 $plugin = 'core';
1165 list($plugintype, $pluginname) = normalize_component($plugin);
1166 $component = is_null($pluginname) ? $plugintype : $plugintype . '_' . $pluginname;
1168 $backtrace = format_backtrace($backtrace, true);
1170 $currentversion = null;
1171 $targetversion = null;
1173 //first try to find out current version number
1174 if ($plugintype === 'core') {
1175 //main
1176 $currentversion = $CFG->version;
1178 $version = null;
1179 include("$CFG->dirroot/version.php");
1180 $targetversion = $version;
1182 } else if ($plugintype === 'mod') {
1183 try {
1184 $currentversion = $DB->get_field('modules', 'version', array('name'=>$pluginname));
1185 $currentversion = ($currentversion === false) ? null : $currentversion;
1186 } catch (Exception $ignored) {
1188 $cd = get_component_directory($component);
1189 if (file_exists("$cd/version.php")) {
1190 $module = new stdClass();
1191 $module->version = null;
1192 include("$cd/version.php");
1193 $targetversion = $module->version;
1196 } else if ($plugintype === 'block') {
1197 try {
1198 if ($block = $DB->get_record('block', array('name'=>$pluginname))) {
1199 $currentversion = $block->version;
1201 } catch (Exception $ignored) {
1203 $cd = get_component_directory($component);
1204 if (file_exists("$cd/version.php")) {
1205 $plugin = new stdClass();
1206 $plugin->version = null;
1207 include("$cd/version.php");
1208 $targetversion = $plugin->version;
1211 } else {
1212 $pluginversion = get_config($component, 'version');
1213 if (!empty($pluginversion)) {
1214 $currentversion = $pluginversion;
1216 $cd = get_component_directory($component);
1217 if (file_exists("$cd/version.php")) {
1218 $plugin = new stdClass();
1219 $plugin->version = null;
1220 include("$cd/version.php");
1221 $targetversion = $plugin->version;
1225 $log = new stdClass();
1226 $log->type = $type;
1227 $log->plugin = $component;
1228 $log->version = $currentversion;
1229 $log->targetversion = $targetversion;
1230 $log->info = $info;
1231 $log->details = $details;
1232 $log->backtrace = $backtrace;
1233 $log->userid = $USER->id;
1234 $log->timemodified = time();
1235 try {
1236 $DB->insert_record('upgrade_log', $log);
1237 } catch (Exception $ignored) {
1238 // possible during install or 2.0 upgrade
1243 * Marks start of upgrade, blocks any other access to site.
1244 * The upgrade is finished at the end of script or after timeout.
1246 * @global object
1247 * @global object
1248 * @global object
1250 function upgrade_started($preinstall=false) {
1251 global $CFG, $DB, $PAGE, $OUTPUT;
1253 static $started = false;
1255 if ($preinstall) {
1256 ignore_user_abort(true);
1257 upgrade_setup_debug(true);
1259 } else if ($started) {
1260 upgrade_set_timeout(120);
1262 } else {
1263 if (!CLI_SCRIPT and !$PAGE->headerprinted) {
1264 $strupgrade = get_string('upgradingversion', 'admin');
1265 $PAGE->set_pagelayout('maintenance');
1266 upgrade_init_javascript();
1267 $PAGE->set_title($strupgrade.' - Moodle '.$CFG->target_release);
1268 $PAGE->set_heading($strupgrade);
1269 $PAGE->navbar->add($strupgrade);
1270 $PAGE->set_cacheable(false);
1271 echo $OUTPUT->header();
1274 ignore_user_abort(true);
1275 register_shutdown_function('upgrade_finished_handler');
1276 upgrade_setup_debug(true);
1277 set_config('upgraderunning', time()+300);
1278 $started = true;
1283 * Internal function - executed if upgrade interrupted.
1285 function upgrade_finished_handler() {
1286 upgrade_finished();
1290 * Indicates upgrade is finished.
1292 * This function may be called repeatedly.
1294 * @global object
1295 * @global object
1297 function upgrade_finished($continueurl=null) {
1298 global $CFG, $DB, $OUTPUT;
1300 if (!empty($CFG->upgraderunning)) {
1301 unset_config('upgraderunning');
1302 // We have to forcefully purge the caches using the writer here.
1303 // This has to be done after we unset the config var. If someone hits the site while this is set they will
1304 // cause the config values to propogate to the caches.
1305 // Caches are purged after the last step in an upgrade but there is several code routines that exceute between
1306 // then and now that leaving a window for things to fall out of sync.
1307 cache_helper::purge_all(true);
1308 upgrade_setup_debug(false);
1309 ignore_user_abort(false);
1310 if ($continueurl) {
1311 echo $OUTPUT->continue_button($continueurl);
1312 echo $OUTPUT->footer();
1313 die;
1319 * @global object
1320 * @global object
1322 function upgrade_setup_debug($starting) {
1323 global $CFG, $DB;
1325 static $originaldebug = null;
1327 if ($starting) {
1328 if ($originaldebug === null) {
1329 $originaldebug = $DB->get_debug();
1331 if (!empty($CFG->upgradeshowsql)) {
1332 $DB->set_debug(true);
1334 } else {
1335 $DB->set_debug($originaldebug);
1339 function print_upgrade_separator() {
1340 if (!CLI_SCRIPT) {
1341 echo '<hr />';
1346 * Default start upgrade callback
1347 * @param string $plugin
1348 * @param bool $installation true if installation, false means upgrade
1350 function print_upgrade_part_start($plugin, $installation, $verbose) {
1351 global $OUTPUT;
1352 if (empty($plugin) or $plugin == 'moodle') {
1353 upgrade_started($installation); // does not store upgrade running flag yet
1354 if ($verbose) {
1355 echo $OUTPUT->heading(get_string('coresystem'));
1357 } else {
1358 upgrade_started();
1359 if ($verbose) {
1360 echo $OUTPUT->heading($plugin);
1363 if ($installation) {
1364 if (empty($plugin) or $plugin == 'moodle') {
1365 // no need to log - log table not yet there ;-)
1366 } else {
1367 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin installation');
1369 } else {
1370 if (empty($plugin) or $plugin == 'moodle') {
1371 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting core upgrade');
1372 } else {
1373 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin upgrade');
1379 * Default end upgrade callback
1380 * @param string $plugin
1381 * @param bool $installation true if installation, false means upgrade
1383 function print_upgrade_part_end($plugin, $installation, $verbose) {
1384 global $OUTPUT;
1385 upgrade_started();
1386 if ($installation) {
1387 if (empty($plugin) or $plugin == 'moodle') {
1388 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core installed');
1389 } else {
1390 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin installed');
1392 } else {
1393 if (empty($plugin) or $plugin == 'moodle') {
1394 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core upgraded');
1395 } else {
1396 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin upgraded');
1399 if ($verbose) {
1400 echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
1401 print_upgrade_separator();
1406 * Sets up JS code required for all upgrade scripts.
1407 * @global object
1409 function upgrade_init_javascript() {
1410 global $PAGE;
1411 // scroll to the end of each upgrade page so that ppl see either error or continue button,
1412 // no need to scroll continuously any more, it is enough to jump to end once the footer is printed ;-)
1413 $js = "window.scrollTo(0, 5000000);";
1414 $PAGE->requires->js_init_code($js);
1418 * Try to upgrade the given language pack (or current language)
1420 * @param string $lang the code of the language to update, defaults to the current language
1422 function upgrade_language_pack($lang = null) {
1423 global $CFG;
1425 if (!empty($CFG->skiplangupgrade)) {
1426 return;
1429 if (!file_exists("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php")) {
1430 // weird, somebody uninstalled the import utility
1431 return;
1434 if (!$lang) {
1435 $lang = current_language();
1438 if (!get_string_manager()->translation_exists($lang)) {
1439 return;
1442 get_string_manager()->reset_caches();
1444 if ($lang === 'en') {
1445 return; // Nothing to do
1448 upgrade_started(false);
1450 require_once("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php");
1451 tool_langimport_preupgrade_update($lang);
1453 get_string_manager()->reset_caches();
1455 print_upgrade_separator();
1459 * Install core moodle tables and initialize
1460 * @param float $version target version
1461 * @param bool $verbose
1462 * @return void, may throw exception
1464 function install_core($version, $verbose) {
1465 global $CFG, $DB;
1467 // We can not call purge_all_caches() yet, make sure the temp and cache dirs exist and are empty.
1468 make_cache_directory('', true);
1469 remove_dir($CFG->cachedir.'', true);
1470 make_temp_directory('', true);
1471 remove_dir($CFG->tempdir.'', true);
1472 make_writable_directory($CFG->dataroot.'/muc', true);
1473 remove_dir($CFG->dataroot.'/muc', true);
1475 try {
1476 set_time_limit(600);
1477 print_upgrade_part_start('moodle', true, $verbose); // does not store upgrade running flag
1479 $DB->get_manager()->install_from_xmldb_file("$CFG->libdir/db/install.xml");
1480 upgrade_started(); // we want the flag to be stored in config table ;-)
1482 // set all core default records and default settings
1483 require_once("$CFG->libdir/db/install.php");
1484 xmldb_main_install(); // installs the capabilities too
1486 // store version
1487 upgrade_main_savepoint(true, $version, false);
1489 // Continue with the installation
1490 log_update_descriptions('moodle');
1491 external_update_descriptions('moodle');
1492 events_update_definition('moodle');
1493 message_update_providers('moodle');
1495 // Write default settings unconditionally
1496 admin_apply_default_settings(NULL, true);
1498 print_upgrade_part_end(null, true, $verbose);
1500 // Purge all caches. They're disabled but this ensures that we don't have any persistent data just in case something
1501 // during installation didn't use APIs.
1502 cache_helper::purge_all();
1503 } catch (exception $ex) {
1504 upgrade_handle_exception($ex);
1509 * Upgrade moodle core
1510 * @param float $version target version
1511 * @param bool $verbose
1512 * @return void, may throw exception
1514 function upgrade_core($version, $verbose) {
1515 global $CFG;
1517 raise_memory_limit(MEMORY_EXTRA);
1519 require_once($CFG->libdir.'/db/upgrade.php'); // Defines upgrades
1521 try {
1522 // Reset caches before any output
1523 purge_all_caches();
1524 cache_helper::purge_all(true);
1526 // Upgrade current language pack if we can
1527 upgrade_language_pack();
1529 print_upgrade_part_start('moodle', false, $verbose);
1531 // Pre-upgrade scripts for local hack workarounds.
1532 $preupgradefile = "$CFG->dirroot/local/preupgrade.php";
1533 if (file_exists($preupgradefile)) {
1534 set_time_limit(0);
1535 require($preupgradefile);
1536 // Reset upgrade timeout to default.
1537 upgrade_set_timeout();
1540 $result = xmldb_main_upgrade($CFG->version);
1541 if ($version > $CFG->version) {
1542 // store version if not already there
1543 upgrade_main_savepoint($result, $version, false);
1546 // perform all other component upgrade routines
1547 update_capabilities('moodle');
1548 log_update_descriptions('moodle');
1549 external_update_descriptions('moodle');
1550 events_update_definition('moodle');
1551 message_update_providers('moodle');
1552 // Update core definitions.
1553 cache_helper::update_definitions(true);
1555 // Purge caches again, just to be sure we arn't holding onto old stuff now.
1556 purge_all_caches();
1557 cache_helper::purge_all(true);
1559 // Clean up contexts - more and more stuff depends on existence of paths and contexts
1560 context_helper::cleanup_instances();
1561 context_helper::create_instances(null, false);
1562 context_helper::build_all_paths(false);
1563 $syscontext = context_system::instance();
1564 $syscontext->mark_dirty();
1566 print_upgrade_part_end('moodle', false, $verbose);
1567 } catch (Exception $ex) {
1568 upgrade_handle_exception($ex);
1573 * Upgrade/install other parts of moodle
1574 * @param bool $verbose
1575 * @return void, may throw exception
1577 function upgrade_noncore($verbose) {
1578 global $CFG;
1580 raise_memory_limit(MEMORY_EXTRA);
1582 // upgrade all plugins types
1583 try {
1584 $plugintypes = get_plugin_types();
1585 foreach ($plugintypes as $type=>$location) {
1586 upgrade_plugins($type, 'print_upgrade_part_start', 'print_upgrade_part_end', $verbose);
1588 // Update cache definitions. Involves scanning each plugin for any changes.
1589 cache_helper::update_definitions();
1590 } catch (Exception $ex) {
1591 upgrade_handle_exception($ex);
1596 * Checks if the main tables have been installed yet or not.
1598 * Note: we can not use caches here because they might be stale,
1599 * use with care!
1601 * @return bool
1603 function core_tables_exist() {
1604 global $DB;
1606 if (!$tables = $DB->get_tables(false) ) { // No tables yet at all.
1607 return false;
1609 } else { // Check for missing main tables
1610 $mtables = array('config', 'course', 'groupings'); // some tables used in 1.9 and 2.0, preferable something from the start and end of install.xml
1611 foreach ($mtables as $mtable) {
1612 if (!in_array($mtable, $tables)) {
1613 return false;
1616 return true;
1621 * upgrades the mnet rpc definitions for the given component.
1622 * this method doesn't return status, an exception will be thrown in the case of an error
1624 * @param string $component the plugin to upgrade, eg auth_mnet
1626 function upgrade_plugin_mnet_functions($component) {
1627 global $DB, $CFG;
1629 list($type, $plugin) = explode('_', $component);
1630 $path = get_plugin_directory($type, $plugin);
1632 $publishes = array();
1633 $subscribes = array();
1634 if (file_exists($path . '/db/mnet.php')) {
1635 require_once($path . '/db/mnet.php'); // $publishes comes from this file
1637 if (empty($publishes)) {
1638 $publishes = array(); // still need this to be able to disable stuff later
1640 if (empty($subscribes)) {
1641 $subscribes = array(); // still need this to be able to disable stuff later
1644 static $servicecache = array();
1646 // rekey an array based on the rpc method for easy lookups later
1647 $publishmethodservices = array();
1648 $subscribemethodservices = array();
1649 foreach($publishes as $servicename => $service) {
1650 if (is_array($service['methods'])) {
1651 foreach($service['methods'] as $methodname) {
1652 $service['servicename'] = $servicename;
1653 $publishmethodservices[$methodname][] = $service;
1658 // Disable functions that don't exist (any more) in the source
1659 // Should these be deleted? What about their permissions records?
1660 foreach ($DB->get_records('mnet_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
1661 if (!array_key_exists($rpc->functionname, $publishmethodservices) && $rpc->enabled) {
1662 $DB->set_field('mnet_rpc', 'enabled', 0, array('id' => $rpc->id));
1663 } else if (array_key_exists($rpc->functionname, $publishmethodservices) && !$rpc->enabled) {
1664 $DB->set_field('mnet_rpc', 'enabled', 1, array('id' => $rpc->id));
1668 // reflect all the services we're publishing and save them
1669 require_once($CFG->dirroot . '/lib/zend/Zend/Server/Reflection.php');
1670 static $cachedclasses = array(); // to store reflection information in
1671 foreach ($publishes as $service => $data) {
1672 $f = $data['filename'];
1673 $c = $data['classname'];
1674 foreach ($data['methods'] as $method) {
1675 $dataobject = new stdClass();
1676 $dataobject->plugintype = $type;
1677 $dataobject->pluginname = $plugin;
1678 $dataobject->enabled = 1;
1679 $dataobject->classname = $c;
1680 $dataobject->filename = $f;
1682 if (is_string($method)) {
1683 $dataobject->functionname = $method;
1685 } else if (is_array($method)) { // wants to override file or class
1686 $dataobject->functionname = $method['method'];
1687 $dataobject->classname = $method['classname'];
1688 $dataobject->filename = $method['filename'];
1690 $dataobject->xmlrpcpath = $type.'/'.$plugin.'/'.$dataobject->filename.'/'.$method;
1691 $dataobject->static = false;
1693 require_once($path . '/' . $dataobject->filename);
1694 $functionreflect = null; // slightly different ways to get this depending on whether it's a class method or a function
1695 if (!empty($dataobject->classname)) {
1696 if (!class_exists($dataobject->classname)) {
1697 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
1699 $key = $dataobject->filename . '|' . $dataobject->classname;
1700 if (!array_key_exists($key, $cachedclasses)) { // look to see if we've already got a reflection object
1701 try {
1702 $cachedclasses[$key] = Zend_Server_Reflection::reflectClass($dataobject->classname);
1703 } catch (Zend_Server_Reflection_Exception $e) { // catch these and rethrow them to something more helpful
1704 throw new moodle_exception('installreflectionclasserror', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname, 'error' => $e->getMessage()));
1707 $r =& $cachedclasses[$key];
1708 if (!$r->hasMethod($dataobject->functionname)) {
1709 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
1711 // stupid workaround for zend not having a getMethod($name) function
1712 $ms = $r->getMethods();
1713 foreach ($ms as $m) {
1714 if ($m->getName() == $dataobject->functionname) {
1715 $functionreflect = $m;
1716 break;
1719 $dataobject->static = (int)$functionreflect->isStatic();
1720 } else {
1721 if (!function_exists($dataobject->functionname)) {
1722 throw new moodle_exception('installnosuchfunction', 'mnet', '', (object)array('method' => $dataobject->functionname, 'file' => $dataobject->filename));
1724 try {
1725 $functionreflect = Zend_Server_Reflection::reflectFunction($dataobject->functionname);
1726 } catch (Zend_Server_Reflection_Exception $e) { // catch these and rethrow them to something more helpful
1727 throw new moodle_exception('installreflectionfunctionerror', 'mnet', '', (object)array('method' => $dataobject->functionname, '' => $dataobject->filename, 'error' => $e->getMessage()));
1730 $dataobject->profile = serialize(admin_mnet_method_profile($functionreflect));
1731 $dataobject->help = $functionreflect->getDescription();
1733 if ($record_exists = $DB->get_record('mnet_rpc', array('xmlrpcpath'=>$dataobject->xmlrpcpath))) {
1734 $dataobject->id = $record_exists->id;
1735 $dataobject->enabled = $record_exists->enabled;
1736 $DB->update_record('mnet_rpc', $dataobject);
1737 } else {
1738 $dataobject->id = $DB->insert_record('mnet_rpc', $dataobject, true);
1741 // TODO this API versioning must be reworked, here the recently processed method
1742 // sets the service API which may not be correct
1743 foreach ($publishmethodservices[$dataobject->functionname] as $service) {
1744 if ($serviceobj = $DB->get_record('mnet_service', array('name'=>$service['servicename']))) {
1745 $serviceobj->apiversion = $service['apiversion'];
1746 $DB->update_record('mnet_service', $serviceobj);
1747 } else {
1748 $serviceobj = new stdClass();
1749 $serviceobj->name = $service['servicename'];
1750 $serviceobj->description = empty($service['description']) ? '' : $service['description'];
1751 $serviceobj->apiversion = $service['apiversion'];
1752 $serviceobj->offer = 1;
1753 $serviceobj->id = $DB->insert_record('mnet_service', $serviceobj);
1755 $servicecache[$service['servicename']] = $serviceobj;
1756 if (!$DB->record_exists('mnet_service2rpc', array('rpcid'=>$dataobject->id, 'serviceid'=>$serviceobj->id))) {
1757 $obj = new stdClass();
1758 $obj->rpcid = $dataobject->id;
1759 $obj->serviceid = $serviceobj->id;
1760 $DB->insert_record('mnet_service2rpc', $obj, true);
1765 // finished with methods we publish, now do subscribable methods
1766 foreach($subscribes as $service => $methods) {
1767 if (!array_key_exists($service, $servicecache)) {
1768 if (!$serviceobj = $DB->get_record('mnet_service', array('name' => $service))) {
1769 debugging("TODO: skipping unknown service $service - somebody needs to fix MDL-21993");
1770 continue;
1772 $servicecache[$service] = $serviceobj;
1773 } else {
1774 $serviceobj = $servicecache[$service];
1776 foreach ($methods as $method => $xmlrpcpath) {
1777 if (!$rpcid = $DB->get_field('mnet_remote_rpc', 'id', array('xmlrpcpath'=>$xmlrpcpath))) {
1778 $remoterpc = (object)array(
1779 'functionname' => $method,
1780 'xmlrpcpath' => $xmlrpcpath,
1781 'plugintype' => $type,
1782 'pluginname' => $plugin,
1783 'enabled' => 1,
1785 $rpcid = $remoterpc->id = $DB->insert_record('mnet_remote_rpc', $remoterpc, true);
1787 if (!$DB->record_exists('mnet_remote_service2rpc', array('rpcid'=>$rpcid, 'serviceid'=>$serviceobj->id))) {
1788 $obj = new stdClass();
1789 $obj->rpcid = $rpcid;
1790 $obj->serviceid = $serviceobj->id;
1791 $DB->insert_record('mnet_remote_service2rpc', $obj, true);
1793 $subscribemethodservices[$method][] = $service;
1797 foreach ($DB->get_records('mnet_remote_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
1798 if (!array_key_exists($rpc->functionname, $subscribemethodservices) && $rpc->enabled) {
1799 $DB->set_field('mnet_remote_rpc', 'enabled', 0, array('id' => $rpc->id));
1800 } else if (array_key_exists($rpc->functionname, $subscribemethodservices) && !$rpc->enabled) {
1801 $DB->set_field('mnet_remote_rpc', 'enabled', 1, array('id' => $rpc->id));
1805 return true;
1809 * Given some sort of Zend Reflection function/method object, return a profile array, ready to be serialized and stored
1811 * @param Zend_Server_Reflection_Function_Abstract $function can be any subclass of this object type
1813 * @return array
1815 function admin_mnet_method_profile(Zend_Server_Reflection_Function_Abstract $function) {
1816 $protos = $function->getPrototypes();
1817 $proto = array_pop($protos);
1818 $ret = $proto->getReturnValue();
1819 $profile = array(
1820 'parameters' => array(),
1821 'return' => array(
1822 'type' => $ret->getType(),
1823 'description' => $ret->getDescription(),
1826 foreach ($proto->getParameters() as $p) {
1827 $profile['parameters'][] = array(
1828 'name' => $p->getName(),
1829 'type' => $p->getType(),
1830 'description' => $p->getDescription(),
1833 return $profile;
1838 * This function finds duplicate records (based on combinations of fields that should be unique)
1839 * and then progamatically generated a "most correct" version of the data, update and removing
1840 * records as appropriate
1842 * Thanks to Dan Marsden for help
1844 * @param string $table Table name
1845 * @param array $uniques Array of field names that should be unique
1846 * @param array $fieldstocheck Array of fields to generate "correct" data from (optional)
1847 * @return void
1849 function upgrade_course_completion_remove_duplicates($table, $uniques, $fieldstocheck = array()) {
1850 global $DB;
1852 // Find duplicates
1853 $sql_cols = implode(', ', $uniques);
1855 $sql = "SELECT {$sql_cols} FROM {{$table}} GROUP BY {$sql_cols} HAVING (count(id) > 1)";
1856 $duplicates = $DB->get_recordset_sql($sql, array());
1858 // Loop through duplicates
1859 foreach ($duplicates as $duplicate) {
1860 $pointer = 0;
1862 // Generate SQL for finding records with these duplicate uniques
1863 $sql_select = implode(' = ? AND ', $uniques).' = ?'; // builds "fieldname = ? AND fieldname = ?"
1864 $uniq_values = array();
1865 foreach ($uniques as $u) {
1866 $uniq_values[] = $duplicate->$u;
1869 $sql_order = implode(' DESC, ', $uniques).' DESC'; // builds "fieldname DESC, fieldname DESC"
1871 // Get records with these duplicate uniques
1872 $records = $DB->get_records_select(
1873 $table,
1874 $sql_select,
1875 $uniq_values,
1876 $sql_order
1879 // Loop through and build a "correct" record, deleting the others
1880 $needsupdate = false;
1881 $origrecord = null;
1882 foreach ($records as $record) {
1883 $pointer++;
1884 if ($pointer === 1) { // keep 1st record but delete all others.
1885 $origrecord = $record;
1886 } else {
1887 // If we have fields to check, update original record
1888 if ($fieldstocheck) {
1889 // we need to keep the "oldest" of all these fields as the valid completion record.
1890 // but we want to ignore null values
1891 foreach ($fieldstocheck as $f) {
1892 if ($record->$f && (($origrecord->$f > $record->$f) || !$origrecord->$f)) {
1893 $origrecord->$f = $record->$f;
1894 $needsupdate = true;
1898 $DB->delete_records($table, array('id' => $record->id));
1901 if ($needsupdate || isset($origrecord->reaggregate)) {
1902 // If this table has a reaggregate field, update to force recheck on next cron run
1903 if (isset($origrecord->reaggregate)) {
1904 $origrecord->reaggregate = time();
1906 $DB->update_record($table, $origrecord);
1912 * Find questions missing an existing category and associate them with
1913 * a category which purpose is to gather them.
1915 * @return void
1917 function upgrade_save_orphaned_questions() {
1918 global $DB;
1920 // Looking for orphaned questions
1921 $orphans = $DB->record_exists_select('question',
1922 'NOT EXISTS (SELECT 1 FROM {question_categories} WHERE {question_categories}.id = {question}.category)');
1923 if (!$orphans) {
1924 return;
1927 // Generate a unique stamp for the orphaned questions category, easier to identify it later on
1928 $uniquestamp = "unknownhost+120719170400+orphan";
1929 $systemcontext = context_system::instance();
1931 // Create the orphaned category at system level
1932 $cat = $DB->get_record('question_categories', array('stamp' => $uniquestamp,
1933 'contextid' => $systemcontext->id));
1934 if (!$cat) {
1935 $cat = new stdClass();
1936 $cat->parent = 0;
1937 $cat->contextid = $systemcontext->id;
1938 $cat->name = get_string('orphanedquestionscategory', 'question');
1939 $cat->info = get_string('orphanedquestionscategoryinfo', 'question');
1940 $cat->sortorder = 999;
1941 $cat->stamp = $uniquestamp;
1942 $cat->id = $DB->insert_record("question_categories", $cat);
1945 // Set a category to those orphans
1946 $params = array('catid' => $cat->id);
1947 $DB->execute('UPDATE {question} SET category = :catid WHERE NOT EXISTS
1948 (SELECT 1 FROM {question_categories} WHERE {question_categories}.id = {question}.category)', $params);
1952 * Rename old backup files to current backup files.
1954 * When added the setting 'backup_shortname' (MDL-28657) the backup file names did not contain the id of the course.
1955 * Further we fixed that behaviour by forcing the id to be always present in the file name (MDL-33812).
1956 * This function will explore the backup directory and attempt to rename the previously created files to include
1957 * the id in the name. Doing this will put them back in the process of deleting the excess backups for each course.
1959 * This function manually recreates the file name, instead of using
1960 * {@link backup_plan_dbops::get_default_backup_filename()}, use it carefully if you're using it outside of the
1961 * usual upgrade process.
1963 * @see backup_cron_automated_helper::remove_excess_backups()
1964 * @link http://tracker.moodle.org/browse/MDL-35116
1965 * @return void
1966 * @since 2.4
1968 function upgrade_rename_old_backup_files_using_shortname() {
1969 global $CFG;
1970 $dir = get_config('backup', 'backup_auto_destination');
1971 $useshortname = get_config('backup', 'backup_shortname');
1972 if (empty($dir) || !is_dir($dir) || !is_writable($dir)) {
1973 return;
1976 require_once($CFG->libdir.'/textlib.class.php');
1977 require_once($CFG->dirroot.'/backup/util/includes/backup_includes.php');
1978 $backupword = str_replace(' ', '_', textlib::strtolower(get_string('backupfilename')));
1979 $backupword = trim(clean_filename($backupword), '_');
1980 $filename = $backupword . '-' . backup::FORMAT_MOODLE . '-' . backup::TYPE_1COURSE . '-';
1981 $regex = '#^'.preg_quote($filename, '#').'.*\.mbz$#';
1982 $thirtyapril = strtotime('30 April 2012 00:00');
1984 // Reading the directory.
1985 if (!$files = scandir($dir)) {
1986 return;
1988 foreach ($files as $file) {
1989 // Skip directories and files which do not start with the common prefix.
1990 // This avoids working on files which are not related to this issue.
1991 if (!is_file($dir . '/' . $file) || !preg_match($regex, $file)) {
1992 continue;
1995 // Extract the information from the XML file.
1996 try {
1997 $bcinfo = backup_general_helper::get_backup_information_from_mbz($dir . '/' . $file);
1998 } catch (backup_helper_exception $e) {
1999 // Some error while retrieving the backup informations, skipping...
2000 continue;
2003 // Make sure this a course backup.
2004 if ($bcinfo->format !== backup::FORMAT_MOODLE || $bcinfo->type !== backup::TYPE_1COURSE) {
2005 continue;
2008 // Skip the backups created before the short name option was initially introduced (MDL-28657).
2009 // This was integrated on the 2nd of May 2012. Let's play safe with timezone and use the 30th of April.
2010 if ($bcinfo->backup_date < $thirtyapril) {
2011 continue;
2014 // Let's check if the file name contains the ID where it is supposed to be, if it is the case then
2015 // we will skip the file. Of course it could happen that the course ID is identical to the course short name
2016 // even though really unlikely, but then renaming this file is not necessary. If the ID is not found in the
2017 // file name then it was probably the short name which was used.
2018 $idfilename = $filename . $bcinfo->original_course_id . '-';
2019 $idregex = '#^'.preg_quote($idfilename, '#').'.*\.mbz$#';
2020 if (preg_match($idregex, $file)) {
2021 continue;
2024 // Generating the file name manually. We do not use backup_plan_dbops::get_default_backup_filename() because
2025 // it will query the database to get some course information, and the course could not exist any more.
2026 $newname = $filename . $bcinfo->original_course_id . '-';
2027 if ($useshortname) {
2028 $shortname = str_replace(' ', '_', $bcinfo->original_course_shortname);
2029 $shortname = textlib::strtolower(trim(clean_filename($shortname), '_'));
2030 $newname .= $shortname . '-';
2033 $backupdateformat = str_replace(' ', '_', get_string('backupnameformat', 'langconfig'));
2034 $date = userdate($bcinfo->backup_date, $backupdateformat, 99, false);
2035 $date = textlib::strtolower(trim(clean_filename($date), '_'));
2036 $newname .= $date;
2038 if (isset($bcinfo->root_settings['users']) && !$bcinfo->root_settings['users']) {
2039 $newname .= '-nu';
2040 } else if (isset($bcinfo->root_settings['anonymize']) && $bcinfo->root_settings['anonymize']) {
2041 $newname .= '-an';
2043 $newname .= '.mbz';
2045 // Final check before attempting the renaming.
2046 if ($newname == $file || file_exists($dir . '/' . $newname)) {
2047 continue;
2049 @rename($dir . '/' . $file, $dir . '/' . $newname);