Jump to content


From mediawiki.org

This code implements simple SPARQL client, which connects to given endpoint, runs a query and fetches the results.


public function __construct( $url, HttpRequestFactory $requestFactory )

To construct the client, you should supply endpoint URL and HTTP request factory object. The factory can be obtained as:

$factory = \MediaWiki\MediaWikiServices::getInstance()->getHttpRequestFactory();


public function query( $sparql, $rawData = false )

Executes SPARQL query, and returns the array of rows, each representing variable => value map. If $rawData is set, then the value represents encoded RDF value as specified in SPARQL 1.1 Query Results JSON Format. If it is not set (default), the value is just string.

If an error happens, MediaWiki\Sparql\SparqlException will be thrown.

Other methods[edit]

public function setClientOptions( $options )

Sets HTTP client options, as defined by MWHttpRequest class.

public function setTimeout( $timeout )

Sets query timeout, in seconds. The default is 30 seconds. Note that the query time is also limited by the SPARQL endpoint timeout settings.

public function appendUserAgent( $agent )

Appends custom string to HTTP client agent string. The default user agent is HTTP client user agent string with " SparqlClient" appended. This is useful to distinguish between SPARQL requests from different parts of the application.