2013-02-08 14:56:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Scribunto_LuaMessageLibrary extends Scribunto_LuaLibraryBase {
|
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 {
|
|
|
|
global $wgContLang;
|
|
|
|
$lang = $wgContLang;
|
|
|
|
}
|
|
|
|
|
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'] );
|
|
|
|
}
|
|
|
|
$msg->inLanguage( $data['lang'] )
|
|
|
|
->useDatabase( $data['useDB'] );
|
|
|
|
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 ) {
|
2013-02-08 14:56:44 +00:00
|
|
|
try {
|
|
|
|
$msg = $this->makeMessage( $data, true );
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $msg->plain() ];
|
2015-06-26 16:37:34 +00:00
|
|
|
} catch ( MWException $ex ) {
|
2014-02-03 20:08:54 +00:00
|
|
|
throw new Scribunto_LuaError( "msg:plain() failed (" . $ex->getMessage() . ")" );
|
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' ] ) ) {
|
2013-02-08 14:56:44 +00:00
|
|
|
throw new Scribunto_LuaError( "invalid what for 'messageCheck'" );
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$msg = $this->makeMessage( $data, false );
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ call_user_func( [ $msg, $what ] ) ];
|
2015-06-26 16:37:34 +00:00
|
|
|
} catch ( MWException $ex ) {
|
2013-02-08 14:56:44 +00:00
|
|
|
throw new Scribunto_LuaError( "msg:$what() failed (" . $ex->getMessage() . ")" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|