-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsponge2rdf.class.php
256 lines (221 loc) · 8.21 KB
/
sponge2rdf.class.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
<?php
/**
* Script to get data from somewhere, soak it up and create some RDF
* supported path elements should be setup in advance
* http://...com/id/colour/00ff00
* http://...com/id/compound/identifer
* It is assumed the path begins with either doc or id
* for information and non-information resources respectively
*
* @author Tim Hodson [email protected]
*/
// use Moriarty code to do stuff
require_once MORIARTY_DIR . 'moriarty.inc.php';
require_once MORIARTY_DIR . 'store.class.php';
require_once MORIARTY_DIR . 'credentials.class.php';
require_once 'sponge_response.class.php';
require_once 'sponge_uri.class.php';
require_once 'sponge_builder.interface.php';
/*
* Pseudo Code
*
* recieve request
* match path e.g. /colour/{hex}
* if not available in a Talis Platform Store
* generate data
* serialize to requested format e.g. turtle
* post to Talis Platform Store
* else
* return from Talis Platform Store
*
*/
class Sponge2Rdf implements SpongeBuilder {
public $uri;
public $graph;
public function __construct() {
if (SPONGE_SEND_RESPONSE)
$this->parse_path();
if (SPONGE_SEND_RESPONSE)
$this->build_response();
//return $this;
}
/**
*
* @param string $uri The URI to parse if not set defaults to $_SERVER['REQUEST_URI']
*/
public function parse_path($uri='') {
if ($uri == '') {
$uri = $_SERVER['REQUEST_URI'];
}
$this->uri = new SpongeUri($this->base_uri, $uri);
}
/**
* This function either redirects an 'id' URI or attempts to return a 'doc' URI.
* It may check a store for the URI before attempting to generate fresh data.
* It may post data back to the store after generating
* This function calls get_rdf() which will be implemented is a user class.
*/
public function build_response() {
if ($this->uri->is_thing()) { //redirect id to doc with a 303
if (SPONGE_SEND_RESPONSE)
$res = new SpongeResponse("303", '', '', $this->uri->get_doc_uri());
}else if ($this->uri->is_doc()) { // serve some data
$this->debug_print($this->uri->get_thing_uri(1), "thing_uri");
// check if we allready have this in the store?
if (SPONGE_STORE_CHECK) {
$store = new Store($this->store_uri);
$ss = $store->get_sparql_service();
$response = $ss->describe($this->uri->get_thing_uri(), 'slcbd');
if ($response->is_success()) {
$this->debug_print($response, "Response from sparql describe for " . $this->uri->get_thing_uri());
$graph = new SimpleGraph();
$graph->from_rdfxml($response->body);
if ($graph->is_empty()) {
// generate new set of data
$this->debug_print($this->uri->get_thing_uri(), 'Store graph is empty');
$data = $this->get_rdf();
$this->debug_print($data, "The new graph");
if (SPONGE_SEND_RESPONSE) {
$res = new SpongeResponse("200", $this->uri->get_content_type(), $data);
}
//store the new data
if(SPONGE_STORE_POST) $this->store_rdf();
} else {
// do something with graph...
$this->debug_print($response, "Response from store");
if (SPONGE_SEND_RESPONSE)
$res = new SpongeResponse("200", $this->uri->get_content_type(), $this->serialiseGraph($graph));
}
} else {
// TODO else there was no useful response, so we generate some...
// $this->debug_print($response,"Sparql Error");
if (SPONGE_SEND_RESPONSE)
$res = new SpongeResponse("200", $this->uri->get_content_type(), $this->get_rdf());
}
} else {
$data = $this->get_rdf();
$this->debug_print($data, "The new graph");
if (SPONGE_SEND_RESPONSE) {
$res = new SpongeResponse("200", $this->uri->get_content_type(), $data);
}
//store the new data
if(SPONGE_STORE_POST) $this->store_rdf();
}
}
}
/**
*
* @param SimpleGraph $g a SimpleGraph object.
* @return string A serialised RDF graph
*/
private function serialiseGraph(SimpleGraph $g) {
$this->debug_print($g, "graph passed to serialiseGraph");
switch ($this->uri->get_extension()) {
case 'html':
return $g->to_html();
break;
case 'json':
return $g->to_json();
break;
case 'rdf' :
return $g->to_rdfxml();
break;
case 'ttl' :
return $g->to_turtle();
break;
case 'nt' :
return $g->to_ntriples();
break;
default:
return $g->to_html();
break;
}
}
/**
* Stores the current graph in $this->graph in a Talis Platform Store
*/
public function store_rdf() {
$this->debug_print('', "in store_data()");
$cred = new Credentials($this->store_user, $this->store_pass);
$store = new Store($this->store_uri, $cred);
$mb = $store->get_metabox();
if (!isset($this->graph)) {
$this->get_rdf();
}
$this->debug_print($this->graph, "Our graph");
$mb->submit_turtle($this->graph->to_turtle());
}
/**
* Creates a new SimpleGraph graph as $this->graph
*/
public function new_graph() {
$this->graph = new SimpleGraph();
}
/*
* public getters
*/
public function get_path() {
return $this->uri;
}
public function get_data() {
// You implement this.
}
public function get_rdf() {
/**
* @todo add extra metadata about data creation time if a file is a doc (etc)?
*/
return $this->serialiseGraph($this->graph);
}
/**
* function to make a value safe to use in a URI
*
* @param string $val
* @return string Which has been sanitised
*/
public function uri_safe($val) {
$val = $this->sanitize($val, true, true);
return $val;
}
/**
* Function: sanitize (borrowed from Chyrp)
* Returns a sanitized string, typically for URLs.
*
* Parameters:
* @param string $string The string to sanitize.
* @param bool $force_lowercase Force the string to lowercase?
* @param bool $strict If set to *true*, will remove all non-alphanumeric characters.
*/
private function sanitize($string, $force_lowercase = true, $strict = false) {
$strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
"}", "\\", "|", ";", ":", "\"", "'", "‘", "’", "“", "”", "–", "—",
"—", "–", ",", "<", ".", ">", "/", "?");
$clean = trim(str_replace($strip, "", strip_tags($string)));
$clean = preg_replace('/\s+/', "_", $clean);
$clean = ($strict) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean;
return ($force_lowercase) ?
(function_exists('mb_strtolower')) ?
mb_strtolower($clean, 'UTF-8') :
strtolower($clean) :
$clean;
}
/**
*
*
* Print debug information to browser.
*
* @access public
*
* @param mixed $var A variable to display to the screen
* @param string $title The title of the display (uses a h2 element)
* @param bool $flag Override the SPONGE_DEBUG constant
*/
public function debug_print($var, $title='', $flag = 0) {
if (SPONGE_DEBUG || $flag) {
echo "<h2>" . $title . "</h2>";
echo "<pre>";
print_r($var);
echo "</pre>";
}
}
}
?>