MDL-74741 javascript: A11y fix for dialogues visible from beginning
[moodle.git] / lib / upgradelib.php
blobb444bbb19a432fad2720efdd493d3349cf90ec3d
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 * Exception thrown when attempting to install a plugin that declares incompatibility with moodle version
90 * @package core
91 * @subpackage upgrade
92 * @copyright 2019 Peter Burnett <peterburnett@catalyst-au.net>
93 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
95 class plugin_incompatible_exception extends moodle_exception {
96 /**
97 * Constructor function for exception
99 * @param \core\plugininfo\base $plugin The plugin causing the exception
100 * @param int $pluginversion The version of the plugin causing the exception
102 public function __construct($plugin, $pluginversion) {
103 global $CFG;
104 $a = new stdClass();
105 $a->pluginname = $plugin;
106 $a->pluginversion = $pluginversion;
107 $a->moodleversion = $CFG->branch;
109 parent::__construct('pluginunsupported', 'error', "$CFG->wwwroot/$CFG->admin/index.php", $a);
114 * @package core
115 * @subpackage upgrade
116 * @copyright 2009 Petr Skoda {@link http://skodak.org}
117 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
119 class plugin_defective_exception extends moodle_exception {
120 function __construct($plugin, $details) {
121 global $CFG;
122 parent::__construct('detectedbrokenplugin', 'error', "$CFG->wwwroot/$CFG->admin/index.php", $plugin, $details);
127 * Misplaced plugin exception.
129 * Note: this should be used only from the upgrade/admin code.
131 * @package core
132 * @subpackage upgrade
133 * @copyright 2009 Petr Skoda {@link http://skodak.org}
134 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
136 class plugin_misplaced_exception extends moodle_exception {
138 * Constructor.
139 * @param string $component the component from version.php
140 * @param string $expected expected directory, null means calculate
141 * @param string $current plugin directory path
143 public function __construct($component, $expected, $current) {
144 global $CFG;
145 if (empty($expected)) {
146 list($type, $plugin) = core_component::normalize_component($component);
147 $plugintypes = core_component::get_plugin_types();
148 if (isset($plugintypes[$type])) {
149 $expected = $plugintypes[$type] . '/' . $plugin;
152 if (strpos($expected, '$CFG->dirroot') !== 0) {
153 $expected = str_replace($CFG->dirroot, '$CFG->dirroot', $expected);
155 if (strpos($current, '$CFG->dirroot') !== 0) {
156 $current = str_replace($CFG->dirroot, '$CFG->dirroot', $current);
158 $a = new stdClass();
159 $a->component = $component;
160 $a->expected = $expected;
161 $a->current = $current;
162 parent::__construct('detectedmisplacedplugin', 'core_plugin', "$CFG->wwwroot/$CFG->admin/index.php", $a);
167 * Static class monitors performance of upgrade steps.
169 class core_upgrade_time {
170 /** @var float Time at start of current upgrade (plugin/system) */
171 protected static $before;
172 /** @var float Time at end of last savepoint */
173 protected static $lastsavepoint;
174 /** @var bool Flag to indicate whether we are recording timestamps or not. */
175 protected static $isrecording = false;
178 * Records current time at the start of the current upgrade item, e.g. plugin.
180 public static function record_start() {
181 self::$before = microtime(true);
182 self::$lastsavepoint = self::$before;
183 self::$isrecording = true;
187 * Records current time at the end of a given numbered step.
189 * @param float $version Version number (may have decimals, or not)
191 public static function record_savepoint($version) {
192 global $CFG, $OUTPUT;
194 // In developer debug mode we show a notification after each individual save point.
195 if ($CFG->debugdeveloper && self::$isrecording) {
196 $time = microtime(true);
198 $notification = new \core\output\notification($version . ': ' .
199 get_string('successduration', '', format_float($time - self::$lastsavepoint, 2)),
200 \core\output\notification::NOTIFY_SUCCESS);
201 $notification->set_show_closebutton(false);
202 echo $OUTPUT->render($notification);
203 self::$lastsavepoint = $time;
208 * Gets the time since the record_start function was called, rounded to 2 digits.
210 * @return float Elapsed time
212 public static function get_elapsed() {
213 return microtime(true) - self::$before;
218 * Sets maximum expected time needed for upgrade task.
219 * Please always make sure that upgrade will not run longer!
221 * The script may be automatically aborted if upgrade times out.
223 * @category upgrade
224 * @param int $max_execution_time in seconds (can not be less than 60 s)
226 function upgrade_set_timeout($max_execution_time=300) {
227 global $CFG;
229 if (!isset($CFG->upgraderunning) or $CFG->upgraderunning < time()) {
230 $upgraderunning = get_config(null, 'upgraderunning');
231 } else {
232 $upgraderunning = $CFG->upgraderunning;
235 if (!$upgraderunning) {
236 if (CLI_SCRIPT) {
237 // never stop CLI upgrades
238 $upgraderunning = 0;
239 } else {
240 // web upgrade not running or aborted
241 print_error('upgradetimedout', 'admin', "$CFG->wwwroot/$CFG->admin/");
245 if ($max_execution_time < 60) {
246 // protection against 0 here
247 $max_execution_time = 60;
250 $expected_end = time() + $max_execution_time;
252 if ($expected_end < $upgraderunning + 10 and $expected_end > $upgraderunning - 10) {
253 // no need to store new end, it is nearly the same ;-)
254 return;
257 if (CLI_SCRIPT) {
258 // there is no point in timing out of CLI scripts, admins can stop them if necessary
259 core_php_time_limit::raise();
260 } else {
261 core_php_time_limit::raise($max_execution_time);
263 set_config('upgraderunning', $expected_end); // keep upgrade locked until this time
267 * Upgrade savepoint, marks end of each upgrade block.
268 * It stores new main version, resets upgrade timeout
269 * and abort upgrade if user cancels page loading.
271 * Please do not make large upgrade blocks with lots of operations,
272 * for example when adding tables keep only one table operation per block.
274 * @category upgrade
275 * @param bool $result false if upgrade step failed, true if completed
276 * @param string or float $version main version
277 * @param bool $allowabort allow user to abort script execution here
278 * @return void
280 function upgrade_main_savepoint($result, $version, $allowabort=true) {
281 global $CFG;
283 //sanity check to avoid confusion with upgrade_mod_savepoint usage.
284 if (!is_bool($allowabort)) {
285 $errormessage = 'Parameter type mismatch. Are you mixing up upgrade_main_savepoint() and upgrade_mod_savepoint()?';
286 throw new coding_exception($errormessage);
289 if (!$result) {
290 throw new upgrade_exception(null, $version);
293 if ($CFG->version >= $version) {
294 // something really wrong is going on in main upgrade script
295 throw new downgrade_exception(null, $CFG->version, $version);
298 set_config('version', $version);
299 upgrade_log(UPGRADE_LOG_NORMAL, null, 'Upgrade savepoint reached');
301 // reset upgrade timeout to default
302 upgrade_set_timeout();
304 core_upgrade_time::record_savepoint($version);
306 // this is a safe place to stop upgrades if user aborts page loading
307 if ($allowabort and connection_aborted()) {
308 die;
313 * Module upgrade savepoint, marks end of module upgrade blocks
314 * It stores module version, resets upgrade timeout
315 * and abort upgrade if user cancels page loading.
317 * @category upgrade
318 * @param bool $result false if upgrade step failed, true if completed
319 * @param string or float $version main version
320 * @param string $modname name of module
321 * @param bool $allowabort allow user to abort script execution here
322 * @return void
324 function upgrade_mod_savepoint($result, $version, $modname, $allowabort=true) {
325 global $DB;
327 $component = 'mod_'.$modname;
329 if (!$result) {
330 throw new upgrade_exception($component, $version);
333 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
335 if (!$module = $DB->get_record('modules', array('name'=>$modname))) {
336 print_error('modulenotexist', 'debug', '', $modname);
339 if ($dbversion >= $version) {
340 // something really wrong is going on in upgrade script
341 throw new downgrade_exception($component, $dbversion, $version);
343 set_config('version', $version, $component);
345 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
347 // reset upgrade timeout to default
348 upgrade_set_timeout();
350 core_upgrade_time::record_savepoint($version);
352 // this is a safe place to stop upgrades if user aborts page loading
353 if ($allowabort and connection_aborted()) {
354 die;
359 * Blocks upgrade savepoint, marks end of blocks upgrade blocks
360 * It stores block version, resets upgrade timeout
361 * and abort upgrade if user cancels page loading.
363 * @category upgrade
364 * @param bool $result false if upgrade step failed, true if completed
365 * @param string or float $version main version
366 * @param string $blockname name of block
367 * @param bool $allowabort allow user to abort script execution here
368 * @return void
370 function upgrade_block_savepoint($result, $version, $blockname, $allowabort=true) {
371 global $DB;
373 $component = 'block_'.$blockname;
375 if (!$result) {
376 throw new upgrade_exception($component, $version);
379 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
381 if (!$block = $DB->get_record('block', array('name'=>$blockname))) {
382 print_error('blocknotexist', 'debug', '', $blockname);
385 if ($dbversion >= $version) {
386 // something really wrong is going on in upgrade script
387 throw new downgrade_exception($component, $dbversion, $version);
389 set_config('version', $version, $component);
391 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
393 // reset upgrade timeout to default
394 upgrade_set_timeout();
396 core_upgrade_time::record_savepoint($version);
398 // this is a safe place to stop upgrades if user aborts page loading
399 if ($allowabort and connection_aborted()) {
400 die;
405 * Plugins upgrade savepoint, marks end of blocks upgrade blocks
406 * It stores plugin version, resets upgrade timeout
407 * and abort upgrade if user cancels page loading.
409 * @category upgrade
410 * @param bool $result false if upgrade step failed, true if completed
411 * @param string or float $version main version
412 * @param string $type The type of the plugin.
413 * @param string $plugin The name of the plugin.
414 * @param bool $allowabort allow user to abort script execution here
415 * @return void
417 function upgrade_plugin_savepoint($result, $version, $type, $plugin, $allowabort=true) {
418 global $DB;
420 $component = $type.'_'.$plugin;
422 if (!$result) {
423 throw new upgrade_exception($component, $version);
426 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
428 if ($dbversion >= $version) {
429 // Something really wrong is going on in the upgrade script
430 throw new downgrade_exception($component, $dbversion, $version);
432 set_config('version', $version, $component);
433 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
435 // Reset upgrade timeout to default
436 upgrade_set_timeout();
438 core_upgrade_time::record_savepoint($version);
440 // This is a safe place to stop upgrades if user aborts page loading
441 if ($allowabort and connection_aborted()) {
442 die;
447 * Detect if there are leftovers in PHP source files.
449 * During main version upgrades administrators MUST move away
450 * old PHP source files and start from scratch (or better
451 * use git).
453 * @return bool true means borked upgrade, false means previous PHP files were properly removed
455 function upgrade_stale_php_files_present() {
456 global $CFG;
458 $someexamplesofremovedfiles = array(
459 // Removed in 3.11.
460 '/customfield/edit.php',
461 '/lib/phpunit/classes/autoloader.php',
462 '/lib/xhprof/README',
463 '/message/defaultoutputs.php',
464 '/user/files_form.php',
465 // Removed in 3.10.
466 '/grade/grading/classes/privacy/gradingform_provider.php',
467 '/lib/coursecatlib.php',
468 '/lib/form/htmleditor.php',
469 '/message/classes/output/messagearea/contact.php',
470 // Removed in 3.9.
471 '/course/classes/output/modchooser_item.php',
472 '/course/yui/build/moodle-course-modchooser/moodle-course-modchooser-min.js',
473 '/course/yui/src/modchooser/js/modchooser.js',
474 '/h5p/classes/autoloader.php',
475 '/lib/adodb/readme.txt',
476 '/lib/maxmind/GeoIp2/Compat/JsonSerializable.php',
477 // Removed in 3.8.
478 '/lib/amd/src/modal_confirm.js',
479 '/lib/fonts/font-awesome-4.7.0/css/font-awesome.css',
480 '/lib/jquery/jquery-3.2.1.min.js',
481 '/lib/recaptchalib.php',
482 '/lib/sessionkeepalive_ajax.php',
483 '/lib/yui/src/checknet/js/checknet.js',
484 '/question/amd/src/qbankmanager.js',
485 // Removed in 3.7.
486 '/lib/form/yui/src/showadvanced/js/showadvanced.js',
487 '/lib/tests/output_external_test.php',
488 '/message/amd/src/message_area.js',
489 '/message/templates/message_area.mustache',
490 '/question/yui/src/qbankmanager/build.json',
491 // Removed in 3.6.
492 '/lib/classes/session/memcache.php',
493 '/lib/eventslib.php',
494 '/lib/form/submitlink.php',
495 '/lib/medialib.php',
496 '/lib/password_compat/lib/password.php',
497 // Removed in 3.5.
498 '/lib/dml/mssql_native_moodle_database.php',
499 '/lib/dml/mssql_native_moodle_recordset.php',
500 '/lib/dml/mssql_native_moodle_temptables.php',
501 // Removed in 3.4.
502 '/auth/README.txt',
503 '/calendar/set.php',
504 '/enrol/users.php',
505 '/enrol/yui/rolemanager/assets/skins/sam/rolemanager.css',
506 // Removed in 3.3.
507 '/badges/backpackconnect.php',
508 '/calendar/yui/src/info/assets/skins/sam/moodle-calendar-info.css',
509 '/competency/classes/external/exporter.php',
510 '/mod/forum/forum.js',
511 '/user/pixgroup.php',
512 // Removed in 3.2.
513 '/calendar/preferences.php',
514 '/lib/alfresco/',
515 '/lib/jquery/jquery-1.12.1.min.js',
516 '/lib/password_compat/tests/',
517 '/lib/phpunit/classes/unittestcase.php',
518 // Removed in 3.1.
519 '/lib/classes/log/sql_internal_reader.php',
520 '/lib/zend/',
521 '/mod/forum/pix/icon.gif',
522 '/tag/templates/tagname.mustache',
523 // Removed in 3.0.
524 '/mod/lti/grade.php',
525 '/tag/coursetagslib.php',
526 // Removed in 2.9.
527 '/lib/timezone.txt',
528 // Removed in 2.8.
529 '/course/delete_category_form.php',
530 // Removed in 2.7.
531 '/admin/tool/qeupgradehelper/version.php',
532 // Removed in 2.6.
533 '/admin/block.php',
534 '/admin/oacleanup.php',
535 // Removed in 2.5.
536 '/backup/lib.php',
537 '/backup/bb/README.txt',
538 '/lib/excel/test.php',
539 // Removed in 2.4.
540 '/admin/tool/unittest/simpletestlib.php',
541 // Removed in 2.3.
542 '/lib/minify/builder/',
543 // Removed in 2.2.
544 '/lib/yui/3.4.1pr1/',
545 // Removed in 2.2.
546 '/search/cron_php5.php',
547 '/course/report/log/indexlive.php',
548 '/admin/report/backups/index.php',
549 '/admin/generator.php',
550 // Removed in 2.1.
551 '/lib/yui/2.8.0r4/',
552 // Removed in 2.0.
553 '/blocks/admin/block_admin.php',
554 '/blocks/admin_tree/block_admin_tree.php',
557 foreach ($someexamplesofremovedfiles as $file) {
558 if (file_exists($CFG->dirroot.$file)) {
559 return true;
563 return false;
567 * Upgrade plugins
568 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
569 * return void
571 function upgrade_plugins($type, $startcallback, $endcallback, $verbose) {
572 global $CFG, $DB;
574 /// special cases
575 if ($type === 'mod') {
576 return upgrade_plugins_modules($startcallback, $endcallback, $verbose);
577 } else if ($type === 'block') {
578 return upgrade_plugins_blocks($startcallback, $endcallback, $verbose);
581 $plugs = core_component::get_plugin_list($type);
583 foreach ($plugs as $plug=>$fullplug) {
584 // Reset time so that it works when installing a large number of plugins
585 core_php_time_limit::raise(600);
586 $component = clean_param($type.'_'.$plug, PARAM_COMPONENT); // standardised plugin name
588 // check plugin dir is valid name
589 if (empty($component)) {
590 throw new plugin_defective_exception($type.'_'.$plug, 'Invalid plugin directory name.');
593 if (!is_readable($fullplug.'/version.php')) {
594 continue;
597 $plugin = new stdClass();
598 $plugin->version = null;
599 $module = $plugin; // Prevent some notices when plugin placed in wrong directory.
600 require($fullplug.'/version.php'); // defines $plugin with version etc
601 unset($module);
603 if (empty($plugin->version)) {
604 throw new plugin_defective_exception($component, 'Missing $plugin->version number in version.php.');
607 if (empty($plugin->component)) {
608 throw new plugin_defective_exception($component, 'Missing $plugin->component declaration in version.php.');
611 if ($plugin->component !== $component) {
612 throw new plugin_misplaced_exception($plugin->component, null, $fullplug);
615 $plugin->name = $plug;
616 $plugin->fullname = $component;
618 if (!empty($plugin->requires)) {
619 if ($plugin->requires > $CFG->version) {
620 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
621 } else if ($plugin->requires < 2010000000) {
622 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
626 // Throw exception if plugin is incompatible with moodle version.
627 if (!empty($plugin->incompatible)) {
628 if ($CFG->branch >= $plugin->incompatible) {
629 throw new plugin_incompatible_exception($component, $plugin->version);
633 // try to recover from interrupted install.php if needed
634 if (file_exists($fullplug.'/db/install.php')) {
635 if (get_config($plugin->fullname, 'installrunning')) {
636 require_once($fullplug.'/db/install.php');
637 $recover_install_function = 'xmldb_'.$plugin->fullname.'_install_recovery';
638 if (function_exists($recover_install_function)) {
639 $startcallback($component, true, $verbose);
640 $recover_install_function();
641 unset_config('installrunning', $plugin->fullname);
642 update_capabilities($component);
643 log_update_descriptions($component);
644 external_update_descriptions($component);
645 \core\task\manager::reset_scheduled_tasks_for_component($component);
646 \core_analytics\manager::update_default_models_for_component($component);
647 message_update_providers($component);
648 \core\message\inbound\manager::update_handlers_for_component($component);
649 if ($type === 'message') {
650 message_update_processors($plug);
652 upgrade_plugin_mnet_functions($component);
653 core_tag_area::reset_definitions_for_component($component);
654 $endcallback($component, true, $verbose);
659 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
660 if (empty($installedversion)) { // new installation
661 $startcallback($component, true, $verbose);
663 /// Install tables if defined
664 if (file_exists($fullplug.'/db/install.xml')) {
665 $DB->get_manager()->install_from_xmldb_file($fullplug.'/db/install.xml');
668 /// store version
669 upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false);
671 /// execute post install file
672 if (file_exists($fullplug.'/db/install.php')) {
673 require_once($fullplug.'/db/install.php');
674 set_config('installrunning', 1, $plugin->fullname);
675 $post_install_function = 'xmldb_'.$plugin->fullname.'_install';
676 $post_install_function();
677 unset_config('installrunning', $plugin->fullname);
680 /// Install various components
681 update_capabilities($component);
682 log_update_descriptions($component);
683 external_update_descriptions($component);
684 \core\task\manager::reset_scheduled_tasks_for_component($component);
685 \core_analytics\manager::update_default_models_for_component($component);
686 message_update_providers($component);
687 \core\message\inbound\manager::update_handlers_for_component($component);
688 if ($type === 'message') {
689 message_update_processors($plug);
691 upgrade_plugin_mnet_functions($component);
692 core_tag_area::reset_definitions_for_component($component);
693 $endcallback($component, true, $verbose);
695 } else if ($installedversion < $plugin->version) { // upgrade
696 /// Run the upgrade function for the plugin.
697 $startcallback($component, false, $verbose);
699 if (is_readable($fullplug.'/db/upgrade.php')) {
700 require_once($fullplug.'/db/upgrade.php'); // defines upgrading function
702 $newupgrade_function = 'xmldb_'.$plugin->fullname.'_upgrade';
703 $result = $newupgrade_function($installedversion);
704 } else {
705 $result = true;
708 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
709 if ($installedversion < $plugin->version) {
710 // store version if not already there
711 upgrade_plugin_savepoint($result, $plugin->version, $type, $plug, false);
714 /// Upgrade various components
715 update_capabilities($component);
716 log_update_descriptions($component);
717 external_update_descriptions($component);
718 \core\task\manager::reset_scheduled_tasks_for_component($component);
719 \core_analytics\manager::update_default_models_for_component($component);
720 message_update_providers($component);
721 \core\message\inbound\manager::update_handlers_for_component($component);
722 if ($type === 'message') {
723 // Ugly hack!
724 message_update_processors($plug);
726 upgrade_plugin_mnet_functions($component);
727 core_tag_area::reset_definitions_for_component($component);
728 $endcallback($component, false, $verbose);
730 } else if ($installedversion > $plugin->version) {
731 throw new downgrade_exception($component, $installedversion, $plugin->version);
737 * Find and check all modules and load them up or upgrade them if necessary
739 * @global object
740 * @global object
742 function upgrade_plugins_modules($startcallback, $endcallback, $verbose) {
743 global $CFG, $DB;
745 $mods = core_component::get_plugin_list('mod');
747 foreach ($mods as $mod=>$fullmod) {
749 if ($mod === 'NEWMODULE') { // Someone has unzipped the template, ignore it
750 continue;
753 $component = clean_param('mod_'.$mod, PARAM_COMPONENT);
755 // check module dir is valid name
756 if (empty($component)) {
757 throw new plugin_defective_exception('mod_'.$mod, 'Invalid plugin directory name.');
760 if (!is_readable($fullmod.'/version.php')) {
761 throw new plugin_defective_exception($component, 'Missing version.php');
764 $module = new stdClass();
765 $plugin = new stdClass();
766 $plugin->version = null;
767 require($fullmod .'/version.php'); // Defines $plugin with version etc.
769 // Check if the legacy $module syntax is still used.
770 if (!is_object($module) or (count((array)$module) > 0)) {
771 throw new plugin_defective_exception($component, 'Unsupported $module syntax detected in version.php');
774 // Prepare the record for the {modules} table.
775 $module = clone($plugin);
776 unset($module->version);
777 unset($module->component);
778 unset($module->dependencies);
779 unset($module->release);
781 if (empty($plugin->version)) {
782 throw new plugin_defective_exception($component, 'Missing $plugin->version number in version.php.');
785 if (empty($plugin->component)) {
786 throw new plugin_defective_exception($component, 'Missing $plugin->component declaration in version.php.');
789 if ($plugin->component !== $component) {
790 throw new plugin_misplaced_exception($plugin->component, null, $fullmod);
793 if (!empty($plugin->requires)) {
794 if ($plugin->requires > $CFG->version) {
795 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
796 } else if ($plugin->requires < 2010000000) {
797 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
801 if (empty($module->cron)) {
802 $module->cron = 0;
805 // all modules must have en lang pack
806 if (!is_readable("$fullmod/lang/en/$mod.php")) {
807 throw new plugin_defective_exception($component, 'Missing mandatory en language pack.');
810 $module->name = $mod; // The name MUST match the directory
812 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
814 if (file_exists($fullmod.'/db/install.php')) {
815 if (get_config($module->name, 'installrunning')) {
816 require_once($fullmod.'/db/install.php');
817 $recover_install_function = 'xmldb_'.$module->name.'_install_recovery';
818 if (function_exists($recover_install_function)) {
819 $startcallback($component, true, $verbose);
820 $recover_install_function();
821 unset_config('installrunning', $module->name);
822 // Install various components too
823 update_capabilities($component);
824 log_update_descriptions($component);
825 external_update_descriptions($component);
826 \core\task\manager::reset_scheduled_tasks_for_component($component);
827 \core_analytics\manager::update_default_models_for_component($component);
828 message_update_providers($component);
829 \core\message\inbound\manager::update_handlers_for_component($component);
830 upgrade_plugin_mnet_functions($component);
831 core_tag_area::reset_definitions_for_component($component);
832 $endcallback($component, true, $verbose);
837 if (empty($installedversion)) {
838 $startcallback($component, true, $verbose);
840 /// Execute install.xml (XMLDB) - must be present in all modules
841 $DB->get_manager()->install_from_xmldb_file($fullmod.'/db/install.xml');
843 /// Add record into modules table - may be needed in install.php already
844 $module->id = $DB->insert_record('modules', $module);
845 upgrade_mod_savepoint(true, $plugin->version, $module->name, false);
847 /// Post installation hook - optional
848 if (file_exists("$fullmod/db/install.php")) {
849 require_once("$fullmod/db/install.php");
850 // Set installation running flag, we need to recover after exception or error
851 set_config('installrunning', 1, $module->name);
852 $post_install_function = 'xmldb_'.$module->name.'_install';
853 $post_install_function();
854 unset_config('installrunning', $module->name);
857 /// Install various components
858 update_capabilities($component);
859 log_update_descriptions($component);
860 external_update_descriptions($component);
861 \core\task\manager::reset_scheduled_tasks_for_component($component);
862 \core_analytics\manager::update_default_models_for_component($component);
863 message_update_providers($component);
864 \core\message\inbound\manager::update_handlers_for_component($component);
865 upgrade_plugin_mnet_functions($component);
866 core_tag_area::reset_definitions_for_component($component);
868 $endcallback($component, true, $verbose);
870 } else if ($installedversion < $plugin->version) {
871 /// If versions say that we need to upgrade but no upgrade files are available, notify and continue
872 $startcallback($component, false, $verbose);
874 if (is_readable($fullmod.'/db/upgrade.php')) {
875 require_once($fullmod.'/db/upgrade.php'); // defines new upgrading function
876 $newupgrade_function = 'xmldb_'.$module->name.'_upgrade';
877 $result = $newupgrade_function($installedversion, $module);
878 } else {
879 $result = true;
882 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
883 $currmodule = $DB->get_record('modules', array('name'=>$module->name));
884 if ($installedversion < $plugin->version) {
885 // store version if not already there
886 upgrade_mod_savepoint($result, $plugin->version, $mod, false);
889 // update cron flag if needed
890 if ($currmodule->cron != $module->cron) {
891 $DB->set_field('modules', 'cron', $module->cron, array('name' => $module->name));
894 // Upgrade various components
895 update_capabilities($component);
896 log_update_descriptions($component);
897 external_update_descriptions($component);
898 \core\task\manager::reset_scheduled_tasks_for_component($component);
899 \core_analytics\manager::update_default_models_for_component($component);
900 message_update_providers($component);
901 \core\message\inbound\manager::update_handlers_for_component($component);
902 upgrade_plugin_mnet_functions($component);
903 core_tag_area::reset_definitions_for_component($component);
905 $endcallback($component, false, $verbose);
907 } else if ($installedversion > $plugin->version) {
908 throw new downgrade_exception($component, $installedversion, $plugin->version);
915 * This function finds all available blocks and install them
916 * into blocks table or do all the upgrade process if newer.
918 * @global object
919 * @global object
921 function upgrade_plugins_blocks($startcallback, $endcallback, $verbose) {
922 global $CFG, $DB;
924 require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
926 $blocktitles = array(); // we do not want duplicate titles
928 //Is this a first install
929 $first_install = null;
931 $blocks = core_component::get_plugin_list('block');
933 foreach ($blocks as $blockname=>$fullblock) {
935 if (is_null($first_install)) {
936 $first_install = ($DB->count_records('block_instances') == 0);
939 if ($blockname === 'NEWBLOCK') { // Someone has unzipped the template, ignore it
940 continue;
943 $component = clean_param('block_'.$blockname, PARAM_COMPONENT);
945 // check block dir is valid name
946 if (empty($component)) {
947 throw new plugin_defective_exception('block_'.$blockname, 'Invalid plugin directory name.');
950 if (!is_readable($fullblock.'/version.php')) {
951 throw new plugin_defective_exception('block/'.$blockname, 'Missing version.php file.');
953 $plugin = new stdClass();
954 $plugin->version = null;
955 $plugin->cron = 0;
956 $module = $plugin; // Prevent some notices when module placed in wrong directory.
957 include($fullblock.'/version.php');
958 unset($module);
959 $block = clone($plugin);
960 unset($block->version);
961 unset($block->component);
962 unset($block->dependencies);
963 unset($block->release);
965 if (empty($plugin->version)) {
966 throw new plugin_defective_exception($component, 'Missing block version number in version.php.');
969 if (empty($plugin->component)) {
970 throw new plugin_defective_exception($component, 'Missing $plugin->component declaration in version.php.');
973 if ($plugin->component !== $component) {
974 throw new plugin_misplaced_exception($plugin->component, null, $fullblock);
977 if (!empty($plugin->requires)) {
978 if ($plugin->requires > $CFG->version) {
979 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
980 } else if ($plugin->requires < 2010000000) {
981 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
985 if (!is_readable($fullblock.'/block_'.$blockname.'.php')) {
986 throw new plugin_defective_exception('block/'.$blockname, 'Missing main block class file.');
988 include_once($fullblock.'/block_'.$blockname.'.php');
990 $classname = 'block_'.$blockname;
992 if (!class_exists($classname)) {
993 throw new plugin_defective_exception($component, 'Can not load main class.');
996 $blockobj = new $classname; // This is what we'll be testing
997 $blocktitle = $blockobj->get_title();
999 // OK, it's as we all hoped. For further tests, the object will do them itself.
1000 if (!$blockobj->_self_test()) {
1001 throw new plugin_defective_exception($component, 'Self test failed.');
1004 $block->name = $blockname; // The name MUST match the directory
1006 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
1008 if (file_exists($fullblock.'/db/install.php')) {
1009 if (get_config('block_'.$blockname, 'installrunning')) {
1010 require_once($fullblock.'/db/install.php');
1011 $recover_install_function = 'xmldb_block_'.$blockname.'_install_recovery';
1012 if (function_exists($recover_install_function)) {
1013 $startcallback($component, true, $verbose);
1014 $recover_install_function();
1015 unset_config('installrunning', 'block_'.$blockname);
1016 // Install various components
1017 update_capabilities($component);
1018 log_update_descriptions($component);
1019 external_update_descriptions($component);
1020 \core\task\manager::reset_scheduled_tasks_for_component($component);
1021 \core_analytics\manager::update_default_models_for_component($component);
1022 message_update_providers($component);
1023 \core\message\inbound\manager::update_handlers_for_component($component);
1024 upgrade_plugin_mnet_functions($component);
1025 core_tag_area::reset_definitions_for_component($component);
1026 $endcallback($component, true, $verbose);
1031 if (empty($installedversion)) { // block not installed yet, so install it
1032 $conflictblock = array_search($blocktitle, $blocktitles);
1033 if ($conflictblock !== false) {
1034 // Duplicate block titles are not allowed, they confuse people
1035 // AND PHP's associative arrays ;)
1036 throw new plugin_defective_exception($component, get_string('blocknameconflict', 'error', (object)array('name'=>$block->name, 'conflict'=>$conflictblock)));
1038 $startcallback($component, true, $verbose);
1040 if (file_exists($fullblock.'/db/install.xml')) {
1041 $DB->get_manager()->install_from_xmldb_file($fullblock.'/db/install.xml');
1043 $block->id = $DB->insert_record('block', $block);
1044 upgrade_block_savepoint(true, $plugin->version, $block->name, false);
1046 if (file_exists($fullblock.'/db/install.php')) {
1047 require_once($fullblock.'/db/install.php');
1048 // Set installation running flag, we need to recover after exception or error
1049 set_config('installrunning', 1, 'block_'.$blockname);
1050 $post_install_function = 'xmldb_block_'.$blockname.'_install';
1051 $post_install_function();
1052 unset_config('installrunning', 'block_'.$blockname);
1055 $blocktitles[$block->name] = $blocktitle;
1057 // Install various components
1058 update_capabilities($component);
1059 log_update_descriptions($component);
1060 external_update_descriptions($component);
1061 \core\task\manager::reset_scheduled_tasks_for_component($component);
1062 \core_analytics\manager::update_default_models_for_component($component);
1063 message_update_providers($component);
1064 \core\message\inbound\manager::update_handlers_for_component($component);
1065 core_tag_area::reset_definitions_for_component($component);
1066 upgrade_plugin_mnet_functions($component);
1068 $endcallback($component, true, $verbose);
1070 } else if ($installedversion < $plugin->version) {
1071 $startcallback($component, false, $verbose);
1073 if (is_readable($fullblock.'/db/upgrade.php')) {
1074 require_once($fullblock.'/db/upgrade.php'); // defines new upgrading function
1075 $newupgrade_function = 'xmldb_block_'.$blockname.'_upgrade';
1076 $result = $newupgrade_function($installedversion, $block);
1077 } else {
1078 $result = true;
1081 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
1082 $currblock = $DB->get_record('block', array('name'=>$block->name));
1083 if ($installedversion < $plugin->version) {
1084 // store version if not already there
1085 upgrade_block_savepoint($result, $plugin->version, $block->name, false);
1088 if ($currblock->cron != $block->cron) {
1089 // update cron flag if needed
1090 $DB->set_field('block', 'cron', $block->cron, array('id' => $currblock->id));
1093 // Upgrade various components
1094 update_capabilities($component);
1095 log_update_descriptions($component);
1096 external_update_descriptions($component);
1097 \core\task\manager::reset_scheduled_tasks_for_component($component);
1098 \core_analytics\manager::update_default_models_for_component($component);
1099 message_update_providers($component);
1100 \core\message\inbound\manager::update_handlers_for_component($component);
1101 upgrade_plugin_mnet_functions($component);
1102 core_tag_area::reset_definitions_for_component($component);
1104 $endcallback($component, false, $verbose);
1106 } else if ($installedversion > $plugin->version) {
1107 throw new downgrade_exception($component, $installedversion, $plugin->version);
1112 // Finally, if we are in the first_install of BLOCKS setup frontpage and admin page blocks
1113 if ($first_install) {
1114 //Iterate over each course - there should be only site course here now
1115 if ($courses = $DB->get_records('course')) {
1116 foreach ($courses as $course) {
1117 blocks_add_default_course_blocks($course);
1121 blocks_add_default_system_blocks();
1127 * Log_display description function used during install and upgrade.
1129 * @param string $component name of component (moodle, mod_assignment, etc.)
1130 * @return void
1132 function log_update_descriptions($component) {
1133 global $DB;
1135 $defpath = core_component::get_component_directory($component).'/db/log.php';
1137 if (!file_exists($defpath)) {
1138 $DB->delete_records('log_display', array('component'=>$component));
1139 return;
1142 // load new info
1143 $logs = array();
1144 include($defpath);
1145 $newlogs = array();
1146 foreach ($logs as $log) {
1147 $newlogs[$log['module'].'-'.$log['action']] = $log; // kind of unique name
1149 unset($logs);
1150 $logs = $newlogs;
1152 $fields = array('module', 'action', 'mtable', 'field');
1153 // update all log fist
1154 $dblogs = $DB->get_records('log_display', array('component'=>$component));
1155 foreach ($dblogs as $dblog) {
1156 $name = $dblog->module.'-'.$dblog->action;
1158 if (empty($logs[$name])) {
1159 $DB->delete_records('log_display', array('id'=>$dblog->id));
1160 continue;
1163 $log = $logs[$name];
1164 unset($logs[$name]);
1166 $update = false;
1167 foreach ($fields as $field) {
1168 if ($dblog->$field != $log[$field]) {
1169 $dblog->$field = $log[$field];
1170 $update = true;
1173 if ($update) {
1174 $DB->update_record('log_display', $dblog);
1177 foreach ($logs as $log) {
1178 $dblog = (object)$log;
1179 $dblog->component = $component;
1180 $DB->insert_record('log_display', $dblog);
1185 * Web service discovery function used during install and upgrade.
1186 * @param string $component name of component (moodle, mod_assignment, etc.)
1187 * @return void
1189 function external_update_descriptions($component) {
1190 global $DB, $CFG;
1192 $defpath = core_component::get_component_directory($component).'/db/services.php';
1194 if (!file_exists($defpath)) {
1195 require_once($CFG->dirroot.'/lib/externallib.php');
1196 external_delete_descriptions($component);
1197 return;
1200 // load new info
1201 $functions = array();
1202 $services = array();
1203 include($defpath);
1205 // update all function fist
1206 $dbfunctions = $DB->get_records('external_functions', array('component'=>$component));
1207 foreach ($dbfunctions as $dbfunction) {
1208 if (empty($functions[$dbfunction->name])) {
1209 $DB->delete_records('external_functions', array('id'=>$dbfunction->id));
1210 // do not delete functions from external_services_functions, beacuse
1211 // we want to notify admins when functions used in custom services disappear
1213 //TODO: this looks wrong, we have to delete it eventually (skodak)
1214 continue;
1217 $function = $functions[$dbfunction->name];
1218 unset($functions[$dbfunction->name]);
1219 $function['classpath'] = empty($function['classpath']) ? null : $function['classpath'];
1220 $function['methodname'] = $function['methodname'] ?? 'execute';
1222 $update = false;
1223 if ($dbfunction->classname != $function['classname']) {
1224 $dbfunction->classname = $function['classname'];
1225 $update = true;
1227 if ($dbfunction->methodname != $function['methodname']) {
1228 $dbfunction->methodname = $function['methodname'];
1229 $update = true;
1231 if ($dbfunction->classpath != $function['classpath']) {
1232 $dbfunction->classpath = $function['classpath'];
1233 $update = true;
1235 $functioncapabilities = array_key_exists('capabilities', $function)?$function['capabilities']:'';
1236 if ($dbfunction->capabilities != $functioncapabilities) {
1237 $dbfunction->capabilities = $functioncapabilities;
1238 $update = true;
1241 if (isset($function['services']) and is_array($function['services'])) {
1242 sort($function['services']);
1243 $functionservices = implode(',', $function['services']);
1244 } else {
1245 // Force null values in the DB.
1246 $functionservices = null;
1249 if ($dbfunction->services != $functionservices) {
1250 // Now, we need to check if services were removed, in that case we need to remove the function from them.
1251 $servicesremoved = array_diff(explode(",", $dbfunction->services), explode(",", $functionservices));
1252 foreach ($servicesremoved as $removedshortname) {
1253 if ($externalserviceid = $DB->get_field('external_services', 'id', array("shortname" => $removedshortname))) {
1254 $DB->delete_records('external_services_functions', array('functionname' => $dbfunction->name,
1255 'externalserviceid' => $externalserviceid));
1259 $dbfunction->services = $functionservices;
1260 $update = true;
1262 if ($update) {
1263 $DB->update_record('external_functions', $dbfunction);
1266 foreach ($functions as $fname => $function) {
1267 $dbfunction = new stdClass();
1268 $dbfunction->name = $fname;
1269 $dbfunction->classname = $function['classname'];
1270 $dbfunction->methodname = $function['methodname'] ?? 'execute';
1271 $dbfunction->classpath = empty($function['classpath']) ? null : $function['classpath'];
1272 $dbfunction->component = $component;
1273 $dbfunction->capabilities = array_key_exists('capabilities', $function)?$function['capabilities']:'';
1275 if (isset($function['services']) and is_array($function['services'])) {
1276 sort($function['services']);
1277 $dbfunction->services = implode(',', $function['services']);
1278 } else {
1279 // Force null values in the DB.
1280 $dbfunction->services = null;
1283 $dbfunction->id = $DB->insert_record('external_functions', $dbfunction);
1285 unset($functions);
1287 // now deal with services
1288 $dbservices = $DB->get_records('external_services', array('component'=>$component));
1289 foreach ($dbservices as $dbservice) {
1290 if (empty($services[$dbservice->name])) {
1291 $DB->delete_records('external_tokens', array('externalserviceid'=>$dbservice->id));
1292 $DB->delete_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
1293 $DB->delete_records('external_services_users', array('externalserviceid'=>$dbservice->id));
1294 $DB->delete_records('external_services', array('id'=>$dbservice->id));
1295 continue;
1297 $service = $services[$dbservice->name];
1298 unset($services[$dbservice->name]);
1299 $service['enabled'] = empty($service['enabled']) ? 0 : $service['enabled'];
1300 $service['requiredcapability'] = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1301 $service['restrictedusers'] = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
1302 $service['downloadfiles'] = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
1303 $service['uploadfiles'] = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles'];
1304 $service['shortname'] = !isset($service['shortname']) ? null : $service['shortname'];
1306 $update = false;
1307 if ($dbservice->requiredcapability != $service['requiredcapability']) {
1308 $dbservice->requiredcapability = $service['requiredcapability'];
1309 $update = true;
1311 if ($dbservice->restrictedusers != $service['restrictedusers']) {
1312 $dbservice->restrictedusers = $service['restrictedusers'];
1313 $update = true;
1315 if ($dbservice->downloadfiles != $service['downloadfiles']) {
1316 $dbservice->downloadfiles = $service['downloadfiles'];
1317 $update = true;
1319 if ($dbservice->uploadfiles != $service['uploadfiles']) {
1320 $dbservice->uploadfiles = $service['uploadfiles'];
1321 $update = true;
1323 //if shortname is not a PARAM_ALPHANUMEXT, fail (tested here for service update and creation)
1324 if (isset($service['shortname']) and
1325 (clean_param($service['shortname'], PARAM_ALPHANUMEXT) != $service['shortname'])) {
1326 throw new moodle_exception('installserviceshortnameerror', 'webservice', '', $service['shortname']);
1328 if ($dbservice->shortname != $service['shortname']) {
1329 //check that shortname is unique
1330 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
1331 $existingservice = $DB->get_record('external_services',
1332 array('shortname' => $service['shortname']));
1333 if (!empty($existingservice)) {
1334 throw new moodle_exception('installexistingserviceshortnameerror', 'webservice', '', $service['shortname']);
1337 $dbservice->shortname = $service['shortname'];
1338 $update = true;
1340 if ($update) {
1341 $DB->update_record('external_services', $dbservice);
1344 $functions = $DB->get_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
1345 foreach ($functions as $function) {
1346 $key = array_search($function->functionname, $service['functions']);
1347 if ($key === false) {
1348 $DB->delete_records('external_services_functions', array('id'=>$function->id));
1349 } else {
1350 unset($service['functions'][$key]);
1353 foreach ($service['functions'] as $fname) {
1354 $newf = new stdClass();
1355 $newf->externalserviceid = $dbservice->id;
1356 $newf->functionname = $fname;
1357 $DB->insert_record('external_services_functions', $newf);
1359 unset($functions);
1361 foreach ($services as $name => $service) {
1362 //check that shortname is unique
1363 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
1364 $existingservice = $DB->get_record('external_services',
1365 array('shortname' => $service['shortname']));
1366 if (!empty($existingservice)) {
1367 throw new moodle_exception('installserviceshortnameerror', 'webservice');
1371 $dbservice = new stdClass();
1372 $dbservice->name = $name;
1373 $dbservice->enabled = empty($service['enabled']) ? 0 : $service['enabled'];
1374 $dbservice->requiredcapability = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1375 $dbservice->restrictedusers = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
1376 $dbservice->downloadfiles = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
1377 $dbservice->uploadfiles = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles'];
1378 $dbservice->shortname = !isset($service['shortname']) ? null : $service['shortname'];
1379 $dbservice->component = $component;
1380 $dbservice->timecreated = time();
1381 $dbservice->id = $DB->insert_record('external_services', $dbservice);
1382 foreach ($service['functions'] as $fname) {
1383 $newf = new stdClass();
1384 $newf->externalserviceid = $dbservice->id;
1385 $newf->functionname = $fname;
1386 $DB->insert_record('external_services_functions', $newf);
1392 * Allow plugins and subsystems to add external functions to other plugins or built-in services.
1393 * This function is executed just after all the plugins have been updated.
1395 function external_update_services() {
1396 global $DB;
1398 // Look for external functions that want to be added in existing services.
1399 $functions = $DB->get_records_select('external_functions', 'services IS NOT NULL');
1401 $servicescache = array();
1402 foreach ($functions as $function) {
1403 // Prevent edge cases.
1404 if (empty($function->services)) {
1405 continue;
1407 $services = explode(',', $function->services);
1409 foreach ($services as $serviceshortname) {
1410 // Get the service id by shortname.
1411 if (!empty($servicescache[$serviceshortname])) {
1412 $serviceid = $servicescache[$serviceshortname];
1413 } else if ($service = $DB->get_record('external_services', array('shortname' => $serviceshortname))) {
1414 // If the component is empty, it means that is not a built-in service.
1415 // We don't allow functions to inject themselves in services created by an user in Moodle.
1416 if (empty($service->component)) {
1417 continue;
1419 $serviceid = $service->id;
1420 $servicescache[$serviceshortname] = $serviceid;
1421 } else {
1422 // Service not found.
1423 continue;
1425 // Finally add the function to the service.
1426 $newf = new stdClass();
1427 $newf->externalserviceid = $serviceid;
1428 $newf->functionname = $function->name;
1430 if (!$DB->record_exists('external_services_functions', (array)$newf)) {
1431 $DB->insert_record('external_services_functions', $newf);
1438 * upgrade logging functions
1440 function upgrade_handle_exception($ex, $plugin = null) {
1441 global $CFG;
1443 // rollback everything, we need to log all upgrade problems
1444 abort_all_db_transactions();
1446 $info = get_exception_info($ex);
1448 // First log upgrade error
1449 upgrade_log(UPGRADE_LOG_ERROR, $plugin, 'Exception: ' . get_class($ex), $info->message, $info->backtrace);
1451 // Always turn on debugging - admins need to know what is going on
1452 set_debugging(DEBUG_DEVELOPER, true);
1454 default_exception_handler($ex, true, $plugin);
1458 * Adds log entry into upgrade_log table
1460 * @param int $type UPGRADE_LOG_NORMAL, UPGRADE_LOG_NOTICE or UPGRADE_LOG_ERROR
1461 * @param string $plugin frankenstyle component name
1462 * @param string $info short description text of log entry
1463 * @param string $details long problem description
1464 * @param string $backtrace string used for errors only
1465 * @return void
1467 function upgrade_log($type, $plugin, $info, $details=null, $backtrace=null) {
1468 global $DB, $USER, $CFG;
1470 if (empty($plugin)) {
1471 $plugin = 'core';
1474 list($plugintype, $pluginname) = core_component::normalize_component($plugin);
1475 $component = is_null($pluginname) ? $plugintype : $plugintype . '_' . $pluginname;
1477 $backtrace = format_backtrace($backtrace, true);
1479 $currentversion = null;
1480 $targetversion = null;
1482 //first try to find out current version number
1483 if ($plugintype === 'core') {
1484 //main
1485 $currentversion = $CFG->version;
1487 $version = null;
1488 include("$CFG->dirroot/version.php");
1489 $targetversion = $version;
1491 } else {
1492 $pluginversion = get_config($component, 'version');
1493 if (!empty($pluginversion)) {
1494 $currentversion = $pluginversion;
1496 $cd = core_component::get_component_directory($component);
1497 if (file_exists("$cd/version.php")) {
1498 $plugin = new stdClass();
1499 $plugin->version = null;
1500 $module = $plugin;
1501 include("$cd/version.php");
1502 $targetversion = $plugin->version;
1506 $log = new stdClass();
1507 $log->type = $type;
1508 $log->plugin = $component;
1509 $log->version = $currentversion;
1510 $log->targetversion = $targetversion;
1511 $log->info = $info;
1512 $log->details = $details;
1513 $log->backtrace = $backtrace;
1514 $log->userid = $USER->id;
1515 $log->timemodified = time();
1516 try {
1517 $DB->insert_record('upgrade_log', $log);
1518 } catch (Exception $ignored) {
1519 // possible during install or 2.0 upgrade
1524 * Marks start of upgrade, blocks any other access to site.
1525 * The upgrade is finished at the end of script or after timeout.
1527 * @global object
1528 * @global object
1529 * @global object
1531 function upgrade_started($preinstall=false) {
1532 global $CFG, $DB, $PAGE, $OUTPUT;
1534 static $started = false;
1536 if ($preinstall) {
1537 ignore_user_abort(true);
1538 upgrade_setup_debug(true);
1540 } else if ($started) {
1541 upgrade_set_timeout(120);
1543 } else {
1544 if (!CLI_SCRIPT and !$PAGE->headerprinted) {
1545 $strupgrade = get_string('upgradingversion', 'admin');
1546 $PAGE->set_pagelayout('maintenance');
1547 upgrade_init_javascript();
1548 $PAGE->set_title($strupgrade.' - Moodle '.$CFG->target_release);
1549 $PAGE->set_heading($strupgrade);
1550 $PAGE->navbar->add($strupgrade);
1551 $PAGE->set_cacheable(false);
1552 echo $OUTPUT->header();
1555 ignore_user_abort(true);
1556 core_shutdown_manager::register_function('upgrade_finished_handler');
1557 upgrade_setup_debug(true);
1558 set_config('upgraderunning', time()+300);
1559 $started = true;
1564 * Internal function - executed if upgrade interrupted.
1566 function upgrade_finished_handler() {
1567 upgrade_finished();
1571 * Indicates upgrade is finished.
1573 * This function may be called repeatedly.
1575 * @global object
1576 * @global object
1578 function upgrade_finished($continueurl=null) {
1579 global $CFG, $DB, $OUTPUT;
1581 if (!empty($CFG->upgraderunning)) {
1582 unset_config('upgraderunning');
1583 // We have to forcefully purge the caches using the writer here.
1584 // This has to be done after we unset the config var. If someone hits the site while this is set they will
1585 // cause the config values to propogate to the caches.
1586 // Caches are purged after the last step in an upgrade but there is several code routines that exceute between
1587 // then and now that leaving a window for things to fall out of sync.
1588 cache_helper::purge_all(true);
1589 upgrade_setup_debug(false);
1590 ignore_user_abort(false);
1591 if ($continueurl) {
1592 echo $OUTPUT->continue_button($continueurl);
1593 echo $OUTPUT->footer();
1594 die;
1600 * @global object
1601 * @global object
1603 function upgrade_setup_debug($starting) {
1604 global $CFG, $DB;
1606 static $originaldebug = null;
1608 if ($starting) {
1609 if ($originaldebug === null) {
1610 $originaldebug = $DB->get_debug();
1612 if (!empty($CFG->upgradeshowsql)) {
1613 $DB->set_debug(true);
1615 } else {
1616 $DB->set_debug($originaldebug);
1620 function print_upgrade_separator() {
1621 if (!CLI_SCRIPT) {
1622 echo '<hr />';
1627 * Default start upgrade callback
1628 * @param string $plugin
1629 * @param bool $installation true if installation, false means upgrade
1631 function print_upgrade_part_start($plugin, $installation, $verbose) {
1632 global $OUTPUT;
1633 if (empty($plugin) or $plugin == 'moodle') {
1634 upgrade_started($installation); // does not store upgrade running flag yet
1635 if ($verbose) {
1636 echo $OUTPUT->heading(get_string('coresystem'));
1638 } else {
1639 upgrade_started();
1640 if ($verbose) {
1641 echo $OUTPUT->heading($plugin);
1644 if ($installation) {
1645 if (empty($plugin) or $plugin == 'moodle') {
1646 // no need to log - log table not yet there ;-)
1647 } else {
1648 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin installation');
1650 } else {
1651 core_upgrade_time::record_start();
1652 if (empty($plugin) or $plugin == 'moodle') {
1653 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting core upgrade');
1654 } else {
1655 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin upgrade');
1661 * Default end upgrade callback
1662 * @param string $plugin
1663 * @param bool $installation true if installation, false means upgrade
1665 function print_upgrade_part_end($plugin, $installation, $verbose) {
1666 global $OUTPUT;
1667 upgrade_started();
1668 if ($installation) {
1669 if (empty($plugin) or $plugin == 'moodle') {
1670 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core installed');
1671 } else {
1672 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin installed');
1674 } else {
1675 if (empty($plugin) or $plugin == 'moodle') {
1676 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core upgraded');
1677 } else {
1678 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin upgraded');
1681 if ($verbose) {
1682 if ($installation) {
1683 $message = get_string('success');
1684 } else {
1685 $duration = core_upgrade_time::get_elapsed();
1686 $message = get_string('successduration', '', format_float($duration, 2));
1688 $notification = new \core\output\notification($message, \core\output\notification::NOTIFY_SUCCESS);
1689 $notification->set_show_closebutton(false);
1690 echo $OUTPUT->render($notification);
1691 print_upgrade_separator();
1696 * Sets up JS code required for all upgrade scripts.
1697 * @global object
1699 function upgrade_init_javascript() {
1700 global $PAGE;
1701 // scroll to the end of each upgrade page so that ppl see either error or continue button,
1702 // no need to scroll continuously any more, it is enough to jump to end once the footer is printed ;-)
1703 $js = "window.scrollTo(0, 5000000);";
1704 $PAGE->requires->js_init_code($js);
1708 * Try to upgrade the given language pack (or current language)
1710 * @param string $lang the code of the language to update, defaults to the current language
1712 function upgrade_language_pack($lang = null) {
1713 global $CFG;
1715 if (!empty($CFG->skiplangupgrade)) {
1716 return;
1719 if (!file_exists("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php")) {
1720 // weird, somebody uninstalled the import utility
1721 return;
1724 if (!$lang) {
1725 $lang = current_language();
1728 if (!get_string_manager()->translation_exists($lang)) {
1729 return;
1732 get_string_manager()->reset_caches();
1734 if ($lang === 'en') {
1735 return; // Nothing to do
1738 upgrade_started(false);
1740 require_once("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php");
1741 tool_langimport_preupgrade_update($lang);
1743 get_string_manager()->reset_caches();
1745 print_upgrade_separator();
1749 * Build the current theme so that the user doesn't have to wait for it
1750 * to build on the first page load after the install / upgrade.
1752 function upgrade_themes() {
1753 global $CFG;
1755 require_once("{$CFG->libdir}/outputlib.php");
1757 // Build the current theme so that the user can immediately
1758 // browse the site without having to wait for the theme to build.
1759 $themeconfig = theme_config::load($CFG->theme);
1760 $direction = right_to_left() ? 'rtl' : 'ltr';
1761 theme_build_css_for_themes([$themeconfig], [$direction]);
1763 // Only queue the task if there isn't already one queued.
1764 if (empty(\core\task\manager::get_adhoc_tasks('\\core\\task\\build_installed_themes_task'))) {
1765 // Queue a task to build all of the site themes at some point
1766 // later. These can happen offline because it doesn't block the
1767 // user unless they quickly change theme.
1768 $adhoctask = new \core\task\build_installed_themes_task();
1769 \core\task\manager::queue_adhoc_task($adhoctask);
1774 * Install core moodle tables and initialize
1775 * @param float $version target version
1776 * @param bool $verbose
1777 * @return void, may throw exception
1779 function install_core($version, $verbose) {
1780 global $CFG, $DB;
1782 // We can not call purge_all_caches() yet, make sure the temp and cache dirs exist and are empty.
1783 remove_dir($CFG->cachedir.'', true);
1784 make_cache_directory('', true);
1786 remove_dir($CFG->localcachedir.'', true);
1787 make_localcache_directory('', true);
1789 remove_dir($CFG->tempdir.'', true);
1790 make_temp_directory('', true);
1792 remove_dir($CFG->backuptempdir.'', true);
1793 make_backup_temp_directory('', true);
1795 remove_dir($CFG->dataroot.'/muc', true);
1796 make_writable_directory($CFG->dataroot.'/muc', true);
1798 try {
1799 core_php_time_limit::raise(600);
1800 print_upgrade_part_start('moodle', true, $verbose); // does not store upgrade running flag
1802 $DB->get_manager()->install_from_xmldb_file("$CFG->libdir/db/install.xml");
1803 upgrade_started(); // we want the flag to be stored in config table ;-)
1805 // set all core default records and default settings
1806 require_once("$CFG->libdir/db/install.php");
1807 xmldb_main_install(); // installs the capabilities too
1809 // store version
1810 upgrade_main_savepoint(true, $version, false);
1812 // Continue with the installation
1813 log_update_descriptions('moodle');
1814 external_update_descriptions('moodle');
1815 \core\task\manager::reset_scheduled_tasks_for_component('moodle');
1816 \core_analytics\manager::update_default_models_for_component('moodle');
1817 message_update_providers('moodle');
1818 \core\message\inbound\manager::update_handlers_for_component('moodle');
1819 core_tag_area::reset_definitions_for_component('moodle');
1821 // Write default settings unconditionally
1822 admin_apply_default_settings(NULL, true);
1824 print_upgrade_part_end(null, true, $verbose);
1826 // Purge all caches. They're disabled but this ensures that we don't have any persistent data just in case something
1827 // during installation didn't use APIs.
1828 cache_helper::purge_all();
1829 } catch (exception $ex) {
1830 upgrade_handle_exception($ex);
1831 } catch (Throwable $ex) {
1832 // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
1833 upgrade_handle_exception($ex);
1838 * Upgrade moodle core
1839 * @param float $version target version
1840 * @param bool $verbose
1841 * @return void, may throw exception
1843 function upgrade_core($version, $verbose) {
1844 global $CFG, $SITE, $DB, $COURSE;
1846 raise_memory_limit(MEMORY_EXTRA);
1848 require_once($CFG->libdir.'/db/upgrade.php'); // Defines upgrades
1850 try {
1851 // Reset caches before any output.
1852 cache_helper::purge_all(true);
1853 purge_all_caches();
1855 // Upgrade current language pack if we can
1856 upgrade_language_pack();
1858 print_upgrade_part_start('moodle', false, $verbose);
1860 // Pre-upgrade scripts for local hack workarounds.
1861 $preupgradefile = "$CFG->dirroot/local/preupgrade.php";
1862 if (file_exists($preupgradefile)) {
1863 core_php_time_limit::raise();
1864 require($preupgradefile);
1865 // Reset upgrade timeout to default.
1866 upgrade_set_timeout();
1869 $result = xmldb_main_upgrade($CFG->version);
1870 if ($version > $CFG->version) {
1871 // store version if not already there
1872 upgrade_main_savepoint($result, $version, false);
1875 // In case structure of 'course' table has been changed and we forgot to update $SITE, re-read it from db.
1876 $SITE = $DB->get_record('course', array('id' => $SITE->id));
1877 $COURSE = clone($SITE);
1879 // perform all other component upgrade routines
1880 update_capabilities('moodle');
1881 log_update_descriptions('moodle');
1882 external_update_descriptions('moodle');
1883 \core\task\manager::reset_scheduled_tasks_for_component('moodle');
1884 \core_analytics\manager::update_default_models_for_component('moodle');
1885 message_update_providers('moodle');
1886 \core\message\inbound\manager::update_handlers_for_component('moodle');
1887 core_tag_area::reset_definitions_for_component('moodle');
1888 // Update core definitions.
1889 cache_helper::update_definitions(true);
1891 // Purge caches again, just to be sure we arn't holding onto old stuff now.
1892 cache_helper::purge_all(true);
1893 purge_all_caches();
1895 // Clean up contexts - more and more stuff depends on existence of paths and contexts
1896 context_helper::cleanup_instances();
1897 context_helper::create_instances(null, false);
1898 context_helper::build_all_paths(false);
1899 $syscontext = context_system::instance();
1900 $syscontext->mark_dirty();
1902 print_upgrade_part_end('moodle', false, $verbose);
1903 } catch (Exception $ex) {
1904 upgrade_handle_exception($ex);
1905 } catch (Throwable $ex) {
1906 // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
1907 upgrade_handle_exception($ex);
1912 * Upgrade/install other parts of moodle
1913 * @param bool $verbose
1914 * @return void, may throw exception
1916 function upgrade_noncore($verbose) {
1917 global $CFG;
1919 raise_memory_limit(MEMORY_EXTRA);
1921 // upgrade all plugins types
1922 try {
1923 // Reset caches before any output.
1924 cache_helper::purge_all(true);
1925 purge_all_caches();
1927 $plugintypes = core_component::get_plugin_types();
1928 foreach ($plugintypes as $type=>$location) {
1929 upgrade_plugins($type, 'print_upgrade_part_start', 'print_upgrade_part_end', $verbose);
1931 // Upgrade services.
1932 // This function gives plugins and subsystems a chance to add functions to existing built-in services.
1933 external_update_services();
1935 // Update cache definitions. Involves scanning each plugin for any changes.
1936 cache_helper::update_definitions();
1937 // Mark the site as upgraded.
1938 set_config('allversionshash', core_component::get_all_versions_hash());
1940 // Purge caches again, just to be sure we arn't holding onto old stuff now.
1941 cache_helper::purge_all(true);
1942 purge_all_caches();
1944 } catch (Exception $ex) {
1945 upgrade_handle_exception($ex);
1946 } catch (Throwable $ex) {
1947 // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
1948 upgrade_handle_exception($ex);
1953 * Checks if the main tables have been installed yet or not.
1955 * Note: we can not use caches here because they might be stale,
1956 * use with care!
1958 * @return bool
1960 function core_tables_exist() {
1961 global $DB;
1963 if (!$tables = $DB->get_tables(false) ) { // No tables yet at all.
1964 return false;
1966 } else { // Check for missing main tables
1967 $mtables = array('config', 'course', 'groupings'); // some tables used in 1.9 and 2.0, preferable something from the start and end of install.xml
1968 foreach ($mtables as $mtable) {
1969 if (!in_array($mtable, $tables)) {
1970 return false;
1973 return true;
1978 * upgrades the mnet rpc definitions for the given component.
1979 * this method doesn't return status, an exception will be thrown in the case of an error
1981 * @param string $component the plugin to upgrade, eg auth_mnet
1983 function upgrade_plugin_mnet_functions($component) {
1984 global $DB, $CFG;
1986 list($type, $plugin) = core_component::normalize_component($component);
1987 $path = core_component::get_plugin_directory($type, $plugin);
1989 $publishes = array();
1990 $subscribes = array();
1991 if (file_exists($path . '/db/mnet.php')) {
1992 require_once($path . '/db/mnet.php'); // $publishes comes from this file
1994 if (empty($publishes)) {
1995 $publishes = array(); // still need this to be able to disable stuff later
1997 if (empty($subscribes)) {
1998 $subscribes = array(); // still need this to be able to disable stuff later
2001 static $servicecache = array();
2003 // rekey an array based on the rpc method for easy lookups later
2004 $publishmethodservices = array();
2005 $subscribemethodservices = array();
2006 foreach($publishes as $servicename => $service) {
2007 if (is_array($service['methods'])) {
2008 foreach($service['methods'] as $methodname) {
2009 $service['servicename'] = $servicename;
2010 $publishmethodservices[$methodname][] = $service;
2015 // Disable functions that don't exist (any more) in the source
2016 // Should these be deleted? What about their permissions records?
2017 foreach ($DB->get_records('mnet_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
2018 if (!array_key_exists($rpc->functionname, $publishmethodservices) && $rpc->enabled) {
2019 $DB->set_field('mnet_rpc', 'enabled', 0, array('id' => $rpc->id));
2020 } else if (array_key_exists($rpc->functionname, $publishmethodservices) && !$rpc->enabled) {
2021 $DB->set_field('mnet_rpc', 'enabled', 1, array('id' => $rpc->id));
2025 // reflect all the services we're publishing and save them
2026 static $cachedclasses = array(); // to store reflection information in
2027 foreach ($publishes as $service => $data) {
2028 $f = $data['filename'];
2029 $c = $data['classname'];
2030 foreach ($data['methods'] as $method) {
2031 $dataobject = new stdClass();
2032 $dataobject->plugintype = $type;
2033 $dataobject->pluginname = $plugin;
2034 $dataobject->enabled = 1;
2035 $dataobject->classname = $c;
2036 $dataobject->filename = $f;
2038 if (is_string($method)) {
2039 $dataobject->functionname = $method;
2041 } else if (is_array($method)) { // wants to override file or class
2042 $dataobject->functionname = $method['method'];
2043 $dataobject->classname = $method['classname'];
2044 $dataobject->filename = $method['filename'];
2046 $dataobject->xmlrpcpath = $type.'/'.$plugin.'/'.$dataobject->filename.'/'.$method;
2047 $dataobject->static = false;
2049 require_once($path . '/' . $dataobject->filename);
2050 $functionreflect = null; // slightly different ways to get this depending on whether it's a class method or a function
2051 if (!empty($dataobject->classname)) {
2052 if (!class_exists($dataobject->classname)) {
2053 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
2055 $key = $dataobject->filename . '|' . $dataobject->classname;
2056 if (!array_key_exists($key, $cachedclasses)) { // look to see if we've already got a reflection object
2057 try {
2058 $cachedclasses[$key] = new ReflectionClass($dataobject->classname);
2059 } catch (ReflectionException $e) { // catch these and rethrow them to something more helpful
2060 throw new moodle_exception('installreflectionclasserror', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname, 'error' => $e->getMessage()));
2063 $r =& $cachedclasses[$key];
2064 if (!$r->hasMethod($dataobject->functionname)) {
2065 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
2067 $functionreflect = $r->getMethod($dataobject->functionname);
2068 $dataobject->static = (int)$functionreflect->isStatic();
2069 } else {
2070 if (!function_exists($dataobject->functionname)) {
2071 throw new moodle_exception('installnosuchfunction', 'mnet', '', (object)array('method' => $dataobject->functionname, 'file' => $dataobject->filename));
2073 try {
2074 $functionreflect = new ReflectionFunction($dataobject->functionname);
2075 } catch (ReflectionException $e) { // catch these and rethrow them to something more helpful
2076 throw new moodle_exception('installreflectionfunctionerror', 'mnet', '', (object)array('method' => $dataobject->functionname, '' => $dataobject->filename, 'error' => $e->getMessage()));
2079 $dataobject->profile = serialize(admin_mnet_method_profile($functionreflect));
2080 $dataobject->help = admin_mnet_method_get_help($functionreflect);
2082 if ($record_exists = $DB->get_record('mnet_rpc', array('xmlrpcpath'=>$dataobject->xmlrpcpath))) {
2083 $dataobject->id = $record_exists->id;
2084 $dataobject->enabled = $record_exists->enabled;
2085 $DB->update_record('mnet_rpc', $dataobject);
2086 } else {
2087 $dataobject->id = $DB->insert_record('mnet_rpc', $dataobject, true);
2090 // TODO this API versioning must be reworked, here the recently processed method
2091 // sets the service API which may not be correct
2092 foreach ($publishmethodservices[$dataobject->functionname] as $service) {
2093 if ($serviceobj = $DB->get_record('mnet_service', array('name'=>$service['servicename']))) {
2094 $serviceobj->apiversion = $service['apiversion'];
2095 $DB->update_record('mnet_service', $serviceobj);
2096 } else {
2097 $serviceobj = new stdClass();
2098 $serviceobj->name = $service['servicename'];
2099 $serviceobj->description = empty($service['description']) ? '' : $service['description'];
2100 $serviceobj->apiversion = $service['apiversion'];
2101 $serviceobj->offer = 1;
2102 $serviceobj->id = $DB->insert_record('mnet_service', $serviceobj);
2104 $servicecache[$service['servicename']] = $serviceobj;
2105 if (!$DB->record_exists('mnet_service2rpc', array('rpcid'=>$dataobject->id, 'serviceid'=>$serviceobj->id))) {
2106 $obj = new stdClass();
2107 $obj->rpcid = $dataobject->id;
2108 $obj->serviceid = $serviceobj->id;
2109 $DB->insert_record('mnet_service2rpc', $obj, true);
2114 // finished with methods we publish, now do subscribable methods
2115 foreach($subscribes as $service => $methods) {
2116 if (!array_key_exists($service, $servicecache)) {
2117 if (!$serviceobj = $DB->get_record('mnet_service', array('name' => $service))) {
2118 debugging("TODO: skipping unknown service $service - somebody needs to fix MDL-21993");
2119 continue;
2121 $servicecache[$service] = $serviceobj;
2122 } else {
2123 $serviceobj = $servicecache[$service];
2125 foreach ($methods as $method => $xmlrpcpath) {
2126 if (!$rpcid = $DB->get_field('mnet_remote_rpc', 'id', array('xmlrpcpath'=>$xmlrpcpath))) {
2127 $remoterpc = (object)array(
2128 'functionname' => $method,
2129 'xmlrpcpath' => $xmlrpcpath,
2130 'plugintype' => $type,
2131 'pluginname' => $plugin,
2132 'enabled' => 1,
2134 $rpcid = $remoterpc->id = $DB->insert_record('mnet_remote_rpc', $remoterpc, true);
2136 if (!$DB->record_exists('mnet_remote_service2rpc', array('rpcid'=>$rpcid, 'serviceid'=>$serviceobj->id))) {
2137 $obj = new stdClass();
2138 $obj->rpcid = $rpcid;
2139 $obj->serviceid = $serviceobj->id;
2140 $DB->insert_record('mnet_remote_service2rpc', $obj, true);
2142 $subscribemethodservices[$method][] = $service;
2146 foreach ($DB->get_records('mnet_remote_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
2147 if (!array_key_exists($rpc->functionname, $subscribemethodservices) && $rpc->enabled) {
2148 $DB->set_field('mnet_remote_rpc', 'enabled', 0, array('id' => $rpc->id));
2149 } else if (array_key_exists($rpc->functionname, $subscribemethodservices) && !$rpc->enabled) {
2150 $DB->set_field('mnet_remote_rpc', 'enabled', 1, array('id' => $rpc->id));
2154 return true;
2158 * Given some sort of reflection function/method object, return a profile array, ready to be serialized and stored
2160 * @param ReflectionFunctionAbstract $function reflection function/method object from which to extract information
2162 * @return array associative array with function/method information
2164 function admin_mnet_method_profile(ReflectionFunctionAbstract $function) {
2165 $commentlines = admin_mnet_method_get_docblock($function);
2166 $getkey = function($key) use ($commentlines) {
2167 return array_values(array_filter($commentlines, function($line) use ($key) {
2168 return $line[0] == $key;
2169 }));
2171 $returnline = $getkey('@return');
2172 return array (
2173 'parameters' => array_map(function($line) {
2174 return array(
2175 'name' => trim($line[2], " \t\n\r\0\x0B$"),
2176 'type' => $line[1],
2177 'description' => $line[3]
2179 }, $getkey('@param')),
2181 'return' => array(
2182 'type' => !empty($returnline[0][1]) ? $returnline[0][1] : 'void',
2183 'description' => !empty($returnline[0][2]) ? $returnline[0][2] : ''
2189 * Given some sort of reflection function/method object, return an array of docblock lines, where each line is an array of
2190 * keywords/descriptions
2192 * @param ReflectionFunctionAbstract $function reflection function/method object from which to extract information
2194 * @return array docblock converted in to an array
2196 function admin_mnet_method_get_docblock(ReflectionFunctionAbstract $function) {
2197 return array_map(function($line) {
2198 $text = trim($line, " \t\n\r\0\x0B*/");
2199 if (strpos($text, '@param') === 0) {
2200 return preg_split('/\s+/', $text, 4);
2203 if (strpos($text, '@return') === 0) {
2204 return preg_split('/\s+/', $text, 3);
2207 return array($text);
2208 }, explode("\n", $function->getDocComment()));
2212 * Given some sort of reflection function/method object, return just the help text
2214 * @param ReflectionFunctionAbstract $function reflection function/method object from which to extract information
2216 * @return string docblock help text
2218 function admin_mnet_method_get_help(ReflectionFunctionAbstract $function) {
2219 $helplines = array_map(function($line) {
2220 return implode(' ', $line);
2221 }, array_values(array_filter(admin_mnet_method_get_docblock($function), function($line) {
2222 return strpos($line[0], '@') !== 0 && !empty($line[0]);
2223 })));
2225 return implode("\n", $helplines);
2229 * This function verifies that the database is not using an unsupported storage engine.
2231 * @param environment_results $result object to update, if relevant
2232 * @return environment_results|null updated results object, or null if the storage engine is supported
2234 function check_database_storage_engine(environment_results $result) {
2235 global $DB;
2237 // Check if MySQL is the DB family (this will also be the same for MariaDB).
2238 if ($DB->get_dbfamily() == 'mysql') {
2239 // Get the database engine we will either be using to install the tables, or what we are currently using.
2240 $engine = $DB->get_dbengine();
2241 // Check if MyISAM is the storage engine that will be used, if so, do not proceed and display an error.
2242 if ($engine == 'MyISAM') {
2243 $result->setInfo('unsupported_db_storage_engine');
2244 $result->setStatus(false);
2245 return $result;
2249 return null;
2253 * Method used to check the usage of slasharguments config and display a warning message.
2255 * @param environment_results $result object to update, if relevant.
2256 * @return environment_results|null updated results or null if slasharguments is disabled.
2258 function check_slasharguments(environment_results $result){
2259 global $CFG;
2261 if (!during_initial_install() && empty($CFG->slasharguments)) {
2262 $result->setInfo('slasharguments');
2263 $result->setStatus(false);
2264 return $result;
2267 return null;
2271 * This function verifies if the database has tables using innoDB Antelope row format.
2273 * @param environment_results $result
2274 * @return environment_results|null updated results object, or null if no Antelope table has been found.
2276 function check_database_tables_row_format(environment_results $result) {
2277 global $DB;
2279 if ($DB->get_dbfamily() == 'mysql') {
2280 $generator = $DB->get_manager()->generator;
2282 foreach ($DB->get_tables(false) as $table) {
2283 $columns = $DB->get_columns($table, false);
2284 $size = $generator->guess_antelope_row_size($columns);
2285 $format = $DB->get_row_format($table);
2287 if ($size <= $generator::ANTELOPE_MAX_ROW_SIZE) {
2288 continue;
2291 if ($format === 'Compact' or $format === 'Redundant') {
2292 $result->setInfo('unsupported_db_table_row_format');
2293 $result->setStatus(false);
2294 return $result;
2299 return null;
2303 * This function verfies that the database has tables using InnoDB Antelope row format.
2305 * @param environment_results $result
2306 * @return environment_results|null updated results object, or null if no Antelope table has been found.
2308 function check_mysql_file_format(environment_results $result) {
2309 global $DB;
2311 if ($DB->get_dbfamily() == 'mysql') {
2312 $collation = $DB->get_dbcollation();
2313 $collationinfo = explode('_', $collation);
2314 $charset = reset($collationinfo);
2316 if ($charset == 'utf8mb4') {
2317 if ($DB->get_row_format() !== "Barracuda") {
2318 $result->setInfo('mysql_full_unicode_support#File_format');
2319 $result->setStatus(false);
2320 return $result;
2324 return null;
2328 * This function verfies that the database has a setting of one file per table. This is required for 'utf8mb4'.
2330 * @param environment_results $result
2331 * @return environment_results|null updated results object, or null if innodb_file_per_table = 1.
2333 function check_mysql_file_per_table(environment_results $result) {
2334 global $DB;
2336 if ($DB->get_dbfamily() == 'mysql') {
2337 $collation = $DB->get_dbcollation();
2338 $collationinfo = explode('_', $collation);
2339 $charset = reset($collationinfo);
2341 if ($charset == 'utf8mb4') {
2342 if (!$DB->is_file_per_table_enabled()) {
2343 $result->setInfo('mysql_full_unicode_support#File_per_table');
2344 $result->setStatus(false);
2345 return $result;
2349 return null;
2353 * This function verfies that the database has the setting of large prefix enabled. This is required for 'utf8mb4'.
2355 * @param environment_results $result
2356 * @return environment_results|null updated results object, or null if innodb_large_prefix = 1.
2358 function check_mysql_large_prefix(environment_results $result) {
2359 global $DB;
2361 if ($DB->get_dbfamily() == 'mysql') {
2362 $collation = $DB->get_dbcollation();
2363 $collationinfo = explode('_', $collation);
2364 $charset = reset($collationinfo);
2366 if ($charset == 'utf8mb4') {
2367 if (!$DB->is_large_prefix_enabled()) {
2368 $result->setInfo('mysql_full_unicode_support#Large_prefix');
2369 $result->setStatus(false);
2370 return $result;
2374 return null;
2378 * This function checks the database to see if it is using incomplete unicode support.
2380 * @param environment_results $result $result
2381 * @return environment_results|null updated results object, or null if unicode is fully supported.
2383 function check_mysql_incomplete_unicode_support(environment_results $result) {
2384 global $DB;
2386 if ($DB->get_dbfamily() == 'mysql') {
2387 $collation = $DB->get_dbcollation();
2388 $collationinfo = explode('_', $collation);
2389 $charset = reset($collationinfo);
2391 if ($charset == 'utf8') {
2392 $result->setInfo('mysql_full_unicode_support');
2393 $result->setStatus(false);
2394 return $result;
2397 return null;
2401 * Check if the site is being served using an ssl url.
2403 * Note this does not really perform any request neither looks for proxies or
2404 * other situations. Just looks to wwwroot and warn if it's not using https.
2406 * @param environment_results $result $result
2407 * @return environment_results|null updated results object, or null if the site is https.
2409 function check_is_https(environment_results $result) {
2410 global $CFG;
2412 // Only if is defined, non-empty and whatever core tell us.
2413 if (!empty($CFG->wwwroot) && !is_https()) {
2414 $result->setInfo('site not https');
2415 $result->setStatus(false);
2416 return $result;
2418 return null;
2422 * Check if the site is using 64 bits PHP.
2424 * @param environment_results $result
2425 * @return environment_results|null updated results object, or null if the site is using 64 bits PHP.
2427 function check_sixtyfour_bits(environment_results $result) {
2429 if (PHP_INT_SIZE === 4) {
2430 $result->setInfo('php not 64 bits');
2431 $result->setStatus(false);
2432 return $result;
2434 return null;
2438 * Check if the igbinary extension installed is buggy one
2440 * There are a few php-igbinary versions that are buggy and
2441 * return any unserialised array with wrong index. This defeats
2442 * key() and next() operations on them.
2444 * This library is used by MUC and also by memcached and redis
2445 * when available.
2447 * Let's inform if there is some problem when:
2448 * - php 7.2 is being used (php 7.3 and up are immune).
2449 * - the igbinary extension is installed.
2450 * - the version of the extension is between 3.2.2 and 3.2.4.
2451 * - the buggy behaviour is reproduced.
2453 * @param environment_results $result object to update, if relevant.
2454 * @return environment_results|null updated results or null.
2456 function check_igbinary322_version(environment_results $result) {
2458 // No problem if using PHP version 7.3 and up.
2459 $phpversion = normalize_version(phpversion());
2460 if (version_compare($phpversion, '7.3', '>=')) {
2461 return null;
2464 // No problem if igbinary is not installed..
2465 if (!function_exists('igbinary_serialize')) {
2466 return null;
2469 // No problem if using igbinary < 3.2.2 or > 3.2.4.
2470 $igbinaryversion = normalize_version(phpversion('igbinary'));
2471 if (version_compare($igbinaryversion, '3.2.2', '<') or version_compare($igbinaryversion, '3.2.4', '>')) {
2472 return null;
2475 // Let's verify the real behaviour to see if the bug is around.
2476 // Note that we need this extra check because they released 3.2.5 with 3.2.4 version number, so
2477 // over the paper, there are 3.2.4 working versions (3.2.5 ones with messed reflection version).
2478 $data = [1, 2, 3];
2479 $data = igbinary_unserialize(igbinary_serialize($data));
2480 if (key($data) === 0) {
2481 return null;
2484 // Arrived here, we are using PHP 7.2 and a buggy verified igbinary version, let's inform and don't allow to continue.
2485 $result->setInfo('igbinary version problem');
2486 $result->setStatus(false);
2487 return $result;
2491 * Assert the upgrade key is provided, if it is defined.
2493 * The upgrade key can be defined in the main config.php as $CFG->upgradekey. If
2494 * it is defined there, then its value must be provided every time the site is
2495 * being upgraded, regardless the administrator is logged in or not.
2497 * This is supposed to be used at certain places in /admin/index.php only.
2499 * @param string|null $upgradekeyhash the SHA-1 of the value provided by the user
2501 function check_upgrade_key($upgradekeyhash) {
2502 global $CFG, $PAGE;
2504 if (isset($CFG->config_php_settings['upgradekey'])) {
2505 if ($upgradekeyhash === null or $upgradekeyhash !== sha1($CFG->config_php_settings['upgradekey'])) {
2506 if (!$PAGE->headerprinted) {
2507 $PAGE->set_title(get_string('upgradekeyreq', 'admin'));
2508 $output = $PAGE->get_renderer('core', 'admin');
2509 echo $output->upgradekey_form_page(new moodle_url('/admin/index.php', array('cache' => 0)));
2510 die();
2511 } else {
2512 // This should not happen.
2513 die('Upgrade locked');
2520 * Helper procedure/macro for installing remote plugins at admin/index.php
2522 * Does not return, always redirects or exits.
2524 * @param array $installable list of \core\update\remote_info
2525 * @param bool $confirmed false: display the validation screen, true: proceed installation
2526 * @param string $heading validation screen heading
2527 * @param moodle_url|string|null $continue URL to proceed with installation at the validation screen
2528 * @param moodle_url|string|null $return URL to go back on cancelling at the validation screen
2530 function upgrade_install_plugins(array $installable, $confirmed, $heading='', $continue=null, $return=null) {
2531 global $CFG, $PAGE;
2533 if (empty($return)) {
2534 $return = $PAGE->url;
2537 if (!empty($CFG->disableupdateautodeploy)) {
2538 redirect($return);
2541 if (empty($installable)) {
2542 redirect($return);
2545 $pluginman = core_plugin_manager::instance();
2547 if ($confirmed) {
2548 // Installation confirmed at the validation results page.
2549 if (!$pluginman->install_plugins($installable, true, true)) {
2550 throw new moodle_exception('install_plugins_failed', 'core_plugin', $return);
2553 // Always redirect to admin/index.php to perform the database upgrade.
2554 // Do not throw away the existing $PAGE->url parameters such as
2555 // confirmupgrade or confirmrelease if $PAGE->url is a superset of the
2556 // URL we must go to.
2557 $mustgoto = new moodle_url('/admin/index.php', array('cache' => 0, 'confirmplugincheck' => 0));
2558 if ($mustgoto->compare($PAGE->url, URL_MATCH_PARAMS)) {
2559 redirect($PAGE->url);
2560 } else {
2561 redirect($mustgoto);
2564 } else {
2565 $output = $PAGE->get_renderer('core', 'admin');
2566 echo $output->header();
2567 if ($heading) {
2568 echo $output->heading($heading, 3);
2570 echo html_writer::start_tag('pre', array('class' => 'plugin-install-console'));
2571 $validated = $pluginman->install_plugins($installable, false, false);
2572 echo html_writer::end_tag('pre');
2573 if ($validated) {
2574 echo $output->plugins_management_confirm_buttons($continue, $return);
2575 } else {
2576 echo $output->plugins_management_confirm_buttons(null, $return);
2578 echo $output->footer();
2579 die();
2583 * Method used to check the installed unoconv version.
2585 * @param environment_results $result object to update, if relevant.
2586 * @return environment_results|null updated results or null if unoconv path is not executable.
2588 function check_unoconv_version(environment_results $result) {
2589 global $CFG;
2591 if (!during_initial_install() && !empty($CFG->pathtounoconv) && file_is_executable(trim($CFG->pathtounoconv))) {
2592 $currentversion = 0;
2593 $supportedversion = 0.7;
2594 $unoconvbin = \escapeshellarg($CFG->pathtounoconv);
2595 $command = "$unoconvbin --version";
2596 exec($command, $output);
2598 // If the command execution returned some output, then get the unoconv version.
2599 if ($output) {
2600 foreach ($output as $response) {
2601 if (preg_match('/unoconv (\\d+\\.\\d+)/', $response, $matches)) {
2602 $currentversion = (float)$matches[1];
2607 if ($currentversion < $supportedversion) {
2608 $result->setInfo('unoconv version not supported');
2609 $result->setStatus(false);
2610 return $result;
2613 return null;
2617 * Checks for up-to-date TLS libraries. NOTE: this is not currently used, see MDL-57262.
2619 * @param environment_results $result object to update, if relevant.
2620 * @return environment_results|null updated results or null if unoconv path is not executable.
2622 function check_tls_libraries(environment_results $result) {
2623 global $CFG;
2625 if (!function_exists('curl_version')) {
2626 $result->setInfo('cURL PHP extension is not installed');
2627 $result->setStatus(false);
2628 return $result;
2631 if (!\core\upgrade\util::validate_php_curl_tls(curl_version(), PHP_ZTS)) {
2632 $result->setInfo('invalid ssl/tls configuration');
2633 $result->setStatus(false);
2634 return $result;
2637 if (!\core\upgrade\util::can_use_tls12(curl_version(), php_uname('r'))) {
2638 $result->setInfo('ssl/tls configuration not supported');
2639 $result->setStatus(false);
2640 return $result;
2643 return null;
2647 * Check if recommended version of libcurl is installed or not.
2649 * @param environment_results $result object to update, if relevant.
2650 * @return environment_results|null updated results or null.
2652 function check_libcurl_version(environment_results $result) {
2654 if (!function_exists('curl_version')) {
2655 $result->setInfo('cURL PHP extension is not installed');
2656 $result->setStatus(false);
2657 return $result;
2660 // Supported version and version number.
2661 $supportedversion = 0x071304;
2662 $supportedversionstring = "7.19.4";
2664 // Installed version.
2665 $curlinfo = curl_version();
2666 $currentversion = $curlinfo['version_number'];
2668 if ($currentversion < $supportedversion) {
2669 // Test fail.
2670 // Set info, we want to let user know how to resolve the problem.
2671 $result->setInfo('Libcurl version check');
2672 $result->setNeededVersion($supportedversionstring);
2673 $result->setCurrentVersion($curlinfo['version']);
2674 $result->setStatus(false);
2675 return $result;
2678 return null;
2682 * Environment check for the php setting max_input_vars
2684 * @param environment_results $result
2685 * @return environment_results|null
2687 function check_max_input_vars(environment_results $result) {
2688 $max = (int)ini_get('max_input_vars');
2689 if ($max < 5000) {
2690 $result->setInfo('max_input_vars');
2691 $result->setStatus(false);
2692 if (PHP_VERSION_ID >= 80000) {
2693 // For PHP8 this check is required.
2694 $result->setLevel('required');
2695 $result->setFeedbackStr('settingmaxinputvarsrequired');
2696 } else {
2697 // For PHP7 this check is optional (recommended).
2698 $result->setFeedbackStr('settingmaxinputvars');
2700 return $result;
2702 return null;
2706 * Check whether the admin directory has been configured and warn if so.
2708 * The admin directory has been deprecated since Moodle 4.0.
2710 * @param environment_results $result
2711 * @return null|environment_results
2713 function check_admin_dir_usage(environment_results $result): ?environment_results {
2714 global $CFG;
2716 if (empty($CFG->admin)) {
2717 return null;
2720 if ($CFG->admin === 'admin') {
2721 return null;
2724 $result->setInfo('admin_dir_usage');
2725 $result->setStatus(false);
2727 return $result;