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