MDL-26104 fix blocknameconflict component in get_string()
[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
db9d4a3d 100/**
101 * Upgrade savepoint, marks end of each upgrade block.
102 * It stores new main version, resets upgrade timeout
103 * and abort upgrade if user cancels page loading.
104 *
105 * Please do not make large upgrade blocks with lots of operations,
106 * for example when adding tables keep only one table operation per block.
107 *
72fb21b6 108 * @global object
db9d4a3d 109 * @param bool $result false if upgrade step failed, true if completed
110 * @param string or float $version main version
111 * @param bool $allowabort allow user to abort script execution here
112 * @return void
113 */
114function upgrade_main_savepoint($result, $version, $allowabort=true) {
115 global $CFG;
116
795a08ad 117 if (!$result) {
520cea92 118 throw new upgrade_exception(null, $version);
795a08ad 119 }
120
121 if ($CFG->version >= $version) {
122 // something really wrong is going on in main upgrade script
123 throw new downgrade_exception(null, $CFG->version, $version);
db9d4a3d 124 }
125
795a08ad 126 set_config('version', $version);
127 upgrade_log(UPGRADE_LOG_NORMAL, null, 'Upgrade savepoint reached');
128
db9d4a3d 129 // reset upgrade timeout to default
130 upgrade_set_timeout();
131
132 // this is a safe place to stop upgrades if user aborts page loading
133 if ($allowabort and connection_aborted()) {
134 die;
135 }
136}
137
138/**
139 * Module upgrade savepoint, marks end of module upgrade blocks
140 * It stores module version, resets upgrade timeout
141 * and abort upgrade if user cancels page loading.
142 *
72fb21b6 143 * @global object
db9d4a3d 144 * @param bool $result false if upgrade step failed, true if completed
145 * @param string or float $version main version
146 * @param string $modname name of module
147 * @param bool $allowabort allow user to abort script execution here
148 * @return void
149 */
150function upgrade_mod_savepoint($result, $version, $modname, $allowabort=true) {
151 global $DB;
152
795a08ad 153 if (!$result) {
520cea92 154 throw new upgrade_exception("mod_$modname", $version);
795a08ad 155 }
156
db9d4a3d 157 if (!$module = $DB->get_record('modules', array('name'=>$modname))) {
158 print_error('modulenotexist', 'debug', '', $modname);
159 }
160
795a08ad 161 if ($module->version >= $version) {
162 // something really wrong is going on in upgrade script
520cea92 163 throw new downgrade_exception("mod_$modname", $module->version, $version);
db9d4a3d 164 }
795a08ad 165 $module->version = $version;
166 $DB->update_record('modules', $module);
520cea92 167 upgrade_log(UPGRADE_LOG_NORMAL, "mod_$modname", 'Upgrade savepoint reached');
db9d4a3d 168
169 // reset upgrade timeout to default
170 upgrade_set_timeout();
171
172 // this is a safe place to stop upgrades if user aborts page loading
173 if ($allowabort and connection_aborted()) {
174 die;
175 }
176}
177
178/**
179 * Blocks upgrade savepoint, marks end of blocks upgrade blocks
180 * It stores block version, resets upgrade timeout
181 * and abort upgrade if user cancels page loading.
182 *
72fb21b6 183 * @global object
db9d4a3d 184 * @param bool $result false if upgrade step failed, true if completed
185 * @param string or float $version main version
186 * @param string $blockname name of block
187 * @param bool $allowabort allow user to abort script execution here
188 * @return void
189 */
795a08ad 190function upgrade_block_savepoint($result, $version, $blockname, $allowabort=true) {
db9d4a3d 191 global $DB;
192
795a08ad 193 if (!$result) {
520cea92 194 throw new upgrade_exception("block_$blockname", $version);
795a08ad 195 }
196
db9d4a3d 197 if (!$block = $DB->get_record('block', array('name'=>$blockname))) {
198 print_error('blocknotexist', 'debug', '', $blockname);
199 }
200
795a08ad 201 if ($block->version >= $version) {
202 // something really wrong is going on in upgrade script
520cea92 203 throw new downgrade_exception("block_$blockname", $block->version, $version);
db9d4a3d 204 }
795a08ad 205 $block->version = $version;
206 $DB->update_record('block', $block);
520cea92 207 upgrade_log(UPGRADE_LOG_NORMAL, "block_$blockname", 'Upgrade savepoint reached');
db9d4a3d 208
209 // reset upgrade timeout to default
210 upgrade_set_timeout();
211
212 // this is a safe place to stop upgrades if user aborts page loading
213 if ($allowabort and connection_aborted()) {
214 die;
215 }
216}
217
218/**
219 * Plugins upgrade savepoint, marks end of blocks upgrade blocks
220 * It stores plugin version, resets upgrade timeout
221 * and abort upgrade if user cancels page loading.
222 *
223 * @param bool $result false if upgrade step failed, true if completed
224 * @param string or float $version main version
225 * @param string $type name of plugin
226 * @param string $dir location of plugin
227 * @param bool $allowabort allow user to abort script execution here
228 * @return void
229 */
17da2e6f 230function upgrade_plugin_savepoint($result, $version, $type, $plugin, $allowabort=true) {
231 $component = $type.'_'.$plugin;
232
795a08ad 233 if (!$result) {
17da2e6f 234 throw new upgrade_exception($component, $version);
db9d4a3d 235 }
236
17da2e6f 237 $installedversion = get_config($component, 'version');
795a08ad 238 if ($installedversion >= $version) {
239 // Something really wrong is going on in the upgrade script
240 throw new downgrade_exception($component, $installedversion, $version);
241 }
17da2e6f 242 set_config('version', $version, $component);
795a08ad 243 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
244
db9d4a3d 245 // Reset upgrade timeout to default
246 upgrade_set_timeout();
247
248 // This is a safe place to stop upgrades if user aborts page loading
249 if ($allowabort and connection_aborted()) {
250 die;
251 }
252}
253
254
255/**
256 * Upgrade plugins
db9d4a3d 257 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
17da2e6f 258 * return void
db9d4a3d 259 */
17da2e6f 260function upgrade_plugins($type, $startcallback, $endcallback, $verbose) {
db9d4a3d 261 global $CFG, $DB;
262
263/// special cases
264 if ($type === 'mod') {
194cdca8 265 return upgrade_plugins_modules($startcallback, $endcallback, $verbose);
795a08ad 266 } else if ($type === 'block') {
194cdca8 267 return upgrade_plugins_blocks($startcallback, $endcallback, $verbose);
db9d4a3d 268 }
269
17da2e6f 270 $plugs = get_plugin_list($type);
db9d4a3d 271
17da2e6f 272 foreach ($plugs as $plug=>$fullplug) {
273 $component = $type.'_'.$plug; // standardised plugin name
db9d4a3d 274
3e858ea7
PS
275 // check plugin dir is valid name
276 $cplug = strtolower($plug);
277 $cplug = clean_param($cplug, PARAM_SAFEDIR);
278 $cplug = str_replace('-', '', $cplug);
279 if ($plug !== $cplug) {
280 throw new plugin_defective_exception($component, 'Invalid plugin directory name.');
281 }
282
795a08ad 283 if (!is_readable($fullplug.'/version.php')) {
284 continue;
db9d4a3d 285 }
286
365a5941 287 $plugin = new stdClass();
795a08ad 288 require($fullplug.'/version.php'); // defines $plugin with version etc
db9d4a3d 289
3e858ea7
PS
290 // if plugin tells us it's full name we may check the location
291 if (isset($plugin->component)) {
292 if ($plugin->component !== $component) {
293 throw new plugin_defective_exception($component, 'Plugin installed in wrong folder.');
294 }
295 }
296
795a08ad 297 if (empty($plugin->version)) {
298 throw new plugin_defective_exception($component, 'Missing version value in version.php');
db9d4a3d 299 }
300
17da2e6f 301 $plugin->name = $plug;
302 $plugin->fullname = $component;
795a08ad 303
304
db9d4a3d 305 if (!empty($plugin->requires)) {
306 if ($plugin->requires > $CFG->version) {
795a08ad 307 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
ef14c1e7
PS
308 } else if ($plugin->requires < 2010000000) {
309 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
db9d4a3d 310 }
311 }
312
9b683d13 313 // try to recover from interrupted install.php if needed
314 if (file_exists($fullplug.'/db/install.php')) {
315 if (get_config($plugin->fullname, 'installrunning')) {
316 require_once($fullplug.'/db/install.php');
317 $recover_install_function = 'xmldb_'.$plugin->fullname.'_install_recovery';
318 if (function_exists($recover_install_function)) {
319 $startcallback($component, true, $verbose);
320 $recover_install_function();
f6f06d69 321 unset_config('installrunning', $plugin->fullname);
9b683d13 322 update_capabilities($component);
c6d75bff 323 log_update_descriptions($component);
c976e271 324 external_update_descriptions($component);
9b683d13 325 events_update_definition($component);
326 message_update_providers($component);
de260e0f 327 upgrade_plugin_mnet_functions($component);
9b683d13 328 $endcallback($component, true, $verbose);
329 }
330 }
331 }
db9d4a3d 332
9b683d13 333 $installedversion = get_config($plugin->fullname, 'version');
795a08ad 334 if (empty($installedversion)) { // new installation
194cdca8 335 $startcallback($component, true, $verbose);
db9d4a3d 336
795a08ad 337 /// Install tables if defined
338 if (file_exists($fullplug.'/db/install.xml')) {
339 $DB->get_manager()->install_from_xmldb_file($fullplug.'/db/install.xml');
db9d4a3d 340 }
9b683d13 341
342 /// store version
343 upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false);
344
795a08ad 345 /// execute post install file
346 if (file_exists($fullplug.'/db/install.php')) {
347 require_once($fullplug.'/db/install.php');
f6f06d69
PS
348 set_config('installrunning', 1, $plugin->fullname);
349 $post_install_function = 'xmldb_'.$plugin->fullname.'_install';
795a08ad 350 $post_install_function();
f6f06d69 351 unset_config('installrunning', $plugin->fullname);
795a08ad 352 }
353
795a08ad 354 /// Install various components
355 update_capabilities($component);
c6d75bff 356 log_update_descriptions($component);
c976e271 357 external_update_descriptions($component);
795a08ad 358 events_update_definition($component);
359 message_update_providers($component);
de260e0f 360 upgrade_plugin_mnet_functions($component);
795a08ad 361
f87eab7e 362 purge_all_caches();
194cdca8 363 $endcallback($component, true, $verbose);
795a08ad 364
365 } else if ($installedversion < $plugin->version) { // upgrade
366 /// Run the upgrade function for the plugin.
194cdca8 367 $startcallback($component, false, $verbose);
795a08ad 368
369 if (is_readable($fullplug.'/db/upgrade.php')) {
370 require_once($fullplug.'/db/upgrade.php'); // defines upgrading function
371
372 $newupgrade_function = 'xmldb_'.$plugin->fullname.'_upgrade';
373 $result = $newupgrade_function($installedversion);
374 } else {
375 $result = true;
376 }
377
378 $installedversion = get_config($plugin->fullname, 'version');
379 if ($installedversion < $plugin->version) {
380 // store version if not already there
381 upgrade_plugin_savepoint($result, $plugin->version, $type, $plug, false);
382 }
383
384 /// Upgrade various components
385 update_capabilities($component);
c6d75bff 386 log_update_descriptions($component);
c976e271 387 external_update_descriptions($component);
795a08ad 388 events_update_definition($component);
389 message_update_providers($component);
de260e0f 390 upgrade_plugin_mnet_functions($component);
795a08ad 391
f87eab7e 392 purge_all_caches();
194cdca8 393 $endcallback($component, false, $verbose);
795a08ad 394
395 } else if ($installedversion > $plugin->version) {
396 throw new downgrade_exception($component, $installedversion, $plugin->version);
db9d4a3d 397 }
398 }
db9d4a3d 399}
400
401/**
402 * Find and check all modules and load them up or upgrade them if necessary
72fb21b6 403 *
404 * @global object
405 * @global object
db9d4a3d 406 */
194cdca8 407function upgrade_plugins_modules($startcallback, $endcallback, $verbose) {
db9d4a3d 408 global $CFG, $DB;
409
17da2e6f 410 $mods = get_plugin_list('mod');
db9d4a3d 411
17da2e6f 412 foreach ($mods as $mod=>$fullmod) {
db9d4a3d 413
3e858ea7 414 if ($mod === 'NEWMODULE') { // Someone has unzipped the template, ignore it
db9d4a3d 415 continue;
416 }
417
17da2e6f 418 $component = 'mod_'.$mod;
db9d4a3d 419
3e858ea7
PS
420 // check module dir is valid name
421 $cmod = strtolower($mod);
422 $cmod = clean_param($cmod, PARAM_SAFEDIR);
423 $cmod = str_replace('-', '', $cmod);
424 $cmod = str_replace('_', '', $cmod); // modules MUST not have '_' in name and never will, sorry
425 if ($mod !== $cmod) {
426 throw new plugin_defective_exception($component, 'Invalid plugin directory name.');
427 }
428
795a08ad 429 if (!is_readable($fullmod.'/version.php')) {
430 throw new plugin_defective_exception($component, 'Missing version.php');
db9d4a3d 431 }
432
365a5941 433 $module = new stdClass();
795a08ad 434 require($fullmod .'/version.php'); // defines $module with version etc
db9d4a3d 435
3e858ea7
PS
436 // if plugin tells us it's full name we may check the location
437 if (isset($module->component)) {
438 if ($module->component !== $component) {
439 throw new plugin_defective_exception($component, 'Plugin installed in wrong folder.');
440 }
441 }
442
795a08ad 443 if (empty($module->version)) {
c1fe2368 444 if (isset($module->version)) {
445 // Version is empty but is set - it means its value is 0 or ''. Let us skip such module.
b921b6ee 446 // This is intended for developers so they can work on the early stages of the module.
c1fe2368 447 continue;
448 }
795a08ad 449 throw new plugin_defective_exception($component, 'Missing version value in version.php');
db9d4a3d 450 }
451
452 if (!empty($module->requires)) {
453 if ($module->requires > $CFG->version) {
795a08ad 454 throw new upgrade_requires_exception($component, $module->version, $CFG->version, $module->requires);
ef14c1e7
PS
455 } else if ($module->requires < 2010000000) {
456 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
db9d4a3d 457 }
458 }
459
3e858ea7
PS
460 // all modules must have en lang pack
461 if (!is_readable("$fullmod/lang/en/$mod.php")) {
462 throw new plugin_defective_exception($component, 'Missing mandatory en language pack.');
463 }
464
db9d4a3d 465 $module->name = $mod; // The name MUST match the directory
466
795a08ad 467 $currmodule = $DB->get_record('modules', array('name'=>$module->name));
db9d4a3d 468
9b683d13 469 if (file_exists($fullmod.'/db/install.php')) {
470 if (get_config($module->name, 'installrunning')) {
471 require_once($fullmod.'/db/install.php');
472 $recover_install_function = 'xmldb_'.$module->name.'_install_recovery';
473 if (function_exists($recover_install_function)) {
474 $startcallback($component, true, $verbose);
475 $recover_install_function();
476 unset_config('installrunning', $module->name);
477 // Install various components too
478 update_capabilities($component);
c6d75bff 479 log_update_descriptions($component);
c976e271 480 external_update_descriptions($component);
9b683d13 481 events_update_definition($component);
482 message_update_providers($component);
de260e0f 483 upgrade_plugin_mnet_functions($component);
9b683d13 484 $endcallback($component, true, $verbose);
485 }
486 }
487 }
488
795a08ad 489 if (empty($currmodule->version)) {
194cdca8 490 $startcallback($component, true, $verbose);
db9d4a3d 491
795a08ad 492 /// Execute install.xml (XMLDB) - must be present in all modules
493 $DB->get_manager()->install_from_xmldb_file($fullmod.'/db/install.xml');
db9d4a3d 494
2e3da297 495 /// Add record into modules table - may be needed in install.php already
496 $module->id = $DB->insert_record('modules', $module);
497
db9d4a3d 498 /// Post installation hook - optional
499 if (file_exists("$fullmod/db/install.php")) {
500 require_once("$fullmod/db/install.php");
9b683d13 501 // Set installation running flag, we need to recover after exception or error
502 set_config('installrunning', 1, $module->name);
db9d4a3d 503 $post_install_function = 'xmldb_'.$module->name.'_install';;
504 $post_install_function();
9b683d13 505 unset_config('installrunning', $module->name);
db9d4a3d 506 }
507
795a08ad 508 /// Install various components
509 update_capabilities($component);
c6d75bff 510 log_update_descriptions($component);
c976e271 511 external_update_descriptions($component);
795a08ad 512 events_update_definition($component);
513 message_update_providers($component);
de260e0f 514 upgrade_plugin_mnet_functions($component);
795a08ad 515
f87eab7e 516 purge_all_caches();
194cdca8 517 $endcallback($component, true, $verbose);
db9d4a3d 518
795a08ad 519 } else if ($currmodule->version < $module->version) {
520 /// If versions say that we need to upgrade but no upgrade files are available, notify and continue
194cdca8 521 $startcallback($component, false, $verbose);
795a08ad 522
523 if (is_readable($fullmod.'/db/upgrade.php')) {
524 require_once($fullmod.'/db/upgrade.php'); // defines new upgrading function
525 $newupgrade_function = 'xmldb_'.$module->name.'_upgrade';
526 $result = $newupgrade_function($currmodule->version, $module);
527 } else {
528 $result = true;
529 }
530
531 $currmodule = $DB->get_record('modules', array('name'=>$module->name));
532 if ($currmodule->version < $module->version) {
533 // store version if not already there
534 upgrade_mod_savepoint($result, $module->version, $mod, false);
535 }
536
537 /// Upgrade various components
538 update_capabilities($component);
c6d75bff 539 log_update_descriptions($component);
c976e271 540 external_update_descriptions($component);
795a08ad 541 events_update_definition($component);
542 message_update_providers($component);
de260e0f 543 upgrade_plugin_mnet_functions($component);
795a08ad 544
f87eab7e 545 purge_all_caches();
795a08ad 546
194cdca8 547 $endcallback($component, false, $verbose);
795a08ad 548
549 } else if ($currmodule->version > $module->version) {
550 throw new downgrade_exception($component, $currmodule->version, $module->version);
551 }
552 }
553}
db9d4a3d 554
db9d4a3d 555
795a08ad 556/**
557 * This function finds all available blocks and install them
558 * into blocks table or do all the upgrade process if newer.
72fb21b6 559 *
560 * @global object
561 * @global object
795a08ad 562 */
194cdca8 563function upgrade_plugins_blocks($startcallback, $endcallback, $verbose) {
795a08ad 564 global $CFG, $DB;
565
795a08ad 566 require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
567
568 $blocktitles = array(); // we do not want duplicate titles
569
570 //Is this a first install
571 $first_install = null;
572
17da2e6f 573 $blocks = get_plugin_list('block');
795a08ad 574
17da2e6f 575 foreach ($blocks as $blockname=>$fullblock) {
795a08ad 576
577 if (is_null($first_install)) {
f46eba7e 578 $first_install = ($DB->count_records('block_instances') == 0);
795a08ad 579 }
580
581 if ($blockname == 'NEWBLOCK') { // Someone has unzipped the template, ignore it
582 continue;
db9d4a3d 583 }
584
17da2e6f 585 $component = 'block_'.$blockname;
db9d4a3d 586
3e858ea7
PS
587 // check block dir is valid name
588 $cblockname = strtolower($blockname);
589 $cblockname = clean_param($cblockname, PARAM_SAFEDIR);
590 $cblockname = str_replace('-', '', $cblockname);
591 if ($blockname !== $cblockname) {
592 throw new plugin_defective_exception($component, 'Invalid plugin directory name.');
593 }
594
8571833f
PS
595 if (!is_readable($fullblock.'/version.php')) {
596 throw new plugin_defective_exception('block/'.$blockname, 'Missing version.php file.');
597 }
365a5941 598 $plugin = new stdClass();
8571833f
PS
599 $plugin->version = NULL;
600 $plugin->cron = 0;
601 include($fullblock.'/version.php');
602 $block = $plugin;
603
3e858ea7
PS
604 // if plugin tells us it's full name we may check the location
605 if (isset($block->component)) {
606 if ($block->component !== $component) {
607 throw new plugin_defective_exception($component, 'Plugin installed in wrong folder.');
608 }
609 }
610
ef14c1e7
PS
611 if (!empty($plugin->requires)) {
612 if ($plugin->requires > $CFG->version) {
613 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
614 } else if ($plugin->requires < 2010000000) {
615 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
616 }
617 }
618
795a08ad 619 if (!is_readable($fullblock.'/block_'.$blockname.'.php')) {
620 throw new plugin_defective_exception('block/'.$blockname, 'Missing main block class file.');
db9d4a3d 621 }
8571833f 622 include_once($fullblock.'/block_'.$blockname.'.php');
db9d4a3d 623
795a08ad 624 $classname = 'block_'.$blockname;
625
626 if (!class_exists($classname)) {
627 throw new plugin_defective_exception($component, 'Can not load main class.');
628 }
629
b921b6ee 630 $blockobj = new $classname; // This is what we'll be testing
795a08ad 631 $blocktitle = $blockobj->get_title();
632
633 // OK, it's as we all hoped. For further tests, the object will do them itself.
634 if (!$blockobj->_self_test()) {
635 throw new plugin_defective_exception($component, 'Self test failed.');
636 }
637
795a08ad 638 $block->name = $blockname; // The name MUST match the directory
795a08ad 639
640 if (empty($block->version)) {
641 throw new plugin_defective_exception($component, 'Missing block version.');
642 }
643
644 $currblock = $DB->get_record('block', array('name'=>$block->name));
645
9b683d13 646 if (file_exists($fullblock.'/db/install.php')) {
647 if (get_config('block_'.$blockname, 'installrunning')) {
648 require_once($fullblock.'/db/install.php');
649 $recover_install_function = 'xmldb_block_'.$blockname.'_install_recovery';
650 if (function_exists($recover_install_function)) {
651 $startcallback($component, true, $verbose);
652 $recover_install_function();
653 unset_config('installrunning', 'block_'.$blockname);
654 // Install various components
655 update_capabilities($component);
c6d75bff 656 log_update_descriptions($component);
c976e271 657 external_update_descriptions($component);
9b683d13 658 events_update_definition($component);
659 message_update_providers($component);
de260e0f 660 upgrade_plugin_mnet_functions($component);
9b683d13 661 $endcallback($component, true, $verbose);
662 }
663 }
664 }
665
795a08ad 666 if (empty($currblock->version)) { // block not installed yet, so install it
795a08ad 667 $conflictblock = array_search($blocktitle, $blocktitles);
668 if ($conflictblock !== false) {
669 // Duplicate block titles are not allowed, they confuse people
670 // AND PHP's associative arrays ;)
c1ddac83 671 throw new plugin_defective_exception($component, get_string('blocknameconflict', 'error', (object)array('name'=>$block->name, 'conflict'=>$conflictblock)));
795a08ad 672 }
194cdca8 673 $startcallback($component, true, $verbose);
795a08ad 674
675 if (file_exists($fullblock.'/db/install.xml')) {
676 $DB->get_manager()->install_from_xmldb_file($fullblock.'/db/install.xml');
677 }
678 $block->id = $DB->insert_record('block', $block);
679
680 if (file_exists($fullblock.'/db/install.php')) {
681 require_once($fullblock.'/db/install.php');
9b683d13 682 // Set installation running flag, we need to recover after exception or error
683 set_config('installrunning', 1, 'block_'.$blockname);
795a08ad 684 $post_install_function = 'xmldb_block_'.$blockname.'_install';;
685 $post_install_function();
9b683d13 686 unset_config('installrunning', 'block_'.$blockname);
795a08ad 687 }
688
689 $blocktitles[$block->name] = $blocktitle;
690
691 // Install various components
692 update_capabilities($component);
c6d75bff 693 log_update_descriptions($component);
c976e271 694 external_update_descriptions($component);
795a08ad 695 events_update_definition($component);
696 message_update_providers($component);
de260e0f 697 upgrade_plugin_mnet_functions($component);
795a08ad 698
f87eab7e 699 purge_all_caches();
194cdca8 700 $endcallback($component, true, $verbose);
795a08ad 701
702 } else if ($currblock->version < $block->version) {
194cdca8 703 $startcallback($component, false, $verbose);
795a08ad 704
705 if (is_readable($fullblock.'/db/upgrade.php')) {
706 require_once($fullblock.'/db/upgrade.php'); // defines new upgrading function
707 $newupgrade_function = 'xmldb_block_'.$blockname.'_upgrade';
708 $result = $newupgrade_function($currblock->version, $block);
709 } else {
710 $result = true;
711 }
712
713 $currblock = $DB->get_record('block', array('name'=>$block->name));
714 if ($currblock->version < $block->version) {
715 // store version if not already there
716 upgrade_block_savepoint($result, $block->version, $block->name, false);
717 }
718
719 if ($currblock->cron != $block->cron) {
720 // update cron flag if needed
721 $currblock->cron = $block->cron;
722 $DB->update_record('block', $currblock);
723 }
724
b921b6ee 725 // Upgrade various components
795a08ad 726 update_capabilities($component);
c6d75bff 727 log_update_descriptions($component);
c976e271 728 external_update_descriptions($component);
17da2e6f 729 events_update_definition($component);
795a08ad 730 message_update_providers($component);
de260e0f 731 upgrade_plugin_mnet_functions($component);
795a08ad 732
f87eab7e 733 purge_all_caches();
194cdca8 734 $endcallback($component, false, $verbose);
795a08ad 735
736 } else if ($currblock->version > $block->version) {
737 throw new downgrade_exception($component, $currblock->version, $block->version);
738 }
739 }
740
741
742 // Finally, if we are in the first_install of BLOCKS setup frontpage and admin page blocks
743 if ($first_install) {
795a08ad 744 //Iterate over each course - there should be only site course here now
745 if ($courses = $DB->get_records('course')) {
746 foreach ($courses as $course) {
9d1d606e 747 blocks_add_default_course_blocks($course);
db9d4a3d 748 }
749 }
795a08ad 750
9d1d606e 751 blocks_add_default_system_blocks();
795a08ad 752 }
753}
754
c6d75bff
PS
755
756/**
757 * Log_display description function used during install and upgrade.
758 *
759 * @param string $component name of component (moodle, mod_assignment, etc.)
760 * @return void
761 */
762function log_update_descriptions($component) {
763 global $DB;
764
765 $defpath = get_component_directory($component).'/db/log.php';
766
767 if (!file_exists($defpath)) {
768 $DB->delete_records('log_display', array('component'=>$component));
769 return;
770 }
771
772 // load new info
773 $logs = array();
774 include($defpath);
775 $newlogs = array();
776 foreach ($logs as $log) {
777 $newlogs[$log['module'].'-'.$log['action']] = $log; // kind of unique name
778 }
779 unset($logs);
780 $logs = $newlogs;
781
782 $fields = array('module', 'action', 'mtable', 'field');
783 // update all log fist
784 $dblogs = $DB->get_records('log_display', array('component'=>$component));
785 foreach ($dblogs as $dblog) {
786 $name = $dblog->module.'-'.$dblog->action;
787
788 if (empty($logs[$name])) {
789 $DB->delete_records('log_display', array('id'=>$dblog->id));
790 continue;
791 }
792
793 $log = $logs[$name];
794 unset($logs[$name]);
795
796 $update = false;
797 foreach ($fields as $field) {
798 if ($dblog->$field != $log[$field]) {
799 $dblog->$field = $log[$field];
800 $update = true;
801 }
802 }
803 if ($update) {
804 $DB->update_record('log_display', $dblog);
805 }
806 }
807 foreach ($logs as $log) {
808 $dblog = (object)$log;
809 $dblog->component = $component;
810 $DB->insert_record('log_display', $dblog);
811 }
812}
813
c976e271 814/**
815 * Web service discovery function used during install and upgrade.
816 * @param string $component name of component (moodle, mod_assignment, etc.)
817 * @return void
818 */
819function external_update_descriptions($component) {
820 global $DB;
821
822 $defpath = get_component_directory($component).'/db/services.php';
823
824 if (!file_exists($defpath)) {
825 external_delete_descriptions($component);
826 return;
827 }
828
829 // load new info
830 $functions = array();
831 $services = array();
832 include($defpath);
833
834 // update all function fist
835 $dbfunctions = $DB->get_records('external_functions', array('component'=>$component));
836 foreach ($dbfunctions as $dbfunction) {
837 if (empty($functions[$dbfunction->name])) {
838 $DB->delete_records('external_functions', array('id'=>$dbfunction->id));
839 // do not delete functions from external_services_functions, beacuse
840 // we want to notify admins when functions used in custom services disappear
c6d75bff
PS
841
842 //TODO: this looks wrong, we have to delete it eventually (skodak)
c976e271 843 continue;
844 }
845
846 $function = $functions[$dbfunction->name];
847 unset($functions[$dbfunction->name]);
848 $function['classpath'] = empty($function['classpath']) ? null : $function['classpath'];
849
850 $update = false;
851 if ($dbfunction->classname != $function['classname']) {
852 $dbfunction->classname = $function['classname'];
853 $update = true;
854 }
855 if ($dbfunction->methodname != $function['methodname']) {
856 $dbfunction->methodname = $function['methodname'];
857 $update = true;
858 }
859 if ($dbfunction->classpath != $function['classpath']) {
860 $dbfunction->classpath = $function['classpath'];
861 $update = true;
862 }
72f68b51 863 $functioncapabilities = key_exists('capabilities', $function)?$function['capabilities']:'';
864 if ($dbfunction->capabilities != $functioncapabilities) {
865 $dbfunction->capabilities = $functioncapabilities;
866 $update = true;
867 }
c976e271 868 if ($update) {
869 $DB->update_record('external_functions', $dbfunction);
870 }
871 }
872 foreach ($functions as $fname => $function) {
365a5941 873 $dbfunction = new stdClass();
c976e271 874 $dbfunction->name = $fname;
875 $dbfunction->classname = $function['classname'];
876 $dbfunction->methodname = $function['methodname'];
877 $dbfunction->classpath = empty($function['classpath']) ? null : $function['classpath'];
878 $dbfunction->component = $component;
72f68b51 879 $dbfunction->capabilities = key_exists('capabilities', $function)?$function['capabilities']:'';
c976e271 880 $dbfunction->id = $DB->insert_record('external_functions', $dbfunction);
881 }
882 unset($functions);
883
884 // now deal with services
885 $dbservices = $DB->get_records('external_services', array('component'=>$component));
886 foreach ($dbservices as $dbservice) {
887 if (empty($services[$dbservice->name])) {
888 $DB->delete_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
889 $DB->delete_records('external_services_users', array('externalserviceid'=>$dbservice->id));
890 $DB->delete_records('external_services', array('id'=>$dbservice->id));
891 continue;
892 }
893 $service = $services[$dbservice->name];
894 unset($services[$dbservice->name]);
895 $service['enabled'] = empty($service['enabled']) ? 0 : $service['enabled'];
896 $service['requiredcapability'] = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
897 $service['restrictedusers'] = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
898
899 $update = false;
900 if ($dbservice->enabled != $service['enabled']) {
901 $dbservice->enabled = $service['enabled'];
902 $update = true;
903 }
904 if ($dbservice->requiredcapability != $service['requiredcapability']) {
905 $dbservice->requiredcapability = $service['requiredcapability'];
906 $update = true;
907 }
908 if ($dbservice->restrictedusers != $service['restrictedusers']) {
909 $dbservice->restrictedusers = $service['restrictedusers'];
910 $update = true;
911 }
912 if ($update) {
913 $DB->update_record('external_services', $dbservice);
914 }
915
916 $functions = $DB->get_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
917 foreach ($functions as $function) {
918 $key = array_search($function->functionname, $service['functions']);
919 if ($key === false) {
920 $DB->delete_records('external_services_functions', array('id'=>$function->id));
921 } else {
922 unset($service['functions'][$key]);
923 }
924 }
925 foreach ($service['functions'] as $fname) {
365a5941 926 $newf = new stdClass();
c976e271 927 $newf->externalserviceid = $dbservice->id;
928 $newf->functionname = $fname;
929 $DB->insert_record('external_services_functions', $newf);
930 }
931 unset($functions);
932 }
933 foreach ($services as $name => $service) {
365a5941 934 $dbservice = new stdClass();
c976e271 935 $dbservice->name = $name;
936 $dbservice->enabled = empty($service['enabled']) ? 0 : $service['enabled'];
937 $dbservice->requiredcapability = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
938 $dbservice->restrictedusers = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
939 $dbservice->component = $component;
e5180580 940 $dbservice->timecreated = time();
c976e271 941 $dbservice->id = $DB->insert_record('external_services', $dbservice);
942 foreach ($service['functions'] as $fname) {
365a5941 943 $newf = new stdClass();
c976e271 944 $newf->externalserviceid = $dbservice->id;
945 $newf->functionname = $fname;
946 $DB->insert_record('external_services_functions', $newf);
947 }
948 }
949}
950
951/**
b921b6ee 952 * Delete all service and external functions information defined in the specified component.
c976e271 953 * @param string $component name of component (moodle, mod_assignment, etc.)
954 * @return void
955 */
956function external_delete_descriptions($component) {
957 global $DB;
958
959 $params = array($component);
960
961 $DB->delete_records_select('external_services_users', "externalserviceid IN (SELECT id FROM {external_services} WHERE component = ?)", $params);
962 $DB->delete_records_select('external_services_functions', "externalserviceid IN (SELECT id FROM {external_services} WHERE component = ?)", $params);
963 $DB->delete_records('external_services', array('component'=>$component));
964 $DB->delete_records('external_functions', array('component'=>$component));
965}
966
72fb21b6 967/**
968 * upgrade logging functions
72fb21b6 969 */
fd1a792e 970function upgrade_handle_exception($ex, $plugin = null) {
96e1fdf4
PS
971 global $CFG;
972
a56c457e
PS
973 // rollback everything, we need to log all upgrade problems
974 abort_all_db_transactions();
975
c19bc39c
PS
976 $info = get_exception_info($ex);
977
978 // First log upgrade error
979 upgrade_log(UPGRADE_LOG_ERROR, $plugin, 'Exception: ' . get_class($ex), $info->message, $info->backtrace);
980
981 // Always turn on debugging - admins need to know what is going on
982 $CFG->debug = DEBUG_DEVELOPER;
983
fd1a792e 984 default_exception_handler($ex, true, $plugin);
db9d4a3d 985}
986
987/**
988 * Adds log entry into upgrade_log table
989 *
72fb21b6 990 * @global object
991 * @global object
992 * @global object
db9d4a3d 993 * @param int $type UPGRADE_LOG_NORMAL, UPGRADE_LOG_NOTICE or UPGRADE_LOG_ERROR
994 * @param string $plugin plugin or null if main
995 * @param string $info short description text of log entry
996 * @param string $details long problem description
997 * @param string $backtrace string used for errors only
998 * @return void
999 */
1000function upgrade_log($type, $plugin, $info, $details=null, $backtrace=null) {
1001 global $DB, $USER, $CFG;
1002
795a08ad 1003 $plugin = ($plugin==='moodle') ? null : $plugin;
1004
34a2777c 1005 $backtrace = format_backtrace($backtrace, true);
db9d4a3d 1006
1007 $version = null;
1008
1009 //first try to find out current version number
795a08ad 1010 if (empty($plugin) or $plugin === 'moodle') {
db9d4a3d 1011 //main
1012 $version = $CFG->version;
1013
795a08ad 1014 } else if ($plugin === 'local') {
1015 //customisation
1016 $version = $CFG->local_version;
1017
1018 } else if (strpos($plugin, 'mod/') === 0) {
db9d4a3d 1019 try {
1020 $modname = substr($plugin, strlen('mod/'));
1021 $version = $DB->get_field('modules', 'version', array('name'=>$modname));
795a08ad 1022 $version = ($version === false) ? null : $version;
db9d4a3d 1023 } catch (Exception $ignored) {
1024 }
1025
795a08ad 1026 } else if (strpos($plugin, 'block/') === 0) {
db9d4a3d 1027 try {
795a08ad 1028 $blockname = substr($plugin, strlen('block/'));
db9d4a3d 1029 if ($block = $DB->get_record('block', array('name'=>$blockname))) {
1030 $version = $block->version;
1031 }
1032 } catch (Exception $ignored) {
1033 }
795a08ad 1034
1035 } else {
1036 $pluginversion = get_config(str_replace('/', '_', $plugin), 'version');
1037 if (!empty($pluginversion)) {
1038 $version = $pluginversion;
1039 }
db9d4a3d 1040 }
1041
365a5941 1042 $log = new stdClass();
db9d4a3d 1043 $log->type = $type;
1044 $log->plugin = $plugin;
1045 $log->version = $version;
1046 $log->info = $info;
1047 $log->details = $details;
1048 $log->backtrace = $backtrace;
1049 $log->userid = $USER->id;
1050 $log->timemodified = time();
db9d4a3d 1051 try {
1052 $DB->insert_record('upgrade_log', $log);
1053 } catch (Exception $ignored) {
795a08ad 1054 // possible during install or 2.0 upgrade
db9d4a3d 1055 }
1056}
1057
1058/**
1059 * Marks start of upgrade, blocks any other access to site.
1060 * The upgrade is finished at the end of script or after timeout.
72fb21b6 1061 *
1062 * @global object
1063 * @global object
1064 * @global object
db9d4a3d 1065 */
1066function upgrade_started($preinstall=false) {
de6d81e6 1067 global $CFG, $DB, $PAGE, $OUTPUT;
db9d4a3d 1068
1069 static $started = false;
1070
1071 if ($preinstall) {
1072 ignore_user_abort(true);
1073 upgrade_setup_debug(true);
1074
1075 } else if ($started) {
1076 upgrade_set_timeout(120);
1077
1078 } else {
c13a5e71 1079 if (!CLI_SCRIPT and !$PAGE->headerprinted) {
db9d4a3d 1080 $strupgrade = get_string('upgradingversion', 'admin');
78946b9b 1081 $PAGE->set_pagelayout('maintenance');
543f54d3 1082 upgrade_init_javascript();
de6d81e6 1083 $PAGE->set_title($strupgrade.' - Moodle '.$CFG->target_release);
1084 $PAGE->set_heading($strupgrade);
1085 $PAGE->navbar->add($strupgrade);
1086 $PAGE->set_cacheable(false);
1087 echo $OUTPUT->header();
db9d4a3d 1088 }
1089
1090 ignore_user_abort(true);
1091 register_shutdown_function('upgrade_finished_handler');
1092 upgrade_setup_debug(true);
1093 set_config('upgraderunning', time()+300);
1094 $started = true;
1095 }
1096}
1097
1098/**
b921b6ee 1099 * Internal function - executed if upgrade interrupted.
db9d4a3d 1100 */
1101function upgrade_finished_handler() {
1102 upgrade_finished();
1103}
1104
1105/**
1106 * Indicates upgrade is finished.
1107 *
1108 * This function may be called repeatedly.
72fb21b6 1109 *
1110 * @global object
1111 * @global object
db9d4a3d 1112 */
1113function upgrade_finished($continueurl=null) {
7e0d6675 1114 global $CFG, $DB, $OUTPUT;
db9d4a3d 1115
1116 if (!empty($CFG->upgraderunning)) {
1117 unset_config('upgraderunning');
1118 upgrade_setup_debug(false);
1119 ignore_user_abort(false);
1120 if ($continueurl) {
aa9a6867 1121 echo $OUTPUT->continue_button($continueurl);
7e0d6675 1122 echo $OUTPUT->footer();
db9d4a3d 1123 die;
1124 }
1125 }
1126}
1127
72fb21b6 1128/**
1129 * @global object
1130 * @global object
1131 */
db9d4a3d 1132function upgrade_setup_debug($starting) {
1133 global $CFG, $DB;
1134
1135 static $originaldebug = null;
1136
1137 if ($starting) {
1138 if ($originaldebug === null) {
1139 $originaldebug = $DB->get_debug();
1140 }
1141 if (!empty($CFG->upgradeshowsql)) {
1142 $DB->set_debug(true);
1143 }
1144 } else {
1145 $DB->set_debug($originaldebug);
1146 }
1147}
1148
72fb21b6 1149/**
1150 * @global object
1151 */
90509582 1152function print_upgrade_reload($url) {
f2a1963c 1153 global $OUTPUT;
90509582 1154
1155 echo "<br />";
1156 echo '<div class="continuebutton">';
b5d0cafc 1157 echo '<a href="'.$url.'" title="'.get_string('reload').'" ><img src="'.$OUTPUT->pix_url('i/reload') . '" alt="" /> '.get_string('reload').'</a>';
90509582 1158 echo '</div><br />';
1159}
1160
db9d4a3d 1161function print_upgrade_separator() {
1162 if (!CLI_SCRIPT) {
1163 echo '<hr />';
1164 }
1165}
1166
795a08ad 1167/**
1168 * Default start upgrade callback
1169 * @param string $plugin
b921b6ee 1170 * @param bool $installation true if installation, false means upgrade
795a08ad 1171 */
194cdca8 1172function print_upgrade_part_start($plugin, $installation, $verbose) {
3c159385 1173 global $OUTPUT;
795a08ad 1174 if (empty($plugin) or $plugin == 'moodle') {
1175 upgrade_started($installation); // does not store upgrade running flag yet
194cdca8 1176 if ($verbose) {
3c159385 1177 echo $OUTPUT->heading(get_string('coresystem'));
194cdca8 1178 }
795a08ad 1179 } else {
1180 upgrade_started();
194cdca8 1181 if ($verbose) {
3c159385 1182 echo $OUTPUT->heading($plugin);
194cdca8 1183 }
795a08ad 1184 }
1185 if ($installation) {
1186 if (empty($plugin) or $plugin == 'moodle') {
1187 // no need to log - log table not yet there ;-)
1188 } else {
1189 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin installation');
1190 }
1191 } else {
1192 if (empty($plugin) or $plugin == 'moodle') {
1193 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting core upgrade');
1194 } else {
1195 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin upgrade');
1196 }
1197 }
1198}
1199
1200/**
1201 * Default end upgrade callback
1202 * @param string $plugin
b921b6ee 1203 * @param bool $installation true if installation, false means upgrade
795a08ad 1204 */
194cdca8 1205function print_upgrade_part_end($plugin, $installation, $verbose) {
aa9a6867 1206 global $OUTPUT;
795a08ad 1207 upgrade_started();
1208 if ($installation) {
1209 if (empty($plugin) or $plugin == 'moodle') {
1210 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core installed');
1211 } else {
1212 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin installed');
1213 }
1214 } else {
1215 if (empty($plugin) or $plugin == 'moodle') {
1216 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core upgraded');
1217 } else {
1218 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin upgraded');
1219 }
1220 }
194cdca8 1221 if ($verbose) {
aa9a6867 1222 echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
194cdca8 1223 print_upgrade_separator();
96db14f9 1224 }
1225}
1226
72fb21b6 1227/**
b921b6ee 1228 * Sets up JS code required for all upgrade scripts.
72fb21b6 1229 * @global object
1230 */
543f54d3 1231function upgrade_init_javascript() {
e29380f3 1232 global $PAGE;
543f54d3
PS
1233 // scroll to the end of each upgrade page so that ppl see either error or continue button,
1234 // no need to scroll continuously any more, it is enough to jump to end once the footer is printed ;-)
1235 $js = "window.scrollTo(0, 5000000);";
1236 $PAGE->requires->js_init_code($js);
db9d4a3d 1237}
1238
1239
1240/**
1241 * Try to upgrade the given language pack (or current language)
af8d4d91
DM
1242 *
1243 * @todo hardcoded Moodle version here - shall be provided by version.php or similar script
db9d4a3d 1244 */
1245function upgrade_language_pack($lang='') {
3c159385 1246 global $CFG, $OUTPUT;
db9d4a3d 1247
af8d4d91
DM
1248 get_string_manager()->reset_caches();
1249
db9d4a3d 1250 if (empty($lang)) {
1251 $lang = current_language();
1252 }
1253
3a915b06 1254 if ($lang == 'en') {
db9d4a3d 1255 return true; // Nothing to do
1256 }
1257
551fe0e5 1258 upgrade_started(false);
3c159385 1259 echo $OUTPUT->heading(get_string('langimport', 'admin').': '.$lang);
db9d4a3d 1260
1261 @mkdir ($CFG->dataroot.'/temp/'); //make it in case it's a fresh install, it might not be there
1262 @mkdir ($CFG->dataroot.'/lang/');
1263
1264 require_once($CFG->libdir.'/componentlib.class.php');
af8d4d91
DM
1265
1266 if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $lang.'.zip', 'languages.md5', 'lang')) {
db9d4a3d 1267 $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED)
1268
1269 if ($status == COMPONENT_INSTALLED) {
af8d4d91 1270 remove_dir($CFG->dataroot.'/cache/languages');
551fe0e5 1271 if ($parentlang = get_parent_language($lang)) {
af8d4d91 1272 if ($cd = new component_installer('http://download.moodle.org', 'langpack/2.0', $parentlang.'.zip', 'languages.md5', 'lang')) {
551fe0e5 1273 $cd->install();
1274 }
1275 }
aa9a6867 1276 echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
db9d4a3d 1277 }
1278 }
af8d4d91
DM
1279
1280 get_string_manager()->reset_caches();
1281
551fe0e5 1282 print_upgrade_separator();
db9d4a3d 1283}
8580535b 1284
1285/**
1286 * Install core moodle tables and initialize
1287 * @param float $version target version
1288 * @param bool $verbose
1289 * @return void, may throw exception
1290 */
1291function install_core($version, $verbose) {
1292 global $CFG, $DB;
1293
1294 try {
c3d0e149 1295 set_time_limit(600);
194cdca8 1296 print_upgrade_part_start('moodle', true, $verbose); // does not store upgrade running flag
8580535b 1297
1298 $DB->get_manager()->install_from_xmldb_file("$CFG->libdir/db/install.xml");
1299 upgrade_started(); // we want the flag to be stored in config table ;-)
1300
1301 // set all core default records and default settings
1302 require_once("$CFG->libdir/db/install.php");
c6d75bff 1303 xmldb_main_install(); // installs the capabilities too
8580535b 1304
1305 // store version
1306 upgrade_main_savepoint(true, $version, false);
1307
df997f84 1308 // Continue with the installation
c6d75bff
PS
1309 log_update_descriptions('moodle');
1310 external_update_descriptions('moodle');
8580535b 1311 events_update_definition('moodle');
1312 message_update_providers('moodle');
8580535b 1313
df997f84 1314 // Write default settings unconditionally
8580535b 1315 admin_apply_default_settings(NULL, true);
1316
194cdca8 1317 print_upgrade_part_end(null, true, $verbose);
8580535b 1318 } catch (exception $ex) {
1319 upgrade_handle_exception($ex);
1320 }
1321}
1322
1323/**
1324 * Upgrade moodle core
1325 * @param float $version target version
1326 * @param bool $verbose
1327 * @return void, may throw exception
1328 */
1329function upgrade_core($version, $verbose) {
1330 global $CFG;
1331
41209c1e
PS
1332 raise_memory_limit(MEMORY_EXTRA);
1333
3316fe24 1334 require_once($CFG->libdir.'/db/upgrade.php'); // Defines upgrades
3316fe24 1335
8580535b 1336 try {
dcf9be7f 1337 // Reset caches before any output
f87eab7e 1338 purge_all_caches();
dcf9be7f 1339
8580535b 1340 // Upgrade current language pack if we can
1341 if (empty($CFG->skiplangupgrade)) {
63891a7c
DM
1342 if (get_string_manager()->translation_exists(current_language())) {
1343 upgrade_language_pack(false);
1344 }
8580535b 1345 }
1346
194cdca8 1347 print_upgrade_part_start('moodle', false, $verbose);
8580535b 1348
17da2e6f 1349 // one time special local migration pre 2.0 upgrade script
8d33799d
PS
1350 if ($CFG->version < 2007101600) {
1351 $pre20upgradefile = "$CFG->dirroot/local/upgrade_pre20.php";
17da2e6f 1352 if (file_exists($pre20upgradefile)) {
1353 set_time_limit(0);
1354 require($pre20upgradefile);
1355 // reset upgrade timeout to default
1356 upgrade_set_timeout();
1357 }
1358 }
1359
8580535b 1360 $result = xmldb_main_upgrade($CFG->version);
1361 if ($version > $CFG->version) {
1362 // store version if not already there
1363 upgrade_main_savepoint($result, $version, false);
1364 }
1365
1366 // perform all other component upgrade routines
1367 update_capabilities('moodle');
c6d75bff 1368 log_update_descriptions('moodle');
c976e271 1369 external_update_descriptions('moodle');
8580535b 1370 events_update_definition('moodle');
1371 message_update_providers('moodle');
8580535b 1372
dcf9be7f 1373 // Reset caches again, just to be sure
f87eab7e 1374 purge_all_caches();
2e5eba85
PS
1375
1376 // Clean up contexts - more and more stuff depends on existence of paths and contexts
1377 cleanup_contexts();
1378 create_contexts();
1379 build_context_path();
df997f84
PS
1380 $syscontext = get_context_instance(CONTEXT_SYSTEM);
1381 mark_context_dirty($syscontext->path);
8580535b 1382
194cdca8 1383 print_upgrade_part_end('moodle', false, $verbose);
8580535b 1384 } catch (Exception $ex) {
1385 upgrade_handle_exception($ex);
1386 }
1387}
1388
1389/**
1390 * Upgrade/install other parts of moodle
1391 * @param bool $verbose
1392 * @return void, may throw exception
1393 */
1394function upgrade_noncore($verbose) {
1395 global $CFG;
1396
41209c1e
PS
1397 raise_memory_limit(MEMORY_EXTRA);
1398
8580535b 1399 // upgrade all plugins types
1400 try {
1401 $plugintypes = get_plugin_types();
1402 foreach ($plugintypes as $type=>$location) {
17da2e6f 1403 upgrade_plugins($type, 'print_upgrade_part_start', 'print_upgrade_part_end', $verbose);
8580535b 1404 }
1405 } catch (Exception $ex) {
1406 upgrade_handle_exception($ex);
1407 }
8580535b 1408}
3316fe24 1409
1410/**
1411 * Checks if the main tables have been installed yet or not.
1412 * @return bool
1413 */
1414function core_tables_exist() {
1415 global $DB;
1416
1417 if (!$tables = $DB->get_tables() ) { // No tables yet at all.
1418 return false;
9b683d13 1419
3316fe24 1420 } else { // Check for missing main tables
1421 $mtables = array('config', 'course', 'groupings'); // some tables used in 1.9 and 2.0, preferable something from the start and end of install.xml
1422 foreach ($mtables as $mtable) {
1423 if (!in_array($mtable, $tables)) {
1424 return false;
1425 }
1426 }
1427 return true;
9b683d13 1428 }
ba04999c 1429}
c71ade2f
PL
1430
1431/**
1432 * upgrades the mnet rpc definitions for the given component.
1433 * this method doesn't return status, an exception will be thrown in the case of an error
1434 *
1435 * @param string $component the plugin to upgrade, eg auth_mnet
1436 */
1437function upgrade_plugin_mnet_functions($component) {
1438 global $DB, $CFG;
1439
1440 list($type, $plugin) = explode('_', $component);
1441 $path = get_plugin_directory($type, $plugin);
1442
17b71716
PS
1443 $publishes = array();
1444 $subscribes = array();
c71ade2f
PL
1445 if (file_exists($path . '/db/mnet.php')) {
1446 require_once($path . '/db/mnet.php'); // $publishes comes from this file
1447 }
1448 if (empty($publishes)) {
1449 $publishes = array(); // still need this to be able to disable stuff later
1450 }
1451 if (empty($subscribes)) {
1452 $subscribes = array(); // still need this to be able to disable stuff later
1453 }
1454
1455 static $servicecache = array();
1456
1457 // rekey an array based on the rpc method for easy lookups later
1458 $publishmethodservices = array();
1459 $subscribemethodservices = array();
1460 foreach($publishes as $servicename => $service) {
1461 if (is_array($service['methods'])) {
1462 foreach($service['methods'] as $methodname) {
1463 $service['servicename'] = $servicename;
1464 $publishmethodservices[$methodname][] = $service;
1465 }
1466 }
1467 }
1468
1469 // Disable functions that don't exist (any more) in the source
1470 // Should these be deleted? What about their permissions records?
1471 foreach ($DB->get_records('mnet_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
1472 if (!array_key_exists($rpc->functionname, $publishmethodservices) && $rpc->enabled) {
1473 $DB->set_field('mnet_rpc', 'enabled', 0, array('id' => $rpc->id));
1474 } else if (array_key_exists($rpc->functionname, $publishmethodservices) && !$rpc->enabled) {
1475 $DB->set_field('mnet_rpc', 'enabled', 1, array('id' => $rpc->id));
1476 }
1477 }
1478
1479 // reflect all the services we're publishing and save them
1480 require_once($CFG->dirroot . '/lib/zend/Zend/Server/Reflection.php');
1481 static $cachedclasses = array(); // to store reflection information in
1482 foreach ($publishes as $service => $data) {
1483 $f = $data['filename'];
1484 $c = $data['classname'];
1485 foreach ($data['methods'] as $method) {
6bdfef5d 1486 $dataobject = new stdClass();
c71ade2f
PL
1487 $dataobject->plugintype = $type;
1488 $dataobject->pluginname = $plugin;
1489 $dataobject->enabled = 1;
1490 $dataobject->classname = $c;
1491 $dataobject->filename = $f;
1492
1493 if (is_string($method)) {
1494 $dataobject->functionname = $method;
1495
1496 } else if (is_array($method)) { // wants to override file or class
1497 $dataobject->functionname = $method['method'];
1498 $dataobject->classname = $method['classname'];
1499 $dataobject->filename = $method['filename'];
1500 }
1501 $dataobject->xmlrpcpath = $type.'/'.$plugin.'/'.$dataobject->filename.'/'.$method;
1502 $dataobject->static = false;
1503
1504 require_once($path . '/' . $dataobject->filename);
1505 $functionreflect = null; // slightly different ways to get this depending on whether it's a class method or a function
1506 if (!empty($dataobject->classname)) {
1507 if (!class_exists($dataobject->classname)) {
1508 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
1509 }
1510 $key = $dataobject->filename . '|' . $dataobject->classname;
1511 if (!array_key_exists($key, $cachedclasses)) { // look to see if we've already got a reflection object
1512 try {
1513 $cachedclasses[$key] = Zend_Server_Reflection::reflectClass($dataobject->classname);
1514 } catch (Zend_Server_Reflection_Exception $e) { // catch these and rethrow them to something more helpful
1515 throw new moodle_exception('installreflectionclasserror', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname, 'error' => $e->getMessage()));
1516 }
1517 }
1518 $r =& $cachedclasses[$key];
1519 if (!$r->hasMethod($dataobject->functionname)) {
1520 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
1521 }
1522 // stupid workaround for zend not having a getMethod($name) function
1523 $ms = $r->getMethods();
1524 foreach ($ms as $m) {
1525 if ($m->getName() == $dataobject->functionname) {
1526 $functionreflect = $m;
1527 break;
1528 }
1529 }
1530 $dataobject->static = (int)$functionreflect->isStatic();
1531 } else {
1532 if (!function_exists($dataobject->functionname)) {
1533 throw new moodle_exception('installnosuchfunction', 'mnet', '', (object)array('method' => $dataobject->functionname, 'file' => $dataobject->filename));
1534 }
1535 try {
1536 $functionreflect = Zend_Server_Reflection::reflectFunction($dataobject->functionname);
1537 } catch (Zend_Server_Reflection_Exception $e) { // catch these and rethrow them to something more helpful
1538 throw new moodle_exception('installreflectionfunctionerror', 'mnet', '', (object)array('method' => $dataobject->functionname, '' => $dataobject->filename, 'error' => $e->getMessage()));
1539 }
1540 }
1541 $dataobject->profile = serialize(admin_mnet_method_profile($functionreflect));
1542 $dataobject->help = $functionreflect->getDescription();
1543
1544 if ($record_exists = $DB->get_record('mnet_rpc', array('xmlrpcpath'=>$dataobject->xmlrpcpath))) {
1545 $dataobject->id = $record_exists->id;
1546 $dataobject->enabled = $record_exists->enabled;
1547 $DB->update_record('mnet_rpc', $dataobject);
1548 } else {
1549 $dataobject->id = $DB->insert_record('mnet_rpc', $dataobject, true);
1550 }
c71ade2f 1551
d5bd0b01
DM
1552 // TODO this API versioning must be reworked, here the recently processed method
1553 // sets the service API which may not be correct
677b6321
PL
1554 foreach ($publishmethodservices[$dataobject->functionname] as $service) {
1555 if ($serviceobj = $DB->get_record('mnet_service', array('name'=>$service['servicename']))) {
1556 $serviceobj->apiversion = $service['apiversion'];
1557 $DB->update_record('mnet_service', $serviceobj);
1558 } else {
1559 $serviceobj = new stdClass();
1560 $serviceobj->name = $service['servicename'];
f4a2817a 1561 $serviceobj->description = empty($service['description']) ? '' : $service['description'];
677b6321
PL
1562 $serviceobj->apiversion = $service['apiversion'];
1563 $serviceobj->offer = 1;
1564 $serviceobj->id = $DB->insert_record('mnet_service', $serviceobj);
1565 }
1566 $servicecache[$service['servicename']] = $serviceobj;
1567 if (!$DB->record_exists('mnet_service2rpc', array('rpcid'=>$dataobject->id, 'serviceid'=>$serviceobj->id))) {
1568 $obj = new stdClass();
1569 $obj->rpcid = $dataobject->id;
1570 $obj->serviceid = $serviceobj->id;
1571 $DB->insert_record('mnet_service2rpc', $obj, true);
1572 }
c71ade2f
PL
1573 }
1574 }
1575 }
c71ade2f
PL
1576 // finished with methods we publish, now do subscribable methods
1577 foreach($subscribes as $service => $methods) {
1578 if (!array_key_exists($service, $servicecache)) {
1579 if (!$serviceobj = $DB->get_record('mnet_service', array('name' => $service))) {
81634f03 1580 debugging("TODO: skipping unknown service $service - somebody needs to fix MDL-21993");
c71ade2f
PL
1581 continue;
1582 }
1583 $servicecache[$service] = $serviceobj;
1584 } else {
1585 $serviceobj = $servicecache[$service];
1586 }
1587 foreach ($methods as $method => $xmlrpcpath) {
1588 if (!$rpcid = $DB->get_field('mnet_remote_rpc', 'id', array('xmlrpcpath'=>$xmlrpcpath))) {
1589 $remoterpc = (object)array(
1590 'functionname' => $method,
1591 'xmlrpcpath' => $xmlrpcpath,
1592 'plugintype' => $type,
1593 'pluginname' => $plugin,
1594 'enabled' => 1,
1595 );
1596 $rpcid = $remoterpc->id = $DB->insert_record('mnet_remote_rpc', $remoterpc, true);
1597 }
1598 if (!$DB->record_exists('mnet_remote_service2rpc', array('rpcid'=>$rpcid, 'serviceid'=>$serviceobj->id))) {
1599 $obj = new stdClass();
1600 $obj->rpcid = $rpcid;
1601 $obj->serviceid = $serviceobj->id;
1602 $DB->insert_record('mnet_remote_service2rpc', $obj, true);
1603 }
677b6321 1604 $subscribemethodservices[$method][] = $service;
c71ade2f
PL
1605 }
1606 }
1607
1608 foreach ($DB->get_records('mnet_remote_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
1609 if (!array_key_exists($rpc->functionname, $subscribemethodservices) && $rpc->enabled) {
1610 $DB->set_field('mnet_remote_rpc', 'enabled', 0, array('id' => $rpc->id));
1611 } else if (array_key_exists($rpc->functionname, $subscribemethodservices) && !$rpc->enabled) {
1612 $DB->set_field('mnet_remote_rpc', 'enabled', 1, array('id' => $rpc->id));
1613 }
1614 }
1615
1616 return true;
1617}
1618
1619/**
1620 * Given some sort of Zend Reflection function/method object, return a profile array, ready to be serialized and stored
1621 *
1622 * @param Zend_Server_Reflection_Function_Abstract $function can be any subclass of this object type
1623 *
1624 * @return array
1625 */
1626function admin_mnet_method_profile(Zend_Server_Reflection_Function_Abstract $function) {
1627 $proto = array_pop($function->getPrototypes());
1628 $ret = $proto->getReturnValue();
1629 $profile = array(
1630 'parameters' => array(),
1631 'return' => array(
1632 'type' => $ret->getType(),
1633 'description' => $ret->getDescription(),
1634 ),
1635 );
1636 foreach ($proto->getParameters() as $p) {
1637 $profile['parameters'][] = array(
1638 'name' => $p->getName(),
1639 'type' => $p->getType(),
1640 'description' => $p->getDescription(),
1641 );
1642 }
1643 return $profile;
1644}