(bug 25201) Respect $wgCacheEpoch in the resource loader
authorRoan Kattouw <catrope@users.mediawiki.org>
Thu, 7 Oct 2010 19:35:05 +0000 (19:35 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Thu, 7 Oct 2010 19:35:05 +0000 (19:35 +0000)
* 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
includes/ResourceLoader.php
includes/ResourceLoaderModule.php

index 722b487..c5dcff3 100644 (file)
@@ -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;
index 363b3a1..e73f443 100644 (file)
@@ -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' ) {
index bda0795..5cfcc63 100644 (file)
@@ -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 ) );
                }