/
lh-rdf.php
514 lines (297 loc) · 11.4 KB
/
lh-rdf.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<?php
/*
Plugin Name: LH RDF
Plugin URI: http://lhero.org/plugins/lh-rdf/
Description: Adds a semantic/SIOC RDF feed to Wordpress
Author: Peter Shaw
Version: 1.2
Author URI: http://shawfactor.com/
== Changelog ==
= 1.0 =
*Complete code overhaul
= 1.1 =
*Bug fix after testing
= 1.2 =
*Fixed feed
License:
Released under the GPL license
http://www.gnu.org/copyleft/gpl.html
Copyright 2011 Peter Shaw (email : pete@localhero.biz)
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class LH_rdf_plugin {
var $format_mapper = array (
//"jsonld" => "text/turtle",
"json" => "application/json",
"n3" => "text/n3",
"ntriples" => "application/n-triples",
"php" => "application/x-httpd-php-source",
"rdfxml" => "application/rdf+xml",
"turtle" => "text/turtle",
//"dot" => "text/vnd.graphviz",
//"gif" => "image/gif",
//"png" => "image/png",
//"svg" => "image/svg+xml"
);
var $standard_namespaces;
public function return_namespaces(){
$namespaces = array (
"lh" => "http://localhero.biz/namespace/lhero/",
"sioc" => "http://rdfs.org/sioc/ns#",
"dc" => "http://purl.org/dc/elements/1.1/",
"content" => "http://purl.org/rss/1.0/modules/content/",
"rss" => "http://purl.org/rss/1.0/",
"dcterms" => "http://purl.org/dc/terms/",
"admin" => "http://webns.net/mvcb/",
"skos" => "http://www.w3.org/2004/02/skos/core#",
"sioct" => "http://rdfs.org/sioc/types#",
"bio" => "http://purl.org/vocab/bio/0.1/",
"img" => "http://jibbering.com/2002/3/svg/#",
"ore" => "http://www.openarchives.org/ore/terms/",
"void" => "http://rdfs.org/ns/void#",
);
$namespaces = apply_filters( "lh_rdf_namespaces", $namespaces);
return $namespaces;
}
private function get_link($format) {
global $post;
if ( is_singular() ){
$base_mid = add_query_arg( "feed", "lhrdf", get_permalink() );
$base_mid = add_query_arg( "format", $format, $base_mid );
} elseif (is_author()){
$base_mid = add_query_arg( "feed", "lhrdf", get_author_posts_url($post->post_author) );
$base_mid = add_query_arg( "format", $format, $base_mid );
} else {
$base_mid = add_query_arg( "feed", "lhrdf", "http://$_SERVER[HTTP_HOST]/" );
$base_mid = add_query_arg( "format", $format, $base_mid );
}
return $base_mid;
}
public function map_mime_request() {
if (isset($_REQUEST['format'])) {
$format = preg_replace("/[^\w\-]+/", '', strtolower($_REQUEST['format']));
} else {
$format = 'rdfxml';
}
if ($this->format_mapper[$format]){
nocache_headers();
header('Content-Type: ' . $this->format_mapper[$format] . '; charset=' . get_option('blog_charset'), true);
return $format;
} else {
nocache_headers();
header('Content-Type: application/rdf+xml; charset=' . get_option('blog_charset'), true);
return "rdfxml";
}
}
function is_rdf_request() {
if ( $_SERVER['HTTP_ACCEPT'] == 'application/rdf+xml' ) {
return true;
}
return false;
}
function get_rdf_link() {
global $post;
if ( is_singular() ){
$base_mid = get_permalink()."?feed=lhrdf";
} elseif (is_author()){
$base_mid = get_author_posts_url($post->post_author);
$base_mid .= "?feed=lhrdf";
} else {
$base_mid = "http://$_SERVER[HTTP_HOST]";
$base_mid .= "/";
$base_mid .= "?feed=lhrdf";
}
return $base_mid;
}
public function do_feed_enhanced() {
?>
<html>
<head>
<title>InContext Example</title>
<!-- Visualizer CSS files -->
<link type="text/css" href="<?php echo plugins_url( '/context/content/visualizer.css', __FILE__ ); ?>" media="screen" rel="Stylesheet" />
<link type="text/css" href="<?php echo plugins_url( '/context/content/visualizer-skin.css', __FILE__ ); ?>" media="screen" rel="Stylesheet" />
<!-- Add transparent PNG support to IE6 -->
<!-- Visualizer IE6 CSS file -->
<!--[if IE 6]>
<script type="text/javascript" src="<?php echo plugins_url( '/context/content/iepngfix_tilebg.js', __FILE__ ); ?>"></script>
<link type="text/css" href="<?php echo plugins_url( '/context/content/visualizer-ie6.css', __FILE__ ); ?>" rel="Stylesheet" />
<![endif]-->
<!-- Visualizer release version script include file -->
<script type="text/javascript" src="<?php echo plugins_url( '/context/scripts/visualizer_compiled_min.js?foo=bar', __FILE__ ); ?>"></script>
<!-- Visualizer example code -->
<script type="text/javascript">
// initialize the visualizer, center the visualizer on an object identified by "http://pof.tnw.utwente.nl/"
var app = new VisualizerApp("visualizer_canvas", "http://royalparktouch.com",
{ // configuration options, see the configuration options documentation page for more information
debug: true,
dataUrl: "<?php echo $this->get_rdf_link(); ?>",
schemaUrl: "<?php echo plugins_url( '/context/example/example_schema.json', __FILE__ ); ?>",
schemaFormat: "application/json",
titleProperties: ["http://purl.org/dc/terms/title", "http://xmlns.com/foaf/0.1/name"],
dontShowProperties: ["http://purl.utwente.nl/ns/escape-system.owl#id", "http://purl.utwente.nl/ns/escape-system.owl#resourceUri"],
annotationTypeId: "http://purl.utwente.nl/ns/escape-annotations.owl#RelationAnnotation",
objectAnnotationTypeId: "http://purl.utwente.nl/ns/escape-annotations.owl#object",
subjectAnnotationTypeId: "http://purl.utwente.nl/ns/escape-annotations.owl#subject",
descriptionAnnotationTypeId: "http://purl.org/dc/terms/description",
imageTypeId: "http://xmlns.com/foaf/0.1/img",
useHistoryManager: true,
baseClassTypes: {
"http://purl.utwente.nl/ns/escape-pubtypes.owl#Publication": "publication",
"http://purl.org/dc/dcmitype/MovingImage": "video",
"http://purl.utwente.nl/ns/escape-events.owl#Event": "event",
"http://purl.utwente.nl/ns/escape-projects.owl#Topic": "topic"
}
});
// subscribe to event that listens for object load
app.subscribe("*.load-object", function (event) {
document.getElementById('showObjectHolder').innerHTML = event.data;
});
// subscribe to event that listens for uri clicks
app.subscribe("*.uri-click", function (event) {
document.getElementById('uriClickDiv').innerHTML = "uri-click - " + event.data;
});
// subscribe to event that listens for object uri clicks
app.subscribe("*.object-uri-click", function (event) {
document.getElementById('uriClickDiv').innerHTML = "object-uri-click - Id: " + event.data.id + " - ExternalId: " + event.data.externalId;
});
</script>
</head>
<body>
<!-- Example button to demonstrate events coming from outside of the visualizer -->
<button onclick="app.loadObject('http://dx.doi.org/10.1126/science.289.5487.2114')">External Call Load Demo</button>
<!-- Example place holders to display information coming from visualizer events -->
Current object: <span id="showObjectHolder"></span>
<div id="uriClickDiv"></div>
<hr />
<!-- The location where the visualizer will be drawn -->
<div id="visualizer_canvas"></div>
</body>
</html>
<?php
}
public function do_feed_rdf() {
$format = $this->map_mime_request();
include('library/EasyRdf.php');
include('library/object-handlers.php');
include('library/php-json-ld.php');
$doc[] = (object)array(
"@id" => "http://dbpedia.org/resource/John_Lennon",
"http://schema.org/name" => "Manu Sporny",
"http://schema.org/url" => (object)array("@id" => "http://manu.sporny.org/"),
"http://schema.org/image" => (object)array("@id" => "http://manu.sporny.org/images/manu.png")
);
$doc[] = (object)array(
"@id" => "http://dbpedia.org/resource/John_walton",
"http://schema.org/name" => "Manu walton",
"http://schema.org/url" => (object)array("@id" => "http://manu.sporny.org/"),
"http://schema.org/image" => (object)array("@id" => "http://manu.sporny.org/images/manu.png")
);
$context = (object)array(
"name" => "http://schema.org/name",
"homepage" => (object)array("@id" => "http://schema.org/url", "@type" => "@id"),
"image" => (object)array("@id" => "http://schema.org/image", "@type" => "@id")
);
// compact a document according to a particular context
// see: http://json-ld.org/spec/latest/json-ld/#compacted-document-form
$compacted = jsonld_compact($doc, $context);
//echo json_encode($compacted, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
foreach ($this->return_namespaces() as $key => $value){
EasyRdf_Namespace::set($key, $value);
}
EasyRdf_Namespace::delete("rss");
$graph = new EasyRdf_Graph();
$lh_rdf_object_handlers = new LH_rdf_object_handlers($format);
if ($theobject = get_queried_object()){
$thetype = get_class($theobject);
$action = "do_content_".$thetype;
$graph = $lh_rdf_object_handlers->$action($graph,$theobject);
} elseif ( is_attachment() ) {
global $wp_query;
if ($wp_query->query[attachment_id]){
$args=array( 'post__in' => array($wp_query->query[attachment_id]) , 'post_type' => 'attachment' );
} else {
$args=array(
'name' => $wp_query->query[attachment],
'post_type' => 'attachment',
'posts_per_page' => 1
);
}
$attachment_post = get_posts($args);
$graph = $lh_rdf_object_handlers->do_content_attachment($graph,$attachment_post[0]);
} else {
global $wp_query;
$graph = $lh_rdf_object_handlers->do_content_wp_query($graph,$wp_query);
}
$graph = apply_filters( "lh_rdf_graph", $graph);
$serialize = $graph->serialise($format);
$etag = md5($serialize);
header("Etag: ".$etag);
echo $serialize;
}
function add_link_to_head() {
echo "\n\n<!-- Start LH RDF -->\n";
foreach ($this->format_mapper as $key => $value){
echo "<link rel=\"meta\" type=\"".$value."\" title=\"SIOC\" href=\"".$this->get_link($key)."\" />\n";
}
echo "<!-- End LH RDF -->\n\n";
}
private function check_if_rdf_request() {
foreach ($this->format_mapper as $key => $value){
if ( $_SERVER['HTTP_ACCEPT'] == $value ) {
$return = $key;
}
}
if (!$return){
return false;
} else {
return $return;
}
}
function get_control() {
if (!is_feed()){
if ( $format = $this->check_if_rdf_request() ) {
$redir = $this->get_link($format);
if ( !empty($redir) ) {
@header( "Location: " . $redir );
die();
}
}
}
}
public static function query_var($vars) {
$vars[] = '__datadump';
return $vars;
}
public static function parse_query($wp) {
if (!array_key_exists('__datadump', $wp->query_vars)) {
return;
}
//$format = $this->map_mime_request();
include('library/EasyRdf.php');
include('library/object-handlers.php');
foreach ($this->standard_namespaces as $key => $value){
EasyRdf_Namespace::set($key, $value);
}
EasyRdf_Namespace::delete("rss");
$graph = new EasyRdf_Graph();
//$lh_rdf_object_handlers = new LH_rdf_object_handlers($format);
die;
}
public function init() {
add_feed('lhrdf', array($this, 'do_feed_rdf'));
add_feed('lhenhanced', array($this, 'do_feed_enhanced'));
}
public function __construct() {
add_action('init', array($this, 'init'));
add_action('template_redirect', array($this, 'get_control'));
add_action('wp_head', array($this, 'add_link_to_head'));
add_filter('query_vars', array($this, 'query_var'));
add_action('parse_query', array($this, 'parse_query'));
}
}
$lh_rdf = new LH_rdf_plugin();
include_once('library/relationships.php');
?>