Skip to content

Commit ca42e75

Browse files
committed
First commit
0 parents  commit ca42e75

File tree

6 files changed

+886
-0
lines changed

6 files changed

+886
-0
lines changed

README

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Sponge to RDF
2+
3+
4+
A simple framework that allows you to get data from one place (soak it up bit by bit), and then squeeze it out as RDF. You'll have some work to do, mapping your data to an RDF model, and setting up this code on a server.
5+
6+
Architecture:
7+
Apache2 with mod_rewrite and PHP5
8+
9+
All requests to the server get directed to index.php
10+
Sponge2RDF then does all the parsing of the URI and content negotiation to return some data from the key in the URI.
11+
12+
Assumptions:
13+
'cool URIs' like this are supported...
14+
http://.../id/container1/key.extension
15+
Which will be redirected to...
16+
http://.../doc/container1/key.extension
17+
You can use up to four contiainers...
18+
http://.../id/container1/container2/container3/container4/key.extension
19+
And either use content negotiation or the file extension to specif the response format.
20+
21+
Installation:
22+
* Place the files in a directory on your webserver.
23+
* Make sure the .htaccess file is present and correct.
24+
* Make sure that AllowOverrides All is present in your Apache vhost config file.
25+
* Read through index.php and follow the code to see what's happenning.
26+
* Now write your own version of index.php
27+
28+
More details perhaps later :)

index.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
/*
4+
* Sponge 2 RDF sucks data from wherever you have it and maps it to a predefined model.
5+
* You can use URIs for both 'things' and 'documents' about a thing.
6+
*
7+
* You may want to use the supplied .htaccess file to redirect all requests
8+
* to the index.php file:
9+
*
10+
11+
*
12+
*/
13+
14+
// path to moriarty and arc libraries on your server
15+
define('MORIARTY_DIR', '/var/www/lib/moriarty/');
16+
define('MORIARTY_ARC_DIR', '/var/www/lib/ARC/');
17+
18+
require_once 'sponge2rdf.class.php';
19+
require_once 'sponge_builder.interface.php';
20+
21+
/*
22+
* set to true to see HTML containing the response. this is not clever, but useful :)
23+
*/
24+
if (!defined('SPONGE_DEBUG')) {
25+
define('SPONGE_DEBUG', false);
26+
}
27+
/*
28+
* Set to false if you want to run the code without sending an http response.
29+
* Useful if yyou are just using this to generate data and post to a store.
30+
*/
31+
if (!defined('SPONGE_SEND_RESPONSE')) {
32+
define('SPONGE_SEND_RESPONSE', true);
33+
}
34+
/*
35+
* If true, will check your store to see if the data already exists there.
36+
* If it does a Symetric Labelled Bound Description will be returned
37+
*/
38+
if (!defined('SPONGE_STORE_CHECK')) {
39+
define('SPONGE_STORE_CHECK', false);
40+
}
41+
/*
42+
* If set to true will post the generated data back to the store.
43+
* Useful if you want to harvet data over time.
44+
*/
45+
if (!defined('SPONGE_STORE_POST')) {
46+
define('SPONGE_STORE_POST', false);
47+
}
48+
49+
50+
/**
51+
* This class is used to implement get_data() and get_rdf()
52+
* Can also set the Store details here too.
53+
*/
54+
class MySpecialSponge extends Sponge2Rdf implements SpongeBuilder {
55+
56+
/**
57+
*
58+
* @var string $base_uri of all URIs generated on the fly
59+
*/
60+
public $base_uri = "http://localhost";
61+
/**
62+
* Talis Platform Store details
63+
*/
64+
public $store_uri = "http://api.talis.com/stores/my-shiny-store";
65+
public $store_user = '';
66+
public $store_pass = '';
67+
68+
69+
public function get_data() {
70+
/**
71+
* Implement code here to retrieve some data from where ever it is,
72+
* using the key from the URI. The URI has already been parsed by
73+
* the inherited __construct of class Sponge2Rdf.
74+
*/
75+
76+
$mykey = $this->uri->get_key();
77+
78+
// in this example we have hardcoded data
79+
$data = array(
80+
'name' => 'Tim',
81+
'surname' => 'Hodson',
82+
'nickname' => 'Tim',
83+
'email' => '[email protected]',
84+
'homepage' => 'http://timhodson.com',
85+
'special_thing' => "Grandad's gold watch"
86+
);
87+
return $data;
88+
}
89+
90+
public function get_rdf() {
91+
/*
92+
* Call the data function we just defined to get our data.
93+
*/
94+
$data = $this->get_data();
95+
96+
// you can include debugging stuff (just output to screen, nothing fancy)
97+
$this->debug_print($this->uri->get_key(), "key");
98+
99+
// Create a new graph to hold our RDF.
100+
// The variable $this->graph will hold our SimpleGraph object (see Moriarty docs)
101+
$this->new_graph();
102+
103+
// we define some useful prefix constants where they haven't already been defined in Moriarty's constants.inc.php
104+
// if you are using your own name spaces, you can set these up now too.
105+
define('FOAF_PREFIX', "http://xmlns.com/foaf/0.1/");
106+
107+
$this->ontology_prefix = 'http://example.com/special-things#';
108+
$this->graph->set_namespace_mapping("myontology", $this->ontology_prefix);
109+
110+
/*
111+
* This example is using a URI like:
112+
* /id/person/{identifier}
113+
* Where the first container is 'person'
114+
* Up to four containers can be configured and the rest of your code should use
115+
* the SpongeUri object's is_first_container(), is_second_container() etc functions
116+
* to test what combination of containers are present in order to determine what to send back.
117+
*/
118+
if ($this->uri->is_first_container('person')) {
119+
$this->debug_print('', "we have a person container");
120+
121+
122+
// add a triple for a resource
123+
$this->graph->add_resource_triple($this->uri->get_thing_uri(1), RDF_TYPE, "foaf:Person");
124+
125+
// add a triple for a literal
126+
$this->graph->add_literal_triple($this->uri->get_thing_uri(1), FOAF_PREFIX."family_name", $data['surname']);
127+
128+
// and so on..
129+
$this->graph->add_literal_triple($this->uri->get_thing_uri(1), FOAF_PREFIX."givenname", $data['name']);
130+
$this->graph->add_literal_triple($this->uri->get_thing_uri(1), RDFS_LABEL, $data['name']." ".$data['surname']);
131+
$this->graph->add_literal_triple($this->uri->get_thing_uri(1), FOAF_NAME, $data['name']." ".$data['surname']);
132+
$this->graph->add_literal_triple($this->uri->get_thing_uri(1), FOAF_NICK, $data['nickname']);
133+
$this->graph->add_resource_triple($this->uri->get_thing_uri(1), FOAF_PREFIX."homepage", $data['homepage']);
134+
$this->graph->add_literal_triple($this->uri->get_thing_uri(1), FOAF_PREFIX."mbox", $data['email']);
135+
$this->graph->add_literal_triple($this->uri->get_thing_uri(1), FOAF_PREFIX."mbox_sha1sum", sha1($data['email']));
136+
$this->graph->add_literal_triple($this->uri->get_thing_uri(1), "myontology:specialThing", $data['special_thing']);
137+
}
138+
139+
/**
140+
* Calling the parent class' version of this function will serialise
141+
* the RDF and optionally add any extra meta rdf, (@todo though this
142+
* hasn't been implemented.)
143+
*
144+
*/
145+
return parent::get_rdf();
146+
}
147+
}
148+
/**
149+
* This is where the magic happens!
150+
*
151+
* Finally create a new instance of our class.
152+
*
153+
* When this page gets hit with a request from the client, everything
154+
* will be set in motion to work out what they want.
155+
*/
156+
$mySponge = new MySpecialSponge();
157+
158+
159+
?>

0 commit comments

Comments
 (0)