MediaWiki  1.34.0
MessageLibrary.php
Go to the documentation of this file.
1 <?php
2 
4  public function register() {
5  $lib = [
6  'plain' => [ $this, 'messagePlain' ],
7  'check' => [ $this, 'messageCheck' ],
8  ];
9 
10  // Get the correct default language from the parser
11  if ( $this->getParser() ) {
12  $lang = $this->getParser()->getTargetLanguage();
13  } else {
14  global $wgContLang;
16  }
17 
18  return $this->getEngine()->registerInterface( 'mw.message.lua', $lib, [
19  'lang' => $lang->getCode(),
20  ] );
21  }
22 
23  private function makeMessage( $data, $setParams ) {
24  if ( isset( $data['rawMessage'] ) ) {
25  $msg = new RawMessage( $data['rawMessage'] );
26  } else {
27  $msg = Message::newFallbackSequence( $data['keys'] );
28  }
29  $msg->inLanguage( $data['lang'] )
30  ->useDatabase( $data['useDB'] );
31  if ( $setParams ) {
32  $msg->params( array_values( $data['params'] ) );
33  }
34  return $msg;
35  }
36 
43  public function messagePlain( $data ) {
44  try {
45  $msg = $this->makeMessage( $data, true );
46  return [ $msg->plain() ];
47  } catch ( MWException $ex ) {
48  throw new Scribunto_LuaError( "msg:plain() failed (" . $ex->getMessage() . ")" );
49  }
50  }
51 
59  public function messageCheck( $what, $data ) {
60  if ( !in_array( $what, [ 'exists', 'isBlank', 'isDisabled' ] ) ) {
61  throw new Scribunto_LuaError( "invalid what for 'messageCheck'" );
62  }
63 
64  try {
65  $msg = $this->makeMessage( $data, false );
66  return [ call_user_func( [ $msg, $what ] ) ];
67  } catch ( MWException $ex ) {
68  throw new Scribunto_LuaError( "msg:$what() failed (" . $ex->getMessage() . ")" );
69  }
70  }
71 }
Scribunto_LuaMessageLibrary\messagePlain
messagePlain( $data)
Handler for messagePlain.
Definition: MessageLibrary.php:43
Scribunto_LuaError
Definition: LuaCommon.php:992
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
Scribunto_LuaMessageLibrary\messageCheck
messageCheck( $what, $data)
Handler for messageCheck.
Definition: MessageLibrary.php:59
Scribunto_LuaMessageLibrary\makeMessage
makeMessage( $data, $setParams)
Definition: MessageLibrary.php:23
Scribunto_LuaLibraryBase\getEngine
getEngine()
Get the engine.
Definition: LibraryBase.php:56
Scribunto_LuaLibraryBase\getParser
getParser()
Get the parser.
Definition: LibraryBase.php:74
MWException
MediaWiki exception.
Definition: MWException.php:26
Scribunto_LuaLibraryBase
This class provides some basic services that Lua libraries will probably need.
Definition: LibraryBase.php:27
RawMessage
Variant of the Message class.
Definition: RawMessage.php:34
$wgContLang
$wgContLang
Definition: Setup.php:801
Scribunto_LuaMessageLibrary
Definition: MessageLibrary.php:3