MDL-66645 install: update the list of stale files for 3.8
[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
03440bcb 140/**
141 * Static class monitors performance of upgrade steps.
142 */
143class core_upgrade_time {
144 /** @var float Time at start of current upgrade (plugin/system) */
145 protected static $before;
146 /** @var float Time at end of last savepoint */
147 protected static $lastsavepoint;
83740b9f
JP
148 /** @var bool Flag to indicate whether we are recording timestamps or not. */
149 protected static $isrecording = false;
03440bcb 150
151 /**
152 * Records current time at the start of the current upgrade item, e.g. plugin.
153 */
154 public static function record_start() {
155 self::$before = microtime(true);
156 self::$lastsavepoint = self::$before;
83740b9f 157 self::$isrecording = true;
03440bcb 158 }
159
160 /**
161 * Records current time at the end of a given numbered step.
162 *
163 * @param float $version Version number (may have decimals, or not)
164 */
165 public static function record_savepoint($version) {
166 global $CFG, $OUTPUT;
167
168 // In developer debug mode we show a notification after each individual save point.
83740b9f 169 if ($CFG->debugdeveloper && self::$isrecording) {
03440bcb 170 $time = microtime(true);
171
172 $notification = new \core\output\notification($version . ': ' .
173 get_string('successduration', '', format_float($time - self::$lastsavepoint, 2)),
174 \core\output\notification::NOTIFY_SUCCESS);
175 $notification->set_show_closebutton(false);
176 echo $OUTPUT->render($notification);
177 self::$lastsavepoint = $time;
178 }
179 }
180
181 /**
182 * Gets the time since the record_start function was called, rounded to 2 digits.
183 *
184 * @return float Elapsed time
185 */
186 public static function get_elapsed() {
187 return microtime(true) - self::$before;
188 }
189}
190
0c5406ca
PS
191/**
192 * Sets maximum expected time needed for upgrade task.
193 * Please always make sure that upgrade will not run longer!
194 *
195 * The script may be automatically aborted if upgrade times out.
196 *
197 * @category upgrade
198 * @param int $max_execution_time in seconds (can not be less than 60 s)
199 */
200function upgrade_set_timeout($max_execution_time=300) {
201 global $CFG;
202
203 if (!isset($CFG->upgraderunning) or $CFG->upgraderunning < time()) {
204 $upgraderunning = get_config(null, 'upgraderunning');
205 } else {
206 $upgraderunning = $CFG->upgraderunning;
207 }
208
209 if (!$upgraderunning) {
b814d1b4
PS
210 if (CLI_SCRIPT) {
211 // never stop CLI upgrades
212 $upgraderunning = 0;
213 } else {
214 // web upgrade not running or aborted
215 print_error('upgradetimedout', 'admin', "$CFG->wwwroot/$CFG->admin/");
216 }
0c5406ca
PS
217 }
218
219 if ($max_execution_time < 60) {
220 // protection against 0 here
221 $max_execution_time = 60;
222 }
223
224 $expected_end = time() + $max_execution_time;
225
226 if ($expected_end < $upgraderunning + 10 and $expected_end > $upgraderunning - 10) {
227 // no need to store new end, it is nearly the same ;-)
228 return;
229 }
230
5563f5fb
PS
231 if (CLI_SCRIPT) {
232 // there is no point in timing out of CLI scripts, admins can stop them if necessary
3ef7279f 233 core_php_time_limit::raise();
5563f5fb 234 } else {
3ef7279f 235 core_php_time_limit::raise($max_execution_time);
5563f5fb 236 }
0c5406ca
PS
237 set_config('upgraderunning', $expected_end); // keep upgrade locked until this time
238}
239
db9d4a3d 240/**
241 * Upgrade savepoint, marks end of each upgrade block.
242 * It stores new main version, resets upgrade timeout
243 * and abort upgrade if user cancels page loading.
244 *
245 * Please do not make large upgrade blocks with lots of operations,
246 * for example when adding tables keep only one table operation per block.
247 *
39b90b51 248 * @category upgrade
db9d4a3d 249 * @param bool $result false if upgrade step failed, true if completed
250 * @param string or float $version main version
251 * @param bool $allowabort allow user to abort script execution here
252 * @return void
253 */
254function upgrade_main_savepoint($result, $version, $allowabort=true) {
255 global $CFG;
256
bb44ef99
RT
257 //sanity check to avoid confusion with upgrade_mod_savepoint usage.
258 if (!is_bool($allowabort)) {
259 $errormessage = 'Parameter type mismatch. Are you mixing up upgrade_main_savepoint() and upgrade_mod_savepoint()?';
260 throw new coding_exception($errormessage);
261 }
262
795a08ad 263 if (!$result) {
520cea92 264 throw new upgrade_exception(null, $version);
795a08ad 265 }
266
267 if ($CFG->version >= $version) {
268 // something really wrong is going on in main upgrade script
269 throw new downgrade_exception(null, $CFG->version, $version);
db9d4a3d 270 }
271
795a08ad 272 set_config('version', $version);
273 upgrade_log(UPGRADE_LOG_NORMAL, null, 'Upgrade savepoint reached');
274
db9d4a3d 275 // reset upgrade timeout to default
276 upgrade_set_timeout();
277
03440bcb 278 core_upgrade_time::record_savepoint($version);
279
db9d4a3d 280 // this is a safe place to stop upgrades if user aborts page loading
281 if ($allowabort and connection_aborted()) {
282 die;
283 }
284}
285
286/**
287 * Module upgrade savepoint, marks end of module upgrade blocks
288 * It stores module version, resets upgrade timeout
289 * and abort upgrade if user cancels page loading.
290 *
39b90b51 291 * @category upgrade
db9d4a3d 292 * @param bool $result false if upgrade step failed, true if completed
293 * @param string or float $version main version
294 * @param string $modname name of module
295 * @param bool $allowabort allow user to abort script execution here
296 * @return void
297 */
298function upgrade_mod_savepoint($result, $version, $modname, $allowabort=true) {
299 global $DB;
300
bde002b8
PS
301 $component = 'mod_'.$modname;
302
795a08ad 303 if (!$result) {
bde002b8 304 throw new upgrade_exception($component, $version);
795a08ad 305 }
306
bde002b8
PS
307 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
308
db9d4a3d 309 if (!$module = $DB->get_record('modules', array('name'=>$modname))) {
310 print_error('modulenotexist', 'debug', '', $modname);
311 }
312
bde002b8 313 if ($dbversion >= $version) {
795a08ad 314 // something really wrong is going on in upgrade script
bde002b8 315 throw new downgrade_exception($component, $dbversion, $version);
db9d4a3d 316 }
bde002b8
PS
317 set_config('version', $version, $component);
318
319 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
db9d4a3d 320
321 // reset upgrade timeout to default
322 upgrade_set_timeout();
323
03440bcb 324 core_upgrade_time::record_savepoint($version);
325
db9d4a3d 326 // this is a safe place to stop upgrades if user aborts page loading
327 if ($allowabort and connection_aborted()) {
328 die;
329 }
330}
331
332/**
333 * Blocks upgrade savepoint, marks end of blocks upgrade blocks
334 * It stores block version, resets upgrade timeout
335 * and abort upgrade if user cancels page loading.
336 *
39b90b51 337 * @category upgrade
db9d4a3d 338 * @param bool $result false if upgrade step failed, true if completed
339 * @param string or float $version main version
340 * @param string $blockname name of block
341 * @param bool $allowabort allow user to abort script execution here
342 * @return void
343 */
795a08ad 344function upgrade_block_savepoint($result, $version, $blockname, $allowabort=true) {
db9d4a3d 345 global $DB;
346
bde002b8
PS
347 $component = 'block_'.$blockname;
348
795a08ad 349 if (!$result) {
bde002b8 350 throw new upgrade_exception($component, $version);
795a08ad 351 }
352
bde002b8
PS
353 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
354
db9d4a3d 355 if (!$block = $DB->get_record('block', array('name'=>$blockname))) {
356 print_error('blocknotexist', 'debug', '', $blockname);
357 }
358
bde002b8 359 if ($dbversion >= $version) {
795a08ad 360 // something really wrong is going on in upgrade script
bde002b8 361 throw new downgrade_exception($component, $dbversion, $version);
db9d4a3d 362 }
bde002b8
PS
363 set_config('version', $version, $component);
364
365 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
db9d4a3d 366
367 // reset upgrade timeout to default
368 upgrade_set_timeout();
369
03440bcb 370 core_upgrade_time::record_savepoint($version);
371
db9d4a3d 372 // this is a safe place to stop upgrades if user aborts page loading
373 if ($allowabort and connection_aborted()) {
374 die;
375 }
376}
377
378/**
379 * Plugins upgrade savepoint, marks end of blocks upgrade blocks
380 * It stores plugin version, resets upgrade timeout
381 * and abort upgrade if user cancels page loading.
382 *
39b90b51 383 * @category upgrade
db9d4a3d 384 * @param bool $result false if upgrade step failed, true if completed
385 * @param string or float $version main version
88d0f635
NR
386 * @param string $type The type of the plugin.
387 * @param string $plugin The name of the plugin.
db9d4a3d 388 * @param bool $allowabort allow user to abort script execution here
389 * @return void
390 */
17da2e6f 391function upgrade_plugin_savepoint($result, $version, $type, $plugin, $allowabort=true) {
bde002b8
PS
392 global $DB;
393
17da2e6f 394 $component = $type.'_'.$plugin;
395
795a08ad 396 if (!$result) {
17da2e6f 397 throw new upgrade_exception($component, $version);
db9d4a3d 398 }
399
bde002b8
PS
400 $dbversion = $DB->get_field('config_plugins', 'value', array('plugin'=>$component, 'name'=>'version'));
401
402 if ($dbversion >= $version) {
795a08ad 403 // Something really wrong is going on in the upgrade script
bde002b8 404 throw new downgrade_exception($component, $dbversion, $version);
795a08ad 405 }
17da2e6f 406 set_config('version', $version, $component);
795a08ad 407 upgrade_log(UPGRADE_LOG_NORMAL, $component, 'Upgrade savepoint reached');
408
db9d4a3d 409 // Reset upgrade timeout to default
410 upgrade_set_timeout();
411
03440bcb 412 core_upgrade_time::record_savepoint($version);
413
db9d4a3d 414 // This is a safe place to stop upgrades if user aborts page loading
415 if ($allowabort and connection_aborted()) {
416 die;
417 }
418}
419
9008ec16
PS
420/**
421 * Detect if there are leftovers in PHP source files.
422 *
423 * During main version upgrades administrators MUST move away
424 * old PHP source files and start from scratch (or better
425 * use git).
426 *
427 * @return bool true means borked upgrade, false means previous PHP files were properly removed
428 */
429function upgrade_stale_php_files_present() {
430 global $CFG;
431
432 $someexamplesofremovedfiles = array(
d1e051d0
EL
433 // Removed in 3.8.
434 '/lib/amd/src/modal_confirm.js',
435 '/lib/fonts/font-awesome-4.7.0/css/font-awesome.css',
436 '/lib/jquery/jquery-3.2.1.min.js',
437 '/lib/recaptchalib.php',
438 '/lib/sessionkeepalive_ajax.php',
439 '/lib/yui/src/checknet/js/checknet.js',
440 '/question/amd/src/qbankmanager.js',
09b97f40
EL
441 // Removed in 3.7.
442 '/lib/form/yui/src/showadvanced/js/showadvanced.js',
443 '/lib/tests/output_external_test.php',
444 '/message/amd/src/message_area.js',
445 '/message/templates/message_area.mustache',
446 '/question/yui/src/qbankmanager/build.json',
512a1d31 447 // Removed in 3.6.
b08cdad2
EL
448 '/lib/classes/session/memcache.php',
449 '/lib/eventslib.php',
450 '/lib/form/submitlink.php',
d77b5db5 451 '/lib/medialib.php',
512a1d31 452 '/lib/password_compat/lib/password.php',
970df71d
EL
453 // Removed in 3.5.
454 '/lib/dml/mssql_native_moodle_database.php',
455 '/lib/dml/mssql_native_moodle_recordset.php',
456 '/lib/dml/mssql_native_moodle_temptables.php',
1ae99ec3
EL
457 // Removed in 3.4.
458 '/auth/README.txt',
459 '/calendar/set.php',
460 '/enrol/users.php',
461 '/enrol/yui/rolemanager/assets/skins/sam/rolemanager.css',
fea4557c
EL
462 // Removed in 3.3.
463 '/badges/backpackconnect.php',
464 '/calendar/yui/src/info/assets/skins/sam/moodle-calendar-info.css',
465 '/competency/classes/external/exporter.php',
466 '/mod/forum/forum.js',
467 '/user/pixgroup.php',
85ef6fe2
EL
468 // Removed in 3.2.
469 '/calendar/preferences.php',
470 '/lib/alfresco/',
471 '/lib/jquery/jquery-1.12.1.min.js',
472 '/lib/password_compat/tests/',
473 '/lib/phpunit/classes/unittestcase.php',
bbd7f7f7 474 // Removed in 3.1.
f8b059f2
EL
475 '/lib/classes/log/sql_internal_reader.php',
476 '/lib/zend/',
477 '/mod/forum/pix/icon.gif',
478 '/tag/templates/tagname.mustache',
5222ab7f
SC
479 // Removed in 3.0.
480 '/mod/lti/grade.php',
0d1e5456 481 '/tag/coursetagslib.php',
aac67170
AG
482 // Removed in 2.9.
483 '/lib/timezone.txt',
484 // Removed in 2.8.
485 '/course/delete_category_form.php',
4e8a07d2
TH
486 // Removed in 2.7.
487 '/admin/tool/qeupgradehelper/version.php',
488 // Removed in 2.6.
bde002b8 489 '/admin/block.php',
72e3a3c9 490 '/admin/oacleanup.php',
4e8a07d2 491 // Removed in 2.5.
c1782ec6
EL
492 '/backup/lib.php',
493 '/backup/bb/README.txt',
301ebbe5 494 '/lib/excel/test.php',
4e8a07d2 495 // Removed in 2.4.
c8b3346c 496 '/admin/tool/unittest/simpletestlib.php',
4e8a07d2 497 // Removed in 2.3.
37d72977 498 '/lib/minify/builder/',
4e8a07d2 499 // Removed in 2.2.
9008ec16 500 '/lib/yui/3.4.1pr1/',
4e8a07d2 501 // Removed in 2.2.
9008ec16
PS
502 '/search/cron_php5.php',
503 '/course/report/log/indexlive.php',
504 '/admin/report/backups/index.php',
505 '/admin/generator.php',
4e8a07d2 506 // Removed in 2.1.
9008ec16 507 '/lib/yui/2.8.0r4/',
4e8a07d2 508 // Removed in 2.0.
9008ec16
PS
509 '/blocks/admin/block_admin.php',
510 '/blocks/admin_tree/block_admin_tree.php',
511 );
512
513 foreach ($someexamplesofremovedfiles as $file) {
514 if (file_exists($CFG->dirroot.$file)) {
515 return true;
516 }
517 }
518
519 return false;
520}
db9d4a3d 521
522/**
523 * Upgrade plugins
db9d4a3d 524 * @param string $type The type of plugins that should be updated (e.g. 'enrol', 'qtype')
17da2e6f 525 * return void
db9d4a3d 526 */
17da2e6f 527function upgrade_plugins($type, $startcallback, $endcallback, $verbose) {
db9d4a3d 528 global $CFG, $DB;
529
530/// special cases
531 if ($type === 'mod') {
194cdca8 532 return upgrade_plugins_modules($startcallback, $endcallback, $verbose);
795a08ad 533 } else if ($type === 'block') {
194cdca8 534 return upgrade_plugins_blocks($startcallback, $endcallback, $verbose);
db9d4a3d 535 }
536
bd3b3bba 537 $plugs = core_component::get_plugin_list($type);
db9d4a3d 538
17da2e6f 539 foreach ($plugs as $plug=>$fullplug) {
ebb98bf5 540 // Reset time so that it works when installing a large number of plugins
3ef7279f 541 core_php_time_limit::raise(600);
aff24313 542 $component = clean_param($type.'_'.$plug, PARAM_COMPONENT); // standardised plugin name
db9d4a3d 543
3e858ea7 544 // check plugin dir is valid name
aff24313
PS
545 if (empty($component)) {
546 throw new plugin_defective_exception($type.'_'.$plug, 'Invalid plugin directory name.');
3e858ea7
PS
547 }
548
795a08ad 549 if (!is_readable($fullplug.'/version.php')) {
550 continue;
db9d4a3d 551 }
552
365a5941 553 $plugin = new stdClass();
bde002b8
PS
554 $plugin->version = null;
555 $module = $plugin; // Prevent some notices when plugin placed in wrong directory.
795a08ad 556 require($fullplug.'/version.php'); // defines $plugin with version etc
bde002b8 557 unset($module);
bdbcb6d7 558
98ea6973
DM
559 if (empty($plugin->version)) {
560 throw new plugin_defective_exception($component, 'Missing $plugin->version number in version.php.');
3e858ea7
PS
561 }
562
98ea6973
DM
563 if (empty($plugin->component)) {
564 throw new plugin_defective_exception($component, 'Missing $plugin->component declaration in version.php.');
565 }
566
567 if ($plugin->component !== $component) {
568 throw new plugin_misplaced_exception($plugin->component, null, $fullplug);
db9d4a3d 569 }
570
17da2e6f 571 $plugin->name = $plug;
572 $plugin->fullname = $component;
795a08ad 573
db9d4a3d 574 if (!empty($plugin->requires)) {
575 if ($plugin->requires > $CFG->version) {
795a08ad 576 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
ef14c1e7
PS
577 } else if ($plugin->requires < 2010000000) {
578 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
db9d4a3d 579 }
580 }
581
9b683d13 582 // try to recover from interrupted install.php if needed
583 if (file_exists($fullplug.'/db/install.php')) {
584 if (get_config($plugin->fullname, 'installrunning')) {
585 require_once($fullplug.'/db/install.php');
586 $recover_install_function = 'xmldb_'.$plugin->fullname.'_install_recovery';
587 if (function_exists($recover_install_function)) {
588 $startcallback($component, true, $verbose);
589 $recover_install_function();
f6f06d69 590 unset_config('installrunning', $plugin->fullname);
9b683d13 591 update_capabilities($component);
c6d75bff 592 log_update_descriptions($component);
c976e271 593 external_update_descriptions($component);
309ae892 594 \core\task\manager::reset_scheduled_tasks_for_component($component);
606c3be0 595 \core_analytics\manager::update_default_models_for_component($component);
9b683d13 596 message_update_providers($component);
6c0bfb14 597 \core\message\inbound\manager::update_handlers_for_component($component);
afed8d0f
RK
598 if ($type === 'message') {
599 message_update_processors($plug);
600 }
de260e0f 601 upgrade_plugin_mnet_functions($component);
c026a28d 602 core_tag_area::reset_definitions_for_component($component);
9b683d13 603 $endcallback($component, true, $verbose);
604 }
605 }
606 }
db9d4a3d 607
bde002b8 608 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
795a08ad 609 if (empty($installedversion)) { // new installation
194cdca8 610 $startcallback($component, true, $verbose);
db9d4a3d 611
795a08ad 612 /// Install tables if defined
613 if (file_exists($fullplug.'/db/install.xml')) {
614 $DB->get_manager()->install_from_xmldb_file($fullplug.'/db/install.xml');
db9d4a3d 615 }
9b683d13 616
617 /// store version
618 upgrade_plugin_savepoint(true, $plugin->version, $type, $plug, false);
619
795a08ad 620 /// execute post install file
621 if (file_exists($fullplug.'/db/install.php')) {
622 require_once($fullplug.'/db/install.php');
f6f06d69
PS
623 set_config('installrunning', 1, $plugin->fullname);
624 $post_install_function = 'xmldb_'.$plugin->fullname.'_install';
795a08ad 625 $post_install_function();
f6f06d69 626 unset_config('installrunning', $plugin->fullname);
795a08ad 627 }
628
795a08ad 629 /// Install various components
630 update_capabilities($component);
c6d75bff 631 log_update_descriptions($component);
c976e271 632 external_update_descriptions($component);
309ae892 633 \core\task\manager::reset_scheduled_tasks_for_component($component);
606c3be0 634 \core_analytics\manager::update_default_models_for_component($component);
795a08ad 635 message_update_providers($component);
6c0bfb14 636 \core\message\inbound\manager::update_handlers_for_component($component);
afed8d0f
RK
637 if ($type === 'message') {
638 message_update_processors($plug);
639 }
de260e0f 640 upgrade_plugin_mnet_functions($component);
c026a28d 641 core_tag_area::reset_definitions_for_component($component);
194cdca8 642 $endcallback($component, true, $verbose);
795a08ad 643
644 } else if ($installedversion < $plugin->version) { // upgrade
645 /// Run the upgrade function for the plugin.
194cdca8 646 $startcallback($component, false, $verbose);
795a08ad 647
648 if (is_readable($fullplug.'/db/upgrade.php')) {
649 require_once($fullplug.'/db/upgrade.php'); // defines upgrading function
650
651 $newupgrade_function = 'xmldb_'.$plugin->fullname.'_upgrade';
652 $result = $newupgrade_function($installedversion);
653 } else {
654 $result = true;
655 }
656
bde002b8 657 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
795a08ad 658 if ($installedversion < $plugin->version) {
659 // store version if not already there
660 upgrade_plugin_savepoint($result, $plugin->version, $type, $plug, false);
661 }
662
663 /// Upgrade various components
664 update_capabilities($component);
c6d75bff 665 log_update_descriptions($component);
c976e271 666 external_update_descriptions($component);
309ae892 667 \core\task\manager::reset_scheduled_tasks_for_component($component);
606c3be0 668 \core_analytics\manager::update_default_models_for_component($component);
795a08ad 669 message_update_providers($component);
6c0bfb14 670 \core\message\inbound\manager::update_handlers_for_component($component);
afed8d0f 671 if ($type === 'message') {
bde002b8 672 // Ugly hack!
afed8d0f
RK
673 message_update_processors($plug);
674 }
de260e0f 675 upgrade_plugin_mnet_functions($component);
c026a28d 676 core_tag_area::reset_definitions_for_component($component);
194cdca8 677 $endcallback($component, false, $verbose);
795a08ad 678
679 } else if ($installedversion > $plugin->version) {
680 throw new downgrade_exception($component, $installedversion, $plugin->version);
db9d4a3d 681 }
682 }
db9d4a3d 683}
684
685/**
686 * Find and check all modules and load them up or upgrade them if necessary
72fb21b6 687 *
688 * @global object
689 * @global object
db9d4a3d 690 */
194cdca8 691function upgrade_plugins_modules($startcallback, $endcallback, $verbose) {
db9d4a3d 692 global $CFG, $DB;
693
bd3b3bba 694 $mods = core_component::get_plugin_list('mod');
db9d4a3d 695
17da2e6f 696 foreach ($mods as $mod=>$fullmod) {
db9d4a3d 697
3e858ea7 698 if ($mod === 'NEWMODULE') { // Someone has unzipped the template, ignore it
db9d4a3d 699 continue;
700 }
701
aff24313 702 $component = clean_param('mod_'.$mod, PARAM_COMPONENT);
db9d4a3d 703
3e858ea7 704 // check module dir is valid name
aff24313
PS
705 if (empty($component)) {
706 throw new plugin_defective_exception('mod_'.$mod, 'Invalid plugin directory name.');
3e858ea7
PS
707 }
708
795a08ad 709 if (!is_readable($fullmod.'/version.php')) {
710 throw new plugin_defective_exception($component, 'Missing version.php');
db9d4a3d 711 }
712
01889f01 713 $module = new stdClass();
bde002b8
PS
714 $plugin = new stdClass();
715 $plugin->version = null;
994e5662 716 require($fullmod .'/version.php'); // Defines $plugin with version etc.
01889f01
DM
717
718 // Check if the legacy $module syntax is still used.
17d2a336 719 if (!is_object($module) or (count((array)$module) > 0)) {
01889f01
DM
720 throw new plugin_defective_exception($component, 'Unsupported $module syntax detected in version.php');
721 }
722
723 // Prepare the record for the {modules} table.
724 $module = clone($plugin);
bde002b8
PS
725 unset($module->version);
726 unset($module->component);
727 unset($module->dependencies);
728 unset($module->release);
bdbcb6d7 729
98ea6973
DM
730 if (empty($plugin->version)) {
731 throw new plugin_defective_exception($component, 'Missing $plugin->version number in version.php.');
3e858ea7
PS
732 }
733
98ea6973
DM
734 if (empty($plugin->component)) {
735 throw new plugin_defective_exception($component, 'Missing $plugin->component declaration in version.php.');
736 }
737
738 if ($plugin->component !== $component) {
739 throw new plugin_misplaced_exception($plugin->component, null, $fullmod);
db9d4a3d 740 }
741
bde002b8
PS
742 if (!empty($plugin->requires)) {
743 if ($plugin->requires > $CFG->version) {
744 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
745 } else if ($plugin->requires < 2010000000) {
ef14c1e7 746 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
db9d4a3d 747 }
748 }
749
ff05bb31
TH
750 if (empty($module->cron)) {
751 $module->cron = 0;
752 }
753
3e858ea7
PS
754 // all modules must have en lang pack
755 if (!is_readable("$fullmod/lang/en/$mod.php")) {
756 throw new plugin_defective_exception($component, 'Missing mandatory en language pack.');
757 }
758
db9d4a3d 759 $module->name = $mod; // The name MUST match the directory
760
bde002b8 761 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
db9d4a3d 762
9b683d13 763 if (file_exists($fullmod.'/db/install.php')) {
764 if (get_config($module->name, 'installrunning')) {
765 require_once($fullmod.'/db/install.php');
766 $recover_install_function = 'xmldb_'.$module->name.'_install_recovery';
767 if (function_exists($recover_install_function)) {
768 $startcallback($component, true, $verbose);
769 $recover_install_function();
770 unset_config('installrunning', $module->name);
771 // Install various components too
772 update_capabilities($component);
c6d75bff 773 log_update_descriptions($component);
c976e271 774 external_update_descriptions($component);
309ae892 775 \core\task\manager::reset_scheduled_tasks_for_component($component);
606c3be0 776 \core_analytics\manager::update_default_models_for_component($component);
9b683d13 777 message_update_providers($component);
6c0bfb14 778 \core\message\inbound\manager::update_handlers_for_component($component);
de260e0f 779 upgrade_plugin_mnet_functions($component);
c026a28d 780 core_tag_area::reset_definitions_for_component($component);
9b683d13 781 $endcallback($component, true, $verbose);
782 }
783 }
784 }
785
bde002b8 786 if (empty($installedversion)) {
194cdca8 787 $startcallback($component, true, $verbose);
db9d4a3d 788
795a08ad 789 /// Execute install.xml (XMLDB) - must be present in all modules
790 $DB->get_manager()->install_from_xmldb_file($fullmod.'/db/install.xml');
db9d4a3d 791
2e3da297 792 /// Add record into modules table - may be needed in install.php already
793 $module->id = $DB->insert_record('modules', $module);
bde002b8 794 upgrade_mod_savepoint(true, $plugin->version, $module->name, false);
2e3da297 795
db9d4a3d 796 /// Post installation hook - optional
797 if (file_exists("$fullmod/db/install.php")) {
798 require_once("$fullmod/db/install.php");
9b683d13 799 // Set installation running flag, we need to recover after exception or error
800 set_config('installrunning', 1, $module->name);
3412cb43 801 $post_install_function = 'xmldb_'.$module->name.'_install';
db9d4a3d 802 $post_install_function();
9b683d13 803 unset_config('installrunning', $module->name);
db9d4a3d 804 }
805
795a08ad 806 /// Install various components
807 update_capabilities($component);
c6d75bff 808 log_update_descriptions($component);
c976e271 809 external_update_descriptions($component);
309ae892 810 \core\task\manager::reset_scheduled_tasks_for_component($component);
606c3be0 811 \core_analytics\manager::update_default_models_for_component($component);
795a08ad 812 message_update_providers($component);
6c0bfb14 813 \core\message\inbound\manager::update_handlers_for_component($component);
de260e0f 814 upgrade_plugin_mnet_functions($component);
c026a28d 815 core_tag_area::reset_definitions_for_component($component);
795a08ad 816
194cdca8 817 $endcallback($component, true, $verbose);
db9d4a3d 818
bde002b8 819 } else if ($installedversion < $plugin->version) {
795a08ad 820 /// If versions say that we need to upgrade but no upgrade files are available, notify and continue
194cdca8 821 $startcallback($component, false, $verbose);
795a08ad 822
823 if (is_readable($fullmod.'/db/upgrade.php')) {
824 require_once($fullmod.'/db/upgrade.php'); // defines new upgrading function
825 $newupgrade_function = 'xmldb_'.$module->name.'_upgrade';
bde002b8 826 $result = $newupgrade_function($installedversion, $module);
795a08ad 827 } else {
828 $result = true;
829 }
830
bde002b8 831 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
795a08ad 832 $currmodule = $DB->get_record('modules', array('name'=>$module->name));
bde002b8 833 if ($installedversion < $plugin->version) {
795a08ad 834 // store version if not already there
bde002b8 835 upgrade_mod_savepoint($result, $plugin->version, $mod, false);
795a08ad 836 }
837
3412cb43
TH
838 // update cron flag if needed
839 if ($currmodule->cron != $module->cron) {
840 $DB->set_field('modules', 'cron', $module->cron, array('name' => $module->name));
841 }
842
843 // Upgrade various components
795a08ad 844 update_capabilities($component);
c6d75bff 845 log_update_descriptions($component);
c976e271 846 external_update_descriptions($component);
309ae892 847 \core\task\manager::reset_scheduled_tasks_for_component($component);
606c3be0 848 \core_analytics\manager::update_default_models_for_component($component);
795a08ad 849 message_update_providers($component);
6c0bfb14 850 \core\message\inbound\manager::update_handlers_for_component($component);
de260e0f 851 upgrade_plugin_mnet_functions($component);
c026a28d 852 core_tag_area::reset_definitions_for_component($component);
795a08ad 853
194cdca8 854 $endcallback($component, false, $verbose);
795a08ad 855
bde002b8
PS
856 } else if ($installedversion > $plugin->version) {
857 throw new downgrade_exception($component, $installedversion, $plugin->version);
795a08ad 858 }
859 }
860}
db9d4a3d 861
db9d4a3d 862
795a08ad 863/**
864 * This function finds all available blocks and install them
865 * into blocks table or do all the upgrade process if newer.
72fb21b6 866 *
867 * @global object
868 * @global object
795a08ad 869 */
194cdca8 870function upgrade_plugins_blocks($startcallback, $endcallback, $verbose) {
795a08ad 871 global $CFG, $DB;
872
795a08ad 873 require_once($CFG->dirroot.'/blocks/moodleblock.class.php');
874
875 $blocktitles = array(); // we do not want duplicate titles
876
877 //Is this a first install
878 $first_install = null;
879
bd3b3bba 880 $blocks = core_component::get_plugin_list('block');
795a08ad 881
17da2e6f 882 foreach ($blocks as $blockname=>$fullblock) {
795a08ad 883
884 if (is_null($first_install)) {
f46eba7e 885 $first_install = ($DB->count_records('block_instances') == 0);
795a08ad 886 }
887
aff24313 888 if ($blockname === 'NEWBLOCK') { // Someone has unzipped the template, ignore it
795a08ad 889 continue;
db9d4a3d 890 }
891
aff24313 892 $component = clean_param('block_'.$blockname, PARAM_COMPONENT);
db9d4a3d 893
3e858ea7 894 // check block dir is valid name
aff24313
PS
895 if (empty($component)) {
896 throw new plugin_defective_exception('block_'.$blockname, 'Invalid plugin directory name.');
3e858ea7
PS
897 }
898
8571833f
PS
899 if (!is_readable($fullblock.'/version.php')) {
900 throw new plugin_defective_exception('block/'.$blockname, 'Missing version.php file.');
901 }
365a5941 902 $plugin = new stdClass();
bde002b8 903 $plugin->version = null;
8571833f 904 $plugin->cron = 0;
bde002b8 905 $module = $plugin; // Prevent some notices when module placed in wrong directory.
8571833f 906 include($fullblock.'/version.php');
bde002b8
PS
907 unset($module);
908 $block = clone($plugin);
909 unset($block->version);
910 unset($block->component);
911 unset($block->dependencies);
912 unset($block->release);
8571833f 913
98ea6973
DM
914 if (empty($plugin->version)) {
915 throw new plugin_defective_exception($component, 'Missing block version number in version.php.');
3e858ea7
PS
916 }
917
98ea6973
DM
918 if (empty($plugin->component)) {
919 throw new plugin_defective_exception($component, 'Missing $plugin->component declaration in version.php.');
920 }
921
922 if ($plugin->component !== $component) {
923 throw new plugin_misplaced_exception($plugin->component, null, $fullblock);
bde002b8
PS
924 }
925
ef14c1e7
PS
926 if (!empty($plugin->requires)) {
927 if ($plugin->requires > $CFG->version) {
928 throw new upgrade_requires_exception($component, $plugin->version, $CFG->version, $plugin->requires);
929 } else if ($plugin->requires < 2010000000) {
930 throw new plugin_defective_exception($component, 'Plugin is not compatible with Moodle 2.x or later.');
931 }
932 }
933
795a08ad 934 if (!is_readable($fullblock.'/block_'.$blockname.'.php')) {
935 throw new plugin_defective_exception('block/'.$blockname, 'Missing main block class file.');
db9d4a3d 936 }
8571833f 937 include_once($fullblock.'/block_'.$blockname.'.php');
db9d4a3d 938
795a08ad 939 $classname = 'block_'.$blockname;
940
941 if (!class_exists($classname)) {
942 throw new plugin_defective_exception($component, 'Can not load main class.');
943 }
944
b921b6ee 945 $blockobj = new $classname; // This is what we'll be testing
795a08ad 946 $blocktitle = $blockobj->get_title();
947
948 // OK, it's as we all hoped. For further tests, the object will do them itself.
949 if (!$blockobj->_self_test()) {
950 throw new plugin_defective_exception($component, 'Self test failed.');
951 }
952
795a08ad 953 $block->name = $blockname; // The name MUST match the directory
795a08ad 954
bde002b8 955 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
795a08ad 956
9b683d13 957 if (file_exists($fullblock.'/db/install.php')) {
958 if (get_config('block_'.$blockname, 'installrunning')) {
959 require_once($fullblock.'/db/install.php');
960 $recover_install_function = 'xmldb_block_'.$blockname.'_install_recovery';
961 if (function_exists($recover_install_function)) {
962 $startcallback($component, true, $verbose);
963 $recover_install_function();
964 unset_config('installrunning', 'block_'.$blockname);
965 // Install various components
966 update_capabilities($component);
c6d75bff 967 log_update_descriptions($component);
c976e271 968 external_update_descriptions($component);
309ae892 969 \core\task\manager::reset_scheduled_tasks_for_component($component);
606c3be0 970 \core_analytics\manager::update_default_models_for_component($component);
9b683d13 971 message_update_providers($component);
6c0bfb14 972 \core\message\inbound\manager::update_handlers_for_component($component);
de260e0f 973 upgrade_plugin_mnet_functions($component);
c026a28d 974 core_tag_area::reset_definitions_for_component($component);
9b683d13 975 $endcallback($component, true, $verbose);
976 }
977 }
978 }
979
bde002b8 980 if (empty($installedversion)) { // block not installed yet, so install it
795a08ad 981 $conflictblock = array_search($blocktitle, $blocktitles);
982 if ($conflictblock !== false) {
983 // Duplicate block titles are not allowed, they confuse people
984 // AND PHP's associative arrays ;)
c1ddac83 985 throw new plugin_defective_exception($component, get_string('blocknameconflict', 'error', (object)array('name'=>$block->name, 'conflict'=>$conflictblock)));
795a08ad 986 }
194cdca8 987 $startcallback($component, true, $verbose);
795a08ad 988
989 if (file_exists($fullblock.'/db/install.xml')) {
990 $DB->get_manager()->install_from_xmldb_file($fullblock.'/db/install.xml');
991 }
992 $block->id = $DB->insert_record('block', $block);
bde002b8 993 upgrade_block_savepoint(true, $plugin->version, $block->name, false);
795a08ad 994
995 if (file_exists($fullblock.'/db/install.php')) {
996 require_once($fullblock.'/db/install.php');
9b683d13 997 // Set installation running flag, we need to recover after exception or error
998 set_config('installrunning', 1, 'block_'.$blockname);
0e35ba6f 999 $post_install_function = 'xmldb_block_'.$blockname.'_install';
795a08ad 1000 $post_install_function();
9b683d13 1001 unset_config('installrunning', 'block_'.$blockname);
795a08ad 1002 }
1003
1004 $blocktitles[$block->name] = $blocktitle;
1005
1006 // Install various components
1007 update_capabilities($component);
c6d75bff 1008 log_update_descriptions($component);
c976e271 1009 external_update_descriptions($component);
309ae892 1010 \core\task\manager::reset_scheduled_tasks_for_component($component);
606c3be0 1011 \core_analytics\manager::update_default_models_for_component($component);
795a08ad 1012 message_update_providers($component);
6c0bfb14 1013 \core\message\inbound\manager::update_handlers_for_component($component);
c026a28d 1014 core_tag_area::reset_definitions_for_component($component);
de260e0f 1015 upgrade_plugin_mnet_functions($component);
795a08ad 1016
194cdca8 1017 $endcallback($component, true, $verbose);
795a08ad 1018
bde002b8 1019 } else if ($installedversion < $plugin->version) {
194cdca8 1020 $startcallback($component, false, $verbose);
795a08ad 1021
1022 if (is_readable($fullblock.'/db/upgrade.php')) {
1023 require_once($fullblock.'/db/upgrade.php'); // defines new upgrading function
1024 $newupgrade_function = 'xmldb_block_'.$blockname.'_upgrade';
bde002b8 1025 $result = $newupgrade_function($installedversion, $block);
795a08ad 1026 } else {
1027 $result = true;
1028 }
1029
bde002b8 1030 $installedversion = $DB->get_field('config_plugins', 'value', array('name'=>'version', 'plugin'=>$component)); // No caching!
795a08ad 1031 $currblock = $DB->get_record('block', array('name'=>$block->name));
bde002b8 1032 if ($installedversion < $plugin->version) {
795a08ad 1033 // store version if not already there
bde002b8 1034 upgrade_block_savepoint($result, $plugin->version, $block->name, false);
795a08ad 1035 }
1036
1037 if ($currblock->cron != $block->cron) {
1038 // update cron flag if needed
1fa84543 1039 $DB->set_field('block', 'cron', $block->cron, array('id' => $currblock->id));
795a08ad 1040 }
1041
b921b6ee 1042 // Upgrade various components
795a08ad 1043 update_capabilities($component);
c6d75bff 1044 log_update_descriptions($component);
c976e271 1045 external_update_descriptions($component);
309ae892 1046 \core\task\manager::reset_scheduled_tasks_for_component($component);
606c3be0 1047 \core_analytics\manager::update_default_models_for_component($component);
795a08ad 1048 message_update_providers($component);
6c0bfb14 1049 \core\message\inbound\manager::update_handlers_for_component($component);
de260e0f 1050 upgrade_plugin_mnet_functions($component);
c026a28d 1051 core_tag_area::reset_definitions_for_component($component);
795a08ad 1052
194cdca8 1053 $endcallback($component, false, $verbose);
795a08ad 1054
bde002b8
PS
1055 } else if ($installedversion > $plugin->version) {
1056 throw new downgrade_exception($component, $installedversion, $plugin->version);
795a08ad 1057 }
1058 }
1059
1060
1061 // Finally, if we are in the first_install of BLOCKS setup frontpage and admin page blocks
1062 if ($first_install) {
795a08ad 1063 //Iterate over each course - there should be only site course here now
1064 if ($courses = $DB->get_records('course')) {
1065 foreach ($courses as $course) {
9d1d606e 1066 blocks_add_default_course_blocks($course);
db9d4a3d 1067 }
1068 }
795a08ad 1069
9d1d606e 1070 blocks_add_default_system_blocks();
795a08ad 1071 }
1072}
1073
c6d75bff
PS
1074
1075/**
1076 * Log_display description function used during install and upgrade.
1077 *
1078 * @param string $component name of component (moodle, mod_assignment, etc.)
1079 * @return void
1080 */
1081function log_update_descriptions($component) {
1082 global $DB;
1083
b0d1d941 1084 $defpath = core_component::get_component_directory($component).'/db/log.php';
c6d75bff
PS
1085
1086 if (!file_exists($defpath)) {
1087 $DB->delete_records('log_display', array('component'=>$component));
1088 return;
1089 }
1090
1091 // load new info
1092 $logs = array();
1093 include($defpath);
1094 $newlogs = array();
1095 foreach ($logs as $log) {
1096 $newlogs[$log['module'].'-'.$log['action']] = $log; // kind of unique name
1097 }
1098 unset($logs);
1099 $logs = $newlogs;
1100
1101 $fields = array('module', 'action', 'mtable', 'field');
1102 // update all log fist
1103 $dblogs = $DB->get_records('log_display', array('component'=>$component));
1104 foreach ($dblogs as $dblog) {
1105 $name = $dblog->module.'-'.$dblog->action;
1106
1107 if (empty($logs[$name])) {
1108 $DB->delete_records('log_display', array('id'=>$dblog->id));
1109 continue;
1110 }
1111
1112 $log = $logs[$name];
1113 unset($logs[$name]);
1114
1115 $update = false;
1116 foreach ($fields as $field) {
1117 if ($dblog->$field != $log[$field]) {
1118 $dblog->$field = $log[$field];
1119 $update = true;
1120 }
1121 }
1122 if ($update) {
1123 $DB->update_record('log_display', $dblog);
1124 }
1125 }
1126 foreach ($logs as $log) {
1127 $dblog = (object)$log;
1128 $dblog->component = $component;
1129 $DB->insert_record('log_display', $dblog);
1130 }
1131}
1132
c976e271 1133/**
1134 * Web service discovery function used during install and upgrade.
1135 * @param string $component name of component (moodle, mod_assignment, etc.)
1136 * @return void
1137 */
1138function external_update_descriptions($component) {
bc81eadb 1139 global $DB, $CFG;
c976e271 1140
b0d1d941 1141 $defpath = core_component::get_component_directory($component).'/db/services.php';
c976e271 1142
1143 if (!file_exists($defpath)) {
bc81eadb 1144 require_once($CFG->dirroot.'/lib/externallib.php');
c976e271 1145 external_delete_descriptions($component);
1146 return;
1147 }
1148
1149 // load new info
1150 $functions = array();
1151 $services = array();
1152 include($defpath);
1153
1154 // update all function fist
1155 $dbfunctions = $DB->get_records('external_functions', array('component'=>$component));
1156 foreach ($dbfunctions as $dbfunction) {
1157 if (empty($functions[$dbfunction->name])) {
1158 $DB->delete_records('external_functions', array('id'=>$dbfunction->id));
1159 // do not delete functions from external_services_functions, beacuse
1160 // we want to notify admins when functions used in custom services disappear
c6d75bff
PS
1161
1162 //TODO: this looks wrong, we have to delete it eventually (skodak)
c976e271 1163 continue;
1164 }
1165
1166 $function = $functions[$dbfunction->name];
1167 unset($functions[$dbfunction->name]);
1168 $function['classpath'] = empty($function['classpath']) ? null : $function['classpath'];
1169
1170 $update = false;
1171 if ($dbfunction->classname != $function['classname']) {
1172 $dbfunction->classname = $function['classname'];
1173 $update = true;
1174 }
1175 if ($dbfunction->methodname != $function['methodname']) {
1176 $dbfunction->methodname = $function['methodname'];
1177 $update = true;
1178 }
1179 if ($dbfunction->classpath != $function['classpath']) {
1180 $dbfunction->classpath = $function['classpath'];
1181 $update = true;
1182 }
12fc8acf 1183 $functioncapabilities = array_key_exists('capabilities', $function)?$function['capabilities']:'';
72f68b51 1184 if ($dbfunction->capabilities != $functioncapabilities) {
1185 $dbfunction->capabilities = $functioncapabilities;
1186 $update = true;
1187 }
186eba1b
JL
1188
1189 if (isset($function['services']) and is_array($function['services'])) {
1190 sort($function['services']);
1191 $functionservices = implode(',', $function['services']);
1192 } else {
1193 // Force null values in the DB.
1194 $functionservices = null;
1195 }
1196
1197 if ($dbfunction->services != $functionservices) {
1198 // Now, we need to check if services were removed, in that case we need to remove the function from them.
1199 $servicesremoved = array_diff(explode(",", $dbfunction->services), explode(",", $functionservices));
1200 foreach ($servicesremoved as $removedshortname) {
1201 if ($externalserviceid = $DB->get_field('external_services', 'id', array("shortname" => $removedshortname))) {
1202 $DB->delete_records('external_services_functions', array('functionname' => $dbfunction->name,
1203 'externalserviceid' => $externalserviceid));
1204 }
1205 }
1206
1207 $dbfunction->services = $functionservices;
1208 $update = true;
1209 }
c976e271 1210 if ($update) {
1211 $DB->update_record('external_functions', $dbfunction);
1212 }
1213 }
1214 foreach ($functions as $fname => $function) {
365a5941 1215 $dbfunction = new stdClass();
c976e271 1216 $dbfunction->name = $fname;
1217 $dbfunction->classname = $function['classname'];
1218 $dbfunction->methodname = $function['methodname'];
1219 $dbfunction->classpath = empty($function['classpath']) ? null : $function['classpath'];
1220 $dbfunction->component = $component;
12fc8acf 1221 $dbfunction->capabilities = array_key_exists('capabilities', $function)?$function['capabilities']:'';
186eba1b
JL
1222
1223 if (isset($function['services']) and is_array($function['services'])) {
1224 sort($function['services']);
1225 $dbfunction->services = implode(',', $function['services']);
1226 } else {
1227 // Force null values in the DB.
1228 $dbfunction->services = null;
1229 }
1230
c976e271 1231 $dbfunction->id = $DB->insert_record('external_functions', $dbfunction);
1232 }
1233 unset($functions);
1234
1235 // now deal with services
1236 $dbservices = $DB->get_records('external_services', array('component'=>$component));
1237 foreach ($dbservices as $dbservice) {
1238 if (empty($services[$dbservice->name])) {
bc81eadb 1239 $DB->delete_records('external_tokens', array('externalserviceid'=>$dbservice->id));
c976e271 1240 $DB->delete_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
1241 $DB->delete_records('external_services_users', array('externalserviceid'=>$dbservice->id));
1242 $DB->delete_records('external_services', array('id'=>$dbservice->id));
1243 continue;
1244 }
1245 $service = $services[$dbservice->name];
1246 unset($services[$dbservice->name]);
1247 $service['enabled'] = empty($service['enabled']) ? 0 : $service['enabled'];
1248 $service['requiredcapability'] = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1249 $service['restrictedusers'] = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
af03513f 1250 $service['downloadfiles'] = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
106c55fb 1251 $service['uploadfiles'] = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles'];
c1b65883 1252 $service['shortname'] = !isset($service['shortname']) ? null : $service['shortname'];
c976e271 1253
1254 $update = false;
c976e271 1255 if ($dbservice->requiredcapability != $service['requiredcapability']) {
1256 $dbservice->requiredcapability = $service['requiredcapability'];
1257 $update = true;
1258 }
1259 if ($dbservice->restrictedusers != $service['restrictedusers']) {
1260 $dbservice->restrictedusers = $service['restrictedusers'];
1261 $update = true;
1262 }
af03513f
JM
1263 if ($dbservice->downloadfiles != $service['downloadfiles']) {
1264 $dbservice->downloadfiles = $service['downloadfiles'];
1265 $update = true;
1266 }
106c55fb
DW
1267 if ($dbservice->uploadfiles != $service['uploadfiles']) {
1268 $dbservice->uploadfiles = $service['uploadfiles'];
1269 $update = true;
1270 }
c1b65883
JM
1271 //if shortname is not a PARAM_ALPHANUMEXT, fail (tested here for service update and creation)
1272 if (isset($service['shortname']) and
1273 (clean_param($service['shortname'], PARAM_ALPHANUMEXT) != $service['shortname'])) {
1274 throw new moodle_exception('installserviceshortnameerror', 'webservice', '', $service['shortname']);
1275 }
1276 if ($dbservice->shortname != $service['shortname']) {
1277 //check that shortname is unique
1278 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
1279 $existingservice = $DB->get_record('external_services',
1280 array('shortname' => $service['shortname']));
1281 if (!empty($existingservice)) {
1282 throw new moodle_exception('installexistingserviceshortnameerror', 'webservice', '', $service['shortname']);
1283 }
1284 }
1285 $dbservice->shortname = $service['shortname'];
1286 $update = true;
1287 }
c976e271 1288 if ($update) {
1289 $DB->update_record('external_services', $dbservice);
1290 }
1291
1292 $functions = $DB->get_records('external_services_functions', array('externalserviceid'=>$dbservice->id));
1293 foreach ($functions as $function) {
1294 $key = array_search($function->functionname, $service['functions']);
1295 if ($key === false) {
1296 $DB->delete_records('external_services_functions', array('id'=>$function->id));
1297 } else {
1298 unset($service['functions'][$key]);
1299 }
1300 }
1301 foreach ($service['functions'] as $fname) {
365a5941 1302 $newf = new stdClass();
c976e271 1303 $newf->externalserviceid = $dbservice->id;
1304 $newf->functionname = $fname;
1305 $DB->insert_record('external_services_functions', $newf);
1306 }
1307 unset($functions);
1308 }
1309 foreach ($services as $name => $service) {
c1b65883
JM
1310 //check that shortname is unique
1311 if (isset($service['shortname'])) { //we currently accepts multiple shortname == null
1312 $existingservice = $DB->get_record('external_services',
1313 array('shortname' => $service['shortname']));
1314 if (!empty($existingservice)) {
1315 throw new moodle_exception('installserviceshortnameerror', 'webservice');
1316 }
1317 }
1318
365a5941 1319 $dbservice = new stdClass();
c976e271 1320 $dbservice->name = $name;
1321 $dbservice->enabled = empty($service['enabled']) ? 0 : $service['enabled'];
1322 $dbservice->requiredcapability = empty($service['requiredcapability']) ? null : $service['requiredcapability'];
1323 $dbservice->restrictedusers = !isset($service['restrictedusers']) ? 1 : $service['restrictedusers'];
af03513f 1324 $dbservice->downloadfiles = !isset($service['downloadfiles']) ? 0 : $service['downloadfiles'];
106c55fb 1325 $dbservice->uploadfiles = !isset($service['uploadfiles']) ? 0 : $service['uploadfiles'];
c1b65883 1326 $dbservice->shortname = !isset($service['shortname']) ? null : $service['shortname'];
c976e271 1327 $dbservice->component = $component;
e5180580 1328 $dbservice->timecreated = time();
c976e271 1329 $dbservice->id = $DB->insert_record('external_services', $dbservice);
1330 foreach ($service['functions'] as $fname) {
365a5941 1331 $newf = new stdClass();
c976e271 1332 $newf->externalserviceid = $dbservice->id;
1333 $newf->functionname = $fname;
1334 $DB->insert_record('external_services_functions', $newf);
1335 }
1336 }
1337}
1338
186eba1b 1339/**
ee7295ee 1340 * Allow plugins and subsystems to add external functions to other plugins or built-in services.
186eba1b
JL
1341 * This function is executed just after all the plugins have been updated.
1342 */
1343function external_update_services() {
1344 global $DB;
1345
1346 // Look for external functions that want to be added in existing services.
1347 $functions = $DB->get_records_select('external_functions', 'services IS NOT NULL');
1348
1349 $servicescache = array();
1350 foreach ($functions as $function) {
1351 // Prevent edge cases.
1352 if (empty($function->services)) {
1353 continue;
1354 }
1355 $services = explode(',', $function->services);
1356
1357 foreach ($services as $serviceshortname) {
1358 // Get the service id by shortname.
1359 if (!empty($servicescache[$serviceshortname])) {
1360 $serviceid = $servicescache[$serviceshortname];
1361 } else if ($service = $DB->get_record('external_services', array('shortname' => $serviceshortname))) {
1362 // If the component is empty, it means that is not a built-in service.
1363 // We don't allow functions to inject themselves in services created by an user in Moodle.
1364 if (empty($service->component)) {
1365 continue;
1366 }
1367 $serviceid = $service->id;
1368 $servicescache[$serviceshortname] = $serviceid;
1369 } else {
1370 // Service not found.
1371 continue;
1372 }
1373 // Finally add the function to the service.
1374 $newf = new stdClass();
1375 $newf->externalserviceid = $serviceid;
1376 $newf->functionname = $function->name;
1377
1378 if (!$DB->record_exists('external_services_functions', (array)$newf)) {
1379 $DB->insert_record('external_services_functions', $newf);
1380 }
1381 }
1382 }
1383}
1384
72fb21b6 1385/**
1386 * upgrade logging functions
72fb21b6 1387 */
fd1a792e 1388function upgrade_handle_exception($ex, $plugin = null) {
96e1fdf4
PS
1389 global $CFG;
1390
a56c457e
PS
1391 // rollback everything, we need to log all upgrade problems
1392 abort_all_db_transactions();
1393
c19bc39c
PS
1394 $info = get_exception_info($ex);
1395
1396 // First log upgrade error
1397 upgrade_log(UPGRADE_LOG_ERROR, $plugin, 'Exception: ' . get_class($ex), $info->message, $info->backtrace);
1398
1399 // Always turn on debugging - admins need to know what is going on
96f81ea3 1400 set_debugging(DEBUG_DEVELOPER, true);
c19bc39c 1401
fd1a792e 1402 default_exception_handler($ex, true, $plugin);
db9d4a3d 1403}
1404
1405/**
1406 * Adds log entry into upgrade_log table
1407 *
1408 * @param int $type UPGRADE_LOG_NORMAL, UPGRADE_LOG_NOTICE or UPGRADE_LOG_ERROR
bb8b2971 1409 * @param string $plugin frankenstyle component name
db9d4a3d 1410 * @param string $info short description text of log entry
1411 * @param string $details long problem description
1412 * @param string $backtrace string used for errors only
1413 * @return void
1414 */
1415function upgrade_log($type, $plugin, $info, $details=null, $backtrace=null) {
1416 global $DB, $USER, $CFG;
1417
bb8b2971
PS
1418 if (empty($plugin)) {
1419 $plugin = 'core';
1420 }
1421
56da374e 1422 list($plugintype, $pluginname) = core_component::normalize_component($plugin);
bb8b2971 1423 $component = is_null($pluginname) ? $plugintype : $plugintype . '_' . $pluginname;
795a08ad 1424
34a2777c 1425 $backtrace = format_backtrace($backtrace, true);
db9d4a3d 1426
bb8b2971
PS
1427 $currentversion = null;
1428 $targetversion = null;
db9d4a3d 1429
1430 //first try to find out current version number
bb8b2971 1431 if ($plugintype === 'core') {
db9d4a3d 1432 //main
bb8b2971 1433 $currentversion = $CFG->version;
db9d4a3d 1434
bb8b2971
PS
1435 $version = null;
1436 include("$CFG->dirroot/version.php");
1437 $targetversion = $version;
795a08ad 1438
795a08ad 1439 } else {
bb8b2971 1440 $pluginversion = get_config($component, 'version');
795a08ad 1441 if (!empty($pluginversion)) {
bb8b2971
PS
1442 $currentversion = $pluginversion;
1443 }
b0d1d941 1444 $cd = core_component::get_component_directory($component);
bb8b2971
PS
1445 if (file_exists("$cd/version.php")) {
1446 $plugin = new stdClass();
1447 $plugin->version = null;
bde002b8 1448 $module = $plugin;
bb8b2971
PS
1449 include("$cd/version.php");
1450 $targetversion = $plugin->version;
795a08ad 1451 }
db9d4a3d 1452 }
1453
365a5941 1454 $log = new stdClass();
bb8b2971
PS
1455 $log->type = $type;
1456 $log->plugin = $component;
1457 $log->version = $currentversion;
1458 $log->targetversion = $targetversion;
1459 $log->info = $info;
1460 $log->details = $details;
1461 $log->backtrace = $backtrace;
1462 $log->userid = $USER->id;
1463 $log->timemodified = time();
db9d4a3d 1464 try {
1465 $DB->insert_record('upgrade_log', $log);
1466 } catch (Exception $ignored) {
795a08ad 1467 // possible during install or 2.0 upgrade
db9d4a3d 1468 }
1469}
1470
1471/**
1472 * Marks start of upgrade, blocks any other access to site.
1473 * The upgrade is finished at the end of script or after timeout.
72fb21b6 1474 *
1475 * @global object
1476 * @global object
1477 * @global object
db9d4a3d 1478 */
1479function upgrade_started($preinstall=false) {
de6d81e6 1480 global $CFG, $DB, $PAGE, $OUTPUT;
db9d4a3d 1481
1482 static $started = false;
1483
1484 if ($preinstall) {
1485 ignore_user_abort(true);
1486 upgrade_setup_debug(true);
1487
1488 } else if ($started) {
1489 upgrade_set_timeout(120);
1490
1491 } else {
c13a5e71 1492 if (!CLI_SCRIPT and !$PAGE->headerprinted) {
db9d4a3d 1493 $strupgrade = get_string('upgradingversion', 'admin');
78946b9b 1494 $PAGE->set_pagelayout('maintenance');
543f54d3 1495 upgrade_init_javascript();
de6d81e6 1496 $PAGE->set_title($strupgrade.' - Moodle '.$CFG->target_release);
1497 $PAGE->set_heading($strupgrade);
1498 $PAGE->navbar->add($strupgrade);
1499 $PAGE->set_cacheable(false);
1500 echo $OUTPUT->header();
db9d4a3d 1501 }
1502
1503 ignore_user_abort(true);
38fc0130 1504 core_shutdown_manager::register_function('upgrade_finished_handler');
db9d4a3d 1505 upgrade_setup_debug(true);
1506 set_config('upgraderunning', time()+300);
1507 $started = true;
1508 }
1509}
1510
1511/**
b921b6ee 1512 * Internal function - executed if upgrade interrupted.
db9d4a3d 1513 */
1514function upgrade_finished_handler() {
1515 upgrade_finished();
1516}
1517
1518/**
1519 * Indicates upgrade is finished.
1520 *
1521 * This function may be called repeatedly.
72fb21b6 1522 *
1523 * @global object
1524 * @global object
db9d4a3d 1525 */
1526function upgrade_finished($continueurl=null) {
7e0d6675 1527 global $CFG, $DB, $OUTPUT;
db9d4a3d 1528
1529 if (!empty($CFG->upgraderunning)) {
1530 unset_config('upgraderunning');
24dedb96
SH
1531 // We have to forcefully purge the caches using the writer here.
1532 // This has to be done after we unset the config var. If someone hits the site while this is set they will
1533 // cause the config values to propogate to the caches.
1534 // Caches are purged after the last step in an upgrade but there is several code routines that exceute between
1535 // then and now that leaving a window for things to fall out of sync.
1536 cache_helper::purge_all(true);
db9d4a3d 1537 upgrade_setup_debug(false);
1538 ignore_user_abort(false);
1539 if ($continueurl) {
aa9a6867 1540 echo $OUTPUT->continue_button($continueurl);
7e0d6675 1541 echo $OUTPUT->footer();
db9d4a3d 1542 die;
1543 }
1544 }
1545}
1546
72fb21b6 1547/**
1548 * @global object
1549 * @global object
1550 */
db9d4a3d 1551function upgrade_setup_debug($starting) {
1552 global $CFG, $DB;
1553
1554 static $originaldebug = null;
1555
1556 if ($starting) {
1557 if ($originaldebug === null) {
1558 $originaldebug = $DB->get_debug();
1559 }
1560 if (!empty($CFG->upgradeshowsql)) {
1561 $DB->set_debug(true);
1562 }
1563 } else {
1564 $DB->set_debug($originaldebug);
1565 }
1566}
1567
1568function print_upgrade_separator() {
1569 if (!CLI_SCRIPT) {
1570 echo '<hr />';
1571 }
1572}
1573
795a08ad 1574/**
1575 * Default start upgrade callback
1576 * @param string $plugin
b921b6ee 1577 * @param bool $installation true if installation, false means upgrade
795a08ad 1578 */
194cdca8 1579function print_upgrade_part_start($plugin, $installation, $verbose) {
3c159385 1580 global $OUTPUT;
795a08ad 1581 if (empty($plugin) or $plugin == 'moodle') {
1582 upgrade_started($installation); // does not store upgrade running flag yet
194cdca8 1583 if ($verbose) {
3c159385 1584 echo $OUTPUT->heading(get_string('coresystem'));
194cdca8 1585 }
795a08ad 1586 } else {
1587 upgrade_started();
194cdca8 1588 if ($verbose) {
3c159385 1589 echo $OUTPUT->heading($plugin);
194cdca8 1590 }
795a08ad 1591 }
1592 if ($installation) {
1593 if (empty($plugin) or $plugin == 'moodle') {
1594 // no need to log - log table not yet there ;-)
1595 } else {
1596 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin installation');
1597 }
1598 } else {
83740b9f 1599 core_upgrade_time::record_start();
795a08ad 1600 if (empty($plugin) or $plugin == 'moodle') {
1601 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting core upgrade');
1602 } else {
1603 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Starting plugin upgrade');
1604 }
1605 }
1606}
1607
1608/**
1609 * Default end upgrade callback
1610 * @param string $plugin
b921b6ee 1611 * @param bool $installation true if installation, false means upgrade
795a08ad 1612 */
194cdca8 1613function print_upgrade_part_end($plugin, $installation, $verbose) {
aa9a6867 1614 global $OUTPUT;
795a08ad 1615 upgrade_started();
1616 if ($installation) {
1617 if (empty($plugin) or $plugin == 'moodle') {
1618 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core installed');
1619 } else {
1620 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin installed');
1621 }
1622 } else {
1623 if (empty($plugin) or $plugin == 'moodle') {
1624 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Core upgraded');
1625 } else {
1626 upgrade_log(UPGRADE_LOG_NORMAL, $plugin, 'Plugin upgraded');
1627 }
1628 }
194cdca8 1629 if ($verbose) {
83740b9f
JP
1630 if ($installation) {
1631 $message = get_string('success');
1632 } else {
1633 $duration = core_upgrade_time::get_elapsed();
1634 $message = get_string('successduration', '', format_float($duration, 2));
1635 }
1636 $notification = new \core\output\notification($message, \core\output\notification::NOTIFY_SUCCESS);
2059d6ed
RW
1637 $notification->set_show_closebutton(false);
1638 echo $OUTPUT->render($notification);
194cdca8 1639 print_upgrade_separator();
96db14f9 1640 }
1641}
1642
72fb21b6 1643/**
b921b6ee 1644 * Sets up JS code required for all upgrade scripts.
72fb21b6 1645 * @global object
1646 */
543f54d3 1647function upgrade_init_javascript() {
e29380f3 1648 global $PAGE;
543f54d3
PS
1649 // scroll to the end of each upgrade page so that ppl see either error or continue button,
1650 // no need to scroll continuously any more, it is enough to jump to end once the footer is printed ;-)
1651 $js = "window.scrollTo(0, 5000000);";
1652 $PAGE->requires->js_init_code($js);
db9d4a3d 1653}
1654
db9d4a3d 1655/**
1656 * Try to upgrade the given language pack (or current language)
af8d4d91 1657 *
74a4c9a9 1658 * @param string $lang the code of the language to update, defaults to the current language
db9d4a3d 1659 */
bd41bdd9
PS
1660function upgrade_language_pack($lang = null) {
1661 global $CFG;
db9d4a3d 1662
bd41bdd9
PS
1663 if (!empty($CFG->skiplangupgrade)) {
1664 return;
1665 }
af8d4d91 1666
bd41bdd9
PS
1667 if (!file_exists("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php")) {
1668 // weird, somebody uninstalled the import utility
1669 return;
1670 }
1671
1672 if (!$lang) {
db9d4a3d 1673 $lang = current_language();
1674 }
1675
bd41bdd9
PS
1676 if (!get_string_manager()->translation_exists($lang)) {
1677 return;
db9d4a3d 1678 }
1679
bd41bdd9
PS
1680 get_string_manager()->reset_caches();
1681
1682 if ($lang === 'en') {
1683 return; // Nothing to do
db9d4a3d 1684 }
af8d4d91 1685
bd41bdd9
PS
1686 upgrade_started(false);
1687
1688 require_once("$CFG->dirroot/$CFG->admin/tool/langimport/lib.php");
1689 tool_langimport_preupgrade_update($lang);
1690
af8d4d91
DM
1691 get_string_manager()->reset_caches();
1692
551fe0e5 1693 print_upgrade_separator();
db9d4a3d 1694}
8580535b 1695
0f268f5d
RW
1696/**
1697 * Build the current theme so that the user doesn't have to wait for it
1698 * to build on the first page load after the install / upgrade.
1699 */
1700function upgrade_themes() {
1701 global $CFG;
1702
1703 require_once("{$CFG->libdir}/outputlib.php");
1704
1705 // Build the current theme so that the user can immediately
1706 // browse the site without having to wait for the theme to build.
1707 $themeconfig = theme_config::load($CFG->theme);
1708 $direction = right_to_left() ? 'rtl' : 'ltr';
1709 theme_build_css_for_themes([$themeconfig], [$direction]);
1710
1711 // Only queue the task if there isn't already one queued.
1712 if (empty(\core\task\manager::get_adhoc_tasks('\\core\\task\\build_installed_themes_task'))) {
1713 // Queue a task to build all of the site themes at some point
1714 // later. These can happen offline because it doesn't block the
1715 // user unless they quickly change theme.
1716 $adhoctask = new \core\task\build_installed_themes_task();
1717 \core\task\manager::queue_adhoc_task($adhoctask);
1718 }
1719}
1720
8580535b 1721/**
1722 * Install core moodle tables and initialize
1723 * @param float $version target version
1724 * @param bool $verbose
1725 * @return void, may throw exception
1726 */
1727function install_core($version, $verbose) {
1728 global $CFG, $DB;
1729
e2e35e71 1730 // We can not call purge_all_caches() yet, make sure the temp and cache dirs exist and are empty.
e2e35e71 1731 remove_dir($CFG->cachedir.'', true);
85b38061
PS
1732 make_cache_directory('', true);
1733
1734 remove_dir($CFG->localcachedir.'', true);
1735 make_localcache_directory('', true);
1736
e2e35e71 1737 remove_dir($CFG->tempdir.'', true);
85b38061
PS
1738 make_temp_directory('', true);
1739
ef844148
MS
1740 remove_dir($CFG->backuptempdir.'', true);
1741 make_backup_temp_directory('', true);
1742
e2e35e71 1743 remove_dir($CFG->dataroot.'/muc', true);
85b38061 1744 make_writable_directory($CFG->dataroot.'/muc', true);
e2e35e71 1745
8580535b 1746 try {
3ef7279f 1747 core_php_time_limit::raise(600);
194cdca8 1748 print_upgrade_part_start('moodle', true, $verbose); // does not store upgrade running flag
8580535b 1749
1750 $DB->get_manager()->install_from_xmldb_file("$CFG->libdir/db/install.xml");
1751 upgrade_started(); // we want the flag to be stored in config table ;-)
1752
1753 // set all core default records and default settings
1754 require_once("$CFG->libdir/db/install.php");
c6d75bff 1755 xmldb_main_install(); // installs the capabilities too
8580535b 1756
1757 // store version
1758 upgrade_main_savepoint(true, $version, false);
1759
df997f84 1760 // Continue with the installation
c6d75bff
PS
1761 log_update_descriptions('moodle');
1762 external_update_descriptions('moodle');
309ae892 1763 \core\task\manager::reset_scheduled_tasks_for_component('moodle');
606c3be0 1764 \core_analytics\manager::update_default_models_for_component('moodle');
8580535b 1765 message_update_providers('moodle');
6c0bfb14 1766 \core\message\inbound\manager::update_handlers_for_component('moodle');
c026a28d 1767 core_tag_area::reset_definitions_for_component('moodle');
8580535b 1768
df997f84 1769 // Write default settings unconditionally
8580535b 1770 admin_apply_default_settings(NULL, true);
1771
194cdca8 1772 print_upgrade_part_end(null, true, $verbose);
94ef67cf 1773
e0d9b7c0
SH
1774 // Purge all caches. They're disabled but this ensures that we don't have any persistent data just in case something
1775 // during installation didn't use APIs.
1776 cache_helper::purge_all();
8580535b 1777 } catch (exception $ex) {
1778 upgrade_handle_exception($ex);
1766e6a1
MG
1779 } catch (Throwable $ex) {
1780 // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
1781 upgrade_handle_exception($ex);
8580535b 1782 }
1783}
1784
1785/**
1786 * Upgrade moodle core
1787 * @param float $version target version
1788 * @param bool $verbose
1789 * @return void, may throw exception
1790 */
1791function upgrade_core($version, $verbose) {
e2049782 1792 global $CFG, $SITE, $DB, $COURSE;
8580535b 1793
41209c1e
PS
1794 raise_memory_limit(MEMORY_EXTRA);
1795
3316fe24 1796 require_once($CFG->libdir.'/db/upgrade.php'); // Defines upgrades
3316fe24 1797
8580535b 1798 try {
7b4d5af7 1799 // Reset caches before any output.
b0dd08dd 1800 cache_helper::purge_all(true);
7b4d5af7 1801 purge_all_caches();
dcf9be7f 1802
8580535b 1803 // Upgrade current language pack if we can
bd41bdd9 1804 upgrade_language_pack();
8580535b 1805
194cdca8 1806 print_upgrade_part_start('moodle', false, $verbose);
8580535b 1807
6b4b7490
PS
1808 // Pre-upgrade scripts for local hack workarounds.
1809 $preupgradefile = "$CFG->dirroot/local/preupgrade.php";
1810 if (file_exists($preupgradefile)) {
3ef7279f 1811 core_php_time_limit::raise();
6b4b7490
PS
1812 require($preupgradefile);
1813 // Reset upgrade timeout to default.
1814 upgrade_set_timeout();
17da2e6f 1815 }
1816
8580535b 1817 $result = xmldb_main_upgrade($CFG->version);
1818 if ($version > $CFG->version) {
1819 // store version if not already there
1820 upgrade_main_savepoint($result, $version, false);
1821 }
1822
e2049782
MG
1823 // In case structure of 'course' table has been changed and we forgot to update $SITE, re-read it from db.
1824 $SITE = $DB->get_record('course', array('id' => $SITE->id));
1825 $COURSE = clone($SITE);
1826
8580535b 1827 // perform all other component upgrade routines
1828 update_capabilities('moodle');
c6d75bff 1829 log_update_descriptions('moodle');
c976e271 1830 external_update_descriptions('moodle');
309ae892 1831 \core\task\manager::reset_scheduled_tasks_for_component('moodle');
606c3be0 1832 \core_analytics\manager::update_default_models_for_component('moodle');
8580535b 1833 message_update_providers('moodle');
6c0bfb14 1834 \core\message\inbound\manager::update_handlers_for_component('moodle');
c026a28d 1835 core_tag_area::reset_definitions_for_component('moodle');
75af47ee
SH
1836 // Update core definitions.
1837 cache_helper::update_definitions(true);
8580535b 1838
94ef67cf 1839 // Purge caches again, just to be sure we arn't holding onto old stuff now.
a5a6e93f 1840 cache_helper::purge_all(true);
7b4d5af7 1841 purge_all_caches();
2e5eba85
PS
1842
1843 // Clean up contexts - more and more stuff depends on existence of paths and contexts
e922fe23
PS
1844 context_helper::cleanup_instances();
1845 context_helper::create_instances(null, false);
1846 context_helper::build_all_paths(false);
1847 $syscontext = context_system::instance();
1848 $syscontext->mark_dirty();
8580535b 1849
194cdca8 1850 print_upgrade_part_end('moodle', false, $verbose);
8580535b 1851 } catch (Exception $ex) {
1852 upgrade_handle_exception($ex);
1766e6a1
MG
1853 } catch (Throwable $ex) {
1854 // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
1855 upgrade_handle_exception($ex);
8580535b 1856 }
1857}
1858
1859/**
1860 * Upgrade/install other parts of moodle
1861 * @param bool $verbose
1862 * @return void, may throw exception
1863 */
1864function upgrade_noncore($verbose) {
1865 global $CFG;
1866
41209c1e
PS
1867 raise_memory_limit(MEMORY_EXTRA);
1868
8580535b 1869 // upgrade all plugins types
1870 try {
7b4d5af7
PS
1871 // Reset caches before any output.
1872 cache_helper::purge_all(true);
1873 purge_all_caches();
1874
46f6f7f2 1875 $plugintypes = core_component::get_plugin_types();
8580535b 1876 foreach ($plugintypes as $type=>$location) {
17da2e6f 1877 upgrade_plugins($type, 'print_upgrade_part_start', 'print_upgrade_part_end', $verbose);
8580535b 1878 }
ee7295ee
JL
1879 // Upgrade services.
1880 // This function gives plugins and subsystems a chance to add functions to existing built-in services.
186eba1b
JL
1881 external_update_services();
1882
94ef67cf 1883 // Update cache definitions. Involves scanning each plugin for any changes.
75af47ee 1884 cache_helper::update_definitions();
c5701ce7
PS
1885 // Mark the site as upgraded.
1886 set_config('allversionshash', core_component::get_all_versions_hash());
7b4d5af7
PS
1887
1888 // Purge caches again, just to be sure we arn't holding onto old stuff now.
1889 cache_helper::purge_all(true);
1890 purge_all_caches();
1891
8580535b 1892 } catch (Exception $ex) {
1893 upgrade_handle_exception($ex);
1766e6a1
MG
1894 } catch (Throwable $ex) {
1895 // Engine errors in PHP7 throw exceptions of type Throwable (this "catch" will be ignored in PHP5).
1896 upgrade_handle_exception($ex);
8580535b 1897 }
8580535b 1898}
3316fe24 1899
1900/**
1901 * Checks if the main tables have been installed yet or not.
e2e35e71
PS
1902 *
1903 * Note: we can not use caches here because they might be stale,
1904 * use with care!
1905 *
3316fe24 1906 * @return bool
1907 */
1908function core_tables_exist() {
1909 global $DB;
1910
e2e35e71 1911 if (!$tables = $DB->get_tables(false) ) { // No tables yet at all.
3316fe24 1912 return false;
9b683d13 1913
3316fe24 1914 } else { // Check for missing main tables
1915 $mtables = array('config', 'course', 'groupings'); // some tables used in 1.9 and 2.0, preferable something from the start and end of install.xml
1916 foreach ($mtables as $mtable) {
1917 if (!in_array($mtable, $tables)) {
1918 return false;
1919 }
1920 }
1921 return true;
9b683d13 1922 }
ba04999c 1923}
c71ade2f
PL
1924
1925/**
1926 * upgrades the mnet rpc definitions for the given component.
1927 * this method doesn't return status, an exception will be thrown in the case of an error
1928 *
1929 * @param string $component the plugin to upgrade, eg auth_mnet
1930 */
1931function upgrade_plugin_mnet_functions($component) {
1932 global $DB, $CFG;
1933
8c59114f 1934 list($type, $plugin) = core_component::normalize_component($component);
1c74b260 1935 $path = core_component::get_plugin_directory($type, $plugin);
c71ade2f 1936
17b71716
PS
1937 $publishes = array();
1938 $subscribes = array();
c71ade2f
PL
1939 if (file_exists($path . '/db/mnet.php')) {
1940 require_once($path . '/db/mnet.php'); // $publishes comes from this file
1941 }
1942 if (empty($publishes)) {
1943 $publishes = array(); // still need this to be able to disable stuff later
1944 }
1945 if (empty($subscribes)) {
1946 $subscribes = array(); // still need this to be able to disable stuff later
1947 }
1948
1949 static $servicecache = array();
1950
1951 // rekey an array based on the rpc method for easy lookups later
1952 $publishmethodservices = array();
1953 $subscribemethodservices = array();
1954 foreach($publishes as $servicename => $service) {
1955 if (is_array($service['methods'])) {
1956 foreach($service['methods'] as $methodname) {
1957 $service['servicename'] = $servicename;
1958 $publishmethodservices[$methodname][] = $service;
1959 }
1960 }
1961 }
1962
1963 // Disable functions that don't exist (any more) in the source
1964 // Should these be deleted? What about their permissions records?
1965 foreach ($DB->get_records('mnet_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
1966 if (!array_key_exists($rpc->functionname, $publishmethodservices) && $rpc->enabled) {
1967 $DB->set_field('mnet_rpc', 'enabled', 0, array('id' => $rpc->id));
1968 } else if (array_key_exists($rpc->functionname, $publishmethodservices) && !$rpc->enabled) {
1969 $DB->set_field('mnet_rpc', 'enabled', 1, array('id' => $rpc->id));
1970 }
1971 }
1972
1973 // reflect all the services we're publishing and save them
c71ade2f
PL
1974 static $cachedclasses = array(); // to store reflection information in
1975 foreach ($publishes as $service => $data) {
1976 $f = $data['filename'];
1977 $c = $data['classname'];
1978 foreach ($data['methods'] as $method) {
6bdfef5d 1979 $dataobject = new stdClass();
c71ade2f
PL
1980 $dataobject->plugintype = $type;
1981 $dataobject->pluginname = $plugin;
1982 $dataobject->enabled = 1;
1983 $dataobject->classname = $c;
1984 $dataobject->filename = $f;
1985
1986 if (is_string($method)) {
1987 $dataobject->functionname = $method;
1988
1989 } else if (is_array($method)) { // wants to override file or class
1990 $dataobject->functionname = $method['method'];
1991 $dataobject->classname = $method['classname'];
1992 $dataobject->filename = $method['filename'];
1993 }
1994 $dataobject->xmlrpcpath = $type.'/'.$plugin.'/'.$dataobject->filename.'/'.$method;
1995 $dataobject->static = false;
1996
1997 require_once($path . '/' . $dataobject->filename);
1998 $functionreflect = null; // slightly different ways to get this depending on whether it's a class method or a function
1999 if (!empty($dataobject->classname)) {
2000 if (!class_exists($dataobject->classname)) {
2001 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
2002 }
2003 $key = $dataobject->filename . '|' . $dataobject->classname;
2004 if (!array_key_exists($key, $cachedclasses)) { // look to see if we've already got a reflection object
2005 try {
052141ab
CB
2006 $cachedclasses[$key] = new ReflectionClass($dataobject->classname);
2007 } catch (ReflectionException $e) { // catch these and rethrow them to something more helpful
c71ade2f
PL
2008 throw new moodle_exception('installreflectionclasserror', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname, 'error' => $e->getMessage()));
2009 }
2010 }
2011 $r =& $cachedclasses[$key];
2012 if (!$r->hasMethod($dataobject->functionname)) {
2013 throw new moodle_exception('installnosuchmethod', 'mnet', '', (object)array('method' => $dataobject->functionname, 'class' => $dataobject->classname));
2014 }
052141ab 2015 $functionreflect = $r->getMethod($dataobject->functionname);
c71ade2f
PL
2016 $dataobject->static = (int)$functionreflect->isStatic();
2017 } else {
2018 if (!function_exists($dataobject->functionname)) {
2019 throw new moodle_exception('installnosuchfunction', 'mnet', '', (object)array('method' => $dataobject->functionname, 'file' => $dataobject->filename));
2020 }
2021 try {
052141ab
CB
2022 $functionreflect = new ReflectionFunction($dataobject->functionname);
2023 } catch (ReflectionException $e) { // catch these and rethrow them to something more helpful
c71ade2f
PL
2024 throw new moodle_exception('installreflectionfunctionerror', 'mnet', '', (object)array('method' => $dataobject->functionname, '' => $dataobject->filename, 'error' => $e->getMessage()));
2025 }
2026 }
2027 $dataobject->profile = serialize(admin_mnet_method_profile($functionreflect));
052141ab 2028 $dataobject->help = admin_mnet_method_get_help($functionreflect);
c71ade2f
PL
2029
2030 if ($record_exists = $DB->get_record('mnet_rpc', array('xmlrpcpath'=>$dataobject->xmlrpcpath))) {
2031 $dataobject->id = $record_exists->id;
2032 $dataobject->enabled = $record_exists->enabled;
2033 $DB->update_record('mnet_rpc', $dataobject);
2034 } else {
2035 $dataobject->id = $DB->insert_record('mnet_rpc', $dataobject, true);
2036 }
c71ade2f 2037
d5bd0b01
DM
2038 // TODO this API versioning must be reworked, here the recently processed method
2039 // sets the service API which may not be correct
677b6321
PL
2040 foreach ($publishmethodservices[$dataobject->functionname] as $service) {
2041 if ($serviceobj = $DB->get_record('mnet_service', array('name'=>$service['servicename']))) {
2042 $serviceobj->apiversion = $service['apiversion'];
2043 $DB->update_record('mnet_service', $serviceobj);
2044 } else {
2045 $serviceobj = new stdClass();
2046 $serviceobj->name = $service['servicename'];
f4a2817a 2047 $serviceobj->description = empty($service['description']) ? '' : $service['description'];
677b6321
PL
2048 $serviceobj->apiversion = $service['apiversion'];
2049 $serviceobj->offer = 1;
2050 $serviceobj->id = $DB->insert_record('mnet_service', $serviceobj);
2051 }
2052 $servicecache[$service['servicename']] = $serviceobj;
2053 if (!$DB->record_exists('mnet_service2rpc', array('rpcid'=>$dataobject->id, 'serviceid'=>$serviceobj->id))) {
2054 $obj = new stdClass();
2055 $obj->rpcid = $dataobject->id;
2056 $obj->serviceid = $serviceobj->id;
2057 $DB->insert_record('mnet_service2rpc', $obj, true);
2058 }
c71ade2f
PL
2059 }
2060 }
2061 }
c71ade2f
PL
2062 // finished with methods we publish, now do subscribable methods
2063 foreach($subscribes as $service => $methods) {
2064 if (!array_key_exists($service, $servicecache)) {
2065 if (!$serviceobj = $DB->get_record('mnet_service', array('name' => $service))) {
81634f03 2066 debugging("TODO: skipping unknown service $service - somebody needs to fix MDL-21993");
c71ade2f
PL
2067 continue;
2068 }
2069 $servicecache[$service] = $serviceobj;
2070 } else {
2071 $serviceobj = $servicecache[$service];
2072 }
2073 foreach ($methods as $method => $xmlrpcpath) {
2074 if (!$rpcid = $DB->get_field('mnet_remote_rpc', 'id', array('xmlrpcpath'=>$xmlrpcpath))) {
2075 $remoterpc = (object)array(
2076 'functionname' => $method,
2077 'xmlrpcpath' => $xmlrpcpath,
2078 'plugintype' => $type,
2079 'pluginname' => $plugin,
2080 'enabled' => 1,
2081 );
2082 $rpcid = $remoterpc->id = $DB->insert_record('mnet_remote_rpc', $remoterpc, true);
2083 }
2084 if (!$DB->record_exists('mnet_remote_service2rpc', array('rpcid'=>$rpcid, 'serviceid'=>$serviceobj->id))) {
2085 $obj = new stdClass();
2086 $obj->rpcid = $rpcid;
2087 $obj->serviceid = $serviceobj->id;
2088 $DB->insert_record('mnet_remote_service2rpc', $obj, true);
2089 }
677b6321 2090 $subscribemethodservices[$method][] = $service;
c71ade2f
PL
2091 }
2092 }
2093
2094 foreach ($DB->get_records('mnet_remote_rpc', array('pluginname'=>$plugin, 'plugintype'=>$type), 'functionname ASC ') as $rpc) {
2095 if (!array_key_exists($rpc->functionname, $subscribemethodservices) && $rpc->enabled) {
2096 $DB->set_field('mnet_remote_rpc', 'enabled', 0, array('id' => $rpc->id));
2097 } else if (array_key_exists($rpc->functionname, $subscribemethodservices) && !$rpc->enabled) {
2098 $DB->set_field('mnet_remote_rpc', 'enabled', 1, array('id' => $rpc->id));
2099 }
2100 }
2101
2102 return true;
2103}
2104
2105/**
052141ab 2106 * Given some sort of reflection function/method object, return a profile array, ready to be serialized and stored
c71ade2f 2107 *
052141ab 2108 * @param ReflectionFunctionAbstract $function reflection function/method object from which to extract information
c71ade2f 2109 *
052141ab 2110 * @return array associative array with function/method information
c71ade2f 2111 */
052141ab
CB
2112function admin_mnet_method_profile(ReflectionFunctionAbstract $function) {
2113 $commentlines = admin_mnet_method_get_docblock($function);
2114 $getkey = function($key) use ($commentlines) {
2115 return array_values(array_filter($commentlines, function($line) use ($key) {
2116 return $line[0] == $key;
2117 }));
2118 };
2119 $returnline = $getkey('@return');
2120 return array (
2121 'parameters' => array_map(function($line) {
2122 return array(
2123 'name' => trim($line[2], " \t\n\r\0\x0B$"),
2124 'type' => $line[1],
2125 'description' => $line[3]
2126 );
2127 }, $getkey('@param')),
2128
2129 'return' => array(
2130 'type' => !empty($returnline[0][1]) ? $returnline[0][1] : 'void',
2131 'description' => !empty($returnline[0][2]) ? $returnline[0][2] : ''
2132 )
c71ade2f 2133 );
052141ab
CB
2134}
2135
2136/**
2137 * Given some sort of reflection function/method object, return an array of docblock lines, where each line is an array of
2138 * keywords/descriptions
2139 *
2140 * @param ReflectionFunctionAbstract $function reflection function/method object from which to extract information
2141 *
2142 * @return array docblock converted in to an array
2143 */
2144function admin_mnet_method_get_docblock(ReflectionFunctionAbstract $function) {
2145 return array_map(function($line) {
2146 $text = trim($line, " \t\n\r\0\x0B*/");
2147 if (strpos($text, '@param') === 0) {
2148 return preg_split('/\s+/', $text, 4);
2149 }
2150
2151 if (strpos($text, '@return') === 0) {
2152 return preg_split('/\s+/', $text, 3);
2153 }
2154
2155 return array($text);
2156 }, explode("\n", $function->getDocComment()));
2157}
2158
2159/**
2160 * Given some sort of reflection function/method object, return just the help text
2161 *
2162 * @param ReflectionFunctionAbstract $function reflection function/method object from which to extract information
2163 *
2164 * @return string docblock help text
2165 */
2166function admin_mnet_method_get_help(ReflectionFunctionAbstract $function) {
2167 $helplines = array_map(function($line) {
2168 return implode(' ', $line);
2169 }, array_values(array_filter(admin_mnet_method_get_docblock($function), function($line) {
2170 return strpos($line[0], '@') !== 0 && !empty($line[0]);
2171 })));
2172
2173 return implode("\n", $helplines);
c71ade2f 2174}
424a19b1 2175
71611510
MN
2176/**
2177 * This function verifies that the database is not using an unsupported storage engine.
2178 *
2179 * @param environment_results $result object to update, if relevant
2180 * @return environment_results|null updated results object, or null if the storage engine is supported
2181 */
2182function check_database_storage_engine(environment_results $result) {
2183 global $DB;
2184
2185 // Check if MySQL is the DB family (this will also be the same for MariaDB).
2186 if ($DB->get_dbfamily() == 'mysql') {
2187 // Get the database engine we will either be using to install the tables, or what we are currently using.
2188 $engine = $DB->get_dbengine();
2189 // Check if MyISAM is the storage engine that will be used, if so, do not proceed and display an error.
2190 if ($engine == 'MyISAM') {
2191 $result->setInfo('unsupported_db_storage_engine');
2192 $result->setStatus(false);
2193 return $result;
2194 }
2195 }
2196
2197 return null;
2198}
1bd4b9fc
SL
2199
2200/**
2201 * Method used to check the usage of slasharguments config and display a warning message.
2202 *
2203 * @param environment_results $result object to update, if relevant.
2204 * @return environment_results|null updated results or null if slasharguments is disabled.
2205 */
2206function check_slasharguments(environment_results $result){
2207 global $CFG;
2208
e284ac02 2209 if (!during_initial_install() && empty($CFG->slasharguments)) {
1bd4b9fc
SL
2210 $result->setInfo('slasharguments');
2211 $result->setStatus(false);
2212 return $result;
2213 }
2214
2215 return null;
2216}
9b8104ce
SL
2217
2218/**
2219 * This function verifies if the database has tables using innoDB Antelope row format.
2220 *
2221 * @param environment_results $result
2222 * @return environment_results|null updated results object, or null if no Antelope table has been found.
2223 */
2224function check_database_tables_row_format(environment_results $result) {
2225 global $DB;
2226
2227 if ($DB->get_dbfamily() == 'mysql') {
2228 $generator = $DB->get_manager()->generator;
2229
2230 foreach ($DB->get_tables(false) as $table) {
2231 $columns = $DB->get_columns($table, false);
c7ccb543 2232 $size = $generator->guess_antelope_row_size($columns);
9b8104ce
SL
2233 $format = $DB->get_row_format($table);
2234
2235 if ($size <= $generator::ANTELOPE_MAX_ROW_SIZE) {
2236 continue;
2237 }
2238
2239 if ($format === 'Compact' or $format === 'Redundant') {
2240 $result->setInfo('unsupported_db_table_row_format');
2241 $result->setStatus(false);
2242 return $result;
2243 }
2244 }
2245 }
2246
2247 return null;
2248}
ebea19cb 2249
0bbefd81
AG
2250/**
2251 * This function verfies that the database has tables using InnoDB Antelope row format.
2252 *
2253 * @param environment_results $result
2254 * @return environment_results|null updated results object, or null if no Antelope table has been found.
2255 */
2256function check_mysql_file_format(environment_results $result) {
2257 global $DB;
2258
2259 if ($DB->get_dbfamily() == 'mysql') {
2260 $collation = $DB->get_dbcollation();
2261 $collationinfo = explode('_', $collation);
2262 $charset = reset($collationinfo);
2263
2264 if ($charset == 'utf8mb4') {
2265 if ($DB->get_row_format() !== "Barracuda") {
2266 $result->setInfo('mysql_full_unicode_support#File_format');
2267 $result->setStatus(false);
2268 return $result;
2269 }
2270 }
2271 }
2272 return null;
2273}
2274
2275/**
2276 * This function verfies that the database has a setting of one file per table. This is required for 'utf8mb4'.
2277 *
2278 * @param environment_results $result
2279 * @return environment_results|null updated results object, or null if innodb_file_per_table = 1.
2280 */
2281function check_mysql_file_per_table(environment_results $result) {
2282 global $DB;
2283
2284 if ($DB->get_dbfamily() == 'mysql') {
2285 $collation = $DB->get_dbcollation();
2286 $collationinfo = explode('_', $collation);
2287 $charset = reset($collationinfo);
2288
2289 if ($charset == 'utf8mb4') {
2290 if (!$DB->is_file_per_table_enabled()) {
2291 $result->setInfo('mysql_full_unicode_support#File_per_table');
2292 $result->setStatus(false);
2293 return $result;
2294 }
2295 }
2296 }
2297 return null;
2298}
2299
2300/**
2301 * This function verfies that the database has the setting of large prefix enabled. This is required for 'utf8mb4'.
2302 *
2303 * @param environment_results $result
2304 * @return environment_results|null updated results object, or null if innodb_large_prefix = 1.
2305 */
2306function check_mysql_large_prefix(environment_results $result) {
2307 global $DB;
2308
2309 if ($DB->get_dbfamily() == 'mysql') {
2310 $collation = $DB->get_dbcollation();
2311 $collationinfo = explode('_', $collation);
2312 $charset = reset($collationinfo);
2313
2314 if ($charset == 'utf8mb4') {
2315 if (!$DB->is_large_prefix_enabled()) {
2316 $result->setInfo('mysql_full_unicode_support#Large_prefix');
2317 $result->setStatus(false);
2318 return $result;
2319 }
2320 }
2321 }
2322 return null;
2323}
2324
2325/**
2326 * This function checks the database to see if it is using incomplete unicode support.
2327 *
2328 * @param environment_results $result $result
2329 * @return environment_results|null updated results object, or null if unicode is fully supported.
2330 */
2331function check_mysql_incomplete_unicode_support(environment_results $result) {
2332 global $DB;
2333
2334 if ($DB->get_dbfamily() == 'mysql') {
2335 $collation = $DB->get_dbcollation();
2336 $collationinfo = explode('_', $collation);
2337 $charset = reset($collationinfo);
2338
2339 if ($charset == 'utf8') {
2340 $result->setInfo('mysql_full_unicode_support');
2341 $result->setStatus(false);
2342 return $result;
2343 }
2344 }
2345 return null;
2346}
2347
9d2112e8
EL
2348/**
2349 * Check if the site is being served using an ssl url.
2350 *
2351 * Note this does not really perform any request neither looks for proxies or
2352 * other situations. Just looks to wwwroot and warn if it's not using https.
2353 *
2354 * @param environment_results $result $result
2355 * @return environment_results|null updated results object, or null if the site is https.
2356 */
2357function check_is_https(environment_results $result) {
2358 global $CFG;
2359
2360 // Only if is defined, non-empty and whatever core tell us.
2361 if (!empty($CFG->wwwroot) && !is_https()) {
2362 $result->setInfo('site not https');
2363 $result->setStatus(false);
2364 return $result;
2365 }
2366 return null;
2367}
2368
9b92642c
EL
2369/**
2370 * Check if the site is using 64 bits PHP.
2371 *
2372 * @param environment_results $result
2373 * @return environment_results|null updated results object, or null if the site is using 64 bits PHP.
2374 */
2375function check_sixtyfour_bits(environment_results $result) {
2376
2377 if (PHP_INT_SIZE === 4) {
2378 $result->setInfo('php not 64 bits');
2379 $result->setStatus(false);
2380 return $result;
2381 }
2382 return null;
2383}
2384
98b32c9e
DM
2385/**
2386 * Assert the upgrade key is provided, if it is defined.
2387 *
2388 * The upgrade key can be defined in the main config.php as $CFG->upgradekey. If
2389 * it is defined there, then its value must be provided every time the site is
2390 * being upgraded, regardless the administrator is logged in or not.
2391 *
2392 * This is supposed to be used at certain places in /admin/index.php only.
2393 *
2394 * @param string|null $upgradekeyhash the SHA-1 of the value provided by the user
2395 */
2396function check_upgrade_key($upgradekeyhash) {
2397 global $CFG, $PAGE;
2398
2399 if (isset($CFG->config_php_settings['upgradekey'])) {
2400 if ($upgradekeyhash === null or $upgradekeyhash !== sha1($CFG->config_php_settings['upgradekey'])) {
2401 if (!$PAGE->headerprinted) {
2402 $output = $PAGE->get_renderer('core', 'admin');
2403 echo $output->upgradekey_form_page(new moodle_url('/admin/index.php', array('cache' => 0)));
2404 die();
2405 } else {
2406 // This should not happen.
2407 die('Upgrade locked');
2408 }
2409 }
2410 }
2411}
531381f9
DM
2412
2413/**
2414 * Helper procedure/macro for installing remote plugins at admin/index.php
2415 *
2416 * Does not return, always redirects or exits.
2417 *
2418 * @param array $installable list of \core\update\remote_info
2419 * @param bool $confirmed false: display the validation screen, true: proceed installation
2420 * @param string $heading validation screen heading
2421 * @param moodle_url|string|null $continue URL to proceed with installation at the validation screen
8726c07c 2422 * @param moodle_url|string|null $return URL to go back on cancelling at the validation screen
531381f9 2423 */
2d00be61 2424function upgrade_install_plugins(array $installable, $confirmed, $heading='', $continue=null, $return=null) {
b0fc7898 2425 global $CFG, $PAGE;
531381f9
DM
2426
2427 if (empty($return)) {
2428 $return = $PAGE->url;
2429 }
2430
b0fc7898
DM
2431 if (!empty($CFG->disableupdateautodeploy)) {
2432 redirect($return);
2433 }
2434
531381f9
DM
2435 if (empty($installable)) {
2436 redirect($return);
2437 }
2438
2439 $pluginman = core_plugin_manager::instance();
2440
2441 if ($confirmed) {
2442 // Installation confirmed at the validation results page.
2d00be61
DM
2443 if (!$pluginman->install_plugins($installable, true, true)) {
2444 throw new moodle_exception('install_plugins_failed', 'core_plugin', $return);
531381f9 2445 }
d9a5b810 2446
8726c07c 2447 // Always redirect to admin/index.php to perform the database upgrade.
d9a5b810
DM
2448 // Do not throw away the existing $PAGE->url parameters such as
2449 // confirmupgrade or confirmrelease if $PAGE->url is a superset of the
2450 // URL we must go to.
2451 $mustgoto = new moodle_url('/admin/index.php', array('cache' => 0, 'confirmplugincheck' => 0));
2452 if ($mustgoto->compare($PAGE->url, URL_MATCH_PARAMS)) {
2453 redirect($PAGE->url);
2454 } else {
2455 redirect($mustgoto);
2456 }
531381f9
DM
2457
2458 } else {
2459 $output = $PAGE->get_renderer('core', 'admin');
2460 echo $output->header();
2461 if ($heading) {
2462 echo $output->heading($heading, 3);
2463 }
2464 echo html_writer::start_tag('pre', array('class' => 'plugin-install-console'));
2d00be61 2465 $validated = $pluginman->install_plugins($installable, false, false);
531381f9
DM
2466 echo html_writer::end_tag('pre');
2467 if ($validated) {
4d7528f9 2468 echo $output->plugins_management_confirm_buttons($continue, $return);
531381f9 2469 } else {
4d7528f9 2470 echo $output->plugins_management_confirm_buttons(null, $return);
531381f9
DM
2471 }
2472 echo $output->footer();
2473 die();
2474 }
2475}
c4d69228
SL
2476/**
2477 * Method used to check the installed unoconv version.
2478 *
2479 * @param environment_results $result object to update, if relevant.
2480 * @return environment_results|null updated results or null if unoconv path is not executable.
2481 */
0eecf876 2482function check_unoconv_version(environment_results $result) {
c4d69228
SL
2483 global $CFG;
2484
0eecf876 2485 if (!during_initial_install() && !empty($CFG->pathtounoconv) && file_is_executable(trim($CFG->pathtounoconv))) {
84440fe3
SL
2486 $currentversion = 0;
2487 $supportedversion = 0.7;
0eecf876
AG
2488 $unoconvbin = \escapeshellarg($CFG->pathtounoconv);
2489 $command = "$unoconvbin --version";
2490 exec($command, $output);
84440fe3
SL
2491
2492 // If the command execution returned some output, then get the unoconv version.
6937d89b 2493 if ($output) {
6937d89b
SL
2494 foreach ($output as $response) {
2495 if (preg_match('/unoconv (\\d+\\.\\d+)/', $response, $matches)) {
2496 $currentversion = (float)$matches[1];
2497 }
2498 }
84440fe3
SL
2499 }
2500
2501 if ($currentversion < $supportedversion) {
2502 $result->setInfo('unoconv version not supported');
2503 $result->setStatus(false);
2504 return $result;
c4d69228
SL
2505 }
2506 }
2507 return null;
2508}
ab3759ff
CB
2509
2510/**
89556019 2511 * Checks for up-to-date TLS libraries. NOTE: this is not currently used, see MDL-57262.
ab3759ff
CB
2512 *
2513 * @param environment_results $result object to update, if relevant.
2514 * @return environment_results|null updated results or null if unoconv path is not executable.
2515 */
2516function check_tls_libraries(environment_results $result) {
2517 global $CFG;
2518
4a1165ba
DM
2519 if (!function_exists('curl_version')) {
2520 $result->setInfo('cURL PHP extension is not installed');
2521 $result->setStatus(false);
2522 return $result;
2523 }
2524
ab3759ff
CB
2525 if (!\core\upgrade\util::validate_php_curl_tls(curl_version(), PHP_ZTS)) {
2526 $result->setInfo('invalid ssl/tls configuration');
2527 $result->setStatus(false);
2528 return $result;
2529 }
2530
2531 if (!\core\upgrade\util::can_use_tls12(curl_version(), php_uname('r'))) {
2532 $result->setInfo('ssl/tls configuration not supported');
2533 $result->setStatus(false);
2534 return $result;
2535 }
2536
2537 return null;
2538}
f2330472
AA
2539
2540/**
2541 * Check if recommended version of libcurl is installed or not.
2542 *
2543 * @param environment_results $result object to update, if relevant.
2544 * @return environment_results|null updated results or null.
2545 */
2546function check_libcurl_version(environment_results $result) {
2547
acefc7d9
VZ
2548 if (!function_exists('curl_version')) {
2549 $result->setInfo('cURL PHP extension is not installed');
2550 $result->setStatus(false);
2551 return $result;
2552 }
2553
f2330472
AA
2554 // Supported version and version number.
2555 $supportedversion = 0x071304;
2556 $supportedversionstring = "7.19.4";
2557
2558 // Installed version.
2559 $curlinfo = curl_version();
2560 $currentversion = $curlinfo['version_number'];
2561
2562 if ($currentversion < $supportedversion) {
2563 // Test fail.
2564 // Set info, we want to let user know how to resolve the problem.
2565 $result->setInfo('Libcurl version check');
2566 $result->setNeededVersion($supportedversionstring);
2567 $result->setCurrentVersion($curlinfo['version']);
2568 $result->setStatus(false);
2569 return $result;
2570 }
2571
2572 return null;
2573}
31bd1023
DM
2574
2575/**
2576 * Fix how auth plugins are called in the 'config_plugins' table.
2577 *
2578 * For legacy reasons, the auth plugins did not always use their frankenstyle
2579 * component name in the 'plugin' column of the 'config_plugins' table. This is
2580 * a helper function to correctly migrate the legacy settings into the expected
2581 * and consistent way.
2582 *
2583 * @param string $plugin the auth plugin name such as 'cas', 'manual' or 'mnet'
2584 */
2585function upgrade_fix_config_auth_plugin_names($plugin) {
2586 global $CFG, $DB, $OUTPUT;
2587
2588 $legacy = (array) get_config('auth/'.$plugin);
2589 $current = (array) get_config('auth_'.$plugin);
2590
2591 // I don't want to rely on array_merge() and friends here just in case
2592 // there was some crazy setting with a numerical name.
2593
2594 if ($legacy) {
2595 $new = $legacy;
2596 } else {
2597 $new = [];
2598 }
2599
2600 if ($current) {
2601 foreach ($current as $name => $value) {
2602 if (isset($legacy[$name]) && ($legacy[$name] !== $value)) {
2603 // No need to pollute the output during unit tests.
2604 if (!empty($CFG->upgraderunning)) {
2605 $message = get_string('settingmigrationmismatch', 'core_auth', [
2606 'plugin' => 'auth_'.$plugin,
2607 'setting' => s($name),
2608 'legacy' => s($legacy[$name]),
2609 'current' => s($value),
2610 ]);
2611 echo $OUTPUT->notification($message, \core\output\notification::NOTIFY_ERROR);
2612
2613 upgrade_log(UPGRADE_LOG_NOTICE, 'auth_'.$plugin, 'Setting values mismatch detected',
2614 'SETTING: '.$name. ' LEGACY: '.$legacy[$name].' CURRENT: '.$value);
2615 }
2616 }
2617
2618 $new[$name] = $value;
2619 }
2620 }
2621
2622 foreach ($new as $name => $value) {
2623 set_config($name, $value, 'auth_'.$plugin);
2624 unset_config($name, 'auth/'.$plugin);
2625 }
2626}
2b948c20
DM
2627
2628/**
2629 * Populate the auth plugin settings with defaults if needed.
2630 *
2631 * As a result of fixing the auth plugins config storage, many settings would
2632 * be falsely reported as new ones by admin/upgradesettings.php. We do not want
2633 * to confuse admins so we try to reduce the bewilderment by pre-populating the
2634 * config_plugins table with default values. This should be done only for
2635 * disabled auth methods. The enabled methods have their settings already
2636 * stored, so reporting actual new settings for them is valid.
2637 *
2638 * @param string $plugin the auth plugin name such as 'cas', 'manual' or 'mnet'
2639 */
2640function upgrade_fix_config_auth_plugin_defaults($plugin) {
2641 global $CFG;
2642
2643 $pluginman = core_plugin_manager::instance();
2644 $enabled = $pluginman->get_enabled_plugins('auth');
2645
2646 if (isset($enabled[$plugin])) {
2647 // Do not touch settings of enabled auth methods.
2648 return;
2649 }
2650
2651 // We can't directly use {@link core\plugininfo\auth::load_settings()} here
2652 // because the plugins are not fully upgraded yet. Instead, we emulate what
2653 // that method does. We fetch a temporary instance of the plugin's settings
2654 // page to get access to the settings and their defaults. Note we are not
2655 // adding that temporary instance into the admin tree. Yes, this is a hack.
2656
2657 $plugininfo = $pluginman->get_plugin_info('auth_'.$plugin);
2658 $adminroot = admin_get_root();
2659 $ADMIN = $adminroot;
2660 $auth = $plugininfo;
2661
2662 $section = $plugininfo->get_settings_section_name();
2663 $settingspath = $plugininfo->full_path('settings.php');
2664
2665 if (file_exists($settingspath)) {
2666 $settings = new admin_settingpage($section, 'Emulated settings page for auth_'.$plugin, 'moodle/site:config');
2667 include($settingspath);
2668
2669 if ($settings) {
2b948c20
DM
2670 admin_apply_default_settings($settings, false);
2671 }
2672 }
2673}
e46fde42
JO
2674
2675/**
2676 * Search for a given theme in any of the parent themes of a given theme.
2677 *
2678 * @param string $needle The name of the theme you want to search for
2679 * @param string $themename The name of the theme you want to search for
2680 * @param string $checkedthemeforparents The name of all the themes already checked
2681 * @return bool True if found, false if not.
2682 */
2683function upgrade_theme_is_from_family($needle, $themename, $checkedthemeforparents = []) {
2684 global $CFG;
2685
2686 // Once we've started checking a theme, don't start checking it again. Prevent recursion.
2687 if (!empty($checkedthemeforparents[$themename])) {
2688 return false;
2689 }
2690 $checkedthemeforparents[$themename] = true;
2691
2692 if ($themename == $needle) {
2693 return true;
2694 }
2695
2696 if ($themedir = upgrade_find_theme_location($themename)) {
2697 $THEME = new stdClass();
2698 require($themedir . '/config.php');
2699 $theme = $THEME;
2700 } else {
2701 return false;
2702 }
2703
2704 if (empty($theme->parents)) {
2705 return false;
2706 }
2707
2708 // Recursively search through each parent theme.
2709 foreach ($theme->parents as $parent) {
2710 if (upgrade_theme_is_from_family($needle, $parent, $checkedthemeforparents)) {
2711 return true;
2712 }
2713 }
2714 return false;
2715}
2716
2717/**
2718 * Finds the theme location and verifies the theme has all needed files.
2719 *
2720 * @param string $themename The name of the theme you want to search for
2721 * @return string full dir path or null if not found
2722 * @see \theme_config::find_theme_location()
2723 */
2724function upgrade_find_theme_location($themename) {
2725 global $CFG;
2726
2727 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
2728 $dir = "$CFG->dirroot/theme/$themename";
2729 } else if (!empty($CFG->themedir) and file_exists("$CFG->themedir/$themename/config.php")) {
2730 $dir = "$CFG->themedir/$themename";
2731 } else {
2732 return null;
2733 }
2734
2735 return $dir;
2736}