MediaWiki REL1_34
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}
$wgContLang
Definition Setup.php:800
MediaWiki exception.
static newFallbackSequence(... $keys)
Factory function accepting multiple message keys and returning a message instance for the first messa...
Definition Message.php:460
Variant of the Message class.
This class provides some basic services that Lua libraries will probably need.
getEngine()
Get the engine.
getParser()
Get the parser.
messagePlain( $data)
Handler for messagePlain.
messageCheck( $what, $data)
Handler for messageCheck.
makeMessage( $data, $setParams)
if(!isset( $args[0])) $lang