My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
<?php
require_once MORIARTY_DIR . 'simplegraph.class.php';
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'paget_response.class.php';
class PAGET_ResourceDescription extends SimpleGraph {
var $_uri;
var $_primary_resource;
var $_is_valid;
var $_media_types = array(
'rdf' => array('type' => 'application/rdf+xml', 'label' => 'RDF/XML'),
'html' => array('type' => 'text/html', 'label' => 'HTML'),
'json' => array('type' => 'application/json', 'label' => 'JSON'),
'turtle' => array('type' => 'text/plain', 'label' => 'Turtle'),
);
function __construct($uri) {
parent::__construct();
$this->_uri = $uri;


if ( preg_match('~\.(html|rdf|xml|turtle|json)$~', $this->_uri, $m) ) {
$this->_media_type = $this->_media_types[$m[1]]['type'];
}
else {
$this->_media_type = $this->_media_types['rdf'];
}

$this->read_triples();


}

function get_media_type() {
return $this->_media_type;
}

function get_prefix_mappings() {
return array_flip($this->_ns);
}

function is_valid() {
return $this->_is_valid;
}

function read_triples() {
$resources = $this->get_resources();
$this->_primary_resource = $this->_uri;
if (count($resources) > 0) {
$this->_primary_resource = $resources[0];
}

$this->add_resource_triple( $this->_uri, RDF_TYPE, FOAF_DOCUMENT );
$this->add_resource_triple( $this->_uri, RDF_TYPE, 'http://purl.org/dc/dcmitype/Text' );
$this->add_resource_triple( $this->_uri, FOAF_PRIMARYTOPIC, $this->_primary_resource );

foreach ($this->_media_types as $extension => $type_info) {
if ( $type_info['type'] != $this->_media_type) {
$this->add_resource_triple( $this->_uri, 'http://purl.org/dc/terms/hasFormat', $this->map_uri($this->_primary_resource) . '.' . $extension );
$this->add_resource_triple( $this->map_uri($this->_primary_resource) . '.' . $extension, RDF_TYPE, 'http://purl.org/dc/dcmitype/Text' );
$this->add_resource_triple( $this->map_uri($this->_primary_resource) . '.' . $extension, RDF_TYPE, FOAF_DOCUMENT );
$this->add_literal_triple( $this->map_uri($this->_primary_resource) . '.' . $extension , 'http://purl.org/dc/elements/1.1/format', $type_info['type'] );
$this->add_literal_triple( $this->map_uri($this->_primary_resource) . '.' . $extension , RDFS_LABEL, $type_info['label'] );
}
}


$this->_is_valid = false;
foreach ($resources as $resource_uri) {
$this->add_resource_triple( $this->_uri, FOAF_TOPIC, $resource_uri );

$generators = $this->get_generators();
foreach ($generators as $generator) {
$generator->add_triples($resource_uri, $this);
}

if ( array_key_exists($resource_uri, $this->get_index())) {
$this->_is_valid = true;
}
}

$augmentors = $this->get_augmentors();
foreach ($augmentors as $augmentor) {
$augmentor->process($this);
}
}

function get_resources() {
$resources = array();
if ( preg_match('~^(.+)\.(html|rdf|xml|turtle|json)$~', $this->_uri, $m) ) {
$uri = preg_replace("~\.local/~", "/", $m[1]);
$resources[] = $uri;
}
return $resources;
}


function get_generators() {
return array( );
}

function get_augmentors() {
return array( new PAGET_SimplePropertyLabeller() );
}

function get_primary_resource_uri() {
return $this->_primary_resource;
}

function get_uri() {
return $this->_uri;
}

function get_label($resource_uri = null) {
if ($resource_uri == null) {
$resource_uri = $this->_primary_resource;
}
$label = $this->get_first_literal($resource_uri,'http://www.w3.org/2004/02/skos/core#prefLabel', '');
if ( strlen($label) == 0) {
$label = $this->get_first_literal($resource_uri,RDFS_LABEL, '');
}
if ( strlen($label) == 0) {
$label = $this->get_first_literal($resource_uri,'http://purl.org/dc/terms/title', '');
}
if ( strlen($label) == 0) {
$label = $this->get_first_literal($resource_uri,DC_TITLE, '');
}
if ( strlen($label) == 0) {
$label = $this->get_first_literal($resource_uri,FOAF_NAME, '');
}
if ( strlen($label) == 0) {
$label = $this->get_first_literal($resource_uri,RDF_VALUE, '');
}
if ( strlen($label) == 0) {
$label = $resource_uri;
}

return $label;
}

function get(&$urispace,&$request) {

$response = new PAGET_Response(200);

switch ($this->_media_type) {
case 'application/xml':
case 'application/rdf+xml':
$response->set_body($this->to_rdfxml());
break;
case 'application/json':
$response->set_body($this->to_json());
break;
case 'text/plain':
$response->set_body($this->to_turtle());
break;
case 'text/html':
$response->set_body($this->get_html($urispace, $request));
break;
default:
$response->set_body($this->to_rdfxml());
}
$response->configure($this, $request);
return $response;
}

function get_html(&$urispace, &$request) {
$tmpl = $urispace->get_template($request);
if ( null == $tmpl ) {
$tmpl = PAGET_DIR . 'templates' . DIRECTORY_SEPARATOR . 'plain.tmpl.html';
}

ob_start();
try {
include($tmpl);
$buffer = ob_get_clean();
return $buffer;
}
catch (Exception $ex) {
ob_end_clean();
throw $ex;
}
}


function get_inverse_index() {
$g = new SimpleGraph();

foreach ($this->_index as $s => $p_list) {
foreach ($p_list as $p => $v_list) {
foreach ($v_list as $v_info) {
if ( $v_info['type'] == 'uri' ) {
$g->add_resource_triple($v_info['value'], $p, $s);
}
}
}
}

return $g->get_index();

}

function consume_first_literal($s, $p, $def = '') {
return $this->get_first_literal($s, $p, $def);
}


// This function maps a URI to a local equivalent
// Override this when you want the link in your HTML output to point to somewhere other than the URI itself, e.g. a proxy
// The default implementation rewrites URIs to the domain name suffixed with .local for assisting with testing
// For example http://example.com/foo might map to http://example.com.local/foo if the application is being accessed from example.com.local
function map_uri($uri) {
if (isset($_SERVER["HTTP_HOST"])) {
if (preg_match('~http://([^/]+)/~i', $uri, $m)) {
if ( $_SERVER["HTTP_HOST"] == $m[1] . '.local' ) {
return str_replace($m[1], $_SERVER["HTTP_HOST"], $uri);
}
else {
return $uri;
}
}
}
}

}

Change log

r110 by m...@iandavis.com on Oct 5, 2009   Diff
Fixed bug where resource description was
not calling simple graph constructor
Go to: 
Project members, sign in to write a code review

Older revisions

r71 by m...@iandavis.com on Apr 21, 2009   Diff
Removed XML media type from resource
description
r69 by m...@iandavis.com on Apr 21, 2009   Diff
Updated for new dublin core URIs
r60 by m...@iandavis.com on Mar 25, 2009   Diff
na
All revisions of this file

File info

Size: 6890 bytes, 221 lines
Powered by Google Project Hosting