diff --git a/admin/cli/maintenance.php b/admin/cli/maintenance.php index b267a039c31..b77cdcf537a 100644 --- a/admin/cli/maintenance.php +++ b/admin/cli/maintenance.php @@ -69,7 +69,7 @@ if ($options['enablelater']) { } $time = time() + ($options['enablelater']*60); - set_config('maintenance_later', $time); + set_config('maintenance_later', $time, null, true); echo get_string('clistatusenabledlater', 'admin', userdate($time))."\n"; return 0; @@ -77,23 +77,32 @@ if ($options['enablelater']) { } else if ($options['enable']) { if (file_exists("$CFG->dataroot/climaintenance.html")) { // The maintenance is already enabled, nothing to do. - } else { - enable_cli_maintenance_mode(); + exit(0); + } + enable_cli_maintenance_mode(); + set_config('maintenance_enabled', 'cli mode', null, true); + + if (isset($CFG->maintenance_later)) { + unset_config('maintenance_later', null, true); } - set_config('maintenance_enabled', 0); - unset_config('maintenance_later'); echo get_string('sitemaintenanceoncli', 'admin')."\n"; exit(0); } else if ($options['enableold']) { - set_config('maintenance_enabled', 1); - unset_config('maintenance_later'); + set_config('maintenance_enabled', 1, null, true); + if (isset($CFG->maintenance_later)) { + unset_config('maintenance_later', null, true); + } echo get_string('sitemaintenanceon', 'admin')."\n"; exit(0); } else if ($options['disable']) { - set_config('maintenance_enabled', 0); - unset_config('maintenance_later'); + if ($CFG->maintenance_enabled !== '0') { + set_config('maintenance_enabled', 0, null, true); + } + if (isset($CFG->maintenance_later)) { + unset_config('maintenance_later', null, true); + } if (file_exists("$CFG->dataroot/climaintenance.html")) { unlink("$CFG->dataroot/climaintenance.html"); } diff --git a/public/lib/moodlelib.php b/public/lib/moodlelib.php index c140d575667..102e4afe6ac 100644 --- a/public/lib/moodlelib.php +++ b/public/lib/moodlelib.php @@ -934,14 +934,23 @@ function html_is_blank($string) { * @param string|int|bool|null $value the value to set (without magic quotes), * null to unset the value * @param string $plugin (optional) the plugin scope, default null + * @param boolean $log (optional) should this emit to the config log * @return bool true or exception */ -function set_config($name, $value, $plugin = null) { +function set_config($name, $value, $plugin = null, bool $log = false) { global $CFG, $DB; // Redirect to appropriate handler when value is null. if ($value === null) { - return unset_config($name, $plugin); + return unset_config($name, $plugin, $log); + } + + if ($log) { + $prev = get_config($plugin, $name); + if ($prev === false) { + $prev = null; + } + add_to_config_log($name, $prev, $value, $plugin); } // Set variables determining conditions and where to store the new config. @@ -1099,11 +1108,20 @@ function get_config($plugin, $name = null) { * * @param string $name the key to set * @param string $plugin (optional) the plugin scope + * @param boolean $log (optional) should this emit to the config log * @return boolean whether the operation succeeded. */ -function unset_config($name, $plugin=null) { +function unset_config($name, $plugin = null, bool $log = false) { global $CFG, $DB; + if ($log) { + $prev = get_config($plugin, $name); + if ($prev === false) { + $prev = null; + } + add_to_config_log($name, $prev, null, $plugin); + } + if (empty($plugin)) { unset($CFG->$name); $DB->delete_records('config', array('name' => $name)); diff --git a/public/lib/setup.php b/public/lib/setup.php index b79c7782a0e..575bd41eb03 100644 --- a/public/lib/setup.php +++ b/public/lib/setup.php @@ -1160,15 +1160,27 @@ if (!empty($_SERVER['HTTP_USER_AGENT']) and strpos($_SERVER['HTTP_USER_AGENT'], // Switch to CLI maintenance mode if required, we need to do it here after all the settings are initialised. if (isset($CFG->maintenance_later) and $CFG->maintenance_later <= time()) { + + // Because maintenance_later is triggered by any potentially real non admin user + // who just happened to be the first to load a page after the time is due, we do + // this simple workaround so add_to_config_log doesn't log it as them. + \core\session\manager::write_close(); + $USER->id = 0; + if (!file_exists("$CFG->dataroot/climaintenance.html")) { require_once("$CFG->libdir/adminlib.php"); + set_config('maintenance_enabled', 'cli mode', null, true); enable_cli_maintenance_mode(); } - unset_config('maintenance_later'); + if (isset($CFG->maintenance_later)) { + unset_config('maintenance_later', null, true); + } if (AJAX_SCRIPT) { die; } else if (!CLI_SCRIPT) { - redirect(new moodle_url('/')); + // We redirect to ourselves to reload the page to get a fresh bootstrap + // so that we get the maintenance page which is earlier in setup. + redirect(new moodle_url($ME)); } }