Skip to content

Commit

Permalink
MDL-66367 core: added templaterev variable
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Sep 4, 2019
1 parent 5beb388 commit e63395b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions admin/classes/form/purge_caches.php
Expand Up @@ -50,6 +50,7 @@ public function definition() {
$mform->createElement('advcheckbox', 'theme', '', get_string('purgethemecache', 'admin')),
$mform->createElement('advcheckbox', 'lang', '', get_string('purgelangcache', 'admin')),
$mform->createElement('advcheckbox', 'js', '', get_string('purgejscache', 'admin')),
$mform->createElement('advcheckbox', 'template', '', get_string('purgetemplates', 'admin')),
$mform->createElement('advcheckbox', 'filter', '', get_string('purgefiltercache', 'admin')),
$mform->createElement('advcheckbox', 'muc', '', get_string('purgemuc', 'admin')),
$mform->createElement('advcheckbox', 'other', '', get_string('purgeothercaches', 'admin'))
Expand Down
1 change: 1 addition & 0 deletions admin/settings/appearance.php
Expand Up @@ -277,6 +277,7 @@

$setting = new admin_setting_configcheckbox('cachetemplates', new lang_string('cachetemplates', 'admin'),
new lang_string('cachetemplates_help', 'admin'), 1);
$setting->set_updatedcallback('template_reset_all_caches');
$temp = new admin_settingpage('templates', new lang_string('templates', 'admin'));
$temp->add($setting);
$ADMIN->add('appearance', $temp);
Expand Down
1 change: 1 addition & 0 deletions lang/en/admin.php
Expand Up @@ -1043,6 +1043,7 @@
$string['purgeothercaches'] = 'All file and miscellaneous caches';
$string['purgeselectedcaches'] = 'Purge selected caches';
$string['purgeselectedcachesfinished'] = 'The selected caches were purged.';
$string['purgetemplates'] = 'Templates';
$string['purgethemecache'] = 'Themes';
$string['requestcategoryselection'] = 'Enable category selection';
$string['restorecourse'] = 'Restore course';
Expand Down
3 changes: 2 additions & 1 deletion lib/db/install.php
Expand Up @@ -284,9 +284,10 @@ function xmldb_main_install() {
set_role_contextlevels($guestrole, get_default_contextlevels('guest'));
set_role_contextlevels($userrole, get_default_contextlevels('user'));

// Init theme and JS revisions
// Init theme, JS and template revisions.
set_config('themerev', time());
set_config('jsrev', time());
set_config('templaterev', time());

// No admin setting for this any more, GD is now required, remove in Moodle 2.6.
set_config('gdversion', 2);
Expand Down
5 changes: 4 additions & 1 deletion lib/moodlelib.php
Expand Up @@ -1648,7 +1648,7 @@ function purge_all_caches() {
* 'other' Purge all other caches?
*/
function purge_caches($options = []) {
$defaults = array_fill_keys(['muc', 'theme', 'lang', 'js', 'filter', 'other'], false);
$defaults = array_fill_keys(['muc', 'theme', 'lang', 'js', 'template', 'filter', 'other'], false);
if (empty(array_filter($options))) {
$options = array_fill_keys(array_keys($defaults), true); // Set all options to true.
} else {
Expand All @@ -1666,6 +1666,9 @@ function purge_caches($options = []) {
if ($options['js']) {
js_reset_all_caches();
}
if ($options['template']) {
template_reset_all_caches();
}
if ($options['filter']) {
reset_text_filters_cache();
}
Expand Down
36 changes: 36 additions & 0 deletions lib/outputrequirementslib.php
Expand Up @@ -413,6 +413,25 @@ protected function get_jsrev() {
return $jsrev;
}

/**
* Determine the correct Template revision to use for this load.
*
* @return int the templaterev to use.
*/
protected function get_templaterev() {
global $CFG;

if (empty($CFG->cachetemplates)) {
$templaterev = -1;
} else if (empty($CFG->templaterev)) {
$templaterev = 1;
} else {
$templaterev = $CFG->templaterev;
}

return $templaterev;
}

/**
* Ensure that the specified JavaScript file is linked to from this page.
*
Expand Down Expand Up @@ -2104,6 +2123,23 @@ public function define_patched_core_modules($combobase, $yuiversion, $patchlevel
}
}

/**
* Invalidate all server and client side template caches.
*/
function template_reset_all_caches() {
global $CFG;

$next = time();
if (isset($CFG->templaterev) and $next <= $CFG->templaterev and $CFG->templaterev - $next < 60 * 60) {
// This resolves problems when reset is requested repeatedly within 1s,
// the < 1h condition prevents accidental switching to future dates
// because we might not recover from it.
$next = $CFG->templaterev + 1;
}

set_config('templaterev', $next);
}

/**
* Invalidate all server and client side JS caches.
*/
Expand Down

0 comments on commit e63395b

Please sign in to comment.