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
+
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