ResourceLoaderULSModule::getModifiedTime updates continuously

Fix is simple: cache the hash of content per language

Added PHPUnit test for the same

Bug: 56856
Change-Id: I590b27af220d6e790c70728062d1a04c098b3d11
This commit is contained in:
Niklas Laxström
2013-11-11 09:04:59 +00:00
committed by Santhosh Thottingal
parent 6cd178359e
commit bd52ae0538
2 changed files with 75 additions and 15 deletions

View File

@@ -23,21 +23,19 @@
* Resource loader module for providing MediaWiki language names. * Resource loader module for providing MediaWiki language names.
*/ */
class ResourceLoaderULSModule extends ResourceLoaderModule { class ResourceLoaderULSModule extends ResourceLoaderModule {
/**
* @var Language
*/
protected $language;
protected $targets = array( 'desktop', 'mobile' ); protected $targets = array( 'desktop', 'mobile' );
/** /**
* Get all the dynamic data for the content language to an array * Get all the dynamic data for the content language to an array.
* *
* @param string $languageCode Language code
* @return array * @return array
*/ */
protected function getData() { protected function getData( $languageCode ) {
$vars = array(); $vars = array();
$vars['wgULSLanguages'] = Language::fetchLanguageNames( $vars['wgULSLanguages'] = Language::fetchLanguageNames(
$this->language->getCode(), 'mwfile' $languageCode,
'mwfile'
); );
return $vars; return $vars;
@@ -45,12 +43,12 @@ class ResourceLoaderULSModule extends ResourceLoaderModule {
/** /**
* @param $context ResourceLoaderContext * @param $context ResourceLoaderContext
* @return string: JavaScript code * @return string JavaScript code
*/ */
public function getScript( ResourceLoaderContext $context ) { public function getScript( ResourceLoaderContext $context ) {
$this->language = Language::factory( $context->getLanguage() ); $languageCode = $context->getLanguage();
$out = ''; $out = '';
foreach ( $this->getData() as $key => $value ) { foreach ( $this->getData( $languageCode ) as $key => $value ) {
$out .= Xml::encodeJsCall( 'mw.config.set', array( $key, $value ) ); $out .= Xml::encodeJsCall( 'mw.config.set', array( $key, $value ) );
} }
@@ -58,15 +56,28 @@ class ResourceLoaderULSModule extends ResourceLoaderModule {
} }
/** /**
* Gets the last modified time for this module depending on the given
* context.
*
* @param $context ResourceLoaderContext * @param $context ResourceLoaderContext
* @return array|int|Mixed * @return int Unix timestamp
*/ */
public function getModifiedTime( ResourceLoaderContext $context ) { public function getModifiedTime( ResourceLoaderContext $context ) {
$this->language = Language::factory( $context->getLanguage() ); $languageCode = $context->getLanguage();
$cache = wfGetCache( CACHE_ANYTHING );
$key = wfMemcKey( 'resourceloader', 'ulsmodule', 'changeinfo' );
$data = $this->getData(); $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(
'resourceloader',
'modulemodifiedhash',
$this->getName(),
$languageCode
);
$data = $this->getData( $languageCode );
$hash = md5( serialize( $data ) ); $hash = md5( serialize( $data ) );
$result = $cache->get( $key ); $result = $cache->get( $key );

View File

@@ -0,0 +1,49 @@
<?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
*/
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" );
}
}