Skip to content

Commit

Permalink
MDL-62767 theme: Remove old localcaches when clearing/updating theme …
Browse files Browse the repository at this point in the history
…cache
  • Loading branch information
mickhawkins authored and junpataleta committed Jul 3, 2018
1 parent 9af6a71 commit a990e4f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/outputlib.php
Expand Up @@ -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);
Expand All @@ -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();
}
Expand Down
24 changes: 24 additions & 0 deletions theme/styles.php
Expand Up @@ -128,6 +128,7 @@
// 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";
Expand Down Expand Up @@ -216,6 +217,7 @@
*/
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()) {
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit a990e4f

Please sign in to comment.