]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-21208 major perf improvement for theme designer mode - now partially cached too ;-)
authorPetr Skoda <skodak@moodle.org>
Tue, 29 Dec 2009 12:04:31 +0000 (12:04 +0000)
committerPetr Skoda <skodak@moodle.org>
Tue, 29 Dec 2009 12:04:31 +0000 (12:04 +0000)
lib/outputlib.php
theme/styles_debug.php

index cbd2ceb06fcffb4b1da495c539781e56da1af2e7..a8037822c1930c205c9e1b67486133aa447df3ab 100644 (file)
@@ -587,8 +587,29 @@ class theme_config {
             return array(new moodle_url($CFG->httpswwwroot.'/theme/styles.php', $params));
 
         } else {
-            // this is painfully slow, but it should not matter much ;-)
-            $css = $this->css_content();
+            // find out the current CSS and cache it now for 5 seconds
+            // the point is to construct the CSS only once and pass it through the
+            // dataroot to the script that actually serves the sheets
+            $candidatesheet = "$CFG->dataroot/cache/theme/$this->name/designer.ser";
+            if (!file_exists($candidatesheet)) {
+                $css = $this->css_content();
+                check_dir_exists(dirname($candidatesheet), true, true);
+                file_put_contents($candidatesheet, serialize($css));
+
+            } else if (filemtime($candidatesheet) > time() - 5) {
+                if ($css = file_get_contents($candidatesheet)) {
+                    $css = unserialize($css);
+                } else {
+                    unlink($candidatesheet);
+                    $css = $this->css_content();
+                }
+
+            } else {
+                unlink($candidatesheet);
+                $css = $this->css_content();
+                file_put_contents($candidatesheet, serialize($css));
+            }
+
             $url = $CFG->httpswwwroot.'/theme/styles_debug.php';
             $urls = array();
             $urls[] = new moodle_url($url, array('theme'=>$this->name,'type'=>'yui2'));
index 7fc338f9d0eeb4bd49b2e9ae6f6a9f8d1110c94f..1db692b4f995be66356eae3205907bf06d2e741e 100644 (file)
  */
 
 
-// no chaching
-define('NO_MOODLE_COOKIES', true); // Session not used here
-define('NO_UPGRADE_CHECK', true);  // Ignore upgrade check
-require('../config.php');
+define('ABORT_AFTER_CONFIG', true);
+require('../config.php'); // this stops immediately at the beginning of lib/setup.php
 
-$themename = required_param('theme', PARAM_SAFEDIR);
-$type      = required_param('type', PARAM_SAFEDIR);
-$subtype   = optional_param('subtype', '', PARAM_SAFEDIR);
-$sheet     = optional_param('sheet', '', PARAM_SAFEDIR);
+$themename = min_optional_param('theme', 'standard', 'SAFEDIR');
+$type      = min_optional_param('type', 'all', 'SAFEDIR');
+$subtype   = min_optional_param('subtype', '', 'SAFEDIR');
+$sheet     = min_optional_param('sheet', '', 'SAFEDIR');
 
 if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
     // exists
@@ -42,19 +40,17 @@ if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
     css_not_found();
 }
 
-if (theme_get_revision() > -1) {
-    //bad luck - this should not happen!
+$candidatesheet = "$CFG->dataroot/cache/theme/$themename/designer.ser";
+
+if (!file_exists($candidatesheet)) {
     css_not_found();
 }
 
-$theme = theme_config::load($themename);
-
-if ($type === 'editor') {
-    $css = $theme->editor_css_content();
-    send_uncached_css($css);
+if (!$css = file_get_contents($candidatesheet)) {
+    css_not_found();
 }
 
-$css = $theme->css_content();
+$css = unserialize($css);
 
 if ($type === 'yui2') {
     send_uncached_css(reset($css['yui2']));
@@ -66,7 +62,7 @@ if ($type === 'yui2') {
 
 } else if ($type === 'parent') {
     if (isset($css['parents'][$subtype][$sheet])) {
-        send_uncached_css($css['parents'][$subtype][$sheet]);
+        send_uncached_css($css['parents'][$subtype][$sheet], 30); // parent sheets are not supposed to change much, right?
     }
 
 } else if ($type === 'theme') {
@@ -81,10 +77,10 @@ css_not_found();
 // we are not using filelib because we need to fine tune all header
 // parameters to get the best performance.
 
-function send_uncached_css($css) {
+function send_uncached_css($css, $lifetime = 2) {
     header('Content-Disposition: inline; filename="styles_debug.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('Expires: '. gmdate('D, d M Y H:i:s', time() + $lifetime) .' GMT');
     header('Pragma: ');
     header('Accept-Ranges: none');
     header('Content-Type: text/css');