| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use DateTimeZone; |
| 7 |
use Exception; |
| 8 |
use MediaWiki\Language\Language; |
| 9 |
use MediaWiki\Language\LanguageCode; |
| 10 |
use MediaWiki\Languages\LanguageNameUtils; |
| 11 |
use MediaWiki\MainConfigNames; |
| 12 |
use MediaWiki\MediaWikiServices; |
| 13 |
use MediaWiki\Title\Title; |
| 14 |
use MediaWiki\User\User; |
| 15 |
use MediaWiki\Utils\MWTimestamp; |
| 16 |
use Wikimedia\RequestTimeout\TimeoutException; |
| 17 |
|
| 18 |
class LanguageLibrary extends LibraryBase { |
| 19 |
|
| 20 |
public $langCache = []; |
| 21 |
|
| 22 |
public $timeCache = []; |
| 23 |
|
| 24 |
public $maxLangCacheSize; |
| 25 |
|
| 26 |
public function register() { |
| 27 |
|
| 28 |
$contLang = MediaWikiServices::getInstance()->getContentLanguage(); |
| 29 |
$this->langCache[$contLang->getCode()] = $contLang; |
| 30 |
$this->maxLangCacheSize = $this->getEngine()->getOption( 'maxLangCacheSize' ); |
| 31 |
|
| 32 |
$statics = [ |
| 33 |
'getContLangCode', |
| 34 |
'isSupportedLanguage', |
| 35 |
'isKnownLanguageTag', |
| 36 |
'isValidCode', |
| 37 |
'isValidBuiltInCode', |
| 38 |
'fetchLanguageName', |
| 39 |
'fetchLanguageNames', |
| 40 |
'getFallbacksFor', |
| 41 |
'toBcp47Code', |
| 42 |
]; |
| 43 |
$methods = [ |
| 44 |
'lcfirst', |
| 45 |
'ucfirst', |
| 46 |
'lc', |
| 47 |
'uc', |
| 48 |
'caseFold', |
| 49 |
'formatNum', |
| 50 |
'formatDate', |
| 51 |
'formatDuration', |
| 52 |
'getDurationIntervals', |
| 53 |
'parseFormattedNumber', |
| 54 |
'convertPlural', |
| 55 |
'convertGrammar', |
| 56 |
'gender', |
| 57 |
'isRTL', |
| 58 |
]; |
| 59 |
$lib = []; |
| 60 |
foreach ( $statics as $name ) { |
| 61 |
$lib[$name] = [ $this, $name ]; |
| 62 |
} |
| 63 |
foreach ( $methods as $name ) { |
| 64 |
$lib[$name] = function () use ( $name ) { |
| 65 |
$args = func_get_args(); |
| 66 |
return $this->languageMethod( $name, $args ); |
| 67 |
}; |
| 68 |
} |
| 69 |
return $this->getEngine()->registerInterface( 'mw.language.lua', $lib ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public function getContLangCode() { |
| 78 |
return [ MediaWikiServices::getInstance()->getContentLanguage()->getCode() ]; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public function isSupportedLanguage( $code ) { |
| 88 |
$this->checkType( 'isSupportedLanguage', 1, $code, 'string' ); |
| 89 |
return [ MediaWikiServices::getInstance()->getLanguageNameUtils()->isSupportedLanguage( $code ) ]; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public function isKnownLanguageTag( $code ) { |
| 99 |
$this->checkType( 'isKnownLanguageTag', 1, $code, 'string' ); |
| 100 |
return [ MediaWikiServices::getInstance()->getLanguageNameUtils()->isKnownLanguageTag( $code ) ]; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function isValidCode( $code ) { |
| 110 |
$this->checkType( 'isValidCode', 1, $code, 'string' ); |
| 111 |
return [ MediaWikiServices::getInstance()->getLanguageNameUtils()->isValidCode( $code ) ]; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function isValidBuiltInCode( $code ) { |
| 121 |
$this->checkType( 'isValidBuiltInCode', 1, $code, 'string' ); |
| 122 |
return [ MediaWikiServices::getInstance()->getLanguageNameUtils()->isValidBuiltInCode( $code ) ]; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public function fetchLanguageName( $code, $inLanguage ) { |
| 133 |
$this->checkType( 'fetchLanguageName', 1, $code, 'string' ); |
| 134 |
$this->checkTypeOptional( 'fetchLanguageName', 2, $inLanguage, 'string', LanguageNameUtils::AUTONYMS ); |
| 135 |
return [ MediaWikiServices::getInstance()->getLanguageNameUtils() |
| 136 |
->getLanguageName( $code, $inLanguage ) ]; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function fetchLanguageNames( $inLanguage, $include ) { |
| 147 |
$this->checkTypeOptional( 'fetchLanguageNames', 1, $inLanguage, 'string', LanguageNameUtils::AUTONYMS ); |
| 148 |
$this->checkTypeOptional( 'fetchLanguageNames', 2, $include, 'string', LanguageNameUtils::DEFINED ); |
| 149 |
return [ MediaWikiServices::getInstance()->getLanguageNameUtils() |
| 150 |
->getLanguageNames( $inLanguage, $include ) ]; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function getFallbacksFor( $code ) { |
| 160 |
$this->checkType( 'getFallbacksFor', 1, $code, 'string' ); |
| 161 |
$ret = MediaWikiServices::getInstance()->getLanguageFallback()->getAll( $code ); |
| 162 |
|
| 163 |
if ( count( $ret ) ) { |
| 164 |
$ret = array_combine( range( 1, count( $ret ) ), $ret ); |
| 165 |
} |
| 166 |
return [ $ret ]; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function toBcp47Code( $code ) { |
| 176 |
$this->checkType( 'toBcp47Code', 1, $code, 'string' ); |
| 177 |
$ret = LanguageCode::bcp47( $code ); |
| 178 |
return [ $ret ]; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function languageMethod( string $name, array $args ): array { |
| 190 |
if ( !is_string( $args[0] ?? null ) ) { |
| 191 |
throw new LuaError( |
| 192 |
"invalid code property of language object when calling $name" |
| 193 |
); |
| 194 |
} |
| 195 |
$code = array_shift( $args ); |
| 196 |
if ( !isset( $this->langCache[$code] ) ) { |
| 197 |
if ( count( $this->langCache ) > $this->maxLangCacheSize ) { |
| 198 |
throw new LuaError( 'too many language codes requested' ); |
| 199 |
} |
| 200 |
$services = MediaWikiServices::getInstance(); |
| 201 |
if ( $services->getLanguageNameUtils()->isValidCode( $code ) ) { |
| 202 |
$this->langCache[$code] = $services->getLanguageFactory()->getLanguage( $code ); |
| 203 |
} else { |
| 204 |
throw new LuaError( "language code '$code' is invalid" ); |
| 205 |
} |
| 206 |
} |
| 207 |
$lang = $this->langCache[$code]; |
| 208 |
switch ( $name ) { |
| 209 |
|
| 210 |
case 'isRTL': |
| 211 |
return [ $lang->$name() ]; |
| 212 |
|
| 213 |
|
| 214 |
case 'lcfirst': |
| 215 |
case 'ucfirst': |
| 216 |
case 'lc': |
| 217 |
case 'uc': |
| 218 |
case 'caseFold': |
| 219 |
$this->checkType( $name, 1, $args[0], 'string' ); |
| 220 |
return [ $lang->$name( $args[0] ) ]; |
| 221 |
|
| 222 |
case 'parseFormattedNumber': |
| 223 |
if ( is_numeric( $args[0] ) ) { |
| 224 |
$args[0] = strval( $args[0] ); |
| 225 |
} |
| 226 |
if ( $this->getLuaType( $args[0] ) !== 'string' ) { |
| 227 |
|
| 228 |
return [ null ]; |
| 229 |
} |
| 230 |
return [ $lang->$name( $args[0] ) ]; |
| 231 |
|
| 232 |
|
| 233 |
default: |
| 234 |
return $this->$name( $lang, $args ); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public function convertPlural( $lang, $args ) { |
| 246 |
$number = array_shift( $args ); |
| 247 |
$this->checkType( 'convertPlural', 1, $number, 'number' ); |
| 248 |
if ( is_array( $args[0] ) ) { |
| 249 |
$args = $args[0]; |
| 250 |
} |
| 251 |
$forms = array_values( array_map( 'strval', $args ) ); |
| 252 |
return [ $lang->convertPlural( $number, $forms ) ]; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
public function convertGrammar( $lang, $args ) { |
| 263 |
$this->checkType( 'convertGrammar', 1, $args[0], 'string' ); |
| 264 |
$this->checkType( 'convertGrammar', 2, $args[1], 'string' ); |
| 265 |
return [ $lang->convertGrammar( $args[0], $args[1] ) ]; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public function gender( $lang, $args ) { |
| 276 |
$this->checkType( 'gender', 1, $args[0], 'string' ); |
| 277 |
$username = trim( array_shift( $args ) ); |
| 278 |
|
| 279 |
if ( is_array( $args[0] ) ) { |
| 280 |
$args = $args[0]; |
| 281 |
} |
| 282 |
$forms = array_values( array_map( 'strval', $args ) ); |
| 283 |
|
| 284 |
|
| 285 |
if ( count( $forms ) === 0 ) { |
| 286 |
return [ '' ]; |
| 287 |
} elseif ( count( $forms ) === 1 ) { |
| 288 |
return [ $forms[0] ]; |
| 289 |
} |
| 290 |
|
| 291 |
if ( $username === 'male' || $username === 'female' ) { |
| 292 |
$gender = $username; |
| 293 |
} else { |
| 294 |
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup(); |
| 295 |
|
| 296 |
$gender = $userOptionsLookup->getDefaultOption( 'gender' ); |
| 297 |
|
| 298 |
|
| 299 |
$title = Title::newFromText( $username ); |
| 300 |
if ( $title && $title->getNamespace() === NS_USER ) { |
| 301 |
$username = $title->getText(); |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
$user = User::newFromName( $username ); |
| 306 |
if ( $user ) { |
| 307 |
$genderCache = MediaWikiServices::getInstance()->getGenderCache(); |
| 308 |
$gender = $genderCache->getGenderOf( $user, __METHOD__ ); |
| 309 |
} elseif ( $username === '' ) { |
| 310 |
$parserOptions = $this->getParserOptions(); |
| 311 |
if ( $parserOptions->getInterfaceMessage() ) { |
| 312 |
$genderCache = MediaWikiServices::getInstance()->getGenderCache(); |
| 313 |
$gender = $genderCache->getGenderOf( $parserOptions->getUserIdentity(), __METHOD__ ); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
return [ $lang->gender( $gender, $forms ) ]; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
public function formatNum( $lang, $args ) { |
| 328 |
$num = $args[0]; |
| 329 |
$this->checkType( 'formatNum', 1, $num, 'number' ); |
| 330 |
if ( is_infinite( $num ) ) { |
| 331 |
throw new LuaError( "bad argument #1 to 'formatNum' (infinite)" ); |
| 332 |
} |
| 333 |
if ( is_nan( $num ) ) { |
| 334 |
throw new LuaError( "bad argument #1 to 'formatNum' (NaN)" ); |
| 335 |
} |
| 336 |
|
| 337 |
$noCommafy = false; |
| 338 |
if ( isset( $args[1] ) ) { |
| 339 |
$this->checkType( 'formatNum', 2, $args[1], 'table' ); |
| 340 |
$options = $args[1]; |
| 341 |
$noCommafy = !empty( $options['noCommafy'] ); |
| 342 |
} |
| 343 |
if ( $noCommafy ) { |
| 344 |
return [ $lang->formatNumNoSeparators( $num ) ]; |
| 345 |
} else { |
| 346 |
return [ $lang->formatNum( $num ) ]; |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public function formatDate( $lang, $args ) { |
| 359 |
$this->checkType( 'formatDate', 1, $args[0], 'string' ); |
| 360 |
$this->checkTypeOptional( 'formatDate', 2, $args[1], 'string', '' ); |
| 361 |
$this->checkTypeOptional( 'formatDate', 3, $args[2], 'boolean', false ); |
| 362 |
|
| 363 |
[ $format, $date, $local ] = $args; |
| 364 |
$langcode = $lang->getCode(); |
| 365 |
|
| 366 |
if ( $date === '' ) { |
| 367 |
$cacheKey = $this->getParserOptions()->getTimestamp(); |
| 368 |
$timestamp = new MWTimestamp( $cacheKey ); |
| 369 |
$date = $timestamp->getTimestamp( TS_ISO_8601 ); |
| 370 |
$useTTL = true; |
| 371 |
} else { |
| 372 |
|
| 373 |
if ( preg_match( '/^[0-9]{4}$/', $date ) ) { |
| 374 |
$date = '00:00 ' . $date; |
| 375 |
} |
| 376 |
|
| 377 |
$cacheKey = $date; |
| 378 |
$useTTL = false; |
| 379 |
} |
| 380 |
|
| 381 |
if ( isset( $this->timeCache[$format][$cacheKey][$langcode][$local] ) ) { |
| 382 |
$ttl = $this->timeCache[$format][$cacheKey][$langcode][$local][1]; |
| 383 |
if ( $useTTL && $ttl !== null ) { |
| 384 |
$this->getEngine()->setTTL( $ttl ); |
| 385 |
} |
| 386 |
return [ $this->timeCache[$format][$cacheKey][$langcode][$local][0] ]; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
try { |
| 391 |
$utc = new DateTimeZone( 'UTC' ); |
| 392 |
$dateObject = new DateTime( $date, $utc ); |
| 393 |
} catch ( TimeoutException $ex ) { |
| 394 |
|
| 395 |
|
| 396 |
throw $ex; |
| 397 |
} catch ( Exception $ex ) { |
| 398 |
throw new LuaError( "bad argument #2 to 'formatDate': invalid timestamp '$date'" ); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
if ( $local ) { |
| 403 |
$localtimezone = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Localtimezone ); |
| 404 |
if ( isset( $localtimezone ) ) { |
| 405 |
$tz = new DateTimeZone( $localtimezone ); |
| 406 |
} else { |
| 407 |
$tz = new DateTimeZone( date_default_timezone_get() ); |
| 408 |
} |
| 409 |
} else { |
| 410 |
$tz = $utc; |
| 411 |
} |
| 412 |
$dateObject->setTimezone( $tz ); |
| 413 |
|
| 414 |
$ts = $dateObject->format( 'YmdHis' ); |
| 415 |
|
| 416 |
if ( $ts < 0 ) { |
| 417 |
throw new LuaError( "mw.language:formatDate() only supports years from 0" ); |
| 418 |
} elseif ( $ts >= 100000000000000 ) { |
| 419 |
throw new LuaError( "mw.language:formatDate() only supports years up to 9999" ); |
| 420 |
} |
| 421 |
|
| 422 |
$ttl = null; |
| 423 |
$ret = $lang->sprintfDate( $format, $ts, $tz, $ttl ); |
| 424 |
$this->timeCache[$format][$cacheKey][$langcode][$local] = [ $ret, $ttl ]; |
| 425 |
if ( $useTTL && $ttl !== null ) { |
| 426 |
$this->getEngine()->setTTL( $ttl ); |
| 427 |
} |
| 428 |
return [ $ret ]; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
public function formatDuration( $lang, $args ) { |
| 439 |
$this->checkType( 'formatDuration', 1, $args[0], 'number' ); |
| 440 |
$this->checkTypeOptional( 'formatDuration', 2, $args[1], 'table', [] ); |
| 441 |
|
| 442 |
[ $seconds, $chosenIntervals ] = $args; |
| 443 |
$chosenIntervals = array_values( $chosenIntervals ); |
| 444 |
|
| 445 |
$ret = $lang->formatDuration( $seconds, $chosenIntervals ); |
| 446 |
return [ $ret ]; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
public function getDurationIntervals( $lang, $args ) { |
| 457 |
$this->checkType( 'getDurationIntervals', 1, $args[0], 'number' ); |
| 458 |
$this->checkTypeOptional( 'getDurationIntervals', 2, $args[1], 'table', [] ); |
| 459 |
|
| 460 |
[ $seconds, $chosenIntervals ] = $args; |
| 461 |
$chosenIntervals = array_values( $chosenIntervals ); |
| 462 |
|
| 463 |
$ret = $lang->getDurationIntervals( $seconds, $chosenIntervals ); |
| 464 |
return [ $ret ]; |
| 465 |
} |
| 466 |
} |