ResourceLoaderStartUpModule: Use hashMtime to detect config changes
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 20 Mar 2014 06:04:48 +0000 (07:04 +0100)
committerTimo Tijhof <krinklemail@gmail.com>
Thu, 20 Mar 2014 06:04:48 +0000 (07:04 +0100)
Changes to the static startup.js file are covered by the file
mtime in #getModifiedTime.

Changes to the module registrations are covered by the max
module mtime loop in #getModifiedTime.

But changes to LocalSettings.php etc. affecting mw.config values
embedded in #getScript aren't traceable. Resort to hashing those
and detecting changes between calls that way.

Without this, changes to mw.config never made it live (not even
after the rebuild every 5 minutes) because of 304 Not Modified
being sent between Apache and Varnish. Changes to config either
didn't matter to front-end right away and made it with the next
deployment that changes a different module, or by touching
startup.js to force a version bump.

Simple third party installs were not affected by this bug as their
settings would only be coming from LocalSettings.php (as opposed
to included files) and $wgInvalidateCacheOnLocalSettingsChange is
true by default.

Bug: 28899
Change-Id: I484166923d97368d95beeb40bb209dec9b849033

includes/resourceloader/ResourceLoaderStartUpModule.php

index a551c4a..1e2f65c 100644 (file)
@@ -27,6 +27,7 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
        /* Protected Members */
 
        protected $modifiedTime = array();
+       protected $configVars = array();
        protected $targets = array( 'desktop', 'mobile' );
 
        /* Protected Methods */
@@ -36,6 +37,12 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
         * @return array
         */
        protected function getConfig( $context ) {
+
+               $hash = $context->getHash();
+               if ( isset( $this->configVars[$hash] ) ) {
+                       return $this->configVars[$hash];
+               }
+
                global $wgLoadScript, $wgScript, $wgStylePath, $wgScriptExtension,
                        $wgArticlePath, $wgScriptPath, $wgServer, $wgContLang,
                        $wgVariantArticlePath, $wgActionPaths, $wgVersion,
@@ -106,7 +113,8 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
 
                wfRunHooks( 'ResourceLoaderGetConfigVars', array( &$vars ) );
 
-               return $vars;
+               $this->configVars[$hash] = $vars;
+               return $this->configVars[$hash];
        }
 
        /**
@@ -278,7 +286,8 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
 
                $time = max(
                        wfTimestamp( TS_UNIX, $wgCacheEpoch ),
-                       filemtime( "$IP/resources/startup.js" )
+                       filemtime( "$IP/resources/startup.js" ),
+                       $this->getHashMtime( $context )
                );
 
                // ATTENTION!: Because of the line below, this is not going to cause
@@ -297,6 +306,25 @@ class ResourceLoaderStartUpModule extends ResourceLoaderModule {
                return $this->modifiedTime[$hash];
        }
 
+       /**
+        * Hash of all dynamic data embedded in getScript().
+        *
+        * Detect changes to mw.config settings embedded in #getScript (bug 28899).
+        *
+        * @param $context ResourceLoaderContext
+        * @return string: Hash
+        */
+       public function getModifiedHash( ResourceLoaderContext $context ) {
+               global $wgLegacyJavaScriptGlobals;
+
+               $data = array(
+                       'vars' => $this->getConfig( $context ),
+                       'wgLegacyJavaScriptGlobals' => $wgLegacyJavaScriptGlobals,
+               );
+
+               return md5( serialize( $data ) );
+       }
+
        /**
         * @return string
         */