2013-02-08 14:56:44 +00:00
|
|
|
<?php
|
|
|
|
|
2022-08-03 12:31:15 +00:00
|
|
|
namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
|
|
|
|
|
2023-05-19 10:31:10 +00:00
|
|
|
use MediaWiki\Language\RawMessage;
|
2020-03-14 19:57:09 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2024-06-10 18:41:33 +00:00
|
|
|
use MediaWiki\Message\Message;
|
2020-03-14 19:57:09 +00:00
|
|
|
|
2022-08-03 12:31:15 +00:00
|
|
|
class MessageLibrary extends LibraryBase {
|
2018-11-09 19:31:08 +00:00
|
|
|
public function register() {
|
2017-06-15 17:19:00 +00:00
|
|
|
$lib = [
|
|
|
|
'plain' => [ $this, 'messagePlain' ],
|
|
|
|
'check' => [ $this, 'messageCheck' ],
|
|
|
|
];
|
2013-02-08 14:56:44 +00:00
|
|
|
|
|
|
|
// Get the correct default language from the parser
|
|
|
|
if ( $this->getParser() ) {
|
|
|
|
$lang = $this->getParser()->getTargetLanguage();
|
|
|
|
} else {
|
2020-03-14 19:57:09 +00:00
|
|
|
$lang = MediaWikiServices::getInstance()->getContentLanguage();
|
2013-02-08 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
return $this->getEngine()->registerInterface( 'mw.message.lua', $lib, [
|
2013-02-08 14:56:44 +00:00
|
|
|
'lang' => $lang->getCode(),
|
2017-06-15 17:19:00 +00:00
|
|
|
] );
|
2013-02-08 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 18:50:34 +00:00
|
|
|
/**
|
|
|
|
* Create a Message
|
|
|
|
* @param array $data
|
|
|
|
* - 'rawMessage': (string, optional) If set, create a RawMessage using this as `$text`
|
|
|
|
* - 'keys': (string|string[]) Message keys. Required unless 'rawMessage' is set.
|
|
|
|
* - 'lang': (Language|StubUserLang|string) Language for the Message.
|
|
|
|
* - 'useDB': (bool) "Use database" flag.
|
|
|
|
* - 'params': (array) Parameters for the Message. May be omitted if $setParams is false.
|
|
|
|
* @param bool $setParams Whether to use $data['params']
|
|
|
|
* @return Message
|
|
|
|
*/
|
2013-02-08 14:56:44 +00:00
|
|
|
private function makeMessage( $data, $setParams ) {
|
|
|
|
if ( isset( $data['rawMessage'] ) ) {
|
|
|
|
$msg = new RawMessage( $data['rawMessage'] );
|
|
|
|
} else {
|
|
|
|
$msg = Message::newFallbackSequence( $data['keys'] );
|
|
|
|
}
|
2022-12-28 13:17:19 +00:00
|
|
|
if ( is_string( $data['lang'] ) &&
|
|
|
|
!MediaWikiServices::getInstance()->getLanguageNameUtils()->isValidCode( $data['lang'] )
|
|
|
|
) {
|
2022-11-18 19:37:45 +00:00
|
|
|
throw new LuaError( "language code '{$data['lang']}' is invalid" );
|
|
|
|
} else {
|
|
|
|
$msg->inLanguage( $data['lang'] );
|
|
|
|
}
|
|
|
|
$msg->useDatabase( $data['useDB'] );
|
2013-02-08 14:56:44 +00:00
|
|
|
if ( $setParams ) {
|
2024-06-12 22:57:45 +00:00
|
|
|
foreach ( $data['params'] as $param ) {
|
|
|
|
// Only rawParam and numParam are supposed by the Lua message API
|
|
|
|
if ( is_array( $param ) && isset( $param['raw'] ) ) {
|
|
|
|
$msg->rawParams( $param );
|
|
|
|
} elseif ( is_array( $param ) && isset( $param['num'] ) ) {
|
|
|
|
$msg->numParams( $param );
|
|
|
|
} else {
|
|
|
|
$msg->params( $param );
|
|
|
|
}
|
|
|
|
}
|
2013-02-08 14:56:44 +00:00
|
|
|
}
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for messagePlain
|
|
|
|
* @internal
|
|
|
|
* @param array $data
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function messagePlain( $data ) {
|
2022-11-18 19:37:45 +00:00
|
|
|
$msg = $this->makeMessage( $data, true );
|
|
|
|
return [ $msg->plain() ];
|
2013-02-08 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for messageCheck
|
|
|
|
* @internal
|
|
|
|
* @param string $what
|
|
|
|
* @param array $data
|
|
|
|
* @return bool[]
|
|
|
|
*/
|
|
|
|
public function messageCheck( $what, $data ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
if ( !in_array( $what, [ 'exists', 'isBlank', 'isDisabled' ] ) ) {
|
2022-08-03 12:31:15 +00:00
|
|
|
throw new LuaError( "invalid what for 'messageCheck'" );
|
2013-02-08 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 19:37:45 +00:00
|
|
|
$msg = $this->makeMessage( $data, false );
|
|
|
|
return [ call_user_func( [ $msg, $what ] ) ];
|
2013-02-08 14:56:44 +00:00
|
|
|
}
|
|
|
|
}
|