]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-21124 towards full JS support in themes
authorPetr Skoda <skodak@moodle.org>
Sun, 20 Dec 2009 23:06:57 +0000 (23:06 +0000)
committerPetr Skoda <skodak@moodle.org>
Sun, 20 Dec 2009 23:06:57 +0000 (23:06 +0000)
lib/outputlib.php
lib/outputrenderers.php
theme/javascripts.php

index 6e4227f696cbac49c13a28d2e08a87a7311a5feb..60dc445478afa50a8a31fae29a08ba32c7e99544 100644 (file)
@@ -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 &amp; 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 &amp; 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)) {
index 2888722082f9c36caddc59377664b484573f3844..5d7fef96d9153e53fc2d4ed6e392926eff541ef5 100644 (file)
@@ -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)) {
index f8baab2c5effd7fbfdd2b78ffe355960ea27a481..7667397eaa97d4b374484850fd2746641e1fbea7 100644 (file)
  * @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;
+}