From e68c5f36cf1fc74875ef42c96f85e26f78233bb6 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sun, 20 Dec 2009 23:06:57 +0000 Subject: [PATCH] MDL-21124 towards full JS support in themes --- lib/outputlib.php | 40 ++++++++++++-------- lib/outputrenderers.php | 2 +- theme/javascripts.php | 83 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 108 insertions(+), 17 deletions(-) diff --git a/lib/outputlib.php b/lib/outputlib.php index 6e4227f696..60dc445478 100644 --- a/lib/outputlib.php +++ b/lib/outputlib.php @@ -381,7 +381,7 @@ class theme_config { $baseconfig = $config; } - $configurable = array('parents', 'sheets', 'parents_exclude_sheets', 'plugins_exclude_sheets', 'javascripts', + $configurable = array('parents', 'sheets', 'parents_exclude_sheets', 'plugins_exclude_sheets', 'javascripts', 'parents_exclude_javascripts', 'layouts', 'resource_mp3player_colors', 'filter_mediaplugin_colors', 'rendererfactory', 'csspostprocess', 'editor_sheets', 'rarrow', 'larrow'); @@ -607,20 +607,6 @@ class theme_config { } } - /** - * Get the javascript URL of this theme - * @param bool $encoded false means use & and true use & in URLs - * @return moodle_url - */ - public function javascript_url() { - global $CFG; - - $rev = theme_get_revision(); - - $params = array('theme'=>$this->name,'rev'=>$rev); - return new moodle_url($CFG->httpswwwroot.'/theme/javascripts.php', $params); - } - /** * Returns the content of the one huge CSS merged from all style sheets. * @return string @@ -741,6 +727,30 @@ class theme_config { return $css; } + + /** + * Get the javascript URL of this theme + * @param bool $encoded false means use & and true use & in URLs + * @return moodle_url + */ + public function javascript_url() { + global $CFG; + + $rev = theme_get_revision(); + + $params = array('theme'=>$this->name,'rev'=>$rev); + return new moodle_url($CFG->httpswwwroot.'/theme/javascripts.php', $params); + } + + /** + * Returns the content of the one huge javascript file merged from all theme javascript files. + * @return string + */ + public function javascript_content() { + //TODO: load contents of all theme JS files + return '/*not yet fully implemented*/'; + } + protected function post_process($css) { // now resolve all image locations if (preg_match_all('/\[\[pix:([a-z_]+\|)?([^\]]+)\]\]/', $css, $matches, PREG_SET_ORDER)) { diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index 2888722082..5d7fef96d9 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -367,7 +367,7 @@ class core_renderer extends renderer_base { // Get the theme javascript $jsurl = $this->page->theme->javascript_url(); - $this->page->requires->js($jsurl->out_raw(), true)->in_head(); + $this->page->requires->js($jsurl->out(), true)->in_head(); // Perform a browser environment check for the flash version. Should only run once per login session. if (isloggedin() && !empty($CFG->excludeoldflashclients) && empty($SESSION->flashversion)) { diff --git a/theme/javascripts.php b/theme/javascripts.php index f8baab2c5e..7667397eaa 100644 --- a/theme/javascripts.php +++ b/theme/javascripts.php @@ -23,4 +23,85 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -//TODO \ No newline at end of file + +// we need just the values from config.php and minlib.php +define('ABORT_AFTER_CONFIG', true); +require('../config.php'); // this stops immediately at the beginning of lib/setup.php + +$themename = min_optional_param('theme', 'standard', 'SAFEDIR'); +$rev = min_optional_param('rev', 0, 'INT'); + +if (!file_exists("$CFG->dirroot/theme/$themename/config.php") and !file_exists("$CFG->dataroot/theme/$themename/config.php")) { + header('HTTP/1.0 404 not found'); + die('Theme was not found, sorry.'); +} + +$candidate = "$CFG->dataroot/cache/theme/$themename/javascript.js"; + +if ($rev > -1 and file_exists($candidate)) { + if (!empty($_SERVER['HTTP_IF_NONE_MATCH'])) { + // we do not actually need to verify the etag value because our files + // never change in cache because we increment the rev parameter + header('HTTP/1.1 304 Not Modified'); + die; + } + send_cached_js($candidate, $rev); +} + +//================================================================================= +// ok, now we need to start normal moodle script, we need to load all libs and $DB +define('ABORT_AFTER_CONFIG_CANCEL', true); + +define('NO_MOODLE_COOKIES', true); // Session not used here +define('NO_UPGRADE_CHECK', true); // Ignore upgrade check + +require("$CFG->dirroot/lib/setup.php"); + +$theme = theme_config::load($themename); + +$js = $theme->javascript_content(); +if ($rev > -1) { + check_dir_exists(dirname($candidate), true, true); + $fp = fopen($candidate, 'w'); + fwrite($fp, $js); + fclose($fp); + send_cached_js($candidate); + +} else { + send_uncached_js($js); +} + +//================================================================================= +//=== utility functions == +// we are not using filelib because we need to fine tune all header +// parameters to get the best performance. + +function send_cached_js($jspath) { + $lifetime = 60*60*24*20; + + header('Content-Disposition: inline; filename="javascripts.php"'); + header('Last-Modified: '. gmdate('D, d M Y H:i:s', filemtime($jspath)) .' GMT'); + header('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT'); + header('Pragma: '); + header('Accept-Ranges: none'); + header('Content-Type: application/x-javascript'); + header('Content-Length: '.filesize($jspath)); + + while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite + readfile($jspath); + die; +} + +function send_uncached_js($js) { + header('Content-Disposition: inline; filename="javascripts.php"'); + header('Last-Modified: '. gmdate('D, d M Y H:i:s', time()) .' GMT'); + header('Expires: '. gmdate('D, d M Y H:i:s', time() + 2) .' GMT'); + header('Pragma: '); + header('Accept-Ranges: none'); + header('Content-Type: application/x-javascript'); + header('Content-Length: '.strlen($js)); + + while (@ob_end_flush()); //flush the buffers - save memory and disable sid rewrite + echo $js; + die; +} -- 2.39.5