2013-01-25 19:03:04 +00:00
|
|
|
<?php
|
|
|
|
|
2022-08-03 12:31:15 +00:00
|
|
|
namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use DateTimeZone;
|
|
|
|
use Exception;
|
|
|
|
use Language;
|
2022-12-29 12:43:27 +00:00
|
|
|
use MediaWiki\Languages\LanguageNameUtils;
|
2019-08-19 20:24:40 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-08-19 04:19:07 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-08-03 12:31:15 +00:00
|
|
|
use MWTimestamp;
|
|
|
|
use User;
|
2019-08-19 20:24:40 +00:00
|
|
|
|
2022-08-03 12:31:15 +00:00
|
|
|
class LanguageLibrary extends LibraryBase {
|
2020-12-18 19:04:17 +00:00
|
|
|
/** @var Language[] */
|
2017-06-15 17:19:00 +00:00
|
|
|
public $langCache = [];
|
2020-12-18 19:04:17 +00:00
|
|
|
/** @var array */
|
2017-06-15 17:19:00 +00:00
|
|
|
public $timeCache = [];
|
2020-12-18 19:04:17 +00:00
|
|
|
/** @var int */
|
2017-03-20 02:59:23 +00:00
|
|
|
public $maxLangCacheSize;
|
2013-01-25 19:03:04 +00:00
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
public function register() {
|
2013-01-25 19:03:04 +00:00
|
|
|
// Pre-populate the language cache
|
2020-03-14 19:57:09 +00:00
|
|
|
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
|
|
|
|
$this->langCache[$contLang->getCode()] = $contLang;
|
2017-03-20 02:59:23 +00:00
|
|
|
$this->maxLangCacheSize = $this->getEngine()->getOption( 'maxLangCacheSize' );
|
2013-01-25 19:03:04 +00:00
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
$statics = [
|
2013-01-25 19:03:04 +00:00
|
|
|
'getContLangCode',
|
|
|
|
'isSupportedLanguage',
|
|
|
|
'isKnownLanguageTag',
|
|
|
|
'isValidCode',
|
|
|
|
'isValidBuiltInCode',
|
|
|
|
'fetchLanguageName',
|
2013-12-10 17:22:09 +00:00
|
|
|
'fetchLanguageNames',
|
2013-12-16 19:12:47 +00:00
|
|
|
'getFallbacksFor',
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
|
|
|
$methods = [
|
2013-01-25 19:03:04 +00:00
|
|
|
'lcfirst',
|
|
|
|
'ucfirst',
|
|
|
|
'lc',
|
|
|
|
'uc',
|
|
|
|
'caseFold',
|
|
|
|
'formatNum',
|
|
|
|
'formatDate',
|
2013-03-04 23:59:37 +00:00
|
|
|
'formatDuration',
|
|
|
|
'getDurationIntervals',
|
2013-01-25 19:03:04 +00:00
|
|
|
'parseFormattedNumber',
|
|
|
|
'convertPlural',
|
|
|
|
'convertGrammar',
|
|
|
|
'gender',
|
|
|
|
'isRTL',
|
2017-06-15 17:19:00 +00:00
|
|
|
];
|
|
|
|
$lib = [];
|
2013-01-25 19:03:04 +00:00
|
|
|
foreach ( $statics as $name ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
$lib[$name] = [ $this, $name ];
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
foreach ( $methods as $name ) {
|
2021-05-15 00:03:24 +00:00
|
|
|
$lib[$name] = function () use ( $name ) {
|
2013-01-25 19:03:04 +00:00
|
|
|
$args = func_get_args();
|
2021-05-15 00:03:24 +00:00
|
|
|
return $this->languageMethod( $name, $args );
|
2013-01-25 19:03:04 +00:00
|
|
|
};
|
|
|
|
}
|
2014-09-26 18:11:21 +00:00
|
|
|
return $this->getEngine()->registerInterface( 'mw.language.lua', $lib );
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for getContLangCode
|
|
|
|
* @internal
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getContLangCode() {
|
2020-03-14 19:57:09 +00:00
|
|
|
return [ MediaWikiServices::getInstance()->getContentLanguage()->getCode() ];
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for isSupportedLanguage
|
|
|
|
* @internal
|
|
|
|
* @param string $code
|
|
|
|
* @return bool[]
|
|
|
|
*/
|
|
|
|
public function isSupportedLanguage( $code ) {
|
2013-01-25 19:03:04 +00:00
|
|
|
$this->checkType( 'isSupportedLanguage', 1, $code, 'string' );
|
2022-12-28 13:17:19 +00:00
|
|
|
return [ MediaWikiServices::getInstance()->getLanguageNameUtils()->isSupportedLanguage( $code ) ];
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for isKnownLanguageTag
|
|
|
|
* @internal
|
|
|
|
* @param string $code
|
|
|
|
* @return bool[]
|
|
|
|
*/
|
|
|
|
public function isKnownLanguageTag( $code ) {
|
2013-01-25 19:03:04 +00:00
|
|
|
$this->checkType( 'isKnownLanguageTag', 1, $code, 'string' );
|
2022-12-28 13:17:19 +00:00
|
|
|
return [ MediaWikiServices::getInstance()->getLanguageNameUtils()->isKnownLanguageTag( $code ) ];
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for isValidCode
|
|
|
|
* @internal
|
|
|
|
* @param string $code
|
|
|
|
* @return bool[]
|
|
|
|
*/
|
|
|
|
public function isValidCode( $code ) {
|
2013-01-25 19:03:04 +00:00
|
|
|
$this->checkType( 'isValidCode', 1, $code, 'string' );
|
2022-12-28 13:17:19 +00:00
|
|
|
return [ MediaWikiServices::getInstance()->getLanguageNameUtils()->isValidCode( $code ) ];
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for isValidBuiltInCode
|
|
|
|
* @internal
|
|
|
|
* @param string $code
|
|
|
|
* @return bool[]
|
|
|
|
*/
|
|
|
|
public function isValidBuiltInCode( $code ) {
|
2013-01-25 19:03:04 +00:00
|
|
|
$this->checkType( 'isValidBuiltInCode', 1, $code, 'string' );
|
2022-12-28 13:17:19 +00:00
|
|
|
return [ MediaWikiServices::getInstance()->getLanguageNameUtils()->isValidBuiltInCode( $code ) ];
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for fetchLanguageName
|
|
|
|
* @internal
|
|
|
|
* @param string $code
|
|
|
|
* @param null|string $inLanguage
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function fetchLanguageName( $code, $inLanguage ) {
|
2013-01-25 19:03:04 +00:00
|
|
|
$this->checkType( 'fetchLanguageName', 1, $code, 'string' );
|
2022-12-29 12:43:27 +00:00
|
|
|
$this->checkTypeOptional( 'fetchLanguageName', 2, $inLanguage, 'string', LanguageNameUtils::AUTONYMS );
|
2022-12-27 16:21:10 +00:00
|
|
|
return [ MediaWikiServices::getInstance()->getLanguageNameUtils()
|
|
|
|
->getLanguageName( $code, $inLanguage ) ];
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for fetchLanguageNames
|
|
|
|
* @internal
|
|
|
|
* @param null|string $inLanguage
|
|
|
|
* @param null|string $include
|
2022-08-22 09:00:38 +00:00
|
|
|
* @return string[][]
|
2018-11-09 19:31:08 +00:00
|
|
|
*/
|
|
|
|
public function fetchLanguageNames( $inLanguage, $include ) {
|
2022-12-29 12:43:27 +00:00
|
|
|
$this->checkTypeOptional( 'fetchLanguageNames', 1, $inLanguage, 'string', LanguageNameUtils::AUTONYMS );
|
|
|
|
$this->checkTypeOptional( 'fetchLanguageNames', 2, $include, 'string', LanguageNameUtils::DEFINED );
|
2022-12-27 16:21:10 +00:00
|
|
|
return [ MediaWikiServices::getInstance()->getLanguageNameUtils()
|
|
|
|
->getLanguageNames( $inLanguage, $include ) ];
|
2013-12-10 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for fetchLanguageNames
|
|
|
|
* @internal
|
|
|
|
* @param string $code
|
|
|
|
* @return string[][]
|
|
|
|
*/
|
|
|
|
public function getFallbacksFor( $code ) {
|
2015-06-26 16:37:34 +00:00
|
|
|
$this->checkType( 'getFallbacksFor', 1, $code, 'string' );
|
2022-12-28 14:57:37 +00:00
|
|
|
$ret = MediaWikiServices::getInstance()->getLanguageFallback()->getAll( $code );
|
2013-12-16 19:12:47 +00:00
|
|
|
// Make 1-based
|
|
|
|
if ( count( $ret ) ) {
|
|
|
|
$ret = array_combine( range( 1, count( $ret ) ), $ret );
|
|
|
|
}
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $ret ];
|
2013-12-16 19:12:47 +00:00
|
|
|
}
|
|
|
|
|
2013-01-25 19:03:04 +00:00
|
|
|
/**
|
|
|
|
* Language object method handler
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param string $name
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
2022-08-03 12:31:15 +00:00
|
|
|
* @throws LuaError
|
2013-01-25 19:03:04 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function languageMethod( $name, $args ) {
|
2013-01-25 19:03:04 +00:00
|
|
|
$name = strval( $name );
|
|
|
|
$code = array_shift( $args );
|
|
|
|
if ( !isset( $this->langCache[$code] ) ) {
|
2017-03-20 02:59:23 +00:00
|
|
|
if ( count( $this->langCache ) > $this->maxLangCacheSize ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( 'too many language codes requested' );
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
2022-12-28 13:17:19 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
|
|
|
if ( $services->getLanguageNameUtils()->isValidCode( $code ) ) {
|
|
|
|
$this->langCache[$code] = $services->getLanguageFactory()->getLanguage( $code );
|
2022-11-18 19:37:45 +00:00
|
|
|
} else {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( "language code '$code' is invalid" );
|
2014-03-05 16:30:09 +00:00
|
|
|
}
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
$lang = $this->langCache[$code];
|
|
|
|
switch ( $name ) {
|
|
|
|
// Zero arguments
|
|
|
|
case 'isRTL':
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $lang->$name() ];
|
2013-01-25 19:03:04 +00:00
|
|
|
|
|
|
|
// One string argument passed straight through
|
|
|
|
case 'lcfirst':
|
|
|
|
case 'ucfirst':
|
|
|
|
case 'lc':
|
|
|
|
case 'uc':
|
|
|
|
case 'caseFold':
|
|
|
|
$this->checkType( $name, 1, $args[0], 'string' );
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $lang->$name( $args[0] ) ];
|
2013-01-25 19:03:04 +00:00
|
|
|
|
|
|
|
case 'parseFormattedNumber':
|
|
|
|
if ( is_numeric( $args[0] ) ) {
|
|
|
|
$args[0] = strval( $args[0] );
|
|
|
|
}
|
2013-07-03 16:01:41 +00:00
|
|
|
if ( $this->getLuaType( $args[0] ) !== 'string' ) {
|
|
|
|
// Be like tonumber(), return nil instead of erroring out
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ null ];
|
2013-07-03 16:01:41 +00:00
|
|
|
}
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $lang->$name( $args[0] ) ];
|
2013-01-25 19:03:04 +00:00
|
|
|
|
|
|
|
// Custom handling
|
|
|
|
default:
|
|
|
|
return $this->$name( $lang, $args );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* convertPlural handler
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param Language $lang
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
2013-01-25 19:03:04 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function convertPlural( $lang, $args ) {
|
2013-01-25 19:03:04 +00:00
|
|
|
$number = array_shift( $args );
|
|
|
|
$this->checkType( 'convertPlural', 1, $number, 'number' );
|
|
|
|
if ( is_array( $args[0] ) ) {
|
|
|
|
$args = $args[0];
|
|
|
|
}
|
|
|
|
$forms = array_values( array_map( 'strval', $args ) );
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $lang->convertPlural( $number, $forms ) ];
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* convertGrammar handler
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param Language $lang
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
2013-01-25 19:03:04 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function convertGrammar( $lang, $args ) {
|
2013-01-25 19:03:04 +00:00
|
|
|
$this->checkType( 'convertGrammar', 1, $args[0], 'string' );
|
|
|
|
$this->checkType( 'convertGrammar', 2, $args[1], 'string' );
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $lang->convertGrammar( $args[0], $args[1] ) ];
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gender handler
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param Language $lang
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
2013-01-25 19:03:04 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function gender( $lang, $args ) {
|
2013-01-25 19:03:04 +00:00
|
|
|
$this->checkType( 'gender', 1, $args[0], 'string' );
|
2014-03-05 16:30:09 +00:00
|
|
|
$username = trim( array_shift( $args ) );
|
2013-01-25 19:03:04 +00:00
|
|
|
|
|
|
|
if ( is_array( $args[0] ) ) {
|
|
|
|
$args = $args[0];
|
|
|
|
}
|
|
|
|
$forms = array_values( array_map( 'strval', $args ) );
|
|
|
|
|
|
|
|
// Shortcuts
|
|
|
|
if ( count( $forms ) === 0 ) {
|
2018-04-09 04:39:06 +00:00
|
|
|
return [ '' ];
|
2013-01-25 19:03:04 +00:00
|
|
|
} elseif ( count( $forms ) === 1 ) {
|
2018-04-09 04:39:06 +00:00
|
|
|
return [ $forms[0] ];
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $username === 'male' || $username === 'female' ) {
|
|
|
|
$gender = $username;
|
|
|
|
} else {
|
2021-03-01 10:44:44 +00:00
|
|
|
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
|
2013-01-25 19:03:04 +00:00
|
|
|
// default
|
2021-03-01 10:44:44 +00:00
|
|
|
$gender = $userOptionsLookup->getDefaultOption( 'gender' );
|
2013-01-25 19:03:04 +00:00
|
|
|
|
|
|
|
// Check for "User:" prefix
|
|
|
|
$title = Title::newFromText( $username );
|
2021-01-01 16:44:40 +00:00
|
|
|
if ( $title && $title->getNamespace() === NS_USER ) {
|
2013-01-25 19:03:04 +00:00
|
|
|
$username = $title->getText();
|
|
|
|
}
|
|
|
|
|
|
|
|
// check parameter, or use the ParserOptions if in interface message
|
|
|
|
$user = User::newFromName( $username );
|
|
|
|
if ( $user ) {
|
2019-08-19 20:24:40 +00:00
|
|
|
$genderCache = MediaWikiServices::getInstance()->getGenderCache();
|
|
|
|
$gender = $genderCache->getGenderOf( $user, __METHOD__ );
|
2013-01-25 19:03:04 +00:00
|
|
|
} elseif ( $username === '' ) {
|
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
if ( $parserOptions->getInterfaceMessage() ) {
|
2019-08-19 20:24:40 +00:00
|
|
|
$genderCache = MediaWikiServices::getInstance()->getGenderCache();
|
2021-10-05 21:47:40 +00:00
|
|
|
$gender = $genderCache->getGenderOf( $parserOptions->getUserIdentity(), __METHOD__ );
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $lang->gender( $gender, $forms ) ];
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* formatNum handler
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param Language $lang
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
2013-01-25 19:03:04 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function formatNum( $lang, $args ) {
|
2013-01-25 19:03:04 +00:00
|
|
|
$num = $args[0];
|
|
|
|
$this->checkType( 'formatNum', 1, $num, 'number' );
|
2020-11-09 21:04:41 +00:00
|
|
|
if ( is_infinite( $num ) ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( "bad argument #1 to 'formatNum' (infinite)" );
|
2020-11-09 21:04:41 +00:00
|
|
|
}
|
|
|
|
if ( is_nan( $num ) ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( "bad argument #1 to 'formatNum' (NaN)" );
|
2020-11-09 21:04:41 +00:00
|
|
|
}
|
2013-01-25 19:03:04 +00:00
|
|
|
|
|
|
|
$noCommafy = false;
|
|
|
|
if ( isset( $args[1] ) ) {
|
|
|
|
$this->checkType( 'formatNum', 2, $args[1], 'table' );
|
|
|
|
$options = $args[1];
|
|
|
|
$noCommafy = !empty( $options['noCommafy'] );
|
|
|
|
}
|
2020-09-09 22:17:23 +00:00
|
|
|
if ( $noCommafy ) {
|
|
|
|
return [ $lang->formatNumNoSeparators( $num ) ];
|
|
|
|
} else {
|
|
|
|
return [ $lang->formatNum( $num ) ];
|
|
|
|
}
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* formatDate handler
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param Language $lang
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
2022-08-03 12:31:15 +00:00
|
|
|
* @throws LuaError
|
2013-01-25 19:03:04 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function formatDate( $lang, $args ) {
|
2013-01-25 19:03:04 +00:00
|
|
|
$this->checkType( 'formatDate', 1, $args[0], 'string' );
|
|
|
|
$this->checkTypeOptional( 'formatDate', 2, $args[1], 'string', '' );
|
|
|
|
$this->checkTypeOptional( 'formatDate', 3, $args[2], 'boolean', false );
|
|
|
|
|
|
|
|
list( $format, $date, $local ) = $args;
|
|
|
|
$langcode = $lang->getCode();
|
|
|
|
|
|
|
|
if ( $date === '' ) {
|
|
|
|
$cacheKey = $this->getParserOptions()->getTimestamp();
|
|
|
|
$timestamp = new MWTimestamp( $cacheKey );
|
|
|
|
$date = $timestamp->getTimestamp( TS_ISO_8601 );
|
2014-06-01 03:15:42 +00:00
|
|
|
$useTTL = true;
|
2013-01-25 19:03:04 +00:00
|
|
|
} else {
|
|
|
|
# Correct for DateTime interpreting 'XXXX' as XX:XX o'clock
|
|
|
|
if ( preg_match( '/^[0-9]{4}$/', $date ) ) {
|
2018-09-03 18:08:53 +00:00
|
|
|
$date = '00:00 ' . $date;
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$cacheKey = $date;
|
2014-06-01 03:15:42 +00:00
|
|
|
$useTTL = false;
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $this->timeCache[$format][$cacheKey][$langcode][$local] ) ) {
|
2014-06-01 03:15:42 +00:00
|
|
|
$ttl = $this->timeCache[$format][$cacheKey][$langcode][$local][1];
|
|
|
|
if ( $useTTL && $ttl !== null ) {
|
|
|
|
$this->getEngine()->setTTL( $ttl );
|
|
|
|
}
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $this->timeCache[$format][$cacheKey][$langcode][$local][0] ];
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Default input timezone is UTC.
|
|
|
|
try {
|
|
|
|
$utc = new DateTimeZone( 'UTC' );
|
|
|
|
$dateObject = new DateTime( $date, $utc );
|
|
|
|
} catch ( Exception $ex ) {
|
2015-07-09 19:05:12 +00:00
|
|
|
throw new LuaError( "bad argument #2 to 'formatDate': invalid timestamp '$date'" );
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Set output timezone.
|
|
|
|
if ( $local ) {
|
2013-03-11 18:55:03 +00:00
|
|
|
global $wgLocaltimezone;
|
2013-01-25 19:03:04 +00:00
|
|
|
if ( isset( $wgLocaltimezone ) ) {
|
|
|
|
$tz = new DateTimeZone( $wgLocaltimezone );
|
|
|
|
} else {
|
|
|
|
$tz = new DateTimeZone( date_default_timezone_get() );
|
|
|
|
}
|
|
|
|
} else {
|
2013-03-22 16:54:58 +00:00
|
|
|
$tz = $utc;
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
2013-03-22 16:54:58 +00:00
|
|
|
$dateObject->setTimezone( $tz );
|
2013-01-25 19:03:04 +00:00
|
|
|
# Generate timestamp
|
|
|
|
$ts = $dateObject->format( 'YmdHis' );
|
|
|
|
|
2013-04-19 21:26:03 +00:00
|
|
|
if ( $ts < 0 ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( "mw.language:formatDate() only supports years from 0" );
|
2013-04-19 21:26:03 +00:00
|
|
|
} elseif ( $ts >= 100000000000000 ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( "mw.language:formatDate() only supports years up to 9999" );
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
|
|
|
|
2014-06-01 03:15:42 +00:00
|
|
|
$ttl = null;
|
|
|
|
$ret = $lang->sprintfDate( $format, $ts, $tz, $ttl );
|
2017-06-15 17:19:00 +00:00
|
|
|
$this->timeCache[$format][$cacheKey][$langcode][$local] = [ $ret, $ttl ];
|
2014-06-01 03:15:42 +00:00
|
|
|
if ( $useTTL && $ttl !== null ) {
|
|
|
|
$this->getEngine()->setTTL( $ttl );
|
|
|
|
}
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $ret ];
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|
2013-03-04 23:59:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* formatDuration handler
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param Language $lang
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
2013-03-04 23:59:37 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function formatDuration( $lang, $args ) {
|
2013-03-04 23:59:37 +00:00
|
|
|
$this->checkType( 'formatDuration', 1, $args[0], 'number' );
|
2017-06-15 17:19:00 +00:00
|
|
|
$this->checkTypeOptional( 'formatDuration', 2, $args[1], 'table', [] );
|
2013-03-04 23:59:37 +00:00
|
|
|
|
|
|
|
list( $seconds, $chosenIntervals ) = $args;
|
|
|
|
$chosenIntervals = array_values( $chosenIntervals );
|
|
|
|
|
|
|
|
$ret = $lang->formatDuration( $seconds, $chosenIntervals );
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $ret ];
|
2013-03-04 23:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* getDurationIntervals handler
|
2018-11-09 19:31:08 +00:00
|
|
|
* @internal
|
2018-01-10 17:45:07 +00:00
|
|
|
* @param Language $lang
|
|
|
|
* @param array $args
|
|
|
|
* @return array
|
2013-03-04 23:59:37 +00:00
|
|
|
*/
|
2018-11-09 19:31:08 +00:00
|
|
|
public function getDurationIntervals( $lang, $args ) {
|
2013-03-04 23:59:37 +00:00
|
|
|
$this->checkType( 'getDurationIntervals', 1, $args[0], 'number' );
|
2017-06-15 17:19:00 +00:00
|
|
|
$this->checkTypeOptional( 'getDurationIntervals', 2, $args[1], 'table', [] );
|
2013-03-04 23:59:37 +00:00
|
|
|
|
|
|
|
list( $seconds, $chosenIntervals ) = $args;
|
|
|
|
$chosenIntervals = array_values( $chosenIntervals );
|
|
|
|
|
|
|
|
$ret = $lang->getDurationIntervals( $seconds, $chosenIntervals );
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $ret ];
|
2013-03-04 23:59:37 +00:00
|
|
|
}
|
2013-01-25 19:03:04 +00:00
|
|
|
}
|