MediaWiki REL1_37
SparqlClient.php
Go to the documentation of this file.
1<?php
22
24
31
35 public const MAX_GET_SIZE = 2048;
36
41 private $userAgent;
42
47 private $timeout = 30;
48
53 private $endpoint;
54
59 private $options = [];
60
65
71 $this->endpoint = $url;
72 $this->requestFactory = $requestFactory;
73 $this->userAgent = $requestFactory->getUserAgent() . " SparqlClient";
74 }
75
81 public function setTimeout( $timeout ) {
82 if ( $timeout >= 0 ) {
83 $this->timeout = $timeout;
84 }
85 return $this;
86 }
87
92 public function setClientOptions( $options ) {
93 $this->options = $options;
94 return $this;
95 }
96
101 public function getUserAgent() {
102 return $this->userAgent;
103 }
104
112 public function setUserAgent( $agent ) {
113 $this->userAgent = $agent;
114 }
115
125 public function appendUserAgent( $agent ) {
126 $this->userAgent .= ' ' . $agent;
127 }
128
139 public function query( $sparql, $rawData = false ) {
140 if ( empty( $this->endpoint ) ) {
141 throw new SparqlException( 'Endpoint URL can not be empty' );
142 }
143 $queryData = [ "query" => $sparql, "format" => "json" ];
144 $options = array_merge( [ 'method' => 'GET' ], $this->options );
145
146 if ( empty( $options['userAgent'] ) ) {
147 $options['userAgent'] = $this->userAgent;
148 }
149
150 if ( $this->timeout >= 0 ) {
151 // Blazegraph setting, see https://wiki.blazegraph.com/wiki/index.php/REST_API
152 $queryData['maxQueryTimeMillis'] = $this->timeout * 1000;
153 $options['timeout'] = $this->timeout;
154 }
155
156 if ( strlen( $sparql ) > self::MAX_GET_SIZE ) {
157 // big requests go to POST
158 $options['method'] = 'POST';
159 $options['postData'] = 'query=' . urlencode( $sparql );
160 unset( $queryData['query'] );
161 }
162
163 $url = wfAppendQuery( $this->endpoint, $queryData );
164 $request = $this->requestFactory->create( $url, $options, __METHOD__ );
165
166 $status = $request->execute();
167
168 if ( !$status->isOK() ) {
169 throw new SparqlException( 'HTTP error: ' . $status->getWikiText( false, false, 'en' ) );
170 }
171 $result = $request->getContent();
172 \Wikimedia\suppressWarnings();
173 $data = json_decode( $result, true );
174 \Wikimedia\restoreWarnings();
175 if ( $data === null || $data === false ) {
176 throw new SparqlException( "HTTP request failed, response:\n" .
177 substr( $result, 1024 ) );
178 }
179
180 return $this->extractData( $data, $rawData );
181 }
182
193 private function extractData( $data, $rawData = false ) {
194 $result = [];
195 if ( $data && !empty( $data['results'] ) ) {
196 $vars = $data['head']['vars'];
197 $resrow = [];
198 foreach ( $data['results']['bindings'] as $row ) {
199 foreach ( $vars as $var ) {
200 if ( !isset( $row[$var] ) ) {
201 $resrow[$var] = null;
202 continue;
203 }
204 if ( $rawData ) {
205 $resrow[$var] = $row[$var];
206 } else {
207 $resrow[$var] = $row[$var]['value'];
208 }
209 }
210 $result[] = $resrow;
211 }
212 }
213 return $result;
214 }
215
216}
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Factory creating MWHttpRequest objects.
Simple SPARQL client.
string $endpoint
SPARQL endpoint URL.
getUserAgent()
Get current user agent.
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)
Mote it is not recommended to completely override user agent for most applications.
extractData( $data, $rawData=false)
Extract data from SPARQL response format.
array $options
Client options.
HttpRequestFactory $requestFactory
Exception for SPARQLClient @newable.