forked from easyrdf/easyrdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparql_queryform.php
78 lines (73 loc) · 2.39 KB
/
sparql_queryform.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
<?php
/**
* Form to submit and display SPARQL queries
*
* This example presents a form that you can enter the URI
* of a a SPARQL endpoint and a SPARQL query into. The
* results are displayed using a call to dump() on what will be
* either a EasyRdf\Sparql\Result or EasyRdf\Graph object.
*
* A list of registered namespaces is displayed above the query
* box - any of these namespaces can be used in the query and PREFIX
* statements will automatically be added to the start of the query
* string.
*
* @package EasyRdf
* @copyright Copyright (c) Nicholas J Humfrey
* @license http://unlicense.org/
*/
require_once realpath(__DIR__.'/..')."/vendor/autoload.php";
require_once __DIR__."/html_tag_helpers.php";
// Stupid PHP :(
if (get_magic_quotes_gpc() and isset($_REQUEST['query'])) {
$_REQUEST['query'] = stripslashes($_REQUEST['query']);
}
?>
<html>
<head>
<title>EasyRdf SPARQL Query Form</title>
<style type="text/css">
.error {
width: 35em;
border: 2px red solid;
padding: 1em;
margin: 0.5em;
background-color: #E6E6E6;
}
</style>
</head>
<body>
<h1>EasyRdf SPARQL Query Form</h1>
<div style="margin: 0.5em">
<?php
print form_tag();
print label_tag('endpoint');
print text_field_tag('endpoint', "http://dbpedia.org/sparql", array('size'=>80)).'<br />';
print "<code>";
foreach(\EasyRdf\RdfNamespace::namespaces() as $prefix => $uri) {
print "PREFIX $prefix: <".htmlspecialchars($uri)."><br />\n";
}
print "</code>";
print text_area_tag('query', "SELECT * WHERE {\n ?s ?p ?o\n}\nLIMIT 10", array('rows' => 10, 'cols' => 80)).'<br />';
print check_box_tag('text') . label_tag('text', 'Plain text results').'<br />';
print reset_tag() . submit_tag();
print form_end_tag();
?>
</div>
<?php
if (isset($_REQUEST['endpoint']) and isset($_REQUEST['query'])) {
$sparql = new \EasyRdf\Sparql\Client($_REQUEST['endpoint']);
try {
$results = $sparql->query($_REQUEST['query']);
if (isset($_REQUEST['text'])) {
print "<pre>".htmlspecialchars($results->dump('text'))."</pre>";
} else {
print $results->dump('html');
}
} catch (Exception $e) {
print "<div class='error'>".$e->getMessage()."</div>\n";
}
}
?>
</body>
</html>