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
This commit is contained in:
Timo Tijhof
2016-08-22 22:26:15 -07:00
parent 694f3719a8
commit f06ac3efca
2 changed files with 9 additions and 93 deletions

View File

@@ -1,6 +1,6 @@
<?php
/**
* Resource loader module for UniversalLanguageSelector
* ResourceLoader module for UniversalLanguageSelector
*
* Copyright (C) 2012 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris,
* Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland and other
@@ -20,7 +20,7 @@
*/
/**
* Resource loader module for providing MediaWiki language names.
* ResourceLoader module for UniversalLanguageSelector
*/
class ResourceLoaderULSModule extends ResourceLoaderModule {
protected $targets = [ 'desktop', 'mobile' ];
@@ -31,13 +31,12 @@ class ResourceLoaderULSModule extends ResourceLoaderModule {
* @param string $languageCode Language code
* @return array
*/
protected function getData( $languageCode ) {
private function getData( $languageCode ) {
$vars = [];
$vars['wgULSLanguages'] = Language::fetchLanguageNames(
$languageCode,
'mwfile'
);
return $vars;
}
@@ -47,49 +46,15 @@ class ResourceLoaderULSModule extends ResourceLoaderModule {
*/
public function getScript( ResourceLoaderContext $context ) {
$languageCode = $context->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;
}
}

View File

@@ -1,49 +0,0 @@
<?php
/**
* PHP Unit tests for ResourceLoaderULSModule class.
*
* @file
* @ingroup Extensions
*
* @author Santhosh Thottingal
*/
/**
* @covers ResourceLoaderULSModule
*/
class ResourceLoaderULSModuleMemcachedTest extends MediaWikiTestCase {
/**
* Test whether the modified time of the RL module varies
* correctly with language code.
* @covers ResourceLoaderSchemaModule::getModifiedTime
*/
public function testModifiedTime() {
$request = new WebRequest();
$module = new ResourceLoaderULSModule();
$request->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' );
}
}