MDL-41197 normalize ascii text conversion
[moodle.git] / lib / upgradelib.php
blobf9eda590c1ea1c1f86a5cc9d1636a2eb03861111
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 $component = 'mod_'.$modname;
227 if (!$result) {
228 throw new upgrade_exception($component, $version);
231 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
233 if (!$module = $DB->get_record('modules', array('name'=>$modname))) {
234 print_error('modulenotexist', 'debug', '', $modname);
237 if ($dbversion >= $version) {
238 // something really wrong is going on in upgrade script
239 throw new downgrade_exception($component, $dbversion, $version);
241 set_config('version', $version, $component);
243 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
245 // reset upgrade timeout to default
246 upgrade_set_timeout();
248 // this is a safe place to stop upgrades if user aborts page loading
249 if ($allowabort and connection_aborted()) {
250 die;
255 * Blocks upgrade savepoint, marks end of blocks upgrade blocks
256 * It stores block version, resets upgrade timeout
257 * and abort upgrade if user cancels page loading.
259 * @category upgrade
260 * @param bool $result false if upgrade step failed, true if completed
261 * @param string or float $version main version
262 * @param string $blockname name of block
263 * @param bool $allowabort allow user to abort script execution here
264 * @return void
266 function upgrade_block_savepoint($result, $version, $blockname, $allowabort=true) {
267 global $DB;
269 $component = 'block_'.$blockname;
271 if (!$result) {
272 throw new upgrade_exception($component, $version);
275 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
277 if (!$block = $DB->get_record('block', array('name'=>$blockname))) {
278 print_error('blocknotexist', 'debug', '', $blockname);
281 if ($dbversion >= $version) {
282 // something really wrong is going on in upgrade script
283 throw new downgrade_exception($component, $dbversion, $version);
285 set_config('version', $version, $component);
287 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
289 // reset upgrade timeout to default
290 upgrade_set_timeout();
292 // this is a safe place to stop upgrades if user aborts page loading
293 if ($allowabort and connection_aborted()) {
294 die;
299 * Plugins upgrade savepoint, marks end of blocks upgrade blocks
300 * It stores plugin version, resets upgrade timeout
301 * and abort upgrade if user cancels page loading.
303 * @category upgrade
304 * @param bool $result false if upgrade step failed, true if completed
305 * @param string or float $version main version
306 * @param string $type name of plugin
307 * @param string $dir location of plugin
308 * @param bool $allowabort allow user to abort script execution here
309 * @return void
311 function upgrade_plugin_savepoint($result, $version, $type, $plugin, $allowabort=true) {
312 global $DB;
314 $component = $type.'_'.$plugin;
316 if (!$result) {
317 throw new upgrade_exception($component, $version);
320 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
322 if ($dbversion >= $version) {
323 // Something really wrong is going on in the upgrade script
324 throw new downgrade_exception($component, $dbversion, $version);
326 set_config('version', $version, $component);
327 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
329 // Reset upgrade timeout to default
330 upgrade_set_timeout();
332 // This is a safe place to stop upgrades if user aborts page loading
333 if ($allowabort and connection_aborted()) {
334 die;
339 * Detect if there are leftovers in PHP source files.
341 * During main version upgrades administrators MUST move away
342 * old PHP source files and start from scratch (or better
343 * use git).
345 * @return bool true means borked upgrade, false means previous PHP files were properly removed
347 function upgrade_stale_php_files_present() {
348 global $CFG;
350 $someexamplesofremovedfiles = array(
351 // removed in 2.6dev
352 '/admin/block.php',
353 '/admin/oacleanup.php',
354 // removed in 2.5dev
355 '/backup/lib.php',
356 '/backup/bb/README.txt',
357 '/lib/excel/test.php',
358 // removed in 2.4dev
359 '/admin/tool/unittest/simpletestlib.php',
360 // removed in 2.3dev
361 '/lib/minify/builder/',
362 // removed in 2.2dev
363 '/lib/yui/3.4.1pr1/',
364 // removed in 2.2
365 '/search/cron_php5.php',
366 '/course/report/log/indexlive.php',
367 '/admin/report/backups/index.php',
368 '/admin/generator.php',
369 // removed in 2.1
370 '/lib/yui/2.8.0r4/',
371 // removed in 2.0
372 '/blocks/admin/block_admin.php',
373 '/blocks/admin_tree/block_admin_tree.php',
376 foreach ($someexamplesofremovedfiles as $file) {
377 if (file_exists($CFG->dirroot.$file)) {
378 return true;
382 return false;
386 * Upgrade plugins
387 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
388 * return void
390 function upgrade_plugins($type, $startcallback, $endcallback, $verbose) {
391 global $CFG, $DB;
393 /// special cases
394 if ($type === 'mod') {
395 return upgrade_plugins_modules($startcallback, $endcallback, $verbose);
396 } else if ($type === 'block') {
397 return upgrade_plugins_blocks($startcallback, $endcallback, $verbose);
400 $plugs = core_component::get_plugin_list($type);
402 foreach ($plugs as $plug=>$fullplug) {
403 // Reset time so that it works when installing a large number of plugins
404 set_time_limit(600);
405 $component = clean_param($type.'_'.$plug, PARAM_COMPONENT); // standardised plugin name
407 // check plugin dir is valid name
408 if (empty($component)) {
409 throw new plugin_defective_exception($type.'_'.$plug, 'Invalid plugin directory name.');
412 if (!is_readable($fullplug.'/version.php')) {
413 continue;
416 $plugin = new stdClass();
417 $plugin->version = null;
418 $module = $plugin; // Prevent some notices when plugin placed in wrong directory.
419 require($fullplug.'/version.php'); // defines $plugin with version etc
420 unset($module);
422 // if plugin tells us it's full name we may check the location
423 if (isset($plugin->component)) {
424 if ($plugin->component !== $component) {
425 $current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullplug);
426 $expected = str_replace($CFG->dirroot, '$CFG->dirroot', core_component::get_component_directory($plugin->component));
427 throw new plugin_misplaced_exception($component, $expected, $current);
431 if (empty($plugin->version)) {
432 throw new plugin_defective_exception($component, 'Missing version value in version.php');
435 $plugin->name = $plug;
436 $plugin->fullname = $component;
438 if (!empty($plugin->requires)) {
439 if ($plugin->requires > $CFG->version) {
440 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
441 } else if ($plugin->requires < 2010000000) {
442 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
446 // try to recover from interrupted install.php if needed
447 if (file_exists($fullplug.'/db/install.php')) {
448 if (get_config($plugin->fullname, 'installrunning')) {
449 require_once($fullplug.'/db/install.php');
450 $recover_install_function = 'xmldb_'.$plugin->fullname.'_install_recovery';
451 if (function_exists($recover_install_function)) {
452 $startcallback($component, true, $verbose);
453 $recover_install_function();
454 unset_config('installrunning', $plugin->fullname);
455 update_capabilities($component);
456 log_update_descriptions($component);
457 external_update_descriptions($component);
458 events_update_definition($component);
459 message_update_providers($component);
460 if ($type === 'message') {
461 message_update_processors($plug);
463 upgrade_plugin_mnet_functions($component);
464 $endcallback($component, true, $verbose);
469 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
470 if (empty($installedversion)) { // new installation
471 $startcallback($component, true, $verbose);
473 /// Install tables if defined
474 if (file_exists($fullplug.'/db/install.xml')) {
475 $DB->get_manager()->install_from_xmldb_file($fullplug.'/db/install.xml');
478 /// store version
479 upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false);
481 /// execute post install file
482 if (file_exists($fullplug.'/db/install.php')) {
483 require_once($fullplug.'/db/install.php');
484 set_config('installrunning', 1, $plugin->fullname);
485 $post_install_function = 'xmldb_'.$plugin->fullname.'_install';
486 $post_install_function();
487 unset_config('installrunning', $plugin->fullname);
490 /// Install various components
491 update_capabilities($component);
492 log_update_descriptions($component);
493 external_update_descriptions($component);
494 events_update_definition($component);
495 message_update_providers($component);
496 if ($type === 'message') {
497 message_update_processors($plug);
499 upgrade_plugin_mnet_functions($component);
500 $endcallback($component, true, $verbose);
502 } else if ($installedversion < $plugin->version) { // upgrade
503 /// Run the upgrade function for the plugin.
504 $startcallback($component, false, $verbose);
506 if (is_readable($fullplug.'/db/upgrade.php')) {
507 require_once($fullplug.'/db/upgrade.php'); // defines upgrading function
509 $newupgrade_function = 'xmldb_'.$plugin->fullname.'_upgrade';
510 $result = $newupgrade_function($installedversion);
511 } else {
512 $result = true;
515 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
516 if ($installedversion < $plugin->version) {
517 // store version if not already there
518 upgrade_plugin_savepoint($result, $plugin->version, $type, $plug, false);
521 /// Upgrade various components
522 update_capabilities($component);
523 log_update_descriptions($component);
524 external_update_descriptions($component);
525 events_update_definition($component);
526 message_update_providers($component);
527 if ($type === 'message') {
528 // Ugly hack!
529 message_update_processors($plug);
531 upgrade_plugin_mnet_functions($component);
532 $endcallback($component, false, $verbose);
534 } else if ($installedversion > $plugin->version) {
535 throw new downgrade_exception($component, $installedversion, $plugin->version);
541 * Find and check all modules and load them up or upgrade them if necessary
543 * @global object
544 * @global object
546 function upgrade_plugins_modules($startcallback, $endcallback, $verbose) {
547 global $CFG, $DB;
549 $mods = core_component::get_plugin_list('mod');
551 foreach ($mods as $mod=>$fullmod) {
553 if ($mod === 'NEWMODULE') { // Someone has unzipped the template, ignore it
554 continue;
557 $component = clean_param('mod_'.$mod, PARAM_COMPONENT);
559 // check module dir is valid name
560 if (empty($component)) {
561 throw new plugin_defective_exception('mod_'.$mod, 'Invalid plugin directory name.');
564 if (!is_readable($fullmod.'/version.php')) {
565 throw new plugin_defective_exception($component, 'Missing version.php');
568 $plugin = new stdClass();
569 $plugin->version = null;
570 $module = $plugin;
571 require($fullmod .'/version.php'); // Defines $module/$plugin with version etc.
572 $plugin = clone($module);
573 unset($module->version);
574 unset($module->component);
575 unset($module->dependencies);
576 unset($module->release);
578 // if plugin tells us it's full name we may check the location
579 if (isset($plugin->component)) {
580 if ($plugin->component !== $component) {
581 $current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullmod);
582 $expected = str_replace($CFG->dirroot, '$CFG->dirroot', core_component::get_component_directory($plugin->component));
583 throw new plugin_misplaced_exception($component, $expected, $current);
587 if (empty($plugin->version)) {
588 // Version must be always set now!
589 throw new plugin_defective_exception($component, 'Missing version value in version.php');
592 if (!empty($plugin->requires)) {
593 if ($plugin->requires > $CFG->version) {
594 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
595 } else if ($plugin->requires < 2010000000) {
596 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
600 if (empty($module->cron)) {
601 $module->cron = 0;
604 // all modules must have en lang pack
605 if (!is_readable("$fullmod/lang/en/$mod.php")) {
606 throw new plugin_defective_exception($component, 'Missing mandatory en language pack.');
609 $module->name = $mod; // The name MUST match the directory
611 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
613 if (file_exists($fullmod.'/db/install.php')) {
614 if (get_config($module->name, 'installrunning')) {
615 require_once($fullmod.'/db/install.php');
616 $recover_install_function = 'xmldb_'.$module->name.'_install_recovery';
617 if (function_exists($recover_install_function)) {
618 $startcallback($component, true, $verbose);
619 $recover_install_function();
620 unset_config('installrunning', $module->name);
621 // Install various components too
622 update_capabilities($component);
623 log_update_descriptions($component);
624 external_update_descriptions($component);
625 events_update_definition($component);
626 message_update_providers($component);
627 upgrade_plugin_mnet_functions($component);
628 $endcallback($component, true, $verbose);
633 if (empty($installedversion)) {
634 $startcallback($component, true, $verbose);
636 /// Execute install.xml (XMLDB) - must be present in all modules
637 $DB->get_manager()->install_from_xmldb_file($fullmod.'/db/install.xml');
639 /// Add record into modules table - may be needed in install.php already
640 $module->id = $DB->insert_record('modules', $module);
641 upgrade_mod_savepoint(true, $plugin->version, $module->name, false);
643 /// Post installation hook - optional
644 if (file_exists("$fullmod/db/install.php")) {
645 require_once("$fullmod/db/install.php");
646 // Set installation running flag, we need to recover after exception or error
647 set_config('installrunning', 1, $module->name);
648 $post_install_function = 'xmldb_'.$module->name.'_install';
649 $post_install_function();
650 unset_config('installrunning', $module->name);
653 /// Install various components
654 update_capabilities($component);
655 log_update_descriptions($component);
656 external_update_descriptions($component);
657 events_update_definition($component);
658 message_update_providers($component);
659 upgrade_plugin_mnet_functions($component);
661 $endcallback($component, true, $verbose);
663 } else if ($installedversion < $plugin->version) {
664 /// If versions say that we need to upgrade but no upgrade files are available, notify and continue
665 $startcallback($component, false, $verbose);
667 if (is_readable($fullmod.'/db/upgrade.php')) {
668 require_once($fullmod.'/db/upgrade.php'); // defines new upgrading function
669 $newupgrade_function = 'xmldb_'.$module->name.'_upgrade';
670 $result = $newupgrade_function($installedversion, $module);
671 } else {
672 $result = true;
675 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
676 $currmodule = $DB->get_record('modules', array('name'=>$module->name));
677 if ($installedversion < $plugin->version) {
678 // store version if not already there
679 upgrade_mod_savepoint($result, $plugin->version, $mod, false);
682 // update cron flag if needed
683 if ($currmodule->cron != $module->cron) {
684 $DB->set_field('modules', 'cron', $module->cron, array('name' => $module->name));
687 // Upgrade various components
688 update_capabilities($component);
689 log_update_descriptions($component);
690 external_update_descriptions($component);
691 events_update_definition($component);
692 message_update_providers($component);
693 upgrade_plugin_mnet_functions($component);
695 $endcallback($component, false, $verbose);
697 } else if ($installedversion > $plugin->version) {
698 throw new downgrade_exception($component, $installedversion, $plugin->version);
705 * This function finds all available blocks and install them
706 * into blocks table or do all the upgrade process if newer.
708 * @global object
709 * @global object
711 function upgrade_plugins_blocks($startcallback, $endcallback, $verbose) {
712 global $CFG, $DB;
714 require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
716 $blocktitles = array(); // we do not want duplicate titles
718 //Is this a first install
719 $first_install = null;
721 $blocks = core_component::get_plugin_list('block');
723 foreach ($blocks as $blockname=>$fullblock) {
725 if (is_null($first_install)) {
726 $first_install = ($DB->count_records('block_instances') == 0);
729 if ($blockname === 'NEWBLOCK') { // Someone has unzipped the template, ignore it
730 continue;
733 $component = clean_param('block_'.$blockname, PARAM_COMPONENT);
735 // check block dir is valid name
736 if (empty($component)) {
737 throw new plugin_defective_exception('block_'.$blockname, 'Invalid plugin directory name.');
740 if (!is_readable($fullblock.'/version.php')) {
741 throw new plugin_defective_exception('block/'.$blockname, 'Missing version.php file.');
743 $plugin = new stdClass();
744 $plugin->version = null;
745 $plugin->cron = 0;
746 $module = $plugin; // Prevent some notices when module placed in wrong directory.
747 include($fullblock.'/version.php');
748 unset($module);
749 $block = clone($plugin);
750 unset($block->version);
751 unset($block->component);
752 unset($block->dependencies);
753 unset($block->release);
755 // if plugin tells us it's full name we may check the location
756 if (isset($plugin->component)) {
757 if ($plugin->component !== $component) {
758 $current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullblock);
759 $expected = str_replace($CFG->dirroot, '$CFG->dirroot', core_component::get_component_directory($plugin->component));
760 throw new plugin_misplaced_exception($component, $expected, $current);
764 if (empty($plugin->version)) {
765 throw new plugin_defective_exception($component, 'Missing block version.');
768 if (!empty($plugin->requires)) {
769 if ($plugin->requires > $CFG->version) {
770 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
771 } else if ($plugin->requires < 2010000000) {
772 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
776 if (!is_readable($fullblock.'/block_'.$blockname.'.php')) {
777 throw new plugin_defective_exception('block/'.$blockname, 'Missing main block class file.');
779 include_once($fullblock.'/block_'.$blockname.'.php');
781 $classname = 'block_'.$blockname;
783 if (!class_exists($classname)) {
784 throw new plugin_defective_exception($component, 'Can not load main class.');
787 $blockobj = new $classname; // This is what we'll be testing
788 $blocktitle = $blockobj->get_title();
790 // OK, it's as we all hoped. For further tests, the object will do them itself.
791 if (!$blockobj->_self_test()) {
792 throw new plugin_defective_exception($component, 'Self test failed.');
795 $block->name = $blockname; // The name MUST match the directory
797 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
799 if (file_exists($fullblock.'/db/install.php')) {
800 if (get_config('block_'.$blockname, 'installrunning')) {
801 require_once($fullblock.'/db/install.php');
802 $recover_install_function = 'xmldb_block_'.$blockname.'_install_recovery';
803 if (function_exists($recover_install_function)) {
804 $startcallback($component, true, $verbose);
805 $recover_install_function();
806 unset_config('installrunning', 'block_'.$blockname);
807 // Install various components
808 update_capabilities($component);
809 log_update_descriptions($component);
810 external_update_descriptions($component);
811 events_update_definition($component);
812 message_update_providers($component);
813 upgrade_plugin_mnet_functions($component);
814 $endcallback($component, true, $verbose);
819 if (empty($installedversion)) { // block not installed yet, so install it
820 $conflictblock = array_search($blocktitle, $blocktitles);
821 if ($conflictblock !== false) {
822 // Duplicate block titles are not allowed, they confuse people
823 // AND PHP's associative arrays ;)
824 throw new plugin_defective_exception($component, get_string('blocknameconflict', 'error', (object)array('name'=>$block->name, 'conflict'=>$conflictblock)));
826 $startcallback($component, true, $verbose);
828 if (file_exists($fullblock.'/db/install.xml')) {
829 $DB->get_manager()->install_from_xmldb_file($fullblock.'/db/install.xml');
831 $block->id = $DB->insert_record('block', $block);
832 upgrade_block_savepoint(true, $plugin->version, $block->name, false);
834 if (file_exists($fullblock.'/db/install.php')) {
835 require_once($fullblock.'/db/install.php');
836 // Set installation running flag, we need to recover after exception or error
837 set_config('installrunning', 1, 'block_'.$blockname);
838 $post_install_function = 'xmldb_block_'.$blockname.'_install';
839 $post_install_function();
840 unset_config('installrunning', 'block_'.$blockname);
843 $blocktitles[$block->name] = $blocktitle;
845 // Install various components
846 update_capabilities($component);
847 log_update_descriptions($component);
848 external_update_descriptions($component);
849 events_update_definition($component);
850 message_update_providers($component);
851 upgrade_plugin_mnet_functions($component);
853 $endcallback($component, true, $verbose);
855 } else if ($installedversion < $plugin->version) {
856 $startcallback($component, false, $verbose);
858 if (is_readable($fullblock.'/db/upgrade.php')) {
859 require_once($fullblock.'/db/upgrade.php'); // defines new upgrading function
860 $newupgrade_function = 'xmldb_block_'.$blockname.'_upgrade';
861 $result = $newupgrade_function($installedversion, $block);
862 } else {
863 $result = true;
866 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
867 $currblock = $DB->get_record('block', array('name'=>$block->name));
868 if ($installedversion < $plugin->version) {
869 // store version if not already there
870 upgrade_block_savepoint($result, $plugin->version, $block->name, false);
873 if ($currblock->cron != $block->cron) {
874 // update cron flag if needed
875 $DB->set_field('block', 'cron', $block->cron, array('id' => $currblock->id));
878 // Upgrade various components
879 update_capabilities($component);
880 log_update_descriptions($component);
881 external_update_descriptions($component);
882 events_update_definition($component);
883 message_update_providers($component);
884 upgrade_plugin_mnet_functions($component);
886 $endcallback($component, false, $verbose);
888 } else if ($installedversion > $plugin->version) {
889 throw new downgrade_exception($component, $installedversion, $plugin->version);
894 // Finally, if we are in the first_install of BLOCKS setup frontpage and admin page blocks
895 if ($first_install) {
896 //Iterate over each course - there should be only site course here now
897 if ($courses = $DB->get_records('course')) {
898 foreach ($courses as $course) {
899 blocks_add_default_course_blocks($course);
903 blocks_add_default_system_blocks();
909 * Log_display description function used during install and upgrade.
911 * @param string $component name of component (moodle, mod_assignment, etc.)
912 * @return void
914 function log_update_descriptions($component) {
915 global $DB;
917 $defpath = core_component::get_component_directory($component).'/db/log.php';
919 if (!file_exists($defpath)) {
920 $DB->delete_records('log_display', array('component'=>$component));
921 return;
924 // load new info
925 $logs = array();
926 include($defpath);
927 $newlogs = array();
928 foreach ($logs as $log) {
929 $newlogs[$log['module'].'-'.$log['action']] = $log; // kind of unique name
931 unset($logs);
932 $logs = $newlogs;
934 $fields = array('module', 'action', 'mtable', 'field');
935 // update all log fist
936 $dblogs = $DB->get_records('log_display', array('component'=>$component));
937 foreach ($dblogs as $dblog) {
938 $name = $dblog->module.'-'.$dblog->action;
940 if (empty($logs[$name])) {
941 $DB->delete_records('log_display', array('id'=>$dblog->id));
942 continue;
945 $log = $logs[$name];
946 unset($logs[$name]);
948 $update = false;
949 foreach ($fields as $field) {
950 if ($dblog->$field != $log[$field]) {
951 $dblog->$field = $log[$field];
952 $update = true;
955 if ($update) {
956 $DB->update_record('log_display', $dblog);
959 foreach ($logs as $log) {
960 $dblog = (object)$log;
961 $dblog->component = $component;
962 $DB->insert_record('log_display', $dblog);
967 * Web service discovery function used during install and upgrade.
968 * @param string $component name of component (moodle, mod_assignment, etc.)
969 * @return void
971 function external_update_descriptions($component) {
972 global $DB, $CFG;
974 $defpath = core_component::get_component_directory($component).'/db/services.php';
976 if (!file_exists($defpath)) {
977 require_once($CFG->dirroot.'/lib/externallib.php');
978 external_delete_descriptions($component);
979 return;
982 // load new info
983 $functions = array();
984 $services = array();
985 include($defpath);
987 // update all function fist
988 $dbfunctions = $DB->get_records('external_functions', array('component'=>$component));
989 foreach ($dbfunctions as $dbfunction) {
990 if (empty($functions[$dbfunction->name])) {
991 $DB->delete_records('external_functions', array('id'=>$dbfunction->id));
992 // do not delete functions from external_services_functions, beacuse
993 // we want to notify admins when functions used in custom services disappear
995 //TODO: this looks wrong, we have to delete it eventually (skodak)
996 continue;
999 $function = $functions[$dbfunction->name];
1000 unset($functions[$dbfunction->name]);
1001 $function['classpath'] = empty($function['classpath']) ? null : $function['classpath'];
1003 $update = false;
1004 if ($dbfunction->classname != $function['classname']) {
1005 $dbfunction->classname = $function['classname'];
1006 $update = true;
1008 if ($dbfunction->methodname != $function['methodname']) {
1009 $dbfunction->methodname = $function['methodname'];
1010 $update = true;
1012 if ($dbfunction->classpath != $function['classpath']) {
1013 $dbfunction->classpath = $function['classpath'];
1014 $update = true;
1016 $functioncapabilities = array_key_exists('capabilities', $function)?$function['capabilities']:'';
1017 if ($dbfunction->capabilities != $functioncapabilities) {
1018 $dbfunction->capabilities = $functioncapabilities;
1019 $update = true;
1021 if ($update) {
1022 $DB->update_record('external_functions', $dbfunction);
1025 foreach ($functions as $fname => $function) {
1026 $dbfunction = new stdClass();
1027 $dbfunction->name = $fname;
1028 $dbfunction->classname = $function['classname'];
1029 $dbfunction->methodname = $function['methodname'];
1030 $dbfunction->classpath = empty($function['classpath']) ? null : $function['classpath'];
1031 $dbfunction->component = $component;
1032 $dbfunction->capabilities = array_key_exists('capabilities', $function)?$function['capabilities']:'';
1033 $dbfunction->id = $DB->insert_record('external_functions', $dbfunction);
1035 unset($functions);
1037 // now deal with services
1038 $dbservices = $DB->get_records('external_services', array('component'=>$component));
1039 foreach ($dbservices as $dbservice) {
1040 if (empty($services[$dbservice->name])) {
1041 $DB->delete_records('external_tokens', array('externalserviceid'=>$dbservice->id));
1042 $DB->delete_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
1043 $DB->delete_records('external_services_users', array('externalserviceid'=>$dbservice->id));
1044 $DB->delete_records('external_services', array('id'=>$dbservice->id));
1045 continue;
1047 $service = $services[$dbservice->name];
1048 unset($services[$dbservice->name]);
1049 $service['enabled'] = empty($service['enabled']) ? 0 : $service['enabled'];
1050 $service['requiredcapability'] = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1051 $service['restrictedusers'] = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
1052 $service['downloadfiles'] = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
1053 $service['uploadfiles'] = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles'];
1054 $service['shortname'] = !isset($service['shortname']) ? null : $service['shortname'];
1056 $update = false;
1057 if ($dbservice->requiredcapability != $service['requiredcapability']) {
1058 $dbservice->requiredcapability = $service['requiredcapability'];
1059 $update = true;
1061 if ($dbservice->restrictedusers != $service['restrictedusers']) {
1062 $dbservice->restrictedusers = $service['restrictedusers'];
1063 $update = true;
1065 if ($dbservice->downloadfiles != $service['downloadfiles']) {
1066 $dbservice->downloadfiles = $service['downloadfiles'];
1067 $update = true;
1069 if ($dbservice->uploadfiles != $service['uploadfiles']) {
1070 $dbservice->uploadfiles = $service['uploadfiles'];
1071 $update = true;
1073 //if shortname is not a PARAM_ALPHANUMEXT, fail (tested here for service update and creation)
1074 if (isset($service['shortname']) and
1075 (clean_param($service['shortname'], PARAM_ALPHANUMEXT) != $service['shortname'])) {
1076 throw new moodle_exception('installserviceshortnameerror', 'webservice', '', $service['shortname']);
1078 if ($dbservice->shortname != $service['shortname']) {
1079 //check that shortname is unique
1080 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
1081 $existingservice = $DB->get_record('external_services',
1082 array('shortname' => $service['shortname']));
1083 if (!empty($existingservice)) {
1084 throw new moodle_exception('installexistingserviceshortnameerror', 'webservice', '', $service['shortname']);
1087 $dbservice->shortname = $service['shortname'];
1088 $update = true;
1090 if ($update) {
1091 $DB->update_record('external_services', $dbservice);
1094 $functions = $DB->get_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
1095 foreach ($functions as $function) {
1096 $key = array_search($function->functionname, $service['functions']);
1097 if ($key === false) {
1098 $DB->delete_records('external_services_functions', array('id'=>$function->id));
1099 } else {
1100 unset($service['functions'][$key]);
1103 foreach ($service['functions'] as $fname) {
1104 $newf = new stdClass();
1105 $newf->externalserviceid = $dbservice->id;
1106 $newf->functionname = $fname;
1107 $DB->insert_record('external_services_functions', $newf);
1109 unset($functions);
1111 foreach ($services as $name => $service) {
1112 //check that shortname is unique
1113 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
1114 $existingservice = $DB->get_record('external_services',
1115 array('shortname' => $service['shortname']));
1116 if (!empty($existingservice)) {
1117 throw new moodle_exception('installserviceshortnameerror', 'webservice');
1121 $dbservice = new stdClass();
1122 $dbservice->name = $name;
1123 $dbservice->enabled = empty($service['enabled']) ? 0 : $service['enabled'];
1124 $dbservice->requiredcapability = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1125 $dbservice->restrictedusers = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
1126 $dbservice->downloadfiles = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
1127 $dbservice->uploadfiles = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles'];
1128 $dbservice->shortname = !isset($service['shortname']) ? null : $service['shortname'];
1129 $dbservice->component = $component;
1130 $dbservice->timecreated = time();
1131 $dbservice->id = $DB->insert_record('external_services', $dbservice);
1132 foreach ($service['functions'] as $fname) {
1133 $newf = new stdClass();
1134 $newf->externalserviceid = $dbservice->id;
1135 $newf->functionname = $fname;
1136 $DB->insert_record('external_services_functions', $newf);
1142 * upgrade logging functions
1144 function upgrade_handle_exception($ex, $plugin = null) {
1145 global $CFG;
1147 // rollback everything, we need to log all upgrade problems
1148 abort_all_db_transactions();
1150 $info = get_exception_info($ex);
1152 // First log upgrade error
1153 upgrade_log(UPGRADE_LOG_ERROR, $plugin, 'Exception: ' . get_class($ex), $info->message, $info->backtrace);
1155 // Always turn on debugging - admins need to know what is going on
1156 set_debugging(DEBUG_DEVELOPER, true);
1158 default_exception_handler($ex, true, $plugin);
1162 * Adds log entry into upgrade_log table
1164 * @param int $type UPGRADE_LOG_NORMAL, UPGRADE_LOG_NOTICE or UPGRADE_LOG_ERROR
1165 * @param string $plugin frankenstyle component name
1166 * @param string $info short description text of log entry
1167 * @param string $details long problem description
1168 * @param string $backtrace string used for errors only
1169 * @return void
1171 function upgrade_log($type, $plugin, $info, $details=null, $backtrace=null) {
1172 global $DB, $USER, $CFG;
1174 if (empty($plugin)) {
1175 $plugin = 'core';
1178 list($plugintype, $pluginname) = core_component::normalize_component($plugin);
1179 $component = is_null($pluginname) ? $plugintype : $plugintype . '_' . $pluginname;
1181 $backtrace = format_backtrace($backtrace, true);
1183 $currentversion = null;
1184 $targetversion = null;
1186 //first try to find out current version number
1187 if ($plugintype === 'core') {
1188 //main
1189 $currentversion = $CFG->version;
1191 $version = null;
1192 include("$CFG->dirroot/version.php");
1193 $targetversion = $version;
1195 } else {
1196 $pluginversion = get_config($component, 'version');
1197 if (!empty($pluginversion)) {
1198 $currentversion = $pluginversion;
1200 $cd = core_component::get_component_directory($component);
1201 if (file_exists("$cd/version.php")) {
1202 $plugin = new stdClass();
1203 $plugin->version = null;
1204 $module = $plugin;
1205 include("$cd/version.php");
1206 $targetversion = $plugin->version;
1210 $log = new stdClass();
1211 $log->type = $type;
1212 $log->plugin = $component;
1213 $log->version = $currentversion;
1214 $log->targetversion = $targetversion;
1215 $log->info = $info;
1216 $log->details = $details;
1217 $log->backtrace = $backtrace;
1218 $log->userid = $USER->id;
1219 $log->timemodified = time();
1220 try {
1221 $DB->insert_record('upgrade_log', $log);
1222 } catch (Exception $ignored) {
1223 // possible during install or 2.0 upgrade
1228 * Marks start of upgrade, blocks any other access to site.
1229 * The upgrade is finished at the end of script or after timeout.
1231 * @global object
1232 * @global object
1233 * @global object
1235 function upgrade_started($preinstall=false) {
1236 global $CFG, $DB, $PAGE, $OUTPUT;
1238 static $started = false;
1240 if ($preinstall) {
1241 ignore_user_abort(true);
1242 upgrade_setup_debug(true);
1244 } else if ($started) {
1245 upgrade_set_timeout(120);
1247 } else {
1248 if (!CLI_SCRIPT and !$PAGE->headerprinted) {
1249 $strupgrade = get_string('upgradingversion', 'admin');
1250 $PAGE->set_pagelayout('maintenance');
1251 upgrade_init_javascript();
1252 $PAGE->set_title($strupgrade.' - Moodle '.$CFG->target_release);
1253 $PAGE->set_heading($strupgrade);
1254 $PAGE->navbar->add($strupgrade);
1255 $PAGE->set_cacheable(false);
1256 echo $OUTPUT->header();
1259 ignore_user_abort(true);
1260 core_shutdown_manager::register_function('upgrade_finished_handler');
1261 upgrade_setup_debug(true);
1262 set_config('upgraderunning', time()+300);
1263 $started = true;
1268 * Internal function - executed if upgrade interrupted.
1270 function upgrade_finished_handler() {
1271 upgrade_finished();
1275 * Indicates upgrade is finished.
1277 * This function may be called repeatedly.
1279 * @global object
1280 * @global object
1282 function upgrade_finished($continueurl=null) {
1283 global $CFG, $DB, $OUTPUT;
1285 if (!empty($CFG->upgraderunning)) {
1286 unset_config('upgraderunning');
1287 // We have to forcefully purge the caches using the writer here.
1288 // This has to be done after we unset the config var. If someone hits the site while this is set they will
1289 // cause the config values to propogate to the caches.
1290 // Caches are purged after the last step in an upgrade but there is several code routines that exceute between
1291 // then and now that leaving a window for things to fall out of sync.
1292 cache_helper::purge_all(true);
1293 upgrade_setup_debug(false);
1294 ignore_user_abort(false);
1295 if ($continueurl) {
1296 echo $OUTPUT->continue_button($continueurl);
1297 echo $OUTPUT->footer();
1298 die;
1304 * @global object
1305 * @global object
1307 function upgrade_setup_debug($starting) {
1308 global $CFG, $DB;
1310 static $originaldebug = null;
1312 if ($starting) {
1313 if ($originaldebug === null) {
1314 $originaldebug = $DB->get_debug();
1316 if (!empty($CFG->upgradeshowsql)) {
1317 $DB->set_debug(true);
1319 } else {
1320 $DB->set_debug($originaldebug);
1324 function print_upgrade_separator() {
1325 if (!CLI_SCRIPT) {
1326 echo '<hr />';
1331 * Default start upgrade callback
1332 * @param string $plugin
1333 * @param bool $installation true if installation, false means upgrade
1335 function print_upgrade_part_start($plugin, $installation, $verbose) {
1336 global $OUTPUT;
1337 if (empty($plugin) or $plugin == 'moodle') {
1338 upgrade_started($installation); // does not store upgrade running flag yet
1339 if ($verbose) {
1340 echo $OUTPUT->heading(get_string('coresystem'));
1342 } else {
1343 upgrade_started();
1344 if ($verbose) {
1345 echo $OUTPUT->heading($plugin);
1348 if ($installation) {
1349 if (empty($plugin) or $plugin == 'moodle') {
1350 // no need to log - log table not yet there ;-)
1351 } else {
1352 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin installation');
1354 } else {
1355 if (empty($plugin) or $plugin == 'moodle') {
1356 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting core upgrade');
1357 } else {
1358 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin upgrade');
1364 * Default end upgrade callback
1365 * @param string $plugin
1366 * @param bool $installation true if installation, false means upgrade
1368 function print_upgrade_part_end($plugin, $installation, $verbose) {
1369 global $OUTPUT;
1370 upgrade_started();
1371 if ($installation) {
1372 if (empty($plugin) or $plugin == 'moodle') {
1373 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core installed');
1374 } else {
1375 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin installed');
1377 } else {
1378 if (empty($plugin) or $plugin == 'moodle') {
1379 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core upgraded');
1380 } else {
1381 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin upgraded');
1384 if ($verbose) {
1385 echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
1386 print_upgrade_separator();
1391 * Sets up JS code required for all upgrade scripts.
1392 * @global object
1394 function upgrade_init_javascript() {
1395 global $PAGE;
1396 // scroll to the end of each upgrade page so that ppl see either error or continue button,
1397 // no need to scroll continuously any more, it is enough to jump to end once the footer is printed ;-)
1398 $js = "window.scrollTo(0, 5000000);";
1399 $PAGE->requires->js_init_code($js);
1403 * Try to upgrade the given language pack (or current language)
1405 * @param string $lang the code of the language to update, defaults to the current language
1407 function upgrade_language_pack($lang = null) {
1408 global $CFG;
1410 if (!empty($CFG->skiplangupgrade)) {
1411 return;
1414 if (!file_exists("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php")) {
1415 // weird, somebody uninstalled the import utility
1416 return;
1419 if (!$lang) {
1420 $lang = current_language();
1423 if (!get_string_manager()->translation_exists($lang)) {
1424 return;
1427 get_string_manager()->reset_caches();
1429 if ($lang === 'en') {
1430 return; // Nothing to do
1433 upgrade_started(false);
1435 require_once("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php");
1436 tool_langimport_preupgrade_update($lang);
1438 get_string_manager()->reset_caches();
1440 print_upgrade_separator();
1444 * Install core moodle tables and initialize
1445 * @param float $version target version
1446 * @param bool $verbose
1447 * @return void, may throw exception
1449 function install_core($version, $verbose) {
1450 global $CFG, $DB;
1452 // We can not call purge_all_caches() yet, make sure the temp and cache dirs exist and are empty.
1453 remove_dir($CFG->cachedir.'', true);
1454 make_cache_directory('', true);
1456 remove_dir($CFG->localcachedir.'', true);
1457 make_localcache_directory('', true);
1459 remove_dir($CFG->tempdir.'', true);
1460 make_temp_directory('', true);
1462 remove_dir($CFG->dataroot.'/muc', true);
1463 make_writable_directory($CFG->dataroot.'/muc', true);
1465 try {
1466 set_time_limit(600);
1467 print_upgrade_part_start('moodle', true, $verbose); // does not store upgrade running flag
1469 $DB->get_manager()->install_from_xmldb_file("$CFG->libdir/db/install.xml");
1470 upgrade_started(); // we want the flag to be stored in config table ;-)
1472 // set all core default records and default settings
1473 require_once("$CFG->libdir/db/install.php");
1474 xmldb_main_install(); // installs the capabilities too
1476 // store version
1477 upgrade_main_savepoint(true, $version, false);
1479 // Continue with the installation
1480 log_update_descriptions('moodle');
1481 external_update_descriptions('moodle');
1482 events_update_definition('moodle');
1483 message_update_providers('moodle');
1485 // Write default settings unconditionally
1486 admin_apply_default_settings(NULL, true);
1488 print_upgrade_part_end(null, true, $verbose);
1490 // Purge all caches. They're disabled but this ensures that we don't have any persistent data just in case something
1491 // during installation didn't use APIs.
1492 cache_helper::purge_all();
1493 } catch (exception $ex) {
1494 upgrade_handle_exception($ex);
1499 * Upgrade moodle core
1500 * @param float $version target version
1501 * @param bool $verbose
1502 * @return void, may throw exception
1504 function upgrade_core($version, $verbose) {
1505 global $CFG;
1507 raise_memory_limit(MEMORY_EXTRA);
1509 require_once($CFG->libdir.'/db/upgrade.php'); // Defines upgrades
1511 try {
1512 // Reset caches before any output.
1513 cache_helper::purge_all(true);
1514 purge_all_caches();
1516 // Upgrade current language pack if we can
1517 upgrade_language_pack();
1519 print_upgrade_part_start('moodle', false, $verbose);
1521 // Pre-upgrade scripts for local hack workarounds.
1522 $preupgradefile = "$CFG->dirroot/local/preupgrade.php";
1523 if (file_exists($preupgradefile)) {
1524 set_time_limit(0);
1525 require($preupgradefile);
1526 // Reset upgrade timeout to default.
1527 upgrade_set_timeout();
1530 $result = xmldb_main_upgrade($CFG->version);
1531 if ($version > $CFG->version) {
1532 // store version if not already there
1533 upgrade_main_savepoint($result, $version, false);
1536 // perform all other component upgrade routines
1537 update_capabilities('moodle');
1538 log_update_descriptions('moodle');
1539 external_update_descriptions('moodle');
1540 events_update_definition('moodle');
1541 message_update_providers('moodle');
1542 // Update core definitions.
1543 cache_helper::update_definitions(true);
1545 // Purge caches again, just to be sure we arn't holding onto old stuff now.
1546 cache_helper::purge_all(true);
1547 purge_all_caches();
1549 // Clean up contexts - more and more stuff depends on existence of paths and contexts
1550 context_helper::cleanup_instances();
1551 context_helper::create_instances(null, false);
1552 context_helper::build_all_paths(false);
1553 $syscontext = context_system::instance();
1554 $syscontext->mark_dirty();
1556 print_upgrade_part_end('moodle', false, $verbose);
1557 } catch (Exception $ex) {
1558 upgrade_handle_exception($ex);
1563 * Upgrade/install other parts of moodle
1564 * @param bool $verbose
1565 * @return void, may throw exception
1567 function upgrade_noncore($verbose) {
1568 global $CFG;
1570 raise_memory_limit(MEMORY_EXTRA);
1572 // upgrade all plugins types
1573 try {
1574 // Reset caches before any output.
1575 cache_helper::purge_all(true);
1576 purge_all_caches();
1578 $plugintypes = core_component::get_plugin_types();
1579 foreach ($plugintypes as $type=>$location) {
1580 upgrade_plugins($type, 'print_upgrade_part_start', 'print_upgrade_part_end', $verbose);
1582 // Update cache definitions. Involves scanning each plugin for any changes.
1583 cache_helper::update_definitions();
1584 // Mark the site as upgraded.
1585 set_config('allversionshash', core_component::get_all_versions_hash());
1587 // Purge caches again, just to be sure we arn't holding onto old stuff now.
1588 cache_helper::purge_all(true);
1589 purge_all_caches();
1591 } catch (Exception $ex) {
1592 upgrade_handle_exception($ex);
1597 * Checks if the main tables have been installed yet or not.
1599 * Note: we can not use caches here because they might be stale,
1600 * use with care!
1602 * @return bool
1604 function core_tables_exist() {
1605 global $DB;
1607 if (!$tables = $DB->get_tables(false) ) { // No tables yet at all.
1608 return false;
1610 } else { // Check for missing main tables
1611 $mtables = array('config', 'course', 'groupings'); // some tables used in 1.9 and 2.0, preferable something from the start and end of install.xml
1612 foreach ($mtables as $mtable) {
1613 if (!in_array($mtable, $tables)) {
1614 return false;
1617 return true;
1622 * upgrades the mnet rpc definitions for the given component.
1623 * this method doesn't return status, an exception will be thrown in the case of an error
1625 * @param string $component the plugin to upgrade, eg auth_mnet
1627 function upgrade_plugin_mnet_functions($component) {
1628 global $DB, $CFG;
1630 list($type, $plugin) = explode('_', $component);
1631 $path = core_component::get_plugin_directory($type, $plugin);
1633 $publishes = array();
1634 $subscribes = array();
1635 if (file_exists($path . '/db/mnet.php')) {
1636 require_once($path . '/db/mnet.php'); // $publishes comes from this file
1638 if (empty($publishes)) {
1639 $publishes = array(); // still need this to be able to disable stuff later
1641 if (empty($subscribes)) {
1642 $subscribes = array(); // still need this to be able to disable stuff later
1645 static $servicecache = array();
1647 // rekey an array based on the rpc method for easy lookups later
1648 $publishmethodservices = array();
1649 $subscribemethodservices = array();
1650 foreach($publishes as $servicename => $service) {
1651 if (is_array($service['methods'])) {
1652 foreach($service['methods'] as $methodname) {
1653 $service['servicename'] = $servicename;
1654 $publishmethodservices[$methodname][] = $service;
1659 // Disable functions that don't exist (any more) in the source
1660 // Should these be deleted? What about their permissions records?
1661 foreach ($DB->get_records('mnet_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
1662 if (!array_key_exists($rpc->functionname, $publishmethodservices) && $rpc->enabled) {
1663 $DB->set_field('mnet_rpc', 'enabled', 0, array('id' => $rpc->id));
1664 } else if (array_key_exists($rpc->functionname, $publishmethodservices) && !$rpc->enabled) {
1665 $DB->set_field('mnet_rpc', 'enabled', 1, array('id' => $rpc->id));
1669 // reflect all the services we're publishing and save them
1670 require_once($CFG->dirroot . '/lib/zend/Zend/Server/Reflection.php');
1671 static $cachedclasses = array(); // to store reflection information in
1672 foreach ($publishes as $service => $data) {
1673 $f = $data['filename'];
1674 $c = $data['classname'];
1675 foreach ($data['methods'] as $method) {
1676 $dataobject = new stdClass();
1677 $dataobject->plugintype = $type;
1678 $dataobject->pluginname = $plugin;
1679 $dataobject->enabled = 1;
1680 $dataobject->classname = $c;
1681 $dataobject->filename = $f;
1683 if (is_string($method)) {
1684 $dataobject->functionname = $method;
1686 } else if (is_array($method)) { // wants to override file or class
1687 $dataobject->functionname = $method['method'];
1688 $dataobject->classname = $method['classname'];
1689 $dataobject->filename = $method['filename'];
1691 $dataobject->xmlrpcpath = $type.'/'.$plugin.'/'.$dataobject->filename.'/'.$method;
1692 $dataobject->static = false;
1694 require_once($path . '/' . $dataobject->filename);
1695 $functionreflect = null; // slightly different ways to get this depending on whether it's a class method or a function
1696 if (!empty($dataobject->classname)) {
1697 if (!class_exists($dataobject->classname)) {
1698 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
1700 $key = $dataobject->filename . '|' . $dataobject->classname;
1701 if (!array_key_exists($key, $cachedclasses)) { // look to see if we've already got a reflection object
1702 try {
1703 $cachedclasses[$key] = Zend_Server_Reflection::reflectClass($dataobject->classname);
1704 } catch (Zend_Server_Reflection_Exception $e) { // catch these and rethrow them to something more helpful
1705 throw new moodle_exception('installreflectionclasserror', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname, 'error' => $e->getMessage()));
1708 $r =& $cachedclasses[$key];
1709 if (!$r->hasMethod($dataobject->functionname)) {
1710 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
1712 // stupid workaround for zend not having a getMethod($name) function
1713 $ms = $r->getMethods();
1714 foreach ($ms as $m) {
1715 if ($m->getName() == $dataobject->functionname) {
1716 $functionreflect = $m;
1717 break;
1720 $dataobject->static = (int)$functionreflect->isStatic();
1721 } else {
1722 if (!function_exists($dataobject->functionname)) {
1723 throw new moodle_exception('installnosuchfunction', 'mnet', '', (object)array('method' => $dataobject->functionname, 'file' => $dataobject->filename));
1725 try {
1726 $functionreflect = Zend_Server_Reflection::reflectFunction($dataobject->functionname);
1727 } catch (Zend_Server_Reflection_Exception $e) { // catch these and rethrow them to something more helpful
1728 throw new moodle_exception('installreflectionfunctionerror', 'mnet', '', (object)array('method' => $dataobject->functionname, '' => $dataobject->filename, 'error' => $e->getMessage()));
1731 $dataobject->profile = serialize(admin_mnet_method_profile($functionreflect));
1732 $dataobject->help = $functionreflect->getDescription();
1734 if ($record_exists = $DB->get_record('mnet_rpc', array('xmlrpcpath'=>$dataobject->xmlrpcpath))) {
1735 $dataobject->id = $record_exists->id;
1736 $dataobject->enabled = $record_exists->enabled;
1737 $DB->update_record('mnet_rpc', $dataobject);
1738 } else {
1739 $dataobject->id = $DB->insert_record('mnet_rpc', $dataobject, true);
1742 // TODO this API versioning must be reworked, here the recently processed method
1743 // sets the service API which may not be correct
1744 foreach ($publishmethodservices[$dataobject->functionname] as $service) {
1745 if ($serviceobj = $DB->get_record('mnet_service', array('name'=>$service['servicename']))) {
1746 $serviceobj->apiversion = $service['apiversion'];
1747 $DB->update_record('mnet_service', $serviceobj);
1748 } else {
1749 $serviceobj = new stdClass();
1750 $serviceobj->name = $service['servicename'];
1751 $serviceobj->description = empty($service['description']) ? '' : $service['description'];
1752 $serviceobj->apiversion = $service['apiversion'];
1753 $serviceobj->offer = 1;
1754 $serviceobj->id = $DB->insert_record('mnet_service', $serviceobj);
1756 $servicecache[$service['servicename']] = $serviceobj;
1757 if (!$DB->record_exists('mnet_service2rpc', array('rpcid'=>$dataobject->id, 'serviceid'=>$serviceobj->id))) {
1758 $obj = new stdClass();
1759 $obj->rpcid = $dataobject->id;
1760 $obj->serviceid = $serviceobj->id;
1761 $DB->insert_record('mnet_service2rpc', $obj, true);
1766 // finished with methods we publish, now do subscribable methods
1767 foreach($subscribes as $service => $methods) {
1768 if (!array_key_exists($service, $servicecache)) {
1769 if (!$serviceobj = $DB->get_record('mnet_service', array('name' => $service))) {
1770 debugging("TODO: skipping unknown service $service - somebody needs to fix MDL-21993");
1771 continue;
1773 $servicecache[$service] = $serviceobj;
1774 } else {
1775 $serviceobj = $servicecache[$service];
1777 foreach ($methods as $method => $xmlrpcpath) {
1778 if (!$rpcid = $DB->get_field('mnet_remote_rpc', 'id', array('xmlrpcpath'=>$xmlrpcpath))) {
1779 $remoterpc = (object)array(
1780 'functionname' => $method,
1781 'xmlrpcpath' => $xmlrpcpath,
1782 'plugintype' => $type,
1783 'pluginname' => $plugin,
1784 'enabled' => 1,
1786 $rpcid = $remoterpc->id = $DB->insert_record('mnet_remote_rpc', $remoterpc, true);
1788 if (!$DB->record_exists('mnet_remote_service2rpc', array('rpcid'=>$rpcid, 'serviceid'=>$serviceobj->id))) {
1789 $obj = new stdClass();
1790 $obj->rpcid = $rpcid;
1791 $obj->serviceid = $serviceobj->id;
1792 $DB->insert_record('mnet_remote_service2rpc', $obj, true);
1794 $subscribemethodservices[$method][] = $service;
1798 foreach ($DB->get_records('mnet_remote_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
1799 if (!array_key_exists($rpc->functionname, $subscribemethodservices) && $rpc->enabled) {
1800 $DB->set_field('mnet_remote_rpc', 'enabled', 0, array('id' => $rpc->id));
1801 } else if (array_key_exists($rpc->functionname, $subscribemethodservices) && !$rpc->enabled) {
1802 $DB->set_field('mnet_remote_rpc', 'enabled', 1, array('id' => $rpc->id));
1806 return true;
1810 * Given some sort of Zend Reflection function/method object, return a profile array, ready to be serialized and stored
1812 * @param Zend_Server_Reflection_Function_Abstract $function can be any subclass of this object type
1814 * @return array
1816 function admin_mnet_method_profile(Zend_Server_Reflection_Function_Abstract $function) {
1817 $protos = $function->getPrototypes();
1818 $proto = array_pop($protos);
1819 $ret = $proto->getReturnValue();
1820 $profile = array(
1821 'parameters' => array(),
1822 'return' => array(
1823 'type' => $ret->getType(),
1824 'description' => $ret->getDescription(),
1827 foreach ($proto->getParameters() as $p) {
1828 $profile['parameters'][] = array(
1829 'name' => $p->getName(),
1830 'type' => $p->getType(),
1831 'description' => $p->getDescription(),
1834 return $profile;
1839 * This function finds duplicate records (based on combinations of fields that should be unique)
1840 * and then progamatically generated a "most correct" version of the data, update and removing
1841 * records as appropriate
1843 * Thanks to Dan Marsden for help
1845 * @param string $table Table name
1846 * @param array $uniques Array of field names that should be unique
1847 * @param array $fieldstocheck Array of fields to generate "correct" data from (optional)
1848 * @return void
1850 function upgrade_course_completion_remove_duplicates($table, $uniques, $fieldstocheck = array()) {
1851 global $DB;
1853 // Find duplicates
1854 $sql_cols = implode(', ', $uniques);
1856 $sql = "SELECT {$sql_cols} FROM {{$table}} GROUP BY {$sql_cols} HAVING (count(id) > 1)";
1857 $duplicates = $DB->get_recordset_sql($sql, array());
1859 // Loop through duplicates
1860 foreach ($duplicates as $duplicate) {
1861 $pointer = 0;
1863 // Generate SQL for finding records with these duplicate uniques
1864 $sql_select = implode(' = ? AND ', $uniques).' = ?'; // builds "fieldname = ? AND fieldname = ?"
1865 $uniq_values = array();
1866 foreach ($uniques as $u) {
1867 $uniq_values[] = $duplicate->$u;
1870 $sql_order = implode(' DESC, ', $uniques).' DESC'; // builds "fieldname DESC, fieldname DESC"
1872 // Get records with these duplicate uniques
1873 $records = $DB->get_records_select(
1874 $table,
1875 $sql_select,
1876 $uniq_values,
1877 $sql_order
1880 // Loop through and build a "correct" record, deleting the others
1881 $needsupdate = false;
1882 $origrecord = null;
1883 foreach ($records as $record) {
1884 $pointer++;
1885 if ($pointer === 1) { // keep 1st record but delete all others.
1886 $origrecord = $record;
1887 } else {
1888 // If we have fields to check, update original record
1889 if ($fieldstocheck) {
1890 // we need to keep the "oldest" of all these fields as the valid completion record.
1891 // but we want to ignore null values
1892 foreach ($fieldstocheck as $f) {
1893 if ($record->$f && (($origrecord->$f > $record->$f) || !$origrecord->$f)) {
1894 $origrecord->$f = $record->$f;
1895 $needsupdate = true;
1899 $DB->delete_records($table, array('id' => $record->id));
1902 if ($needsupdate || isset($origrecord->reaggregate)) {
1903 // If this table has a reaggregate field, update to force recheck on next cron run
1904 if (isset($origrecord->reaggregate)) {
1905 $origrecord->reaggregate = time();
1907 $DB->update_record($table, $origrecord);
1913 * Find questions missing an existing category and associate them with
1914 * a category which purpose is to gather them.
1916 * @return void
1918 function upgrade_save_orphaned_questions() {
1919 global $DB;
1921 // Looking for orphaned questions
1922 $orphans = $DB->record_exists_select('question',
1923 'NOT EXISTS (SELECT 1 FROM {question_categories} WHERE {question_categories}.id = {question}.category)');
1924 if (!$orphans) {
1925 return;
1928 // Generate a unique stamp for the orphaned questions category, easier to identify it later on
1929 $uniquestamp = "unknownhost+120719170400+orphan";
1930 $systemcontext = context_system::instance();
1932 // Create the orphaned category at system level
1933 $cat = $DB->get_record('question_categories', array('stamp' => $uniquestamp,
1934 'contextid' => $systemcontext->id));
1935 if (!$cat) {
1936 $cat = new stdClass();
1937 $cat->parent = 0;
1938 $cat->contextid = $systemcontext->id;
1939 $cat->name = get_string('orphanedquestionscategory', 'question');
1940 $cat->info = get_string('orphanedquestionscategoryinfo', 'question');
1941 $cat->sortorder = 999;
1942 $cat->stamp = $uniquestamp;
1943 $cat->id = $DB->insert_record("question_categories", $cat);
1946 // Set a category to those orphans
1947 $params = array('catid' => $cat->id);
1948 $DB->execute('UPDATE {question} SET category = :catid WHERE NOT EXISTS
1949 (SELECT 1 FROM {question_categories} WHERE {question_categories}.id = {question}.category)', $params);
1953 * Rename old backup files to current backup files.
1955 * When added the setting 'backup_shortname' (MDL-28657) the backup file names did not contain the id of the course.
1956 * Further we fixed that behaviour by forcing the id to be always present in the file name (MDL-33812).
1957 * This function will explore the backup directory and attempt to rename the previously created files to include
1958 * the id in the name. Doing this will put them back in the process of deleting the excess backups for each course.
1960 * This function manually recreates the file name, instead of using
1961 * {@link backup_plan_dbops::get_default_backup_filename()}, use it carefully if you're using it outside of the
1962 * usual upgrade process.
1964 * @see backup_cron_automated_helper::remove_excess_backups()
1965 * @link http://tracker.moodle.org/browse/MDL-35116
1966 * @return void
1967 * @since 2.4
1969 function upgrade_rename_old_backup_files_using_shortname() {
1970 global $CFG;
1971 $dir = get_config('backup', 'backup_auto_destination');
1972 $useshortname = get_config('backup', 'backup_shortname');
1973 if (empty($dir) || !is_dir($dir) || !is_writable($dir)) {
1974 return;
1977 require_once($CFG->dirroot.'/backup/util/includes/backup_includes.php');
1978 $backupword = str_replace(' ', '_', core_text::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 = core_text::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 = core_text::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);