MDL-62767 theme: Remove old localcaches when clearing/updating theme cache

This commit is contained in:
Michael Hawkins
2018-07-03 14:00:51 +08:00
committed by Jun Pataleta
parent 032abb1a6f
commit d683aff069
2 changed files with 31 additions and 0 deletions
+7
View File
@@ -245,6 +245,7 @@ function theme_build_css_for_themes($themeconfigs = [], $directions = ['rtl', 'l
*/
function theme_reset_all_caches() {
global $CFG, $PAGE;
require_once("{$CFG->libdir}/filelib.php");
$next = theme_get_next_revision();
theme_set_revision($next);
@@ -257,6 +258,12 @@ function theme_reset_all_caches() {
// Purge compiled post processed css.
cache::make('core', 'postprocessedcss')->purge();
// Delete all old theme localcaches.
$themecachedirs = glob("{$CFG->localcachedir}/theme/*", GLOB_ONLYDIR);
foreach ($themecachedirs as $localcachedir) {
fulldelete($localcachedir);
}
if ($PAGE) {
$PAGE->reload_theme();
}
+24
View File
@@ -128,6 +128,7 @@ $cache = true;
// likely being regenerated.
if ($themerev <= 0 or $themerev != $rev or $themesubrev != $currentthemesubrev) {
$rev = $themerev;
$themesubrev = $currentthemesubrev;
$cache = false;
$candidatedir = "$CFG->localcachedir/theme/$rev/$themename/css";
@@ -216,6 +217,7 @@ if ($sendaftergeneration || $lock) {
*/
function theme_styles_generate_and_store($theme, $rev, $themesubrev, $candidatedir) {
global $CFG;
require_once("{$CFG->libdir}/filelib.php");
// Generate the content first.
if (!$csscontent = $theme->get_css_cached_content()) {
@@ -260,6 +262,28 @@ function theme_styles_generate_and_store($theme, $rev, $themesubrev, $candidated
. theme_styles_get_filename($type, 0, $theme->use_svg_icons());
css_store_css($theme, $fallbacksheet, $csscontent, true, $chunkurl);
// Delete older revisions from localcache.
$themecachedirs = glob("{$CFG->localcachedir}/theme/*", GLOB_ONLYDIR);
foreach ($themecachedirs as $localcachedir) {
$cachedrev = [];
preg_match("/\/theme\/([0-9]+)$/", $localcachedir, $cachedrev);
$cachedrev = isset($cachedrev[1]) ? intval($cachedrev[1]) : 0;
if ($cachedrev > 0 && $cachedrev < $rev) {
fulldelete($localcachedir);
}
}
// Delete older theme subrevision CSS from localcache.
$subrevfiles = glob("{$CFG->localcachedir}/theme/{$rev}/{$theme->name}/css/*.css");
foreach ($subrevfiles as $subrevfile) {
$cachedsubrev = [];
preg_match("/_([0-9]+)\.([0-9]+\.)?css$/", $subrevfile, $cachedsubrev);
$cachedsubrev = isset($cachedsubrev[1]) ? intval($cachedsubrev[1]) : 0;
if ($cachedsubrev > 0 && $cachedsubrev < $themesubrev) {
fulldelete($subrevfile);
}
}
return $candidatesheet;
}