MDL-59148 core: save css file totemp directory

Save a copy of the css file on the temp directory
to be used as a fallback when waiting for a theme to be compiled
and is not versioned in any way.
This commit is contained in:
Simey Lameze 2017-06-13 10:36:27 +08:00 committed by David Monllao
parent 1555f25b24
commit 3b9b47312c

View file

@ -97,27 +97,32 @@ if (file_exists("$CFG->dirroot/theme/$themename/config.php")) {
$candidatedir = "$CFG->localcachedir/theme/$rev/$themename/css"; $candidatedir = "$CFG->localcachedir/theme/$rev/$themename/css";
$etag = "$rev/$themename/$type/$themesubrev"; $etag = "$rev/$themename/$type/$themesubrev";
$candidatename = ($themesubrev > 0) ? "{$type}_{$themesubrev}" : $type; $candidatesheet = "{$candidatedir}/{$type}";
if ($themesubrev > 0) {
$candidatesheet .= "_{$themesubrev}";
}
if (!$usesvg) { if (!$usesvg) {
// Add to the sheet name, one day we'll be able to just drop this. // Add to the sheet name, one day we'll be able to just drop this.
$candidatedir .= '/nosvg'; $candidatesheet .= "-nosvg";
$etag .= '/nosvg'; $etag .= '/nosvg';
} }
if ($chunk !== null) { if ($chunk !== null) {
$etag .= '/chunk'.$chunk; $etag .= "/chunk{$chunk}";
$candidatename .= '.'.$chunk; $chunkedcandidatesheet = "{$candidatesheet}.{$chunk}.css";
$candidatesheet = "{$candidatesheet}.css";
} else {
$candidatesheet = $chunkedcandidatesheet = "{$candidatesheet}.css";
} }
$candidatesheet = "$candidatedir/$candidatename.css";
$etag = sha1($etag); $etag = sha1($etag);
if (file_exists($candidatesheet)) { if (file_exists($chunkedcandidatesheet)) {
if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
// We do not actually need to verify the etag value because our files // We do not actually need to verify the etag value because our files
// never change in cache because we increment the rev counter. // never change in cache because we increment the rev counter.
css_send_unmodified(filemtime($candidatesheet), $etag); css_send_unmodified(filemtime($chunkedcandidatesheet), $etag);
} }
css_send_cached_css($candidatesheet, $etag); css_send_cached_css($chunkedcandidatesheet, $etag);
} }
// Ok, now we need to start normal moodle script, we need to load all libs and $DB. // Ok, now we need to start normal moodle script, we need to load all libs and $DB.
@ -146,18 +151,23 @@ if ($themerev <= 0 or $themerev != $rev or $themesubrev != $currentthemesubrev)
$candidatedir = "$CFG->localcachedir/theme/$rev/$themename/css"; $candidatedir = "$CFG->localcachedir/theme/$rev/$themename/css";
$etag = "$rev/$themename/$type/$themesubrev"; $etag = "$rev/$themename/$type/$themesubrev";
$candidatename = ($themesubrev > 0) ? "{$type}_{$themesubrev}" : $type; $candidatesheet = "{$candidatedir}/{$type}";
if ($themesubrev > 0) {
$candidatesheet .= "_{$themesubrev}";
}
if (!$usesvg) { if (!$usesvg) {
// Add to the sheet name, one day we'll be able to just drop this. // Add to the sheet name, one day we'll be able to just drop this.
$candidatedir .= '/nosvg'; $candidatesheet .= "-nosvg";
$etag .= '/nosvg'; $etag .= '/nosvg';
} }
if ($chunk !== null) { if ($chunk !== null) {
$etag .= '/chunk'.$chunk; $etag .= "/chunk{$chunk}";
$candidatename .= '.'.$chunk; $chunkedcandidatesheet = "{$candidatesheet}.{$chunk}.css";
$candidatesheet = "{$candidatesheet}.css";
} else {
$candidatesheet = $chunkedcandidatesheet = "{$candidatesheet}.css";
} }
$candidatesheet = "$candidatedir/$candidatename.css";
$etag = sha1($etag); $etag = sha1($etag);
} }
@ -165,7 +175,7 @@ make_localcache_directory('theme', false);
if ($type === 'editor') { if ($type === 'editor') {
$csscontent = $theme->get_css_content_editor(); $csscontent = $theme->get_css_content_editor();
css_store_css($theme, "$candidatedir/editor.css", $csscontent, false); css_store_css($theme, $candidatesheet, $csscontent, false);
} else { } else {
// Fetch a lock whilst the CSS is fetched as this can be slow and CPU intensive. // Fetch a lock whilst the CSS is fetched as this can be slow and CPU intensive.
@ -173,16 +183,16 @@ if ($type === 'editor') {
$lockfactory = \core\lock\lock_config::get_lock_factory('core_theme_get_css_content'); $lockfactory = \core\lock\lock_config::get_lock_factory('core_theme_get_css_content');
$lock = $lockfactory->get_lock($themename, rand(90, 120)); $lock = $lockfactory->get_lock($themename, rand(90, 120));
if (file_exists($candidatesheet)) { if (file_exists($chunkedcandidatesheet)) {
// The file was built while we waited for the lock, we release the lock and serve the file. // The file was built while we waited for the lock, we release the lock and serve the file.
if ($lock) { if ($lock) {
$lock->release(); $lock->release();
} }
if ($cache) { if ($cache) {
css_send_cached_css($candidatesheet, $etag); css_send_cached_css($chunkedcandidatesheet, $etag);
} else { } else {
css_send_uncached_css(file_get_contents($candidatesheet)); css_send_uncached_css(file_get_contents($chunkedcandidatesheet));
} }
} }
@ -207,8 +217,7 @@ if ($type === 'editor') {
$chunkurl = "{$relroot}/theme/styles.php?theme={$themename}&rev={$rev}&type=$type&svg=0"; $chunkurl = "{$relroot}/theme/styles.php?theme={$themename}&rev={$rev}&type=$type&svg=0";
} }
} }
css_store_css($theme, $candidatesheet, $csscontent, true, $chunkurl);
css_store_css($theme, "$candidatedir/$candidatename.css", $csscontent, true, $chunkurl);
if ($lock) { if ($lock) {
// Now that the CSS has been generated and/or stored, release the lock. // Now that the CSS has been generated and/or stored, release the lock.
@ -222,9 +231,9 @@ if (!$cache) {
// let's ignore legacy IE breakage here too. // let's ignore legacy IE breakage here too.
css_send_uncached_css($csscontent); css_send_uncached_css($csscontent);
} else if ($chunk !== null and file_exists($candidatesheet)) { } else if ($chunk !== null and file_exists($chunkedcandidatesheet)) {
// Greetings stupid legacy IEs! // Greetings stupid legacy IEs!
css_send_cached_css($candidatesheet, $etag); css_send_cached_css($chunkedcandidatesheet, $etag);
} else { } else {
// Real browsers - this is the expected result! // Real browsers - this is the expected result!