| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Sparql; |
| 22 |
|
| 23 |
use MediaWiki\Http\HttpRequestFactory; |
| 24 |
use Wikimedia\AtEase\AtEase; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class SparqlClient { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public const MAX_GET_SIZE = 2048; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private string $userAgent; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private int $timeout = 30; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private string $endpoint; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private array $options = []; |
| 57 |
|
| 58 |
private HttpRequestFactory $requestFactory; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function __construct( string $url, HttpRequestFactory $requestFactory ) { |
| 65 |
$this->endpoint = $url; |
| 66 |
$this->requestFactory = $requestFactory; |
| 67 |
$this->userAgent = $requestFactory->getUserAgent() . " SparqlClient"; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function setTimeout( int $timeout ): SparqlClient { |
| 76 |
if ( $timeout >= 0 ) { |
| 77 |
$this->timeout = $timeout; |
| 78 |
} |
| 79 |
return $this; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function setClientOptions( array $options ): SparqlClient { |
| 87 |
$this->options = $options; |
| 88 |
return $this; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function getUserAgent(): string { |
| 96 |
return $this->userAgent; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public function setUserAgent( string $agent ) { |
| 107 |
$this->userAgent = $agent; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function appendUserAgent( string $agent ) { |
| 120 |
$this->userAgent .= ' ' . $agent; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function query( string $sparql, bool $rawData = false ): array { |
| 134 |
if ( !$this->endpoint ) { |
| 135 |
throw new SparqlException( 'Endpoint URL can not be empty' ); |
| 136 |
} |
| 137 |
$queryData = [ "query" => $sparql, "format" => "json" ]; |
| 138 |
$options = array_merge( [ 'method' => 'GET' ], $this->options ); |
| 139 |
|
| 140 |
if ( empty( $options['userAgent'] ) ) { |
| 141 |
$options['userAgent'] = $this->userAgent; |
| 142 |
} |
| 143 |
|
| 144 |
if ( $this->timeout >= 0 ) { |
| 145 |
|
| 146 |
$queryData['maxQueryTimeMillis'] = $this->timeout * 1000; |
| 147 |
$options['timeout'] = $this->timeout; |
| 148 |
} |
| 149 |
|
| 150 |
if ( strlen( $sparql ) > self::MAX_GET_SIZE ) { |
| 151 |
|
| 152 |
$options['method'] = 'POST'; |
| 153 |
$options['postData'] = 'query=' . urlencode( $sparql ); |
| 154 |
unset( $queryData['query'] ); |
| 155 |
} |
| 156 |
|
| 157 |
$url = wfAppendQuery( $this->endpoint, $queryData ); |
| 158 |
$request = $this->requestFactory->create( $url, $options, __METHOD__ ); |
| 159 |
|
| 160 |
$status = $request->execute(); |
| 161 |
|
| 162 |
if ( !$status->isOK() ) { |
| 163 |
throw new SparqlException( 'HTTP error: ' . $status->getWikiText( false, false, 'en' ) ); |
| 164 |
} |
| 165 |
$result = $request->getContent(); |
| 166 |
AtEase::suppressWarnings(); |
| 167 |
$data = json_decode( $result, true ); |
| 168 |
AtEase::restoreWarnings(); |
| 169 |
if ( $data === null || $data === false ) { |
| 170 |
throw new SparqlException( "HTTP request failed, response:\n" . |
| 171 |
substr( $result, 1024 ) ); |
| 172 |
} |
| 173 |
|
| 174 |
return $this->extractData( $data, $rawData ); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
private function extractData( array $data, bool $rawData = false ): array { |
| 188 |
$result = []; |
| 189 |
if ( $data && !empty( $data['results'] ) ) { |
| 190 |
$vars = $data['head']['vars']; |
| 191 |
$resrow = []; |
| 192 |
foreach ( $data['results']['bindings'] as $row ) { |
| 193 |
foreach ( $vars as $var ) { |
| 194 |
if ( !isset( $row[$var] ) ) { |
| 195 |
$resrow[$var] = null; |
| 196 |
continue; |
| 197 |
} |
| 198 |
if ( $rawData ) { |
| 199 |
$resrow[$var] = $row[$var]; |
| 200 |
} else { |
| 201 |
$resrow[$var] = $row[$var]['value']; |
| 202 |
} |
| 203 |
} |
| 204 |
$result[] = $resrow; |
| 205 |
} |
| 206 |
} |
| 207 |
return $result; |
| 208 |
} |
| 209 |
|
| 210 |
} |