|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SMW\Scribunto; |
|
4
|
|
|
|
|
5
|
|
|
use SMWQueryResult as QueryResult; |
|
6
|
|
|
use SMWResultArray as ResultArray; |
|
7
|
|
|
use SMWDataValue as DataValue; |
|
8
|
|
|
use SMW\Query\PrintRequest; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class LuaAskResultProcessor |
|
12
|
|
|
* |
|
13
|
|
|
* @license GNU GPL v2+ |
|
14
|
|
|
* @since 1.0 |
|
15
|
|
|
* |
|
16
|
|
|
* @author Tobias Oetterer |
|
17
|
|
|
* |
|
18
|
|
|
* @package SMW\Scribunto |
|
19
|
|
|
*/ |
|
20
|
|
|
class LuaAskResultProcessor { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Holds all possible representations of "true" in this smw instance |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private $msgTrue; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* This counter serves as fallback, if no label for printout is specified |
|
31
|
|
|
* |
|
32
|
|
|
* @var int |
|
33
|
|
|
*/ |
|
34
|
|
|
private $numericIndex; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Holds the query result for this object |
|
38
|
|
|
* |
|
39
|
|
|
* @var QueryResult |
|
40
|
|
|
*/ |
|
41
|
|
|
private $queryResult; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* LuaAskResultProcessor constructor. |
|
45
|
|
|
* |
|
46
|
|
|
* @param QueryResult $queryResult |
|
47
|
|
|
*/ |
|
48
|
15 |
|
public function __construct( QueryResult $queryResult ) { |
|
49
|
15 |
|
$this->queryResult = $queryResult; |
|
50
|
15 |
|
$this->msgTrue = explode( ',', wfMessage( 'smw_true_words' )->text() . ',true,t,yes,y' ); |
|
51
|
15 |
|
$this->numericIndex = 1; |
|
52
|
15 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Extracts the data in {@see $queryResult} and returns it as a table |
|
56
|
|
|
* usable in lua context |
|
57
|
|
|
* |
|
58
|
|
|
* @uses getDataFromQueryResultRow |
|
59
|
|
|
* |
|
60
|
|
|
* @return array|null |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function getProcessedResult() { |
|
63
|
|
|
|
|
64
|
3 |
|
$result = null; |
|
65
|
|
|
|
|
66
|
3 |
|
if ( $this->queryResult->getCount() ) { |
|
67
|
|
|
|
|
68
|
3 |
|
$result = []; |
|
69
|
|
|
|
|
70
|
3 |
|
while ( $resultRow = $this->queryResult->getNext() ) { |
|
71
|
3 |
|
$result[] = $this->getDataFromQueryResultRow( $resultRow ); |
|
72
|
3 |
|
} |
|
73
|
3 |
|
} |
|
74
|
|
|
|
|
75
|
3 |
|
return $result; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Extracts the data of a single result row in the {@see $queryResult} |
|
80
|
|
|
* and returns it as a table usable in lua context |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $resultRow result row as an array of {@see ResultArray} objects |
|
83
|
|
|
* |
|
84
|
|
|
* @uses getDataFromResultArray |
|
85
|
|
|
* |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
4 |
|
public function getDataFromQueryResultRow( array $resultRow ) { |
|
89
|
|
|
|
|
90
|
4 |
|
$rowData = []; |
|
91
|
|
|
|
|
92
|
|
|
/** @var ResultArray $resultArray */ |
|
93
|
4 |
|
foreach ( $resultRow as $resultArray ) { |
|
94
|
|
|
|
|
95
|
|
|
// get key and data |
|
96
|
4 |
|
list ( $key, $data ) = $this->getDataFromResultArray( $resultArray ); |
|
97
|
|
|
|
|
98
|
4 |
|
$rowData[$key] = $data; |
|
99
|
4 |
|
} |
|
100
|
|
|
return $rowData; |
|
101
|
4 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Extracts the data of a single printRequest / query field |
|
105
|
|
|
* |
|
106
|
|
|
* @param ResultArray $resultArray |
|
107
|
|
|
* |
|
108
|
|
|
* @uses getKeyFromPrintRequest, getValueFromDataValue, extractLuaDataFromDVData |
|
109
|
|
|
* |
|
110
|
|
|
* @return array array ( int|string, array ) |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getDataFromResultArray( ResultArray $resultArray ) { |
|
113
|
5 |
|
|
|
114
|
|
|
// first, extract the key (label), if any |
|
115
|
|
|
$key = $this->getKeyFromPrintRequest( $resultArray->getPrintRequest() ); |
|
116
|
5 |
|
|
|
117
|
|
|
$resultArrayData = []; |
|
118
|
5 |
|
|
|
119
|
|
|
// then get all data that is stored in this printRequest / query field |
|
120
|
|
|
/** @var DataValue $dataValue */ |
|
121
|
|
|
while ( ( $dataValue = $resultArray->getNextDataValue() ) !== false ) { |
|
122
|
5 |
|
|
|
123
|
|
|
$resultArrayData[] = $this->getValueFromDataValue( $dataValue ); |
|
124
|
5 |
|
} |
|
125
|
5 |
|
|
|
126
|
|
|
$resultArrayData = $this->extractLuaDataFromDVData( $resultArrayData ); |
|
127
|
5 |
|
|
|
128
|
|
|
return [ $key, $resultArrayData ]; |
|
129
|
5 |
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Takes an smw query print request and tries to retrieve the label |
|
133
|
|
|
* falls back to {@see getNumericIndex} if none was found |
|
134
|
|
|
* |
|
135
|
|
|
* @param PrintRequest $printRequest |
|
136
|
|
|
* |
|
137
|
|
|
* @uses getNumericIndex |
|
138
|
|
|
* |
|
139
|
|
|
* @return int|string |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getKeyFromPrintRequest( PrintRequest $printRequest ) { |
|
142
|
6 |
|
|
|
143
|
|
|
if ( $printRequest->getLabel() !== '' ) { |
|
144
|
6 |
|
return $printRequest->getText( SMW_OUTPUT_WIKI ); |
|
145
|
6 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $this->getNumericIndex(); |
|
148
|
2 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Extracts the data of a single value of the current printRequest / query field |
|
152
|
|
|
* The value is formatted according to the type of the property |
|
153
|
|
|
* |
|
154
|
|
|
* @param DataValue $dataValue |
|
155
|
|
|
* |
|
156
|
|
|
* @return mixed |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getValueFromDataValue( DataValue $dataValue ) { |
|
159
|
9 |
|
|
|
160
|
|
|
switch ( $dataValue->getTypeID() ) { |
|
161
|
9 |
|
case '_boo': |
|
162
|
9 |
|
// boolean value found, convert it |
|
163
|
|
|
$value = in_array( strtolower( $dataValue->getWikiValue() ), $this->msgTrue ); |
|
164
|
1 |
|
break; |
|
165
|
1 |
|
case '_num': |
|
166
|
8 |
|
// number value found |
|
167
|
|
|
/** @var \SMWNumberValue $dataValue */ |
|
168
|
|
|
$value = $dataValue->getNumber(); |
|
169
|
2 |
|
$value = ( $value == (int)$value ) ? intval( $value ) : $value; |
|
170
|
2 |
|
break; |
|
171
|
2 |
|
default: |
|
172
|
7 |
|
# FIXME ignores parameter link=none|subject |
|
173
|
|
|
$value = $dataValue->getShortText( SMW_OUTPUT_WIKI, new \Linker() ); |
|
174
|
7 |
|
} |
|
175
|
9 |
|
|
|
176
|
|
|
return $value; |
|
177
|
9 |
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Takes an array of data fields and returns either null, a single value or a lua table. |
|
181
|
|
|
* |
|
182
|
|
|
* @param array $resultArrayData |
|
183
|
|
|
* |
|
184
|
|
|
* @return mixed |
|
185
|
|
|
*/ |
|
186
|
|
|
public function extractLuaDataFromDVData( $resultArrayData ) { |
|
187
|
8 |
|
|
|
188
|
|
|
if ( empty( $resultArrayData ) ) { |
|
189
|
8 |
|
// this key has no value(s). set to null |
|
190
|
|
|
return null; |
|
191
|
1 |
|
} elseif ( count( $resultArrayData ) == 1 ) { |
|
192
|
8 |
|
// there was only one semantic value found. reduce the array to this value |
|
193
|
|
|
return array_shift( $resultArrayData ); |
|
194
|
6 |
|
} else { |
|
195
|
6 |
|
// $key has multiple values. keep the array |
|
196
|
|
|
// Note: we do not un-shift it (remember: lua array counting starts with 1), but we defer |
|
197
|
|
|
// conversion into a lua table to a later step |
|
198
|
|
|
return $resultArrayData; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
8 |
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Returns current numeric index (and increases it afterwards) |
|
204
|
|
|
* |
|
205
|
|
|
* @uses $numericIndex |
|
206
|
|
|
* |
|
207
|
|
|
* @return int |
|
208
|
|
|
*/ |
|
209
|
|
|
public function getNumericIndex() { |
|
210
|
|
|
return $this->numericIndex++; |
|
211
|
3 |
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|