Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.62% |
29 / 32 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
MessageLibrary | |
90.62% |
29 / 32 |
|
25.00% |
1 / 4 |
15.19 | |
0.00% |
0 / 1 |
register | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
2.00 | |||
makeMessage | |
93.75% |
15 / 16 |
|
0.00% |
0 / 1 |
10.02 | |||
messagePlain | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
messageCheck | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon; |
4 | |
5 | use MediaWiki\Language\RawMessage; |
6 | use MediaWiki\MediaWikiServices; |
7 | use MediaWiki\Message\Message; |
8 | |
9 | class MessageLibrary extends LibraryBase { |
10 | public function register() { |
11 | $lib = [ |
12 | 'plain' => [ $this, 'messagePlain' ], |
13 | 'check' => [ $this, 'messageCheck' ], |
14 | ]; |
15 | |
16 | // Get the correct default language from the parser |
17 | if ( $this->getParser() ) { |
18 | $lang = $this->getParser()->getTargetLanguage(); |
19 | } else { |
20 | $lang = MediaWikiServices::getInstance()->getContentLanguage(); |
21 | } |
22 | |
23 | return $this->getEngine()->registerInterface( 'mw.message.lua', $lib, [ |
24 | 'lang' => $lang->getCode(), |
25 | ] ); |
26 | } |
27 | |
28 | /** |
29 | * Create a Message |
30 | * @param array $data |
31 | * - 'rawMessage': (string, optional) If set, create a RawMessage using this as `$text` |
32 | * - 'keys': (string|string[]) Message keys. Required unless 'rawMessage' is set. |
33 | * - 'lang': (Language|StubUserLang|string) Language for the Message. |
34 | * - 'useDB': (bool) "Use database" flag. |
35 | * - 'params': (array) Parameters for the Message. May be omitted if $setParams is false. |
36 | * @param bool $setParams Whether to use $data['params'] |
37 | * @return Message |
38 | */ |
39 | private function makeMessage( $data, $setParams ) { |
40 | if ( isset( $data['rawMessage'] ) ) { |
41 | $msg = new RawMessage( $data['rawMessage'] ); |
42 | } else { |
43 | $msg = Message::newFallbackSequence( $data['keys'] ); |
44 | } |
45 | if ( is_string( $data['lang'] ) && |
46 | !MediaWikiServices::getInstance()->getLanguageNameUtils()->isValidCode( $data['lang'] ) |
47 | ) { |
48 | throw new LuaError( "language code '{$data['lang']}' is invalid" ); |
49 | } else { |
50 | $msg->inLanguage( $data['lang'] ); |
51 | } |
52 | $msg->useDatabase( $data['useDB'] ); |
53 | if ( $setParams ) { |
54 | foreach ( $data['params'] as $param ) { |
55 | // Only rawParam and numParam are supposed by the Lua message API |
56 | if ( is_array( $param ) && isset( $param['raw'] ) ) { |
57 | $msg->rawParams( $param ); |
58 | } elseif ( is_array( $param ) && isset( $param['num'] ) ) { |
59 | $msg->numParams( $param ); |
60 | } else { |
61 | $msg->params( $param ); |
62 | } |
63 | } |
64 | } |
65 | return $msg; |
66 | } |
67 | |
68 | /** |
69 | * Handler for messagePlain |
70 | * @internal |
71 | * @param array $data |
72 | * @return string[] |
73 | */ |
74 | public function messagePlain( $data ) { |
75 | $msg = $this->makeMessage( $data, true ); |
76 | return [ $msg->plain() ]; |
77 | } |
78 | |
79 | /** |
80 | * Handler for messageCheck |
81 | * @internal |
82 | * @param string $what |
83 | * @param array $data |
84 | * @return bool[] |
85 | */ |
86 | public function messageCheck( $what, $data ) { |
87 | if ( !in_array( $what, [ 'exists', 'isBlank', 'isDisabled' ] ) ) { |
88 | throw new LuaError( "invalid what for 'messageCheck'" ); |
89 | } |
90 | |
91 | $msg = $this->makeMessage( $data, false ); |
92 | return [ call_user_func( [ $msg, $what ] ) ]; |
93 | } |
94 | } |