72 $this->endpoint = $url;
117 $this->userAgent = $agent;
130 $this->userAgent .=
' ' . $agent;
143 public function query( $sparql, $rawData =
false ) {
144 if ( empty( $this->endpoint ) ) {
147 $queryData = [
"query" => $sparql,
"format" =>
"json" ];
148 $options = array_merge( [
'method' =>
'GET' ], $this->options );
150 if ( empty(
$options[
'userAgent'] ) ) {
154 if ( $this->timeout >= 0 ) {
156 $queryData[
'maxQueryTimeMillis'] = $this->timeout * 1000;
160 if ( strlen( $sparql ) > self::MAX_GET_SIZE ) {
163 $options[
'postData'] =
'query=' . urlencode( $sparql );
164 unset( $queryData[
'query'] );
168 $request = $this->requestFactory->create( $url,
$options, __METHOD__ );
170 $status = $request->execute();
172 if ( !$status->isOK() ) {
173 throw new SparqlException(
'HTTP error: ' . $status->getWikiText(
false,
false,
'en' ) );
175 $result = $request->getContent();
176 \Wikimedia\suppressWarnings();
177 $data = json_decode( $result,
true );
178 \Wikimedia\restoreWarnings();
179 if ( $data ===
null || $data ===
false ) {
181 substr( $result, 1024 ) );
199 if ( $data && !empty( $data[
'results'] ) ) {
200 $vars = $data[
'head'][
'vars'];
202 foreach ( $data[
'results'][
'bindings'] as $row ) {
203 foreach ( $vars as $var ) {
204 if ( !isset( $row[$var] ) ) {
205 $resrow[$var] =
null;
209 $resrow[$var] = $row[$var];
211 $resrow[$var] = $row[$var][
'value'];
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.
static userAgent()
A standard user-agent we can use for external requests.