/
metadata.php
85 lines (70 loc) · 2.22 KB
/
metadata.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
<?php
namespace Trilby;
define('void_ns', 'http://rdfs.org/ns/void#');
define('rdf_ns', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
define('dcterms_ns', 'http://purl.org/dc/terms/');
class Graph {
var $_index = array();
var $uri=null;
function __construct($uri, &$index){
$this->_index =& $index;
$this->uri = $uri;
}
function setLiteral($prop, $val){
$this->_index[$this->uri][$prop][]=array(
'value' => $val,
'type' => 'literal'
);
return $this;
}
function setResource($prop, $val){
$this->_index[$this->uri][$prop][]=array(
'value' => $val,
'type' => 'uri'
);
$graph = new Graph($val, $this->_index);
return $graph;
}
function getIndex(){
return $this->_index;
}
}
function addMetadata($data, &$Config, $types, $facets, $namespaces){
$datasetUri = 'http://' .$_SERVER['HTTP_HOST']. dirname($_SERVER['SCRIPT_NAME']).'/';
$documentUri = 'http://' .$_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI'];
$documentUri = array_shift(explode('?', $documentUri));
if($query = getQuery()){
$documentUri.='?'.$query;
}
if(isset($data[$documentUri])){
$documentUri.='&_output=turtle';
}
$DocumentGraph = new Graph($documentUri, $data);
$count = 1;
foreach($data as $uri => $props){
$prop = rdf_ns.'_'.$count++;
$DocumentGraph->setResource($prop,$uri);
}
if($documentUri!=$datasetUri){
$DocumentGraph->setResource(void_ns.'inDataset', $datasetUri);
} else {
$DocumentGraph->setResource(rdf_ns.'type', void_ns.'Dataset');
foreach($types as $type => $entities){
$classPartition = $DocumentGraph->setResource(void_ns.'classPartition', $datasetUri.'?rdf:type='.curie($type));
$classPartition->setResource(void_ns.'class', $type);
$classPartition->setLiteral(void_ns.'entities', $entities);
}
foreach($namespaces as $ns => $n){
$vocabUri = preg_replace('@#$@', '',$ns);
$DocumentGraph->setResource(void_ns.'vocabulary', $vocabUri);
}
}
if(!empty($Config->license)){
$DocumentGraph->setResource(dcterms_ns.'license', $Config->license);
}
if(!empty($Config->name)){
$DocumentGraph->setLiteral(dcterms_ns.'title', $Config->name);
}
return $DocumentGraph->getIndex();
}
?>