Merge branch 'MDL-33192-master-1' of git://git.luns.net.uk/moodle
[moodle.git] / lib / upgradelib.php
CommitLineData
96db14f9 1<?php
2
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.
14//
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/>.
db9d4a3d 17
18/**
8580535b 19 * Various upgrade/install related functions and classes.
db9d4a3d 20 *
78bfb562 21 * @package core
96db14f9 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
db9d4a3d 25 */
26
78bfb562
PS
27defined('MOODLE_INTERNAL') || die();
28
72fb21b6 29/** UPGRADE_LOG_NORMAL = 0 */
db9d4a3d 30define('UPGRADE_LOG_NORMAL', 0);
72fb21b6 31/** UPGRADE_LOG_NOTICE = 1 */
db9d4a3d 32define('UPGRADE_LOG_NOTICE', 1);
72fb21b6 33/** UPGRADE_LOG_ERROR = 2 */
795a08ad 34define('UPGRADE_LOG_ERROR', 2);
35
36/**
37 * Exception indicating unknown error during upgrade.
72fb21b6 38 *
d32fb550 39 * @package core
72fb21b6 40 * @subpackage upgrade
d32fb550 41 * @copyright 2009 Petr Skoda {@link http://skodak.org}
72fb21b6 42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
795a08ad 43 */
44class upgrade_exception extends moodle_exception {
4f0c2d00 45 function __construct($plugin, $version, $debuginfo=NULL) {
795a08ad 46 global $CFG;
47 $a = (object)array('plugin'=>$plugin, 'version'=>$version);
4f0c2d00 48 parent::__construct('upgradeerror', 'admin', "$CFG->wwwroot/$CFG->admin/index.php", $a, $debuginfo);
795a08ad 49 }
50}
51
52/**
53 * Exception indicating downgrade error during upgrade.
72fb21b6 54 *
d32fb550 55 * @package core
72fb21b6 56 * @subpackage upgrade
d32fb550 57 * @copyright 2009 Petr Skoda {@link http://skodak.org}
72fb21b6 58 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
795a08ad 59 */
60class 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);
66 }
67}
68
72fb21b6 69/**
d32fb550 70 * @package core
72fb21b6 71 * @subpackage upgrade
d32fb550 72 * @copyright 2009 Petr Skoda {@link http://skodak.org}
72fb21b6 73 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
74 */
795a08ad 75class upgrade_requires_exception extends moodle_exception {
76 function __construct($plugin, $pluginversion, $currentmoodle, $requiremoodle) {
77 global $CFG;
365a5941 78 $a = new stdClass();
795a08ad 79 $a->pluginname = $plugin;
80 $a->pluginversion = $pluginversion;
81 $a->currentmoodle = $currentmoodle;
17da2e6f 82 $a->requiremoodle = $requiremoodle;
795a08ad 83 parent::__construct('pluginrequirementsnotmet', 'error', "$CFG->wwwroot/$CFG->admin/index.php", $a);
84 }
85}
86
72fb21b6 87/**
d32fb550 88 * @package core
72fb21b6 89 * @subpackage upgrade
d32fb550 90 * @copyright 2009 Petr Skoda {@link http://skodak.org}
72fb21b6 91 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
92 */
795a08ad 93class 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);
97 }
98}
db9d4a3d 99
0c5406ca
PS
100/**
101 * Sets maximum expected time needed for upgrade task.
102 * Please always make sure that upgrade will not run longer!
103 *
104 * The script may be automatically aborted if upgrade times out.
105 *
106 * @category upgrade
107 * @param int $max_execution_time in seconds (can not be less than 60 s)
108 */
109function upgrade_set_timeout($max_execution_time=300) {
110 global $CFG;
111
112 if (!isset($CFG->upgraderunning) or $CFG->upgraderunning < time()) {
113 $upgraderunning = get_config(null, 'upgraderunning');
114 } else {
115 $upgraderunning = $CFG->upgraderunning;
116 }
117
118 if (!$upgraderunning) {
b814d1b4
PS
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/");
125 }
0c5406ca
PS
126 }
127
128 if ($max_execution_time < 60) {
129 // protection against 0 here
130 $max_execution_time = 60;
131 }
132
133 $expected_end = time() + $max_execution_time;
134
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;
138 }
139
5563f5fb
PS
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);
145 }
0c5406ca
PS
146 set_config('upgraderunning', $expected_end); // keep upgrade locked until this time
147}
148
db9d4a3d 149/**
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.
153 *
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.
156 *
39b90b51 157 * @category upgrade
db9d4a3d 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
162 */
163function upgrade_main_savepoint($result, $version, $allowabort=true) {
164 global $CFG;
165
bb44ef99
RT
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);
170 }
171
795a08ad 172 if (!$result) {
520cea92 173 throw new upgrade_exception(null, $version);
795a08ad 174 }
175
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);
db9d4a3d 179 }
180
795a08ad 181 set_config('version', $version);
182 upgrade_log(UPGRADE_LOG_NORMAL, null, 'Upgrade savepoint reached');
183
db9d4a3d 184 // reset upgrade timeout to default
185 upgrade_set_timeout();
186
187 // this is a safe place to stop upgrades if user aborts page loading
188 if ($allowabort and connection_aborted()) {
189 die;
190 }
191}
192
193/**
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.
197 *
39b90b51 198 * @category upgrade
db9d4a3d 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
204 */
205function upgrade_mod_savepoint($result, $version, $modname, $allowabort=true) {
206 global $DB;
207
795a08ad 208 if (!$result) {
520cea92 209 throw new upgrade_exception("mod_$modname", $version);
795a08ad 210 }
211
db9d4a3d 212 if (!$module = $DB->get_record('modules', array('name'=>$modname))) {
213 print_error('modulenotexist', 'debug', '', $modname);
214 }
215
795a08ad 216 if ($module->version >= $version) {
217 // something really wrong is going on in upgrade script
520cea92 218 throw new downgrade_exception("mod_$modname", $module->version, $version);
db9d4a3d 219 }
795a08ad 220 $module->version = $version;
221 $DB->update_record('modules', $module);
520cea92 222 upgrade_log(UPGRADE_LOG_NORMAL, "mod_$modname", 'Upgrade savepoint reached');
db9d4a3d 223
224 // reset upgrade timeout to default
225 upgrade_set_timeout();
226
227 // this is a safe place to stop upgrades if user aborts page loading
228 if ($allowabort and connection_aborted()) {
229 die;
230 }
231}
232
233/**
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.
237 *
39b90b51 238 * @category upgrade
db9d4a3d 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
244 */
795a08ad 245function upgrade_block_savepoint($result, $version, $blockname, $allowabort=true) {
db9d4a3d 246 global $DB;
247
795a08ad 248 if (!$result) {
520cea92 249 throw new upgrade_exception("block_$blockname", $version);
795a08ad 250 }
251
db9d4a3d 252 if (!$block = $DB->get_record('block', array('name'=>$blockname))) {
253 print_error('blocknotexist', 'debug', '', $blockname);
254 }
255
795a08ad 256 if ($block->version >= $version) {
257 // something really wrong is going on in upgrade script
520cea92 258 throw new downgrade_exception("block_$blockname", $block->version, $version);
db9d4a3d 259 }
795a08ad 260 $block->version = $version;
261 $DB->update_record('block', $block);
520cea92 262 upgrade_log(UPGRADE_LOG_NORMAL, "block_$blockname", 'Upgrade savepoint reached');
db9d4a3d 263
264 // reset upgrade timeout to default
265 upgrade_set_timeout();
266
267 // this is a safe place to stop upgrades if user aborts page loading
268 if ($allowabort and connection_aborted()) {
269 die;
270 }
271}
272
273/**
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.
277 *
39b90b51 278 * @category upgrade
db9d4a3d 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
285 */
17da2e6f 286function upgrade_plugin_savepoint($result, $version, $type, $plugin, $allowabort=true) {
287 $component = $type.'_'.$plugin;
288
795a08ad 289 if (!$result) {
17da2e6f 290 throw new upgrade_exception($component, $version);
db9d4a3d 291 }
292
17da2e6f 293 $installedversion = get_config($component, 'version');
795a08ad 294 if ($installedversion >= $version) {
295 // Something really wrong is going on in the upgrade script
296 throw new downgrade_exception($component, $installedversion, $version);
297 }
17da2e6f 298 set_config('version', $version, $component);
795a08ad 299 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
300
db9d4a3d 301 // Reset upgrade timeout to default
302 upgrade_set_timeout();
303
304 // This is a safe place to stop upgrades if user aborts page loading
305 if ($allowabort and connection_aborted()) {
306 die;
307 }
308}
309
9008ec16
PS
310/**
311 * Detect if there are leftovers in PHP source files.
312 *
313 * During main version upgrades administrators MUST move away
314 * old PHP source files and start from scratch (or better
315 * use git).
316 *
317 * @return bool true means borked upgrade, false means previous PHP files were properly removed
318 */
319function upgrade_stale_php_files_present() {
320 global $CFG;
321
322 $someexamplesofremovedfiles = array(
37d72977
PS
323 // removed in 2.3dev
324 '/lib/minify/builder/',
9008ec16
PS
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',
337 );
338
339 foreach ($someexamplesofremovedfiles as $file) {
340 if (file_exists($CFG->dirroot.$file)) {
341 return true;
342 }
343 }
344
345 return false;
346}
db9d4a3d 347
348/**
349 * Upgrade plugins
db9d4a3d 350 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
17da2e6f 351 * return void
db9d4a3d 352 */
17da2e6f 353function upgrade_plugins($type, $startcallback, $endcallback, $verbose) {
db9d4a3d 354 global $CFG, $DB;
355
356/// special cases
357 if ($type === 'mod') {
194cdca8 358 return upgrade_plugins_modules($startcallback, $endcallback, $verbose);
795a08ad 359 } else if ($type === 'block') {
194cdca8 360 return upgrade_plugins_blocks($startcallback, $endcallback, $verbose);
db9d4a3d 361 }
362
17da2e6f 363 $plugs = get_plugin_list($type);
db9d4a3d 364
17da2e6f 365 foreach ($plugs as $plug=>$fullplug) {
ebb98bf5 366 // Reset time so that it works when installing a large number of plugins
367 set_time_limit(600);
aff24313 368 $component = clean_param($type.'_'.$plug, PARAM_COMPONENT); // standardised plugin name
db9d4a3d 369
3e858ea7 370 // check plugin dir is valid name
aff24313
PS
371 if (empty($component)) {
372 throw new plugin_defective_exception($type.'_'.$plug, 'Invalid plugin directory name.');
3e858ea7
PS
373 }
374
795a08ad 375 if (!is_readable($fullplug.'/version.php')) {
376 continue;
db9d4a3d 377 }
378
365a5941 379 $plugin = new stdClass();
795a08ad 380 require($fullplug.'/version.php'); // defines $plugin with version etc
db9d4a3d 381
3e858ea7
PS
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.');
386 }
387 }
388
795a08ad 389 if (empty($plugin->version)) {
390 throw new plugin_defective_exception($component, 'Missing version value in version.php');
db9d4a3d 391 }
392
17da2e6f 393 $plugin->name = $plug;
394 $plugin->fullname = $component;
795a08ad 395
396
db9d4a3d 397 if (!empty($plugin->requires)) {
398 if ($plugin->requires > $CFG->version) {
795a08ad 399 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
ef14c1e7
PS
400 } else if ($plugin->requires < 2010000000) {
401 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
db9d4a3d 402 }
403 }
404
9b683d13 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();
f6f06d69 413 unset_config('installrunning', $plugin->fullname);
9b683d13 414 update_capabilities($component);
c6d75bff 415 log_update_descriptions($component);
c976e271 416 external_update_descriptions($component);
9b683d13 417 events_update_definition($component);
418 message_update_providers($component);
afed8d0f
RK
419 if ($type === 'message') {
420 message_update_processors($plug);
421 }
de260e0f 422 upgrade_plugin_mnet_functions($component);
9b683d13 423 $endcallback($component, true, $verbose);
424 }
425 }
426 }
db9d4a3d 427
9b683d13 428 $installedversion = get_config($plugin->fullname, 'version');
795a08ad 429 if (empty($installedversion)) { // new installation
194cdca8 430 $startcallback($component, true, $verbose);
db9d4a3d 431
795a08ad 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');
db9d4a3d 435 }
9b683d13 436
437 /// store version
438 upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false);
439
795a08ad 440 /// execute post install file
441 if (file_exists($fullplug.'/db/install.php')) {
442 require_once($fullplug.'/db/install.php');
f6f06d69
PS
443 set_config('installrunning', 1, $plugin->fullname);
444 $post_install_function = 'xmldb_'.$plugin->fullname.'_install';
795a08ad 445 $post_install_function();
f6f06d69 446 unset_config('installrunning', $plugin->fullname);
795a08ad 447 }
448
795a08ad 449 /// Install various components
450 update_capabilities($component);
c6d75bff 451 log_update_descriptions($component);
c976e271 452 external_update_descriptions($component);
795a08ad 453 events_update_definition($component);
454 message_update_providers($component);
afed8d0f
RK
455 if ($type === 'message') {
456 message_update_processors($plug);
457 }
de260e0f 458 upgrade_plugin_mnet_functions($component);
795a08ad 459
f87eab7e 460 purge_all_caches();
194cdca8 461 $endcallback($component, true, $verbose);
795a08ad 462
463 } else if ($installedversion < $plugin->version) { // upgrade
464 /// Run the upgrade function for the plugin.
194cdca8 465 $startcallback($component, false, $verbose);
795a08ad 466
467 if (is_readable($fullplug.'/db/upgrade.php')) {
468 require_once($fullplug.'/db/upgrade.php'); // defines upgrading function
469
470 $newupgrade_function = 'xmldb_'.$plugin->fullname.'_upgrade';
471 $result = $newupgrade_function($installedversion);
472 } else {
473 $result = true;
474 }
475
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);
480 }
481
482 /// Upgrade various components
483 update_capabilities($component);
c6d75bff 484 log_update_descriptions($component);
c976e271 485 external_update_descriptions($component);
795a08ad 486 events_update_definition($component);
487 message_update_providers($component);
afed8d0f
RK
488 if ($type === 'message') {
489 message_update_processors($plug);
490 }
de260e0f 491 upgrade_plugin_mnet_functions($component);
795a08ad 492
f87eab7e 493 purge_all_caches();
194cdca8 494 $endcallback($component, false, $verbose);
795a08ad 495
496 } else if ($installedversion > $plugin->version) {
497 throw new downgrade_exception($component, $installedversion, $plugin->version);
db9d4a3d 498 }
499 }
db9d4a3d 500}
501
502/**
503 * Find and check all modules and load them up or upgrade them if necessary
72fb21b6 504 *
505 * @global object
506 * @global object
db9d4a3d 507 */
194cdca8 508function upgrade_plugins_modules($startcallback, $endcallback, $verbose) {
db9d4a3d 509 global $CFG, $DB;
510
17da2e6f 511 $mods = get_plugin_list('mod');
db9d4a3d 512
17da2e6f 513 foreach ($mods as $mod=>$fullmod) {
db9d4a3d 514
3e858ea7 515 if ($mod === 'NEWMODULE') { // Someone has unzipped the template, ignore it
db9d4a3d 516 continue;
517 }
518
aff24313 519 $component = clean_param('mod_'.$mod, PARAM_COMPONENT);
db9d4a3d 520
3e858ea7 521 // check module dir is valid name
aff24313
PS
522 if (empty($component)) {
523 throw new plugin_defective_exception('mod_'.$mod, 'Invalid plugin directory name.');
3e858ea7
PS
524 }
525
795a08ad 526 if (!is_readable($fullmod.'/version.php')) {
527 throw new plugin_defective_exception($component, 'Missing version.php');
db9d4a3d 528 }
529
365a5941 530 $module = new stdClass();
795a08ad 531 require($fullmod .'/version.php'); // defines $module with version etc
db9d4a3d 532
3e858ea7
PS
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.');
537 }
538 }
539
795a08ad 540 if (empty($module->version)) {
c1fe2368 541 if (isset($module->version)) {
542 // Version is empty but is set - it means its value is 0 or ''. Let us skip such module.
b921b6ee 543 // This is intended for developers so they can work on the early stages of the module.
c1fe2368 544 continue;
545 }
795a08ad 546 throw new plugin_defective_exception($component, 'Missing version value in version.php');
db9d4a3d 547 }
548
549 if (!empty($module->requires)) {
550 if ($module->requires > $CFG->version) {
795a08ad 551 throw new upgrade_requires_exception($component, $module->version, $CFG->version, $module->requires);
ef14c1e7
PS
552 } else if ($module->requires < 2010000000) {
553 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
db9d4a3d 554 }
555 }
556
ff05bb31
TH
557 if (empty($module->cron)) {
558 $module->cron = 0;
559 }
560
3e858ea7
PS
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.');
564 }
565
db9d4a3d 566 $module->name = $mod; // The name MUST match the directory
567
795a08ad 568 $currmodule = $DB->get_record('modules', array('name'=>$module->name));
db9d4a3d 569
9b683d13 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);
c6d75bff 580 log_update_descriptions($component);
c976e271 581 external_update_descriptions($component);
9b683d13 582 events_update_definition($component);
583 message_update_providers($component);
de260e0f 584 upgrade_plugin_mnet_functions($component);
9b683d13 585 $endcallback($component, true, $verbose);
586 }
587 }
588 }
589
795a08ad 590 if (empty($currmodule->version)) {
194cdca8 591 $startcallback($component, true, $verbose);
db9d4a3d 592
795a08ad 593 /// Execute install.xml (XMLDB) - must be present in all modules
594 $DB->get_manager()->install_from_xmldb_file($fullmod.'/db/install.xml');
db9d4a3d 595
2e3da297 596 /// Add record into modules table - may be needed in install.php already
597 $module->id = $DB->insert_record('modules', $module);
598
db9d4a3d 599 /// Post installation hook - optional
600 if (file_exists("$fullmod/db/install.php")) {
601 require_once("$fullmod/db/install.php");
9b683d13 602 // Set installation running flag, we need to recover after exception or error
603 set_config('installrunning', 1, $module->name);
3412cb43 604 $post_install_function = 'xmldb_'.$module->name.'_install';
db9d4a3d 605 $post_install_function();
9b683d13 606 unset_config('installrunning', $module->name);
db9d4a3d 607 }
608
795a08ad 609 /// Install various components
610 update_capabilities($component);
c6d75bff 611 log_update_descriptions($component);
c976e271 612 external_update_descriptions($component);
795a08ad 613 events_update_definition($component);
614 message_update_providers($component);
de260e0f 615 upgrade_plugin_mnet_functions($component);
795a08ad 616
f87eab7e 617 purge_all_caches();
194cdca8 618 $endcallback($component, true, $verbose);
db9d4a3d 619
795a08ad 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
194cdca8 622 $startcallback($component, false, $verbose);
795a08ad 623
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;
630 }
631
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);
636 }
637
3412cb43
TH
638 // update cron flag if needed
639 if ($currmodule->cron != $module->cron) {
640 $DB->set_field('modules', 'cron', $module->cron, array('name' => $module->name));
641 }
642
643 // Upgrade various components
795a08ad 644 update_capabilities($component);
c6d75bff 645 log_update_descriptions($component);
c976e271 646 external_update_descriptions($component);
795a08ad 647 events_update_definition($component);
648 message_update_providers($component);
de260e0f 649 upgrade_plugin_mnet_functions($component);
795a08ad 650
f87eab7e 651 purge_all_caches();
795a08ad 652
194cdca8 653 $endcallback($component, false, $verbose);
795a08ad 654
655 } else if ($currmodule->version > $module->version) {
656 throw new downgrade_exception($component, $currmodule->version, $module->version);
657 }
658 }
659}
db9d4a3d 660
db9d4a3d 661
795a08ad 662/**
663 * This function finds all available blocks and install them
664 * into blocks table or do all the upgrade process if newer.
72fb21b6 665 *
666 * @global object
667 * @global object
795a08ad 668 */
194cdca8 669function upgrade_plugins_blocks($startcallback, $endcallback, $verbose) {
795a08ad 670 global $CFG, $DB;
671
795a08ad 672 require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
673
674 $blocktitles = array(); // we do not want duplicate titles
675
676 //Is this a first install
677 $first_install = null;
678
17da2e6f 679 $blocks = get_plugin_list('block');
795a08ad 680
17da2e6f 681 foreach ($blocks as $blockname=>$fullblock) {
795a08ad 682
683 if (is_null($first_install)) {
f46eba7e 684 $first_install = ($DB->count_records('block_instances') == 0);
795a08ad 685 }
686
aff24313 687 if ($blockname === 'NEWBLOCK') { // Someone has unzipped the template, ignore it
795a08ad 688 continue;
db9d4a3d 689 }
690
aff24313 691 $component = clean_param('block_'.$blockname, PARAM_COMPONENT);
db9d4a3d 692
3e858ea7 693 // check block dir is valid name
aff24313
PS
694 if (empty($component)) {
695 throw new plugin_defective_exception('block_'.$blockname, 'Invalid plugin directory name.');
3e858ea7
PS
696 }
697
8571833f
PS
698 if (!is_readable($fullblock.'/version.php')) {
699 throw new plugin_defective_exception('block/'.$blockname, 'Missing version.php file.');
700 }
365a5941 701 $plugin = new stdClass();
8571833f
PS
702 $plugin->version = NULL;
703 $plugin->cron = 0;
704 include($fullblock.'/version.php');
705 $block = $plugin;
706
3e858ea7
PS
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.');
711 }
712 }
713
ef14c1e7
PS
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.');
719 }
720 }
721
795a08ad 722 if (!is_readable($fullblock.'/block_'.$blockname.'.php')) {
723 throw new plugin_defective_exception('block/'.$blockname, 'Missing main block class file.');
db9d4a3d 724 }
8571833f 725 include_once($fullblock.'/block_'.$blockname.'.php');
db9d4a3d 726
795a08ad 727 $classname = 'block_'.$blockname;
728
729 if (!class_exists($classname)) {
730 throw new plugin_defective_exception($component, 'Can not load main class.');
731 }
732
b921b6ee 733 $blockobj = new $classname; // This is what we'll be testing
795a08ad 734 $blocktitle = $blockobj->get_title();
735
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.');
739 }
740
795a08ad 741 $block->name = $blockname; // The name MUST match the directory
795a08ad 742
743 if (empty($block->version)) {
744 throw new plugin_defective_exception($component, 'Missing block version.');
745 }
746
747 $currblock = $DB->get_record('block', array('name'=>$block->name));
748
9b683d13 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);
c6d75bff 759 log_update_descriptions($component);
c976e271 760 external_update_descriptions($component);
9b683d13 761 events_update_definition($component);
762 message_update_providers($component);
de260e0f 763 upgrade_plugin_mnet_functions($component);
9b683d13 764 $endcallback($component, true, $verbose);
765 }
766 }
767 }
768
795a08ad 769 if (empty($currblock->version)) { // block not installed yet, so install it
795a08ad 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 ;)
c1ddac83 774 throw new plugin_defective_exception($component, get_string('blocknameconflict', 'error', (object)array('name'=>$block->name, 'conflict'=>$conflictblock)));
795a08ad 775 }
194cdca8 776 $startcallback($component, true, $verbose);
795a08ad 777
778 if (file_exists($fullblock.'/db/install.xml')) {
779 $DB->get_manager()->install_from_xmldb_file($fullblock.'/db/install.xml');
780 }
781 $block->id = $DB->insert_record('block', $block);
782
783 if (file_exists($fullblock.'/db/install.php')) {
784 require_once($fullblock.'/db/install.php');
9b683d13 785 // Set installation running flag, we need to recover after exception or error
786 set_config('installrunning', 1, 'block_'.$blockname);
795a08ad 787 $post_install_function = 'xmldb_block_'.$blockname.'_install';;
788 $post_install_function();
9b683d13 789 unset_config('installrunning', 'block_'.$blockname);
795a08ad 790 }
791
792 $blocktitles[$block->name] = $blocktitle;
793
794 // Install various components
795 update_capabilities($component);
c6d75bff 796 log_update_descriptions($component);
c976e271 797 external_update_descriptions($component);
795a08ad 798 events_update_definition($component);
799 message_update_providers($component);
de260e0f 800 upgrade_plugin_mnet_functions($component);
795a08ad 801
f87eab7e 802 purge_all_caches();
194cdca8 803 $endcallback($component, true, $verbose);
795a08ad 804
805 } else if ($currblock->version < $block->version) {
194cdca8 806 $startcallback($component, false, $verbose);
795a08ad 807
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;
814 }
815
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);
820 }
821
822 if ($currblock->cron != $block->cron) {
823 // update cron flag if needed
824 $currblock->cron = $block->cron;
825 $DB->update_record('block', $currblock);
826 }
827
b921b6ee 828 // Upgrade various components
795a08ad 829 update_capabilities($component);
c6d75bff 830 log_update_descriptions($component);
c976e271 831 external_update_descriptions($component);
17da2e6f 832 events_update_definition($component);
795a08ad 833 message_update_providers($component);
de260e0f 834 upgrade_plugin_mnet_functions($component);
795a08ad 835
f87eab7e 836 purge_all_caches();
194cdca8 837 $endcallback($component, false, $verbose);
795a08ad 838
839 } else if ($currblock->version > $block->version) {
840 throw new downgrade_exception($component, $currblock->version, $block->version);
841 }
842 }
843
844
845 // Finally, if we are in the first_install of BLOCKS setup frontpage and admin page blocks
846 if ($first_install) {
795a08ad 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) {
9d1d606e 850 blocks_add_default_course_blocks($course);
db9d4a3d 851 }
852 }
795a08ad 853
9d1d606e 854 blocks_add_default_system_blocks();
795a08ad 855 }
856}
857
c6d75bff
PS
858
859/**
860 * Log_display description function used during install and upgrade.
861 *
862 * @param string $component name of component (moodle, mod_assignment, etc.)
863 * @return void
864 */
865function log_update_descriptions($component) {
866 global $DB;
867
868 $defpath = get_component_directory($component).'/db/log.php';
869
870 if (!file_exists($defpath)) {
871 $DB->delete_records('log_display', array('component'=>$component));
872 return;
873 }
874
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
881 }
882 unset($logs);
883 $logs = $newlogs;
884
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;
890
891 if (empty($logs[$name])) {
892 $DB->delete_records('log_display', array('id'=>$dblog->id));
893 continue;
894 }
895
896 $log = $logs[$name];
897 unset($logs[$name]);
898
899 $update = false;
900 foreach ($fields as $field) {
901 if ($dblog->$field != $log[$field]) {
902 $dblog->$field = $log[$field];
903 $update = true;
904 }
905 }
906 if ($update) {
907 $DB->update_record('log_display', $dblog);
908 }
909 }
910 foreach ($logs as $log) {
911 $dblog = (object)$log;
912 $dblog->component = $component;
913 $DB->insert_record('log_display', $dblog);
914 }
915}
916
c976e271 917/**
918 * Web service discovery function used during install and upgrade.
919 * @param string $component name of component (moodle, mod_assignment, etc.)
920 * @return void
921 */
922function external_update_descriptions($component) {
bc81eadb 923 global $DB, $CFG;
c976e271 924
925 $defpath = get_component_directory($component).'/db/services.php';
926
927 if (!file_exists($defpath)) {
bc81eadb 928 require_once($CFG->dirroot.'/lib/externallib.php');
c976e271 929 external_delete_descriptions($component);
930 return;
931 }
932
933 // load new info
934 $functions = array();
935 $services = array();
936 include($defpath);
937
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
c6d75bff
PS
945
946 //TODO: this looks wrong, we have to delete it eventually (skodak)
c976e271 947 continue;
948 }
949
950 $function = $functions[$dbfunction->name];
951 unset($functions[$dbfunction->name]);
952 $function['classpath'] = empty($function['classpath']) ? null : $function['classpath'];
953
954 $update = false;
955 if ($dbfunction->classname != $function['classname']) {
956 $dbfunction->classname = $function['classname'];
957 $update = true;
958 }
959 if ($dbfunction->methodname != $function['methodname']) {
960 $dbfunction->methodname = $function['methodname'];
961 $update = true;
962 }
963 if ($dbfunction->classpath != $function['classpath']) {
964 $dbfunction->classpath = $function['classpath'];
965 $update = true;
966 }
72f68b51 967 $functioncapabilities = key_exists('capabilities', $function)?$function['capabilities']:'';
968 if ($dbfunction->capabilities != $functioncapabilities) {
969 $dbfunction->capabilities = $functioncapabilities;
970 $update = true;
971 }
c976e271 972 if ($update) {
973 $DB->update_record('external_functions', $dbfunction);
974 }
975 }
976 foreach ($functions as $fname => $function) {
365a5941 977 $dbfunction = new stdClass();
c976e271 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;
72f68b51 983 $dbfunction->capabilities = key_exists('capabilities', $function)?$function['capabilities']:'';
c976e271 984 $dbfunction->id = $DB->insert_record('external_functions', $dbfunction);
985 }
986 unset($functions);
987
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])) {
bc81eadb 992 $DB->delete_records('external_tokens', array('externalserviceid'=>$dbservice->id));
c976e271 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;
997 }
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'];
af03513f 1003 $service['downloadfiles'] = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
c1b65883 1004 $service['shortname'] = !isset($service['shortname']) ? null : $service['shortname'];
c976e271 1005
1006 $update = false;
c976e271 1007 if ($dbservice->requiredcapability != $service['requiredcapability']) {
1008 $dbservice->requiredcapability = $service['requiredcapability'];
1009 $update = true;
1010 }
1011 if ($dbservice->restrictedusers != $service['restrictedusers']) {
1012 $dbservice->restrictedusers = $service['restrictedusers'];
1013 $update = true;
1014 }
af03513f
JM
1015 if ($dbservice->downloadfiles != $service['downloadfiles']) {
1016 $dbservice->downloadfiles = $service['downloadfiles'];
1017 $update = true;
1018 }
c1b65883
JM
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']);
1023 }
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']);
1031 }
1032 }
1033 $dbservice->shortname = $service['shortname'];
1034 $update = true;
1035 }
c976e271 1036 if ($update) {
1037 $DB->update_record('external_services', $dbservice);
1038 }
1039
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]);
1047 }
1048 }
1049 foreach ($service['functions'] as $fname) {
365a5941 1050 $newf = new stdClass();
c976e271 1051 $newf->externalserviceid = $dbservice->id;
1052 $newf->functionname = $fname;
1053 $DB->insert_record('external_services_functions', $newf);
1054 }
1055 unset($functions);
1056 }
1057 foreach ($services as $name => $service) {
c1b65883
JM
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');
1064 }
1065 }
1066
365a5941 1067 $dbservice = new stdClass();
c976e271 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'];
af03513f 1072 $dbservice->downloadfiles = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
c1b65883 1073 $dbservice->shortname = !isset($service['shortname']) ? null : $service['shortname'];
c976e271 1074 $dbservice->component = $component;
e5180580 1075 $dbservice->timecreated = time();
c976e271 1076 $dbservice->id = $DB->insert_record('external_services', $dbservice);
1077 foreach ($service['functions'] as $fname) {
365a5941 1078 $newf = new stdClass();
c976e271 1079 $newf->externalserviceid = $dbservice->id;
1080 $newf->functionname = $fname;
1081 $DB->insert_record('external_services_functions', $newf);
1082 }
1083 }
1084}
1085
72fb21b6 1086/**
1087 * upgrade logging functions
72fb21b6 1088 */
fd1a792e 1089function upgrade_handle_exception($ex, $plugin = null) {
96e1fdf4
PS
1090 global $CFG;
1091
a56c457e
PS
1092 // rollback everything, we need to log all upgrade problems
1093 abort_all_db_transactions();
1094
c19bc39c
PS
1095 $info = get_exception_info($ex);
1096
1097 // First log upgrade error
1098 upgrade_log(UPGRADE_LOG_ERROR, $plugin, 'Exception: ' . get_class($ex), $info->message, $info->backtrace);
1099
1100 // Always turn on debugging - admins need to know what is going on
1101 $CFG->debug = DEBUG_DEVELOPER;
1102
fd1a792e 1103 default_exception_handler($ex, true, $plugin);
db9d4a3d 1104}
1105
1106/**
1107 * Adds log entry into upgrade_log table
1108 *
1109 * @param int $type UPGRADE_LOG_NORMAL, UPGRADE_LOG_NOTICE or UPGRADE_LOG_ERROR
bb8b2971 1110 * @param string $plugin frankenstyle component name
db9d4a3d 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
1115 */
1116function upgrade_log($type, $plugin, $info, $details=null, $backtrace=null) {
1117 global $DB, $USER, $CFG;
1118
bb8b2971
PS
1119 if (empty($plugin)) {
1120 $plugin = 'core';
1121 }
1122
1123 list($plugintype, $pluginname) = normalize_component($plugin);
1124 $component = is_null($pluginname) ? $plugintype : $plugintype . '_' . $pluginname;
795a08ad 1125
34a2777c 1126 $backtrace = format_backtrace($backtrace, true);
db9d4a3d 1127
bb8b2971
PS
1128 $currentversion = null;
1129 $targetversion = null;
db9d4a3d 1130
1131 //first try to find out current version number
bb8b2971 1132 if ($plugintype === 'core') {
db9d4a3d 1133 //main
bb8b2971 1134 $currentversion = $CFG->version;
db9d4a3d 1135
bb8b2971
PS
1136 $version = null;
1137 include("$CFG->dirroot/version.php");
1138 $targetversion = $version;
795a08ad 1139
bb8b2971 1140 } else if ($plugintype === 'mod') {
db9d4a3d 1141 try {
bb8b2971
PS
1142 $currentversion = $DB->get_field('modules', 'version', array('name'=>$pluginname));
1143 $currentversion = ($currentversion === false) ? null : $currentversion;
db9d4a3d 1144 } catch (Exception $ignored) {
1145 }
bb8b2971
PS
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;
1152 }
db9d4a3d 1153
bb8b2971 1154 } else if ($plugintype === 'block') {
db9d4a3d 1155 try {
bb8b2971
PS
1156 if ($block = $DB->get_record('block', array('name'=>$pluginname))) {
1157 $currentversion = $block->version;
db9d4a3d 1158 }
1159 } catch (Exception $ignored) {
1160 }
bb8b2971
PS
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;
1167 }
795a08ad 1168
1169 } else {
bb8b2971 1170 $pluginversion = get_config($component, 'version');
795a08ad 1171 if (!empty($pluginversion)) {
bb8b2971
PS
1172 $currentversion = $pluginversion;
1173 }
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;
795a08ad 1180 }
db9d4a3d 1181 }
1182
365a5941 1183 $log = new stdClass();
bb8b2971
PS
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();
db9d4a3d 1193 try {
1194 $DB->insert_record('upgrade_log', $log);
1195 } catch (Exception $ignored) {
795a08ad 1196 // possible during install or 2.0 upgrade
db9d4a3d 1197 }
1198}
1199
1200/**
1201 * Marks start of upgrade, blocks any other access to site.
1202 * The upgrade is finished at the end of script or after timeout.
72fb21b6 1203 *
1204 * @global object
1205 * @global object
1206 * @global object
db9d4a3d 1207 */
1208function upgrade_started($preinstall=false) {
de6d81e6 1209 global $CFG, $DB, $PAGE, $OUTPUT;
db9d4a3d 1210
1211 static $started = false;
1212
1213 if ($preinstall) {
1214 ignore_user_abort(true);
1215 upgrade_setup_debug(true);
1216
1217 } else if ($started) {
1218 upgrade_set_timeout(120);
1219
1220 } else {
c13a5e71 1221 if (!CLI_SCRIPT and !$PAGE->headerprinted) {
db9d4a3d 1222 $strupgrade = get_string('upgradingversion', 'admin');
78946b9b 1223 $PAGE->set_pagelayout('maintenance');
543f54d3 1224 upgrade_init_javascript();
de6d81e6 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();
db9d4a3d 1230 }
1231
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;
1237 }
1238}
1239
1240/**
b921b6ee 1241 * Internal function - executed if upgrade interrupted.
db9d4a3d 1242 */
1243function upgrade_finished_handler() {
1244 upgrade_finished();
1245}
1246
1247/**
1248 * Indicates upgrade is finished.
1249 *
1250 * This function may be called repeatedly.
72fb21b6 1251 *
1252 * @global object
1253 * @global object
db9d4a3d 1254 */
1255function upgrade_finished($continueurl=null) {
7e0d6675 1256 global $CFG, $DB, $OUTPUT;
db9d4a3d 1257
1258 if (!empty($CFG->upgraderunning)) {
1259 unset_config('upgraderunning');
1260 upgrade_setup_debug(false);
1261 ignore_user_abort(false);
1262 if ($continueurl) {
aa9a6867 1263 echo $OUTPUT->continue_button($continueurl);
7e0d6675 1264 echo $OUTPUT->footer();
db9d4a3d 1265 die;
1266 }
1267 }
1268}
1269
72fb21b6 1270/**
1271 * @global object
1272 * @global object
1273 */
db9d4a3d 1274function upgrade_setup_debug($starting) {
1275 global $CFG, $DB;
1276
1277 static $originaldebug = null;
1278
1279 if ($starting) {
1280 if ($originaldebug === null) {
1281 $originaldebug = $DB->get_debug();
1282 }
1283 if (!empty($CFG->upgradeshowsql)) {
1284 $DB->set_debug(true);
1285 }
1286 } else {
1287 $DB->set_debug($originaldebug);
1288 }
1289}
1290
1291function print_upgrade_separator() {
1292 if (!CLI_SCRIPT) {
1293 echo '<hr />';
1294 }
1295}
1296
795a08ad 1297/**
1298 * Default start upgrade callback
1299 * @param string $plugin
b921b6ee 1300 * @param bool $installation true if installation, false means upgrade
795a08ad 1301 */
194cdca8 1302function print_upgrade_part_start($plugin, $installation, $verbose) {
3c159385 1303 global $OUTPUT;
795a08ad 1304 if (empty($plugin) or $plugin == 'moodle') {
1305 upgrade_started($installation); // does not store upgrade running flag yet
194cdca8 1306 if ($verbose) {
3c159385 1307 echo $OUTPUT->heading(get_string('coresystem'));
194cdca8 1308 }
795a08ad 1309 } else {
1310 upgrade_started();
194cdca8 1311 if ($verbose) {
3c159385 1312 echo $OUTPUT->heading($plugin);
194cdca8 1313 }
795a08ad 1314 }
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');
1320 }
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');
1326 }
1327 }
1328}
1329
1330/**
1331 * Default end upgrade callback
1332 * @param string $plugin
b921b6ee 1333 * @param bool $installation true if installation, false means upgrade
795a08ad 1334 */
194cdca8 1335function print_upgrade_part_end($plugin, $installation, $verbose) {
aa9a6867 1336 global $OUTPUT;
795a08ad 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');
1343 }
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');
1349 }
1350 }
194cdca8 1351 if ($verbose) {
aa9a6867 1352 echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
194cdca8 1353 print_upgrade_separator();
96db14f9 1354 }
1355}
1356
72fb21b6 1357/**
b921b6ee 1358 * Sets up JS code required for all upgrade scripts.
72fb21b6 1359 * @global object
1360 */
543f54d3 1361function upgrade_init_javascript() {
e29380f3 1362 global $PAGE;
543f54d3
PS
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);
db9d4a3d 1367}
1368
db9d4a3d 1369/**
1370 * Try to upgrade the given language pack (or current language)
af8d4d91 1371 *
74a4c9a9 1372 * @param string $lang the code of the language to update, defaults to the current language
db9d4a3d 1373 */
bd41bdd9
PS
1374function upgrade_language_pack($lang = null) {
1375 global $CFG;
db9d4a3d 1376
bd41bdd9
PS
1377 if (!empty($CFG->skiplangupgrade)) {
1378 return;
1379 }
af8d4d91 1380
bd41bdd9
PS
1381 if (!file_exists("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php")) {
1382 // weird, somebody uninstalled the import utility
1383 return;
1384 }
1385
1386 if (!$lang) {
db9d4a3d 1387 $lang = current_language();
1388 }
1389
bd41bdd9
PS
1390 if (!get_string_manager()->translation_exists($lang)) {
1391 return;
db9d4a3d 1392 }
1393
bd41bdd9
PS
1394 get_string_manager()->reset_caches();
1395
1396 if ($lang === 'en') {
1397 return; // Nothing to do
db9d4a3d 1398 }
af8d4d91 1399
bd41bdd9
PS
1400 upgrade_started(false);
1401
1402 require_once("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php");
1403 tool_langimport_preupgrade_update($lang);
1404
af8d4d91
DM
1405 get_string_manager()->reset_caches();
1406
551fe0e5 1407 print_upgrade_separator();
db9d4a3d 1408}
8580535b 1409
1410/**
1411 * Install core moodle tables and initialize
1412 * @param float $version target version
1413 * @param bool $verbose
1414 * @return void, may throw exception
1415 */
1416function install_core($version, $verbose) {
1417 global $CFG, $DB;
1418
1419 try {
c3d0e149 1420 set_time_limit(600);
194cdca8 1421 print_upgrade_part_start('moodle', true, $verbose); // does not store upgrade running flag
8580535b 1422
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 ;-)
1425
1426 // set all core default records and default settings
1427 require_once("$CFG->libdir/db/install.php");
c6d75bff 1428 xmldb_main_install(); // installs the capabilities too
8580535b 1429
1430 // store version
1431 upgrade_main_savepoint(true, $version, false);
1432
df997f84 1433 // Continue with the installation
c6d75bff
PS
1434 log_update_descriptions('moodle');
1435 external_update_descriptions('moodle');
8580535b 1436 events_update_definition('moodle');
1437 message_update_providers('moodle');
8580535b 1438
df997f84 1439 // Write default settings unconditionally
8580535b 1440 admin_apply_default_settings(NULL, true);
1441
194cdca8 1442 print_upgrade_part_end(null, true, $verbose);
8580535b 1443 } catch (exception $ex) {
1444 upgrade_handle_exception($ex);
1445 }
1446}
1447
1448/**
1449 * Upgrade moodle core
1450 * @param float $version target version
1451 * @param bool $verbose
1452 * @return void, may throw exception
1453 */
1454function upgrade_core($version, $verbose) {
1455 global $CFG;
1456
41209c1e
PS
1457 raise_memory_limit(MEMORY_EXTRA);
1458
3316fe24 1459 require_once($CFG->libdir.'/db/upgrade.php'); // Defines upgrades
3316fe24 1460
8580535b 1461 try {
dcf9be7f 1462 // Reset caches before any output
f87eab7e 1463 purge_all_caches();
dcf9be7f 1464
8580535b 1465 // Upgrade current language pack if we can
bd41bdd9 1466 upgrade_language_pack();
8580535b 1467
194cdca8 1468 print_upgrade_part_start('moodle', false, $verbose);
8580535b 1469
17da2e6f 1470 // one time special local migration pre 2.0 upgrade script
8d33799d
PS
1471 if ($CFG->version < 2007101600) {
1472 $pre20upgradefile = "$CFG->dirroot/local/upgrade_pre20.php";
17da2e6f 1473 if (file_exists($pre20upgradefile)) {
1474 set_time_limit(0);
1475 require($pre20upgradefile);
1476 // reset upgrade timeout to default
1477 upgrade_set_timeout();
1478 }
1479 }
1480
8580535b 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);
1485 }
1486
1487 // perform all other component upgrade routines
1488 update_capabilities('moodle');
c6d75bff 1489 log_update_descriptions('moodle');
c976e271 1490 external_update_descriptions('moodle');
8580535b 1491 events_update_definition('moodle');
1492 message_update_providers('moodle');
8580535b 1493
dcf9be7f 1494 // Reset caches again, just to be sure
f87eab7e 1495 purge_all_caches();
2e5eba85
PS
1496
1497 // Clean up contexts - more and more stuff depends on existence of paths and contexts
e922fe23
PS
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();
8580535b 1503
194cdca8 1504 print_upgrade_part_end('moodle', false, $verbose);
8580535b 1505 } catch (Exception $ex) {
1506 upgrade_handle_exception($ex);
1507 }
1508}
1509
1510/**
1511 * Upgrade/install other parts of moodle
1512 * @param bool $verbose
1513 * @return void, may throw exception
1514 */
1515function upgrade_noncore($verbose) {
1516 global $CFG;
1517
41209c1e
PS
1518 raise_memory_limit(MEMORY_EXTRA);
1519
8580535b 1520 // upgrade all plugins types
1521 try {
1522 $plugintypes = get_plugin_types();
1523 foreach ($plugintypes as $type=>$location) {
17da2e6f 1524 upgrade_plugins($type, 'print_upgrade_part_start', 'print_upgrade_part_end', $verbose);
8580535b 1525 }
1526 } catch (Exception $ex) {
1527 upgrade_handle_exception($ex);
1528 }
8580535b 1529}
3316fe24 1530
1531/**
1532 * Checks if the main tables have been installed yet or not.
1533 * @return bool
1534 */
1535function core_tables_exist() {
1536 global $DB;
1537
1538 if (!$tables = $DB->get_tables() ) { // No tables yet at all.
1539 return false;
9b683d13 1540
3316fe24 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;
1546 }
1547 }
1548 return true;
9b683d13 1549 }
ba04999c 1550}
c71ade2f
PL
1551
1552/**
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
1555 *
1556 * @param string $component the plugin to upgrade, eg auth_mnet
1557 */
1558function upgrade_plugin_mnet_functions($component) {
1559 global $DB, $CFG;
1560
1561 list($type, $plugin) = explode('_', $component);
1562 $path = get_plugin_directory($type, $plugin);
1563
17b71716
PS
1564 $publishes = array();
1565 $subscribes = array();
c71ade2f
PL
1566 if (file_exists($path . '/db/mnet.php')) {
1567 require_once($path . '/db/mnet.php'); // $publishes comes from this file
1568 }
1569 if (empty($publishes)) {
1570 $publishes = array(); // still need this to be able to disable stuff later
1571 }
1572 if (empty($subscribes)) {
1573 $subscribes = array(); // still need this to be able to disable stuff later
1574 }
1575
1576 static $servicecache = array();
1577
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;
1586 }
1587 }
1588 }
1589
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));
1597 }
1598 }
1599
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) {
6bdfef5d 1607 $dataobject = new stdClass();
c71ade2f
PL
1608 $dataobject->plugintype = $type;
1609 $dataobject->pluginname = $plugin;
1610 $dataobject->enabled = 1;
1611 $dataobject->classname = $c;
1612 $dataobject->filename = $f;
1613
1614 if (is_string($method)) {
1615 $dataobject->functionname = $method;
1616
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'];
1621 }
1622 $dataobject->xmlrpcpath = $type.'/'.$plugin.'/'.$dataobject->filename.'/'.$method;
1623 $dataobject->static = false;
1624
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));
1630 }
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()));
1637 }
1638 }
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));
1642 }
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;
1649 }
1650 }
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));
1655 }
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()));
1660 }
1661 }
1662 $dataobject->profile = serialize(admin_mnet_method_profile($functionreflect));
1663 $dataobject->help = $functionreflect->getDescription();
1664
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);
1671 }
c71ade2f 1672
d5bd0b01
DM
1673 // TODO this API versioning must be reworked, here the recently processed method
1674 // sets the service API which may not be correct
677b6321
PL
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'];
f4a2817a 1682 $serviceobj->description = empty($service['description']) ? '' : $service['description'];
677b6321
PL
1683 $serviceobj->apiversion = $service['apiversion'];
1684 $serviceobj->offer = 1;
1685 $serviceobj->id = $DB->insert_record('mnet_service', $serviceobj);
1686 }
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);
1693 }
c71ade2f
PL
1694 }
1695 }
1696 }
c71ade2f
PL
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))) {
81634f03 1701 debugging("TODO: skipping unknown service $service - somebody needs to fix MDL-21993");
c71ade2f
PL
1702 continue;
1703 }
1704 $servicecache[$service] = $serviceobj;
1705 } else {
1706 $serviceobj = $servicecache[$service];
1707 }
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,
1716 );
1717 $rpcid = $remoterpc->id = $DB->insert_record('mnet_remote_rpc', $remoterpc, true);
1718 }
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);
1724 }
677b6321 1725 $subscribemethodservices[$method][] = $service;
c71ade2f
PL
1726 }
1727 }
1728
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));
1734 }
1735 }
1736
1737 return true;
1738}
1739
1740/**
1741 * Given some sort of Zend Reflection function/method object, return a profile array, ready to be serialized and stored
1742 *
1743 * @param Zend_Server_Reflection_Function_Abstract $function can be any subclass of this object type
1744 *
1745 * @return array
1746 */
1747function admin_mnet_method_profile(Zend_Server_Reflection_Function_Abstract $function) {
f20edd52
PS
1748 $protos = $function->getPrototypes();
1749 $proto = array_pop($protos);
c71ade2f
PL
1750 $ret = $proto->getReturnValue();
1751 $profile = array(
1752 'parameters' => array(),
1753 'return' => array(
1754 'type' => $ret->getType(),
1755 'description' => $ret->getDescription(),
1756 ),
1757 );
1758 foreach ($proto->getParameters() as $p) {
1759 $profile['parameters'][] = array(
1760 'name' => $p->getName(),
1761 'type' => $p->getType(),
1762 'description' => $p->getDescription(),
1763 );
1764 }
1765 return $profile;
1766}