From 8e65282ff67ebf152ecd4478890eaec6814e6ef3 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Thu, 7 Oct 2010 19:35:05 +0000 Subject: [PATCH] (bug 25201) Respect $wgCacheEpoch in the resource loader * The mtime of each module is now maxed with $wgCacheEpoch before being used. I chose to make this the caller's responsibility rather than getModifiedTime()'s, otherwise custom modules can (and inevitably will) forget to respect $wgCacheEpoch * Invalidate message blobs if they're older than $wgCacheEpoch --- includes/MessageBlobStore.php | 6 +++++- includes/ResourceLoader.php | 4 ++-- includes/ResourceLoaderModule.php | 12 +++++++----- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/includes/MessageBlobStore.php b/includes/MessageBlobStore.php index 722b4877b3..c5dcff38ca 100644 --- a/includes/MessageBlobStore.php +++ b/includes/MessageBlobStore.php @@ -311,6 +311,7 @@ class MessageBlobStore { * @return array Array mapping module names to blobs */ private static function getFromDB( ResourceLoader $resourceLoader, $modules, $lang ) { + global $wgCacheEpoch; $retval = array(); $dbr = wfGetDB( DB_SLAVE ); $res = $dbr->select( 'msg_resource', @@ -325,7 +326,10 @@ class MessageBlobStore { // This shouldn't be possible throw new MWException( __METHOD__ . ' passed an invalid module name' ); } - if ( array_keys( FormatJson::decode( $row->mr_blob, true ) ) !== $module->getMessages() ) { + // Update the module's blobs if the set of messages changed or if the blob is + // older than $wgCacheEpoch + if ( array_keys( FormatJson::decode( $row->mr_blob, true ) ) !== $module->getMessages() || + wfTimestamp( TS_MW, $row->mr_timestamp ) <= $wgCacheEpoch ) { $retval[$row->mr_resource] = self::updateModule( $row->mr_resource, $lang ); } else { $retval[$row->mr_resource] = $row->mr_blob; diff --git a/includes/ResourceLoader.php b/includes/ResourceLoader.php index 363b3a1918..e73f4433ce 100644 --- a/includes/ResourceLoader.php +++ b/includes/ResourceLoader.php @@ -240,7 +240,7 @@ class ResourceLoader { * @param $context ResourceLoaderContext object */ public function respond( ResourceLoaderContext $context ) { - global $wgResourceLoaderMaxage; + global $wgResourceLoaderMaxage, $wgCacheEpoch; wfProfileIn( __METHOD__ ); @@ -275,7 +275,7 @@ class ResourceLoader { // To send Last-Modified and support If-Modified-Since, we need to detect // the last modified time wfProfileIn( __METHOD__.'-getModifiedTime' ); - $mtime = 1; + $mtime = $wgCacheEpoch; foreach ( $modules as $module ) { // Bypass squid cache if the request includes any private modules if ( $module->getGroup() === 'private' ) { diff --git a/includes/ResourceLoaderModule.php b/includes/ResourceLoaderModule.php index bda0795ad7..5cfcc639ba 100644 --- a/includes/ResourceLoaderModule.php +++ b/includes/ResourceLoaderModule.php @@ -1059,6 +1059,7 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule { * @return String: JavaScript code for registering all modules with the client loader */ public static function getModuleRegistrations( ResourceLoaderContext $context ) { + global $wgCacheEpoch; wfProfileIn( __METHOD__ ); $out = ''; @@ -1073,22 +1074,23 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule { } // Automatically register module else { + $mtime = max( $module->getModifiedTime( $context ), $wgCacheEpoch ); // Modules without dependencies or a group pass two arguments (name, timestamp) to // mediaWiki.loader.register() if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) { - $registrations[] = array( $name, $module->getModifiedTime( $context ) ); + $registrations[] = array( $name, $mtime ); } // Modules with dependencies but no group pass three arguments (name, timestamp, dependencies) // to mediaWiki.loader.register() else if ( $module->getGroup() === null ) { $registrations[] = array( - $name, $module->getModifiedTime( $context ), $module->getDependencies() ); + $name, $mtime, $module->getDependencies() ); } // Modules with dependencies pass four arguments (name, timestamp, dependencies, group) // to mediaWiki.loader.register() else { $registrations[] = array( - $name, $module->getModifiedTime( $context ), $module->getDependencies(), $module->getGroup() ); + $name, $mtime, $module->getDependencies(), $module->getGroup() ); } } } @@ -1134,7 +1136,7 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule { } public function getModifiedTime( ResourceLoaderContext $context ) { - global $IP; + global $IP, $wgCacheEpoch; $hash = $context->getHash(); if ( isset( $this->modifiedTime[$hash] ) ) { @@ -1144,7 +1146,7 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule { // ATTENTION!: Because of the line above, this is not going to cause infinite recursion - think carefully // before making changes to this code! - $time = 1; // wfTimestamp() treats 0 as 'now', so that's not a suitable choice + $time = $wgCacheEpoch; foreach ( $context->getResourceLoader()->getModules() as $module ) { $time = max( $time, $module->getModifiedTime( $context ) ); } -- 2.20.1