forked from alexlatchford/phpSesame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpSesame.php
415 lines (361 loc) · 11.8 KB
/
phpSesame.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
/**
*
*@package phpSesame
*/
require_once "HTTP/Request2.php";//The PEAR base directory must be in your include_path
require_once "resultFormats.php";
/**
* This Class is an interface to Sesame. It connects and perform queries through http.
*
* This class requires Sesame running in a servlet container (tested on tomcat).
* The class does not perform all the operation implemented in the sesame http api but
* does the minimal work needed for hyperJournal. You can find more details about Sesame
* and its HTTP API at www.openrdf.org
*
* Based on the phesame library, http://www.hjournal.org/phesame/
*
* @author Alex Latchford
* @version 0.1
*/
class phpSesame
{
// Return MIME types
const SPARQL_XML = 'application/sparql-results+xml';
const SPARQL_JSON = 'application/sparql-results+json'; // Unsupported - No results handler
const BINARY_TABLE = 'application/x-binary-rdf-results-table'; // Unsupported - No results handler
const BOOLEAN = 'text/boolean'; // Unsupported - No results handler
// Input MIME Types
const RDFXML = 'application/rdf+xml';
const NTRIPLES = 'text/plain';
const TURTLE = 'application/x-turtle';
const N3 = 'text/rdf+n3';
const TRIX = 'application/trix';
const TRIG = 'application/x-trig';
//const RDFTRANSACTION = 'application/x-rdftransaction'; // Unsupported, needs more documentation (http://www.franz.com/agraph/allegrograph/doc/http-protocol.html#header3-67)
/**
* @var string connection string
*/
private $dsn;
/**
* @var string the selected repository
*/
private $repository;
/**
*
*
* @param string $sesameUrl Sesame server connection string
* @param string $repository The repository name
*/
function __construct($sesameUrl = 'http://localhost:8080/openrdf-sesame', $repository = null)
{
$this->dsn = $sesameUrl;
$this->setRepository($repository);
}
/**
* Selects a repository to work on
*
* @return void
* @param string $rep The repository name
*/
public function setRepository($rep)
{
$this->repository = $rep;
}
/**
* Gets a list of all the available repositories on the Sesame installation
*
* @return phpSesame_SparqlRes
*/
public function listRepositories()
{
$request =& new HTTP_Request2($this->dsn . '/repositories', HTTP_Request2::METHOD_GET);
$request->setHeader('Accept: ' . self::SPARQL_XML);
$response = $request->send();
if($response->getStatus() != 200)
{
throw new Exception ('Phesame engine response error');
}
return new phpSesame_SparqlRes($response->getBody());
}
private function checkRepository()
{
if (empty($this->repository) || $this->repository == '')
{
throw new Exception ('No repository has been selected.');
}
}
private function checkQueryLang($queryLang)
{
if ($queryLang != 'sparql' && $queryLang != 'serql')
{
throw new Exception ('Please supply a valid query language, SPARQL or SeRQL supported.');
}
}
/**
* @todo Add in the other potentially supported formats once handlers have been written.
*
* @param string $format
*/
private function checkResultFormat($format)
{
if ($format != self::SPARQL_XML)
{
throw new Exception ('Please supply a valid result format.');
}
}
/**
*
*
* @param string &$context
*/
private function checkContext(&$context)
{
if($context != 'null')
{
$context = (substr($context, 0, 1) != '<' || substr($context, strlen($context) - 1, 1) != '>') ? "<$context>" : $context;
$context = urlencode($context);
}
}
private function checkInputFormat($format)
{
if ($format != self::RDFXML && $format != self::N3 && $format != self::NTRIPLES
&& $format != self::TRIG && $format != self::TRIX && $format != self::TURTLE)
{
throw new Exception ('Please supply a valid input format.');
}
}
/**
* Performs a simple Query.
*
* Performs a query and returns the result in the selected format. Throws an
* exception if the query returns an error.
*
* @param string $query String used for query
* @param string $resultFormat Returned result format, see const definitions for supported list.
* @param string $queryLang Language used for querying, SPARQL and SeRQL supported
* @param bool $infer Use inference in the query
*
* @return phpSesame_SparqlRes
*/
public function query($query, $resultFormat = self::SPARQL_XML, $queryLang = 'sparql', $infer = true)
{
$this->checkRepository();
$this->checkQueryLang($queryLang);
$this->checkResultFormat($resultFormat);
$request =& new HTTP_Request2($this->dsn . '/repositories/' . $this->repository, HTTP_Request2::METHOD_POST);
$request->setHeader('Accept: ' . self::SPARQL_XML);
$request->addPostParameter('query', $query);
$request->addPostParameter('queryLn', $queryLang);
$request->addPostParameter('infer', $infer);
$response = $request->send();
if($response->getStatus() != 200)
{
throw new Exception ('Failed to run query, HTTP response error: ' . $response->getStatus());
}
return new phpSesame_SparqlRes($response->getBody());
}
/**
* Appends data to the selected repository
*
*
*
* @param string $data Data in the supplied format
* @param string $context The context the query should be run against
* @param string $inputFormat See class const definitions for supported formats.
*/
public function append($data, $context = 'null', $inputFormat = self::RDFXML)
{
$this->checkRepository();
$this->checkContext($context);
$this->checkInputFormat($inputFormat);
$request =& new HTTP_Request2($this->dsn . '/repositories/' . $this->repository . '/statements?context=' . $context, HTTP_Request2::METHOD_POST);
$request->setHeader('Content-type: ' . $inputFormat);
$request->setBody($data);
$response = $request->send();
if($response->getStatus() != 204)
{
throw new Exception ('Failed to append data to the repository, HTTP response error: ' . $response->getStatus());
}
}
/**
* Appends data to the selected repository
*
*
*
* @param string $filePath The filepath of data, can be a URL
* @param string $context The context the query should be run against
* @param string $inputFormat See class const definitions for supported formats.
*/
public function appendFile($filePath, $context = 'null', $inputFormat = self::RDFXML)
{
$data = $this->getFile($filePath);
$this->append($data, $context, $inputFormat);
}
/**
* Overwrites data in the selected repository, can optionally take a context parameter
*
* @param string $data Data in the supplied format
* @param string $context The context the query should be run against
* @param string $inputFormat See class const definitions for supported formats.
*/
public function overwrite($data, $context = 'null', $inputFormat = self::RDFXML)
{
$this->checkRepository();
$this->checkContext($context);
$this->checkInputFormat($inputFormat);
$request =& new HTTP_Request2($this->dsn . '/repositories/' . $this->repository . '/statements?context=' . $context, HTTP_Request2::METHOD_PUT);
$request->setHeader('Content-type: ' . $inputFormat);
$request->setBody($data);
$response = $request->send();
if($response->getStatus() != 204)
{
throw new Exception ('Failed to append data to the repository, HTTP response error: ' . $response->getStatus());
}
}
/**
* Overwrites data in the selected repository, can optionally take a context parameter
*
* @param string $filePath The filepath of data, can be a URL
* @param string $context The context the query should be run against
* @param string $inputFormat See class const definitions for supported formats.
*/
public function overwriteFile($filePath, $context = 'null', $inputFormat = self::RDFXML)
{
$data = $this->getFile($filePath);
$this->overwrite($data, $context, $inputFormat);
}
/**
* @param string $filePath The filepath of data, can be a URL
* @return string
*/
private function getFile($filePath)
{
if(empty($filePath) || $filePath == '')
{
throw new Exception('Please supply a filepath.');
}
return file_get_contents($filePath);
}
/**
* Gets the namespace URL for the supplied prefix
*
* @param string $prefix Data in the supplied format
*
* @return string The URL of the namespace
*/
public function getNS($prefix)
{
$this->checkRepository();
if(empty($prefix))
{
throw new Exception('Please supply a prefix.');
}
$request =& new HTTP_Request2($this->dsn . '/repositories/' . $this->repository . '/namespaces/' . $prefix, HTTP_Request2::METHOD_GET);
$request->setHeader('Accept: text/plain');
$response = $request->send();
if($response->getStatus() != 200)
{
throw new Exception ('Failed to run query, HTTP response error: ' . $response->getStatus());
}
return (string) $response->getBody();
}
/**
* Sets the the namespace for the specified prefix
*
* @param string $prefix Data in the supplied format
* @param string $namespace The context the query should be run against
*/
public function setNS($prefix, $namespace)
{
$this->checkRepository();
if(empty($prefix) || empty($namespace))
{
throw new Exception('Please supply both a prefix and a namesapce.');
}
$request =& new HTTP_Request2($this->dsn . '/repositories/' . $this->repository . '/namespaces/' . $prefix, HTTP_Request2::METHOD_PUT);
$request->setHeader('Content-type: text/plain');
$request->setBody($namespace);
$response = $request->send();
if($response->getStatus() != 204)
{
throw new Exception ('Failed to set the namespace, HTTP response error: ' . $response->getStatus());
}
}
/**
* Deletes the the namespace for the specified prefix
*
* @param string $prefix Data in the supplied format
*/
public function deleteNS($prefix)
{
$this->checkRepository();
if(empty($prefix))
{
throw new Exception('Please supply a prefix.');
}
$request =& new HTTP_Request2($this->dsn . '/repositories/' . $this->repository . '/namespaces/' . $prefix, HTTP_Request2::METHOD_DELETE);
$response = $request->send();
if($response->getStatus() != 204)
{
throw new Exception ('Failed to delete the namespace, HTTP response error: ' . $response->getStatus());
}
}
/**
* Returns a list of all the contexts in the repository.
*
* @param string $resultFormat Returned result format, see const definitions for supported list.
*
* @return phpSesame_SparqlRes
*/
public function contexts($resultFormat = self::SPARQL_XML)
{
$this->checkRepository();
$this->checkResultFormat($resultFormat);
$request =& new HTTP_Request2($this->dsn . '/repositories/' . $this->repository . '/contexts', HTTP_Request2::METHOD_POST);
$request->setHeader('Accept: ' . self::SPARQL_XML);
$response = $request->send();
if($response->getStatus() != 200)
{
throw new Exception ('Failed to run query, HTTP response error: ' . $response->getStatus());
}
return new phpSesame_SparqlRes($response->getBody());
}
/**
* Returns the size of the repository
*
* @param string $context The context the query should be run against
*
* @return int
*/
public function size($context = 'null')
{
$this->checkRepository();
$request =& new HTTP_Request2($this->dsn . '/repositories/' . $this->repository . '/size?context=' . $context, HTTP_Request2::METHOD_POST);
$request->setHeader('Accept: text/plain');
$response = $request->send();
if($response->getStatus() != 200)
{
throw new Exception ('Failed to run query, HTTP response error: ' . $response->getStatus());
}
return (int) $response->getBody();
}
/**
* Clears the repository
*
* Removes all data from the selected repository from ALL contexts.
*
* @return void
*/
public function clear()
{
$this->checkRepository();
$request =& new HTTP_Request2($this->dsn . '/repositories/' . $this->repository . '/statements', HTTP_Request2::METHOD_DELETE);
$response = $request->send();
if($response->getStatus() != 204)
{
throw new Exception ('Failed to clear repository, HTTP response error: ' . $response->getStatus());
}
}
}
?>