2013-02-08 14:56:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Scribunto_LuaMessageLibrary extends Scribunto_LuaLibraryBase {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-03 20:08:54 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() . ")" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|