From f63aed74592b49bc56cd452a4462b8145da411fa Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Thu, 11 Sep 2025 18:13:19 +1000 Subject: [PATCH] MDL-84504 cli: Add config logging for cli maintenance mode --- admin/cli/maintenance.php | 27 ++++++++++++++++++--------- public/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/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)); } }