1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SEQL\ByHttpRequest; |
4
|
|
|
|
5
|
|
|
use SMW\DataValueFactory; |
6
|
|
|
use SMW\DIProperty; |
7
|
|
|
use SMW\DIWikiPage; |
8
|
|
|
use SMW\Localizer; |
9
|
|
|
use SMW\Query\PrintRequest; |
10
|
|
|
use SMWDataValue as DataValue; |
11
|
|
|
use SMWResultArray as ResultArray; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @license GNU GPL v2+ |
15
|
|
|
* @since 1.0 |
16
|
|
|
* |
17
|
|
|
* @author mwjames |
18
|
|
|
*/ |
19
|
|
|
class CannedResultArray extends ResultArray { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var PrintRequest |
23
|
|
|
*/ |
24
|
|
|
private $mPrintRequest; |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var DIWikiPage |
28
|
|
|
*/ |
29
|
|
|
private $mResult; |
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var JsonResponseParser |
33
|
|
|
*/ |
34
|
|
|
private $jsonResponseParser; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var DataValue[]|false |
38
|
|
|
*/ |
39
|
|
|
private $mContent; |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @since 1.0 |
43
|
|
|
* |
44
|
|
|
* @param DIWikiPage $resultPage |
45
|
|
|
* @param PrintRequest $printRequest |
46
|
|
|
* @param JsonResponseParser $jsonResponseParser |
47
|
|
|
*/ |
48
|
7 |
|
public function __construct( DIWikiPage $resultPage, PrintRequest $printRequest, JsonResponseParser $jsonResponseParser ) { |
49
|
7 |
|
$this->mResult = $resultPage; |
50
|
7 |
|
$this->mPrintRequest = $printRequest; |
51
|
7 |
|
$this->jsonResponseParser = $jsonResponseParser; |
52
|
7 |
|
$this->mContent = false; |
53
|
7 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @see ResultArray::getResultSubject |
57
|
|
|
* |
58
|
|
|
* @return DIWikiPage |
59
|
|
|
*/ |
60
|
1 |
|
public function getResultSubject() { |
61
|
1 |
|
return $this->mResult; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @see ResultArray::getContent |
66
|
|
|
* |
67
|
|
|
* @return SMWDataItem[]|false |
68
|
|
|
*/ |
69
|
4 |
|
public function getContent() { |
70
|
4 |
|
$this->loadContent(); |
71
|
|
|
|
72
|
4 |
|
if ( !$this->mContent ) { |
73
|
|
|
return $this->mContent; |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
$content = array(); |
77
|
|
|
|
78
|
4 |
|
foreach ( $this->mContent as $value ) { |
79
|
4 |
|
$content[] = $value instanceof DataValue ? $value->getDataItem() : $value; |
80
|
4 |
|
} |
81
|
|
|
|
82
|
4 |
|
return $content; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @see ResultArray::getPrintRequest |
87
|
|
|
* |
88
|
|
|
* @return PrintRequest |
89
|
|
|
*/ |
90
|
|
|
public function getPrintRequest() { |
91
|
|
|
return $this->mPrintRequest; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @see ResultArray::getNextDataItem |
96
|
|
|
* |
97
|
|
|
* @since 1.6 |
98
|
|
|
* |
99
|
|
|
* @return SMWDataItem|false |
100
|
|
|
*/ |
101
|
4 |
|
public function getNextDataItem() { |
102
|
|
|
|
103
|
4 |
|
$this->loadContent(); |
104
|
4 |
|
$result = current( $this->mContent ); |
105
|
4 |
|
next( $this->mContent ); |
106
|
|
|
|
107
|
4 |
|
return $result instanceof DataValue ? $result->getDataItem() : $result; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @see ResultArray::reset |
112
|
|
|
* |
113
|
|
|
* @since 1.7.1 |
114
|
|
|
* |
115
|
|
|
* @return SMWDataItem|false |
116
|
|
|
*/ |
117
|
4 |
|
public function reset() { |
118
|
|
|
|
119
|
4 |
|
$this->loadContent(); |
120
|
4 |
|
$result = reset( $this->mContent ); |
121
|
|
|
|
122
|
4 |
|
return $result instanceof DataValue ? $result->getDataItem() : $result; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @see ResultArray::getNextDataValue |
127
|
|
|
* |
128
|
|
|
* @since 1.6 |
129
|
|
|
* |
130
|
|
|
* @return SMWDataValue|false |
131
|
|
|
*/ |
132
|
5 |
|
public function getNextDataValue() { |
133
|
|
|
|
134
|
5 |
|
$this->loadContent(); |
135
|
5 |
|
$content = current( $this->mContent ); |
136
|
5 |
|
next( $this->mContent ); |
137
|
|
|
|
138
|
5 |
|
if ( $content === false ) { |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
|
142
|
5 |
|
$property = $this->getMatchablePropertyFromPrintRequest(); |
143
|
|
|
|
144
|
|
|
// Units of measurement can not be assumed to be declared on a wiki |
145
|
|
|
// therefore don't try to recreate a DataValue and use the DV created |
146
|
|
|
// from the raw API response |
147
|
5 |
|
if ( $this->mPrintRequest->getMode() === PrintRequest::PRINT_PROP && |
148
|
5 |
|
$property->findPropertyTypeId() === '_qty' ) { |
149
|
1 |
|
return $content; |
150
|
|
|
} |
151
|
|
|
|
152
|
4 |
|
if ( $this->mPrintRequest->getMode() === PrintRequest::PRINT_PROP && |
153
|
4 |
|
strpos( $property->findPropertyTypeId(), '_rec' ) !== false ) { |
154
|
|
|
|
155
|
|
|
if ( $this->mPrintRequest->getParameter( 'index' ) === false ) { |
156
|
|
|
return $content; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$pos = $this->mPrintRequest->getParameter( 'index' ) - 1; |
160
|
|
|
$dataItems = $content->getDataItems(); |
|
|
|
|
161
|
|
|
|
162
|
|
|
if ( !array_key_exists( $pos, $dataItems ) ) { |
163
|
|
|
return $content; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$diProperties = $content->getPropertyDataItems(); |
|
|
|
|
167
|
|
|
$content = $dataItems[$pos]; |
168
|
|
|
|
169
|
|
|
if ( array_key_exists( $pos, $diProperties ) && |
170
|
|
|
!is_null( $diProperties[$pos] ) ) { |
171
|
|
|
$diProperty = $diProperties[$pos]; |
172
|
|
|
} else { |
173
|
|
|
$diProperty = null; |
174
|
|
|
} |
175
|
|
|
|
176
|
4 |
|
} elseif ( $this->mPrintRequest->getMode() == PrintRequest::PRINT_PROP ) { |
177
|
1 |
|
$diProperty = $property; |
178
|
1 |
|
} else { |
179
|
3 |
|
$diProperty = null; |
180
|
|
|
} |
181
|
|
|
|
182
|
4 |
|
if ( $content instanceof DataValue ) { |
183
|
1 |
|
$content = $content->getDataItem(); |
184
|
1 |
|
} |
185
|
|
|
|
186
|
4 |
|
$dataValue = DataValueFactory::getInstance()->newDataItemValue( |
|
|
|
|
187
|
4 |
|
$content, |
188
|
|
|
$diProperty |
189
|
4 |
|
); |
190
|
|
|
|
191
|
4 |
|
$dataValue->setContextPage( |
192
|
4 |
|
$this->mResult |
|
|
|
|
193
|
4 |
|
); |
194
|
|
|
|
195
|
4 |
|
if ( $this->mPrintRequest->getOutputFormat() ) { |
196
|
|
|
$dataValue->setOutputFormat( $this->mPrintRequest->getOutputFormat() ); |
197
|
|
|
} |
198
|
|
|
|
199
|
4 |
|
return $dataValue; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Load results of the given print request and result subject. This is only |
204
|
|
|
* done when needed. |
205
|
|
|
*/ |
206
|
5 |
|
protected function loadContent() { |
207
|
5 |
|
if ( $this->mContent !== false ) { |
208
|
4 |
|
return; |
209
|
|
|
} |
210
|
|
|
|
211
|
5 |
|
$this->mContent = array(); |
212
|
|
|
|
213
|
5 |
|
switch ( $this->mPrintRequest->getMode() ) { |
214
|
5 |
|
case PrintRequest::PRINT_THIS: |
215
|
2 |
|
$this->mContent = array( $this->mResult ); |
|
|
|
|
216
|
2 |
|
break; |
217
|
3 |
|
case PrintRequest::PRINT_CCAT: |
218
|
3 |
|
case PrintRequest::PRINT_CATS: |
219
|
|
|
|
220
|
1 |
|
$this->mContent = $this->jsonResponseParser->getPropertyValuesFor( |
221
|
1 |
|
$this->mResult, |
222
|
1 |
|
new DIProperty( '_INST' ) |
223
|
1 |
|
); |
224
|
|
|
|
225
|
1 |
|
break; |
226
|
2 |
|
case PrintRequest::PRINT_PROP: |
227
|
2 |
|
$propertyValue = $this->mPrintRequest->getData(); |
228
|
|
|
|
229
|
2 |
|
if ( $propertyValue->isValid() ) { |
230
|
2 |
|
$this->mContent = $this->jsonResponseParser->getPropertyValuesFor( |
231
|
2 |
|
$this->mResult, |
232
|
2 |
|
$this->getMatchablePropertyFromPrintRequest() |
|
|
|
|
233
|
2 |
|
); |
234
|
2 |
|
} |
235
|
|
|
|
236
|
2 |
|
break; |
237
|
|
|
default: |
238
|
|
|
$this->mContent = array(); // Unknown print request. |
239
|
5 |
|
} |
240
|
|
|
|
241
|
5 |
|
reset( $this->mContent ); |
242
|
5 |
|
} |
243
|
|
|
|
244
|
5 |
|
private function getMatchablePropertyFromPrintRequest() { |
245
|
|
|
|
246
|
5 |
|
if ( $this->mPrintRequest->getMode() !== PrintRequest::PRINT_PROP ) { |
247
|
3 |
|
return null; |
248
|
|
|
} |
249
|
|
|
|
250
|
2 |
|
$property = $this->mPrintRequest->getData()->getDataItem(); |
251
|
|
|
|
252
|
|
|
// The API may not deploy the natural property key (until 0.8+) hence something |
253
|
|
|
// like |?Has population=Population (in K) does not return a valid result from |
254
|
|
|
// the parser because "Has population" cannot be connected to "Population (in K)" |
255
|
|
|
// without the extra "key" field therefore construct a new property to match the |
256
|
|
|
// label |
257
|
2 |
|
if ( $this->mPrintRequest->getLabel() !== '' && $this->mPrintRequest->getLabel() !== $property->getLabel() ) { |
258
|
1 |
|
return $this->jsonResponseParser->findPropertyFromInMemoryExternalRepositoryCache( |
259
|
1 |
|
DIProperty::newFromUserLabel( $this->mPrintRequest->getLabel() ) |
260
|
1 |
|
); |
261
|
|
|
} |
262
|
|
|
|
263
|
1 |
|
return $this->jsonResponseParser->findPropertyFromInMemoryExternalRepositoryCache( |
264
|
|
|
$property |
265
|
1 |
|
); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
} |
269
|
|
|
|