MediaWiki REL1_34
SparqlClient.php
Go to the documentation of this file.
1<?php
22
23use Http;
25
32
36 const MAX_GET_SIZE = 2048;
37
42 private $userAgent;
43
48 private $timeout = 30;
49
54 private $endpoint;
55
60 private $options = [];
61
66
72 $this->endpoint = $url;
73 $this->requestFactory = $requestFactory;
74 $this->userAgent = Http::userAgent() . " SparqlClient";
75 }
76
82 public function setTimeout( $timeout ) {
83 if ( $timeout >= 0 ) {
84 $this->timeout = $timeout;
85 }
86 return $this;
87 }
88
94 public function setClientOptions( $options ) {
95 $this->options = $options;
96 return $this;
97 }
98
103 public function getUserAgent() {
104 return $this->userAgent;
105 }
106
116 public function setUserAgent( $agent ) {
117 $this->userAgent = $agent;
118 }
119
129 public function appendUserAgent( $agent ) {
130 $this->userAgent .= ' ' . $agent;
131 }
132
143 public function query( $sparql, $rawData = false ) {
144 if ( empty( $this->endpoint ) ) {
145 throw new SparqlException( 'Endpoint URL can not be empty' );
146 }
147 $queryData = [ "query" => $sparql, "format" => "json" ];
148 $options = array_merge( [ 'method' => 'GET' ], $this->options );
149
150 if ( empty( $options['userAgent'] ) ) {
151 $options['userAgent'] = $this->userAgent;
152 }
153
154 if ( $this->timeout >= 0 ) {
155 // Blazegraph setting, see https://wiki.blazegraph.com/wiki/index.php/REST_API
156 $queryData['maxQueryTimeMillis'] = $this->timeout * 1000;
157 $options['timeout'] = $this->timeout;
158 }
159
160 if ( strlen( $sparql ) > self::MAX_GET_SIZE ) {
161 // big requests go to POST
162 $options['method'] = 'POST';
163 $options['postData'] = 'query=' . urlencode( $sparql );
164 unset( $queryData['query'] );
165 }
166
167 $url = wfAppendQuery( $this->endpoint, $queryData );
168 $request = $this->requestFactory->create( $url, $options, __METHOD__ );
169
170 $status = $request->execute();
171
172 if ( !$status->isOK() ) {
173 throw new SparqlException( 'HTTP error: ' . $status->getWikiText( false, false, 'en' ) );
174 }
175 $result = $request->getContent();
176 \Wikimedia\suppressWarnings();
177 $data = json_decode( $result, true );
178 \Wikimedia\restoreWarnings();
179 if ( $data === null || $data === false ) {
180 throw new SparqlException( "HTTP request failed, response:\n" .
181 substr( $result, 1024 ) );
182 }
183
184 return $this->extractData( $data, $rawData );
185 }
186
197 private function extractData( $data, $rawData = false ) {
198 $result = [];
199 if ( $data && !empty( $data['results'] ) ) {
200 $vars = $data['head']['vars'];
201 $resrow = [];
202 foreach ( $data['results']['bindings'] as $row ) {
203 foreach ( $vars as $var ) {
204 if ( !isset( $row[$var] ) ) {
205 $resrow[$var] = null;
206 continue;
207 }
208 if ( $rawData ) {
209 $resrow[$var] = $row[$var];
210 } else {
211 $resrow[$var] = $row[$var]['value'];
212 }
213 }
214 $result[] = $resrow;
215 }
216 }
217 return $result;
218 }
219
220}
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Various HTTP related functions.
Definition Http.php:29
static userAgent()
A standard user-agent we can use for external requests.
Definition Http.php:98
Factory creating MWHttpRequest objects.
Simple SPARQL client.
string $endpoint
SPARQL endpoint URL.
getUserAgent()
Get current user agent.
setClientOptions( $options)
Set client options.
query( $sparql, $rawData=false)
Query SPARQL endpoint.
setTimeout( $timeout)
Set query timeout (in seconds)
string $userAgent
User agent for HTTP requests.
appendUserAgent( $agent)
Append specific string to user agent.
const MAX_GET_SIZE
Limit on how long can be the query to be sent by GET.
__construct( $url, HttpRequestFactory $requestFactory)
int $timeout
Query timeout (seconds)
setUserAgent( $agent)
Set user agent string.
extractData( $data, $rawData=false)
Extract data from SPARQL response format.
array $options
Client options.
HttpRequestFactory $requestFactory
Exception for SPARQLClient.