Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
DisplayTitleLuaLibrary | |
0.00% |
0 / 30 |
|
0.00% |
0 / 5 |
182 | |
0.00% |
0 / 1 |
register | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
getDisplayTitle | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
setDisplayTitle | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
convertToLuaValue | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
42 | |||
toLua | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\DisplayTitle; |
4 | |
5 | use CoreParserFunctions; |
6 | use MediaWiki\MediaWikiServices; |
7 | use Scribunto_LuaLibraryBase; |
8 | use Title; |
9 | |
10 | /** |
11 | * Class DisplayTitleLuaLibrary |
12 | * |
13 | * Implements lua integration for Extension:DisplayTitle |
14 | * |
15 | * @since 1.2 |
16 | * @author Tobias Oetterer < oetterer@uni-paderborn.de > |
17 | */ |
18 | class DisplayTitleLuaLibrary extends Scribunto_LuaLibraryBase { |
19 | /** |
20 | * Called to register the library. |
21 | * |
22 | * This should do any necessary setup and then call $this->getEngine()->registerInterface(). |
23 | * The value returned by that call should be returned from this function, |
24 | * and must be for 'deferLoad' libraries to work right. |
25 | * |
26 | * @return array Lua package |
27 | */ |
28 | public function register(): array { |
29 | $lib = [ |
30 | 'get' => [ $this, 'getDisplayTitle' ], |
31 | 'set' => [ $this, 'setDisplayTitle' ], |
32 | ]; |
33 | |
34 | return $this->getEngine()->registerInterface( __DIR__ . '/' . 'displaytitle.lua', $lib, [] ); |
35 | } |
36 | |
37 | /** |
38 | * Returns the display title for a given page. |
39 | * |
40 | * Mirrors the functionality of parser function #getdisplaytitle. |
41 | * @uses DisplayTitleHooks::getDisplayTitle, DisplayTitleLuaLibrary::toLua |
42 | * @param string $pageName the name of the page, the display title should be received for |
43 | * @return string[] |
44 | */ |
45 | public function getDisplayTitle( string $pageName ): array { |
46 | if ( strlen( $pageName ) ) { |
47 | $title = Title::newFromText( $pageName ); |
48 | if ( $title !== null ) { |
49 | $displayTitleService = MediaWikiServices::getInstance()->get( 'DisplayTitleService' ); |
50 | $displayTitleService->getDisplayTitle( $title, $pageName ); |
51 | } |
52 | return $this->toLua( $pageName ); |
53 | } |
54 | return [ '' ]; |
55 | } |
56 | |
57 | /** |
58 | * Sets the display title for the current page. |
59 | * |
60 | * Mirrors the functionality of the magic word DISPLAYTITLE. |
61 | * @uses CoreParserFunctions::displaytitle, DisplayTitleLuaLibrary::toLua |
62 | * @param string $newDisplayTitle the new display title for the current page |
63 | * @return string[] |
64 | */ |
65 | public function setDisplayTitle( string $newDisplayTitle ): array { |
66 | if ( strlen( $newDisplayTitle ) ) { |
67 | return $this->toLua( CoreParserFunctions::displaytitle( |
68 | $this->getParser(), |
69 | $newDisplayTitle |
70 | ) ); |
71 | } else { |
72 | return [ '' ]; |
73 | } |
74 | } |
75 | |
76 | /** |
77 | * This takes any value and makes sure, that it can be used inside lua. |
78 | * I.e. converts php arrays to lua tables, dumps objects and functions, etc. |
79 | * E.g. A resulting table has its numerical indices start with 1 |
80 | * @uses Scribunto_LuaLibraryBase::getLuaType |
81 | * @param mixed $valueToConvert |
82 | * @return mixed |
83 | */ |
84 | private function convertToLuaValue( $valueToConvert ) { |
85 | $type = $this->getLuaType( $valueToConvert ); |
86 | if ( $type == 'nil' |
87 | || $type == 'function' |
88 | || preg_match( '/^PHP .*/', $valueToConvert ) |
89 | ) { |
90 | return null; |
91 | } |
92 | if ( is_array( $valueToConvert ) ) { |
93 | foreach ( $valueToConvert as $key => $value ) { |
94 | $valueToConvert[$key] = $this->convertToLuaValue( $value ); |
95 | } |
96 | array_unshift( $valueToConvert, '' ); |
97 | unset( $valueToConvert[0] ); |
98 | } |
99 | return $valueToConvert; |
100 | } |
101 | |
102 | /** |
103 | * This makes sure that you can return any given value directly to lua. |
104 | * Does all your type checking and conversion for you. Also wraps in 'array()'. |
105 | * @param mixed $val |
106 | * @return array |
107 | */ |
108 | private function toLua( $val ): array { |
109 | return [ $this->convertToLuaValue( $val ) ]; |
110 | } |
111 | } |