maintenance.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * Enable or disable maintenance mode.
  18. *
  19. * @package core
  20. * @subpackage cli
  21. * @copyright 2009 Petr Skoda (http://skodak.org)
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. define('CLI_SCRIPT', true);
  25. require(__DIR__.'/../../config.php');
  26. require_once("$CFG->libdir/clilib.php");
  27. require_once("$CFG->libdir/adminlib.php");
  28. // Now get cli options.
  29. list($options, $unrecognized) = cli_get_params(array('enable'=>false, 'enablelater'=>0, 'enableold'=>false, 'disable'=>false, 'help'=>false),
  30. array('h'=>'help'));
  31. if ($unrecognized) {
  32. $unrecognized = implode("\n ", $unrecognized);
  33. cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  34. }
  35. if ($options['help']) {
  36. $help =
  37. "Maintenance mode settings.
  38. Current status displayed if not option specified.
  39. Options:
  40. --enable Enable CLI maintenance mode
  41. --enablelater=MINUTES Number of minutes before entering CLI maintenance mode
  42. --enableold Enable legacy half-maintenance mode
  43. --disable Disable maintenance mode
  44. -h, --help Print out this help
  45. Example:
  46. \$ sudo -u www-data /usr/bin/php admin/cli/maintenance.php
  47. "; //TODO: localize - to be translated later when everything is finished
  48. echo $help;
  49. die;
  50. }
  51. cli_heading(get_string('sitemaintenancemode', 'admin')." ($CFG->wwwroot)");
  52. if ($options['enablelater']) {
  53. if (file_exists("$CFG->dataroot/climaintenance.html")) {
  54. // Already enabled, sorry.
  55. echo get_string('clistatusenabled', 'admin')."\n";
  56. return 1;
  57. }
  58. $time = time() + ($options['enablelater']*60);
  59. set_config('maintenance_later', $time);
  60. echo get_string('clistatusenabledlater', 'admin', userdate($time))."\n";
  61. return 0;
  62. } else if ($options['enable']) {
  63. if (file_exists("$CFG->dataroot/climaintenance.html")) {
  64. // The maintenance is already enabled, nothing to do.
  65. } else {
  66. enable_cli_maintenance_mode();
  67. }
  68. set_config('maintenance_enabled', 0);
  69. unset_config('maintenance_later');
  70. echo get_string('sitemaintenanceoncli', 'admin')."\n";
  71. exit(0);
  72. } else if ($options['enableold']) {
  73. set_config('maintenance_enabled', 1);
  74. unset_config('maintenance_later');
  75. echo get_string('sitemaintenanceon', 'admin')."\n";
  76. exit(0);
  77. } else if ($options['disable']) {
  78. set_config('maintenance_enabled', 0);
  79. unset_config('maintenance_later');
  80. if (file_exists("$CFG->dataroot/climaintenance.html")) {
  81. unlink("$CFG->dataroot/climaintenance.html");
  82. }
  83. echo get_string('sitemaintenanceoff', 'admin')."\n";
  84. exit(0);
  85. }
  86. if (!empty($CFG->maintenance_enabled) or file_exists("$CFG->dataroot/climaintenance.html")) {
  87. echo get_string('clistatusenabled', 'admin')."\n";
  88. } else if (isset($CFG->maintenance_later)) {
  89. echo get_string('clistatusenabledlater', 'admin', userdate($CFG->maintenance_later))."\n";
  90. } else {
  91. echo get_string('clistatusdisabled', 'admin')."\n";
  92. }