From 48e114a51794b8c56a5b882b62e9ea418d3f3ec8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Mon, 21 Jan 2013 16:51:28 +0100 Subject: [PATCH] MDL-37596 optionally enable CLI maintenance later --- admin/cli/maintenance.php | 56 ++++++++++++++++++++++++++++++++------- lang/en/admin.php | 3 +++ lib/adminlib.php | 28 ++++++++++++++++++++ lib/outputrenderers.php | 21 +++++++++++++++ lib/setup.php | 13 +++++++++ lib/setuplib.php | 6 ++--- 6 files changed, 114 insertions(+), 13 deletions(-) diff --git a/admin/cli/maintenance.php b/admin/cli/maintenance.php index 01bfcc631ec40..b267a039c310d 100644 --- a/admin/cli/maintenance.php +++ b/admin/cli/maintenance.php @@ -1,5 +1,4 @@ . /** - * Enable or disable maintenance mode + * Enable or disable maintenance mode. * * @package core * @subpackage cli @@ -26,12 +25,13 @@ define('CLI_SCRIPT', true); -require(dirname(dirname(dirname(__FILE__))).'/config.php'); -require_once($CFG->libdir.'/clilib.php'); // cli only functions +require(__DIR__.'/../../config.php'); +require_once("$CFG->libdir/clilib.php"); +require_once("$CFG->libdir/adminlib.php"); -// now get cli options -list($options, $unrecognized) = cli_get_params(array('enable'=>false, 'disable'=>false, 'help'=>false), +// Now get cli options. +list($options, $unrecognized) = cli_get_params(array('enable'=>false, 'enablelater'=>0, 'enableold'=>false, 'disable'=>false, 'help'=>false), array('h'=>'help')); if ($unrecognized) { @@ -45,12 +45,14 @@ Current status displayed if not option specified. Options: ---enable Enable maintenance mode +--enable Enable CLI maintenance mode +--enablelater=MINUTES Number of minutes before entering CLI maintenance mode +--enableold Enable legacy half-maintenance mode --disable Disable maintenance mode -h, --help Print out this help Example: -\$sudo -u www-data /usr/bin/php admin/cli/maintenance.php +\$ sudo -u www-data /usr/bin/php admin/cli/maintenance.php "; //TODO: localize - to be translated later when everything is finished echo $help; @@ -59,18 +61,52 @@ cli_heading(get_string('sitemaintenancemode', 'admin')." ($CFG->wwwroot)"); -if ($options['enable']) { +if ($options['enablelater']) { + if (file_exists("$CFG->dataroot/climaintenance.html")) { + // Already enabled, sorry. + echo get_string('clistatusenabled', 'admin')."\n"; + return 1; + } + + $time = time() + ($options['enablelater']*60); + set_config('maintenance_later', $time); + + echo get_string('clistatusenabledlater', 'admin', userdate($time))."\n"; + return 0; + +} else if ($options['enable']) { + if (file_exists("$CFG->dataroot/climaintenance.html")) { + // The maintenance is already enabled, nothing to do. + } else { + enable_cli_maintenance_mode(); + } + 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'); echo get_string('sitemaintenanceon', 'admin')."\n"; exit(0); + } else if ($options['disable']) { set_config('maintenance_enabled', 0); + unset_config('maintenance_later'); + if (file_exists("$CFG->dataroot/climaintenance.html")) { + unlink("$CFG->dataroot/climaintenance.html"); + } echo get_string('sitemaintenanceoff', 'admin')."\n"; exit(0); } -if (!empty($CFG->maintenance_enabled)) { +if (!empty($CFG->maintenance_enabled) or file_exists("$CFG->dataroot/climaintenance.html")) { echo get_string('clistatusenabled', 'admin')."\n"; + +} else if (isset($CFG->maintenance_later)) { + echo get_string('clistatusenabledlater', 'admin', userdate($CFG->maintenance_later))."\n"; + } else { echo get_string('clistatusdisabled', 'admin')."\n"; } diff --git a/lang/en/admin.php b/lang/en/admin.php index df948f3dc4974..12854e1e2eb65 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -105,6 +105,7 @@ $string['cliincorrectvalueretry'] = 'Incorrect value, please retry'; $string['clistatusdisabled'] = 'Status: disabled'; $string['clistatusenabled'] = 'Status: enabled'; +$string['clistatusenabledlater'] = 'status: CLI maintenance mode will be enabled on {$a}'; $string['clitypevalue'] = 'type value'; $string['clitypevaluedefault'] = 'type value, press Enter to use default value ({$a})'; $string['cliunknowoption'] = 'Unrecognised options: @@ -664,6 +665,7 @@ $string['loglifetime'] = 'Keep logs for'; $string['longtimewarning'] = 'Please note that this process can take a long time.'; $string['maintenancemode'] = 'In maintenance mode'; +$string['maintenancemodeisscheduled'] = 'Site is switching to maintenance mode in {$a} minutes'; $string['maintfileopenerror'] = 'Error opening maintenance files!'; $string['maintinprogress'] = 'Maintenance is in progress...'; $string['manageformats'] = 'Manage course formats'; @@ -955,6 +957,7 @@ $string['sitemaintenancemode'] = 'Maintenance mode'; $string['sitemaintenanceoff'] = 'Maintenance mode has been disabled and the site is running normally again'; $string['sitemaintenanceon'] = 'Your site is currently in maintenance mode (only admins can log in or use the site).'; +$string['sitemaintenanceoncli'] = 'Your site is currently in CLI maintenance mode, no web access is allowed.'; $string['sitemaintenancewarning'] = 'Your site is currently in maintenance mode (only admins can log in). To return this site to normal operation, disable maintenance mode.'; $string['sitemaintenancewarning2'] = 'Your site is currently in maintenance mode (only admins can log in). To return this site to normal operation, disable maintenance mode.'; $string['sitepolicies'] = 'Site policies'; diff --git a/lib/adminlib.php b/lib/adminlib.php index 17f22fc315266..736532c6a244e 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -679,6 +679,34 @@ function is_dataroot_insecure($fetchtest=false) { return INSECURE_DATAROOT_WARNING; } +/** + * Enables CLI maintenance mode by creating new dataroot/climaintenance.html file. + */ +function enable_cli_maintenance_mode() { + global $CFG; + + if (file_exists("$CFG->dataroot/climaintenance.html")) { + unlink("$CFG->dataroot/climaintenance.html"); + } + + if (isset($CFG->maintenance_message) and !html_is_blank($CFG->maintenance_message)) { + $data = $CFG->maintenance_message; + $data = bootstrap_renderer::early_error_content($data, null, null, null); + $data = bootstrap_renderer::plain_page(get_string('sitemaintenance', 'admin'), $data); + + } else if (file_exists("$CFG->dataroot/climaintenance.template.html")) { + $data = file_get_contents("$CFG->dataroot/climaintenance.template.html"); + + } else { + $data = get_string('sitemaintenance', 'admin'); + $data = bootstrap_renderer::early_error_content($data, null, null, null); + $data = bootstrap_renderer::plain_page(get_string('sitemaintenance', 'admin'), $data); + } + + file_put_contents("$CFG->dataroot/climaintenance.html", $data); + chmod("$CFG->dataroot/climaintenance.html", $CFG->filepermissions); +} + /// CLASS DEFINITIONS ///////////////////////////////////////////////////////// diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 76fe99dad11dc..c5633ba46049d 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -428,6 +428,27 @@ public function standard_top_of_body_html() { if (!empty($CFG->additionalhtmltopofbody)) { $output .= "\n".$CFG->additionalhtmltopofbody; } + $output .= $this->maintenance_warning(); + return $output; + } + + /** + * Scheduled maintenance warning message. + * + * Note: This is a nasty hack to display maintenance notice, this should be moved + * to some general notification area once we have it. + * + * @return string + */ + public function maintenance_warning() { + global $CFG; + + $output = ''; + if (isset($CFG->maintenance_later) and $CFG->maintenance_later > time()) { + $output .= $this->box_start('errorbox maintenancewarning'); + $output .= get_string('maintenancemodeisscheduled', 'admin', (int)(($CFG->maintenance_later-time())/60)); + $output .= $this->box_end(); + } return $output; } diff --git a/lib/setup.php b/lib/setup.php index f25301f14aeaf..8cf275312e6cb 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -888,6 +888,19 @@ function stripslashes_deep($value) { } } +// 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()) { + if (!file_exists("$CFG->dataroot/climaintenance.html")) { + require_once("$CFG->libdir/adminlib.php"); + enable_cli_maintenance_mode(); + } + unset_config('maintenance_later'); + if (AJAX_SCRIPT) { + die; + } else if (!CLI_SCRIPT) { + redirect(new moodle_url('/')); + } +} // note: we can not block non utf-8 installations here, because empty mysql database // might be converted to utf-8 in admin/index.php during installation diff --git a/lib/setuplib.php b/lib/setuplib.php index 3ef5396f818f9..88fd5fdad3d2c 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -1596,15 +1596,15 @@ public static function early_redirect_message($encodedurl, $message, $delay) { * @param string $meta meta tag * @return string html page */ - protected static function plain_page($title, $content, $meta = '') { + public static function plain_page($title, $content, $meta = '') { if (function_exists('get_string') && function_exists('get_html_lang')) { $htmllang = get_html_lang(); } else { $htmllang = ''; } - return ' - + return ' + '.$meta.'