|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Contains the class handling the scribunto lua support. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright (C) 2018, Tobias Oetterer, Paderborn University |
|
6
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.html GNU General Public License, version 3 (or later) |
|
7
|
|
|
* |
|
8
|
|
|
* This file is part of the MediaWiki extension BootstrapComponents. |
|
9
|
|
|
* The BootstrapComponents extension is free software: you can redistribute it |
|
10
|
|
|
* and/or modify it under the terms of the GNU General Public License as published |
|
11
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
12
|
|
|
* (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* The BootstrapComponents extension is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
* @file |
|
23
|
|
|
* @ingroup BootstrapComponents |
|
24
|
|
|
* @author Tobias Oetterer |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace BootstrapComponents; |
|
28
|
|
|
|
|
29
|
|
|
use \ReflectionClass; |
|
30
|
|
|
use \Scribunto_LuaEngine; |
|
31
|
|
|
use \Scribunto_LuaLibraryBase; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Class LuaLibrary |
|
35
|
|
|
* |
|
36
|
|
|
* Class to handle the scribunto lua support. |
|
37
|
|
|
* |
|
38
|
|
|
* @since 1.1 |
|
39
|
|
|
*/ |
|
40
|
|
|
class LuaLibrary extends Scribunto_LuaLibraryBase { |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var ApplicationFactory $applicationFactory; |
|
44
|
|
|
*/ |
|
45
|
|
|
private $applicationFactory; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* LuaLibrary constructor. |
|
49
|
|
|
* |
|
50
|
|
|
* @param Scribunto_LuaEngine $engine |
|
51
|
|
|
*/ |
|
52
|
19 |
|
public function __construct( $engine ) { |
|
53
|
19 |
|
parent::__construct( $engine ); |
|
54
|
19 |
|
$this->applicationFactory = ApplicationFactory::getInstance(); |
|
55
|
19 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
11 |
|
public function register() { |
|
61
|
|
|
|
|
62
|
|
|
$lib = [ |
|
63
|
11 |
|
'parse' => [ $this, 'parse' ], |
|
64
|
11 |
|
'getSkin' => [ $this, 'getSkin' ], |
|
65
|
11 |
|
]; |
|
66
|
|
|
|
|
67
|
11 |
|
return $this->getEngine()->registerInterface( __DIR__ . '/../lua/mw.bootstrap.lua', $lib, [] ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $componentName |
|
72
|
|
|
* @param string $input |
|
73
|
|
|
* @param array $arguments |
|
74
|
|
|
* |
|
75
|
|
|
* @throws \ReflectionException |
|
76
|
|
|
* @throws \MWException |
|
77
|
|
|
* |
|
78
|
|
|
* @return string[] |
|
79
|
|
|
*/ |
|
80
|
8 |
|
public function parse( $componentName, $input, $arguments ) { |
|
81
|
|
|
|
|
82
|
8 |
|
if ( empty( $componentName ) ) { |
|
83
|
5 |
|
return [ wfMessage( 'bootstrap-components-lua-error-no-component' )->text() ]; |
|
84
|
|
|
} |
|
85
|
5 |
|
$componentLibrary = $this->getApplicationFactory()->getComponentLibrary(); |
|
86
|
5 |
|
if ( !in_array( $componentName, $componentLibrary->getRegisteredComponents() ) ) { |
|
87
|
3 |
|
return [ wfMessage( 'bootstrap-components-lua-error-invalid-component', $componentName )->text() ]; |
|
88
|
|
|
} |
|
89
|
4 |
|
$componentClass = $componentLibrary->getClassFor( $componentName ); |
|
90
|
4 |
|
$parserRequest = $this->buildParserRequest( $input, $arguments, $componentName ); |
|
91
|
4 |
|
$component = $this->getComponent( $componentClass ); |
|
92
|
|
|
return [ |
|
93
|
4 |
|
$component->parseComponent( $parserRequest ) |
|
94
|
4 |
|
]; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @throws \MWException |
|
99
|
|
|
* |
|
100
|
|
|
* @return string[] |
|
101
|
|
|
*/ |
|
102
|
4 |
|
public function getSkin() { |
|
103
|
4 |
|
return [ $this->getApplicationFactory()->getParserOutputHelper( $this->getParser() )->getNameOfActiveSkin() ]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param string $input |
|
108
|
|
|
* @param array $arguments |
|
109
|
|
|
* @param null|string $component |
|
110
|
|
|
* |
|
111
|
|
|
* @throws \MWException |
|
112
|
|
|
* |
|
113
|
|
|
* @return \BootstrapComponents\ParserRequest |
|
114
|
|
|
*/ |
|
115
|
4 |
|
protected function buildParserRequest( $input, $arguments, $component = null ) { |
|
116
|
|
|
|
|
117
|
|
|
// prepare the arguments array |
|
118
|
4 |
|
$parserRequestArguments = $this->processLuaArguments( $arguments ); |
|
119
|
4 |
|
array_unshift( $parserRequestArguments, $input ); |
|
120
|
4 |
|
array_unshift( $parserRequestArguments, $this->getParser() ); |
|
121
|
|
|
|
|
122
|
4 |
|
return ApplicationFactory::getInstance()->getNewParserRequest( $parserRequestArguments, true, $component ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $componentClass |
|
127
|
|
|
* |
|
128
|
|
|
* @throws \MWException |
|
129
|
|
|
* @throws \ReflectionException |
|
130
|
|
|
* |
|
131
|
|
|
* @return AbstractComponent |
|
132
|
|
|
*/ |
|
133
|
4 |
|
protected function getComponent( $componentClass ) { |
|
134
|
|
|
|
|
135
|
4 |
|
$objectReflection = new ReflectionClass( $componentClass ); |
|
136
|
|
|
/** @var AbstractComponent $component */ |
|
137
|
4 |
|
$component = $objectReflection->newInstanceArgs( |
|
138
|
|
|
[ |
|
139
|
4 |
|
$this->getApplicationFactory()->getComponentLibrary(), |
|
140
|
4 |
|
$this->getApplicationFactory()->getParserOutputHelper( $this->getParser() ), |
|
141
|
4 |
|
$this->getApplicationFactory()->getNestingController(), |
|
142
|
|
|
] |
|
143
|
4 |
|
); |
|
144
|
4 |
|
return $component; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return ApplicationFactory |
|
149
|
|
|
*/ |
|
150
|
8 |
|
protected function getApplicationFactory() { |
|
151
|
8 |
|
return $this->applicationFactory; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Takes the $arguments passed from lua and pre-processes them: make sure, |
|
156
|
|
|
* we have a sequence array (not associative) |
|
157
|
|
|
* |
|
158
|
|
|
* @param string|array $arguments |
|
159
|
|
|
* |
|
160
|
|
|
* @return array |
|
161
|
|
|
*/ |
|
162
|
4 |
|
private function processLuaArguments( $arguments ) { |
|
163
|
|
|
|
|
164
|
|
|
// make sure, we have an array of parameters |
|
165
|
4 |
|
if ( !is_array( $arguments ) ) { |
|
166
|
|
|
$arguments = preg_split( "/(?<=[^\|])\|(?=[^\|])/", $arguments ); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
// if $arguments were supplied as key => value pair (aka associative array), |
|
170
|
|
|
// we rectify this here |
|
171
|
4 |
|
$processedArguments = []; |
|
172
|
4 |
|
foreach ( $arguments as $key => $value ) { |
|
173
|
2 |
|
if ( !is_int( $key ) && !preg_match( '/[0-9]+/', $key ) ) { |
|
174
|
2 |
|
$value = (string) $key . '=' . (string) $value; |
|
175
|
2 |
|
} |
|
176
|
2 |
|
$processedArguments[] = $value; |
|
177
|
4 |
|
} |
|
178
|
|
|
|
|
179
|
4 |
|
return $processedArguments; |
|
180
|
|
|
} |
|
181
|
|
|
} |