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