|
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
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @license GNU GPL v2+ |
|
17
|
|
|
* @since 1.0 |
|
18
|
|
|
* |
|
19
|
|
|
* @author mwjames |
|
20
|
|
|
*/ |
|
21
|
|
|
class ScribuntoLuaLibrary extends Scribunto_LuaLibraryBase { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* This is the name of the key for error messages |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
* @since 1.0 |
|
28
|
|
|
*/ |
|
29
|
|
|
const SMW_ERROR_FIELD='error'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @since 1.0 |
|
33
|
|
|
*/ |
|
34
|
9 |
|
public function register() { |
|
35
|
|
|
|
|
36
|
|
|
$lib = array( |
|
37
|
9 |
|
'getQueryResult' => array( $this, 'getQueryResult' ), |
|
38
|
9 |
|
'getPropertyType' => array( $this, 'getPropertyType' ), |
|
39
|
9 |
|
'set' => array( $this, 'set' ), |
|
40
|
9 |
|
); |
|
41
|
|
|
|
|
42
|
9 |
|
$this->getEngine()->registerInterface( __DIR__ . '/' . 'mw.smw.lua', $lib, array() ); |
|
43
|
9 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Returns query results in for of the standard API return format |
|
47
|
|
|
* |
|
48
|
|
|
* @since 1.0 |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $argString |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function getQueryResult( $argString = null ) { |
|
55
|
|
|
|
|
56
|
2 |
|
$rawParameters = preg_split( "/(?<=[^\|])\|(?=[^\|])/", $argString ); |
|
57
|
|
|
|
|
58
|
2 |
|
list( $queryString, $parameters, $printouts ) = QueryProcessor::getComponentsFromFunctionParams( |
|
59
|
2 |
|
$rawParameters, |
|
60
|
|
|
false |
|
61
|
2 |
|
); |
|
62
|
|
|
|
|
63
|
2 |
|
QueryProcessor::addThisPrintout( $printouts, $parameters ); |
|
64
|
|
|
|
|
65
|
2 |
|
$query = QueryProcessor::createQuery( |
|
66
|
2 |
|
$queryString, |
|
67
|
2 |
|
QueryProcessor::getProcessedParams( $parameters, $printouts ), |
|
68
|
2 |
|
QueryProcessor::SPECIAL_PAGE, |
|
69
|
2 |
|
'', |
|
70
|
|
|
$printouts |
|
71
|
2 |
|
); |
|
72
|
|
|
|
|
73
|
2 |
|
$queryResult = ApplicationFactory::getInstance()->getStore()->getQueryResult( $query )->toArray(); |
|
74
|
2 |
|
if(!empty($queryResult["results"])) { |
|
75
|
|
|
$queryResult["results"] = array_combine(range(1, count($queryResult["results"])), array_values($queryResult["results"])); |
|
76
|
|
|
} |
|
77
|
2 |
|
return array( $queryResult ); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns property type |
|
82
|
|
|
* |
|
83
|
|
|
* @since 1.0 |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $propertyName |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
3 |
|
public function getPropertyType( $propertyName = null ) { |
|
90
|
|
|
|
|
91
|
3 |
|
$this->checkType( 'getPropertyType', 1, $propertyName, 'string' ); |
|
92
|
3 |
|
$propertyName = trim( $propertyName ); |
|
93
|
|
|
|
|
94
|
3 |
|
if ( $propertyName === '' ) { |
|
95
|
1 |
|
return array( null ); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
$property = DIProperty::newFromUserLabel( $propertyName ); |
|
99
|
|
|
|
|
100
|
2 |
|
if ( $property === null ) { |
|
101
|
|
|
return array( null ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
2 |
|
return array( $property->findPropertyTypeID() ); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* This mirrors the functionality of the parser function #set and makes it available in lua. |
|
109
|
|
|
* |
|
110
|
|
|
* @param string|array $parameters parameters passed from lua, string or array depending on call |
|
111
|
|
|
* |
|
112
|
|
|
* @uses \SMW\ParserFunctionFactory::__construct, ParameterProcessorFactory::newFromArray |
|
113
|
|
|
* |
|
114
|
|
|
* @return null|array|array[] |
|
115
|
|
|
*/ |
|
116
|
4 |
|
public function set( $parameters ) |
|
117
|
|
|
{ |
|
118
|
4 |
|
$parser = $this->getEngine()->getParser(); |
|
119
|
|
|
|
|
120
|
|
|
# make sure, we have an array of parameters |
|
121
|
4 |
|
if ( !is_array($parameters) ) { |
|
122
|
4 |
|
$parameters = array($parameters); |
|
123
|
4 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
# if $parameters were supplied as key => value pair (aka associative array), we have to rectify this here |
|
126
|
4 |
|
$argumentsToParserFunction = array(); |
|
127
|
4 |
|
foreach ( $parameters as $key => $value ) { |
|
128
|
4 |
|
if ( !is_int($key) && !preg_match('/[0-9]+/', $key) ) { |
|
129
|
|
|
$value = $key . '=' . $value; |
|
130
|
|
|
} |
|
131
|
4 |
|
if ( $value ) { |
|
132
|
|
|
# only add, when value is set. could be empty, if set was called with no parameter or empty string |
|
133
|
3 |
|
$argumentsToParserFunction[] = $value; |
|
134
|
3 |
|
} |
|
135
|
4 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
# if we have no arguments, do nothing |
|
138
|
4 |
|
if ( !sizeof($argumentsToParserFunction) ) { |
|
139
|
1 |
|
return null; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
# prepare setParserFunction object |
|
143
|
3 |
|
$parserFunctionFactory = new ParserFunctionFactory( $parser ); |
|
144
|
3 |
|
$setParserFunction = $parserFunctionFactory->newSetParserFunction( $parser ); |
|
145
|
|
|
|
|
146
|
|
|
# call parse on setParserFunction |
|
147
|
3 |
|
$parserFunctionCallResult = $setParserFunction->parse( |
|
148
|
3 |
|
ParameterProcessorFactory::newFromArray( $argumentsToParserFunction ) |
|
149
|
3 |
|
); |
|
150
|
|
|
|
|
151
|
|
|
# get result |
|
152
|
3 |
|
if ( is_array($parserFunctionCallResult) ) { |
|
153
|
3 |
|
$result = $parserFunctionCallResult[0]; |
|
154
|
3 |
|
$noParse = isset($parserFunctionCallResult['noparse']) ? $parserFunctionCallResult['noparse'] : true; |
|
155
|
3 |
|
} else { |
|
156
|
|
|
$result = $parserFunctionCallResult; |
|
157
|
|
|
$noParse = true; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
3 |
|
if ( ! $noParse ) { |
|
161
|
|
|
$result = $parser->recursiveTagParseFully( $result ); |
|
162
|
|
|
} |
|
163
|
3 |
|
$result = trim($result); |
|
164
|
|
|
|
|
165
|
3 |
|
if ( strlen ($result) ) { |
|
166
|
|
|
# if result a non empty string, assume an error message |
|
167
|
1 |
|
return array( [ 1 => false, self::SMW_ERROR_FIELD => preg_replace('/<[^>]+>/', '', $result) ] ); |
|
168
|
|
|
} else { |
|
169
|
|
|
# on success, return true |
|
170
|
2 |
|
return array( 1 => true ); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|