MDL-40563 use revisions for theme css cache files and fix potential race condition

This commit is contained in:
Petr Škoda 2013-07-12 23:25:00 +02:00
parent 0238ca0d6e
commit ef201c966f

View File

@ -32,6 +32,10 @@ define('ABORT_AFTER_CONFIG', true);
require('../config.php'); // this stops immediately at the beginning of lib/setup.php
require_once($CFG->dirroot.'/lib/csslib.php');
if (!defined('THEME_DESIGNER_CACHE_LIFETIME')) {
define('THEME_DESIGNER_CACHE_LIFETIME', 4); // this can be also set in config.php
}
if ($slashargument = min_get_slash_argument()) {
$slashargument = ltrim($slashargument, '/');
if (substr_count($slashargument, '/') < 2) {
@ -71,8 +75,7 @@ if ($type === 'editor') {
} else if ($type === 'all') {
// We're fine.
} else {
header('HTTP/1.0 404 not found');
die('Theme was not found, sorry.');
css_send_css_not_found();
}
if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
@ -84,8 +87,8 @@ if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
die('Theme was not found, sorry.');
}
$candidatedir = "$CFG->cachedir/theme/$themename/css";
$etag = "$themename/$rev/$type";
$candidatedir = "$CFG->cachedir/theme/$rev/$themename/css";
$etag = "$rev/$themename/$type";
$candidatename = $type;
if (!$usesvg) {
// Add to the sheet name, one day we'll be able to just drop this.
@ -121,26 +124,39 @@ require("$CFG->dirroot/lib/setup.php");
$theme = theme_config::load($themename);
$theme->force_svg_use($usesvg);
$rev = theme_get_revision();
$themerev = theme_get_revision();
$etag = "$themename/$rev/$type";
if (!$usesvg) {
// Add to the etag, one day we'll be able to just delete svg nonsense this.
$etag .= '/nosvg';
$cache = true;
if ($themerev <= 0 or $themerev != $rev) {
$rev = $themerev;
$cache = false;
$candidatedir = "$CFG->cachedir/theme/$rev/$themename/css";
$etag = "$rev/$themename/$type";
$candidatename = $type;
if (!$usesvg) {
// Add to the sheet name, one day we'll be able to just drop this.
$candidatedir .= '/nosvg';
$etag .= '/nosvg';
}
if ($chunk !== null) {
$etag .= '/chunk'.$chunk;
$candidatename .= '.'.$chunk;
}
$candidatesheet = "$candidatedir/$candidatename.css";
$etag = sha1($etag);
}
$etag = sha1($etag);
make_cache_directory('theme', false);
if ($type === 'editor') {
$cssfiles = $theme->editor_css_files();
css_store_css($theme, $candidatesheet, $cssfiles);
css_store_css($theme, "$candidatedir/editor.css", $cssfiles, false);
} else {
// IE requests plugins/parents/theme instead of all at once.
$basedir = "$CFG->cachedir/theme/$themename/css";
if (!$usesvg) {
$basedir .= '/nosvg';
}
// Older IEs require smaller chunks.
$css = $theme->css_files();
$allfiles = array();
$relroot = preg_replace('|^http.?://[^/]+|', '', $CFG->wwwroot);
if (!empty($slashargument)) {
if ($usesvg) {
@ -156,8 +172,8 @@ if ($type === 'editor') {
}
}
$cssfiles = array();
foreach ($css as $key=>$value) {
foreach($value as $val) {
foreach ($css as $key => $value) {
foreach ($value as $val) {
if (is_array($val)) {
foreach ($val as $k=>$v) {
$cssfiles[] = $v;
@ -167,13 +183,24 @@ if ($type === 'editor') {
}
}
}
css_store_css($theme, "$basedir/all.css", $cssfiles, true, $chunkurl);
css_store_css($theme, "$candidatedir/all.css", $cssfiles, true, $chunkurl);
}
// verify nothing failed in cache file creation
clearstatcache();
if (!file_exists($candidatesheet)) {
css_send_css_not_found();
}
// We need to send at least something, IE does not get it chunked properly but who cares.
$css = '';
foreach ($cssfiles as $file) {
$css .= file_get_contents($file)."\n";
}
css_send_uncached_css($css, false);
css_send_cached_css($candidatesheet, $etag);
} else if (!$cache) {
// Do not pollute browser caches if invalid revision requested.
css_send_uncached_css(file_get_contents($candidatesheet), false);
} else {
// This is the expected result!
css_send_cached_css($candidatesheet, $etag);
}