Files
mediawiki-extensions-Univer…/ResourceLoaderULSModule.php
Amir E. Aharoni c9a5ec7f19 Add backward compatibility for fetchLanguageNames
Bug: 46737
Change-Id: Ife7c2834c2b2c4f2619b4a5d8e0b0aa1db595984
2013-04-30 16:01:47 +03:00

85 lines
2.4 KiB
PHP

<?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();
if ( method_exists( 'Language', 'fetchLanguageNames' ) ) {
// since 1.20
$vars['wgULSLanguages'] = Language::fetchLanguageNames(
$this->language->getCode(), 'mwfile'
);
} else {
$vars['wgULSLanguages'] = Language::getLanguageNames( false );
}
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;
}
}