|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SMW\Scribunto; |
|
4
|
|
|
|
|
5
|
|
|
use Scribunto_LuaLibraryBase; |
|
6
|
|
|
use SMW\DIProperty; |
|
7
|
|
|
use FauxRequest; |
|
8
|
|
|
use ApiMain; |
|
9
|
|
|
|
|
10
|
|
|
use SMWQueryProcessor as QueryProcessor; |
|
11
|
|
|
use SMW\ApplicationFactory; |
|
12
|
|
|
use SMW\ParameterProcessorFactory; |
|
13
|
|
|
use SMW\ParserFunctionFactory; |
|
14
|
|
|
use SMWOutputs; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @license GNU GPL v2+ |
|
18
|
|
|
* @since 1.0 |
|
19
|
|
|
* |
|
20
|
|
|
* @author mwjames |
|
21
|
|
|
*/ |
|
22
|
|
|
class ScribuntoLuaLibrary extends Scribunto_LuaLibraryBase { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* This is the name of the key for error messages |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
* @since 1.0 |
|
29
|
|
|
*/ |
|
30
|
|
|
const SMW_ERROR_FIELD='error'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @since 1.0 |
|
34
|
|
|
*/ |
|
35
|
13 |
|
public function register() { |
|
36
|
|
|
|
|
37
|
|
|
$lib = array( |
|
38
|
13 |
|
'getPropertyType' => array( $this, 'getPropertyType' ), |
|
39
|
13 |
|
'getQueryResult' => array( $this, 'getQueryResult' ), |
|
40
|
13 |
|
'info' => array( $this, 'info' ), |
|
41
|
13 |
|
'set' => array( $this, 'set' ), |
|
42
|
13 |
|
); |
|
43
|
|
|
|
|
44
|
13 |
|
$this->getEngine()->registerInterface( __DIR__ . '/' . 'mw.smw.lua', $lib, array() ); |
|
45
|
13 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Returns query results in for of the standard API return format |
|
49
|
|
|
* |
|
50
|
|
|
* @since 1.0 |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $argString |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function getQueryResult( $argString = null ) { |
|
57
|
|
|
|
|
58
|
2 |
|
$rawParameters = preg_split( "/(?<=[^\|])\|(?=[^\|])/", $argString ); |
|
59
|
|
|
|
|
60
|
2 |
|
list( $queryString, $parameters, $printouts ) = QueryProcessor::getComponentsFromFunctionParams( |
|
61
|
2 |
|
$rawParameters, |
|
62
|
|
|
false |
|
63
|
2 |
|
); |
|
64
|
|
|
|
|
65
|
2 |
|
QueryProcessor::addThisPrintout( $printouts, $parameters ); |
|
66
|
|
|
|
|
67
|
2 |
|
$query = QueryProcessor::createQuery( |
|
68
|
2 |
|
$queryString, |
|
69
|
2 |
|
QueryProcessor::getProcessedParams( $parameters, $printouts ), |
|
70
|
2 |
|
QueryProcessor::SPECIAL_PAGE, |
|
71
|
2 |
|
'', |
|
72
|
|
|
$printouts |
|
73
|
2 |
|
); |
|
74
|
|
|
|
|
75
|
2 |
|
$queryResult = ApplicationFactory::getInstance()->getStore()->getQueryResult( $query )->toArray(); |
|
76
|
2 |
|
if(!empty($queryResult["results"])) { |
|
77
|
|
|
$queryResult["results"] = array_combine(range(1, count($queryResult["results"])), array_values($queryResult["results"])); |
|
78
|
|
|
} |
|
79
|
2 |
|
return array( $queryResult ); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns property type |
|
84
|
|
|
* |
|
85
|
|
|
* @since 1.0 |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $propertyName |
|
88
|
|
|
* |
|
89
|
|
|
* @return array |
|
90
|
|
|
*/ |
|
91
|
3 |
|
public function getPropertyType( $propertyName = null ) { |
|
92
|
|
|
|
|
93
|
3 |
|
$this->checkType( 'getPropertyType', 1, $propertyName, 'string' ); |
|
94
|
3 |
|
$propertyName = trim( $propertyName ); |
|
95
|
|
|
|
|
96
|
3 |
|
if ( $propertyName === '' ) { |
|
97
|
1 |
|
return array( null ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
$property = DIProperty::newFromUserLabel( $propertyName ); |
|
101
|
|
|
|
|
102
|
2 |
|
if ( $property === null ) { |
|
103
|
|
|
return array( null ); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
return array( $property->findPropertyTypeID() ); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* This mirrors the functionality of the parser function #info and makes it available to lua. |
|
111
|
|
|
* |
|
112
|
|
|
* @since 1.0 |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $text text to show inside the info popup |
|
115
|
|
|
* @param string $icon identifier for the icon to use |
|
116
|
|
|
* |
|
117
|
|
|
* @uses \SMW\ParserFunctionFactory::__construct |
|
118
|
|
|
* |
|
119
|
|
|
* @return string[] |
|
120
|
|
|
*/ |
|
121
|
4 |
|
public function info( $text, $icon = 'info' ) { |
|
122
|
|
|
|
|
123
|
4 |
|
$parser = $this->getEngine()->getParser(); |
|
124
|
|
|
|
|
125
|
|
|
// do some parameter processing |
|
126
|
4 |
|
if ( ! trim( $text ) || ! is_string( $text ) ) { |
|
127
|
|
|
// no info-text present, or wrong type. abort |
|
128
|
1 |
|
return null; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// check if icon is set and valid |
|
132
|
3 |
|
if ( !is_string( $icon ) || !in_array( $icon, [ 'note', 'warning' ] ) ) { |
|
133
|
3 |
|
$icon = 'info'; |
|
134
|
3 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
// the actual info message is easy to create: |
|
137
|
4 |
|
$result = smwfEncodeMessages( |
|
138
|
3 |
|
array( $text ), |
|
139
|
3 |
|
$icon, |
|
140
|
3 |
|
' <!--br-->', |
|
141
|
|
|
false // No escaping. |
|
142
|
3 |
|
); |
|
143
|
|
|
|
|
144
|
|
|
// to have all necessary data committed to output, use SMWOutputs::commitToParser() |
|
145
|
3 |
|
SMWOutputs::commitToParser( $parser ); |
|
146
|
|
|
|
|
147
|
3 |
|
return array( $this->extractResultString( $result ) ); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* This mirrors the functionality of the parser function #set and makes it available in lua. |
|
152
|
|
|
* |
|
153
|
|
|
* @since 1.0 |
|
154
|
|
|
* |
|
155
|
|
|
* @param string|array $parameters parameters passed from lua, string or array depending on call |
|
156
|
|
|
* |
|
157
|
|
|
* @uses \SMW\ParserFunctionFactory::__construct, ParameterProcessorFactory::newFromArray |
|
158
|
|
|
* |
|
159
|
|
|
* @return null|array|array[] |
|
160
|
|
|
*/ |
|
161
|
4 |
|
public function set( $parameters ) { |
|
162
|
|
|
|
|
163
|
4 |
|
$parser = $this->getEngine()->getParser(); |
|
164
|
|
|
|
|
165
|
|
|
// if we have no arguments, do nothing |
|
166
|
4 |
|
if ( !sizeof( $arguments = $this->processLuaArguments( $parameters ) ) ) { |
|
167
|
1 |
|
return null; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
// prepare setParserFunction object |
|
171
|
3 |
|
$parserFunctionFactory = new ParserFunctionFactory( $parser ); |
|
172
|
3 |
|
$setParserFunction = $parserFunctionFactory->newSetParserFunction( $parser ); |
|
173
|
|
|
|
|
174
|
|
|
// call parse on setParserFunction |
|
175
|
3 |
|
$parserFunctionCallResult = $setParserFunction->parse( |
|
176
|
3 |
|
ParameterProcessorFactory::newFromArray( $arguments ) |
|
177
|
3 |
|
); |
|
178
|
|
|
|
|
179
|
|
|
// get usable result |
|
180
|
3 |
|
$result = $this->extractResultString( $parserFunctionCallResult ); |
|
181
|
|
|
|
|
182
|
3 |
|
if ( strlen( $result ) ) { |
|
183
|
|
|
// if result a non empty string, assume an error message |
|
184
|
1 |
|
return array( [ 1 => false, self::SMW_ERROR_FIELD => preg_replace( '/<[^>]+>/', '', $result ) ] ); |
|
185
|
|
|
} else { |
|
186
|
|
|
// on success, return true |
|
187
|
2 |
|
return array( 1 => true ); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Takes a result returned from a parser function call and prepares it to be used as parsed string. |
|
193
|
|
|
* |
|
194
|
|
|
* @since 1.0 |
|
195
|
|
|
* |
|
196
|
|
|
* @param string|array $parserFunctionResult |
|
197
|
|
|
* |
|
198
|
|
|
* @return string |
|
199
|
|
|
*/ |
|
200
|
6 |
|
private function extractResultString( $parserFunctionResult ) { |
|
201
|
|
|
|
|
202
|
|
|
// parser function call can return string or array |
|
203
|
6 |
|
if ( is_array( $parserFunctionResult ) ) { |
|
204
|
3 |
|
$result = $parserFunctionResult[0]; |
|
205
|
3 |
|
$noParse = isset($parserFunctionResult['noparse']) ? $parserFunctionResult['noparse'] : true; |
|
206
|
3 |
|
} else { |
|
207
|
3 |
|
$result = $parserFunctionResult; |
|
208
|
3 |
|
$noParse = true; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
6 |
|
if ( ! $noParse ) { |
|
212
|
|
|
$result = $this->getEngine()->getParser()->recursiveTagParseFully( $result ); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
6 |
|
return trim( $result ); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Takes the $arguments passed from lua and pre-processes them: make sure, we have a sequence array (not associative) |
|
220
|
|
|
* |
|
221
|
|
|
* @since 1.0 |
|
222
|
|
|
* |
|
223
|
|
|
* @param string|array $arguments |
|
224
|
|
|
* |
|
225
|
|
|
* @return array |
|
226
|
|
|
*/ |
|
227
|
4 |
|
private function processLuaArguments( $arguments ) { |
|
228
|
|
|
|
|
229
|
|
|
// make sure, we have an array of parameters |
|
230
|
4 |
|
if ( !is_array( $arguments ) ) { |
|
231
|
4 |
|
$arguments = array( $arguments ); |
|
232
|
4 |
|
} |
|
233
|
|
|
|
|
234
|
|
|
// if $arguments were supplied as key => value pair (aka associative array), we rectify this here |
|
235
|
4 |
|
$processedArguments = array(); |
|
236
|
4 |
|
foreach ( $arguments as $key => $value ) { |
|
237
|
4 |
|
if ( !is_int( $key ) && !preg_match( '/[0-9]+/', $key ) ) { |
|
238
|
1 |
|
$value = (string) $key . '=' . (string) $value; |
|
239
|
|
|
} |
|
240
|
4 |
|
if ( $value ) { |
|
241
|
|
|
// only add, when value is set. could be empty, if lua function was called with no parameter or empty string |
|
242
|
3 |
|
$processedArguments[] = $value; |
|
243
|
3 |
|
} |
|
244
|
4 |
|
} |
|
245
|
|
|
|
|
246
|
4 |
|
return $processedArguments; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
} |
|
250
|
|
|
|