Skip to content

Commit

Permalink
MDL-41460 core_component: Validate cache against version.php
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederic Massart committed Sep 5, 2013
1 parent ee78814 commit 3274c5d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
10 changes: 10 additions & 0 deletions admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@
}

require('../config.php');

// Invalidate the cache of version.php in any circumstances to help core_component
// detecting if the version has changed and component cache should be reset.
if (function_exists('opcache_invalidate')) {
opcache_invalidate($CFG->dirroot . '/version.php', true);
}
// Make sure the component cache gets rebuilt if necessary, any method that
// indirectly calls the protected init() method is good here.
core_component::get_core_subsystems();

require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions
require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions
require_once($CFG->libdir.'/pluginlib.php'); // available updates notifications
Expand Down
29 changes: 25 additions & 4 deletions lib/classes/component.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class core_component {
protected static $classmap = null;
/** @var null list of some known files that can be included. */
protected static $filemap = null;
/** @var int|float core version. */
protected static $version = null;
/** @var array list of the files to map. */
protected static $filestomap = array('lib.php', 'settings.php');

Expand Down Expand Up @@ -133,8 +135,11 @@ protected static function init() {
include($cachefile);
if (!is_array($cache)) {
// Something is very wrong.
} else if (!isset($cache['plugintypes']) or !isset($cache['plugins']) or !isset($cache['subsystems']) or !isset($cache['classmap'])) {
} else if (!isset($cache['version'])) {
// Something is very wrong.
} else if ((float) $cache['version'] !== (float) self::fetch_core_version()) {
// Outdated cache. We trigger an error log to track an eventual repetitive failure of float comparison.
error_log('Resetting core_component cache after core upgrade to version ' . self::fetch_core_version());
} else if ($cache['plugintypes']['mod'] !== "$CFG->dirroot/mod") {
// $CFG->dirroot was changed.
} else {
Expand Down Expand Up @@ -227,6 +232,7 @@ public static function get_cache_content() {
'plugins' => self::$plugins,
'classmap' => self::$classmap,
'filemap' => self::$filemap,
'version' => self::$version,
);

return '<?php
Expand All @@ -249,6 +255,23 @@ protected static function fill_all_caches() {

self::fill_classmap_cache();
self::fill_filemap_cache();
self::fetch_core_version();
}

/**
* Get the core version.
*
* In order for this to work properly, opcache should be reset beforehand.
*
* @return float core version.
*/
protected static function fetch_core_version() {
global $CFG;
if (self::$version === null) {
require($CFG->dirroot . '/version.php');
self::$version = $version;
}
return self::$version;
}

/**
Expand Down Expand Up @@ -867,9 +890,7 @@ public static function get_all_versions_hash() {
$versions = array();

// Main version first.
$version = null;
include($CFG->dirroot.'/version.php');
$versions['core'] = $version;
$versions['core'] = self::fetch_core_version();

// The problem here is tha the component cache might be stable,
// we want this to work also on frontpage without resetting the component cache.
Expand Down

0 comments on commit 3274c5d

Please sign in to comment.