|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SEQL\ByHttpRequest; |
|
4
|
|
|
|
|
5
|
|
|
use SEQL\DataValueDeserializer; |
|
6
|
|
|
use SMW\DIProperty; |
|
7
|
|
|
use SMW\DIWikiPage; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @license GNU GPL v2+ |
|
11
|
|
|
* @since 1.0 |
|
12
|
|
|
* |
|
13
|
|
|
* @author mwjames |
|
14
|
|
|
*/ |
|
15
|
|
|
class JsonResponseParser { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var DataValueDeserializer |
|
19
|
|
|
*/ |
|
20
|
|
|
private $dataValueDeserializer; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $subjectList = array(); |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private $printRequestPropertyList = array(); |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var boolean |
|
34
|
|
|
*/ |
|
35
|
|
|
private $furtherResults = false; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
private $printouts = array(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
private $internalLabelToKeyMap = array(); |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
private $rawResponseResult = array(); |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @since 1.0 |
|
54
|
|
|
* |
|
55
|
|
|
* @param DataValueDeserializer $dataValueDeserializer |
|
56
|
|
|
*/ |
|
57
|
14 |
|
public function __construct( DataValueDeserializer $dataValueDeserializer ) { |
|
58
|
14 |
|
$this->dataValueDeserializer = $dataValueDeserializer; |
|
59
|
14 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @since 1.0 |
|
63
|
|
|
* |
|
64
|
|
|
* @param DIProperty $property |
|
65
|
|
|
* |
|
66
|
|
|
* @return DIProperty |
|
67
|
|
|
*/ |
|
68
|
3 |
|
public function findPropertyFromInMemoryExternalRepositoryCache( DIProperty $property ) { |
|
69
|
|
|
|
|
70
|
3 |
|
$key = $property->getKey(); |
|
71
|
|
|
|
|
72
|
3 |
|
if ( isset( $this->internalLabelToKeyMap[$key] ) ) { |
|
73
|
1 |
|
$key = $this->internalLabelToKeyMap[$key]; |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
return isset( $this->printRequestPropertyList[$key] ) ? $this->printRequestPropertyList[$key] : $property; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @since 1.0 |
|
81
|
|
|
* |
|
82
|
|
|
* @param DIWikiPage[] |
|
83
|
|
|
*/ |
|
84
|
8 |
|
public function getResultSubjectList() { |
|
85
|
8 |
|
return $this->subjectList; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @since 1.0 |
|
90
|
|
|
* |
|
91
|
|
|
* @param [] |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getPrintouts() { |
|
94
|
|
|
return $this->printouts; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @since 1.0 |
|
99
|
|
|
* |
|
100
|
|
|
* @param [] |
|
101
|
|
|
*/ |
|
102
|
8 |
|
public function getPrintRequestPropertyList() { |
|
103
|
8 |
|
return $this->printRequestPropertyList; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @since 1.0 |
|
108
|
|
|
* |
|
109
|
|
|
* @return boolean |
|
110
|
|
|
*/ |
|
111
|
12 |
|
public function hasFurtherResults() { |
|
112
|
12 |
|
return $this->furtherResults; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @since 1.0 |
|
117
|
|
|
* |
|
118
|
|
|
* @return array |
|
119
|
|
|
*/ |
|
120
|
5 |
|
public function getRawResponseResult() { |
|
121
|
5 |
|
return $this->rawResponseResult; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @since 1.0 |
|
126
|
|
|
* |
|
127
|
|
|
* @param DIWikiPage $subject |
|
128
|
|
|
* @param DIProperty $property |
|
129
|
|
|
* |
|
130
|
|
|
* @return array |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getPropertyValuesFor( DIWikiPage $subject, DIProperty $property ) { |
|
133
|
|
|
|
|
134
|
|
|
$key = $property->getKey(); |
|
135
|
|
|
$hash = $subject->getHash(); |
|
136
|
|
|
|
|
137
|
|
|
if ( isset( $this->internalLabelToKeyMap[$key] ) ) { |
|
138
|
|
|
$key = $this->internalLabelToKeyMap[$key]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return isset( $this->printouts[$hash][$key] ) ? $this->printouts[$hash][$key] : array(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @since 1.0 |
|
146
|
|
|
* |
|
147
|
|
|
* @param array $result |
|
148
|
|
|
*/ |
|
149
|
13 |
|
public function doParse( array $result ) { |
|
150
|
|
|
|
|
151
|
13 |
|
if ( isset( $result['query'] ) ) { |
|
152
|
13 |
|
$this->rawResponseResult = $result['query'] ; |
|
153
|
13 |
|
} |
|
154
|
|
|
|
|
155
|
13 |
|
foreach ( $result as $key => $item ) { |
|
156
|
|
|
|
|
157
|
13 |
|
if ( $key === 'query-continue-offset' ) { |
|
158
|
4 |
|
$this->furtherResults = true; |
|
159
|
4 |
|
continue; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
13 |
|
if ( !isset( $item['printrequests'] ) || !isset( $item['results'] ) ) { |
|
163
|
3 |
|
continue; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
10 |
|
foreach ( $item['printrequests'] as $k => $value ) { |
|
167
|
10 |
|
$this->addPrintRequestToPropertyList( $value ); |
|
168
|
10 |
|
} |
|
169
|
|
|
|
|
170
|
10 |
|
foreach ( $item['results'] as $k => $value ) { |
|
171
|
8 |
|
$this->addResultsToPrintoutList( $k, $value ); |
|
172
|
10 |
|
} |
|
173
|
13 |
|
} |
|
174
|
13 |
|
} |
|
175
|
|
|
|
|
176
|
8 |
|
private function addResultsToPrintoutList( $k, $value ) { |
|
177
|
|
|
|
|
178
|
|
|
// Most likely caused by `mainlabel=-` therefore mark it as special and |
|
179
|
|
|
// restore row integrity |
|
180
|
8 |
|
if ( !isset( $value['namespace'] ) || !isset( $value['fulltext'] ) ) { |
|
181
|
1 |
|
$value['namespace'] = 0; |
|
182
|
1 |
|
$value['fulltext'] = $k; |
|
183
|
1 |
|
} |
|
184
|
|
|
|
|
185
|
8 |
|
$subject = $this->dataValueDeserializer->newDiWikiPage( $value ); |
|
186
|
|
|
|
|
187
|
8 |
|
if ( !$subject ) { |
|
188
|
|
|
return; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
8 |
|
$hash = $subject->getHash(); |
|
192
|
8 |
|
$this->subjectList[] = $subject; |
|
193
|
|
|
|
|
194
|
8 |
|
if ( !isset( $value['printouts'] ) ) { |
|
195
|
|
|
return; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
8 |
|
foreach ( $value['printouts'] as $pk => $pvalues ) { |
|
199
|
8 |
|
$this->addPropertyValues( $hash, $pk, $pvalues ); |
|
200
|
8 |
|
} |
|
201
|
8 |
|
} |
|
202
|
|
|
|
|
203
|
8 |
|
private function addPropertyValues( $hash, $pk, $pvalues ) { |
|
204
|
|
|
|
|
205
|
8 |
|
$property = DIProperty::newFromUserLabel( $pk ); |
|
206
|
8 |
|
$pk = $property->getKey(); |
|
207
|
|
|
|
|
208
|
|
|
// Need to match the property to its possible internal |
|
209
|
|
|
// representation (_INST etc.() |
|
210
|
8 |
|
if ( isset( $this->internalLabelToKeyMap[$pk] ) ) { |
|
211
|
1 |
|
$pk = $this->internalLabelToKeyMap[$pk]; |
|
212
|
1 |
|
} |
|
213
|
|
|
|
|
214
|
8 |
|
if ( !isset( $this->printRequestPropertyList[$pk] ) ) { |
|
215
|
|
|
return; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
8 |
|
$property = $this->printRequestPropertyList[$pk]; |
|
219
|
|
|
|
|
220
|
8 |
|
foreach ( $pvalues as $pvalue ) { |
|
221
|
|
|
|
|
222
|
8 |
|
if ( !isset( $this->printouts[$hash][$pk] ) ) { |
|
223
|
8 |
|
$this->printouts[$hash][$pk] = array(); |
|
224
|
8 |
|
} |
|
225
|
|
|
|
|
226
|
|
|
// Unique row value display |
|
227
|
8 |
|
$vhash = md5( json_encode( $pvalue ) ); |
|
228
|
|
|
|
|
229
|
8 |
|
if ( !isset( $this->printouts[$hash][$pk][$vhash] ) ) { |
|
230
|
8 |
|
$this->printouts[$hash][$pk][$vhash] = $this->dataValueDeserializer->newDataValueFrom( $property, $pvalue ); |
|
231
|
8 |
|
} |
|
232
|
8 |
|
} |
|
233
|
8 |
|
} |
|
234
|
|
|
|
|
235
|
10 |
|
private function addPrintRequestToPropertyList( $value ) { |
|
236
|
|
|
|
|
237
|
10 |
|
if ( $value['label'] === '' ) { |
|
238
|
6 |
|
return; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
10 |
|
if ( $value['mode'] == 0 ) { |
|
242
|
2 |
|
$property = new DIProperty( '_INST' ); |
|
243
|
2 |
|
$this->internalLabelToKeyMap[$value['label']] = $property->getKey(); |
|
244
|
2 |
|
} else { |
|
245
|
8 |
|
$property = DIProperty::newFromUserLabel( $value['label'] ); |
|
246
|
8 |
|
$property->setPropertyTypeId( $value['typeid'] ); |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
10 |
|
if ( isset( $value['redi'] ) ) { |
|
250
|
2 |
|
$this->internalLabelToKeyMap[$value['redi']] = $property->getKey(); |
|
251
|
2 |
|
} |
|
252
|
|
|
|
|
253
|
10 |
|
$property->setInterwiki( $this->dataValueDeserializer->getQuerySource() ); |
|
254
|
10 |
|
$this->printRequestPropertyList[$property->getKey()] = $property; |
|
255
|
10 |
|
} |
|
256
|
|
|
|
|
257
|
|
|
} |
|
258
|
|
|
|