2013-02-08 14:56:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->getEngine()->registerInterface( 'mw.message.lua', $lib, array(
|
|
|
|
'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 ) {
|
|
|
|
if ( isset( $data['title'] ) ) {
|
|
|
|
$title = Title::newFromText( $data['title'] );
|
|
|
|
} else {
|
|
|
|
$title = $this->getTitle();
|
|
|
|
}
|
|
|
|
|
|
|
|
$msg->params( array_values( $data['params'] ) )
|
|
|
|
->title( $title );
|
|
|
|
}
|
|
|
|
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() );
|
2013-02-08 14:56:44 +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 ) ) );
|
|
|
|
} catch( MWException $ex ) {
|
|
|
|
throw new Scribunto_LuaError( "msg:$what() failed (" . $ex->getMessage() . ")" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|