1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SEQL\ByAskApiHttpRequest; |
4
|
|
|
|
5
|
|
|
use Onoi\HttpRequest\HttpRequestFactory; |
6
|
|
|
use SEQL\QueryEncoder; |
7
|
|
|
use SEQL\QueryResultFactory; |
8
|
|
|
use SMWQuery as Query; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 1.0 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class QueryResultFetcher { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var HttpRequestFactory |
20
|
|
|
*/ |
21
|
|
|
private $httpRequestFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var QueryResultFactory |
25
|
|
|
*/ |
26
|
|
|
private $queryResultFactory; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var JsonResponseParser |
30
|
|
|
*/ |
31
|
|
|
private $jsonResponseParser; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $httpRequestEndpoint = ''; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $repositoryTargetUrl = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $httpResponseCachePrefix; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var integer |
50
|
|
|
*/ |
51
|
|
|
private $httpResponseCacheLifetime; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @since 1.0 |
55
|
|
|
* |
56
|
|
|
* @param HttpRequestFactory $httpRequestFactory |
57
|
|
|
* @param QueryResultFactory $queryResultFactory |
58
|
|
|
* @param JsonResponseParser $jsonResponseParser |
59
|
|
|
*/ |
60
|
6 |
|
public function __construct( HttpRequestFactory $httpRequestFactory, QueryResultFactory $queryResultFactory, JsonResponseParser $jsonResponseParser ) { |
61
|
6 |
|
$this->httpRequestFactory = $httpRequestFactory; |
62
|
6 |
|
$this->queryResultFactory = $queryResultFactory; |
63
|
6 |
|
$this->jsonResponseParser = $jsonResponseParser; |
64
|
6 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @since 1.0 |
68
|
|
|
* |
69
|
|
|
* @param string $httpRequestEndpoint |
70
|
|
|
*/ |
71
|
2 |
|
public function setHttpRequestEndpoint( $httpRequestEndpoint ) { |
72
|
2 |
|
$this->httpRequestEndpoint = $httpRequestEndpoint; |
73
|
2 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @since 1.0 |
77
|
|
|
* |
78
|
|
|
* @param string $repositoryTargetUrl |
79
|
|
|
*/ |
80
|
2 |
|
public function setRepositoryTargetUrl( $repositoryTargetUrl ) { |
81
|
2 |
|
$this->repositoryTargetUrl = $repositoryTargetUrl; |
82
|
2 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @since 1.0 |
86
|
|
|
* |
87
|
|
|
* @param string $httpResponseCachePrefix |
88
|
|
|
*/ |
89
|
1 |
|
public function setHttpResponseCachePrefix( $httpResponseCachePrefix ) { |
90
|
1 |
|
$this->httpResponseCachePrefix = $httpResponseCachePrefix; |
91
|
1 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @since 1.0 |
95
|
|
|
* |
96
|
|
|
* @param integer $httpResponseCacheLifetime |
97
|
|
|
*/ |
98
|
1 |
|
public function setHttpResponseCacheLifetime( $httpResponseCacheLifetime ) { |
99
|
1 |
|
$this->httpResponseCacheLifetime = $httpResponseCacheLifetime; |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @since 1.0 |
104
|
|
|
* |
105
|
|
|
* @param Query $query |
106
|
|
|
* |
107
|
|
|
* @return QueryResult |
108
|
|
|
*/ |
109
|
5 |
|
public function fetchQueryResult( Query $query ) { |
110
|
|
|
|
111
|
5 |
|
$this->doResetPrintRequestsToQuerySource( $query ); |
112
|
|
|
|
113
|
5 |
|
$result = $this->doMakeHttpRequestFor( $query ); |
114
|
|
|
|
115
|
5 |
|
if ( $result === array() || $result === false || $result === null ) { |
116
|
3 |
|
return $this->queryResultFactory->newEmptyQueryResult( $query ); |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
if ( isset( $result['error'] ) ) { |
120
|
1 |
|
$query->addErrors( isset( $result['error']['info'] ) ? array( $result['error']['info'] ) : $result['error']['query'] ); |
121
|
1 |
|
return $this->queryResultFactory->newEmptyQueryResult( $query ); |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
$this->jsonResponseParser->doParse( $result ); |
125
|
|
|
|
126
|
1 |
|
$queryResult = $this->queryResultFactory->newByAskApiHttpRequestQueryResult( |
127
|
1 |
|
$query, |
128
|
1 |
|
$this->jsonResponseParser |
129
|
1 |
|
); |
130
|
|
|
|
131
|
1 |
|
$queryResult->setRemoteTargetUrl( |
132
|
1 |
|
str_replace( '$1', '', $this->repositoryTargetUrl ) |
133
|
1 |
|
); |
134
|
|
|
|
135
|
1 |
|
return $queryResult; |
136
|
|
|
} |
137
|
|
|
|
138
|
5 |
|
private function doResetPrintRequestsToQuerySource( $query ) { |
139
|
|
|
|
140
|
5 |
|
$querySource = $query->getQuerySource(); |
141
|
|
|
|
142
|
5 |
|
foreach ( $query->getExtraPrintouts() as $printRequest ) { |
143
|
|
|
|
144
|
5 |
|
if ( $printRequest->getData() === null ) { |
145
|
4 |
|
continue; |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
$property = $printRequest->getData()->getDataItem(); |
149
|
1 |
|
$property->setInterwiki( $querySource ); |
150
|
|
|
|
151
|
1 |
|
$printRequest->getData()->setDataItem( $property ); |
152
|
|
|
|
153
|
|
|
// Reset label after dataItem was re-added |
154
|
1 |
|
$printRequest->setLabel( $printRequest->getLabel() ); |
155
|
5 |
|
} |
156
|
5 |
|
} |
157
|
|
|
|
158
|
5 |
|
private function doMakeHttpRequestFor( $query ) { |
159
|
|
|
|
160
|
5 |
|
$httpRequest = $this->httpRequestFactory->newCachedCurlRequest(); |
161
|
|
|
|
162
|
5 |
|
$httpRequest->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_TTL, $this->httpResponseCacheLifetime ); |
163
|
5 |
|
$httpRequest->setOption( ONOI_HTTP_REQUEST_RESPONSECACHE_PREFIX, $this->httpResponseCachePrefix . ':' ); |
164
|
|
|
|
165
|
5 |
|
$httpRequest->setOption( CURLOPT_FOLLOWLOCATION, true ); |
166
|
|
|
|
167
|
5 |
|
$httpRequest->setOption( CURLOPT_RETURNTRANSFER, true ); |
168
|
5 |
|
$httpRequest->setOption( CURLOPT_FAILONERROR, true ); |
169
|
5 |
|
$httpRequest->setOption( CURLOPT_SSL_VERIFYPEER, false ); |
170
|
|
|
|
171
|
5 |
|
$httpRequest->setOption( CURLOPT_URL, $this->httpRequestEndpoint . '?action=ask&format=json&query=' . QueryEncoder::rawUrlEncode( $query ) ); |
172
|
|
|
|
173
|
5 |
|
$httpRequest->setOption( CURLOPT_HTTPHEADER, array( |
174
|
5 |
|
'Accept: application/json', |
175
|
|
|
'Content-Type: application/json; charset=utf-8' |
176
|
5 |
|
) ); |
177
|
|
|
|
178
|
5 |
|
$response = $httpRequest->execute(); |
179
|
|
|
|
180
|
5 |
|
return json_decode( $response, true ); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|