Skip to content

Commit

Permalink
Make maintenance mode a variable instead of a config. Then create
Browse files Browse the repository at this point in the history
links on the Admin > Maintenance page to allow you to turn it on and
off.  This should be efficient since we cache all vars and look them
up on every request anyway.

This also allows us to have the Fix task enable maintenance mode while
it's running which greatly reduces the chances that somebody will come
along and hork the database while we're tinkering with MPTT pointers.

Fixes ticket #1259.
  • Loading branch information
bharat committed Aug 2, 2010
1 parent 49eb3e3 commit c33b24c
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 23 deletions.
7 changes: 0 additions & 7 deletions application/config/config.php
Expand Up @@ -151,10 +151,3 @@
array(MODPATH . "gallery_unit_test",
MODPATH . "unit_test"));
}

/**
* Setting the maintenance_mode to block all non administrative access. In
* this mode a user can attempt to logon, but will be unable to access anything.
* The application will behave normally if an adminstrator logs on.
*/
//$config["maintenance_mode"] = true;
6 changes: 6 additions & 0 deletions modules/gallery/controllers/admin_maintenance.php
Expand Up @@ -226,4 +226,10 @@ public function run($task_id) {
"done" => (bool) $task->done)));
}
}

public function maintenance_mode($value) {
access::verify_csrf();
module::set_var("gallery", "maintenance_mode", $value);
url::redirect("admin/maintenance");
}
}
6 changes: 3 additions & 3 deletions modules/gallery/helpers/gallery.php
Expand Up @@ -25,9 +25,9 @@ class gallery_Core {
* down for maintenance" page.
*/
static function maintenance_mode() {
$maintenance_mode = Kohana::config("core.maintenance_mode", false, false);

if (Router::$controller != "login" && !empty($maintenance_mode) && !identity::active_user()->admin) {
if (Router::$controller != "login" &&
module::get_var("gallery", "maintenance_mode", false) &&
!identity::active_user()->admin) {
Router::$controller = "maintenance";
Router::$controller_path = MODPATH . "gallery/controllers/maintenance.php";
Router::$method = "index";
Expand Down
10 changes: 8 additions & 2 deletions modules/gallery/helpers/gallery_installer.php
Expand Up @@ -295,7 +295,8 @@ static function install() {
module::set_var("gallery", "credits", (string) $powered_by_string);
module::set_var("gallery", "simultaneous_upload_limit", 5);
module::set_var("gallery", "admin_area_timeout", 90 * 60);
module::set_version("gallery", 30);
module::set_var("gallery", "maintenance_mode", 0);
module::set_version("gallery", 31);
}

static function upgrade($version) {
Expand Down Expand Up @@ -554,7 +555,12 @@ static function upgrade($version) {
if ($version == 29) {
$db->query("ALTER TABLE {caches} ADD KEY (`key`);");
module::set_version("gallery", $version = 30);
}
}

if ($version == 30) {
module::set_var("gallery", "maintenance_mode", 0);
module::set_version("gallery", $version = 31);
}
}

static function uninstall() {
Expand Down
9 changes: 7 additions & 2 deletions modules/gallery/helpers/gallery_task.php
Expand Up @@ -56,8 +56,8 @@ static function available_tasks() {
$tasks[] = Task_Definition::factory()
->callback("gallery_task::fix")
->name(t("Fix your Gallery"))
->description(t("Fix up a variety of little problems that might be causing " .
"your Gallery to act a little weird"))
->description(t("Fix a variety of problems that might cause your Gallery to act " .
"strangely. Requires maintenance mode."))
->severity(log::SUCCESS);

return $tasks;
Expand Down Expand Up @@ -343,6 +343,10 @@ static function fix($task) {
$completed = $task->get("completed");
$state = $task->get("state");

if (!module::get_var("gallery", "maintenance_mode")) {
module::set_var("gallery", "maintenance_mode", 1);
}

// This is a state machine that checks each item in the database. It verifies the following
// attributes for an item.
// 1. Left and right MPTT pointers are correct
Expand Down Expand Up @@ -543,6 +547,7 @@ static function fix($task) {
$task->done = true;
$task->state = "success";
$task->percent_complete = 100;
module::set_var("gallery", "maintenance_mode", 0);
} else {
$task->percent_complete = round(100 * $completed / $total);
}
Expand Down
8 changes: 5 additions & 3 deletions modules/gallery/libraries/Theme_View.php
Expand Up @@ -46,9 +46,11 @@ public function __construct($name, $page_type, $page_subtype) {
$this->set_global("thumb_proportion", $this->thumb_proportion());
}

$maintenance_mode = Kohana::config("core.maintenance_mode", false, false);
if ($maintenance_mode) {
message::warning(t("This site is currently in maintenance mode"));
if (module::get_var("gallery", "maintenance_mode", false)) {
if (identity::active_user()->admin) {
message::warning(t("This site is currently in maintenance mode. Visit the <a href=\"%maintenance_url\">maintenance page</a>", array("maintenance_url" => url::site("admin/maintenance"))));
} else
message::warning(t("This site is currently in maintenance mode."));
}
}

Expand Down
2 changes: 1 addition & 1 deletion modules/gallery/module.info
@@ -1,3 +1,3 @@
name = "Gallery 3"
description = "Gallery core application"
version = 30
version = 31
26 changes: 21 additions & 5 deletions modules/gallery/views/admin_maintenance.html.php
@@ -1,13 +1,29 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<div id="g-admin-maintenance" class="g-block">
<h1> <?= t("Maintenance tasks") ?> </h1>
<p>
<?= t("Occasionally your Gallery will require some maintenance. Here are some tasks you can use to keep it running smoothly.") ?>
</p>
<h1> <?= t("Maintenance") ?> </h1>
<div class="g-block-content">
<div id="g-maintenance-mode">
<?= t("When you're performing maintenance on your Gallery, you can enable <b>maintenance mode</b> which prevents any non-admin from accessing your Gallery. Some of the tasks below will automatically put your Gallery in maintenance mode for you.") ?>
<ul id="g-action-status" class="g-message-block">
<? if (module::get_var("gallery", "maintenance_mode")): ?>
<li class="g-warning">
<?= t("Maintenance mode is <b>on</b>. Non admins cannot access your Gallery. <a href=\"%enable_maintenance_mode_url\">Turn off maintenance mode</a>", array("enable_maintenance_mode_url" => url::site("admin/maintenance/maintenance_mode/0?csrf=$csrf"))) ?>
</li>
<? else: ?>
<li class="g-info">
<?= t("Maintenance mode is off. User access is permitted. <a href=\"%enable_maintenance_mode_url\">Turn on maintenance mode</a>", array("enable_maintenance_mode_url" => url::site("admin/maintenance/maintenance_mode/1?csrf=$csrf"))) ?>
</li>
<? endif ?>
</ul>
</div>
</div>

<div class="g-block-content">
<div id="g-available-tasks">
<h2> <?= t("Available tasks") ?> </h2>
<h2> <?= t("Maintenance tasks") ?> </h2>
<p>
<?= t("Occasionally your Gallery will require some maintenance. Here are some tasks you can use to keep it running smoothly.") ?>
</p>
<table>
<tr>
<th>
Expand Down

0 comments on commit c33b24c

Please sign in to comment.