MediaWiki  master
SparqlClient.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\Sparql;
22 
24 
30 class SparqlClient {
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 
64  private $requestFactory;
65 
70  public function __construct( $url, HttpRequestFactory $requestFactory ) {
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 }
MediaWiki\Sparql\SparqlClient\setUserAgent
setUserAgent( $agent)
Mote it is not recommended to completely override user agent for most applications.
Definition: SparqlClient.php:112
MediaWiki\Http\HttpRequestFactory
Factory creating MWHttpRequest objects.
Definition: HttpRequestFactory.php:35
MediaWiki\Sparql\SparqlClient\appendUserAgent
appendUserAgent( $agent)
Append specific string to user agent.
Definition: SparqlClient.php:125
MediaWiki\Sparql\SparqlClient
Simple SPARQL client.
Definition: SparqlClient.php:30
MediaWiki\Sparql\SparqlClient\$endpoint
string $endpoint
SPARQL endpoint URL.
Definition: SparqlClient.php:53
MediaWiki\Sparql\SparqlClient\$timeout
int $timeout
Query timeout (seconds)
Definition: SparqlClient.php:47
MediaWiki\Sparql\SparqlClient\setClientOptions
setClientOptions( $options)
Definition: SparqlClient.php:92
MediaWiki\Sparql\SparqlClient\$requestFactory
HttpRequestFactory $requestFactory
Definition: SparqlClient.php:64
MediaWiki\Sparql\SparqlClient\$options
array $options
Client options.
Definition: SparqlClient.php:59
MediaWiki\Http\HttpRequestFactory\getUserAgent
getUserAgent()
Definition: HttpRequestFactory.php:214
wfAppendQuery
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Definition: GlobalFunctions.php:422
MediaWiki\Sparql\SparqlClient\$userAgent
string $userAgent
User agent for HTTP requests.
Definition: SparqlClient.php:41
MediaWiki\Sparql\SparqlClient\query
query( $sparql, $rawData=false)
Query SPARQL endpoint.
Definition: SparqlClient.php:139
MediaWiki\Sparql\SparqlClient\__construct
__construct( $url, HttpRequestFactory $requestFactory)
Definition: SparqlClient.php:70
MediaWiki\Sparql\SparqlClient\getUserAgent
getUserAgent()
Get current user agent.
Definition: SparqlClient.php:101
MediaWiki\Sparql\SparqlClient\setTimeout
setTimeout( $timeout)
Set query timeout (in seconds)
Definition: SparqlClient.php:81
MediaWiki\Sparql\SparqlException
Exception for SPARQLClient.
Definition: SparqlException.php:30
MediaWiki\Sparql
Definition: SparqlClient.php:21
MediaWiki\Sparql\SparqlClient\extractData
extractData( $data, $rawData=false)
Extract data from SPARQL response format.
Definition: SparqlClient.php:193
MediaWiki\Sparql\SparqlClient\MAX_GET_SIZE
const MAX_GET_SIZE
Limit on how long can be the query to be sent by GET.
Definition: SparqlClient.php:35