r74454 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r74453‎ | r74454 | r74455 >
Date:19:35, 7 October 2010
Author:catrope
Status:resolved (Comments)
Tags:
Comment:
(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
Modified paths:
  • /trunk/phase3/includes/MessageBlobStore.php (modified) (history)
  • /trunk/phase3/includes/ResourceLoader.php (modified) (history)
  • /trunk/phase3/includes/ResourceLoaderModule.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/ResourceLoader.php
@@ -240,7 +240,7 @@
241241 * @param $context ResourceLoaderContext object
242242 */
243243 public function respond( ResourceLoaderContext $context ) {
244 - global $wgResourceLoaderMaxage;
 244+ global $wgResourceLoaderMaxage, $wgCacheEpoch;
245245
246246 wfProfileIn( __METHOD__ );
247247
@@ -275,7 +275,7 @@
276276 // To send Last-Modified and support If-Modified-Since, we need to detect
277277 // the last modified time
278278 wfProfileIn( __METHOD__.'-getModifiedTime' );
279 - $mtime = 1;
 279+ $mtime = $wgCacheEpoch;
280280 foreach ( $modules as $module ) {
281281 // Bypass squid cache if the request includes any private modules
282282 if ( $module->getGroup() === 'private' ) {
Index: trunk/phase3/includes/MessageBlobStore.php
@@ -311,6 +311,7 @@
312312 * @return array Array mapping module names to blobs
313313 */
314314 private static function getFromDB( ResourceLoader $resourceLoader, $modules, $lang ) {
 315+ global $wgCacheEpoch;
315316 $retval = array();
316317 $dbr = wfGetDB( DB_SLAVE );
317318 $res = $dbr->select( 'msg_resource',
@@ -325,7 +326,10 @@
326327 // This shouldn't be possible
327328 throw new MWException( __METHOD__ . ' passed an invalid module name' );
328329 }
329 - if ( array_keys( FormatJson::decode( $row->mr_blob, true ) ) !== $module->getMessages() ) {
 330+ // Update the module's blobs if the set of messages changed or if the blob is
 331+ // older than $wgCacheEpoch
 332+ if ( array_keys( FormatJson::decode( $row->mr_blob, true ) ) !== $module->getMessages() ||
 333+ wfTimestamp( TS_MW, $row->mr_timestamp ) <= $wgCacheEpoch ) {
330334 $retval[$row->mr_resource] = self::updateModule( $row->mr_resource, $lang );
331335 } else {
332336 $retval[$row->mr_resource] = $row->mr_blob;
Index: trunk/phase3/includes/ResourceLoaderModule.php
@@ -1059,6 +1059,7 @@
10601060 * @return String: JavaScript code for registering all modules with the client loader
10611061 */
10621062 public static function getModuleRegistrations( ResourceLoaderContext $context ) {
 1063+ global $wgCacheEpoch;
10631064 wfProfileIn( __METHOD__ );
10641065
10651066 $out = '';
@@ -1073,22 +1074,23 @@
10741075 }
10751076 // Automatically register module
10761077 else {
 1078+ $mtime = max( $module->getModifiedTime( $context ), $wgCacheEpoch );
10771079 // Modules without dependencies or a group pass two arguments (name, timestamp) to
10781080 // mediaWiki.loader.register()
10791081 if ( !count( $module->getDependencies() && $module->getGroup() === null ) ) {
1080 - $registrations[] = array( $name, $module->getModifiedTime( $context ) );
 1082+ $registrations[] = array( $name, $mtime );
10811083 }
10821084 // Modules with dependencies but no group pass three arguments (name, timestamp, dependencies)
10831085 // to mediaWiki.loader.register()
10841086 else if ( $module->getGroup() === null ) {
10851087 $registrations[] = array(
1086 - $name, $module->getModifiedTime( $context ), $module->getDependencies() );
 1088+ $name, $mtime, $module->getDependencies() );
10871089 }
10881090 // Modules with dependencies pass four arguments (name, timestamp, dependencies, group)
10891091 // to mediaWiki.loader.register()
10901092 else {
10911093 $registrations[] = array(
1092 - $name, $module->getModifiedTime( $context ), $module->getDependencies(), $module->getGroup() );
 1094+ $name, $mtime, $module->getDependencies(), $module->getGroup() );
10931095 }
10941096 }
10951097 }
@@ -1134,7 +1136,7 @@
11351137 }
11361138
11371139 public function getModifiedTime( ResourceLoaderContext $context ) {
1138 - global $IP;
 1140+ global $IP, $wgCacheEpoch;
11391141
11401142 $hash = $context->getHash();
11411143 if ( isset( $this->modifiedTime[$hash] ) ) {
@@ -1144,7 +1146,7 @@
11451147
11461148 // ATTENTION!: Because of the line above, this is not going to cause infinite recursion - think carefully
11471149 // before making changes to this code!
1148 - $time = 1; // wfTimestamp() treats 0 as 'now', so that's not a suitable choice
 1150+ $time = $wgCacheEpoch;
11491151 foreach ( $context->getResourceLoader()->getModules() as $module ) {
11501152 $time = max( $time, $module->getModifiedTime( $context ) );
11511153 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r74502Fix r74454: convert $wgCacheEpoch to a UNIX timestamp before comparing it wit...catrope10:25, 8 October 2010

Comments

#Comment by Brion VIBBER (talk | contribs)   04:59, 8 October 2010

It looks like ResourceLoaderModule::getModifiedTime() returns a numeric Unix timestamp, while $wgCacheEpoch is customarily a string in TS_MW format. These need to be normalized before being compared.

#Comment by Catrope (talk | contribs)   10:25, 8 October 2010

Good catch! Fixed in r74502.

Status & tagging log