Issue #22 - Self-refresh maintenance page every 5 minutes or the outage duration, whatever is lower.

This commit is contained in:
Daniel Thee Roperto
2016-11-14 15:43:57 +11:00
parent 3360d357d4
commit 5961e82b85
4 changed files with 89 additions and 3 deletions

View File

@@ -42,7 +42,9 @@ defined('MOODLE_INTERNAL') || die();
class maintenance_static_page {
/**
* Creates a page based on the given outage.
*
* @param outage|null $outage
*
* @return maintenance_static_page
* @throws coding_exception
*/
@@ -63,12 +65,18 @@ class maintenance_static_page {
$html = $data['contents'];
}
return self::create_from_html($html);
$page = self::create_from_html($html);
if (!is_null($outage)) {
$page->set_max_refresh_time($outage->get_duration_planned());
}
return $page;
}
/**
* Creates a page based on the given HTML.
*
* @param string|null $html
*
* @return maintenance_static_page
* @throws coding_exception
*/
@@ -96,7 +104,9 @@ class maintenance_static_page {
/**
* maintenance_static_page constructor.
*
* @param DOMDocument|null $dom
*
* @throws coding_exception
*/
protected function __construct($dom) {
@@ -117,4 +127,15 @@ class maintenance_static_page {
public function get_io() {
return $this->generator->get_io();
}
/**
* Sets the maximum amount of seconds to auto refresh the static page.
* @param int $maxsecs
*/
public function set_max_refresh_time($maxsecs) {
$current = $this->generator->get_refresh_time();
if ($maxsecs < $current) {
$this->generator->set_refresh_time($maxsecs);
}
}
}

View File

@@ -48,6 +48,9 @@ class maintenance_static_page_generator {
/** @var maintenance_static_page_io */
protected $io;
/** @var int */
protected $refreshtime = 300;
/**
* maintenance_static_page_generator constructor.
*
@@ -74,6 +77,7 @@ class maintenance_static_page_generator {
$this->io->create_resources_path();
$this->remove_script_tags();
$this->add_meta_refresh();
$this->update_link_stylesheet();
$this->update_link_favicon();
$this->update_images();
@@ -265,4 +269,29 @@ class maintenance_static_page_generator {
}
return $matches;
}
private function add_meta_refresh() {
$meta = $this->dom->createElement('meta');
$meta->setAttribute('http-equiv', 'refresh');
$meta->setAttribute('content', $this->refreshtime);
$head = $this->dom->getElementsByTagName('head')[0];
if ($head) {
$head->appendChild($meta);
}
}
/**
* @return int
*/
public function get_refresh_time() {
return $this->refreshtime;
}
/**
* @param int $refreshtime
*/
public function set_refresh_time($refreshtime) {
$this->refreshtime = $refreshtime;
}
}