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