mirror of
https://github.com/catalyst/moodle-auth_outage.git
synced 2026-05-17 05:48:43 +02:00
Issue #22 - Infopage now is not responsible for generating a static page anymore, hooked into maintenance_static_page.
This commit is contained in:
@@ -28,11 +28,8 @@ namespace auth_outage\local\controllers;
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\outage;
|
||||
use auth_outage\local\outagelib;
|
||||
use auth_outage\output\renderer;
|
||||
use coding_exception;
|
||||
use context_system;
|
||||
use file_exception;
|
||||
use invalid_state_exception;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
@@ -51,11 +48,6 @@ class infopage {
|
||||
*/
|
||||
private $outage;
|
||||
|
||||
/**
|
||||
* @var bool Flags if a static version of this page should be displayed (maintenance mode).
|
||||
*/
|
||||
private $static;
|
||||
|
||||
/**
|
||||
* infopage_controller constructor.
|
||||
* @param array $params Parameters to use or null to get from Moodle API (request).
|
||||
@@ -64,14 +56,12 @@ class infopage {
|
||||
if (is_null($params)) {
|
||||
$params = [
|
||||
'id' => optional_param('id', null, PARAM_INT),
|
||||
'static' => optional_param('static', false, PARAM_BOOL),
|
||||
'outage' => null,
|
||||
];
|
||||
} else {
|
||||
$defaults = [
|
||||
'id' => null,
|
||||
'outage' => null,
|
||||
'static' => false,
|
||||
];
|
||||
$params = array_merge($defaults, $params);
|
||||
}
|
||||
@@ -79,98 +69,6 @@ class infopage {
|
||||
$this->set_parameters($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Given the HTML code for the static page, find the outage id for that page.
|
||||
* @param string $html Static info page HTML.
|
||||
* @return int|null Outage id or null if not found.
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public static function find_outageid_from_infopage($html) {
|
||||
if (!is_string($html)) {
|
||||
throw new coding_exception('$html must be a string.', gettype($html));
|
||||
}
|
||||
|
||||
$output = [];
|
||||
if (preg_match('/data-outage-id="(?P<id>\d+)"/', $html, $output)) {
|
||||
return (int)$output['id'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a static info page for the given outage.
|
||||
* @param outage $outage Outage to generate the info page.
|
||||
* @param string $file File to save the static info page.
|
||||
* @throws coding_exception
|
||||
* @throws file_exception
|
||||
* @throws invalid_state_exception
|
||||
*/
|
||||
public static function save_static_page(outage $outage, $file) {
|
||||
if (!is_string($file)) {
|
||||
throw new coding_exception('$file is not a string.', $file);
|
||||
}
|
||||
|
||||
$info = new infopage(['outage' => $outage, 'static' => true]);
|
||||
$html = $info->get_output();
|
||||
|
||||
self::save_static_page_sanitycheck($html);
|
||||
|
||||
$dir = dirname($file);
|
||||
if (!file_exists($dir) || !is_dir($dir)) {
|
||||
throw new file_exception('Directory must exists: '.$dir);
|
||||
}
|
||||
file_put_contents($file, $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the static info page by (re)creating or deleting it as needed.
|
||||
* @param outage|null $outage Outage or null if no scheduled outage.
|
||||
* @param string|null $file File to update. Null to use default.
|
||||
* @throws coding_exception
|
||||
* @throws file_exception
|
||||
*/
|
||||
public static function update_static_page($outage, $file = null) {
|
||||
if (is_null($file)) {
|
||||
$file = self::get_defaulttemplatefile();
|
||||
}
|
||||
if (!is_string($file)) {
|
||||
throw new coding_exception('$file is not a string.', $file);
|
||||
}
|
||||
if (!is_null($outage) && !($outage instanceof outage)) {
|
||||
throw new coding_exception('$outage must be null or an outage object.');
|
||||
}
|
||||
|
||||
if (is_null($outage)) {
|
||||
if (file_exists($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
} else {
|
||||
self::save_static_page($outage, $file);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default template file to use for static info page.
|
||||
* @return string The default template file to use for static info page.
|
||||
*/
|
||||
public static function get_defaulttemplatefile() {
|
||||
global $CFG;
|
||||
return $CFG->dataroot.'/climaintenance.template.html';
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures the data to create the page is valid.
|
||||
* It should never be invalid, but if it is we will get a blank page while the maintenance is ongoing and the
|
||||
* system administrators may become frustrated to understand why it is not working, let's not provoke them!
|
||||
* @param string $html The HTML to check.
|
||||
* @throws invalid_state_exception
|
||||
*/
|
||||
public static function save_static_page_sanitycheck($html) {
|
||||
if (!is_string($html) || (trim($html) == '') || (trim(html_to_text($html)) == '')) {
|
||||
throw new invalid_state_exception('Sanity check failed. Invalid contents on $html.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates and returns the HTML for the info page.
|
||||
* @return string HTML for the info page.
|
||||
@@ -187,14 +85,6 @@ class infopage {
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if this page should have admin options, taking in consideration it should happen if generating a static page.
|
||||
* @return bool True if it should display admin options.
|
||||
*/
|
||||
public function has_admin_options() {
|
||||
return (!$this->static && is_siteadmin());
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates and outputs the HTML for the info page.
|
||||
* @uses redirect
|
||||
@@ -203,11 +93,7 @@ class infopage {
|
||||
global $PAGE, $CFG, $OUTPUT;
|
||||
|
||||
if (is_null($this->outage)) {
|
||||
if ($this->static) {
|
||||
throw new coding_exception('Cannot render a static info page without an outage.');
|
||||
} else {
|
||||
redirect(new moodle_url('/'));
|
||||
}
|
||||
redirect(new moodle_url('/'));
|
||||
}
|
||||
|
||||
$viewbag = [
|
||||
@@ -216,24 +102,19 @@ class infopage {
|
||||
];
|
||||
|
||||
$PAGE->set_context(context_system::instance());
|
||||
if ($this->static) {
|
||||
$viewbag['admin'] = false;
|
||||
renderer::get()->output_view('info/static.php', $viewbag);
|
||||
} else {
|
||||
$PAGE->set_title($this->outage->get_title());
|
||||
$PAGE->set_heading($this->outage->get_title());
|
||||
$PAGE->set_url(new moodle_url('/auth/outage/info.php'));
|
||||
$PAGE->set_title($this->outage->get_title());
|
||||
$PAGE->set_heading($this->outage->get_title());
|
||||
$PAGE->set_url(new moodle_url('/auth/outage/info.php'));
|
||||
|
||||
// No hooks injecting into this page, do it manually.
|
||||
outagelib::inject();
|
||||
// No hooks injecting into this page, do it manually.
|
||||
outagelib::inject();
|
||||
|
||||
echo $OUTPUT->header();
|
||||
require($CFG->dirroot.'/auth/outage/views/info/content.php');
|
||||
echo $OUTPUT->header();
|
||||
require($CFG->dirroot.'/auth/outage/views/info/content.php');
|
||||
|
||||
// Moodle 2.7 did not check for CLI mode, which was fixed later.
|
||||
if (!($CFG->branch == '27' && CLI_SCRIPT)) {
|
||||
echo $OUTPUT->footer();
|
||||
}
|
||||
// Moodle 2.7 did not check for CLI mode, which was fixed later.
|
||||
if (!($CFG->branch == '27' && CLI_SCRIPT)) {
|
||||
echo $OUTPUT->footer();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -258,6 +139,5 @@ class infopage {
|
||||
}
|
||||
|
||||
$this->outage = $params['outage'];
|
||||
$this->static = (bool)$params['static'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,8 +28,8 @@ namespace auth_outage\local\controllers;
|
||||
use auth_outage\local\outage;
|
||||
use coding_exception;
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use invalid_parameter_exception;
|
||||
use invalid_state_exception;
|
||||
use moodle_url;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
@@ -45,32 +45,49 @@ defined('MOODLE_INTERNAL') || die();
|
||||
class maintenance_static_page {
|
||||
/**
|
||||
* Creates a page based on the given outage.
|
||||
* @param outage $outage
|
||||
* @param outage|null $outage
|
||||
* @return maintenance_static_page
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public static function create_from_outage(outage $outage) {
|
||||
public static function create_from_outage($outage) {
|
||||
global $CFG;
|
||||
$html = file_get_contents($CFG->wwwroot.'/auth/outage/info.php?auth_outage_hide_warning=1&id='.$outage->id);
|
||||
|
||||
if (!is_null($outage) && !($outage instanceof outage)) {
|
||||
throw new coding_exception('$outage must be null or an outage object.');
|
||||
}
|
||||
|
||||
if (is_null($outage)) {
|
||||
$html = null;
|
||||
} else if (PHPUNIT_TEST) {
|
||||
$html = '<html></html>';
|
||||
} else {
|
||||
$html = file_get_contents($CFG->wwwroot.'/auth/outage/info.php?auth_outage_hide_warning=1&id='.$outage->id);
|
||||
}
|
||||
|
||||
return self::create_from_html($html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a page based on the given HTML.
|
||||
* @param string $html
|
||||
* @param string|null $html
|
||||
* @return maintenance_static_page
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public static function create_from_html($html) {
|
||||
if (!is_string($html)) {
|
||||
if (!is_null($html) && !is_string($html)) {
|
||||
throw new coding_exception('$html is not valid.');
|
||||
}
|
||||
|
||||
$dom = new DOMDocument();
|
||||
if (is_null($html)) {
|
||||
$dom = null;
|
||||
} else {
|
||||
$dom = new DOMDocument();
|
||||
|
||||
// Let's assume we have no parsing errors as we cannot rely on a badly-formed page anyway.
|
||||
libxml_use_internal_errors(true);
|
||||
$dom->loadHTML($html);
|
||||
libxml_clear_errors();
|
||||
// Let's assume we have no parsing errors as we cannot rely on a badly-formed page anyway.
|
||||
libxml_use_internal_errors(true);
|
||||
$dom->loadHTML($html);
|
||||
libxml_clear_errors();
|
||||
}
|
||||
|
||||
return new maintenance_static_page($dom);
|
||||
}
|
||||
@@ -83,9 +100,13 @@ class maintenance_static_page {
|
||||
|
||||
/**
|
||||
* maintenance_static_page constructor.
|
||||
* @param DOMDocument $dom
|
||||
* @param DOMDocument|null $dom
|
||||
* @throws coding_exception
|
||||
*/
|
||||
public function __construct(DOMDocument $dom) {
|
||||
public function __construct($dom) {
|
||||
if (!is_null($dom) && !($dom instanceof DOMDocument)) {
|
||||
throw new coding_exception('$dom must be null or an DOMDocument object.');
|
||||
}
|
||||
$this->dom = $dom;
|
||||
}
|
||||
|
||||
@@ -123,12 +144,21 @@ class maintenance_static_page {
|
||||
* Generates the page.
|
||||
*/
|
||||
public function generate() {
|
||||
self::prepare_dataroot();
|
||||
self::remove_script_tags();
|
||||
self::update_link_stylesheet();
|
||||
self::update_link_favicon();
|
||||
self::update_images();
|
||||
file_put_contents(self::get_template_file(), $this->dom->saveHTML());
|
||||
self::cleanup();
|
||||
|
||||
if (!is_null($this->dom)) {
|
||||
self::remove_script_tags();
|
||||
self::update_link_stylesheet();
|
||||
self::update_link_favicon();
|
||||
self::update_images();
|
||||
|
||||
$html = $this->dom->saveHTML();
|
||||
if (trim($html) == '') {
|
||||
// Should never happen, but just in case...
|
||||
throw new invalid_state_exception('Sanity check failed, $html is empty.');
|
||||
}
|
||||
file_put_contents(self::get_template_file(), $html);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,12 +189,20 @@ class maintenance_static_page {
|
||||
/**
|
||||
* Clean up the dataroot as needed.
|
||||
*/
|
||||
private function prepare_dataroot() {
|
||||
$dir = self::get_resources_folder();
|
||||
if (is_dir($dir)) {
|
||||
self::delete_directory_recursively($dir);
|
||||
private function cleanup() {
|
||||
$resources = $this->get_resources_folder();
|
||||
if (is_dir($resources)) {
|
||||
self::delete_directory_recursively($resources);
|
||||
}
|
||||
|
||||
$template = $this->get_template_file();
|
||||
if (is_file($template)) {
|
||||
unlink($template);
|
||||
}
|
||||
|
||||
if (!is_null($this->dom)) {
|
||||
mkdir($resources, 0775, true);
|
||||
}
|
||||
mkdir($dir, 0775, true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
namespace auth_outage\local;
|
||||
|
||||
use auth_outage\dml\outagedb;
|
||||
use auth_outage\local\controllers\infopage;
|
||||
use auth_outage\local\controllers\maintenance_static_page;
|
||||
use auth_outage\output\renderer;
|
||||
use coding_exception;
|
||||
use Exception;
|
||||
@@ -156,7 +156,7 @@ class outagelib {
|
||||
if (is_null($outage)) {
|
||||
$outage = outagedb::get_next_starting();
|
||||
}
|
||||
infopage::update_static_page($outage);
|
||||
maintenance_static_page::create_from_outage($outage)->generate();
|
||||
self::update_climaintenance_code($outage);
|
||||
self::update_maintenance_later($outage);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user