-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathQueryResultFetcher.php
More file actions
272 lines (209 loc) · 6.92 KB
/
QueryResultFetcher.php
File metadata and controls
272 lines (209 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
namespace SEQL\ByHttpRequest;
use Onoi\HttpRequest\HttpRequestFactory;
use SEQL\QueryEncoder;
use SEQL\QueryResultFactory;
use SMWQuery as Query;
/**
* @license GNU GPL v2+
* @since 1.0
*
* @author mwjames
*/
class QueryResultFetcher {
/**
* @var HttpRequestFactory
*/
private $httpRequestFactory;
/**
* @var QueryResultFactory
*/
private $queryResultFactory;
/**
* @var JsonResponseParser
*/
private $jsonResponseParser;
/**
* @var string
*/
private $httpRequestEndpoint = '';
/**
* @var string
*/
private $repositoryTargetUrl = '';
/**
* @var string
*/
private $httpResponseCachePrefix;
/**
* @var integer
*/
private $httpResponseCacheLifetime;
/**
* @var array
*/
private $credentials;
/**
* @var string
*/
private static $cookies;
/**
* @since 1.0
*
* @param HttpRequestFactory $httpRequestFactory
* @param QueryResultFactory $queryResultFactory
* @param JsonResponseParser $jsonResponseParser
*/
public function __construct( HttpRequestFactory $httpRequestFactory, QueryResultFactory $queryResultFactory, JsonResponseParser $jsonResponseParser, $credentials ) {
$this->httpRequestFactory = $httpRequestFactory;
$this->queryResultFactory = $queryResultFactory;
$this->jsonResponseParser = $jsonResponseParser;
$this->credentials = $credentials;
}
/**
* @since 1.0
*
* @param string $httpRequestEndpoint
*/
public function setHttpRequestEndpoint( $httpRequestEndpoint ) {
$this->httpRequestEndpoint = $httpRequestEndpoint;
}
/**
* @since 1.0
*
* @param string $repositoryTargetUrl
*/
public function setRepositoryTargetUrl( $repositoryTargetUrl ) {
$this->repositoryTargetUrl = $repositoryTargetUrl;
}
/**
* @since 1.0
*
* @param string $httpResponseCachePrefix
*/
public function setHttpResponseCachePrefix( $httpResponseCachePrefix ) {
$this->httpResponseCachePrefix = $httpResponseCachePrefix;
}
/**
* @since 1.0
*
* @param integer $httpResponseCacheLifetime
*/
public function setHttpResponseCacheLifetime( $httpResponseCacheLifetime ) {
$this->httpResponseCacheLifetime = $httpResponseCacheLifetime;
}
/**
* Authenticates query against remote wiki using 'login' api and stores
* cookies to use on other requests
*
* @param array $credentials
*/
public function doAuthenticateRemoteWiki( $credentials ) {
$cookiefile = 'seql_'.time();
$httpRequest = $this->httpRequestFactory->newCurlRequest();
$httpRequest->setOption( CURLOPT_FOLLOWLOCATION, true );
$httpRequest->setOption( CURLOPT_RETURNTRANSFER, true );
$httpRequest->setOption( CURLOPT_FAILONERROR, true );
$httpRequest->setOption( CURLOPT_SSL_VERIFYPEER, false );
$httpRequest->setOption( CURLOPT_COOKIESESSION, true );
$httpRequest->setOption( CURLOPT_COOKIEJAR, $cookiefile );
$httpRequest->setOption( CURLOPT_COOKIEFILE, $cookiefile );
$httpRequest->setOption( CURLOPT_URL, $this->httpRequestEndpoint . '?action=query&format=json&meta=tokens&type=login' );
$response = $httpRequest->execute();
$result = json_decode( $response, true );
if( isset( $result['query']['tokens']['logintoken'] ) ) {
$token = $result['query']['tokens']['logintoken'];
$httpRequest->setOption( CURLOPT_FOLLOWLOCATION, true );
$httpRequest->setOption( CURLOPT_RETURNTRANSFER, true );
$httpRequest->setOption( CURLOPT_FAILONERROR, true );
$httpRequest->setOption( CURLOPT_SSL_VERIFYPEER, false );
$httpRequest->setOption( CURLOPT_POST, true );
$httpRequest->setOption( CURLOPT_URL, $this->httpRequestEndpoint );
$httpRequest->setOption( CURLOPT_COOKIEJAR, $cookiefile );
$httpRequest->setOption( CURLOPT_COOKIEFILE, $cookiefile );
$httpRequest->setOption( CURLOPT_POSTFIELDS, http_build_query( array(
'action' => 'login',
'format' => 'json',
'lgname' => $credentials['username'],
'lgpassword' => $credentials['password'],
'lgtoken' => $token
))
);
$response = $httpRequest->execute();
$result = json_decode( $response, true );
if ( isset( $result['login']['lguserid'] ) ) {
self::$cookies = $cookiefile;
}
}
}
/**
* @since 1.0
*
* @param Query $query
*
* @return QueryResult
*/
public function fetchQueryResult( Query $query ) {
$this->doResetPrintRequestsToQuerySource( $query );
if( $this->credentials && !self::$cookies ) {
$this->doAuthenticateRemoteWiki( $this->credentials );
}
list( $result, $isFromCache ) = $this->doMakeHttpRequestFor( $query );
if ( $result === array() || $result === false || $result === null ) {
return $this->queryResultFactory->newEmptyQueryResult( $query );
}
if ( isset( $result['error'] ) ) {
$query->addErrors(
isset( $result['error']['info'] ) ? array( implode( ', ', $result['error'] ) ) : $result['error']['query']
);
return $this->queryResultFactory->newEmptyQueryResult( $query );
}
// Add the source from where the result was retrieved
if ( isset( $result['query']['meta']['source'] ) ) {
$result['query']['meta']['source'] = $query->getQuerySource();
}
$this->jsonResponseParser->doParse( $result );
$queryResult = $this->queryResultFactory->newByHttpRequestQueryResult(
$query,
$this->jsonResponseParser
);
$queryResult->setFromCache( $isFromCache );
$queryResult->setRemoteTargetUrl(
str_replace( '$1', '', $this->repositoryTargetUrl )
);
return $queryResult;
}
private function doResetPrintRequestsToQuerySource( $query ) {
$querySource = $query->getQuerySource();
foreach ( $query->getExtraPrintouts() as $printRequest ) {
if ( $printRequest->getData() === null ) {
continue;
}
$property = $printRequest->getData()->getDataItem();
$property->setInterwiki( $querySource );
$printRequest->getData()->setDataItem( $property );
// Reset label after dataItem was re-added
$printRequest->setLabel( $printRequest->getLabel() );
}
}
private function doMakeHttpRequestFor( $query ) {
$httpRequest = $this->httpRequestFactory->newCachedCurlRequest();
$httpRequest->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL, $this->httpResponseCacheLifetime );
$httpRequest->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX, $this->httpResponseCachePrefix . ':seql:' );
$httpRequest->setOption( CURLOPT_FOLLOWLOCATION, true );
$httpRequest->setOption( CURLOPT_RETURNTRANSFER, true );
$httpRequest->setOption( CURLOPT_FAILONERROR, true );
$httpRequest->setOption( CURLOPT_SSL_VERIFYPEER, false );
$httpRequest->setOption( CURLOPT_URL, $this->httpRequestEndpoint . '?action=ask&format=json&query=' . QueryEncoder::rawUrlEncode( $query ) );
$httpRequest->setOption( CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json; charset=utf-8'
) );
if( self::$cookies ) {
$httpRequest->setOption( CURLOPT_COOKIEJAR, self::$cookies );
$httpRequest->setOption( CURLOPT_COOKIEFILE, self::$cookies );
}
$response = $httpRequest->execute();
return array( json_decode( $response, true ), $httpRequest->isFromCache() );
}
}