-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathw2s.php
98 lines (98 loc) · 2.91 KB
/
w2s.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
<?php
$text = "Nothing yet...";
$run = false;
$status = 200;
$syntax = "Wikidata";
ini_set('xdebug.max_nesting_level', 10000);
if(!empty($_REQUEST['wdq'])) {
require_once __DIR__.'/WDQ.php';
$parser = new WDQParser();
$parsed = $parser->parse($_REQUEST['wdq']);
if(!$parsed) {
$text = "Failed to parse the query";
$status = 400;
} else {
if(!empty($_REQUEST['syntax'])) {
$syntax = preg_replace("/[^a-zA-Z]/", "", $_REQUEST['syntax']);
}
$klass = "Sparql\\Syntax\\$syntax";
if(class_exists($klass) && is_a($klass, "Sparql\\Syntax", true)) {
$syntaxClass = new $klass;
$exp = $parser->generate($parsed, "?item");
$sparql = $exp->emit($syntaxClass, ' ');
$text = '';
foreach($syntaxClass->getPrefixes() as $pref => $url) {
$text .= "prefix $pref: <$url>\n";
}
$text .= "SELECT ?item WHERE {\n$sparql}";
$run = true;
} else {
$text = "Unknown syntax";
$status = 400;
}
if(empty($_POST['gui'])) {
// Labs runs 5.3, 5.3 does not have http_response_code(). Sad.
header("HTTP/1.0 $status Banana");
header("Access-Control-Allow-Origin: *");
if(!empty($_REQUEST['jsonp'])) {
$funcname = preg_replace("/[^a-zA-Z0-9_]/", "", $_REQUEST['jsonp']);
$text = "$funcname(".json_encode($text).");";
header("Content-type: application/javascript");
} else {
header("Content-type: text/plain");
}
echo $text;
exit();
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="//tools-static.wmflabs.org/cdnjs/ajax/libs/twitter-bootstrap/2.3.1/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="//tools-static.wmflabs.org/cdnjs/ajax/libs/github-fork-ribbon-css/0.2.0/gh-fork-ribbon.min.css" />
<title>WDQ2SPARQL</title>
</head>
<body style="margin: 10px">
<div style="float: right; margin-right: 100px">
Supported syntax:<br>
<ul>
<li>claim, noclaim
<li>tree/web
<li>quantity/between/string
<li>and/or
<li>Subqueries within claim/noclaim
<li>link/nolink
<li>around
<li>qualifiers
</ul>
Not supported yet:<br>
<ul>
<li>Subqueries within tree/web
</ul>
</div>
<form action="w2s.php" method="POST">
<input type="hidden" name="gui" value="1">
Please enter WDQ query:<br>
<textarea cols="80" rows="10" name="wdq" style="width: 40em">
<?= @$_POST['wdq']; ?>
</textarea><br>
<input type="submit" value="Translate"/>
<br clear="all">
</form>
<hr>
Translation to SPARQL:<br>
<pre>
<?= htmlentities($text); ?>
</pre>
<?php if($run) {
$runURLs = array('Wikidata' => 'http://query.wikidata.org/#', "WDTK" => 'http://milenio.dcc.uchile.cl/sparql?query=');
?>
<a target="_blank" href="<?=$runURLs[$syntax].rawurlencode($text); ?>">Run this query!</a>
<?php } ?>
<a class="github-fork-ribbon" href="https://github.com/smalyshev/wdq2sparql" data-ribbon="Fork me on GitHub" title="Fork me on GitHub">Fork me on GitHub</a>
</body>
</html>