2013-02-08 14:56:44 +00:00
|
|
|
<?php
|
|
|
|
|
2016-05-17 14:52:05 +00:00
|
|
|
// @codingStandardsIgnoreLine Squiz.Classes.ValidClassName.NotCamelCaps
|
2013-02-08 14:56:44 +00:00
|
|
|
class Scribunto_LuaMessageLibrary extends Scribunto_LuaLibraryBase {
|
|
|
|
function register() {
|
|
|
|
$lib = array(
|
2014-02-03 20:08:54 +00:00
|
|
|
'plain' => array( $this, 'messagePlain' ),
|
2013-02-08 14:56:44 +00:00
|
|
|
'check' => array( $this, 'messageCheck' ),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get the correct default language from the parser
|
|
|
|
if ( $this->getParser() ) {
|
|
|
|
$lang = $this->getParser()->getTargetLanguage();
|
|
|
|
} else {
|
|
|
|
global $wgContLang;
|
|
|
|
$lang = $wgContLang;
|
|
|
|
}
|
|
|
|
|
2014-09-26 18:11:21 +00:00
|
|
|
return $this->getEngine()->registerInterface( 'mw.message.lua', $lib, array(
|
2013-02-08 14:56:44 +00:00
|
|
|
'lang' => $lang->getCode(),
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|
2014-02-03 20:08:54 +00:00
|
|
|
return array( $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 ) {
|
|
|
|
if ( !in_array( $what, array( 'exists', 'isBlank', 'isDisabled' ) ) ) {
|
|
|
|
throw new Scribunto_LuaError( "invalid what for 'messageCheck'" );
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$msg = $this->makeMessage( $data, false );
|
|
|
|
return array( call_user_func( array( $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() . ")" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|