Automatically generated installer lang files
[moodle.git] / lib / upgradelib.php
blob6cb95b76e5cfb74bbe8123d9efa352d3f4e79557
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 * Misplaced plugin exception.
103 * Note: this should be used only from the upgrade/admin code.
105 * @package core
106 * @subpackage upgrade
107 * @copyright 2009 Petr Skoda {@link http://skodak.org}
108 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
110 class plugin_misplaced_exception extends moodle_exception {
112 * Constructor.
113 * @param string $component the component from version.php
114 * @param string $expected expected directory, null means calculate
115 * @param string $current plugin directory path
117 public function __construct($component, $expected, $current) {
118 global $CFG;
119 if (empty($expected)) {
120 list($type, $plugin) = core_component::normalize_component($component);
121 $plugintypes = core_component::get_plugin_types();
122 if (isset($plugintypes[$type])) {
123 $expected = $plugintypes[$type] . '/' . $plugin;
126 if (strpos($expected, '$CFG->dirroot') !== 0) {
127 $expected = str_replace($CFG->dirroot, '$CFG->dirroot', $expected);
129 if (strpos($current, '$CFG->dirroot') !== 0) {
130 $current = str_replace($CFG->dirroot, '$CFG->dirroot', $current);
132 $a = new stdClass();
133 $a->component = $component;
134 $a->expected = $expected;
135 $a->current = $current;
136 parent::__construct('detectedmisplacedplugin', 'core_plugin', "$CFG->wwwroot/$CFG->admin/index.php", $a);
141 * Static class monitors performance of upgrade steps.
143 class core_upgrade_time {
144 /** @var float Time at start of current upgrade (plugin/system) */
145 protected static $before;
146 /** @var float Time at end of last savepoint */
147 protected static $lastsavepoint;
148 /** @var bool Flag to indicate whether we are recording timestamps or not. */
149 protected static $isrecording = false;
152 * Records current time at the start of the current upgrade item, e.g. plugin.
154 public static function record_start() {
155 self::$before = microtime(true);
156 self::$lastsavepoint = self::$before;
157 self::$isrecording = true;
161 * Records current time at the end of a given numbered step.
163 * @param float $version Version number (may have decimals, or not)
165 public static function record_savepoint($version) {
166 global $CFG, $OUTPUT;
168 // In developer debug mode we show a notification after each individual save point.
169 if ($CFG->debugdeveloper && self::$isrecording) {
170 $time = microtime(true);
172 $notification = new \core\output\notification($version . ': ' .
173 get_string('successduration', '', format_float($time - self::$lastsavepoint, 2)),
174 \core\output\notification::NOTIFY_SUCCESS);
175 $notification->set_show_closebutton(false);
176 echo $OUTPUT->render($notification);
177 self::$lastsavepoint = $time;
182 * Gets the time since the record_start function was called, rounded to 2 digits.
184 * @return float Elapsed time
186 public static function get_elapsed() {
187 return microtime(true) - self::$before;
192 * Sets maximum expected time needed for upgrade task.
193 * Please always make sure that upgrade will not run longer!
195 * The script may be automatically aborted if upgrade times out.
197 * @category upgrade
198 * @param int $max_execution_time in seconds (can not be less than 60 s)
200 function upgrade_set_timeout($max_execution_time=300) {
201 global $CFG;
203 if (!isset($CFG->upgraderunning) or $CFG->upgraderunning < time()) {
204 $upgraderunning = get_config(null, 'upgraderunning');
205 } else {
206 $upgraderunning = $CFG->upgraderunning;
209 if (!$upgraderunning) {
210 if (CLI_SCRIPT) {
211 // never stop CLI upgrades
212 $upgraderunning = 0;
213 } else {
214 // web upgrade not running or aborted
215 print_error('upgradetimedout', 'admin', "$CFG->wwwroot/$CFG->admin/");
219 if ($max_execution_time < 60) {
220 // protection against 0 here
221 $max_execution_time = 60;
224 $expected_end = time() + $max_execution_time;
226 if ($expected_end < $upgraderunning + 10 and $expected_end > $upgraderunning - 10) {
227 // no need to store new end, it is nearly the same ;-)
228 return;
231 if (CLI_SCRIPT) {
232 // there is no point in timing out of CLI scripts, admins can stop them if necessary
233 core_php_time_limit::raise();
234 } else {
235 core_php_time_limit::raise($max_execution_time);
237 set_config('upgraderunning', $expected_end); // keep upgrade locked until this time
241 * Upgrade savepoint, marks end of each upgrade block.
242 * It stores new main version, resets upgrade timeout
243 * and abort upgrade if user cancels page loading.
245 * Please do not make large upgrade blocks with lots of operations,
246 * for example when adding tables keep only one table operation per block.
248 * @category upgrade
249 * @param bool $result false if upgrade step failed, true if completed
250 * @param string or float $version main version
251 * @param bool $allowabort allow user to abort script execution here
252 * @return void
254 function upgrade_main_savepoint($result, $version, $allowabort=true) {
255 global $CFG;
257 //sanity check to avoid confusion with upgrade_mod_savepoint usage.
258 if (!is_bool($allowabort)) {
259 $errormessage = 'Parameter type mismatch. Are you mixing up upgrade_main_savepoint() and upgrade_mod_savepoint()?';
260 throw new coding_exception($errormessage);
263 if (!$result) {
264 throw new upgrade_exception(null, $version);
267 if ($CFG->version >= $version) {
268 // something really wrong is going on in main upgrade script
269 throw new downgrade_exception(null, $CFG->version, $version);
272 set_config('version', $version);
273 upgrade_log(UPGRADE_LOG_NORMAL, null, 'Upgrade savepoint reached');
275 // reset upgrade timeout to default
276 upgrade_set_timeout();
278 core_upgrade_time::record_savepoint($version);
280 // this is a safe place to stop upgrades if user aborts page loading
281 if ($allowabort and connection_aborted()) {
282 die;
287 * Module upgrade savepoint, marks end of module upgrade blocks
288 * It stores module version, resets upgrade timeout
289 * and abort upgrade if user cancels page loading.
291 * @category upgrade
292 * @param bool $result false if upgrade step failed, true if completed
293 * @param string or float $version main version
294 * @param string $modname name of module
295 * @param bool $allowabort allow user to abort script execution here
296 * @return void
298 function upgrade_mod_savepoint($result, $version, $modname, $allowabort=true) {
299 global $DB;
301 $component = 'mod_'.$modname;
303 if (!$result) {
304 throw new upgrade_exception($component, $version);
307 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
309 if (!$module = $DB->get_record('modules', array('name'=>$modname))) {
310 print_error('modulenotexist', 'debug', '', $modname);
313 if ($dbversion >= $version) {
314 // something really wrong is going on in upgrade script
315 throw new downgrade_exception($component, $dbversion, $version);
317 set_config('version', $version, $component);
319 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
321 // reset upgrade timeout to default
322 upgrade_set_timeout();
324 core_upgrade_time::record_savepoint($version);
326 // this is a safe place to stop upgrades if user aborts page loading
327 if ($allowabort and connection_aborted()) {
328 die;
333 * Blocks upgrade savepoint, marks end of blocks upgrade blocks
334 * It stores block version, resets upgrade timeout
335 * and abort upgrade if user cancels page loading.
337 * @category upgrade
338 * @param bool $result false if upgrade step failed, true if completed
339 * @param string or float $version main version
340 * @param string $blockname name of block
341 * @param bool $allowabort allow user to abort script execution here
342 * @return void
344 function upgrade_block_savepoint($result, $version, $blockname, $allowabort=true) {
345 global $DB;
347 $component = 'block_'.$blockname;
349 if (!$result) {
350 throw new upgrade_exception($component, $version);
353 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
355 if (!$block = $DB->get_record('block', array('name'=>$blockname))) {
356 print_error('blocknotexist', 'debug', '', $blockname);
359 if ($dbversion >= $version) {
360 // something really wrong is going on in upgrade script
361 throw new downgrade_exception($component, $dbversion, $version);
363 set_config('version', $version, $component);
365 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
367 // reset upgrade timeout to default
368 upgrade_set_timeout();
370 core_upgrade_time::record_savepoint($version);
372 // this is a safe place to stop upgrades if user aborts page loading
373 if ($allowabort and connection_aborted()) {
374 die;
379 * Plugins upgrade savepoint, marks end of blocks upgrade blocks
380 * It stores plugin version, resets upgrade timeout
381 * and abort upgrade if user cancels page loading.
383 * @category upgrade
384 * @param bool $result false if upgrade step failed, true if completed
385 * @param string or float $version main version
386 * @param string $type The type of the plugin.
387 * @param string $plugin The name of the plugin.
388 * @param bool $allowabort allow user to abort script execution here
389 * @return void
391 function upgrade_plugin_savepoint($result, $version, $type, $plugin, $allowabort=true) {
392 global $DB;
394 $component = $type.'_'.$plugin;
396 if (!$result) {
397 throw new upgrade_exception($component, $version);
400 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
402 if ($dbversion >= $version) {
403 // Something really wrong is going on in the upgrade script
404 throw new downgrade_exception($component, $dbversion, $version);
406 set_config('version', $version, $component);
407 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
409 // Reset upgrade timeout to default
410 upgrade_set_timeout();
412 core_upgrade_time::record_savepoint($version);
414 // This is a safe place to stop upgrades if user aborts page loading
415 if ($allowabort and connection_aborted()) {
416 die;
421 * Detect if there are leftovers in PHP source files.
423 * During main version upgrades administrators MUST move away
424 * old PHP source files and start from scratch (or better
425 * use git).
427 * @return bool true means borked upgrade, false means previous PHP files were properly removed
429 function upgrade_stale_php_files_present() {
430 global $CFG;
432 $someexamplesofremovedfiles = array(
433 // Removed in 3.8.
434 '/lib/amd/src/modal_confirm.js',
435 '/lib/fonts/font-awesome-4.7.0/css/font-awesome.css',
436 '/lib/jquery/jquery-3.2.1.min.js',
437 '/lib/recaptchalib.php',
438 '/lib/sessionkeepalive_ajax.php',
439 '/lib/yui/src/checknet/js/checknet.js',
440 '/question/amd/src/qbankmanager.js',
441 // Removed in 3.7.
442 '/lib/form/yui/src/showadvanced/js/showadvanced.js',
443 '/lib/tests/output_external_test.php',
444 '/message/amd/src/message_area.js',
445 '/message/templates/message_area.mustache',
446 '/question/yui/src/qbankmanager/build.json',
447 // Removed in 3.6.
448 '/lib/classes/session/memcache.php',
449 '/lib/eventslib.php',
450 '/lib/form/submitlink.php',
451 '/lib/medialib.php',
452 '/lib/password_compat/lib/password.php',
453 // Removed in 3.5.
454 '/lib/dml/mssql_native_moodle_database.php',
455 '/lib/dml/mssql_native_moodle_recordset.php',
456 '/lib/dml/mssql_native_moodle_temptables.php',
457 // Removed in 3.4.
458 '/auth/README.txt',
459 '/calendar/set.php',
460 '/enrol/users.php',
461 '/enrol/yui/rolemanager/assets/skins/sam/rolemanager.css',
462 // Removed in 3.3.
463 '/badges/backpackconnect.php',
464 '/calendar/yui/src/info/assets/skins/sam/moodle-calendar-info.css',
465 '/competency/classes/external/exporter.php',
466 '/mod/forum/forum.js',
467 '/user/pixgroup.php',
468 // Removed in 3.2.
469 '/calendar/preferences.php',
470 '/lib/alfresco/',
471 '/lib/jquery/jquery-1.12.1.min.js',
472 '/lib/password_compat/tests/',
473 '/lib/phpunit/classes/unittestcase.php',
474 // Removed in 3.1.
475 '/lib/classes/log/sql_internal_reader.php',
476 '/lib/zend/',
477 '/mod/forum/pix/icon.gif',
478 '/tag/templates/tagname.mustache',
479 // Removed in 3.0.
480 '/mod/lti/grade.php',
481 '/tag/coursetagslib.php',
482 // Removed in 2.9.
483 '/lib/timezone.txt',
484 // Removed in 2.8.
485 '/course/delete_category_form.php',
486 // Removed in 2.7.
487 '/admin/tool/qeupgradehelper/version.php',
488 // Removed in 2.6.
489 '/admin/block.php',
490 '/admin/oacleanup.php',
491 // Removed in 2.5.
492 '/backup/lib.php',
493 '/backup/bb/README.txt',
494 '/lib/excel/test.php',
495 // Removed in 2.4.
496 '/admin/tool/unittest/simpletestlib.php',
497 // Removed in 2.3.
498 '/lib/minify/builder/',
499 // Removed in 2.2.
500 '/lib/yui/3.4.1pr1/',
501 // Removed in 2.2.
502 '/search/cron_php5.php',
503 '/course/report/log/indexlive.php',
504 '/admin/report/backups/index.php',
505 '/admin/generator.php',
506 // Removed in 2.1.
507 '/lib/yui/2.8.0r4/',
508 // Removed in 2.0.
509 '/blocks/admin/block_admin.php',
510 '/blocks/admin_tree/block_admin_tree.php',
513 foreach ($someexamplesofremovedfiles as $file) {
514 if (file_exists($CFG->dirroot.$file)) {
515 return true;
519 return false;
523 * Upgrade plugins
524 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
525 * return void
527 function upgrade_plugins($type, $startcallback, $endcallback, $verbose) {
528 global $CFG, $DB;
530 /// special cases
531 if ($type === 'mod') {
532 return upgrade_plugins_modules($startcallback, $endcallback, $verbose);
533 } else if ($type === 'block') {
534 return upgrade_plugins_blocks($startcallback, $endcallback, $verbose);
537 $plugs = core_component::get_plugin_list($type);
539 foreach ($plugs as $plug=>$fullplug) {
540 // Reset time so that it works when installing a large number of plugins
541 core_php_time_limit::raise(600);
542 $component = clean_param($type.'_'.$plug, PARAM_COMPONENT); // standardised plugin name
544 // check plugin dir is valid name
545 if (empty($component)) {
546 throw new plugin_defective_exception($type.'_'.$plug, 'Invalid plugin directory name.');
549 if (!is_readable($fullplug.'/version.php')) {
550 continue;
553 $plugin = new stdClass();
554 $plugin->version = null;
555 $module = $plugin; // Prevent some notices when plugin placed in wrong directory.
556 require($fullplug.'/version.php'); // defines $plugin with version etc
557 unset($module);
559 if (empty($plugin->version)) {
560 throw new plugin_defective_exception($component, 'Missing $plugin->version number in version.php.');
563 if (empty($plugin->component)) {
564 throw new plugin_defective_exception($component, 'Missing $plugin->component declaration in version.php.');
567 if ($plugin->component !== $component) {
568 throw new plugin_misplaced_exception($plugin->component, null, $fullplug);
571 $plugin->name = $plug;
572 $plugin->fullname = $component;
574 if (!empty($plugin->requires)) {
575 if ($plugin->requires > $CFG->version) {
576 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
577 } else if ($plugin->requires < 2010000000) {
578 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
582 // try to recover from interrupted install.php if needed
583 if (file_exists($fullplug.'/db/install.php')) {
584 if (get_config($plugin->fullname, 'installrunning')) {
585 require_once($fullplug.'/db/install.php');
586 $recover_install_function = 'xmldb_'.$plugin->fullname.'_install_recovery';
587 if (function_exists($recover_install_function)) {
588 $startcallback($component, true, $verbose);
589 $recover_install_function();
590 unset_config('installrunning', $plugin->fullname);
591 update_capabilities($component);
592 log_update_descriptions($component);
593 external_update_descriptions($component);
594 \core\task\manager::reset_scheduled_tasks_for_component($component);
595 \core_analytics\manager::update_default_models_for_component($component);
596 message_update_providers($component);
597 \core\message\inbound\manager::update_handlers_for_component($component);
598 if ($type === 'message') {
599 message_update_processors($plug);
601 upgrade_plugin_mnet_functions($component);
602 core_tag_area::reset_definitions_for_component($component);
603 $endcallback($component, true, $verbose);
608 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
609 if (empty($installedversion)) { // new installation
610 $startcallback($component, true, $verbose);
612 /// Install tables if defined
613 if (file_exists($fullplug.'/db/install.xml')) {
614 $DB->get_manager()->install_from_xmldb_file($fullplug.'/db/install.xml');
617 /// store version
618 upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false);
620 /// execute post install file
621 if (file_exists($fullplug.'/db/install.php')) {
622 require_once($fullplug.'/db/install.php');
623 set_config('installrunning', 1, $plugin->fullname);
624 $post_install_function = 'xmldb_'.$plugin->fullname.'_install';
625 $post_install_function();
626 unset_config('installrunning', $plugin->fullname);
629 /// Install various components
630 update_capabilities($component);
631 log_update_descriptions($component);
632 external_update_descriptions($component);
633 \core\task\manager::reset_scheduled_tasks_for_component($component);
634 \core_analytics\manager::update_default_models_for_component($component);
635 message_update_providers($component);
636 \core\message\inbound\manager::update_handlers_for_component($component);
637 if ($type === 'message') {
638 message_update_processors($plug);
640 upgrade_plugin_mnet_functions($component);
641 core_tag_area::reset_definitions_for_component($component);
642 $endcallback($component, true, $verbose);
644 } else if ($installedversion < $plugin->version) { // upgrade
645 /// Run the upgrade function for the plugin.
646 $startcallback($component, false, $verbose);
648 if (is_readable($fullplug.'/db/upgrade.php')) {
649 require_once($fullplug.'/db/upgrade.php'); // defines upgrading function
651 $newupgrade_function = 'xmldb_'.$plugin->fullname.'_upgrade';
652 $result = $newupgrade_function($installedversion);
653 } else {
654 $result = true;
657 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
658 if ($installedversion < $plugin->version) {
659 // store version if not already there
660 upgrade_plugin_savepoint($result, $plugin->version, $type, $plug, false);
663 /// Upgrade various components
664 update_capabilities($component);
665 log_update_descriptions($component);
666 external_update_descriptions($component);
667 \core\task\manager::reset_scheduled_tasks_for_component($component);
668 \core_analytics\manager::update_default_models_for_component($component);
669 message_update_providers($component);
670 \core\message\inbound\manager::update_handlers_for_component($component);
671 if ($type === 'message') {
672 // Ugly hack!
673 message_update_processors($plug);
675 upgrade_plugin_mnet_functions($component);
676 core_tag_area::reset_definitions_for_component($component);
677 $endcallback($component, false, $verbose);
679 } else if ($installedversion > $plugin->version) {
680 throw new downgrade_exception($component, $installedversion, $plugin->version);
686 * Find and check all modules and load them up or upgrade them if necessary
688 * @global object
689 * @global object
691 function upgrade_plugins_modules($startcallback, $endcallback, $verbose) {
692 global $CFG, $DB;
694 $mods = core_component::get_plugin_list('mod');
696 foreach ($mods as $mod=>$fullmod) {
698 if ($mod === 'NEWMODULE') { // Someone has unzipped the template, ignore it
699 continue;
702 $component = clean_param('mod_'.$mod, PARAM_COMPONENT);
704 // check module dir is valid name
705 if (empty($component)) {
706 throw new plugin_defective_exception('mod_'.$mod, 'Invalid plugin directory name.');
709 if (!is_readable($fullmod.'/version.php')) {
710 throw new plugin_defective_exception($component, 'Missing version.php');
713 $module = new stdClass();
714 $plugin = new stdClass();
715 $plugin->version = null;
716 require($fullmod .'/version.php'); // Defines $plugin with version etc.
718 // Check if the legacy $module syntax is still used.
719 if (!is_object($module) or (count((array)$module) > 0)) {
720 throw new plugin_defective_exception($component, 'Unsupported $module syntax detected in version.php');
723 // Prepare the record for the {modules} table.
724 $module = clone($plugin);
725 unset($module->version);
726 unset($module->component);
727 unset($module->dependencies);
728 unset($module->release);
730 if (empty($plugin->version)) {
731 throw new plugin_defective_exception($component, 'Missing $plugin->version number in version.php.');
734 if (empty($plugin->component)) {
735 throw new plugin_defective_exception($component, 'Missing $plugin->component declaration in version.php.');
738 if ($plugin->component !== $component) {
739 throw new plugin_misplaced_exception($plugin->component, null, $fullmod);
742 if (!empty($plugin->requires)) {
743 if ($plugin->requires > $CFG->version) {
744 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
745 } else if ($plugin->requires < 2010000000) {
746 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
750 if (empty($module->cron)) {
751 $module->cron = 0;
754 // all modules must have en lang pack
755 if (!is_readable("$fullmod/lang/en/$mod.php")) {
756 throw new plugin_defective_exception($component, 'Missing mandatory en language pack.');
759 $module->name = $mod; // The name MUST match the directory
761 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
763 if (file_exists($fullmod.'/db/install.php')) {
764 if (get_config($module->name, 'installrunning')) {
765 require_once($fullmod.'/db/install.php');
766 $recover_install_function = 'xmldb_'.$module->name.'_install_recovery';
767 if (function_exists($recover_install_function)) {
768 $startcallback($component, true, $verbose);
769 $recover_install_function();
770 unset_config('installrunning', $module->name);
771 // Install various components too
772 update_capabilities($component);
773 log_update_descriptions($component);
774 external_update_descriptions($component);
775 \core\task\manager::reset_scheduled_tasks_for_component($component);
776 \core_analytics\manager::update_default_models_for_component($component);
777 message_update_providers($component);
778 \core\message\inbound\manager::update_handlers_for_component($component);
779 upgrade_plugin_mnet_functions($component);
780 core_tag_area::reset_definitions_for_component($component);
781 $endcallback($component, true, $verbose);
786 if (empty($installedversion)) {
787 $startcallback($component, true, $verbose);
789 /// Execute install.xml (XMLDB) - must be present in all modules
790 $DB->get_manager()->install_from_xmldb_file($fullmod.'/db/install.xml');
792 /// Add record into modules table - may be needed in install.php already
793 $module->id = $DB->insert_record('modules', $module);
794 upgrade_mod_savepoint(true, $plugin->version, $module->name, false);
796 /// Post installation hook - optional
797 if (file_exists("$fullmod/db/install.php")) {
798 require_once("$fullmod/db/install.php");
799 // Set installation running flag, we need to recover after exception or error
800 set_config('installrunning', 1, $module->name);
801 $post_install_function = 'xmldb_'.$module->name.'_install';
802 $post_install_function();
803 unset_config('installrunning', $module->name);
806 /// Install various components
807 update_capabilities($component);
808 log_update_descriptions($component);
809 external_update_descriptions($component);
810 \core\task\manager::reset_scheduled_tasks_for_component($component);
811 \core_analytics\manager::update_default_models_for_component($component);
812 message_update_providers($component);
813 \core\message\inbound\manager::update_handlers_for_component($component);
814 upgrade_plugin_mnet_functions($component);
815 core_tag_area::reset_definitions_for_component($component);
817 $endcallback($component, true, $verbose);
819 } else if ($installedversion < $plugin->version) {
820 /// If versions say that we need to upgrade but no upgrade files are available, notify and continue
821 $startcallback($component, false, $verbose);
823 if (is_readable($fullmod.'/db/upgrade.php')) {
824 require_once($fullmod.'/db/upgrade.php'); // defines new upgrading function
825 $newupgrade_function = 'xmldb_'.$module->name.'_upgrade';
826 $result = $newupgrade_function($installedversion, $module);
827 } else {
828 $result = true;
831 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
832 $currmodule = $DB->get_record('modules', array('name'=>$module->name));
833 if ($installedversion < $plugin->version) {
834 // store version if not already there
835 upgrade_mod_savepoint($result, $plugin->version, $mod, false);
838 // update cron flag if needed
839 if ($currmodule->cron != $module->cron) {
840 $DB->set_field('modules', 'cron', $module->cron, array('name' => $module->name));
843 // Upgrade various components
844 update_capabilities($component);
845 log_update_descriptions($component);
846 external_update_descriptions($component);
847 \core\task\manager::reset_scheduled_tasks_for_component($component);
848 \core_analytics\manager::update_default_models_for_component($component);
849 message_update_providers($component);
850 \core\message\inbound\manager::update_handlers_for_component($component);
851 upgrade_plugin_mnet_functions($component);
852 core_tag_area::reset_definitions_for_component($component);
854 $endcallback($component, false, $verbose);
856 } else if ($installedversion > $plugin->version) {
857 throw new downgrade_exception($component, $installedversion, $plugin->version);
864 * This function finds all available blocks and install them
865 * into blocks table or do all the upgrade process if newer.
867 * @global object
868 * @global object
870 function upgrade_plugins_blocks($startcallback, $endcallback, $verbose) {
871 global $CFG, $DB;
873 require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
875 $blocktitles = array(); // we do not want duplicate titles
877 //Is this a first install
878 $first_install = null;
880 $blocks = core_component::get_plugin_list('block');
882 foreach ($blocks as $blockname=>$fullblock) {
884 if (is_null($first_install)) {
885 $first_install = ($DB->count_records('block_instances') == 0);
888 if ($blockname === 'NEWBLOCK') { // Someone has unzipped the template, ignore it
889 continue;
892 $component = clean_param('block_'.$blockname, PARAM_COMPONENT);
894 // check block dir is valid name
895 if (empty($component)) {
896 throw new plugin_defective_exception('block_'.$blockname, 'Invalid plugin directory name.');
899 if (!is_readable($fullblock.'/version.php')) {
900 throw new plugin_defective_exception('block/'.$blockname, 'Missing version.php file.');
902 $plugin = new stdClass();
903 $plugin->version = null;
904 $plugin->cron = 0;
905 $module = $plugin; // Prevent some notices when module placed in wrong directory.
906 include($fullblock.'/version.php');
907 unset($module);
908 $block = clone($plugin);
909 unset($block->version);
910 unset($block->component);
911 unset($block->dependencies);
912 unset($block->release);
914 if (empty($plugin->version)) {
915 throw new plugin_defective_exception($component, 'Missing block version number in version.php.');
918 if (empty($plugin->component)) {
919 throw new plugin_defective_exception($component, 'Missing $plugin->component declaration in version.php.');
922 if ($plugin->component !== $component) {
923 throw new plugin_misplaced_exception($plugin->component, null, $fullblock);
926 if (!empty($plugin->requires)) {
927 if ($plugin->requires > $CFG->version) {
928 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
929 } else if ($plugin->requires < 2010000000) {
930 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
934 if (!is_readable($fullblock.'/block_'.$blockname.'.php')) {
935 throw new plugin_defective_exception('block/'.$blockname, 'Missing main block class file.');
937 include_once($fullblock.'/block_'.$blockname.'.php');
939 $classname = 'block_'.$blockname;
941 if (!class_exists($classname)) {
942 throw new plugin_defective_exception($component, 'Can not load main class.');
945 $blockobj = new $classname; // This is what we'll be testing
946 $blocktitle = $blockobj->get_title();
948 // OK, it's as we all hoped. For further tests, the object will do them itself.
949 if (!$blockobj->_self_test()) {
950 throw new plugin_defective_exception($component, 'Self test failed.');
953 $block->name = $blockname; // The name MUST match the directory
955 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
957 if (file_exists($fullblock.'/db/install.php')) {
958 if (get_config('block_'.$blockname, 'installrunning')) {
959 require_once($fullblock.'/db/install.php');
960 $recover_install_function = 'xmldb_block_'.$blockname.'_install_recovery';
961 if (function_exists($recover_install_function)) {
962 $startcallback($component, true, $verbose);
963 $recover_install_function();
964 unset_config('installrunning', 'block_'.$blockname);
965 // Install various components
966 update_capabilities($component);
967 log_update_descriptions($component);
968 external_update_descriptions($component);
969 \core\task\manager::reset_scheduled_tasks_for_component($component);
970 \core_analytics\manager::update_default_models_for_component($component);
971 message_update_providers($component);
972 \core\message\inbound\manager::update_handlers_for_component($component);
973 upgrade_plugin_mnet_functions($component);
974 core_tag_area::reset_definitions_for_component($component);
975 $endcallback($component, true, $verbose);
980 if (empty($installedversion)) { // block not installed yet, so install it
981 $conflictblock = array_search($blocktitle, $blocktitles);
982 if ($conflictblock !== false) {
983 // Duplicate block titles are not allowed, they confuse people
984 // AND PHP's associative arrays ;)
985 throw new plugin_defective_exception($component, get_string('blocknameconflict', 'error', (object)array('name'=>$block->name, 'conflict'=>$conflictblock)));
987 $startcallback($component, true, $verbose);
989 if (file_exists($fullblock.'/db/install.xml')) {
990 $DB->get_manager()->install_from_xmldb_file($fullblock.'/db/install.xml');
992 $block->id = $DB->insert_record('block', $block);
993 upgrade_block_savepoint(true, $plugin->version, $block->name, false);
995 if (file_exists($fullblock.'/db/install.php')) {
996 require_once($fullblock.'/db/install.php');
997 // Set installation running flag, we need to recover after exception or error
998 set_config('installrunning', 1, 'block_'.$blockname);
999 $post_install_function = 'xmldb_block_'.$blockname.'_install';
1000 $post_install_function();
1001 unset_config('installrunning', 'block_'.$blockname);
1004 $blocktitles[$block->name] = $blocktitle;
1006 // Install various components
1007 update_capabilities($component);
1008 log_update_descriptions($component);
1009 external_update_descriptions($component);
1010 \core\task\manager::reset_scheduled_tasks_for_component($component);
1011 \core_analytics\manager::update_default_models_for_component($component);
1012 message_update_providers($component);
1013 \core\message\inbound\manager::update_handlers_for_component($component);
1014 core_tag_area::reset_definitions_for_component($component);
1015 upgrade_plugin_mnet_functions($component);
1017 $endcallback($component, true, $verbose);
1019 } else if ($installedversion < $plugin->version) {
1020 $startcallback($component, false, $verbose);
1022 if (is_readable($fullblock.'/db/upgrade.php')) {
1023 require_once($fullblock.'/db/upgrade.php'); // defines new upgrading function
1024 $newupgrade_function = 'xmldb_block_'.$blockname.'_upgrade';
1025 $result = $newupgrade_function($installedversion, $block);
1026 } else {
1027 $result = true;
1030 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
1031 $currblock = $DB->get_record('block', array('name'=>$block->name));
1032 if ($installedversion < $plugin->version) {
1033 // store version if not already there
1034 upgrade_block_savepoint($result, $plugin->version, $block->name, false);
1037 if ($currblock->cron != $block->cron) {
1038 // update cron flag if needed
1039 $DB->set_field('block', 'cron', $block->cron, array('id' => $currblock->id));
1042 // Upgrade various components
1043 update_capabilities($component);
1044 log_update_descriptions($component);
1045 external_update_descriptions($component);
1046 \core\task\manager::reset_scheduled_tasks_for_component($component);
1047 \core_analytics\manager::update_default_models_for_component($component);
1048 message_update_providers($component);
1049 \core\message\inbound\manager::update_handlers_for_component($component);
1050 upgrade_plugin_mnet_functions($component);
1051 core_tag_area::reset_definitions_for_component($component);
1053 $endcallback($component, false, $verbose);
1055 } else if ($installedversion > $plugin->version) {
1056 throw new downgrade_exception($component, $installedversion, $plugin->version);
1061 // Finally, if we are in the first_install of BLOCKS setup frontpage and admin page blocks
1062 if ($first_install) {
1063 //Iterate over each course - there should be only site course here now
1064 if ($courses = $DB->get_records('course')) {
1065 foreach ($courses as $course) {
1066 blocks_add_default_course_blocks($course);
1070 blocks_add_default_system_blocks();
1076 * Log_display description function used during install and upgrade.
1078 * @param string $component name of component (moodle, mod_assignment, etc.)
1079 * @return void
1081 function log_update_descriptions($component) {
1082 global $DB;
1084 $defpath = core_component::get_component_directory($component).'/db/log.php';
1086 if (!file_exists($defpath)) {
1087 $DB->delete_records('log_display', array('component'=>$component));
1088 return;
1091 // load new info
1092 $logs = array();
1093 include($defpath);
1094 $newlogs = array();
1095 foreach ($logs as $log) {
1096 $newlogs[$log['module'].'-'.$log['action']] = $log; // kind of unique name
1098 unset($logs);
1099 $logs = $newlogs;
1101 $fields = array('module', 'action', 'mtable', 'field');
1102 // update all log fist
1103 $dblogs = $DB->get_records('log_display', array('component'=>$component));
1104 foreach ($dblogs as $dblog) {
1105 $name = $dblog->module.'-'.$dblog->action;
1107 if (empty($logs[$name])) {
1108 $DB->delete_records('log_display', array('id'=>$dblog->id));
1109 continue;
1112 $log = $logs[$name];
1113 unset($logs[$name]);
1115 $update = false;
1116 foreach ($fields as $field) {
1117 if ($dblog->$field != $log[$field]) {
1118 $dblog->$field = $log[$field];
1119 $update = true;
1122 if ($update) {
1123 $DB->update_record('log_display', $dblog);
1126 foreach ($logs as $log) {
1127 $dblog = (object)$log;
1128 $dblog->component = $component;
1129 $DB->insert_record('log_display', $dblog);
1134 * Web service discovery function used during install and upgrade.
1135 * @param string $component name of component (moodle, mod_assignment, etc.)
1136 * @return void
1138 function external_update_descriptions($component) {
1139 global $DB, $CFG;
1141 $defpath = core_component::get_component_directory($component).'/db/services.php';
1143 if (!file_exists($defpath)) {
1144 require_once($CFG->dirroot.'/lib/externallib.php');
1145 external_delete_descriptions($component);
1146 return;
1149 // load new info
1150 $functions = array();
1151 $services = array();
1152 include($defpath);
1154 // update all function fist
1155 $dbfunctions = $DB->get_records('external_functions', array('component'=>$component));
1156 foreach ($dbfunctions as $dbfunction) {
1157 if (empty($functions[$dbfunction->name])) {
1158 $DB->delete_records('external_functions', array('id'=>$dbfunction->id));
1159 // do not delete functions from external_services_functions, beacuse
1160 // we want to notify admins when functions used in custom services disappear
1162 //TODO: this looks wrong, we have to delete it eventually (skodak)
1163 continue;
1166 $function = $functions[$dbfunction->name];
1167 unset($functions[$dbfunction->name]);
1168 $function['classpath'] = empty($function['classpath']) ? null : $function['classpath'];
1170 $update = false;
1171 if ($dbfunction->classname != $function['classname']) {
1172 $dbfunction->classname = $function['classname'];
1173 $update = true;
1175 if ($dbfunction->methodname != $function['methodname']) {
1176 $dbfunction->methodname = $function['methodname'];
1177 $update = true;
1179 if ($dbfunction->classpath != $function['classpath']) {
1180 $dbfunction->classpath = $function['classpath'];
1181 $update = true;
1183 $functioncapabilities = array_key_exists('capabilities', $function)?$function['capabilities']:'';
1184 if ($dbfunction->capabilities != $functioncapabilities) {
1185 $dbfunction->capabilities = $functioncapabilities;
1186 $update = true;
1189 if (isset($function['services']) and is_array($function['services'])) {
1190 sort($function['services']);
1191 $functionservices = implode(',', $function['services']);
1192 } else {
1193 // Force null values in the DB.
1194 $functionservices = null;
1197 if ($dbfunction->services != $functionservices) {
1198 // Now, we need to check if services were removed, in that case we need to remove the function from them.
1199 $servicesremoved = array_diff(explode(",", $dbfunction->services), explode(",", $functionservices));
1200 foreach ($servicesremoved as $removedshortname) {
1201 if ($externalserviceid = $DB->get_field('external_services', 'id', array("shortname" => $removedshortname))) {
1202 $DB->delete_records('external_services_functions', array('functionname' => $dbfunction->name,
1203 'externalserviceid' => $externalserviceid));
1207 $dbfunction->services = $functionservices;
1208 $update = true;
1210 if ($update) {
1211 $DB->update_record('external_functions', $dbfunction);
1214 foreach ($functions as $fname => $function) {
1215 $dbfunction = new stdClass();
1216 $dbfunction->name = $fname;
1217 $dbfunction->classname = $function['classname'];
1218 $dbfunction->methodname = $function['methodname'];
1219 $dbfunction->classpath = empty($function['classpath']) ? null : $function['classpath'];
1220 $dbfunction->component = $component;
1221 $dbfunction->capabilities = array_key_exists('capabilities', $function)?$function['capabilities']:'';
1223 if (isset($function['services']) and is_array($function['services'])) {
1224 sort($function['services']);
1225 $dbfunction->services = implode(',', $function['services']);
1226 } else {
1227 // Force null values in the DB.
1228 $dbfunction->services = null;
1231 $dbfunction->id = $DB->insert_record('external_functions', $dbfunction);
1233 unset($functions);
1235 // now deal with services
1236 $dbservices = $DB->get_records('external_services', array('component'=>$component));
1237 foreach ($dbservices as $dbservice) {
1238 if (empty($services[$dbservice->name])) {
1239 $DB->delete_records('external_tokens', array('externalserviceid'=>$dbservice->id));
1240 $DB->delete_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
1241 $DB->delete_records('external_services_users', array('externalserviceid'=>$dbservice->id));
1242 $DB->delete_records('external_services', array('id'=>$dbservice->id));
1243 continue;
1245 $service = $services[$dbservice->name];
1246 unset($services[$dbservice->name]);
1247 $service['enabled'] = empty($service['enabled']) ? 0 : $service['enabled'];
1248 $service['requiredcapability'] = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1249 $service['restrictedusers'] = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
1250 $service['downloadfiles'] = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
1251 $service['uploadfiles'] = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles'];
1252 $service['shortname'] = !isset($service['shortname']) ? null : $service['shortname'];
1254 $update = false;
1255 if ($dbservice->requiredcapability != $service['requiredcapability']) {
1256 $dbservice->requiredcapability = $service['requiredcapability'];
1257 $update = true;
1259 if ($dbservice->restrictedusers != $service['restrictedusers']) {
1260 $dbservice->restrictedusers = $service['restrictedusers'];
1261 $update = true;
1263 if ($dbservice->downloadfiles != $service['downloadfiles']) {
1264 $dbservice->downloadfiles = $service['downloadfiles'];
1265 $update = true;
1267 if ($dbservice->uploadfiles != $service['uploadfiles']) {
1268 $dbservice->uploadfiles = $service['uploadfiles'];
1269 $update = true;
1271 //if shortname is not a PARAM_ALPHANUMEXT, fail (tested here for service update and creation)
1272 if (isset($service['shortname']) and
1273 (clean_param($service['shortname'], PARAM_ALPHANUMEXT) != $service['shortname'])) {
1274 throw new moodle_exception('installserviceshortnameerror', 'webservice', '', $service['shortname']);
1276 if ($dbservice->shortname != $service['shortname']) {
1277 //check that shortname is unique
1278 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
1279 $existingservice = $DB->get_record('external_services',
1280 array('shortname' => $service['shortname']));
1281 if (!empty($existingservice)) {
1282 throw new moodle_exception('installexistingserviceshortnameerror', 'webservice', '', $service['shortname']);
1285 $dbservice->shortname = $service['shortname'];
1286 $update = true;
1288 if ($update) {
1289 $DB->update_record('external_services', $dbservice);
1292 $functions = $DB->get_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
1293 foreach ($functions as $function) {
1294 $key = array_search($function->functionname, $service['functions']);
1295 if ($key === false) {
1296 $DB->delete_records('external_services_functions', array('id'=>$function->id));
1297 } else {
1298 unset($service['functions'][$key]);
1301 foreach ($service['functions'] as $fname) {
1302 $newf = new stdClass();
1303 $newf->externalserviceid = $dbservice->id;
1304 $newf->functionname = $fname;
1305 $DB->insert_record('external_services_functions', $newf);
1307 unset($functions);
1309 foreach ($services as $name => $service) {
1310 //check that shortname is unique
1311 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
1312 $existingservice = $DB->get_record('external_services',
1313 array('shortname' => $service['shortname']));
1314 if (!empty($existingservice)) {
1315 throw new moodle_exception('installserviceshortnameerror', 'webservice');
1319 $dbservice = new stdClass();
1320 $dbservice->name = $name;
1321 $dbservice->enabled = empty($service['enabled']) ? 0 : $service['enabled'];
1322 $dbservice->requiredcapability = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1323 $dbservice->restrictedusers = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
1324 $dbservice->downloadfiles = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
1325 $dbservice->uploadfiles = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles'];
1326 $dbservice->shortname = !isset($service['shortname']) ? null : $service['shortname'];
1327 $dbservice->component = $component;
1328 $dbservice->timecreated = time();
1329 $dbservice->id = $DB->insert_record('external_services', $dbservice);
1330 foreach ($service['functions'] as $fname) {
1331 $newf = new stdClass();
1332 $newf->externalserviceid = $dbservice->id;
1333 $newf->functionname = $fname;
1334 $DB->insert_record('external_services_functions', $newf);
1340 * Allow plugins and subsystems to add external functions to other plugins or built-in services.
1341 * This function is executed just after all the plugins have been updated.
1343 function external_update_services() {
1344 global $DB;
1346 // Look for external functions that want to be added in existing services.
1347 $functions = $DB->get_records_select('external_functions', 'services IS NOT NULL');
1349 $servicescache = array();
1350 foreach ($functions as $function) {
1351 // Prevent edge cases.
1352 if (empty($function->services)) {
1353 continue;
1355 $services = explode(',', $function->services);
1357 foreach ($services as $serviceshortname) {
1358 // Get the service id by shortname.
1359 if (!empty($servicescache[$serviceshortname])) {
1360 $serviceid = $servicescache[$serviceshortname];
1361 } else if ($service = $DB->get_record('external_services', array('shortname' => $serviceshortname))) {
1362 // If the component is empty, it means that is not a built-in service.
1363 // We don't allow functions to inject themselves in services created by an user in Moodle.
1364 if (empty($service->component)) {
1365 continue;
1367 $serviceid = $service->id;
1368 $servicescache[$serviceshortname] = $serviceid;
1369 } else {
1370 // Service not found.
1371 continue;
1373 // Finally add the function to the service.
1374 $newf = new stdClass();
1375 $newf->externalserviceid = $serviceid;
1376 $newf->functionname = $function->name;
1378 if (!$DB->record_exists('external_services_functions', (array)$newf)) {
1379 $DB->insert_record('external_services_functions', $newf);
1386 * upgrade logging functions
1388 function upgrade_handle_exception($ex, $plugin = null) {
1389 global $CFG;
1391 // rollback everything, we need to log all upgrade problems
1392 abort_all_db_transactions();
1394 $info = get_exception_info($ex);
1396 // First log upgrade error
1397 upgrade_log(UPGRADE_LOG_ERROR, $plugin, 'Exception: ' . get_class($ex), $info->message, $info->backtrace);
1399 // Always turn on debugging - admins need to know what is going on
1400 set_debugging(DEBUG_DEVELOPER, true);
1402 default_exception_handler($ex, true, $plugin);
1406 * Adds log entry into upgrade_log table
1408 * @param int $type UPGRADE_LOG_NORMAL, UPGRADE_LOG_NOTICE or UPGRADE_LOG_ERROR
1409 * @param string $plugin frankenstyle component name
1410 * @param string $info short description text of log entry
1411 * @param string $details long problem description
1412 * @param string $backtrace string used for errors only
1413 * @return void
1415 function upgrade_log($type, $plugin, $info, $details=null, $backtrace=null) {
1416 global $DB, $USER, $CFG;
1418 if (empty($plugin)) {
1419 $plugin = 'core';
1422 list($plugintype, $pluginname) = core_component::normalize_component($plugin);
1423 $component = is_null($pluginname) ? $plugintype : $plugintype . '_' . $pluginname;
1425 $backtrace = format_backtrace($backtrace, true);
1427 $currentversion = null;
1428 $targetversion = null;
1430 //first try to find out current version number
1431 if ($plugintype === 'core') {
1432 //main
1433 $currentversion = $CFG->version;
1435 $version = null;
1436 include("$CFG->dirroot/version.php");
1437 $targetversion = $version;
1439 } else {
1440 $pluginversion = get_config($component, 'version');
1441 if (!empty($pluginversion)) {
1442 $currentversion = $pluginversion;
1444 $cd = core_component::get_component_directory($component);
1445 if (file_exists("$cd/version.php")) {
1446 $plugin = new stdClass();
1447 $plugin->version = null;
1448 $module = $plugin;
1449 include("$cd/version.php");
1450 $targetversion = $plugin->version;
1454 $log = new stdClass();
1455 $log->type = $type;
1456 $log->plugin = $component;
1457 $log->version = $currentversion;
1458 $log->targetversion = $targetversion;
1459 $log->info = $info;
1460 $log->details = $details;
1461 $log->backtrace = $backtrace;
1462 $log->userid = $USER->id;
1463 $log->timemodified = time();
1464 try {
1465 $DB->insert_record('upgrade_log', $log);
1466 } catch (Exception $ignored) {
1467 // possible during install or 2.0 upgrade
1472 * Marks start of upgrade, blocks any other access to site.
1473 * The upgrade is finished at the end of script or after timeout.
1475 * @global object
1476 * @global object
1477 * @global object
1479 function upgrade_started($preinstall=false) {
1480 global $CFG, $DB, $PAGE, $OUTPUT;
1482 static $started = false;
1484 if ($preinstall) {
1485 ignore_user_abort(true);
1486 upgrade_setup_debug(true);
1488 } else if ($started) {
1489 upgrade_set_timeout(120);
1491 } else {
1492 if (!CLI_SCRIPT and !$PAGE->headerprinted) {
1493 $strupgrade = get_string('upgradingversion', 'admin');
1494 $PAGE->set_pagelayout('maintenance');
1495 upgrade_init_javascript();
1496 $PAGE->set_title($strupgrade.' - Moodle '.$CFG->target_release);
1497 $PAGE->set_heading($strupgrade);
1498 $PAGE->navbar->add($strupgrade);
1499 $PAGE->set_cacheable(false);
1500 echo $OUTPUT->header();
1503 ignore_user_abort(true);
1504 core_shutdown_manager::register_function('upgrade_finished_handler');
1505 upgrade_setup_debug(true);
1506 set_config('upgraderunning', time()+300);
1507 $started = true;
1512 * Internal function - executed if upgrade interrupted.
1514 function upgrade_finished_handler() {
1515 upgrade_finished();
1519 * Indicates upgrade is finished.
1521 * This function may be called repeatedly.
1523 * @global object
1524 * @global object
1526 function upgrade_finished($continueurl=null) {
1527 global $CFG, $DB, $OUTPUT;
1529 if (!empty($CFG->upgraderunning)) {
1530 unset_config('upgraderunning');
1531 // We have to forcefully purge the caches using the writer here.
1532 // This has to be done after we unset the config var. If someone hits the site while this is set they will
1533 // cause the config values to propogate to the caches.
1534 // Caches are purged after the last step in an upgrade but there is several code routines that exceute between
1535 // then and now that leaving a window for things to fall out of sync.
1536 cache_helper::purge_all(true);
1537 upgrade_setup_debug(false);
1538 ignore_user_abort(false);
1539 if ($continueurl) {
1540 echo $OUTPUT->continue_button($continueurl);
1541 echo $OUTPUT->footer();
1542 die;
1548 * @global object
1549 * @global object
1551 function upgrade_setup_debug($starting) {
1552 global $CFG, $DB;
1554 static $originaldebug = null;
1556 if ($starting) {
1557 if ($originaldebug === null) {
1558 $originaldebug = $DB->get_debug();
1560 if (!empty($CFG->upgradeshowsql)) {
1561 $DB->set_debug(true);
1563 } else {
1564 $DB->set_debug($originaldebug);
1568 function print_upgrade_separator() {
1569 if (!CLI_SCRIPT) {
1570 echo '<hr />';
1575 * Default start upgrade callback
1576 * @param string $plugin
1577 * @param bool $installation true if installation, false means upgrade
1579 function print_upgrade_part_start($plugin, $installation, $verbose) {
1580 global $OUTPUT;
1581 if (empty($plugin) or $plugin == 'moodle') {
1582 upgrade_started($installation); // does not store upgrade running flag yet
1583 if ($verbose) {
1584 echo $OUTPUT->heading(get_string('coresystem'));
1586 } else {
1587 upgrade_started();
1588 if ($verbose) {
1589 echo $OUTPUT->heading($plugin);
1592 if ($installation) {
1593 if (empty($plugin) or $plugin == 'moodle') {
1594 // no need to log - log table not yet there ;-)
1595 } else {
1596 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin installation');
1598 } else {
1599 core_upgrade_time::record_start();
1600 if (empty($plugin) or $plugin == 'moodle') {
1601 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting core upgrade');
1602 } else {
1603 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin upgrade');
1609 * Default end upgrade callback
1610 * @param string $plugin
1611 * @param bool $installation true if installation, false means upgrade
1613 function print_upgrade_part_end($plugin, $installation, $verbose) {
1614 global $OUTPUT;
1615 upgrade_started();
1616 if ($installation) {
1617 if (empty($plugin) or $plugin == 'moodle') {
1618 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core installed');
1619 } else {
1620 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin installed');
1622 } else {
1623 if (empty($plugin) or $plugin == 'moodle') {
1624 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core upgraded');
1625 } else {
1626 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin upgraded');
1629 if ($verbose) {
1630 if ($installation) {
1631 $message = get_string('success');
1632 } else {
1633 $duration = core_upgrade_time::get_elapsed();
1634 $message = get_string('successduration', '', format_float($duration, 2));
1636 $notification = new \core\output\notification($message, \core\output\notification::NOTIFY_SUCCESS);
1637 $notification->set_show_closebutton(false);
1638 echo $OUTPUT->render($notification);
1639 print_upgrade_separator();
1644 * Sets up JS code required for all upgrade scripts.
1645 * @global object
1647 function upgrade_init_javascript() {
1648 global $PAGE;
1649 // scroll to the end of each upgrade page so that ppl see either error or continue button,
1650 // no need to scroll continuously any more, it is enough to jump to end once the footer is printed ;-)
1651 $js = "window.scrollTo(0, 5000000);";
1652 $PAGE->requires->js_init_code($js);
1656 * Try to upgrade the given language pack (or current language)
1658 * @param string $lang the code of the language to update, defaults to the current language
1660 function upgrade_language_pack($lang = null) {
1661 global $CFG;
1663 if (!empty($CFG->skiplangupgrade)) {
1664 return;
1667 if (!file_exists("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php")) {
1668 // weird, somebody uninstalled the import utility
1669 return;
1672 if (!$lang) {
1673 $lang = current_language();
1676 if (!get_string_manager()->translation_exists($lang)) {
1677 return;
1680 get_string_manager()->reset_caches();
1682 if ($lang === 'en') {
1683 return; // Nothing to do
1686 upgrade_started(false);
1688 require_once("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php");
1689 tool_langimport_preupgrade_update($lang);
1691 get_string_manager()->reset_caches();
1693 print_upgrade_separator();
1697 * Build the current theme so that the user doesn't have to wait for it
1698 * to build on the first page load after the install / upgrade.
1700 function upgrade_themes() {
1701 global $CFG;
1703 require_once("{$CFG->libdir}/outputlib.php");
1705 // Build the current theme so that the user can immediately
1706 // browse the site without having to wait for the theme to build.
1707 $themeconfig = theme_config::load($CFG->theme);
1708 $direction = right_to_left() ? 'rtl' : 'ltr';
1709 theme_build_css_for_themes([$themeconfig], [$direction]);
1711 // Only queue the task if there isn't already one queued.
1712 if (empty(\core\task\manager::get_adhoc_tasks('\\core\\task\\build_installed_themes_task'))) {
1713 // Queue a task to build all of the site themes at some point
1714 // later. These can happen offline because it doesn't block the
1715 // user unless they quickly change theme.
1716 $adhoctask = new \core\task\build_installed_themes_task();
1717 \core\task\manager::queue_adhoc_task($adhoctask);
1722 * Install core moodle tables and initialize
1723 * @param float $version target version
1724 * @param bool $verbose
1725 * @return void, may throw exception
1727 function install_core($version, $verbose) {
1728 global $CFG, $DB;
1730 // We can not call purge_all_caches() yet, make sure the temp and cache dirs exist and are empty.
1731 remove_dir($CFG->cachedir.'', true);
1732 make_cache_directory('', true);
1734 remove_dir($CFG->localcachedir.'', true);
1735 make_localcache_directory('', true);
1737 remove_dir($CFG->tempdir.'', true);
1738 make_temp_directory('', true);
1740 remove_dir($CFG->backuptempdir.'', true);
1741 make_backup_temp_directory('', true);
1743 remove_dir($CFG->dataroot.'/muc', true);
1744 make_writable_directory($CFG->dataroot.'/muc', true);
1746 try {
1747 core_php_time_limit::raise(600);
1748 print_upgrade_part_start('moodle', true, $verbose); // does not store upgrade running flag
1750 $DB->get_manager()->install_from_xmldb_file("$CFG->libdir/db/install.xml");
1751 upgrade_started(); // we want the flag to be stored in config table ;-)
1753 // set all core default records and default settings
1754 require_once("$CFG->libdir/db/install.php");
1755 xmldb_main_install(); // installs the capabilities too
1757 // store version
1758 upgrade_main_savepoint(true, $version, false);
1760 // Continue with the installation
1761 log_update_descriptions('moodle');
1762 external_update_descriptions('moodle');
1763 \core\task\manager::reset_scheduled_tasks_for_component('moodle');
1764 \core_analytics\manager::update_default_models_for_component('moodle');
1765 message_update_providers('moodle');
1766 \core\message\inbound\manager::update_handlers_for_component('moodle');
1767 core_tag_area::reset_definitions_for_component('moodle');
1769 // Write default settings unconditionally
1770 admin_apply_default_settings(NULL, true);
1772 print_upgrade_part_end(null, true, $verbose);
1774 // Purge all caches. They're disabled but this ensures that we don't have any persistent data just in case something
1775 // during installation didn't use APIs.
1776 cache_helper::purge_all();
1777 } catch (exception $ex) {
1778 upgrade_handle_exception($ex);
1779 } catch (Throwable $ex) {
1780 // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
1781 upgrade_handle_exception($ex);
1786 * Upgrade moodle core
1787 * @param float $version target version
1788 * @param bool $verbose
1789 * @return void, may throw exception
1791 function upgrade_core($version, $verbose) {
1792 global $CFG, $SITE, $DB, $COURSE;
1794 raise_memory_limit(MEMORY_EXTRA);
1796 require_once($CFG->libdir.'/db/upgrade.php'); // Defines upgrades
1798 try {
1799 // Reset caches before any output.
1800 cache_helper::purge_all(true);
1801 purge_all_caches();
1803 // Upgrade current language pack if we can
1804 upgrade_language_pack();
1806 print_upgrade_part_start('moodle', false, $verbose);
1808 // Pre-upgrade scripts for local hack workarounds.
1809 $preupgradefile = "$CFG->dirroot/local/preupgrade.php";
1810 if (file_exists($preupgradefile)) {
1811 core_php_time_limit::raise();
1812 require($preupgradefile);
1813 // Reset upgrade timeout to default.
1814 upgrade_set_timeout();
1817 $result = xmldb_main_upgrade($CFG->version);
1818 if ($version > $CFG->version) {
1819 // store version if not already there
1820 upgrade_main_savepoint($result, $version, false);
1823 // In case structure of 'course' table has been changed and we forgot to update $SITE, re-read it from db.
1824 $SITE = $DB->get_record('course', array('id' => $SITE->id));
1825 $COURSE = clone($SITE);
1827 // perform all other component upgrade routines
1828 update_capabilities('moodle');
1829 log_update_descriptions('moodle');
1830 external_update_descriptions('moodle');
1831 \core\task\manager::reset_scheduled_tasks_for_component('moodle');
1832 \core_analytics\manager::update_default_models_for_component('moodle');
1833 message_update_providers('moodle');
1834 \core\message\inbound\manager::update_handlers_for_component('moodle');
1835 core_tag_area::reset_definitions_for_component('moodle');
1836 // Update core definitions.
1837 cache_helper::update_definitions(true);
1839 // Purge caches again, just to be sure we arn't holding onto old stuff now.
1840 cache_helper::purge_all(true);
1841 purge_all_caches();
1843 // Clean up contexts - more and more stuff depends on existence of paths and contexts
1844 context_helper::cleanup_instances();
1845 context_helper::create_instances(null, false);
1846 context_helper::build_all_paths(false);
1847 $syscontext = context_system::instance();
1848 $syscontext->mark_dirty();
1850 print_upgrade_part_end('moodle', false, $verbose);
1851 } catch (Exception $ex) {
1852 upgrade_handle_exception($ex);
1853 } catch (Throwable $ex) {
1854 // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
1855 upgrade_handle_exception($ex);
1860 * Upgrade/install other parts of moodle
1861 * @param bool $verbose
1862 * @return void, may throw exception
1864 function upgrade_noncore($verbose) {
1865 global $CFG;
1867 raise_memory_limit(MEMORY_EXTRA);
1869 // upgrade all plugins types
1870 try {
1871 // Reset caches before any output.
1872 cache_helper::purge_all(true);
1873 purge_all_caches();
1875 $plugintypes = core_component::get_plugin_types();
1876 foreach ($plugintypes as $type=>$location) {
1877 upgrade_plugins($type, 'print_upgrade_part_start', 'print_upgrade_part_end', $verbose);
1879 // Upgrade services.
1880 // This function gives plugins and subsystems a chance to add functions to existing built-in services.
1881 external_update_services();
1883 // Update cache definitions. Involves scanning each plugin for any changes.
1884 cache_helper::update_definitions();
1885 // Mark the site as upgraded.
1886 set_config('allversionshash', core_component::get_all_versions_hash());
1888 // Purge caches again, just to be sure we arn't holding onto old stuff now.
1889 cache_helper::purge_all(true);
1890 purge_all_caches();
1892 } catch (Exception $ex) {
1893 upgrade_handle_exception($ex);
1894 } catch (Throwable $ex) {
1895 // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
1896 upgrade_handle_exception($ex);
1901 * Checks if the main tables have been installed yet or not.
1903 * Note: we can not use caches here because they might be stale,
1904 * use with care!
1906 * @return bool
1908 function core_tables_exist() {
1909 global $DB;
1911 if (!$tables = $DB->get_tables(false) ) { // No tables yet at all.
1912 return false;
1914 } else { // Check for missing main tables
1915 $mtables = array('config', 'course', 'groupings'); // some tables used in 1.9 and 2.0, preferable something from the start and end of install.xml
1916 foreach ($mtables as $mtable) {
1917 if (!in_array($mtable, $tables)) {
1918 return false;
1921 return true;
1926 * upgrades the mnet rpc definitions for the given component.
1927 * this method doesn't return status, an exception will be thrown in the case of an error
1929 * @param string $component the plugin to upgrade, eg auth_mnet
1931 function upgrade_plugin_mnet_functions($component) {
1932 global $DB, $CFG;
1934 list($type, $plugin) = core_component::normalize_component($component);
1935 $path = core_component::get_plugin_directory($type, $plugin);
1937 $publishes = array();
1938 $subscribes = array();
1939 if (file_exists($path . '/db/mnet.php')) {
1940 require_once($path . '/db/mnet.php'); // $publishes comes from this file
1942 if (empty($publishes)) {
1943 $publishes = array(); // still need this to be able to disable stuff later
1945 if (empty($subscribes)) {
1946 $subscribes = array(); // still need this to be able to disable stuff later
1949 static $servicecache = array();
1951 // rekey an array based on the rpc method for easy lookups later
1952 $publishmethodservices = array();
1953 $subscribemethodservices = array();
1954 foreach($publishes as $servicename => $service) {
1955 if (is_array($service['methods'])) {
1956 foreach($service['methods'] as $methodname) {
1957 $service['servicename'] = $servicename;
1958 $publishmethodservices[$methodname][] = $service;
1963 // Disable functions that don't exist (any more) in the source
1964 // Should these be deleted? What about their permissions records?
1965 foreach ($DB->get_records('mnet_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
1966 if (!array_key_exists($rpc->functionname, $publishmethodservices) && $rpc->enabled) {
1967 $DB->set_field('mnet_rpc', 'enabled', 0, array('id' => $rpc->id));
1968 } else if (array_key_exists($rpc->functionname, $publishmethodservices) && !$rpc->enabled) {
1969 $DB->set_field('mnet_rpc', 'enabled', 1, array('id' => $rpc->id));
1973 // reflect all the services we're publishing and save them
1974 static $cachedclasses = array(); // to store reflection information in
1975 foreach ($publishes as $service => $data) {
1976 $f = $data['filename'];
1977 $c = $data['classname'];
1978 foreach ($data['methods'] as $method) {
1979 $dataobject = new stdClass();
1980 $dataobject->plugintype = $type;
1981 $dataobject->pluginname = $plugin;
1982 $dataobject->enabled = 1;
1983 $dataobject->classname = $c;
1984 $dataobject->filename = $f;
1986 if (is_string($method)) {
1987 $dataobject->functionname = $method;
1989 } else if (is_array($method)) { // wants to override file or class
1990 $dataobject->functionname = $method['method'];
1991 $dataobject->classname = $method['classname'];
1992 $dataobject->filename = $method['filename'];
1994 $dataobject->xmlrpcpath = $type.'/'.$plugin.'/'.$dataobject->filename.'/'.$method;
1995 $dataobject->static = false;
1997 require_once($path . '/' . $dataobject->filename);
1998 $functionreflect = null; // slightly different ways to get this depending on whether it's a class method or a function
1999 if (!empty($dataobject->classname)) {
2000 if (!class_exists($dataobject->classname)) {
2001 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
2003 $key = $dataobject->filename . '|' . $dataobject->classname;
2004 if (!array_key_exists($key, $cachedclasses)) { // look to see if we've already got a reflection object
2005 try {
2006 $cachedclasses[$key] = new ReflectionClass($dataobject->classname);
2007 } catch (ReflectionException $e) { // catch these and rethrow them to something more helpful
2008 throw new moodle_exception('installreflectionclasserror', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname, 'error' => $e->getMessage()));
2011 $r =& $cachedclasses[$key];
2012 if (!$r->hasMethod($dataobject->functionname)) {
2013 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
2015 $functionreflect = $r->getMethod($dataobject->functionname);
2016 $dataobject->static = (int)$functionreflect->isStatic();
2017 } else {
2018 if (!function_exists($dataobject->functionname)) {
2019 throw new moodle_exception('installnosuchfunction', 'mnet', '', (object)array('method' => $dataobject->functionname, 'file' => $dataobject->filename));
2021 try {
2022 $functionreflect = new ReflectionFunction($dataobject->functionname);
2023 } catch (ReflectionException $e) { // catch these and rethrow them to something more helpful
2024 throw new moodle_exception('installreflectionfunctionerror', 'mnet', '', (object)array('method' => $dataobject->functionname, '' => $dataobject->filename, 'error' => $e->getMessage()));
2027 $dataobject->profile = serialize(admin_mnet_method_profile($functionreflect));
2028 $dataobject->help = admin_mnet_method_get_help($functionreflect);
2030 if ($record_exists = $DB->get_record('mnet_rpc', array('xmlrpcpath'=>$dataobject->xmlrpcpath))) {
2031 $dataobject->id = $record_exists->id;
2032 $dataobject->enabled = $record_exists->enabled;
2033 $DB->update_record('mnet_rpc', $dataobject);
2034 } else {
2035 $dataobject->id = $DB->insert_record('mnet_rpc', $dataobject, true);
2038 // TODO this API versioning must be reworked, here the recently processed method
2039 // sets the service API which may not be correct
2040 foreach ($publishmethodservices[$dataobject->functionname] as $service) {
2041 if ($serviceobj = $DB->get_record('mnet_service', array('name'=>$service['servicename']))) {
2042 $serviceobj->apiversion = $service['apiversion'];
2043 $DB->update_record('mnet_service', $serviceobj);
2044 } else {
2045 $serviceobj = new stdClass();
2046 $serviceobj->name = $service['servicename'];
2047 $serviceobj->description = empty($service['description']) ? '' : $service['description'];
2048 $serviceobj->apiversion = $service['apiversion'];
2049 $serviceobj->offer = 1;
2050 $serviceobj->id = $DB->insert_record('mnet_service', $serviceobj);
2052 $servicecache[$service['servicename']] = $serviceobj;
2053 if (!$DB->record_exists('mnet_service2rpc', array('rpcid'=>$dataobject->id, 'serviceid'=>$serviceobj->id))) {
2054 $obj = new stdClass();
2055 $obj->rpcid = $dataobject->id;
2056 $obj->serviceid = $serviceobj->id;
2057 $DB->insert_record('mnet_service2rpc', $obj, true);
2062 // finished with methods we publish, now do subscribable methods
2063 foreach($subscribes as $service => $methods) {
2064 if (!array_key_exists($service, $servicecache)) {
2065 if (!$serviceobj = $DB->get_record('mnet_service', array('name' => $service))) {
2066 debugging("TODO: skipping unknown service $service - somebody needs to fix MDL-21993");
2067 continue;
2069 $servicecache[$service] = $serviceobj;
2070 } else {
2071 $serviceobj = $servicecache[$service];
2073 foreach ($methods as $method => $xmlrpcpath) {
2074 if (!$rpcid = $DB->get_field('mnet_remote_rpc', 'id', array('xmlrpcpath'=>$xmlrpcpath))) {
2075 $remoterpc = (object)array(
2076 'functionname' => $method,
2077 'xmlrpcpath' => $xmlrpcpath,
2078 'plugintype' => $type,
2079 'pluginname' => $plugin,
2080 'enabled' => 1,
2082 $rpcid = $remoterpc->id = $DB->insert_record('mnet_remote_rpc', $remoterpc, true);
2084 if (!$DB->record_exists('mnet_remote_service2rpc', array('rpcid'=>$rpcid, 'serviceid'=>$serviceobj->id))) {
2085 $obj = new stdClass();
2086 $obj->rpcid = $rpcid;
2087 $obj->serviceid = $serviceobj->id;
2088 $DB->insert_record('mnet_remote_service2rpc', $obj, true);
2090 $subscribemethodservices[$method][] = $service;
2094 foreach ($DB->get_records('mnet_remote_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
2095 if (!array_key_exists($rpc->functionname, $subscribemethodservices) && $rpc->enabled) {
2096 $DB->set_field('mnet_remote_rpc', 'enabled', 0, array('id' => $rpc->id));
2097 } else if (array_key_exists($rpc->functionname, $subscribemethodservices) && !$rpc->enabled) {
2098 $DB->set_field('mnet_remote_rpc', 'enabled', 1, array('id' => $rpc->id));
2102 return true;
2106 * Given some sort of reflection function/method object, return a profile array, ready to be serialized and stored
2108 * @param ReflectionFunctionAbstract $function reflection function/method object from which to extract information
2110 * @return array associative array with function/method information
2112 function admin_mnet_method_profile(ReflectionFunctionAbstract $function) {
2113 $commentlines = admin_mnet_method_get_docblock($function);
2114 $getkey = function($key) use ($commentlines) {
2115 return array_values(array_filter($commentlines, function($line) use ($key) {
2116 return $line[0] == $key;
2117 }));
2119 $returnline = $getkey('@return');
2120 return array (
2121 'parameters' => array_map(function($line) {
2122 return array(
2123 'name' => trim($line[2], " \t\n\r\0\x0B$"),
2124 'type' => $line[1],
2125 'description' => $line[3]
2127 }, $getkey('@param')),
2129 'return' => array(
2130 'type' => !empty($returnline[0][1]) ? $returnline[0][1] : 'void',
2131 'description' => !empty($returnline[0][2]) ? $returnline[0][2] : ''
2137 * Given some sort of reflection function/method object, return an array of docblock lines, where each line is an array of
2138 * keywords/descriptions
2140 * @param ReflectionFunctionAbstract $function reflection function/method object from which to extract information
2142 * @return array docblock converted in to an array
2144 function admin_mnet_method_get_docblock(ReflectionFunctionAbstract $function) {
2145 return array_map(function($line) {
2146 $text = trim($line, " \t\n\r\0\x0B*/");
2147 if (strpos($text, '@param') === 0) {
2148 return preg_split('/\s+/', $text, 4);
2151 if (strpos($text, '@return') === 0) {
2152 return preg_split('/\s+/', $text, 3);
2155 return array($text);
2156 }, explode("\n", $function->getDocComment()));
2160 * Given some sort of reflection function/method object, return just the help text
2162 * @param ReflectionFunctionAbstract $function reflection function/method object from which to extract information
2164 * @return string docblock help text
2166 function admin_mnet_method_get_help(ReflectionFunctionAbstract $function) {
2167 $helplines = array_map(function($line) {
2168 return implode(' ', $line);
2169 }, array_values(array_filter(admin_mnet_method_get_docblock($function), function($line) {
2170 return strpos($line[0], '@') !== 0 && !empty($line[0]);
2171 })));
2173 return implode("\n", $helplines);
2177 * This function verifies that the database is not using an unsupported storage engine.
2179 * @param environment_results $result object to update, if relevant
2180 * @return environment_results|null updated results object, or null if the storage engine is supported
2182 function check_database_storage_engine(environment_results $result) {
2183 global $DB;
2185 // Check if MySQL is the DB family (this will also be the same for MariaDB).
2186 if ($DB->get_dbfamily() == 'mysql') {
2187 // Get the database engine we will either be using to install the tables, or what we are currently using.
2188 $engine = $DB->get_dbengine();
2189 // Check if MyISAM is the storage engine that will be used, if so, do not proceed and display an error.
2190 if ($engine == 'MyISAM') {
2191 $result->setInfo('unsupported_db_storage_engine');
2192 $result->setStatus(false);
2193 return $result;
2197 return null;
2201 * Method used to check the usage of slasharguments config and display a warning message.
2203 * @param environment_results $result object to update, if relevant.
2204 * @return environment_results|null updated results or null if slasharguments is disabled.
2206 function check_slasharguments(environment_results $result){
2207 global $CFG;
2209 if (!during_initial_install() && empty($CFG->slasharguments)) {
2210 $result->setInfo('slasharguments');
2211 $result->setStatus(false);
2212 return $result;
2215 return null;
2219 * This function verifies if the database has tables using innoDB Antelope row format.
2221 * @param environment_results $result
2222 * @return environment_results|null updated results object, or null if no Antelope table has been found.
2224 function check_database_tables_row_format(environment_results $result) {
2225 global $DB;
2227 if ($DB->get_dbfamily() == 'mysql') {
2228 $generator = $DB->get_manager()->generator;
2230 foreach ($DB->get_tables(false) as $table) {
2231 $columns = $DB->get_columns($table, false);
2232 $size = $generator->guess_antelope_row_size($columns);
2233 $format = $DB->get_row_format($table);
2235 if ($size <= $generator::ANTELOPE_MAX_ROW_SIZE) {
2236 continue;
2239 if ($format === 'Compact' or $format === 'Redundant') {
2240 $result->setInfo('unsupported_db_table_row_format');
2241 $result->setStatus(false);
2242 return $result;
2247 return null;
2251 * This function verfies that the database has tables using InnoDB Antelope row format.
2253 * @param environment_results $result
2254 * @return environment_results|null updated results object, or null if no Antelope table has been found.
2256 function check_mysql_file_format(environment_results $result) {
2257 global $DB;
2259 if ($DB->get_dbfamily() == 'mysql') {
2260 $collation = $DB->get_dbcollation();
2261 $collationinfo = explode('_', $collation);
2262 $charset = reset($collationinfo);
2264 if ($charset == 'utf8mb4') {
2265 if ($DB->get_row_format() !== "Barracuda") {
2266 $result->setInfo('mysql_full_unicode_support#File_format');
2267 $result->setStatus(false);
2268 return $result;
2272 return null;
2276 * This function verfies that the database has a setting of one file per table. This is required for 'utf8mb4'.
2278 * @param environment_results $result
2279 * @return environment_results|null updated results object, or null if innodb_file_per_table = 1.
2281 function check_mysql_file_per_table(environment_results $result) {
2282 global $DB;
2284 if ($DB->get_dbfamily() == 'mysql') {
2285 $collation = $DB->get_dbcollation();
2286 $collationinfo = explode('_', $collation);
2287 $charset = reset($collationinfo);
2289 if ($charset == 'utf8mb4') {
2290 if (!$DB->is_file_per_table_enabled()) {
2291 $result->setInfo('mysql_full_unicode_support#File_per_table');
2292 $result->setStatus(false);
2293 return $result;
2297 return null;
2301 * This function verfies that the database has the setting of large prefix enabled. This is required for 'utf8mb4'.
2303 * @param environment_results $result
2304 * @return environment_results|null updated results object, or null if innodb_large_prefix = 1.
2306 function check_mysql_large_prefix(environment_results $result) {
2307 global $DB;
2309 if ($DB->get_dbfamily() == 'mysql') {
2310 $collation = $DB->get_dbcollation();
2311 $collationinfo = explode('_', $collation);
2312 $charset = reset($collationinfo);
2314 if ($charset == 'utf8mb4') {
2315 if (!$DB->is_large_prefix_enabled()) {
2316 $result->setInfo('mysql_full_unicode_support#Large_prefix');
2317 $result->setStatus(false);
2318 return $result;
2322 return null;
2326 * This function checks the database to see if it is using incomplete unicode support.
2328 * @param environment_results $result $result
2329 * @return environment_results|null updated results object, or null if unicode is fully supported.
2331 function check_mysql_incomplete_unicode_support(environment_results $result) {
2332 global $DB;
2334 if ($DB->get_dbfamily() == 'mysql') {
2335 $collation = $DB->get_dbcollation();
2336 $collationinfo = explode('_', $collation);
2337 $charset = reset($collationinfo);
2339 if ($charset == 'utf8') {
2340 $result->setInfo('mysql_full_unicode_support');
2341 $result->setStatus(false);
2342 return $result;
2345 return null;
2349 * Check if the site is being served using an ssl url.
2351 * Note this does not really perform any request neither looks for proxies or
2352 * other situations. Just looks to wwwroot and warn if it's not using https.
2354 * @param environment_results $result $result
2355 * @return environment_results|null updated results object, or null if the site is https.
2357 function check_is_https(environment_results $result) {
2358 global $CFG;
2360 // Only if is defined, non-empty and whatever core tell us.
2361 if (!empty($CFG->wwwroot) && !is_https()) {
2362 $result->setInfo('site not https');
2363 $result->setStatus(false);
2364 return $result;
2366 return null;
2370 * Check if the site is using 64 bits PHP.
2372 * @param environment_results $result
2373 * @return environment_results|null updated results object, or null if the site is using 64 bits PHP.
2375 function check_sixtyfour_bits(environment_results $result) {
2377 if (PHP_INT_SIZE === 4) {
2378 $result->setInfo('php not 64 bits');
2379 $result->setStatus(false);
2380 return $result;
2382 return null;
2386 * Assert the upgrade key is provided, if it is defined.
2388 * The upgrade key can be defined in the main config.php as $CFG->upgradekey. If
2389 * it is defined there, then its value must be provided every time the site is
2390 * being upgraded, regardless the administrator is logged in or not.
2392 * This is supposed to be used at certain places in /admin/index.php only.
2394 * @param string|null $upgradekeyhash the SHA-1 of the value provided by the user
2396 function check_upgrade_key($upgradekeyhash) {
2397 global $CFG, $PAGE;
2399 if (isset($CFG->config_php_settings['upgradekey'])) {
2400 if ($upgradekeyhash === null or $upgradekeyhash !== sha1($CFG->config_php_settings['upgradekey'])) {
2401 if (!$PAGE->headerprinted) {
2402 $output = $PAGE->get_renderer('core', 'admin');
2403 echo $output->upgradekey_form_page(new moodle_url('/admin/index.php', array('cache' => 0)));
2404 die();
2405 } else {
2406 // This should not happen.
2407 die('Upgrade locked');
2414 * Helper procedure/macro for installing remote plugins at admin/index.php
2416 * Does not return, always redirects or exits.
2418 * @param array $installable list of \core\update\remote_info
2419 * @param bool $confirmed false: display the validation screen, true: proceed installation
2420 * @param string $heading validation screen heading
2421 * @param moodle_url|string|null $continue URL to proceed with installation at the validation screen
2422 * @param moodle_url|string|null $return URL to go back on cancelling at the validation screen
2424 function upgrade_install_plugins(array $installable, $confirmed, $heading='', $continue=null, $return=null) {
2425 global $CFG, $PAGE;
2427 if (empty($return)) {
2428 $return = $PAGE->url;
2431 if (!empty($CFG->disableupdateautodeploy)) {
2432 redirect($return);
2435 if (empty($installable)) {
2436 redirect($return);
2439 $pluginman = core_plugin_manager::instance();
2441 if ($confirmed) {
2442 // Installation confirmed at the validation results page.
2443 if (!$pluginman->install_plugins($installable, true, true)) {
2444 throw new moodle_exception('install_plugins_failed', 'core_plugin', $return);
2447 // Always redirect to admin/index.php to perform the database upgrade.
2448 // Do not throw away the existing $PAGE->url parameters such as
2449 // confirmupgrade or confirmrelease if $PAGE->url is a superset of the
2450 // URL we must go to.
2451 $mustgoto = new moodle_url('/admin/index.php', array('cache' => 0, 'confirmplugincheck' => 0));
2452 if ($mustgoto->compare($PAGE->url, URL_MATCH_PARAMS)) {
2453 redirect($PAGE->url);
2454 } else {
2455 redirect($mustgoto);
2458 } else {
2459 $output = $PAGE->get_renderer('core', 'admin');
2460 echo $output->header();
2461 if ($heading) {
2462 echo $output->heading($heading, 3);
2464 echo html_writer::start_tag('pre', array('class' => 'plugin-install-console'));
2465 $validated = $pluginman->install_plugins($installable, false, false);
2466 echo html_writer::end_tag('pre');
2467 if ($validated) {
2468 echo $output->plugins_management_confirm_buttons($continue, $return);
2469 } else {
2470 echo $output->plugins_management_confirm_buttons(null, $return);
2472 echo $output->footer();
2473 die();
2477 * Method used to check the installed unoconv version.
2479 * @param environment_results $result object to update, if relevant.
2480 * @return environment_results|null updated results or null if unoconv path is not executable.
2482 function check_unoconv_version(environment_results $result) {
2483 global $CFG;
2485 if (!during_initial_install() && !empty($CFG->pathtounoconv) && file_is_executable(trim($CFG->pathtounoconv))) {
2486 $currentversion = 0;
2487 $supportedversion = 0.7;
2488 $unoconvbin = \escapeshellarg($CFG->pathtounoconv);
2489 $command = "$unoconvbin --version";
2490 exec($command, $output);
2492 // If the command execution returned some output, then get the unoconv version.
2493 if ($output) {
2494 foreach ($output as $response) {
2495 if (preg_match('/unoconv (\\d+\\.\\d+)/', $response, $matches)) {
2496 $currentversion = (float)$matches[1];
2501 if ($currentversion < $supportedversion) {
2502 $result->setInfo('unoconv version not supported');
2503 $result->setStatus(false);
2504 return $result;
2507 return null;
2511 * Checks for up-to-date TLS libraries. NOTE: this is not currently used, see MDL-57262.
2513 * @param environment_results $result object to update, if relevant.
2514 * @return environment_results|null updated results or null if unoconv path is not executable.
2516 function check_tls_libraries(environment_results $result) {
2517 global $CFG;
2519 if (!function_exists('curl_version')) {
2520 $result->setInfo('cURL PHP extension is not installed');
2521 $result->setStatus(false);
2522 return $result;
2525 if (!\core\upgrade\util::validate_php_curl_tls(curl_version(), PHP_ZTS)) {
2526 $result->setInfo('invalid ssl/tls configuration');
2527 $result->setStatus(false);
2528 return $result;
2531 if (!\core\upgrade\util::can_use_tls12(curl_version(), php_uname('r'))) {
2532 $result->setInfo('ssl/tls configuration not supported');
2533 $result->setStatus(false);
2534 return $result;
2537 return null;
2541 * Check if recommended version of libcurl is installed or not.
2543 * @param environment_results $result object to update, if relevant.
2544 * @return environment_results|null updated results or null.
2546 function check_libcurl_version(environment_results $result) {
2548 if (!function_exists('curl_version')) {
2549 $result->setInfo('cURL PHP extension is not installed');
2550 $result->setStatus(false);
2551 return $result;
2554 // Supported version and version number.
2555 $supportedversion = 0x071304;
2556 $supportedversionstring = "7.19.4";
2558 // Installed version.
2559 $curlinfo = curl_version();
2560 $currentversion = $curlinfo['version_number'];
2562 if ($currentversion < $supportedversion) {
2563 // Test fail.
2564 // Set info, we want to let user know how to resolve the problem.
2565 $result->setInfo('Libcurl version check');
2566 $result->setNeededVersion($supportedversionstring);
2567 $result->setCurrentVersion($curlinfo['version']);
2568 $result->setStatus(false);
2569 return $result;
2572 return null;
2576 * Fix how auth plugins are called in the 'config_plugins' table.
2578 * For legacy reasons, the auth plugins did not always use their frankenstyle
2579 * component name in the 'plugin' column of the 'config_plugins' table. This is
2580 * a helper function to correctly migrate the legacy settings into the expected
2581 * and consistent way.
2583 * @param string $plugin the auth plugin name such as 'cas', 'manual' or 'mnet'
2585 function upgrade_fix_config_auth_plugin_names($plugin) {
2586 global $CFG, $DB, $OUTPUT;
2588 $legacy = (array) get_config('auth/'.$plugin);
2589 $current = (array) get_config('auth_'.$plugin);
2591 // I don't want to rely on array_merge() and friends here just in case
2592 // there was some crazy setting with a numerical name.
2594 if ($legacy) {
2595 $new = $legacy;
2596 } else {
2597 $new = [];
2600 if ($current) {
2601 foreach ($current as $name => $value) {
2602 if (isset($legacy[$name]) && ($legacy[$name] !== $value)) {
2603 // No need to pollute the output during unit tests.
2604 if (!empty($CFG->upgraderunning)) {
2605 $message = get_string('settingmigrationmismatch', 'core_auth', [
2606 'plugin' => 'auth_'.$plugin,
2607 'setting' => s($name),
2608 'legacy' => s($legacy[$name]),
2609 'current' => s($value),
2611 echo $OUTPUT->notification($message, \core\output\notification::NOTIFY_ERROR);
2613 upgrade_log(UPGRADE_LOG_NOTICE, 'auth_'.$plugin, 'Setting values mismatch detected',
2614 'SETTING: '.$name. ' LEGACY: '.$legacy[$name].' CURRENT: '.$value);
2618 $new[$name] = $value;
2622 foreach ($new as $name => $value) {
2623 set_config($name, $value, 'auth_'.$plugin);
2624 unset_config($name, 'auth/'.$plugin);
2629 * Populate the auth plugin settings with defaults if needed.
2631 * As a result of fixing the auth plugins config storage, many settings would
2632 * be falsely reported as new ones by admin/upgradesettings.php. We do not want
2633 * to confuse admins so we try to reduce the bewilderment by pre-populating the
2634 * config_plugins table with default values. This should be done only for
2635 * disabled auth methods. The enabled methods have their settings already
2636 * stored, so reporting actual new settings for them is valid.
2638 * @param string $plugin the auth plugin name such as 'cas', 'manual' or 'mnet'
2640 function upgrade_fix_config_auth_plugin_defaults($plugin) {
2641 global $CFG;
2643 $pluginman = core_plugin_manager::instance();
2644 $enabled = $pluginman->get_enabled_plugins('auth');
2646 if (isset($enabled[$plugin])) {
2647 // Do not touch settings of enabled auth methods.
2648 return;
2651 // We can't directly use {@link core\plugininfo\auth::load_settings()} here
2652 // because the plugins are not fully upgraded yet. Instead, we emulate what
2653 // that method does. We fetch a temporary instance of the plugin's settings
2654 // page to get access to the settings and their defaults. Note we are not
2655 // adding that temporary instance into the admin tree. Yes, this is a hack.
2657 $plugininfo = $pluginman->get_plugin_info('auth_'.$plugin);
2658 $adminroot = admin_get_root();
2659 $ADMIN = $adminroot;
2660 $auth = $plugininfo;
2662 $section = $plugininfo->get_settings_section_name();
2663 $settingspath = $plugininfo->full_path('settings.php');
2665 if (file_exists($settingspath)) {
2666 $settings = new admin_settingpage($section, 'Emulated settings page for auth_'.$plugin, 'moodle/site:config');
2667 include($settingspath);
2669 if ($settings) {
2670 admin_apply_default_settings($settings, false);
2676 * Search for a given theme in any of the parent themes of a given theme.
2678 * @param string $needle The name of the theme you want to search for
2679 * @param string $themename The name of the theme you want to search for
2680 * @param string $checkedthemeforparents The name of all the themes already checked
2681 * @return bool True if found, false if not.
2683 function upgrade_theme_is_from_family($needle, $themename, $checkedthemeforparents = []) {
2684 global $CFG;
2686 // Once we've started checking a theme, don't start checking it again. Prevent recursion.
2687 if (!empty($checkedthemeforparents[$themename])) {
2688 return false;
2690 $checkedthemeforparents[$themename] = true;
2692 if ($themename == $needle) {
2693 return true;
2696 if ($themedir = upgrade_find_theme_location($themename)) {
2697 $THEME = new stdClass();
2698 require($themedir . '/config.php');
2699 $theme = $THEME;
2700 } else {
2701 return false;
2704 if (empty($theme->parents)) {
2705 return false;
2708 // Recursively search through each parent theme.
2709 foreach ($theme->parents as $parent) {
2710 if (upgrade_theme_is_from_family($needle, $parent, $checkedthemeforparents)) {
2711 return true;
2714 return false;
2718 * Finds the theme location and verifies the theme has all needed files.
2720 * @param string $themename The name of the theme you want to search for
2721 * @return string full dir path or null if not found
2722 * @see \theme_config::find_theme_location()
2724 function upgrade_find_theme_location($themename) {
2725 global $CFG;
2727 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
2728 $dir = "$CFG->dirroot/theme/$themename";
2729 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
2730 $dir = "$CFG->themedir/$themename";
2731 } else {
2732 return null;
2735 return $dir;