Merge branch 'MDL-32657-master-1' of git://git.luns.net.uk/moodle
[moodle.git] / lib / upgradelib.php
blob6e6e0298b759e7cf390b9ed120201ce65a7e108f
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Various upgrade/install related functions and classes.
21 * @package core
22 * @subpackage upgrade
23 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
29 /** UPGRADE_LOG_NORMAL = 0 */
30 define('UPGRADE_LOG_NORMAL', 0);
31 /** UPGRADE_LOG_NOTICE = 1 */
32 define('UPGRADE_LOG_NOTICE', 1);
33 /** UPGRADE_LOG_ERROR = 2 */
34 define('UPGRADE_LOG_ERROR', 2);
36 /**
37 * Exception indicating unknown error during upgrade.
39 * @package core
40 * @subpackage upgrade
41 * @copyright 2009 Petr Skoda {@link http://skodak.org}
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44 class upgrade_exception extends moodle_exception {
45 function __construct($plugin, $version, $debuginfo=NULL) {
46 global $CFG;
47 $a = (object)array('plugin'=>$plugin, 'version'=>$version);
48 parent::__construct('upgradeerror', 'admin', "$CFG->wwwroot/$CFG->admin/index.php", $a, $debuginfo);
52 /**
53 * Exception indicating downgrade error during upgrade.
55 * @package core
56 * @subpackage upgrade
57 * @copyright 2009 Petr Skoda {@link http://skodak.org}
58 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
60 class downgrade_exception extends moodle_exception {
61 function __construct($plugin, $oldversion, $newversion) {
62 global $CFG;
63 $plugin = is_null($plugin) ? 'moodle' : $plugin;
64 $a = (object)array('plugin'=>$plugin, 'oldversion'=>$oldversion, 'newversion'=>$newversion);
65 parent::__construct('cannotdowngrade', 'debug', "$CFG->wwwroot/$CFG->admin/index.php", $a);
69 /**
70 * @package core
71 * @subpackage upgrade
72 * @copyright 2009 Petr Skoda {@link http://skodak.org}
73 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
75 class upgrade_requires_exception extends moodle_exception {
76 function __construct($plugin, $pluginversion, $currentmoodle, $requiremoodle) {
77 global $CFG;
78 $a = new stdClass();
79 $a->pluginname = $plugin;
80 $a->pluginversion = $pluginversion;
81 $a->currentmoodle = $currentmoodle;
82 $a->requiremoodle = $requiremoodle;
83 parent::__construct('pluginrequirementsnotmet', 'error', "$CFG->wwwroot/$CFG->admin/index.php", $a);
87 /**
88 * @package core
89 * @subpackage upgrade
90 * @copyright 2009 Petr Skoda {@link http://skodak.org}
91 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
93 class plugin_defective_exception extends moodle_exception {
94 function __construct($plugin, $details) {
95 global $CFG;
96 parent::__construct('detectedbrokenplugin', 'error', "$CFG->wwwroot/$CFG->admin/index.php", $plugin, $details);
101 * Sets maximum expected time needed for upgrade task.
102 * Please always make sure that upgrade will not run longer!
104 * The script may be automatically aborted if upgrade times out.
106 * @category upgrade
107 * @param int $max_execution_time in seconds (can not be less than 60 s)
109 function upgrade_set_timeout($max_execution_time=300) {
110 global $CFG;
112 if (!isset($CFG->upgraderunning) or $CFG->upgraderunning < time()) {
113 $upgraderunning = get_config(null, 'upgraderunning');
114 } else {
115 $upgraderunning = $CFG->upgraderunning;
118 if (!$upgraderunning) {
119 if (CLI_SCRIPT) {
120 // never stop CLI upgrades
121 $upgraderunning = 0;
122 } else {
123 // web upgrade not running or aborted
124 print_error('upgradetimedout', 'admin', "$CFG->wwwroot/$CFG->admin/");
128 if ($max_execution_time < 60) {
129 // protection against 0 here
130 $max_execution_time = 60;
133 $expected_end = time() + $max_execution_time;
135 if ($expected_end < $upgraderunning + 10 and $expected_end > $upgraderunning - 10) {
136 // no need to store new end, it is nearly the same ;-)
137 return;
140 if (CLI_SCRIPT) {
141 // there is no point in timing out of CLI scripts, admins can stop them if necessary
142 set_time_limit(0);
143 } else {
144 set_time_limit($max_execution_time);
146 set_config('upgraderunning', $expected_end); // keep upgrade locked until this time
150 * Upgrade savepoint, marks end of each upgrade block.
151 * It stores new main version, resets upgrade timeout
152 * and abort upgrade if user cancels page loading.
154 * Please do not make large upgrade blocks with lots of operations,
155 * for example when adding tables keep only one table operation per block.
157 * @category upgrade
158 * @param bool $result false if upgrade step failed, true if completed
159 * @param string or float $version main version
160 * @param bool $allowabort allow user to abort script execution here
161 * @return void
163 function upgrade_main_savepoint($result, $version, $allowabort=true) {
164 global $CFG;
166 //sanity check to avoid confusion with upgrade_mod_savepoint usage.
167 if (!is_bool($allowabort)) {
168 $errormessage = 'Parameter type mismatch. Are you mixing up upgrade_main_savepoint() and upgrade_mod_savepoint()?';
169 throw new coding_exception($errormessage);
172 if (!$result) {
173 throw new upgrade_exception(null, $version);
176 if ($CFG->version >= $version) {
177 // something really wrong is going on in main upgrade script
178 throw new downgrade_exception(null, $CFG->version, $version);
181 set_config('version', $version);
182 upgrade_log(UPGRADE_LOG_NORMAL, null, 'Upgrade savepoint reached');
184 // reset upgrade timeout to default
185 upgrade_set_timeout();
187 // this is a safe place to stop upgrades if user aborts page loading
188 if ($allowabort and connection_aborted()) {
189 die;
194 * Module upgrade savepoint, marks end of module upgrade blocks
195 * It stores module version, resets upgrade timeout
196 * and abort upgrade if user cancels page loading.
198 * @category upgrade
199 * @param bool $result false if upgrade step failed, true if completed
200 * @param string or float $version main version
201 * @param string $modname name of module
202 * @param bool $allowabort allow user to abort script execution here
203 * @return void
205 function upgrade_mod_savepoint($result, $version, $modname, $allowabort=true) {
206 global $DB;
208 if (!$result) {
209 throw new upgrade_exception("mod_$modname", $version);
212 if (!$module = $DB->get_record('modules', array('name'=>$modname))) {
213 print_error('modulenotexist', 'debug', '', $modname);
216 if ($module->version >= $version) {
217 // something really wrong is going on in upgrade script
218 throw new downgrade_exception("mod_$modname", $module->version, $version);
220 $module->version = $version;
221 $DB->update_record('modules', $module);
222 upgrade_log(UPGRADE_LOG_NORMAL, "mod_$modname", 'Upgrade savepoint reached');
224 // reset upgrade timeout to default
225 upgrade_set_timeout();
227 // this is a safe place to stop upgrades if user aborts page loading
228 if ($allowabort and connection_aborted()) {
229 die;
234 * Blocks upgrade savepoint, marks end of blocks upgrade blocks
235 * It stores block version, resets upgrade timeout
236 * and abort upgrade if user cancels page loading.
238 * @category upgrade
239 * @param bool $result false if upgrade step failed, true if completed
240 * @param string or float $version main version
241 * @param string $blockname name of block
242 * @param bool $allowabort allow user to abort script execution here
243 * @return void
245 function upgrade_block_savepoint($result, $version, $blockname, $allowabort=true) {
246 global $DB;
248 if (!$result) {
249 throw new upgrade_exception("block_$blockname", $version);
252 if (!$block = $DB->get_record('block', array('name'=>$blockname))) {
253 print_error('blocknotexist', 'debug', '', $blockname);
256 if ($block->version >= $version) {
257 // something really wrong is going on in upgrade script
258 throw new downgrade_exception("block_$blockname", $block->version, $version);
260 $block->version = $version;
261 $DB->update_record('block', $block);
262 upgrade_log(UPGRADE_LOG_NORMAL, "block_$blockname", 'Upgrade savepoint reached');
264 // reset upgrade timeout to default
265 upgrade_set_timeout();
267 // this is a safe place to stop upgrades if user aborts page loading
268 if ($allowabort and connection_aborted()) {
269 die;
274 * Plugins upgrade savepoint, marks end of blocks upgrade blocks
275 * It stores plugin version, resets upgrade timeout
276 * and abort upgrade if user cancels page loading.
278 * @category upgrade
279 * @param bool $result false if upgrade step failed, true if completed
280 * @param string or float $version main version
281 * @param string $type name of plugin
282 * @param string $dir location of plugin
283 * @param bool $allowabort allow user to abort script execution here
284 * @return void
286 function upgrade_plugin_savepoint($result, $version, $type, $plugin, $allowabort=true) {
287 $component = $type.'_'.$plugin;
289 if (!$result) {
290 throw new upgrade_exception($component, $version);
293 $installedversion = get_config($component, 'version');
294 if ($installedversion >= $version) {
295 // Something really wrong is going on in the upgrade script
296 throw new downgrade_exception($component, $installedversion, $version);
298 set_config('version', $version, $component);
299 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
301 // Reset upgrade timeout to default
302 upgrade_set_timeout();
304 // This is a safe place to stop upgrades if user aborts page loading
305 if ($allowabort and connection_aborted()) {
306 die;
311 * Detect if there are leftovers in PHP source files.
313 * During main version upgrades administrators MUST move away
314 * old PHP source files and start from scratch (or better
315 * use git).
317 * @return bool true means borked upgrade, false means previous PHP files were properly removed
319 function upgrade_stale_php_files_present() {
320 global $CFG;
322 $someexamplesofremovedfiles = array(
323 // removed in 2.3dev
324 '/lib/minify/builder/',
325 // removed in 2.2dev
326 '/lib/yui/3.4.1pr1/',
327 // removed in 2.2
328 '/search/cron_php5.php',
329 '/course/report/log/indexlive.php',
330 '/admin/report/backups/index.php',
331 '/admin/generator.php',
332 // removed in 2.1
333 '/lib/yui/2.8.0r4/',
334 // removed in 2.0
335 '/blocks/admin/block_admin.php',
336 '/blocks/admin_tree/block_admin_tree.php',
339 foreach ($someexamplesofremovedfiles as $file) {
340 if (file_exists($CFG->dirroot.$file)) {
341 return true;
345 return false;
349 * Upgrade plugins
350 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
351 * return void
353 function upgrade_plugins($type, $startcallback, $endcallback, $verbose) {
354 global $CFG, $DB;
356 /// special cases
357 if ($type === 'mod') {
358 return upgrade_plugins_modules($startcallback, $endcallback, $verbose);
359 } else if ($type === 'block') {
360 return upgrade_plugins_blocks($startcallback, $endcallback, $verbose);
363 $plugs = get_plugin_list($type);
365 foreach ($plugs as $plug=>$fullplug) {
366 // Reset time so that it works when installing a large number of plugins
367 set_time_limit(600);
368 $component = clean_param($type.'_'.$plug, PARAM_COMPONENT); // standardised plugin name
370 // check plugin dir is valid name
371 if (empty($component)) {
372 throw new plugin_defective_exception($type.'_'.$plug, 'Invalid plugin directory name.');
375 if (!is_readable($fullplug.'/version.php')) {
376 continue;
379 $plugin = new stdClass();
380 require($fullplug.'/version.php'); // defines $plugin with version etc
382 // if plugin tells us it's full name we may check the location
383 if (isset($plugin->component)) {
384 if ($plugin->component !== $component) {
385 throw new plugin_defective_exception($component, 'Plugin installed in wrong folder.');
389 if (empty($plugin->version)) {
390 throw new plugin_defective_exception($component, 'Missing version value in version.php');
393 $plugin->name = $plug;
394 $plugin->fullname = $component;
397 if (!empty($plugin->requires)) {
398 if ($plugin->requires > $CFG->version) {
399 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
400 } else if ($plugin->requires < 2010000000) {
401 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
405 // try to recover from interrupted install.php if needed
406 if (file_exists($fullplug.'/db/install.php')) {
407 if (get_config($plugin->fullname, 'installrunning')) {
408 require_once($fullplug.'/db/install.php');
409 $recover_install_function = 'xmldb_'.$plugin->fullname.'_install_recovery';
410 if (function_exists($recover_install_function)) {
411 $startcallback($component, true, $verbose);
412 $recover_install_function();
413 unset_config('installrunning', $plugin->fullname);
414 update_capabilities($component);
415 log_update_descriptions($component);
416 external_update_descriptions($component);
417 events_update_definition($component);
418 message_update_providers($component);
419 if ($type === 'message') {
420 message_update_processors($plug);
422 upgrade_plugin_mnet_functions($component);
423 $endcallback($component, true, $verbose);
428 $installedversion = get_config($plugin->fullname, 'version');
429 if (empty($installedversion)) { // new installation
430 $startcallback($component, true, $verbose);
432 /// Install tables if defined
433 if (file_exists($fullplug.'/db/install.xml')) {
434 $DB->get_manager()->install_from_xmldb_file($fullplug.'/db/install.xml');
437 /// store version
438 upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false);
440 /// execute post install file
441 if (file_exists($fullplug.'/db/install.php')) {
442 require_once($fullplug.'/db/install.php');
443 set_config('installrunning', 1, $plugin->fullname);
444 $post_install_function = 'xmldb_'.$plugin->fullname.'_install';
445 $post_install_function();
446 unset_config('installrunning', $plugin->fullname);
449 /// Install various components
450 update_capabilities($component);
451 log_update_descriptions($component);
452 external_update_descriptions($component);
453 events_update_definition($component);
454 message_update_providers($component);
455 if ($type === 'message') {
456 message_update_processors($plug);
458 upgrade_plugin_mnet_functions($component);
460 purge_all_caches();
461 $endcallback($component, true, $verbose);
463 } else if ($installedversion < $plugin->version) { // upgrade
464 /// Run the upgrade function for the plugin.
465 $startcallback($component, false, $verbose);
467 if (is_readable($fullplug.'/db/upgrade.php')) {
468 require_once($fullplug.'/db/upgrade.php'); // defines upgrading function
470 $newupgrade_function = 'xmldb_'.$plugin->fullname.'_upgrade';
471 $result = $newupgrade_function($installedversion);
472 } else {
473 $result = true;
476 $installedversion = get_config($plugin->fullname, 'version');
477 if ($installedversion < $plugin->version) {
478 // store version if not already there
479 upgrade_plugin_savepoint($result, $plugin->version, $type, $plug, false);
482 /// Upgrade various components
483 update_capabilities($component);
484 log_update_descriptions($component);
485 external_update_descriptions($component);
486 events_update_definition($component);
487 message_update_providers($component);
488 if ($type === 'message') {
489 message_update_processors($plug);
491 upgrade_plugin_mnet_functions($component);
493 purge_all_caches();
494 $endcallback($component, false, $verbose);
496 } else if ($installedversion > $plugin->version) {
497 throw new downgrade_exception($component, $installedversion, $plugin->version);
503 * Find and check all modules and load them up or upgrade them if necessary
505 * @global object
506 * @global object
508 function upgrade_plugins_modules($startcallback, $endcallback, $verbose) {
509 global $CFG, $DB;
511 $mods = get_plugin_list('mod');
513 foreach ($mods as $mod=>$fullmod) {
515 if ($mod === 'NEWMODULE') { // Someone has unzipped the template, ignore it
516 continue;
519 $component = clean_param('mod_'.$mod, PARAM_COMPONENT);
521 // check module dir is valid name
522 if (empty($component)) {
523 throw new plugin_defective_exception('mod_'.$mod, 'Invalid plugin directory name.');
526 if (!is_readable($fullmod.'/version.php')) {
527 throw new plugin_defective_exception($component, 'Missing version.php');
530 $module = new stdClass();
531 require($fullmod .'/version.php'); // defines $module with version etc
533 // if plugin tells us it's full name we may check the location
534 if (isset($module->component)) {
535 if ($module->component !== $component) {
536 throw new plugin_defective_exception($component, 'Plugin installed in wrong folder.');
540 if (empty($module->version)) {
541 if (isset($module->version)) {
542 // Version is empty but is set - it means its value is 0 or ''. Let us skip such module.
543 // This is intended for developers so they can work on the early stages of the module.
544 continue;
546 throw new plugin_defective_exception($component, 'Missing version value in version.php');
549 if (!empty($module->requires)) {
550 if ($module->requires > $CFG->version) {
551 throw new upgrade_requires_exception($component, $module->version, $CFG->version, $module->requires);
552 } else if ($module->requires < 2010000000) {
553 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
557 if (empty($module->cron)) {
558 $module->cron = 0;
561 // all modules must have en lang pack
562 if (!is_readable("$fullmod/lang/en/$mod.php")) {
563 throw new plugin_defective_exception($component, 'Missing mandatory en language pack.');
566 $module->name = $mod; // The name MUST match the directory
568 $currmodule = $DB->get_record('modules', array('name'=>$module->name));
570 if (file_exists($fullmod.'/db/install.php')) {
571 if (get_config($module->name, 'installrunning')) {
572 require_once($fullmod.'/db/install.php');
573 $recover_install_function = 'xmldb_'.$module->name.'_install_recovery';
574 if (function_exists($recover_install_function)) {
575 $startcallback($component, true, $verbose);
576 $recover_install_function();
577 unset_config('installrunning', $module->name);
578 // Install various components too
579 update_capabilities($component);
580 log_update_descriptions($component);
581 external_update_descriptions($component);
582 events_update_definition($component);
583 message_update_providers($component);
584 upgrade_plugin_mnet_functions($component);
585 $endcallback($component, true, $verbose);
590 if (empty($currmodule->version)) {
591 $startcallback($component, true, $verbose);
593 /// Execute install.xml (XMLDB) - must be present in all modules
594 $DB->get_manager()->install_from_xmldb_file($fullmod.'/db/install.xml');
596 /// Add record into modules table - may be needed in install.php already
597 $module->id = $DB->insert_record('modules', $module);
599 /// Post installation hook - optional
600 if (file_exists("$fullmod/db/install.php")) {
601 require_once("$fullmod/db/install.php");
602 // Set installation running flag, we need to recover after exception or error
603 set_config('installrunning', 1, $module->name);
604 $post_install_function = 'xmldb_'.$module->name.'_install';
605 $post_install_function();
606 unset_config('installrunning', $module->name);
609 /// Install various components
610 update_capabilities($component);
611 log_update_descriptions($component);
612 external_update_descriptions($component);
613 events_update_definition($component);
614 message_update_providers($component);
615 upgrade_plugin_mnet_functions($component);
617 purge_all_caches();
618 $endcallback($component, true, $verbose);
620 } else if ($currmodule->version < $module->version) {
621 /// If versions say that we need to upgrade but no upgrade files are available, notify and continue
622 $startcallback($component, false, $verbose);
624 if (is_readable($fullmod.'/db/upgrade.php')) {
625 require_once($fullmod.'/db/upgrade.php'); // defines new upgrading function
626 $newupgrade_function = 'xmldb_'.$module->name.'_upgrade';
627 $result = $newupgrade_function($currmodule->version, $module);
628 } else {
629 $result = true;
632 $currmodule = $DB->get_record('modules', array('name'=>$module->name));
633 if ($currmodule->version < $module->version) {
634 // store version if not already there
635 upgrade_mod_savepoint($result, $module->version, $mod, false);
638 // update cron flag if needed
639 if ($currmodule->cron != $module->cron) {
640 $DB->set_field('modules', 'cron', $module->cron, array('name' => $module->name));
643 // Upgrade various components
644 update_capabilities($component);
645 log_update_descriptions($component);
646 external_update_descriptions($component);
647 events_update_definition($component);
648 message_update_providers($component);
649 upgrade_plugin_mnet_functions($component);
651 purge_all_caches();
653 $endcallback($component, false, $verbose);
655 } else if ($currmodule->version > $module->version) {
656 throw new downgrade_exception($component, $currmodule->version, $module->version);
663 * This function finds all available blocks and install them
664 * into blocks table or do all the upgrade process if newer.
666 * @global object
667 * @global object
669 function upgrade_plugins_blocks($startcallback, $endcallback, $verbose) {
670 global $CFG, $DB;
672 require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
674 $blocktitles = array(); // we do not want duplicate titles
676 //Is this a first install
677 $first_install = null;
679 $blocks = get_plugin_list('block');
681 foreach ($blocks as $blockname=>$fullblock) {
683 if (is_null($first_install)) {
684 $first_install = ($DB->count_records('block_instances') == 0);
687 if ($blockname === 'NEWBLOCK') { // Someone has unzipped the template, ignore it
688 continue;
691 $component = clean_param('block_'.$blockname, PARAM_COMPONENT);
693 // check block dir is valid name
694 if (empty($component)) {
695 throw new plugin_defective_exception('block_'.$blockname, 'Invalid plugin directory name.');
698 if (!is_readable($fullblock.'/version.php')) {
699 throw new plugin_defective_exception('block/'.$blockname, 'Missing version.php file.');
701 $plugin = new stdClass();
702 $plugin->version = NULL;
703 $plugin->cron = 0;
704 include($fullblock.'/version.php');
705 $block = $plugin;
707 // if plugin tells us it's full name we may check the location
708 if (isset($block->component)) {
709 if ($block->component !== $component) {
710 throw new plugin_defective_exception($component, 'Plugin installed in wrong folder.');
714 if (!empty($plugin->requires)) {
715 if ($plugin->requires > $CFG->version) {
716 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
717 } else if ($plugin->requires < 2010000000) {
718 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
722 if (!is_readable($fullblock.'/block_'.$blockname.'.php')) {
723 throw new plugin_defective_exception('block/'.$blockname, 'Missing main block class file.');
725 include_once($fullblock.'/block_'.$blockname.'.php');
727 $classname = 'block_'.$blockname;
729 if (!class_exists($classname)) {
730 throw new plugin_defective_exception($component, 'Can not load main class.');
733 $blockobj = new $classname; // This is what we'll be testing
734 $blocktitle = $blockobj->get_title();
736 // OK, it's as we all hoped. For further tests, the object will do them itself.
737 if (!$blockobj->_self_test()) {
738 throw new plugin_defective_exception($component, 'Self test failed.');
741 $block->name = $blockname; // The name MUST match the directory
743 if (empty($block->version)) {
744 throw new plugin_defective_exception($component, 'Missing block version.');
747 $currblock = $DB->get_record('block', array('name'=>$block->name));
749 if (file_exists($fullblock.'/db/install.php')) {
750 if (get_config('block_'.$blockname, 'installrunning')) {
751 require_once($fullblock.'/db/install.php');
752 $recover_install_function = 'xmldb_block_'.$blockname.'_install_recovery';
753 if (function_exists($recover_install_function)) {
754 $startcallback($component, true, $verbose);
755 $recover_install_function();
756 unset_config('installrunning', 'block_'.$blockname);
757 // Install various components
758 update_capabilities($component);
759 log_update_descriptions($component);
760 external_update_descriptions($component);
761 events_update_definition($component);
762 message_update_providers($component);
763 upgrade_plugin_mnet_functions($component);
764 $endcallback($component, true, $verbose);
769 if (empty($currblock->version)) { // block not installed yet, so install it
770 $conflictblock = array_search($blocktitle, $blocktitles);
771 if ($conflictblock !== false) {
772 // Duplicate block titles are not allowed, they confuse people
773 // AND PHP's associative arrays ;)
774 throw new plugin_defective_exception($component, get_string('blocknameconflict', 'error', (object)array('name'=>$block->name, 'conflict'=>$conflictblock)));
776 $startcallback($component, true, $verbose);
778 if (file_exists($fullblock.'/db/install.xml')) {
779 $DB->get_manager()->install_from_xmldb_file($fullblock.'/db/install.xml');
781 $block->id = $DB->insert_record('block', $block);
783 if (file_exists($fullblock.'/db/install.php')) {
784 require_once($fullblock.'/db/install.php');
785 // Set installation running flag, we need to recover after exception or error
786 set_config('installrunning', 1, 'block_'.$blockname);
787 $post_install_function = 'xmldb_block_'.$blockname.'_install';;
788 $post_install_function();
789 unset_config('installrunning', 'block_'.$blockname);
792 $blocktitles[$block->name] = $blocktitle;
794 // Install various components
795 update_capabilities($component);
796 log_update_descriptions($component);
797 external_update_descriptions($component);
798 events_update_definition($component);
799 message_update_providers($component);
800 upgrade_plugin_mnet_functions($component);
802 purge_all_caches();
803 $endcallback($component, true, $verbose);
805 } else if ($currblock->version < $block->version) {
806 $startcallback($component, false, $verbose);
808 if (is_readable($fullblock.'/db/upgrade.php')) {
809 require_once($fullblock.'/db/upgrade.php'); // defines new upgrading function
810 $newupgrade_function = 'xmldb_block_'.$blockname.'_upgrade';
811 $result = $newupgrade_function($currblock->version, $block);
812 } else {
813 $result = true;
816 $currblock = $DB->get_record('block', array('name'=>$block->name));
817 if ($currblock->version < $block->version) {
818 // store version if not already there
819 upgrade_block_savepoint($result, $block->version, $block->name, false);
822 if ($currblock->cron != $block->cron) {
823 // update cron flag if needed
824 $currblock->cron = $block->cron;
825 $DB->update_record('block', $currblock);
828 // Upgrade various components
829 update_capabilities($component);
830 log_update_descriptions($component);
831 external_update_descriptions($component);
832 events_update_definition($component);
833 message_update_providers($component);
834 upgrade_plugin_mnet_functions($component);
836 purge_all_caches();
837 $endcallback($component, false, $verbose);
839 } else if ($currblock->version > $block->version) {
840 throw new downgrade_exception($component, $currblock->version, $block->version);
845 // Finally, if we are in the first_install of BLOCKS setup frontpage and admin page blocks
846 if ($first_install) {
847 //Iterate over each course - there should be only site course here now
848 if ($courses = $DB->get_records('course')) {
849 foreach ($courses as $course) {
850 blocks_add_default_course_blocks($course);
854 blocks_add_default_system_blocks();
860 * Log_display description function used during install and upgrade.
862 * @param string $component name of component (moodle, mod_assignment, etc.)
863 * @return void
865 function log_update_descriptions($component) {
866 global $DB;
868 $defpath = get_component_directory($component).'/db/log.php';
870 if (!file_exists($defpath)) {
871 $DB->delete_records('log_display', array('component'=>$component));
872 return;
875 // load new info
876 $logs = array();
877 include($defpath);
878 $newlogs = array();
879 foreach ($logs as $log) {
880 $newlogs[$log['module'].'-'.$log['action']] = $log; // kind of unique name
882 unset($logs);
883 $logs = $newlogs;
885 $fields = array('module', 'action', 'mtable', 'field');
886 // update all log fist
887 $dblogs = $DB->get_records('log_display', array('component'=>$component));
888 foreach ($dblogs as $dblog) {
889 $name = $dblog->module.'-'.$dblog->action;
891 if (empty($logs[$name])) {
892 $DB->delete_records('log_display', array('id'=>$dblog->id));
893 continue;
896 $log = $logs[$name];
897 unset($logs[$name]);
899 $update = false;
900 foreach ($fields as $field) {
901 if ($dblog->$field != $log[$field]) {
902 $dblog->$field = $log[$field];
903 $update = true;
906 if ($update) {
907 $DB->update_record('log_display', $dblog);
910 foreach ($logs as $log) {
911 $dblog = (object)$log;
912 $dblog->component = $component;
913 $DB->insert_record('log_display', $dblog);
918 * Web service discovery function used during install and upgrade.
919 * @param string $component name of component (moodle, mod_assignment, etc.)
920 * @return void
922 function external_update_descriptions($component) {
923 global $DB, $CFG;
925 $defpath = get_component_directory($component).'/db/services.php';
927 if (!file_exists($defpath)) {
928 require_once($CFG->dirroot.'/lib/externallib.php');
929 external_delete_descriptions($component);
930 return;
933 // load new info
934 $functions = array();
935 $services = array();
936 include($defpath);
938 // update all function fist
939 $dbfunctions = $DB->get_records('external_functions', array('component'=>$component));
940 foreach ($dbfunctions as $dbfunction) {
941 if (empty($functions[$dbfunction->name])) {
942 $DB->delete_records('external_functions', array('id'=>$dbfunction->id));
943 // do not delete functions from external_services_functions, beacuse
944 // we want to notify admins when functions used in custom services disappear
946 //TODO: this looks wrong, we have to delete it eventually (skodak)
947 continue;
950 $function = $functions[$dbfunction->name];
951 unset($functions[$dbfunction->name]);
952 $function['classpath'] = empty($function['classpath']) ? null : $function['classpath'];
954 $update = false;
955 if ($dbfunction->classname != $function['classname']) {
956 $dbfunction->classname = $function['classname'];
957 $update = true;
959 if ($dbfunction->methodname != $function['methodname']) {
960 $dbfunction->methodname = $function['methodname'];
961 $update = true;
963 if ($dbfunction->classpath != $function['classpath']) {
964 $dbfunction->classpath = $function['classpath'];
965 $update = true;
967 $functioncapabilities = key_exists('capabilities', $function)?$function['capabilities']:'';
968 if ($dbfunction->capabilities != $functioncapabilities) {
969 $dbfunction->capabilities = $functioncapabilities;
970 $update = true;
972 if ($update) {
973 $DB->update_record('external_functions', $dbfunction);
976 foreach ($functions as $fname => $function) {
977 $dbfunction = new stdClass();
978 $dbfunction->name = $fname;
979 $dbfunction->classname = $function['classname'];
980 $dbfunction->methodname = $function['methodname'];
981 $dbfunction->classpath = empty($function['classpath']) ? null : $function['classpath'];
982 $dbfunction->component = $component;
983 $dbfunction->capabilities = key_exists('capabilities', $function)?$function['capabilities']:'';
984 $dbfunction->id = $DB->insert_record('external_functions', $dbfunction);
986 unset($functions);
988 // now deal with services
989 $dbservices = $DB->get_records('external_services', array('component'=>$component));
990 foreach ($dbservices as $dbservice) {
991 if (empty($services[$dbservice->name])) {
992 $DB->delete_records('external_tokens', array('externalserviceid'=>$dbservice->id));
993 $DB->delete_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
994 $DB->delete_records('external_services_users', array('externalserviceid'=>$dbservice->id));
995 $DB->delete_records('external_services', array('id'=>$dbservice->id));
996 continue;
998 $service = $services[$dbservice->name];
999 unset($services[$dbservice->name]);
1000 $service['enabled'] = empty($service['enabled']) ? 0 : $service['enabled'];
1001 $service['requiredcapability'] = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1002 $service['restrictedusers'] = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
1003 $service['downloadfiles'] = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
1004 $service['shortname'] = !isset($service['shortname']) ? null : $service['shortname'];
1006 $update = false;
1007 if ($dbservice->requiredcapability != $service['requiredcapability']) {
1008 $dbservice->requiredcapability = $service['requiredcapability'];
1009 $update = true;
1011 if ($dbservice->restrictedusers != $service['restrictedusers']) {
1012 $dbservice->restrictedusers = $service['restrictedusers'];
1013 $update = true;
1015 if ($dbservice->downloadfiles != $service['downloadfiles']) {
1016 $dbservice->downloadfiles = $service['downloadfiles'];
1017 $update = true;
1019 //if shortname is not a PARAM_ALPHANUMEXT, fail (tested here for service update and creation)
1020 if (isset($service['shortname']) and
1021 (clean_param($service['shortname'], PARAM_ALPHANUMEXT) != $service['shortname'])) {
1022 throw new moodle_exception('installserviceshortnameerror', 'webservice', '', $service['shortname']);
1024 if ($dbservice->shortname != $service['shortname']) {
1025 //check that shortname is unique
1026 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
1027 $existingservice = $DB->get_record('external_services',
1028 array('shortname' => $service['shortname']));
1029 if (!empty($existingservice)) {
1030 throw new moodle_exception('installexistingserviceshortnameerror', 'webservice', '', $service['shortname']);
1033 $dbservice->shortname = $service['shortname'];
1034 $update = true;
1036 if ($update) {
1037 $DB->update_record('external_services', $dbservice);
1040 $functions = $DB->get_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
1041 foreach ($functions as $function) {
1042 $key = array_search($function->functionname, $service['functions']);
1043 if ($key === false) {
1044 $DB->delete_records('external_services_functions', array('id'=>$function->id));
1045 } else {
1046 unset($service['functions'][$key]);
1049 foreach ($service['functions'] as $fname) {
1050 $newf = new stdClass();
1051 $newf->externalserviceid = $dbservice->id;
1052 $newf->functionname = $fname;
1053 $DB->insert_record('external_services_functions', $newf);
1055 unset($functions);
1057 foreach ($services as $name => $service) {
1058 //check that shortname is unique
1059 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
1060 $existingservice = $DB->get_record('external_services',
1061 array('shortname' => $service['shortname']));
1062 if (!empty($existingservice)) {
1063 throw new moodle_exception('installserviceshortnameerror', 'webservice');
1067 $dbservice = new stdClass();
1068 $dbservice->name = $name;
1069 $dbservice->enabled = empty($service['enabled']) ? 0 : $service['enabled'];
1070 $dbservice->requiredcapability = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1071 $dbservice->restrictedusers = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
1072 $dbservice->downloadfiles = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
1073 $dbservice->shortname = !isset($service['shortname']) ? null : $service['shortname'];
1074 $dbservice->component = $component;
1075 $dbservice->timecreated = time();
1076 $dbservice->id = $DB->insert_record('external_services', $dbservice);
1077 foreach ($service['functions'] as $fname) {
1078 $newf = new stdClass();
1079 $newf->externalserviceid = $dbservice->id;
1080 $newf->functionname = $fname;
1081 $DB->insert_record('external_services_functions', $newf);
1087 * upgrade logging functions
1089 function upgrade_handle_exception($ex, $plugin = null) {
1090 global $CFG;
1092 // rollback everything, we need to log all upgrade problems
1093 abort_all_db_transactions();
1095 $info = get_exception_info($ex);
1097 // First log upgrade error
1098 upgrade_log(UPGRADE_LOG_ERROR, $plugin, 'Exception: ' . get_class($ex), $info->message, $info->backtrace);
1100 // Always turn on debugging - admins need to know what is going on
1101 $CFG->debug = DEBUG_DEVELOPER;
1103 default_exception_handler($ex, true, $plugin);
1107 * Adds log entry into upgrade_log table
1109 * @param int $type UPGRADE_LOG_NORMAL, UPGRADE_LOG_NOTICE or UPGRADE_LOG_ERROR
1110 * @param string $plugin frankenstyle component name
1111 * @param string $info short description text of log entry
1112 * @param string $details long problem description
1113 * @param string $backtrace string used for errors only
1114 * @return void
1116 function upgrade_log($type, $plugin, $info, $details=null, $backtrace=null) {
1117 global $DB, $USER, $CFG;
1119 if (empty($plugin)) {
1120 $plugin = 'core';
1123 list($plugintype, $pluginname) = normalize_component($plugin);
1124 $component = is_null($pluginname) ? $plugintype : $plugintype . '_' . $pluginname;
1126 $backtrace = format_backtrace($backtrace, true);
1128 $currentversion = null;
1129 $targetversion = null;
1131 //first try to find out current version number
1132 if ($plugintype === 'core') {
1133 //main
1134 $currentversion = $CFG->version;
1136 $version = null;
1137 include("$CFG->dirroot/version.php");
1138 $targetversion = $version;
1140 } else if ($plugintype === 'mod') {
1141 try {
1142 $currentversion = $DB->get_field('modules', 'version', array('name'=>$pluginname));
1143 $currentversion = ($currentversion === false) ? null : $currentversion;
1144 } catch (Exception $ignored) {
1146 $cd = get_component_directory($component);
1147 if (file_exists("$cd/version.php")) {
1148 $module = new stdClass();
1149 $module->version = null;
1150 include("$cd/version.php");
1151 $targetversion = $module->version;
1154 } else if ($plugintype === 'block') {
1155 try {
1156 if ($block = $DB->get_record('block', array('name'=>$pluginname))) {
1157 $currentversion = $block->version;
1159 } catch (Exception $ignored) {
1161 $cd = get_component_directory($component);
1162 if (file_exists("$cd/version.php")) {
1163 $plugin = new stdClass();
1164 $plugin->version = null;
1165 include("$cd/version.php");
1166 $targetversion = $plugin->version;
1169 } else {
1170 $pluginversion = get_config($component, 'version');
1171 if (!empty($pluginversion)) {
1172 $currentversion = $pluginversion;
1174 $cd = get_component_directory($component);
1175 if (file_exists("$cd/version.php")) {
1176 $plugin = new stdClass();
1177 $plugin->version = null;
1178 include("$cd/version.php");
1179 $targetversion = $plugin->version;
1183 $log = new stdClass();
1184 $log->type = $type;
1185 $log->plugin = $component;
1186 $log->version = $currentversion;
1187 $log->targetversion = $targetversion;
1188 $log->info = $info;
1189 $log->details = $details;
1190 $log->backtrace = $backtrace;
1191 $log->userid = $USER->id;
1192 $log->timemodified = time();
1193 try {
1194 $DB->insert_record('upgrade_log', $log);
1195 } catch (Exception $ignored) {
1196 // possible during install or 2.0 upgrade
1201 * Marks start of upgrade, blocks any other access to site.
1202 * The upgrade is finished at the end of script or after timeout.
1204 * @global object
1205 * @global object
1206 * @global object
1208 function upgrade_started($preinstall=false) {
1209 global $CFG, $DB, $PAGE, $OUTPUT;
1211 static $started = false;
1213 if ($preinstall) {
1214 ignore_user_abort(true);
1215 upgrade_setup_debug(true);
1217 } else if ($started) {
1218 upgrade_set_timeout(120);
1220 } else {
1221 if (!CLI_SCRIPT and !$PAGE->headerprinted) {
1222 $strupgrade = get_string('upgradingversion', 'admin');
1223 $PAGE->set_pagelayout('maintenance');
1224 upgrade_init_javascript();
1225 $PAGE->set_title($strupgrade.' - Moodle '.$CFG->target_release);
1226 $PAGE->set_heading($strupgrade);
1227 $PAGE->navbar->add($strupgrade);
1228 $PAGE->set_cacheable(false);
1229 echo $OUTPUT->header();
1232 ignore_user_abort(true);
1233 register_shutdown_function('upgrade_finished_handler');
1234 upgrade_setup_debug(true);
1235 set_config('upgraderunning', time()+300);
1236 $started = true;
1241 * Internal function - executed if upgrade interrupted.
1243 function upgrade_finished_handler() {
1244 upgrade_finished();
1248 * Indicates upgrade is finished.
1250 * This function may be called repeatedly.
1252 * @global object
1253 * @global object
1255 function upgrade_finished($continueurl=null) {
1256 global $CFG, $DB, $OUTPUT;
1258 if (!empty($CFG->upgraderunning)) {
1259 unset_config('upgraderunning');
1260 upgrade_setup_debug(false);
1261 ignore_user_abort(false);
1262 if ($continueurl) {
1263 echo $OUTPUT->continue_button($continueurl);
1264 echo $OUTPUT->footer();
1265 die;
1271 * @global object
1272 * @global object
1274 function upgrade_setup_debug($starting) {
1275 global $CFG, $DB;
1277 static $originaldebug = null;
1279 if ($starting) {
1280 if ($originaldebug === null) {
1281 $originaldebug = $DB->get_debug();
1283 if (!empty($CFG->upgradeshowsql)) {
1284 $DB->set_debug(true);
1286 } else {
1287 $DB->set_debug($originaldebug);
1291 function print_upgrade_separator() {
1292 if (!CLI_SCRIPT) {
1293 echo '<hr />';
1298 * Default start upgrade callback
1299 * @param string $plugin
1300 * @param bool $installation true if installation, false means upgrade
1302 function print_upgrade_part_start($plugin, $installation, $verbose) {
1303 global $OUTPUT;
1304 if (empty($plugin) or $plugin == 'moodle') {
1305 upgrade_started($installation); // does not store upgrade running flag yet
1306 if ($verbose) {
1307 echo $OUTPUT->heading(get_string('coresystem'));
1309 } else {
1310 upgrade_started();
1311 if ($verbose) {
1312 echo $OUTPUT->heading($plugin);
1315 if ($installation) {
1316 if (empty($plugin) or $plugin == 'moodle') {
1317 // no need to log - log table not yet there ;-)
1318 } else {
1319 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin installation');
1321 } else {
1322 if (empty($plugin) or $plugin == 'moodle') {
1323 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting core upgrade');
1324 } else {
1325 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin upgrade');
1331 * Default end upgrade callback
1332 * @param string $plugin
1333 * @param bool $installation true if installation, false means upgrade
1335 function print_upgrade_part_end($plugin, $installation, $verbose) {
1336 global $OUTPUT;
1337 upgrade_started();
1338 if ($installation) {
1339 if (empty($plugin) or $plugin == 'moodle') {
1340 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core installed');
1341 } else {
1342 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin installed');
1344 } else {
1345 if (empty($plugin) or $plugin == 'moodle') {
1346 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core upgraded');
1347 } else {
1348 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin upgraded');
1351 if ($verbose) {
1352 echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
1353 print_upgrade_separator();
1358 * Sets up JS code required for all upgrade scripts.
1359 * @global object
1361 function upgrade_init_javascript() {
1362 global $PAGE;
1363 // scroll to the end of each upgrade page so that ppl see either error or continue button,
1364 // no need to scroll continuously any more, it is enough to jump to end once the footer is printed ;-)
1365 $js = "window.scrollTo(0, 5000000);";
1366 $PAGE->requires->js_init_code($js);
1370 * Try to upgrade the given language pack (or current language)
1372 * @param string $lang the code of the language to update, defaults to the current language
1374 function upgrade_language_pack($lang = null) {
1375 global $CFG;
1377 if (!empty($CFG->skiplangupgrade)) {
1378 return;
1381 if (!file_exists("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php")) {
1382 // weird, somebody uninstalled the import utility
1383 return;
1386 if (!$lang) {
1387 $lang = current_language();
1390 if (!get_string_manager()->translation_exists($lang)) {
1391 return;
1394 get_string_manager()->reset_caches();
1396 if ($lang === 'en') {
1397 return; // Nothing to do
1400 upgrade_started(false);
1402 require_once("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php");
1403 tool_langimport_preupgrade_update($lang);
1405 get_string_manager()->reset_caches();
1407 print_upgrade_separator();
1411 * Install core moodle tables and initialize
1412 * @param float $version target version
1413 * @param bool $verbose
1414 * @return void, may throw exception
1416 function install_core($version, $verbose) {
1417 global $CFG, $DB;
1419 try {
1420 set_time_limit(600);
1421 print_upgrade_part_start('moodle', true, $verbose); // does not store upgrade running flag
1423 $DB->get_manager()->install_from_xmldb_file("$CFG->libdir/db/install.xml");
1424 upgrade_started(); // we want the flag to be stored in config table ;-)
1426 // set all core default records and default settings
1427 require_once("$CFG->libdir/db/install.php");
1428 xmldb_main_install(); // installs the capabilities too
1430 // store version
1431 upgrade_main_savepoint(true, $version, false);
1433 // Continue with the installation
1434 log_update_descriptions('moodle');
1435 external_update_descriptions('moodle');
1436 events_update_definition('moodle');
1437 message_update_providers('moodle');
1439 // Write default settings unconditionally
1440 admin_apply_default_settings(NULL, true);
1442 print_upgrade_part_end(null, true, $verbose);
1443 } catch (exception $ex) {
1444 upgrade_handle_exception($ex);
1449 * Upgrade moodle core
1450 * @param float $version target version
1451 * @param bool $verbose
1452 * @return void, may throw exception
1454 function upgrade_core($version, $verbose) {
1455 global $CFG;
1457 raise_memory_limit(MEMORY_EXTRA);
1459 require_once($CFG->libdir.'/db/upgrade.php'); // Defines upgrades
1461 try {
1462 // Reset caches before any output
1463 purge_all_caches();
1465 // Upgrade current language pack if we can
1466 upgrade_language_pack();
1468 print_upgrade_part_start('moodle', false, $verbose);
1470 // one time special local migration pre 2.0 upgrade script
1471 if ($CFG->version < 2007101600) {
1472 $pre20upgradefile = "$CFG->dirroot/local/upgrade_pre20.php";
1473 if (file_exists($pre20upgradefile)) {
1474 set_time_limit(0);
1475 require($pre20upgradefile);
1476 // reset upgrade timeout to default
1477 upgrade_set_timeout();
1481 $result = xmldb_main_upgrade($CFG->version);
1482 if ($version > $CFG->version) {
1483 // store version if not already there
1484 upgrade_main_savepoint($result, $version, false);
1487 // perform all other component upgrade routines
1488 update_capabilities('moodle');
1489 log_update_descriptions('moodle');
1490 external_update_descriptions('moodle');
1491 events_update_definition('moodle');
1492 message_update_providers('moodle');
1494 // Reset caches again, just to be sure
1495 purge_all_caches();
1497 // Clean up contexts - more and more stuff depends on existence of paths and contexts
1498 context_helper::cleanup_instances();
1499 context_helper::create_instances(null, false);
1500 context_helper::build_all_paths(false);
1501 $syscontext = context_system::instance();
1502 $syscontext->mark_dirty();
1504 print_upgrade_part_end('moodle', false, $verbose);
1505 } catch (Exception $ex) {
1506 upgrade_handle_exception($ex);
1511 * Upgrade/install other parts of moodle
1512 * @param bool $verbose
1513 * @return void, may throw exception
1515 function upgrade_noncore($verbose) {
1516 global $CFG;
1518 raise_memory_limit(MEMORY_EXTRA);
1520 // upgrade all plugins types
1521 try {
1522 $plugintypes = get_plugin_types();
1523 foreach ($plugintypes as $type=>$location) {
1524 upgrade_plugins($type, 'print_upgrade_part_start', 'print_upgrade_part_end', $verbose);
1526 } catch (Exception $ex) {
1527 upgrade_handle_exception($ex);
1532 * Checks if the main tables have been installed yet or not.
1533 * @return bool
1535 function core_tables_exist() {
1536 global $DB;
1538 if (!$tables = $DB->get_tables() ) { // No tables yet at all.
1539 return false;
1541 } else { // Check for missing main tables
1542 $mtables = array('config', 'course', 'groupings'); // some tables used in 1.9 and 2.0, preferable something from the start and end of install.xml
1543 foreach ($mtables as $mtable) {
1544 if (!in_array($mtable, $tables)) {
1545 return false;
1548 return true;
1553 * upgrades the mnet rpc definitions for the given component.
1554 * this method doesn't return status, an exception will be thrown in the case of an error
1556 * @param string $component the plugin to upgrade, eg auth_mnet
1558 function upgrade_plugin_mnet_functions($component) {
1559 global $DB, $CFG;
1561 list($type, $plugin) = explode('_', $component);
1562 $path = get_plugin_directory($type, $plugin);
1564 $publishes = array();
1565 $subscribes = array();
1566 if (file_exists($path . '/db/mnet.php')) {
1567 require_once($path . '/db/mnet.php'); // $publishes comes from this file
1569 if (empty($publishes)) {
1570 $publishes = array(); // still need this to be able to disable stuff later
1572 if (empty($subscribes)) {
1573 $subscribes = array(); // still need this to be able to disable stuff later
1576 static $servicecache = array();
1578 // rekey an array based on the rpc method for easy lookups later
1579 $publishmethodservices = array();
1580 $subscribemethodservices = array();
1581 foreach($publishes as $servicename => $service) {
1582 if (is_array($service['methods'])) {
1583 foreach($service['methods'] as $methodname) {
1584 $service['servicename'] = $servicename;
1585 $publishmethodservices[$methodname][] = $service;
1590 // Disable functions that don't exist (any more) in the source
1591 // Should these be deleted? What about their permissions records?
1592 foreach ($DB->get_records('mnet_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
1593 if (!array_key_exists($rpc->functionname, $publishmethodservices) && $rpc->enabled) {
1594 $DB->set_field('mnet_rpc', 'enabled', 0, array('id' => $rpc->id));
1595 } else if (array_key_exists($rpc->functionname, $publishmethodservices) && !$rpc->enabled) {
1596 $DB->set_field('mnet_rpc', 'enabled', 1, array('id' => $rpc->id));
1600 // reflect all the services we're publishing and save them
1601 require_once($CFG->dirroot . '/lib/zend/Zend/Server/Reflection.php');
1602 static $cachedclasses = array(); // to store reflection information in
1603 foreach ($publishes as $service => $data) {
1604 $f = $data['filename'];
1605 $c = $data['classname'];
1606 foreach ($data['methods'] as $method) {
1607 $dataobject = new stdClass();
1608 $dataobject->plugintype = $type;
1609 $dataobject->pluginname = $plugin;
1610 $dataobject->enabled = 1;
1611 $dataobject->classname = $c;
1612 $dataobject->filename = $f;
1614 if (is_string($method)) {
1615 $dataobject->functionname = $method;
1617 } else if (is_array($method)) { // wants to override file or class
1618 $dataobject->functionname = $method['method'];
1619 $dataobject->classname = $method['classname'];
1620 $dataobject->filename = $method['filename'];
1622 $dataobject->xmlrpcpath = $type.'/'.$plugin.'/'.$dataobject->filename.'/'.$method;
1623 $dataobject->static = false;
1625 require_once($path . '/' . $dataobject->filename);
1626 $functionreflect = null; // slightly different ways to get this depending on whether it's a class method or a function
1627 if (!empty($dataobject->classname)) {
1628 if (!class_exists($dataobject->classname)) {
1629 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
1631 $key = $dataobject->filename . '|' . $dataobject->classname;
1632 if (!array_key_exists($key, $cachedclasses)) { // look to see if we've already got a reflection object
1633 try {
1634 $cachedclasses[$key] = Zend_Server_Reflection::reflectClass($dataobject->classname);
1635 } catch (Zend_Server_Reflection_Exception $e) { // catch these and rethrow them to something more helpful
1636 throw new moodle_exception('installreflectionclasserror', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname, 'error' => $e->getMessage()));
1639 $r =& $cachedclasses[$key];
1640 if (!$r->hasMethod($dataobject->functionname)) {
1641 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
1643 // stupid workaround for zend not having a getMethod($name) function
1644 $ms = $r->getMethods();
1645 foreach ($ms as $m) {
1646 if ($m->getName() == $dataobject->functionname) {
1647 $functionreflect = $m;
1648 break;
1651 $dataobject->static = (int)$functionreflect->isStatic();
1652 } else {
1653 if (!function_exists($dataobject->functionname)) {
1654 throw new moodle_exception('installnosuchfunction', 'mnet', '', (object)array('method' => $dataobject->functionname, 'file' => $dataobject->filename));
1656 try {
1657 $functionreflect = Zend_Server_Reflection::reflectFunction($dataobject->functionname);
1658 } catch (Zend_Server_Reflection_Exception $e) { // catch these and rethrow them to something more helpful
1659 throw new moodle_exception('installreflectionfunctionerror', 'mnet', '', (object)array('method' => $dataobject->functionname, '' => $dataobject->filename, 'error' => $e->getMessage()));
1662 $dataobject->profile = serialize(admin_mnet_method_profile($functionreflect));
1663 $dataobject->help = $functionreflect->getDescription();
1665 if ($record_exists = $DB->get_record('mnet_rpc', array('xmlrpcpath'=>$dataobject->xmlrpcpath))) {
1666 $dataobject->id = $record_exists->id;
1667 $dataobject->enabled = $record_exists->enabled;
1668 $DB->update_record('mnet_rpc', $dataobject);
1669 } else {
1670 $dataobject->id = $DB->insert_record('mnet_rpc', $dataobject, true);
1673 // TODO this API versioning must be reworked, here the recently processed method
1674 // sets the service API which may not be correct
1675 foreach ($publishmethodservices[$dataobject->functionname] as $service) {
1676 if ($serviceobj = $DB->get_record('mnet_service', array('name'=>$service['servicename']))) {
1677 $serviceobj->apiversion = $service['apiversion'];
1678 $DB->update_record('mnet_service', $serviceobj);
1679 } else {
1680 $serviceobj = new stdClass();
1681 $serviceobj->name = $service['servicename'];
1682 $serviceobj->description = empty($service['description']) ? '' : $service['description'];
1683 $serviceobj->apiversion = $service['apiversion'];
1684 $serviceobj->offer = 1;
1685 $serviceobj->id = $DB->insert_record('mnet_service', $serviceobj);
1687 $servicecache[$service['servicename']] = $serviceobj;
1688 if (!$DB->record_exists('mnet_service2rpc', array('rpcid'=>$dataobject->id, 'serviceid'=>$serviceobj->id))) {
1689 $obj = new stdClass();
1690 $obj->rpcid = $dataobject->id;
1691 $obj->serviceid = $serviceobj->id;
1692 $DB->insert_record('mnet_service2rpc', $obj, true);
1697 // finished with methods we publish, now do subscribable methods
1698 foreach($subscribes as $service => $methods) {
1699 if (!array_key_exists($service, $servicecache)) {
1700 if (!$serviceobj = $DB->get_record('mnet_service', array('name' => $service))) {
1701 debugging("TODO: skipping unknown service $service - somebody needs to fix MDL-21993");
1702 continue;
1704 $servicecache[$service] = $serviceobj;
1705 } else {
1706 $serviceobj = $servicecache[$service];
1708 foreach ($methods as $method => $xmlrpcpath) {
1709 if (!$rpcid = $DB->get_field('mnet_remote_rpc', 'id', array('xmlrpcpath'=>$xmlrpcpath))) {
1710 $remoterpc = (object)array(
1711 'functionname' => $method,
1712 'xmlrpcpath' => $xmlrpcpath,
1713 'plugintype' => $type,
1714 'pluginname' => $plugin,
1715 'enabled' => 1,
1717 $rpcid = $remoterpc->id = $DB->insert_record('mnet_remote_rpc', $remoterpc, true);
1719 if (!$DB->record_exists('mnet_remote_service2rpc', array('rpcid'=>$rpcid, 'serviceid'=>$serviceobj->id))) {
1720 $obj = new stdClass();
1721 $obj->rpcid = $rpcid;
1722 $obj->serviceid = $serviceobj->id;
1723 $DB->insert_record('mnet_remote_service2rpc', $obj, true);
1725 $subscribemethodservices[$method][] = $service;
1729 foreach ($DB->get_records('mnet_remote_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
1730 if (!array_key_exists($rpc->functionname, $subscribemethodservices) && $rpc->enabled) {
1731 $DB->set_field('mnet_remote_rpc', 'enabled', 0, array('id' => $rpc->id));
1732 } else if (array_key_exists($rpc->functionname, $subscribemethodservices) && !$rpc->enabled) {
1733 $DB->set_field('mnet_remote_rpc', 'enabled', 1, array('id' => $rpc->id));
1737 return true;
1741 * Given some sort of Zend Reflection function/method object, return a profile array, ready to be serialized and stored
1743 * @param Zend_Server_Reflection_Function_Abstract $function can be any subclass of this object type
1745 * @return array
1747 function admin_mnet_method_profile(Zend_Server_Reflection_Function_Abstract $function) {
1748 $protos = $function->getPrototypes();
1749 $proto = array_pop($protos);
1750 $ret = $proto->getReturnValue();
1751 $profile = array(
1752 'parameters' => array(),
1753 'return' => array(
1754 'type' => $ret->getType(),
1755 'description' => $ret->getDescription(),
1758 foreach ($proto->getParameters() as $p) {
1759 $profile['parameters'][] = array(
1760 'name' => $p->getName(),
1761 'type' => $p->getType(),
1762 'description' => $p->getDescription(),
1765 return $profile;