From f06ac3efca82d3376f5a031f10444f1c782ae543 Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Mon, 22 Aug 2016 22:26:15 -0700 Subject: [PATCH] Simplify ResourceLoaderULSModule with content-based versioning * Remove manual tracking of when a hash is first seen. ResourceLoader is no longer time-based, rather it is content based. Except where a timestamp is actually wanted as key, a hash is all we need. The default implementation of simply calling getScript() and hashing its output suffices, and isn't a performance problem in this case. * Also simplify getScript() by passing an object to 'mw.config.set'. Instead of multiple calls for each key/value. This is a no-op now because there is only one key. * Fix inaccurate class comment that was copied from an unrelated module in MediaWiki core. Change-Id: I9bb82cadd9caa7e584e20dd49ce30b64218326c4 --- includes/ResourceLoaderULSModule.php | 53 +++++-------------------- tests/phpunit/ResourceLoaderULSTest.php | 49 ----------------------- 2 files changed, 9 insertions(+), 93 deletions(-) delete mode 100644 tests/phpunit/ResourceLoaderULSTest.php diff --git a/includes/ResourceLoaderULSModule.php b/includes/ResourceLoaderULSModule.php index c0c44395..be839f9a 100644 --- a/includes/ResourceLoaderULSModule.php +++ b/includes/ResourceLoaderULSModule.php @@ -1,6 +1,6 @@ getLanguage(); - $out = ''; - foreach ( $this->getData( $languageCode ) as $key => $value ) { - $out .= Xml::encodeJsCall( 'mw.config.set', [ $key, $value ] ); - } - - return $out; + return Xml::encodeJsCall( 'mw.config.set', [ + $this->getData( $languageCode ) + ] ); } /** - * Gets the last modified time for this module depending on the given - * context. - * - * @param $context ResourceLoaderContext - * @return int Unix timestamp + * @return bool */ - public function getModifiedTime( ResourceLoaderContext $context ) { - $languageCode = $context->getLanguage(); - - $cache = wfGetCache( CACHE_ANYTHING ); - - // Since we are updating the timestamp on hash change, we need to - // cache the hash per language to avoid updating the timestamp when - // different languages are being requested. - $key = wfMemcKey( - 'uls', - 'modulemodifiedhash', - $this->getName(), - $languageCode - ); - - $data = $this->getData( $languageCode ); - $hash = md5( serialize( $data ) ); - - $result = $cache->get( $key ); - if ( is_array( $result ) && $result['hash'] === $hash ) { - return $result['timestamp']; - } - $timestamp = wfTimestamp(); - $cache->set( $key, [ - 'hash' => $hash, - 'timestamp' => $timestamp, - ] ); - - return $timestamp; + public function enableModuleContentVersion() { + return true; } } diff --git a/tests/phpunit/ResourceLoaderULSTest.php b/tests/phpunit/ResourceLoaderULSTest.php deleted file mode 100644 index f567420e..00000000 --- a/tests/phpunit/ResourceLoaderULSTest.php +++ /dev/null @@ -1,49 +0,0 @@ -setVal( 'lang', 'he' ); - $context = new ResourceLoaderContext( - new ResourceLoader(), $request ); - $mtimeHebrew = $module->getModifiedTime( $context ); - // sleep for 1 second - sleep( 1 ); - $request->setVal( 'lang', 'hi' ); - $context = new ResourceLoaderContext( new ResourceLoader(), $request ); - $mtimeHindi = $module->getModifiedTime( $context ); - $this->assertGreaterThan( $mtimeHebrew, $mtimeHindi, 'Hindi has recent timestamp than Hebrew' ); - - // sleep for 1 second - sleep( 1 ); - $request->setVal( 'lang', 'he' ); - $context = new ResourceLoaderContext( new ResourceLoader(), $request ); - $mtimeHebrewNew = $module->getModifiedTime( $context ); - $this->assertEquals( $mtimeHebrewNew, $mtimeHebrew, 'Hebrew timestamp remained same' ); - - // sleep for 1 second - sleep( 1 ); - $request->setVal( 'lang', 'hi' ); - $context = new ResourceLoaderContext( new ResourceLoader(), $request ); - $mtimeHindiNew = $module->getModifiedTime( $context ); - $this->assertEquals( $mtimeHindi, $mtimeHindiNew, 'Hindi timestamp remained same' ); - } -}