MDL-42882 upgrade: improved SQL query
[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
bdbcb6d7
PS
100/**
101 * @package core
102 * @subpackage upgrade
103 * @copyright 2009 Petr Skoda {@link http://skodak.org}
104 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
105 */
106class plugin_misplaced_exception extends moodle_exception {
107 function __construct($component, $expected, $current) {
108 global $CFG;
109 $a = new stdClass();
110 $a->component = $component;
111 $a->expected = $expected;
112 $a->current = $current;
113 parent::__construct('detectedmisplacedplugin', 'core_plugin', "$CFG->wwwroot/$CFG->admin/index.php", $a);
114 }
115}
116
0c5406ca
PS
117/**
118 * Sets maximum expected time needed for upgrade task.
119 * Please always make sure that upgrade will not run longer!
120 *
121 * The script may be automatically aborted if upgrade times out.
122 *
123 * @category upgrade
124 * @param int $max_execution_time in seconds (can not be less than 60 s)
125 */
126function upgrade_set_timeout($max_execution_time=300) {
127 global $CFG;
128
129 if (!isset($CFG->upgraderunning) or $CFG->upgraderunning < time()) {
130 $upgraderunning = get_config(null, 'upgraderunning');
131 } else {
132 $upgraderunning = $CFG->upgraderunning;
133 }
134
135 if (!$upgraderunning) {
b814d1b4
PS
136 if (CLI_SCRIPT) {
137 // never stop CLI upgrades
138 $upgraderunning = 0;
139 } else {
140 // web upgrade not running or aborted
141 print_error('upgradetimedout', 'admin', "$CFG->wwwroot/$CFG->admin/");
142 }
0c5406ca
PS
143 }
144
145 if ($max_execution_time < 60) {
146 // protection against 0 here
147 $max_execution_time = 60;
148 }
149
150 $expected_end = time() + $max_execution_time;
151
152 if ($expected_end < $upgraderunning + 10 and $expected_end > $upgraderunning - 10) {
153 // no need to store new end, it is nearly the same ;-)
154 return;
155 }
156
5563f5fb
PS
157 if (CLI_SCRIPT) {
158 // there is no point in timing out of CLI scripts, admins can stop them if necessary
3ef7279f 159 core_php_time_limit::raise();
5563f5fb 160 } else {
3ef7279f 161 core_php_time_limit::raise($max_execution_time);
5563f5fb 162 }
0c5406ca
PS
163 set_config('upgraderunning', $expected_end); // keep upgrade locked until this time
164}
165
db9d4a3d 166/**
167 * Upgrade savepoint, marks end of each upgrade block.
168 * It stores new main version, resets upgrade timeout
169 * and abort upgrade if user cancels page loading.
170 *
171 * Please do not make large upgrade blocks with lots of operations,
172 * for example when adding tables keep only one table operation per block.
173 *
39b90b51 174 * @category upgrade
db9d4a3d 175 * @param bool $result false if upgrade step failed, true if completed
176 * @param string or float $version main version
177 * @param bool $allowabort allow user to abort script execution here
178 * @return void
179 */
180function upgrade_main_savepoint($result, $version, $allowabort=true) {
181 global $CFG;
182
bb44ef99
RT
183 //sanity check to avoid confusion with upgrade_mod_savepoint usage.
184 if (!is_bool($allowabort)) {
185 $errormessage = 'Parameter type mismatch. Are you mixing up upgrade_main_savepoint() and upgrade_mod_savepoint()?';
186 throw new coding_exception($errormessage);
187 }
188
795a08ad 189 if (!$result) {
520cea92 190 throw new upgrade_exception(null, $version);
795a08ad 191 }
192
193 if ($CFG->version >= $version) {
194 // something really wrong is going on in main upgrade script
195 throw new downgrade_exception(null, $CFG->version, $version);
db9d4a3d 196 }
197
795a08ad 198 set_config('version', $version);
199 upgrade_log(UPGRADE_LOG_NORMAL, null, 'Upgrade savepoint reached');
200
db9d4a3d 201 // reset upgrade timeout to default
202 upgrade_set_timeout();
203
204 // this is a safe place to stop upgrades if user aborts page loading
205 if ($allowabort and connection_aborted()) {
206 die;
207 }
208}
209
210/**
211 * Module upgrade savepoint, marks end of module upgrade blocks
212 * It stores module version, resets upgrade timeout
213 * and abort upgrade if user cancels page loading.
214 *
39b90b51 215 * @category upgrade
db9d4a3d 216 * @param bool $result false if upgrade step failed, true if completed
217 * @param string or float $version main version
218 * @param string $modname name of module
219 * @param bool $allowabort allow user to abort script execution here
220 * @return void
221 */
222function upgrade_mod_savepoint($result, $version, $modname, $allowabort=true) {
223 global $DB;
224
bde002b8
PS
225 $component = 'mod_'.$modname;
226
795a08ad 227 if (!$result) {
bde002b8 228 throw new upgrade_exception($component, $version);
795a08ad 229 }
230
bde002b8
PS
231 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
232
db9d4a3d 233 if (!$module = $DB->get_record('modules', array('name'=>$modname))) {
234 print_error('modulenotexist', 'debug', '', $modname);
235 }
236
bde002b8 237 if ($dbversion >= $version) {
795a08ad 238 // something really wrong is going on in upgrade script
bde002b8 239 throw new downgrade_exception($component, $dbversion, $version);
db9d4a3d 240 }
bde002b8
PS
241 set_config('version', $version, $component);
242
243 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
db9d4a3d 244
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 * Blocks upgrade savepoint, marks end of blocks upgrade blocks
256 * It stores block version, resets upgrade timeout
257 * and abort upgrade if user cancels page loading.
258 *
39b90b51 259 * @category upgrade
db9d4a3d 260 * @param bool $result false if upgrade step failed, true if completed
261 * @param string or float $version main version
262 * @param string $blockname name of block
263 * @param bool $allowabort allow user to abort script execution here
264 * @return void
265 */
795a08ad 266function upgrade_block_savepoint($result, $version, $blockname, $allowabort=true) {
db9d4a3d 267 global $DB;
268
bde002b8
PS
269 $component = 'block_'.$blockname;
270
795a08ad 271 if (!$result) {
bde002b8 272 throw new upgrade_exception($component, $version);
795a08ad 273 }
274
bde002b8
PS
275 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
276
db9d4a3d 277 if (!$block = $DB->get_record('block', array('name'=>$blockname))) {
278 print_error('blocknotexist', 'debug', '', $blockname);
279 }
280
bde002b8 281 if ($dbversion >= $version) {
795a08ad 282 // something really wrong is going on in upgrade script
bde002b8 283 throw new downgrade_exception($component, $dbversion, $version);
db9d4a3d 284 }
bde002b8
PS
285 set_config('version', $version, $component);
286
287 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
db9d4a3d 288
289 // reset upgrade timeout to default
290 upgrade_set_timeout();
291
292 // this is a safe place to stop upgrades if user aborts page loading
293 if ($allowabort and connection_aborted()) {
294 die;
295 }
296}
297
298/**
299 * Plugins upgrade savepoint, marks end of blocks upgrade blocks
300 * It stores plugin version, resets upgrade timeout
301 * and abort upgrade if user cancels page loading.
302 *
39b90b51 303 * @category upgrade
db9d4a3d 304 * @param bool $result false if upgrade step failed, true if completed
305 * @param string or float $version main version
306 * @param string $type name of plugin
307 * @param string $dir location of plugin
308 * @param bool $allowabort allow user to abort script execution here
309 * @return void
310 */
17da2e6f 311function upgrade_plugin_savepoint($result, $version, $type, $plugin, $allowabort=true) {
bde002b8
PS
312 global $DB;
313
17da2e6f 314 $component = $type.'_'.$plugin;
315
795a08ad 316 if (!$result) {
17da2e6f 317 throw new upgrade_exception($component, $version);
db9d4a3d 318 }
319
bde002b8
PS
320 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
321
322 if ($dbversion >= $version) {
795a08ad 323 // Something really wrong is going on in the upgrade script
bde002b8 324 throw new downgrade_exception($component, $dbversion, $version);
795a08ad 325 }
17da2e6f 326 set_config('version', $version, $component);
795a08ad 327 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
328
db9d4a3d 329 // Reset upgrade timeout to default
330 upgrade_set_timeout();
331
332 // This is a safe place to stop upgrades if user aborts page loading
333 if ($allowabort and connection_aborted()) {
334 die;
335 }
336}
337
9008ec16
PS
338/**
339 * Detect if there are leftovers in PHP source files.
340 *
341 * During main version upgrades administrators MUST move away
342 * old PHP source files and start from scratch (or better
343 * use git).
344 *
345 * @return bool true means borked upgrade, false means previous PHP files were properly removed
346 */
347function upgrade_stale_php_files_present() {
348 global $CFG;
349
350 $someexamplesofremovedfiles = array(
72e3a3c9 351 // removed in 2.6dev
bde002b8 352 '/admin/block.php',
72e3a3c9 353 '/admin/oacleanup.php',
301ebbe5 354 // removed in 2.5dev
c1782ec6
EL
355 '/backup/lib.php',
356 '/backup/bb/README.txt',
301ebbe5 357 '/lib/excel/test.php',
c8b3346c
PS
358 // removed in 2.4dev
359 '/admin/tool/unittest/simpletestlib.php',
37d72977
PS
360 // removed in 2.3dev
361 '/lib/minify/builder/',
9008ec16
PS
362 // removed in 2.2dev
363 '/lib/yui/3.4.1pr1/',
364 // removed in 2.2
365 '/search/cron_php5.php',
366 '/course/report/log/indexlive.php',
367 '/admin/report/backups/index.php',
368 '/admin/generator.php',
369 // removed in 2.1
370 '/lib/yui/2.8.0r4/',
371 // removed in 2.0
372 '/blocks/admin/block_admin.php',
373 '/blocks/admin_tree/block_admin_tree.php',
374 );
375
376 foreach ($someexamplesofremovedfiles as $file) {
377 if (file_exists($CFG->dirroot.$file)) {
378 return true;
379 }
380 }
381
382 return false;
383}
db9d4a3d 384
385/**
386 * Upgrade plugins
db9d4a3d 387 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
17da2e6f 388 * return void
db9d4a3d 389 */
17da2e6f 390function upgrade_plugins($type, $startcallback, $endcallback, $verbose) {
db9d4a3d 391 global $CFG, $DB;
392
393/// special cases
394 if ($type === 'mod') {
194cdca8 395 return upgrade_plugins_modules($startcallback, $endcallback, $verbose);
795a08ad 396 } else if ($type === 'block') {
194cdca8 397 return upgrade_plugins_blocks($startcallback, $endcallback, $verbose);
db9d4a3d 398 }
399
bd3b3bba 400 $plugs = core_component::get_plugin_list($type);
db9d4a3d 401
17da2e6f 402 foreach ($plugs as $plug=>$fullplug) {
ebb98bf5 403 // Reset time so that it works when installing a large number of plugins
3ef7279f 404 core_php_time_limit::raise(600);
aff24313 405 $component = clean_param($type.'_'.$plug, PARAM_COMPONENT); // standardised plugin name
db9d4a3d 406
3e858ea7 407 // check plugin dir is valid name
aff24313
PS
408 if (empty($component)) {
409 throw new plugin_defective_exception($type.'_'.$plug, 'Invalid plugin directory name.');
3e858ea7
PS
410 }
411
795a08ad 412 if (!is_readable($fullplug.'/version.php')) {
413 continue;
db9d4a3d 414 }
415
365a5941 416 $plugin = new stdClass();
bde002b8
PS
417 $plugin->version = null;
418 $module = $plugin; // Prevent some notices when plugin placed in wrong directory.
795a08ad 419 require($fullplug.'/version.php'); // defines $plugin with version etc
bde002b8 420 unset($module);
bdbcb6d7 421
3e858ea7
PS
422 // if plugin tells us it's full name we may check the location
423 if (isset($plugin->component)) {
424 if ($plugin->component !== $component) {
bdbcb6d7 425 $current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullplug);
b0d1d941 426 $expected = str_replace($CFG->dirroot, '$CFG->dirroot', core_component::get_component_directory($plugin->component));
bdbcb6d7 427 throw new plugin_misplaced_exception($component, $expected, $current);
3e858ea7
PS
428 }
429 }
430
795a08ad 431 if (empty($plugin->version)) {
432 throw new plugin_defective_exception($component, 'Missing version value in version.php');
db9d4a3d 433 }
434
17da2e6f 435 $plugin->name = $plug;
436 $plugin->fullname = $component;
795a08ad 437
db9d4a3d 438 if (!empty($plugin->requires)) {
439 if ($plugin->requires > $CFG->version) {
795a08ad 440 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
ef14c1e7
PS
441 } else if ($plugin->requires < 2010000000) {
442 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
db9d4a3d 443 }
444 }
445
9b683d13 446 // try to recover from interrupted install.php if needed
447 if (file_exists($fullplug.'/db/install.php')) {
448 if (get_config($plugin->fullname, 'installrunning')) {
449 require_once($fullplug.'/db/install.php');
450 $recover_install_function = 'xmldb_'.$plugin->fullname.'_install_recovery';
451 if (function_exists($recover_install_function)) {
452 $startcallback($component, true, $verbose);
453 $recover_install_function();
f6f06d69 454 unset_config('installrunning', $plugin->fullname);
9b683d13 455 update_capabilities($component);
c6d75bff 456 log_update_descriptions($component);
c976e271 457 external_update_descriptions($component);
9b683d13 458 events_update_definition($component);
459 message_update_providers($component);
afed8d0f
RK
460 if ($type === 'message') {
461 message_update_processors($plug);
462 }
de260e0f 463 upgrade_plugin_mnet_functions($component);
9b683d13 464 $endcallback($component, true, $verbose);
465 }
466 }
467 }
db9d4a3d 468
bde002b8 469 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
795a08ad 470 if (empty($installedversion)) { // new installation
194cdca8 471 $startcallback($component, true, $verbose);
db9d4a3d 472
795a08ad 473 /// Install tables if defined
474 if (file_exists($fullplug.'/db/install.xml')) {
475 $DB->get_manager()->install_from_xmldb_file($fullplug.'/db/install.xml');
db9d4a3d 476 }
9b683d13 477
478 /// store version
479 upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false);
480
795a08ad 481 /// execute post install file
482 if (file_exists($fullplug.'/db/install.php')) {
483 require_once($fullplug.'/db/install.php');
f6f06d69
PS
484 set_config('installrunning', 1, $plugin->fullname);
485 $post_install_function = 'xmldb_'.$plugin->fullname.'_install';
795a08ad 486 $post_install_function();
f6f06d69 487 unset_config('installrunning', $plugin->fullname);
795a08ad 488 }
489
795a08ad 490 /// Install various components
491 update_capabilities($component);
c6d75bff 492 log_update_descriptions($component);
c976e271 493 external_update_descriptions($component);
795a08ad 494 events_update_definition($component);
495 message_update_providers($component);
afed8d0f
RK
496 if ($type === 'message') {
497 message_update_processors($plug);
498 }
de260e0f 499 upgrade_plugin_mnet_functions($component);
194cdca8 500 $endcallback($component, true, $verbose);
795a08ad 501
502 } else if ($installedversion < $plugin->version) { // upgrade
503 /// Run the upgrade function for the plugin.
194cdca8 504 $startcallback($component, false, $verbose);
795a08ad 505
506 if (is_readable($fullplug.'/db/upgrade.php')) {
507 require_once($fullplug.'/db/upgrade.php'); // defines upgrading function
508
509 $newupgrade_function = 'xmldb_'.$plugin->fullname.'_upgrade';
510 $result = $newupgrade_function($installedversion);
511 } else {
512 $result = true;
513 }
514
bde002b8 515 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
795a08ad 516 if ($installedversion < $plugin->version) {
517 // store version if not already there
518 upgrade_plugin_savepoint($result, $plugin->version, $type, $plug, false);
519 }
520
521 /// Upgrade various components
522 update_capabilities($component);
c6d75bff 523 log_update_descriptions($component);
c976e271 524 external_update_descriptions($component);
795a08ad 525 events_update_definition($component);
526 message_update_providers($component);
afed8d0f 527 if ($type === 'message') {
bde002b8 528 // Ugly hack!
afed8d0f
RK
529 message_update_processors($plug);
530 }
de260e0f 531 upgrade_plugin_mnet_functions($component);
194cdca8 532 $endcallback($component, false, $verbose);
795a08ad 533
534 } else if ($installedversion > $plugin->version) {
535 throw new downgrade_exception($component, $installedversion, $plugin->version);
db9d4a3d 536 }
537 }
db9d4a3d 538}
539
540/**
541 * Find and check all modules and load them up or upgrade them if necessary
72fb21b6 542 *
543 * @global object
544 * @global object
db9d4a3d 545 */
194cdca8 546function upgrade_plugins_modules($startcallback, $endcallback, $verbose) {
db9d4a3d 547 global $CFG, $DB;
548
bd3b3bba 549 $mods = core_component::get_plugin_list('mod');
db9d4a3d 550
17da2e6f 551 foreach ($mods as $mod=>$fullmod) {
db9d4a3d 552
3e858ea7 553 if ($mod === 'NEWMODULE') { // Someone has unzipped the template, ignore it
db9d4a3d 554 continue;
555 }
556
aff24313 557 $component = clean_param('mod_'.$mod, PARAM_COMPONENT);
db9d4a3d 558
3e858ea7 559 // check module dir is valid name
aff24313
PS
560 if (empty($component)) {
561 throw new plugin_defective_exception('mod_'.$mod, 'Invalid plugin directory name.');
3e858ea7
PS
562 }
563
795a08ad 564 if (!is_readable($fullmod.'/version.php')) {
565 throw new plugin_defective_exception($component, 'Missing version.php');
db9d4a3d 566 }
567
994e5662 568 // TODO: Support for $module will end with Moodle 2.10 by MDL-43896. Was deprecated for Moodle 2.7 by MDL-43040.
bde002b8
PS
569 $plugin = new stdClass();
570 $plugin->version = null;
571 $module = $plugin;
994e5662 572 require($fullmod .'/version.php'); // Defines $plugin with version etc.
bde002b8
PS
573 $plugin = clone($module);
574 unset($module->version);
575 unset($module->component);
576 unset($module->dependencies);
577 unset($module->release);
bdbcb6d7 578
3e858ea7 579 // if plugin tells us it's full name we may check the location
bde002b8
PS
580 if (isset($plugin->component)) {
581 if ($plugin->component !== $component) {
bdbcb6d7 582 $current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullmod);
bde002b8 583 $expected = str_replace($CFG->dirroot, '$CFG->dirroot', core_component::get_component_directory($plugin->component));
bdbcb6d7 584 throw new plugin_misplaced_exception($component, $expected, $current);
3e858ea7
PS
585 }
586 }
587
bde002b8
PS
588 if (empty($plugin->version)) {
589 // Version must be always set now!
795a08ad 590 throw new plugin_defective_exception($component, 'Missing version value in version.php');
db9d4a3d 591 }
592
bde002b8
PS
593 if (!empty($plugin->requires)) {
594 if ($plugin->requires > $CFG->version) {
595 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
596 } else if ($plugin->requires < 2010000000) {
ef14c1e7 597 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
db9d4a3d 598 }
599 }
600
ff05bb31
TH
601 if (empty($module->cron)) {
602 $module->cron = 0;
603 }
604
3e858ea7
PS
605 // all modules must have en lang pack
606 if (!is_readable("$fullmod/lang/en/$mod.php")) {
607 throw new plugin_defective_exception($component, 'Missing mandatory en language pack.');
608 }
609
db9d4a3d 610 $module->name = $mod; // The name MUST match the directory
611
bde002b8 612 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
db9d4a3d 613
9b683d13 614 if (file_exists($fullmod.'/db/install.php')) {
615 if (get_config($module->name, 'installrunning')) {
616 require_once($fullmod.'/db/install.php');
617 $recover_install_function = 'xmldb_'.$module->name.'_install_recovery';
618 if (function_exists($recover_install_function)) {
619 $startcallback($component, true, $verbose);
620 $recover_install_function();
621 unset_config('installrunning', $module->name);
622 // Install various components too
623 update_capabilities($component);
c6d75bff 624 log_update_descriptions($component);
c976e271 625 external_update_descriptions($component);
9b683d13 626 events_update_definition($component);
627 message_update_providers($component);
de260e0f 628 upgrade_plugin_mnet_functions($component);
9b683d13 629 $endcallback($component, true, $verbose);
630 }
631 }
632 }
633
bde002b8 634 if (empty($installedversion)) {
194cdca8 635 $startcallback($component, true, $verbose);
db9d4a3d 636
795a08ad 637 /// Execute install.xml (XMLDB) - must be present in all modules
638 $DB->get_manager()->install_from_xmldb_file($fullmod.'/db/install.xml');
db9d4a3d 639
2e3da297 640 /// Add record into modules table - may be needed in install.php already
641 $module->id = $DB->insert_record('modules', $module);
bde002b8 642 upgrade_mod_savepoint(true, $plugin->version, $module->name, false);
2e3da297 643
db9d4a3d 644 /// Post installation hook - optional
645 if (file_exists("$fullmod/db/install.php")) {
646 require_once("$fullmod/db/install.php");
9b683d13 647 // Set installation running flag, we need to recover after exception or error
648 set_config('installrunning', 1, $module->name);
3412cb43 649 $post_install_function = 'xmldb_'.$module->name.'_install';
db9d4a3d 650 $post_install_function();
9b683d13 651 unset_config('installrunning', $module->name);
db9d4a3d 652 }
653
795a08ad 654 /// Install various components
655 update_capabilities($component);
c6d75bff 656 log_update_descriptions($component);
c976e271 657 external_update_descriptions($component);
795a08ad 658 events_update_definition($component);
659 message_update_providers($component);
de260e0f 660 upgrade_plugin_mnet_functions($component);
795a08ad 661
194cdca8 662 $endcallback($component, true, $verbose);
db9d4a3d 663
bde002b8 664 } else if ($installedversion < $plugin->version) {
795a08ad 665 /// If versions say that we need to upgrade but no upgrade files are available, notify and continue
194cdca8 666 $startcallback($component, false, $verbose);
795a08ad 667
668 if (is_readable($fullmod.'/db/upgrade.php')) {
669 require_once($fullmod.'/db/upgrade.php'); // defines new upgrading function
670 $newupgrade_function = 'xmldb_'.$module->name.'_upgrade';
bde002b8 671 $result = $newupgrade_function($installedversion, $module);
795a08ad 672 } else {
673 $result = true;
674 }
675
bde002b8 676 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
795a08ad 677 $currmodule = $DB->get_record('modules', array('name'=>$module->name));
bde002b8 678 if ($installedversion < $plugin->version) {
795a08ad 679 // store version if not already there
bde002b8 680 upgrade_mod_savepoint($result, $plugin->version, $mod, false);
795a08ad 681 }
682
3412cb43
TH
683 // update cron flag if needed
684 if ($currmodule->cron != $module->cron) {
685 $DB->set_field('modules', 'cron', $module->cron, array('name' => $module->name));
686 }
687
688 // Upgrade various components
795a08ad 689 update_capabilities($component);
c6d75bff 690 log_update_descriptions($component);
c976e271 691 external_update_descriptions($component);
795a08ad 692 events_update_definition($component);
693 message_update_providers($component);
de260e0f 694 upgrade_plugin_mnet_functions($component);
795a08ad 695
194cdca8 696 $endcallback($component, false, $verbose);
795a08ad 697
bde002b8
PS
698 } else if ($installedversion > $plugin->version) {
699 throw new downgrade_exception($component, $installedversion, $plugin->version);
795a08ad 700 }
701 }
702}
db9d4a3d 703
db9d4a3d 704
795a08ad 705/**
706 * This function finds all available blocks and install them
707 * into blocks table or do all the upgrade process if newer.
72fb21b6 708 *
709 * @global object
710 * @global object
795a08ad 711 */
194cdca8 712function upgrade_plugins_blocks($startcallback, $endcallback, $verbose) {
795a08ad 713 global $CFG, $DB;
714
795a08ad 715 require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
716
717 $blocktitles = array(); // we do not want duplicate titles
718
719 //Is this a first install
720 $first_install = null;
721
bd3b3bba 722 $blocks = core_component::get_plugin_list('block');
795a08ad 723
17da2e6f 724 foreach ($blocks as $blockname=>$fullblock) {
795a08ad 725
726 if (is_null($first_install)) {
f46eba7e 727 $first_install = ($DB->count_records('block_instances') == 0);
795a08ad 728 }
729
aff24313 730 if ($blockname === 'NEWBLOCK') { // Someone has unzipped the template, ignore it
795a08ad 731 continue;
db9d4a3d 732 }
733
aff24313 734 $component = clean_param('block_'.$blockname, PARAM_COMPONENT);
db9d4a3d 735
3e858ea7 736 // check block dir is valid name
aff24313
PS
737 if (empty($component)) {
738 throw new plugin_defective_exception('block_'.$blockname, 'Invalid plugin directory name.');
3e858ea7
PS
739 }
740
8571833f
PS
741 if (!is_readable($fullblock.'/version.php')) {
742 throw new plugin_defective_exception('block/'.$blockname, 'Missing version.php file.');
743 }
365a5941 744 $plugin = new stdClass();
bde002b8 745 $plugin->version = null;
8571833f 746 $plugin->cron = 0;
bde002b8 747 $module = $plugin; // Prevent some notices when module placed in wrong directory.
8571833f 748 include($fullblock.'/version.php');
bde002b8
PS
749 unset($module);
750 $block = clone($plugin);
751 unset($block->version);
752 unset($block->component);
753 unset($block->dependencies);
754 unset($block->release);
8571833f 755
3e858ea7 756 // if plugin tells us it's full name we may check the location
bde002b8
PS
757 if (isset($plugin->component)) {
758 if ($plugin->component !== $component) {
bdbcb6d7 759 $current = str_replace($CFG->dirroot, '$CFG->dirroot', $fullblock);
bde002b8 760 $expected = str_replace($CFG->dirroot, '$CFG->dirroot', core_component::get_component_directory($plugin->component));
bdbcb6d7 761 throw new plugin_misplaced_exception($component, $expected, $current);
3e858ea7
PS
762 }
763 }
764
bde002b8
PS
765 if (empty($plugin->version)) {
766 throw new plugin_defective_exception($component, 'Missing block version.');
767 }
768
ef14c1e7
PS
769 if (!empty($plugin->requires)) {
770 if ($plugin->requires > $CFG->version) {
771 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
772 } else if ($plugin->requires < 2010000000) {
773 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
774 }
775 }
776
795a08ad 777 if (!is_readable($fullblock.'/block_'.$blockname.'.php')) {
778 throw new plugin_defective_exception('block/'.$blockname, 'Missing main block class file.');
db9d4a3d 779 }
8571833f 780 include_once($fullblock.'/block_'.$blockname.'.php');
db9d4a3d 781
795a08ad 782 $classname = 'block_'.$blockname;
783
784 if (!class_exists($classname)) {
785 throw new plugin_defective_exception($component, 'Can not load main class.');
786 }
787
b921b6ee 788 $blockobj = new $classname; // This is what we'll be testing
795a08ad 789 $blocktitle = $blockobj->get_title();
790
791 // OK, it's as we all hoped. For further tests, the object will do them itself.
792 if (!$blockobj->_self_test()) {
793 throw new plugin_defective_exception($component, 'Self test failed.');
794 }
795
795a08ad 796 $block->name = $blockname; // The name MUST match the directory
795a08ad 797
bde002b8 798 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
795a08ad 799
9b683d13 800 if (file_exists($fullblock.'/db/install.php')) {
801 if (get_config('block_'.$blockname, 'installrunning')) {
802 require_once($fullblock.'/db/install.php');
803 $recover_install_function = 'xmldb_block_'.$blockname.'_install_recovery';
804 if (function_exists($recover_install_function)) {
805 $startcallback($component, true, $verbose);
806 $recover_install_function();
807 unset_config('installrunning', 'block_'.$blockname);
808 // Install various components
809 update_capabilities($component);
c6d75bff 810 log_update_descriptions($component);
c976e271 811 external_update_descriptions($component);
9b683d13 812 events_update_definition($component);
813 message_update_providers($component);
de260e0f 814 upgrade_plugin_mnet_functions($component);
9b683d13 815 $endcallback($component, true, $verbose);
816 }
817 }
818 }
819
bde002b8 820 if (empty($installedversion)) { // block not installed yet, so install it
795a08ad 821 $conflictblock = array_search($blocktitle, $blocktitles);
822 if ($conflictblock !== false) {
823 // Duplicate block titles are not allowed, they confuse people
824 // AND PHP's associative arrays ;)
c1ddac83 825 throw new plugin_defective_exception($component, get_string('blocknameconflict', 'error', (object)array('name'=>$block->name, 'conflict'=>$conflictblock)));
795a08ad 826 }
194cdca8 827 $startcallback($component, true, $verbose);
795a08ad 828
829 if (file_exists($fullblock.'/db/install.xml')) {
830 $DB->get_manager()->install_from_xmldb_file($fullblock.'/db/install.xml');
831 }
832 $block->id = $DB->insert_record('block', $block);
bde002b8 833 upgrade_block_savepoint(true, $plugin->version, $block->name, false);
795a08ad 834
835 if (file_exists($fullblock.'/db/install.php')) {
836 require_once($fullblock.'/db/install.php');
9b683d13 837 // Set installation running flag, we need to recover after exception or error
838 set_config('installrunning', 1, 'block_'.$blockname);
0e35ba6f 839 $post_install_function = 'xmldb_block_'.$blockname.'_install';
795a08ad 840 $post_install_function();
9b683d13 841 unset_config('installrunning', 'block_'.$blockname);
795a08ad 842 }
843
844 $blocktitles[$block->name] = $blocktitle;
845
846 // Install various components
847 update_capabilities($component);
c6d75bff 848 log_update_descriptions($component);
c976e271 849 external_update_descriptions($component);
795a08ad 850 events_update_definition($component);
851 message_update_providers($component);
de260e0f 852 upgrade_plugin_mnet_functions($component);
795a08ad 853
194cdca8 854 $endcallback($component, true, $verbose);
795a08ad 855
bde002b8 856 } else if ($installedversion < $plugin->version) {
194cdca8 857 $startcallback($component, false, $verbose);
795a08ad 858
859 if (is_readable($fullblock.'/db/upgrade.php')) {
860 require_once($fullblock.'/db/upgrade.php'); // defines new upgrading function
861 $newupgrade_function = 'xmldb_block_'.$blockname.'_upgrade';
bde002b8 862 $result = $newupgrade_function($installedversion, $block);
795a08ad 863 } else {
864 $result = true;
865 }
866
bde002b8 867 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
795a08ad 868 $currblock = $DB->get_record('block', array('name'=>$block->name));
bde002b8 869 if ($installedversion < $plugin->version) {
795a08ad 870 // store version if not already there
bde002b8 871 upgrade_block_savepoint($result, $plugin->version, $block->name, false);
795a08ad 872 }
873
874 if ($currblock->cron != $block->cron) {
875 // update cron flag if needed
1fa84543 876 $DB->set_field('block', 'cron', $block->cron, array('id' => $currblock->id));
795a08ad 877 }
878
b921b6ee 879 // Upgrade various components
795a08ad 880 update_capabilities($component);
c6d75bff 881 log_update_descriptions($component);
c976e271 882 external_update_descriptions($component);
17da2e6f 883 events_update_definition($component);
795a08ad 884 message_update_providers($component);
de260e0f 885 upgrade_plugin_mnet_functions($component);
795a08ad 886
194cdca8 887 $endcallback($component, false, $verbose);
795a08ad 888
bde002b8
PS
889 } else if ($installedversion > $plugin->version) {
890 throw new downgrade_exception($component, $installedversion, $plugin->version);
795a08ad 891 }
892 }
893
894
895 // Finally, if we are in the first_install of BLOCKS setup frontpage and admin page blocks
896 if ($first_install) {
795a08ad 897 //Iterate over each course - there should be only site course here now
898 if ($courses = $DB->get_records('course')) {
899 foreach ($courses as $course) {
9d1d606e 900 blocks_add_default_course_blocks($course);
db9d4a3d 901 }
902 }
795a08ad 903
9d1d606e 904 blocks_add_default_system_blocks();
795a08ad 905 }
906}
907
c6d75bff
PS
908
909/**
910 * Log_display description function used during install and upgrade.
911 *
912 * @param string $component name of component (moodle, mod_assignment, etc.)
913 * @return void
914 */
915function log_update_descriptions($component) {
916 global $DB;
917
b0d1d941 918 $defpath = core_component::get_component_directory($component).'/db/log.php';
c6d75bff
PS
919
920 if (!file_exists($defpath)) {
921 $DB->delete_records('log_display', array('component'=>$component));
922 return;
923 }
924
925 // load new info
926 $logs = array();
927 include($defpath);
928 $newlogs = array();
929 foreach ($logs as $log) {
930 $newlogs[$log['module'].'-'.$log['action']] = $log; // kind of unique name
931 }
932 unset($logs);
933 $logs = $newlogs;
934
935 $fields = array('module', 'action', 'mtable', 'field');
936 // update all log fist
937 $dblogs = $DB->get_records('log_display', array('component'=>$component));
938 foreach ($dblogs as $dblog) {
939 $name = $dblog->module.'-'.$dblog->action;
940
941 if (empty($logs[$name])) {
942 $DB->delete_records('log_display', array('id'=>$dblog->id));
943 continue;
944 }
945
946 $log = $logs[$name];
947 unset($logs[$name]);
948
949 $update = false;
950 foreach ($fields as $field) {
951 if ($dblog->$field != $log[$field]) {
952 $dblog->$field = $log[$field];
953 $update = true;
954 }
955 }
956 if ($update) {
957 $DB->update_record('log_display', $dblog);
958 }
959 }
960 foreach ($logs as $log) {
961 $dblog = (object)$log;
962 $dblog->component = $component;
963 $DB->insert_record('log_display', $dblog);
964 }
965}
966
c976e271 967/**
968 * Web service discovery function used during install and upgrade.
969 * @param string $component name of component (moodle, mod_assignment, etc.)
970 * @return void
971 */
972function external_update_descriptions($component) {
bc81eadb 973 global $DB, $CFG;
c976e271 974
b0d1d941 975 $defpath = core_component::get_component_directory($component).'/db/services.php';
c976e271 976
977 if (!file_exists($defpath)) {
bc81eadb 978 require_once($CFG->dirroot.'/lib/externallib.php');
c976e271 979 external_delete_descriptions($component);
980 return;
981 }
982
983 // load new info
984 $functions = array();
985 $services = array();
986 include($defpath);
987
988 // update all function fist
989 $dbfunctions = $DB->get_records('external_functions', array('component'=>$component));
990 foreach ($dbfunctions as $dbfunction) {
991 if (empty($functions[$dbfunction->name])) {
992 $DB->delete_records('external_functions', array('id'=>$dbfunction->id));
993 // do not delete functions from external_services_functions, beacuse
994 // we want to notify admins when functions used in custom services disappear
c6d75bff
PS
995
996 //TODO: this looks wrong, we have to delete it eventually (skodak)
c976e271 997 continue;
998 }
999
1000 $function = $functions[$dbfunction->name];
1001 unset($functions[$dbfunction->name]);
1002 $function['classpath'] = empty($function['classpath']) ? null : $function['classpath'];
1003
1004 $update = false;
1005 if ($dbfunction->classname != $function['classname']) {
1006 $dbfunction->classname = $function['classname'];
1007 $update = true;
1008 }
1009 if ($dbfunction->methodname != $function['methodname']) {
1010 $dbfunction->methodname = $function['methodname'];
1011 $update = true;
1012 }
1013 if ($dbfunction->classpath != $function['classpath']) {
1014 $dbfunction->classpath = $function['classpath'];
1015 $update = true;
1016 }
12fc8acf 1017 $functioncapabilities = array_key_exists('capabilities', $function)?$function['capabilities']:'';
72f68b51 1018 if ($dbfunction->capabilities != $functioncapabilities) {
1019 $dbfunction->capabilities = $functioncapabilities;
1020 $update = true;
1021 }
c976e271 1022 if ($update) {
1023 $DB->update_record('external_functions', $dbfunction);
1024 }
1025 }
1026 foreach ($functions as $fname => $function) {
365a5941 1027 $dbfunction = new stdClass();
c976e271 1028 $dbfunction->name = $fname;
1029 $dbfunction->classname = $function['classname'];
1030 $dbfunction->methodname = $function['methodname'];
1031 $dbfunction->classpath = empty($function['classpath']) ? null : $function['classpath'];
1032 $dbfunction->component = $component;
12fc8acf 1033 $dbfunction->capabilities = array_key_exists('capabilities', $function)?$function['capabilities']:'';
c976e271 1034 $dbfunction->id = $DB->insert_record('external_functions', $dbfunction);
1035 }
1036 unset($functions);
1037
1038 // now deal with services
1039 $dbservices = $DB->get_records('external_services', array('component'=>$component));
1040 foreach ($dbservices as $dbservice) {
1041 if (empty($services[$dbservice->name])) {
bc81eadb 1042 $DB->delete_records('external_tokens', array('externalserviceid'=>$dbservice->id));
c976e271 1043 $DB->delete_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
1044 $DB->delete_records('external_services_users', array('externalserviceid'=>$dbservice->id));
1045 $DB->delete_records('external_services', array('id'=>$dbservice->id));
1046 continue;
1047 }
1048 $service = $services[$dbservice->name];
1049 unset($services[$dbservice->name]);
1050 $service['enabled'] = empty($service['enabled']) ? 0 : $service['enabled'];
1051 $service['requiredcapability'] = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1052 $service['restrictedusers'] = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
af03513f 1053 $service['downloadfiles'] = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
106c55fb 1054 $service['uploadfiles'] = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles'];
c1b65883 1055 $service['shortname'] = !isset($service['shortname']) ? null : $service['shortname'];
c976e271 1056
1057 $update = false;
c976e271 1058 if ($dbservice->requiredcapability != $service['requiredcapability']) {
1059 $dbservice->requiredcapability = $service['requiredcapability'];
1060 $update = true;
1061 }
1062 if ($dbservice->restrictedusers != $service['restrictedusers']) {
1063 $dbservice->restrictedusers = $service['restrictedusers'];
1064 $update = true;
1065 }
af03513f
JM
1066 if ($dbservice->downloadfiles != $service['downloadfiles']) {
1067 $dbservice->downloadfiles = $service['downloadfiles'];
1068 $update = true;
1069 }
106c55fb
DW
1070 if ($dbservice->uploadfiles != $service['uploadfiles']) {
1071 $dbservice->uploadfiles = $service['uploadfiles'];
1072 $update = true;
1073 }
c1b65883
JM
1074 //if shortname is not a PARAM_ALPHANUMEXT, fail (tested here for service update and creation)
1075 if (isset($service['shortname']) and
1076 (clean_param($service['shortname'], PARAM_ALPHANUMEXT) != $service['shortname'])) {
1077 throw new moodle_exception('installserviceshortnameerror', 'webservice', '', $service['shortname']);
1078 }
1079 if ($dbservice->shortname != $service['shortname']) {
1080 //check that shortname is unique
1081 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
1082 $existingservice = $DB->get_record('external_services',
1083 array('shortname' => $service['shortname']));
1084 if (!empty($existingservice)) {
1085 throw new moodle_exception('installexistingserviceshortnameerror', 'webservice', '', $service['shortname']);
1086 }
1087 }
1088 $dbservice->shortname = $service['shortname'];
1089 $update = true;
1090 }
c976e271 1091 if ($update) {
1092 $DB->update_record('external_services', $dbservice);
1093 }
1094
1095 $functions = $DB->get_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
1096 foreach ($functions as $function) {
1097 $key = array_search($function->functionname, $service['functions']);
1098 if ($key === false) {
1099 $DB->delete_records('external_services_functions', array('id'=>$function->id));
1100 } else {
1101 unset($service['functions'][$key]);
1102 }
1103 }
1104 foreach ($service['functions'] as $fname) {
365a5941 1105 $newf = new stdClass();
c976e271 1106 $newf->externalserviceid = $dbservice->id;
1107 $newf->functionname = $fname;
1108 $DB->insert_record('external_services_functions', $newf);
1109 }
1110 unset($functions);
1111 }
1112 foreach ($services as $name => $service) {
c1b65883
JM
1113 //check that shortname is unique
1114 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
1115 $existingservice = $DB->get_record('external_services',
1116 array('shortname' => $service['shortname']));
1117 if (!empty($existingservice)) {
1118 throw new moodle_exception('installserviceshortnameerror', 'webservice');
1119 }
1120 }
1121
365a5941 1122 $dbservice = new stdClass();
c976e271 1123 $dbservice->name = $name;
1124 $dbservice->enabled = empty($service['enabled']) ? 0 : $service['enabled'];
1125 $dbservice->requiredcapability = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1126 $dbservice->restrictedusers = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
af03513f 1127 $dbservice->downloadfiles = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
106c55fb 1128 $dbservice->uploadfiles = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles'];
c1b65883 1129 $dbservice->shortname = !isset($service['shortname']) ? null : $service['shortname'];
c976e271 1130 $dbservice->component = $component;
e5180580 1131 $dbservice->timecreated = time();
c976e271 1132 $dbservice->id = $DB->insert_record('external_services', $dbservice);
1133 foreach ($service['functions'] as $fname) {
365a5941 1134 $newf = new stdClass();
c976e271 1135 $newf->externalserviceid = $dbservice->id;
1136 $newf->functionname = $fname;
1137 $DB->insert_record('external_services_functions', $newf);
1138 }
1139 }
1140}
1141
72fb21b6 1142/**
1143 * upgrade logging functions
72fb21b6 1144 */
fd1a792e 1145function upgrade_handle_exception($ex, $plugin = null) {
96e1fdf4
PS
1146 global $CFG;
1147
a56c457e
PS
1148 // rollback everything, we need to log all upgrade problems
1149 abort_all_db_transactions();
1150
c19bc39c
PS
1151 $info = get_exception_info($ex);
1152
1153 // First log upgrade error
1154 upgrade_log(UPGRADE_LOG_ERROR, $plugin, 'Exception: ' . get_class($ex), $info->message, $info->backtrace);
1155
1156 // Always turn on debugging - admins need to know what is going on
96f81ea3 1157 set_debugging(DEBUG_DEVELOPER, true);
c19bc39c 1158
fd1a792e 1159 default_exception_handler($ex, true, $plugin);
db9d4a3d 1160}
1161
1162/**
1163 * Adds log entry into upgrade_log table
1164 *
1165 * @param int $type UPGRADE_LOG_NORMAL, UPGRADE_LOG_NOTICE or UPGRADE_LOG_ERROR
bb8b2971 1166 * @param string $plugin frankenstyle component name
db9d4a3d 1167 * @param string $info short description text of log entry
1168 * @param string $details long problem description
1169 * @param string $backtrace string used for errors only
1170 * @return void
1171 */
1172function upgrade_log($type, $plugin, $info, $details=null, $backtrace=null) {
1173 global $DB, $USER, $CFG;
1174
bb8b2971
PS
1175 if (empty($plugin)) {
1176 $plugin = 'core';
1177 }
1178
56da374e 1179 list($plugintype, $pluginname) = core_component::normalize_component($plugin);
bb8b2971 1180 $component = is_null($pluginname) ? $plugintype : $plugintype . '_' . $pluginname;
795a08ad 1181
34a2777c 1182 $backtrace = format_backtrace($backtrace, true);
db9d4a3d 1183
bb8b2971
PS
1184 $currentversion = null;
1185 $targetversion = null;
db9d4a3d 1186
1187 //first try to find out current version number
bb8b2971 1188 if ($plugintype === 'core') {
db9d4a3d 1189 //main
bb8b2971 1190 $currentversion = $CFG->version;
db9d4a3d 1191
bb8b2971
PS
1192 $version = null;
1193 include("$CFG->dirroot/version.php");
1194 $targetversion = $version;
795a08ad 1195
795a08ad 1196 } else {
bb8b2971 1197 $pluginversion = get_config($component, 'version');
795a08ad 1198 if (!empty($pluginversion)) {
bb8b2971
PS
1199 $currentversion = $pluginversion;
1200 }
b0d1d941 1201 $cd = core_component::get_component_directory($component);
bb8b2971
PS
1202 if (file_exists("$cd/version.php")) {
1203 $plugin = new stdClass();
1204 $plugin->version = null;
bde002b8 1205 $module = $plugin;
bb8b2971
PS
1206 include("$cd/version.php");
1207 $targetversion = $plugin->version;
795a08ad 1208 }
db9d4a3d 1209 }
1210
365a5941 1211 $log = new stdClass();
bb8b2971
PS
1212 $log->type = $type;
1213 $log->plugin = $component;
1214 $log->version = $currentversion;
1215 $log->targetversion = $targetversion;
1216 $log->info = $info;
1217 $log->details = $details;
1218 $log->backtrace = $backtrace;
1219 $log->userid = $USER->id;
1220 $log->timemodified = time();
db9d4a3d 1221 try {
1222 $DB->insert_record('upgrade_log', $log);
1223 } catch (Exception $ignored) {
795a08ad 1224 // possible during install or 2.0 upgrade
db9d4a3d 1225 }
1226}
1227
1228/**
1229 * Marks start of upgrade, blocks any other access to site.
1230 * The upgrade is finished at the end of script or after timeout.
72fb21b6 1231 *
1232 * @global object
1233 * @global object
1234 * @global object
db9d4a3d 1235 */
1236function upgrade_started($preinstall=false) {
de6d81e6 1237 global $CFG, $DB, $PAGE, $OUTPUT;
db9d4a3d 1238
1239 static $started = false;
1240
1241 if ($preinstall) {
1242 ignore_user_abort(true);
1243 upgrade_setup_debug(true);
1244
1245 } else if ($started) {
1246 upgrade_set_timeout(120);
1247
1248 } else {
c13a5e71 1249 if (!CLI_SCRIPT and !$PAGE->headerprinted) {
db9d4a3d 1250 $strupgrade = get_string('upgradingversion', 'admin');
78946b9b 1251 $PAGE->set_pagelayout('maintenance');
543f54d3 1252 upgrade_init_javascript();
de6d81e6 1253 $PAGE->set_title($strupgrade.' - Moodle '.$CFG->target_release);
1254 $PAGE->set_heading($strupgrade);
1255 $PAGE->navbar->add($strupgrade);
1256 $PAGE->set_cacheable(false);
1257 echo $OUTPUT->header();
db9d4a3d 1258 }
1259
1260 ignore_user_abort(true);
38fc0130 1261 core_shutdown_manager::register_function('upgrade_finished_handler');
db9d4a3d 1262 upgrade_setup_debug(true);
1263 set_config('upgraderunning', time()+300);
1264 $started = true;
1265 }
1266}
1267
1268/**
b921b6ee 1269 * Internal function - executed if upgrade interrupted.
db9d4a3d 1270 */
1271function upgrade_finished_handler() {
1272 upgrade_finished();
1273}
1274
1275/**
1276 * Indicates upgrade is finished.
1277 *
1278 * This function may be called repeatedly.
72fb21b6 1279 *
1280 * @global object
1281 * @global object
db9d4a3d 1282 */
1283function upgrade_finished($continueurl=null) {
7e0d6675 1284 global $CFG, $DB, $OUTPUT;
db9d4a3d 1285
1286 if (!empty($CFG->upgraderunning)) {
1287 unset_config('upgraderunning');
24dedb96
SH
1288 // We have to forcefully purge the caches using the writer here.
1289 // This has to be done after we unset the config var. If someone hits the site while this is set they will
1290 // cause the config values to propogate to the caches.
1291 // Caches are purged after the last step in an upgrade but there is several code routines that exceute between
1292 // then and now that leaving a window for things to fall out of sync.
1293 cache_helper::purge_all(true);
db9d4a3d 1294 upgrade_setup_debug(false);
1295 ignore_user_abort(false);
1296 if ($continueurl) {
aa9a6867 1297 echo $OUTPUT->continue_button($continueurl);
7e0d6675 1298 echo $OUTPUT->footer();
db9d4a3d 1299 die;
1300 }
1301 }
1302}
1303
72fb21b6 1304/**
1305 * @global object
1306 * @global object
1307 */
db9d4a3d 1308function upgrade_setup_debug($starting) {
1309 global $CFG, $DB;
1310
1311 static $originaldebug = null;
1312
1313 if ($starting) {
1314 if ($originaldebug === null) {
1315 $originaldebug = $DB->get_debug();
1316 }
1317 if (!empty($CFG->upgradeshowsql)) {
1318 $DB->set_debug(true);
1319 }
1320 } else {
1321 $DB->set_debug($originaldebug);
1322 }
1323}
1324
1325function print_upgrade_separator() {
1326 if (!CLI_SCRIPT) {
1327 echo '<hr />';
1328 }
1329}
1330
795a08ad 1331/**
1332 * Default start upgrade callback
1333 * @param string $plugin
b921b6ee 1334 * @param bool $installation true if installation, false means upgrade
795a08ad 1335 */
194cdca8 1336function print_upgrade_part_start($plugin, $installation, $verbose) {
3c159385 1337 global $OUTPUT;
795a08ad 1338 if (empty($plugin) or $plugin == 'moodle') {
1339 upgrade_started($installation); // does not store upgrade running flag yet
194cdca8 1340 if ($verbose) {
3c159385 1341 echo $OUTPUT->heading(get_string('coresystem'));
194cdca8 1342 }
795a08ad 1343 } else {
1344 upgrade_started();
194cdca8 1345 if ($verbose) {
3c159385 1346 echo $OUTPUT->heading($plugin);
194cdca8 1347 }
795a08ad 1348 }
1349 if ($installation) {
1350 if (empty($plugin) or $plugin == 'moodle') {
1351 // no need to log - log table not yet there ;-)
1352 } else {
1353 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin installation');
1354 }
1355 } else {
1356 if (empty($plugin) or $plugin == 'moodle') {
1357 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting core upgrade');
1358 } else {
1359 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin upgrade');
1360 }
1361 }
1362}
1363
1364/**
1365 * Default end upgrade callback
1366 * @param string $plugin
b921b6ee 1367 * @param bool $installation true if installation, false means upgrade
795a08ad 1368 */
194cdca8 1369function print_upgrade_part_end($plugin, $installation, $verbose) {
aa9a6867 1370 global $OUTPUT;
795a08ad 1371 upgrade_started();
1372 if ($installation) {
1373 if (empty($plugin) or $plugin == 'moodle') {
1374 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core installed');
1375 } else {
1376 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin installed');
1377 }
1378 } else {
1379 if (empty($plugin) or $plugin == 'moodle') {
1380 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core upgraded');
1381 } else {
1382 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin upgraded');
1383 }
1384 }
194cdca8 1385 if ($verbose) {
aa9a6867 1386 echo $OUTPUT->notification(get_string('success'), 'notifysuccess');
194cdca8 1387 print_upgrade_separator();
96db14f9 1388 }
1389}
1390
72fb21b6 1391/**
b921b6ee 1392 * Sets up JS code required for all upgrade scripts.
72fb21b6 1393 * @global object
1394 */
543f54d3 1395function upgrade_init_javascript() {
e29380f3 1396 global $PAGE;
543f54d3
PS
1397 // scroll to the end of each upgrade page so that ppl see either error or continue button,
1398 // no need to scroll continuously any more, it is enough to jump to end once the footer is printed ;-)
1399 $js = "window.scrollTo(0, 5000000);";
1400 $PAGE->requires->js_init_code($js);
db9d4a3d 1401}
1402
db9d4a3d 1403/**
1404 * Try to upgrade the given language pack (or current language)
af8d4d91 1405 *
74a4c9a9 1406 * @param string $lang the code of the language to update, defaults to the current language
db9d4a3d 1407 */
bd41bdd9
PS
1408function upgrade_language_pack($lang = null) {
1409 global $CFG;
db9d4a3d 1410
bd41bdd9
PS
1411 if (!empty($CFG->skiplangupgrade)) {
1412 return;
1413 }
af8d4d91 1414
bd41bdd9
PS
1415 if (!file_exists("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php")) {
1416 // weird, somebody uninstalled the import utility
1417 return;
1418 }
1419
1420 if (!$lang) {
db9d4a3d 1421 $lang = current_language();
1422 }
1423
bd41bdd9
PS
1424 if (!get_string_manager()->translation_exists($lang)) {
1425 return;
db9d4a3d 1426 }
1427
bd41bdd9
PS
1428 get_string_manager()->reset_caches();
1429
1430 if ($lang === 'en') {
1431 return; // Nothing to do
db9d4a3d 1432 }
af8d4d91 1433
bd41bdd9
PS
1434 upgrade_started(false);
1435
1436 require_once("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php");
1437 tool_langimport_preupgrade_update($lang);
1438
af8d4d91
DM
1439 get_string_manager()->reset_caches();
1440
551fe0e5 1441 print_upgrade_separator();
db9d4a3d 1442}
8580535b 1443
1444/**
1445 * Install core moodle tables and initialize
1446 * @param float $version target version
1447 * @param bool $verbose
1448 * @return void, may throw exception
1449 */
1450function install_core($version, $verbose) {
1451 global $CFG, $DB;
1452
e2e35e71 1453 // We can not call purge_all_caches() yet, make sure the temp and cache dirs exist and are empty.
e2e35e71 1454 remove_dir($CFG->cachedir.'', true);
85b38061
PS
1455 make_cache_directory('', true);
1456
1457 remove_dir($CFG->localcachedir.'', true);
1458 make_localcache_directory('', true);
1459
e2e35e71 1460 remove_dir($CFG->tempdir.'', true);
85b38061
PS
1461 make_temp_directory('', true);
1462
e2e35e71 1463 remove_dir($CFG->dataroot.'/muc', true);
85b38061 1464 make_writable_directory($CFG->dataroot.'/muc', true);
e2e35e71 1465
8580535b 1466 try {
3ef7279f 1467 core_php_time_limit::raise(600);
194cdca8 1468 print_upgrade_part_start('moodle', true, $verbose); // does not store upgrade running flag
8580535b 1469
1470 $DB->get_manager()->install_from_xmldb_file("$CFG->libdir/db/install.xml");
1471 upgrade_started(); // we want the flag to be stored in config table ;-)
1472
1473 // set all core default records and default settings
1474 require_once("$CFG->libdir/db/install.php");
c6d75bff 1475 xmldb_main_install(); // installs the capabilities too
8580535b 1476
1477 // store version
1478 upgrade_main_savepoint(true, $version, false);
1479
df997f84 1480 // Continue with the installation
c6d75bff
PS
1481 log_update_descriptions('moodle');
1482 external_update_descriptions('moodle');
8580535b 1483 events_update_definition('moodle');
1484 message_update_providers('moodle');
8580535b 1485
df997f84 1486 // Write default settings unconditionally
8580535b 1487 admin_apply_default_settings(NULL, true);
1488
194cdca8 1489 print_upgrade_part_end(null, true, $verbose);
94ef67cf 1490
e0d9b7c0
SH
1491 // Purge all caches. They're disabled but this ensures that we don't have any persistent data just in case something
1492 // during installation didn't use APIs.
1493 cache_helper::purge_all();
8580535b 1494 } catch (exception $ex) {
1495 upgrade_handle_exception($ex);
1496 }
1497}
1498
1499/**
1500 * Upgrade moodle core
1501 * @param float $version target version
1502 * @param bool $verbose
1503 * @return void, may throw exception
1504 */
1505function upgrade_core($version, $verbose) {
1506 global $CFG;
1507
41209c1e
PS
1508 raise_memory_limit(MEMORY_EXTRA);
1509
3316fe24 1510 require_once($CFG->libdir.'/db/upgrade.php'); // Defines upgrades
3316fe24 1511
8580535b 1512 try {
7b4d5af7 1513 // Reset caches before any output.
b0dd08dd 1514 cache_helper::purge_all(true);
7b4d5af7 1515 purge_all_caches();
dcf9be7f 1516
8580535b 1517 // Upgrade current language pack if we can
bd41bdd9 1518 upgrade_language_pack();
8580535b 1519
194cdca8 1520 print_upgrade_part_start('moodle', false, $verbose);
8580535b 1521
6b4b7490
PS
1522 // Pre-upgrade scripts for local hack workarounds.
1523 $preupgradefile = "$CFG->dirroot/local/preupgrade.php";
1524 if (file_exists($preupgradefile)) {
3ef7279f 1525 core_php_time_limit::raise();
6b4b7490
PS
1526 require($preupgradefile);
1527 // Reset upgrade timeout to default.
1528 upgrade_set_timeout();
17da2e6f 1529 }
1530
8580535b 1531 $result = xmldb_main_upgrade($CFG->version);
1532 if ($version > $CFG->version) {
1533 // store version if not already there
1534 upgrade_main_savepoint($result, $version, false);
1535 }
1536
1537 // perform all other component upgrade routines
1538 update_capabilities('moodle');
c6d75bff 1539 log_update_descriptions('moodle');
c976e271 1540 external_update_descriptions('moodle');
8580535b 1541 events_update_definition('moodle');
1542 message_update_providers('moodle');
75af47ee
SH
1543 // Update core definitions.
1544 cache_helper::update_definitions(true);
8580535b 1545
94ef67cf 1546 // Purge caches again, just to be sure we arn't holding onto old stuff now.
a5a6e93f 1547 cache_helper::purge_all(true);
7b4d5af7 1548 purge_all_caches();
2e5eba85
PS
1549
1550 // Clean up contexts - more and more stuff depends on existence of paths and contexts
e922fe23
PS
1551 context_helper::cleanup_instances();
1552 context_helper::create_instances(null, false);
1553 context_helper::build_all_paths(false);
1554 $syscontext = context_system::instance();
1555 $syscontext->mark_dirty();
8580535b 1556
194cdca8 1557 print_upgrade_part_end('moodle', false, $verbose);
8580535b 1558 } catch (Exception $ex) {
1559 upgrade_handle_exception($ex);
1560 }
1561}
1562
1563/**
1564 * Upgrade/install other parts of moodle
1565 * @param bool $verbose
1566 * @return void, may throw exception
1567 */
1568function upgrade_noncore($verbose) {
1569 global $CFG;
1570
41209c1e
PS
1571 raise_memory_limit(MEMORY_EXTRA);
1572
8580535b 1573 // upgrade all plugins types
1574 try {
7b4d5af7
PS
1575 // Reset caches before any output.
1576 cache_helper::purge_all(true);
1577 purge_all_caches();
1578
46f6f7f2 1579 $plugintypes = core_component::get_plugin_types();
8580535b 1580 foreach ($plugintypes as $type=>$location) {
17da2e6f 1581 upgrade_plugins($type, 'print_upgrade_part_start', 'print_upgrade_part_end', $verbose);
8580535b 1582 }
94ef67cf 1583 // Update cache definitions. Involves scanning each plugin for any changes.
75af47ee 1584 cache_helper::update_definitions();
c5701ce7
PS
1585 // Mark the site as upgraded.
1586 set_config('allversionshash', core_component::get_all_versions_hash());
7b4d5af7
PS
1587
1588 // Purge caches again, just to be sure we arn't holding onto old stuff now.
1589 cache_helper::purge_all(true);
1590 purge_all_caches();
1591
8580535b 1592 } catch (Exception $ex) {
1593 upgrade_handle_exception($ex);
1594 }
8580535b 1595}
3316fe24 1596
1597/**
1598 * Checks if the main tables have been installed yet or not.
e2e35e71
PS
1599 *
1600 * Note: we can not use caches here because they might be stale,
1601 * use with care!
1602 *
3316fe24 1603 * @return bool
1604 */
1605function core_tables_exist() {
1606 global $DB;
1607
e2e35e71 1608 if (!$tables = $DB->get_tables(false) ) { // No tables yet at all.
3316fe24 1609 return false;
9b683d13 1610
3316fe24 1611 } else { // Check for missing main tables
1612 $mtables = array('config', 'course', 'groupings'); // some tables used in 1.9 and 2.0, preferable something from the start and end of install.xml
1613 foreach ($mtables as $mtable) {
1614 if (!in_array($mtable, $tables)) {
1615 return false;
1616 }
1617 }
1618 return true;
9b683d13 1619 }
ba04999c 1620}
c71ade2f
PL
1621
1622/**
1623 * upgrades the mnet rpc definitions for the given component.
1624 * this method doesn't return status, an exception will be thrown in the case of an error
1625 *
1626 * @param string $component the plugin to upgrade, eg auth_mnet
1627 */
1628function upgrade_plugin_mnet_functions($component) {
1629 global $DB, $CFG;
1630
8c59114f 1631 list($type, $plugin) = core_component::normalize_component($component);
1c74b260 1632 $path = core_component::get_plugin_directory($type, $plugin);
c71ade2f 1633
17b71716
PS
1634 $publishes = array();
1635 $subscribes = array();
c71ade2f
PL
1636 if (file_exists($path . '/db/mnet.php')) {
1637 require_once($path . '/db/mnet.php'); // $publishes comes from this file
1638 }
1639 if (empty($publishes)) {
1640 $publishes = array(); // still need this to be able to disable stuff later
1641 }
1642 if (empty($subscribes)) {
1643 $subscribes = array(); // still need this to be able to disable stuff later
1644 }
1645
1646 static $servicecache = array();
1647
1648 // rekey an array based on the rpc method for easy lookups later
1649 $publishmethodservices = array();
1650 $subscribemethodservices = array();
1651 foreach($publishes as $servicename => $service) {
1652 if (is_array($service['methods'])) {
1653 foreach($service['methods'] as $methodname) {
1654 $service['servicename'] = $servicename;
1655 $publishmethodservices[$methodname][] = $service;
1656 }
1657 }
1658 }
1659
1660 // Disable functions that don't exist (any more) in the source
1661 // Should these be deleted? What about their permissions records?
1662 foreach ($DB->get_records('mnet_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
1663 if (!array_key_exists($rpc->functionname, $publishmethodservices) && $rpc->enabled) {
1664 $DB->set_field('mnet_rpc', 'enabled', 0, array('id' => $rpc->id));
1665 } else if (array_key_exists($rpc->functionname, $publishmethodservices) && !$rpc->enabled) {
1666 $DB->set_field('mnet_rpc', 'enabled', 1, array('id' => $rpc->id));
1667 }
1668 }
1669
1670 // reflect all the services we're publishing and save them
1671 require_once($CFG->dirroot . '/lib/zend/Zend/Server/Reflection.php');
1672 static $cachedclasses = array(); // to store reflection information in
1673 foreach ($publishes as $service => $data) {
1674 $f = $data['filename'];
1675 $c = $data['classname'];
1676 foreach ($data['methods'] as $method) {
6bdfef5d 1677 $dataobject = new stdClass();
c71ade2f
PL
1678 $dataobject->plugintype = $type;
1679 $dataobject->pluginname = $plugin;
1680 $dataobject->enabled = 1;
1681 $dataobject->classname = $c;
1682 $dataobject->filename = $f;
1683
1684 if (is_string($method)) {
1685 $dataobject->functionname = $method;
1686
1687 } else if (is_array($method)) { // wants to override file or class
1688 $dataobject->functionname = $method['method'];
1689 $dataobject->classname = $method['classname'];
1690 $dataobject->filename = $method['filename'];
1691 }
1692 $dataobject->xmlrpcpath = $type.'/'.$plugin.'/'.$dataobject->filename.'/'.$method;
1693 $dataobject->static = false;
1694
1695 require_once($path . '/' . $dataobject->filename);
1696 $functionreflect = null; // slightly different ways to get this depending on whether it's a class method or a function
1697 if (!empty($dataobject->classname)) {
1698 if (!class_exists($dataobject->classname)) {
1699 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
1700 }
1701 $key = $dataobject->filename . '|' . $dataobject->classname;
1702 if (!array_key_exists($key, $cachedclasses)) { // look to see if we've already got a reflection object
1703 try {
1704 $cachedclasses[$key] = Zend_Server_Reflection::reflectClass($dataobject->classname);
1705 } catch (Zend_Server_Reflection_Exception $e) { // catch these and rethrow them to something more helpful
1706 throw new moodle_exception('installreflectionclasserror', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname, 'error' => $e->getMessage()));
1707 }
1708 }
1709 $r =& $cachedclasses[$key];
1710 if (!$r->hasMethod($dataobject->functionname)) {
1711 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
1712 }
1713 // stupid workaround for zend not having a getMethod($name) function
1714 $ms = $r->getMethods();
1715 foreach ($ms as $m) {
1716 if ($m->getName() == $dataobject->functionname) {
1717 $functionreflect = $m;
1718 break;
1719 }
1720 }
1721 $dataobject->static = (int)$functionreflect->isStatic();
1722 } else {
1723 if (!function_exists($dataobject->functionname)) {
1724 throw new moodle_exception('installnosuchfunction', 'mnet', '', (object)array('method' => $dataobject->functionname, 'file' => $dataobject->filename));
1725 }
1726 try {
1727 $functionreflect = Zend_Server_Reflection::reflectFunction($dataobject->functionname);
1728 } catch (Zend_Server_Reflection_Exception $e) { // catch these and rethrow them to something more helpful
1729 throw new moodle_exception('installreflectionfunctionerror', 'mnet', '', (object)array('method' => $dataobject->functionname, '' => $dataobject->filename, 'error' => $e->getMessage()));
1730 }
1731 }
1732 $dataobject->profile = serialize(admin_mnet_method_profile($functionreflect));
1733 $dataobject->help = $functionreflect->getDescription();
1734
1735 if ($record_exists = $DB->get_record('mnet_rpc', array('xmlrpcpath'=>$dataobject->xmlrpcpath))) {
1736 $dataobject->id = $record_exists->id;
1737 $dataobject->enabled = $record_exists->enabled;
1738 $DB->update_record('mnet_rpc', $dataobject);
1739 } else {
1740 $dataobject->id = $DB->insert_record('mnet_rpc', $dataobject, true);
1741 }
c71ade2f 1742
d5bd0b01
DM
1743 // TODO this API versioning must be reworked, here the recently processed method
1744 // sets the service API which may not be correct
677b6321
PL
1745 foreach ($publishmethodservices[$dataobject->functionname] as $service) {
1746 if ($serviceobj = $DB->get_record('mnet_service', array('name'=>$service['servicename']))) {
1747 $serviceobj->apiversion = $service['apiversion'];
1748 $DB->update_record('mnet_service', $serviceobj);
1749 } else {
1750 $serviceobj = new stdClass();
1751 $serviceobj->name = $service['servicename'];
f4a2817a 1752 $serviceobj->description = empty($service['description']) ? '' : $service['description'];
677b6321
PL
1753 $serviceobj->apiversion = $service['apiversion'];
1754 $serviceobj->offer = 1;
1755 $serviceobj->id = $DB->insert_record('mnet_service', $serviceobj);
1756 }
1757 $servicecache[$service['servicename']] = $serviceobj;
1758 if (!$DB->record_exists('mnet_service2rpc', array('rpcid'=>$dataobject->id, 'serviceid'=>$serviceobj->id))) {
1759 $obj = new stdClass();
1760 $obj->rpcid = $dataobject->id;
1761 $obj->serviceid = $serviceobj->id;
1762 $DB->insert_record('mnet_service2rpc', $obj, true);
1763 }
c71ade2f
PL
1764 }
1765 }
1766 }
c71ade2f
PL
1767 // finished with methods we publish, now do subscribable methods
1768 foreach($subscribes as $service => $methods) {
1769 if (!array_key_exists($service, $servicecache)) {
1770 if (!$serviceobj = $DB->get_record('mnet_service', array('name' => $service))) {
81634f03 1771 debugging("TODO: skipping unknown service $service - somebody needs to fix MDL-21993");
c71ade2f
PL
1772 continue;
1773 }
1774 $servicecache[$service] = $serviceobj;
1775 } else {
1776 $serviceobj = $servicecache[$service];
1777 }
1778 foreach ($methods as $method => $xmlrpcpath) {
1779 if (!$rpcid = $DB->get_field('mnet_remote_rpc', 'id', array('xmlrpcpath'=>$xmlrpcpath))) {
1780 $remoterpc = (object)array(
1781 'functionname' => $method,
1782 'xmlrpcpath' => $xmlrpcpath,
1783 'plugintype' => $type,
1784 'pluginname' => $plugin,
1785 'enabled' => 1,
1786 );
1787 $rpcid = $remoterpc->id = $DB->insert_record('mnet_remote_rpc', $remoterpc, true);
1788 }
1789 if (!$DB->record_exists('mnet_remote_service2rpc', array('rpcid'=>$rpcid, 'serviceid'=>$serviceobj->id))) {
1790 $obj = new stdClass();
1791 $obj->rpcid = $rpcid;
1792 $obj->serviceid = $serviceobj->id;
1793 $DB->insert_record('mnet_remote_service2rpc', $obj, true);
1794 }
677b6321 1795 $subscribemethodservices[$method][] = $service;
c71ade2f
PL
1796 }
1797 }
1798
1799 foreach ($DB->get_records('mnet_remote_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
1800 if (!array_key_exists($rpc->functionname, $subscribemethodservices) && $rpc->enabled) {
1801 $DB->set_field('mnet_remote_rpc', 'enabled', 0, array('id' => $rpc->id));
1802 } else if (array_key_exists($rpc->functionname, $subscribemethodservices) && !$rpc->enabled) {
1803 $DB->set_field('mnet_remote_rpc', 'enabled', 1, array('id' => $rpc->id));
1804 }
1805 }
1806
1807 return true;
1808}
1809
1810/**
1811 * Given some sort of Zend Reflection function/method object, return a profile array, ready to be serialized and stored
1812 *
1813 * @param Zend_Server_Reflection_Function_Abstract $function can be any subclass of this object type
1814 *
1815 * @return array
1816 */
1817function admin_mnet_method_profile(Zend_Server_Reflection_Function_Abstract $function) {
f20edd52
PS
1818 $protos = $function->getPrototypes();
1819 $proto = array_pop($protos);
c71ade2f
PL
1820 $ret = $proto->getReturnValue();
1821 $profile = array(
1822 'parameters' => array(),
1823 'return' => array(
1824 'type' => $ret->getType(),
1825 'description' => $ret->getDescription(),
1826 ),
1827 );
1828 foreach ($proto->getParameters() as $p) {
1829 $profile['parameters'][] = array(
1830 'name' => $p->getName(),
1831 'type' => $p->getType(),
1832 'description' => $p->getDescription(),
1833 );
1834 }
1835 return $profile;
1836}
424a19b1
AB
1837
1838
1839/**
1840 * This function finds duplicate records (based on combinations of fields that should be unique)
1841 * and then progamatically generated a "most correct" version of the data, update and removing
1842 * records as appropriate
1843 *
1844 * Thanks to Dan Marsden for help
1845 *
1846 * @param string $table Table name
1847 * @param array $uniques Array of field names that should be unique
e6f55285 1848 * @param array $fieldstocheck Array of fields to generate "correct" data from (optional)
424a19b1
AB
1849 * @return void
1850 */
1851function upgrade_course_completion_remove_duplicates($table, $uniques, $fieldstocheck = array()) {
1852 global $DB;
1853
1854 // Find duplicates
1855 $sql_cols = implode(', ', $uniques);
1856
1857 $sql = "SELECT {$sql_cols} FROM {{$table}} GROUP BY {$sql_cols} HAVING (count(id) > 1)";
1858 $duplicates = $DB->get_recordset_sql($sql, array());
1859
1860 // Loop through duplicates
1861 foreach ($duplicates as $duplicate) {
1862 $pointer = 0;
1863
1864 // Generate SQL for finding records with these duplicate uniques
e6f55285 1865 $sql_select = implode(' = ? AND ', $uniques).' = ?'; // builds "fieldname = ? AND fieldname = ?"
424a19b1
AB
1866 $uniq_values = array();
1867 foreach ($uniques as $u) {
1868 $uniq_values[] = $duplicate->$u;
1869 }
1870
1871 $sql_order = implode(' DESC, ', $uniques).' DESC'; // builds "fieldname DESC, fieldname DESC"
1872
1873 // Get records with these duplicate uniques
1874 $records = $DB->get_records_select(
1875 $table,
1876 $sql_select,
1877 $uniq_values,
1878 $sql_order
1879 );
1880
1881 // Loop through and build a "correct" record, deleting the others
1882 $needsupdate = false;
1883 $origrecord = null;
1884 foreach ($records as $record) {
1885 $pointer++;
1886 if ($pointer === 1) { // keep 1st record but delete all others.
1887 $origrecord = $record;
1888 } else {
1889 // If we have fields to check, update original record
1890 if ($fieldstocheck) {
1891 // we need to keep the "oldest" of all these fields as the valid completion record.
1892 // but we want to ignore null values
1893 foreach ($fieldstocheck as $f) {
1894 if ($record->$f && (($origrecord->$f > $record->$f) || !$origrecord->$f)) {
1895 $origrecord->$f = $record->$f;
1896 $needsupdate = true;
1897 }
1898 }
1899 }
1900 $DB->delete_records($table, array('id' => $record->id));
1901 }
1902 }
1903 if ($needsupdate || isset($origrecord->reaggregate)) {
e6f55285 1904 // If this table has a reaggregate field, update to force recheck on next cron run
424a19b1
AB
1905 if (isset($origrecord->reaggregate)) {
1906 $origrecord->reaggregate = time();
1907 }
1908 $DB->update_record($table, $origrecord);
1909 }
1910 }
1911}
67e3dd2c
FM
1912
1913/**
1914 * Find questions missing an existing category and associate them with
1915 * a category which purpose is to gather them.
1916 *
1917 * @return void
1918 */
1919function upgrade_save_orphaned_questions() {
1920 global $DB;
1921
1922 // Looking for orphaned questions
1923 $orphans = $DB->record_exists_select('question',
1924 'NOT EXISTS (SELECT 1 FROM {question_categories} WHERE {question_categories}.id = {question}.category)');
1925 if (!$orphans) {
1926 return;
1927 }
1928
1929 // Generate a unique stamp for the orphaned questions category, easier to identify it later on
1930 $uniquestamp = "unknownhost+120719170400+orphan";
1931 $systemcontext = context_system::instance();
1932
1933 // Create the orphaned category at system level
1934 $cat = $DB->get_record('question_categories', array('stamp' => $uniquestamp,
1935 'contextid' => $systemcontext->id));
1936 if (!$cat) {
1937 $cat = new stdClass();
1938 $cat->parent = 0;
1939 $cat->contextid = $systemcontext->id;
1940 $cat->name = get_string('orphanedquestionscategory', 'question');
1941 $cat->info = get_string('orphanedquestionscategoryinfo', 'question');
1942 $cat->sortorder = 999;
1943 $cat->stamp = $uniquestamp;
1944 $cat->id = $DB->insert_record("question_categories", $cat);
1945 }
1946
1947 // Set a category to those orphans
1948 $params = array('catid' => $cat->id);
1949 $DB->execute('UPDATE {question} SET category = :catid WHERE NOT EXISTS
1950 (SELECT 1 FROM {question_categories} WHERE {question_categories}.id = {question}.category)', $params);
1951}
1952
d8271e39
FM
1953/**
1954 * Rename old backup files to current backup files.
1955 *
1956 * When added the setting 'backup_shortname' (MDL-28657) the backup file names did not contain the id of the course.
1957 * Further we fixed that behaviour by forcing the id to be always present in the file name (MDL-33812).
1958 * This function will explore the backup directory and attempt to rename the previously created files to include
1959 * the id in the name. Doing this will put them back in the process of deleting the excess backups for each course.
1960 *
1961 * This function manually recreates the file name, instead of using
1962 * {@link backup_plan_dbops::get_default_backup_filename()}, use it carefully if you're using it outside of the
1963 * usual upgrade process.
1964 *
1965 * @see backup_cron_automated_helper::remove_excess_backups()
1966 * @link http://tracker.moodle.org/browse/MDL-35116
1967 * @return void
1968 * @since 2.4
1969 */
1970function upgrade_rename_old_backup_files_using_shortname() {
1971 global $CFG;
1972 $dir = get_config('backup', 'backup_auto_destination');
1973 $useshortname = get_config('backup', 'backup_shortname');
1974 if (empty($dir) || !is_dir($dir) || !is_writable($dir)) {
1975 return;
1976 }
1977
d8271e39 1978 require_once($CFG->dirroot.'/backup/util/includes/backup_includes.php');
2f1e464a 1979 $backupword = str_replace(' ', '_', core_text::strtolower(get_string('backupfilename')));
d8271e39
FM
1980 $backupword = trim(clean_filename($backupword), '_');
1981 $filename = $backupword . '-' . backup::FORMAT_MOODLE . '-' . backup::TYPE_1COURSE . '-';
1982 $regex = '#^'.preg_quote($filename, '#').'.*\.mbz$#';
1983 $thirtyapril = strtotime('30 April 2012 00:00');
1984
1985 // Reading the directory.
1986 if (!$files = scandir($dir)) {
1987 return;
1988 }
1989 foreach ($files as $file) {
1990 // Skip directories and files which do not start with the common prefix.
1991 // This avoids working on files which are not related to this issue.
1992 if (!is_file($dir . '/' . $file) || !preg_match($regex, $file)) {
1993 continue;
1994 }
1995
1996 // Extract the information from the XML file.
1997 try {
1998 $bcinfo = backup_general_helper::get_backup_information_from_mbz($dir . '/' . $file);
1999 } catch (backup_helper_exception $e) {
2000 // Some error while retrieving the backup informations, skipping...
2001 continue;
2002 }
2003
2004 // Make sure this a course backup.
2005 if ($bcinfo->format !== backup::FORMAT_MOODLE || $bcinfo->type !== backup::TYPE_1COURSE) {
2006 continue;
2007 }
2008
2009 // Skip the backups created before the short name option was initially introduced (MDL-28657).
2010 // This was integrated on the 2nd of May 2012. Let's play safe with timezone and use the 30th of April.
2011 if ($bcinfo->backup_date < $thirtyapril) {
2012 continue;
2013 }
2014
2015 // Let's check if the file name contains the ID where it is supposed to be, if it is the case then
2016 // we will skip the file. Of course it could happen that the course ID is identical to the course short name
2017 // even though really unlikely, but then renaming this file is not necessary. If the ID is not found in the
2018 // file name then it was probably the short name which was used.
2019 $idfilename = $filename . $bcinfo->original_course_id . '-';
2020 $idregex = '#^'.preg_quote($idfilename, '#').'.*\.mbz$#';
2021 if (preg_match($idregex, $file)) {
2022 continue;
2023 }
2024
2025 // Generating the file name manually. We do not use backup_plan_dbops::get_default_backup_filename() because
2026 // it will query the database to get some course information, and the course could not exist any more.
2027 $newname = $filename . $bcinfo->original_course_id . '-';
2028 if ($useshortname) {
2029 $shortname = str_replace(' ', '_', $bcinfo->original_course_shortname);
2f1e464a 2030 $shortname = core_text::strtolower(trim(clean_filename($shortname), '_'));
d8271e39
FM
2031 $newname .= $shortname . '-';
2032 }
2033
2034 $backupdateformat = str_replace(' ', '_', get_string('backupnameformat', 'langconfig'));
2035 $date = userdate($bcinfo->backup_date, $backupdateformat, 99, false);
2f1e464a 2036 $date = core_text::strtolower(trim(clean_filename($date), '_'));
d8271e39
FM
2037 $newname .= $date;
2038
2039 if (isset($bcinfo->root_settings['users']) && !$bcinfo->root_settings['users']) {
2040 $newname .= '-nu';
2041 } else if (isset($bcinfo->root_settings['anonymize']) && $bcinfo->root_settings['anonymize']) {
2042 $newname .= '-an';
2043 }
2044 $newname .= '.mbz';
2045
2046 // Final check before attempting the renaming.
2047 if ($newname == $file || file_exists($dir . '/' . $newname)) {
2048 continue;
2049 }
2050 @rename($dir . '/' . $file, $dir . '/' . $newname);
2051 }
2052}
2a9d7a42
DP
2053
2054/**
2055 * Detect duplicate grade item sortorders and resort the
2056 * items to remove them.
2057 */
2058function upgrade_grade_item_fix_sortorder() {
2059 global $DB;
2060
2061 // The simple way to fix these sortorder duplicates would be simply to resort each
2062 // affected course. But in order to reduce the impact of this upgrade step we're trying
2063 // to do it more efficiently by doing a series of update statements rather than updating
2064 // every single grade item in affected courses.
2065
2066 $transaction = $DB->start_delegated_transaction();
2067
2068 $sql = "SELECT g1.id, g1.courseid, g1.sortorder
2069 FROM {grade_items} g1
2070 JOIN {grade_items} g2 ON g1.courseid = g2.courseid
2071 WHERE g1.sortorder = g2.sortorder AND g1.id != g2.id
2072 ORDER BY g1.courseid ASC, g1.sortorder DESC, g1.id DESC";
2073
2074 // Get all duplicates in course order, highest sort order, and higest id first so that we can make space at the
2075 // bottom higher end of the sort orders and work down by id.
2076 $rs = $DB->get_recordset_sql($sql);
2077
2078 foreach($rs as $duplicate) {
2079 $DB->execute("UPDATE {grade_items}
2080 SET sortorder = sortorder + 1
b35ef82f
MG
2081 WHERE courseid = :courseid AND
2082 (sortorder > :sortorder OR (sortorder = :sortorder2 AND id > :id))",
2083 array('courseid' => $duplicate->courseid,
2084 'sortorder' => $duplicate->sortorder,
2085 'sortorder2' => $duplicate->sortorder,
2086 'id' => $duplicate->id));
2a9d7a42
DP
2087 }
2088 $rs->close();
2089
2090 $transaction->allow_commit();
2091}
119cf17e
DP
2092
2093/**
2094 * Detect file areas with missing root directory records and add them.
2095 */
2096function upgrade_fix_missing_root_folders() {
2097 global $DB, $USER;
2098
2099 $transaction = $DB->start_delegated_transaction();
40c761e0
DP
2100
2101 $sql = "SELECT contextid, component, filearea, itemid
2102 FROM {files}
2103 WHERE (component <> 'user' OR filearea <> 'draft')
2104 GROUP BY contextid, component, filearea, itemid
2105 HAVING MAX(CASE WHEN filename = '.' AND filepath = '/' THEN 1 ELSE 0 END) = 0";
2106
119cf17e
DP
2107 $rs = $DB->get_recordset_sql($sql);
2108 $defaults = array('filepath' => '/',
2109 'filename' => '.',
2110 'userid' => $USER->id,
2111 'filesize' => 0,
2112 'timecreated' => time(),
2113 'timemodified' => time(),
2114 'contenthash' => sha1(''));
2115 foreach ($rs as $r) {
2116 $pathhash = sha1("/$r->contextid/$r->component/$r->filearea/$r->itemid".'/.');
2117 $DB->insert_record('files', (array)$r + $defaults +
2118 array('pathnamehash' => $pathhash));
2119 }
2120 $rs->close();
2121 $transaction->allow_commit();
2122}