MDL-62767 theme: Remove old localcaches when clearing/updating theme cache
authorMichael Hawkins <michaelh@moodle.com>
Mon, 25 Jun 2018 02:54:07 +0000 (10:54 +0800)
committerJun Pataleta <jun@moodle.com>
Tue, 3 Jul 2018 06:00:29 +0000 (14:00 +0800)
lib/outputlib.php
theme/styles.php

index 00e9fff..c511a10 100644 (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();
     }
index 6a4aa9b..9f3ce5f 100644 (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;
 }