Load ULS language list from a resource loader module

Having them in the page source of every page view is wasteful.

Bug: 41210
Change-Id: If15e297a4781cc49ded06fbc906529f57a22aafe
This commit is contained in:
Niklas Laxström
2013-04-26 12:57:31 +00:00
committed by Gerrit Code Review
parent 60242e2c53
commit 4554c79d51
4 changed files with 84 additions and 3 deletions

View File

@@ -0,0 +1,78 @@
<?php
/**
* Resource loader 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
* contributors. See CREDITS for a list.
*
* UniversalLanguageSelector is dual licensed GPLv2 or later and MIT. You don't
* have to do anything special to choose one license or the other and you don't
* have to notify anyone which license you are using. You are free to use
* UniversalLanguageSelector in commercial projects as long as the copyright
* header is left intact. See files GPL-LICENSE and MIT-LICENSE for details.
*
* @file
* @author Niklas Laxström
* @ingroup Extensions
* @licence GNU General Public Licence 2.0 or later
* @licence MIT License
*/
/**
* Resource loader module for providing MediaWiki language names.
*/
class ResourceLoaderULSModule extends ResourceLoaderModule {
protected $language;
protected $targets = array( 'desktop', 'mobile' );
/**
* Get all the dynamic data for the content language to an array
*
* @return array
*/
protected function getData() {
$vars = array();
$vars['wgULSLanguages'] = Language::fetchLanguageNames(
$this->language->getCode(), 'mwfile'
);
return $vars;
}
/**
* @param $context ResourceLoaderContext
* @return string: JavaScript code
*/
public function getScript( ResourceLoaderContext $context ) {
$this->language = Language::factory( $context->getLanguage() );
$out = '';
foreach ( $this->getData() as $key => $value ) {
$out .= Xml::encodeJsCall( 'mw.config.set', array( $key, $value ) );
}
return $out;
}
/**
* @param $context ResourceLoaderContext
* @return array|int|Mixed
*/
public function getModifiedTime( ResourceLoaderContext $context ) {
$this->language = Language::factory( $context->getLanguage() );
$cache = wfGetCache( CACHE_ANYTHING );
$key = wfMemcKey( 'resourceloader', 'ulsmodule', 'changeinfo' );
$data = $this->getData();
$hash = md5( serialize( $data ) );
$result = $cache->get( $key );
if ( is_array( $result ) && $result['hash'] === $hash ) {
return $result['timestamp'];
}
$timestamp = wfTimestamp();
$cache->set( $key, array(
'hash' => $hash,
'timestamp' => $timestamp,
) );
return $timestamp;
}
}

View File

@@ -11,6 +11,10 @@ $resourcePaths = array(
'remoteExtPath' => 'UniversalLanguageSelector' 'remoteExtPath' => 'UniversalLanguageSelector'
); );
$wgResourceModules['ext.uls.languagenames'] = array(
'class' => 'ResourceLoaderULSModule'
);
$wgResourceModules['ext.uls.displaysettings'] = array( $wgResourceModules['ext.uls.displaysettings'] = array(
'scripts' => 'resources/js/ext.uls.displaysettings.js', 'scripts' => 'resources/js/ext.uls.displaysettings.js',
'styles' => 'resources/css/ext.uls.displaysettings.css', 'styles' => 'resources/css/ext.uls.displaysettings.css',
@@ -41,6 +45,7 @@ $wgResourceModules['ext.uls.init'] = array(
'monobook' => 'resources/css/ext.uls-monobook.css', 'monobook' => 'resources/css/ext.uls-monobook.css',
), ),
'dependencies' => array( 'dependencies' => array(
'ext.uls.languagenames',
'mediawiki.Uri', 'mediawiki.Uri',
'mediawiki.util', 'mediawiki.util',
'jquery.json', 'jquery.json',

View File

@@ -281,9 +281,6 @@ class UniversalLanguageSelectorHooks {
*/ */
public static function addVariables( &$vars, OutputPage $out ) { public static function addVariables( &$vars, OutputPage $out ) {
// Place request context dependent stuff here // Place request context dependent stuff here
$vars['wgULSLanguages'] = Language::fetchLanguageNames(
$out->getLanguage()->getCode(), 'mwfile'
);
$vars['wgULSAcceptLanguageList'] = array_keys( $out->getRequest()->getAcceptLang() ); $vars['wgULSAcceptLanguageList'] = array_keys( $out->getRequest()->getAcceptLang() );
return true; return true;

View File

@@ -123,6 +123,7 @@ $wgExtensionMessagesFiles['UniversalLanguageSelector'] = "$dir/UniversalLanguage
// Register auto load for the page class // Register auto load for the page class
$wgAutoloadClasses['UniversalLanguageSelectorHooks'] = "$dir/UniversalLanguageSelector.hooks.php"; $wgAutoloadClasses['UniversalLanguageSelectorHooks'] = "$dir/UniversalLanguageSelector.hooks.php";
$wgAutoloadClasses['ResourceLoaderULSModule'] = "$dir/ResourceLoaderULSModule.php";
$wgAutoloadClasses['ApiLanguageSearch'] = "$dir/api/ApiLanguageSearch.php"; $wgAutoloadClasses['ApiLanguageSearch'] = "$dir/api/ApiLanguageSearch.php";
$wgAutoloadClasses['LanguageNameSearch'] = "$dir/data/LanguageNameSearch.php"; $wgAutoloadClasses['LanguageNameSearch'] = "$dir/data/LanguageNameSearch.php";