Skip to content

Commit

Permalink
MDL-21208 major perf improvement for theme designer mode - now partia…
Browse files Browse the repository at this point in the history
…lly cached too ;-)
  • Loading branch information
skodak committed Dec 29, 2009
1 parent 13ea159 commit 7829cf7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
25 changes: 23 additions & 2 deletions lib/outputlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,29 @@ public function css_urls() {
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'));
Expand Down
34 changes: 15 additions & 19 deletions theme/styles_debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@
*/


// 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
Expand All @@ -42,19 +40,17 @@
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']));
Expand All @@ -66,7 +62,7 @@

} 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') {
Expand All @@ -81,10 +77,10 @@
// 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');
Expand Down

0 comments on commit 7829cf7

Please sign in to comment.