* Fix inaccurate class comment that was copied from an unrelated module in MediaWiki core. * Remove use of deprecated getModifiedTime(). While this implementation was straight forward, it is more reliable and deterministic to use a content hash because timestamps are not tracked by Git. This currently causes needless cache invalidation for all users at Wikimedia every week because git-clone starts with fresh timestamps on disk. I'm not using enableModuleContentVersion() right now because calling getScript() reads all files into memory which is more expensive than simply hashing the files. Especially because safeFileHash() is backed by an APC cache via FileContentsHasher. This matches the implementation in core ResourceLoaderFileModule. Change-Id: If9aaefa6a3cf047c6c36e1cdd0f350412d59c849
70 lines
1.7 KiB
PHP
70 lines
1.7 KiB
PHP
<?php
|
|
/**
|
|
* ResourceLoader module for client-side loading of json-based localization.
|
|
*
|
|
* @file
|
|
* @ingroup Extensions
|
|
* @author Santhosh Thottingal
|
|
*/
|
|
|
|
/**
|
|
* ResourceLoader module for client-side loading of json-based localization.
|
|
*/
|
|
class ResourceLoaderULSJsonMessageModule extends ResourceLoaderModule {
|
|
/**
|
|
* Part of the ResourceLoader module interface.
|
|
* Declares the core ext.uls.i18n module as a dependency.
|
|
* @param ResourceLoaderContext $context
|
|
* @return string[] Module names.
|
|
*/
|
|
public function getDependencies( ResourceLoaderContext $context = null ) {
|
|
return [ 'ext.uls.i18n' ];
|
|
}
|
|
|
|
/**
|
|
* Get supported mobile targets
|
|
* @return string[] supported targets
|
|
*/
|
|
public function getTargets() {
|
|
return [ 'desktop', 'mobile' ];
|
|
}
|
|
|
|
/**
|
|
* @param ResourceLoaderContext $context
|
|
* @return array
|
|
*/
|
|
public function getDefinitionSummary( ResourceLoaderContext $context ) {
|
|
$code = $context->getLanguage();
|
|
if ( !Language::isValidCode( $code ) ) {
|
|
$code = 'en';
|
|
}
|
|
$fileHashes = array_map(
|
|
[ __CLASS__, 'safeFileHash' ],
|
|
ULSJsonMessageLoader::getFilenames( $code )
|
|
);
|
|
|
|
$summary = parent::getDefinitionSummary( $context );
|
|
$summary[] = [
|
|
'fileHashes' => $fileHashes
|
|
];
|
|
return $summary;
|
|
}
|
|
|
|
/**
|
|
* Get the message strings for the current UI language. Uses
|
|
* mw.uls.loadLocalization to register them on the frontend.
|
|
* @param ResourceLoaderContext $context
|
|
* @return string JavaScript code.
|
|
*/
|
|
public function getScript( ResourceLoaderContext $context ) {
|
|
$code = $context->getLanguage();
|
|
if ( !Language::isValidCode( $code ) ) {
|
|
$code = 'en';
|
|
}
|
|
|
|
$params = [ $code, ULSJsonMessageLoader::getMessages( $code ) ];
|
|
|
|
return Xml::encodeJsCall( 'mw.uls.loadLocalization', $params );
|
|
}
|
|
}
|