2013-02-08 14:56:44 +00:00
|
|
|
<?php
|
|
|
|
|
2022-08-03 12:31:15 +00:00
|
|
|
namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
|
|
|
|
|
2022-11-18 19:37:45 +00:00
|
|
|
use Language;
|
2020-03-14 19:57:09 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-08-03 12:31:15 +00:00
|
|
|
use Message;
|
|
|
|
use RawMessage;
|
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-11-18 19:37:45 +00:00
|
|
|
if ( is_string( $data['lang'] ) && !Language::isValidCode( $data['lang'] ) ) {
|
|
|
|
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 ) {
|
2014-03-18 17:52:51 +00:00
|
|
|
$msg->params( array_values( $data['params'] ) );
|
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
|
|
|
}
|
|
|
|
}
|