From 10b28ebb08593ba75bf1055f17bef44be4681c5f Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Tue, 18 Feb 2025 11:40:01 +1100 Subject: [PATCH 1/2] MDL-84504 core: Allow setting config and logging with one call --- lib/moodlelib.php | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 5247b840e31..2716a923e1e 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -921,14 +921,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. @@ -1086,11 +1095,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)); From b7a3d429e2ca399cfe3f11a3df282fa4631452ce Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Tue, 18 Feb 2025 11:40:35 +1100 Subject: [PATCH 2/2] MDL-84504 cli: Add config logging for cli maintenance mode --- admin/cli/maintenance.php | 27 ++++++++++++++++++--------- lib/setup.php | 16 ++++++++++++++-- 2 files changed, 32 insertions(+), 11 deletions(-) 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/lib/setup.php b/lib/setup.php index a42ef9cd643..04210378a94 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -1161,15 +1161,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)); } }