Debian Codegraph @

includes/sparql/SparqlClient.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Sparql;
22
23 use MediaWiki\Http\HttpRequestFactory;
24 use Wikimedia\AtEase\AtEase;
25
26 /**
27 * Simple SPARQL client
28 *
29 * @author Stas Malyshev
30 */
31 class SparqlClient {
32
33 /**
34 * Limit on how long can be the query to be sent by GET.
35 */
36 public const MAX_GET_SIZE = 2048;
37
38 /**
39 * User agent for HTTP requests.
40 */
41 private string $userAgent;
42
43 /**
44 * Query timeout (seconds)
45 */
46 private int $timeout = 30;
47
48 /**
49 * SPARQL endpoint URL
50 */
51 private string $endpoint;
52
53 /**
54 * Client options
55 */
56 private array $options = [];
57
58 private HttpRequestFactory $requestFactory;
59
60 /**
61 * @param string $url SPARQL Endpoint
62 * @param HttpRequestFactory $requestFactory
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 * Set query timeout (in seconds)
72 * @param int $timeout
73 * @return $this
74 */
75 public function setTimeout( int $timeout ): SparqlClient {
76 if ( $timeout >= 0 ) {
77 $this->timeout = $timeout;
78 }
79 return $this;
80 }
81
82 /**
83 * @param array $options
84 * @return $this
85 */
86 public function setClientOptions( array $options ): SparqlClient {
87 $this->options = $options;
88 return $this;
89 }
90
91 /**
92 * Get current user agent.
93 * @return string
94 */
95 public function getUserAgent(): string {
96 return $this->userAgent;
97 }
98
99 /**
100 * Mote it is not recommended to completely override user agent for
101 * most applications.
102 * @see appendUserAgent() for recommended way of specifying user agent.
103 *
104 * @param string $agent
105 */
106 public function setUserAgent( string $agent ) {
107 $this->userAgent = $agent;
108 }
109
110 /**
111 * Append specific string to user agent.
112 *
113 * This is the recommended way of specifying the user agent
114 * for specific applications of the SparqlClient inside MediaWiki
115 * and extension code.
116 *
117 * @param string $agent
118 */
119 public function appendUserAgent( string $agent ) {
120 $this->userAgent .= ' ' . $agent;
121 }
122
123 /**
124 * Query SPARQL endpoint
125 *
126 * @param string $sparql query
127 * @param bool $rawData Whether to return only values or full data objects
128 *
129 * @return array[] List of results, one row per array element
130 * Each row will contain fields indexed by variable name.
131 * @throws SparqlException
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 // Blazegraph setting, see https://wiki.blazegraph.com/wiki/index.php/REST_API
146 $queryData['maxQueryTimeMillis'] = $this->timeout * 1000;
147 $options['timeout'] = $this->timeout;
148 }
149
150 if ( strlen( $sparql ) > self::MAX_GET_SIZE ) {
151 // big requests go to POST
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 * Extract data from SPARQL response format.
179 * The response must be in format described in:
180 * https://www.w3.org/TR/sparql11-results-json/
181 *
182 * @param array $data SPARQL result
183 * @param bool $rawData Whether to return only values or full data objects
184 *
185 * @return array[] List of results, one row per element.
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 }