From 2ab797c90bc34d8b0f1eec59f7312203b8e5dc42 Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Fri, 5 Feb 2016 19:45:00 +1100 Subject: [PATCH] MDL-28030 weblib: Added $CFG->urlrewriteclass in config.php This enables a plugin to implement rewriting rules enabling 'clean' or 'semantic' urls such as /course/COMP100/ instead of /course/view.php?id=1234 --- config-dist.php | 5 +++ lib/classes/output/url_rewriter.php | 58 +++++++++++++++++++++++++++++ lib/outputrenderers.php | 7 ++++ lib/upgrade.txt | 2 + lib/weblib.php | 32 ++++++++++++++++ 5 files changed, 104 insertions(+) create mode 100644 lib/classes/output/url_rewriter.php diff --git a/config-dist.php b/config-dist.php index 04b51d7bda1..469884d211f 100644 --- a/config-dist.php +++ b/config-dist.php @@ -298,6 +298,11 @@ $CFG->admin = 'admin'; // This setting will make some graphs (eg user logs) use lines instead of bars // $CFG->preferlinegraphs = true; // +// This setting allows you to specify a class to rewrite outgoing urls +// enabling 'clean urls' in conjunction with an apache / nginx handler. +// The handler must implement \core\output\url_rewriter. +// $CFG->urlrewriteclass = '\local_cleanurls\url_rewriter'; +// // Enabling this will allow custom scripts to replace existing moodle scripts. // For example: if $CFG->customscripts/course/view.php exists then // it will be used instead of $CFG->wwwroot/course/view.php diff --git a/lib/classes/output/url_rewriter.php b/lib/classes/output/url_rewriter.php new file mode 100644 index 00000000000..435bd55afb9 --- /dev/null +++ b/lib/classes/output/url_rewriter.php @@ -0,0 +1,58 @@ +. + +/** + * URL rewriter base. + * + * @package core + * @author Brendan Heywood + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\output; + +defined('MOODLE_INTERNAL') || die(); + +/** + * URL rewriter interface + * + * @package core + * @author Brendan Heywood + * @copyright Catalyst IT + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +interface url_rewriter { + + /** + * Rewrite moodle_urls into another form. + * + * @param moodle_url $url a url to potentially rewrite + * @return moodle_url Returns a new, or the original, moodle_url; + */ + public static function url_rewrite(\moodle_url $url); + + /** + * Gives a url rewriting plugin a chance to rewrite the current page url + * avoiding redirects and improving performance. + * + * @return void + */ + public static function html_head_setup(); + + +} + diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 858061ea009..5df8eaf3443 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -475,6 +475,13 @@ class core_renderer extends renderer_base { } $output = ''; + + // Allow a url_rewrite plugin to setup any dynamic head content. + if (isset($CFG->urlrewriteclass) && !isset($CFG->upgraderunning)) { + $class = $CFG->urlrewriteclass; + $output .= $class::html_head_setup(); + } + $output .= '' . "\n"; $output .= '' . "\n"; // This is only set by the {@link redirect()} method diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 255b7647e41..081b0a13cfa 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -39,6 +39,8 @@ information provided here is intended especially for developers. Note than you will need to bump the plugin version so moodle is aware that you removed the plugin's event handlers. * mforms validation functions are not available in the global JS namespace anymore, event listeners are assigned to fields and buttons through a self-contained JS function. +* Added $CFG->urlrewriteclass option to config.php allowing clean / semantic urls to + be implemented in a plugin, eg local_cleanurls. === 3.0 === diff --git a/lib/weblib.php b/lib/weblib.php index 2f2adf77bfd..8f3c3330f4c 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -553,6 +553,38 @@ class moodle_url { * @return string Resulting URL */ public function out($escaped = true, array $overrideparams = null) { + + global $CFG; + + if (!is_bool($escaped)) { + debugging('Escape parameter must be of type boolean, '.gettype($escaped).' given instead.'); + } + + $url = $this; + + // Allow url's to be rewritten by a plugin. + if (isset($CFG->urlrewriteclass) && !isset($CFG->upgraderunning)) { + $class = $CFG->urlrewriteclass; + $pluginurl = $class::url_rewrite($url); + if ($pluginurl instanceof moodle_url) { + $url = $pluginurl; + } + } + + return $url->raw_out($escaped, $overrideparams); + + } + + /** + * Output url without any rewrites + * + * This is identical in signature and use to out() but doesn't call the rewrite handler. + * + * @param bool $escaped Use & as params separator instead of plain & + * @param array $overrideparams params to add to the output url, these override existing ones with the same name. + * @return string Resulting URL + */ + public function raw_out($escaped = true, array $overrideparams = null) { if (!is_bool($escaped)) { debugging('Escape parameter must be of type boolean, '.gettype($escaped).' given instead.'); }