Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Enable or disable maintenance mode.
  19   *
  20   * @package    core
  21   * @subpackage cli
  22   * @copyright  2009 Petr Skoda (http://skodak.org)
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  define('CLI_SCRIPT', true);
  27  
  28  require(__DIR__.'/../../config.php');
  29  require_once("$CFG->libdir/clilib.php");
  30  require_once("$CFG->libdir/adminlib.php");
  31  
  32  
  33  // Now get cli options.
  34  list($options, $unrecognized) = cli_get_params(array('enable'=>false, 'enablelater'=>0, 'enableold'=>false, 'disable'=>false, 'help'=>false),
  35                                                 array('h'=>'help'));
  36  
  37  if ($unrecognized) {
  38      $unrecognized = implode("\n  ", $unrecognized);
  39      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  40  }
  41  
  42  if ($options['help']) {
  43      $help =
  44  "Maintenance mode settings.
  45  Current status displayed if not option specified.
  46  
  47  Options:
  48  --enable              Enable CLI maintenance mode
  49  --enablelater=MINUTES Number of minutes before entering CLI maintenance mode
  50  --enableold           Enable legacy half-maintenance mode
  51  --disable             Disable maintenance mode
  52  -h, --help            Print out this help
  53  
  54  Example:
  55  \$ sudo -u www-data /usr/bin/php admin/cli/maintenance.php
  56  "; //TODO: localize - to be translated later when everything is finished
  57  
  58      echo $help;
  59      die;
  60  }
  61  
  62  cli_heading(get_string('sitemaintenancemode', 'admin')." ($CFG->wwwroot)");
  63  
  64  if ($options['enablelater']) {
  65      if (file_exists("$CFG->dataroot/climaintenance.html")) {
  66          // Already enabled, sorry.
  67          echo get_string('clistatusenabled', 'admin')."\n";
  68          return 1;
  69      }
  70  
  71      $time = time() + ($options['enablelater']*60);
  72      set_config('maintenance_later', $time);
  73  
  74      echo get_string('clistatusenabledlater', 'admin', userdate($time))."\n";
  75      return 0;
  76  
  77  } else if ($options['enable']) {
  78      if (file_exists("$CFG->dataroot/climaintenance.html")) {
  79          // The maintenance is already enabled, nothing to do.
  80      } else {
  81          enable_cli_maintenance_mode();
  82      }
  83      set_config('maintenance_enabled', 0);
  84      unset_config('maintenance_later');
  85      echo get_string('sitemaintenanceoncli', 'admin')."\n";
  86      exit(0);
  87  
  88  } else if ($options['enableold']) {
  89      set_config('maintenance_enabled', 1);
  90      unset_config('maintenance_later');
  91      echo get_string('sitemaintenanceon', 'admin')."\n";
  92      exit(0);
  93  
  94  } else if ($options['disable']) {
  95      set_config('maintenance_enabled', 0);
  96      unset_config('maintenance_later');
  97      if (file_exists("$CFG->dataroot/climaintenance.html")) {
  98          unlink("$CFG->dataroot/climaintenance.html");
  99      }
 100      echo get_string('sitemaintenanceoff', 'admin')."\n";
 101      exit(0);
 102  }
 103  
 104  if (!empty($CFG->maintenance_enabled) or file_exists("$CFG->dataroot/climaintenance.html")) {
 105      echo get_string('clistatusenabled', 'admin')."\n";
 106  
 107  } else if (isset($CFG->maintenance_later)) {
 108      echo get_string('clistatusenabledlater', 'admin', userdate($CFG->maintenance_later))."\n";
 109  
 110  } else {
 111      echo get_string('clistatusdisabled', 'admin')."\n";
 112  }