-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathphpspecgen.php
272 lines (243 loc) · 10.4 KB
/
phpspecgen.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
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '../easyrdf/lib/');
require_once "EasyRdf.php";
class Phpspecgen_Term extends EasyRdf_Resource
{
public function termLink() {
$name = htmlspecialchars($this->localName());
return "<a href=\"#term-$name\">$name</a>";
}
public function propertyList($property) {
$items = array();
foreach ($this->all($property) as $value) {
if ($value instanceof Phpspecgen_Term) {
array_push($items, $value->termLink());
} else if ($value instanceof EasyRdf_Resource) {
array_push($items, $value->htmlLink($value->shorten()));
} else {
array_push($items, strval($value));
}
}
return $items;
}
public function propertyRow($title, $property) {
$items = $this->propertyList($property);
if (count($items) > 0) {
return "<tr><th>$title:</th> <td>".implode(', ', $items)."</td></tr>\n";
} else {
return '';
}
}
}
class Phpspecgen_Class extends Phpspecgen_Term
{
public function termType() {
return 'Class';
}
public function inheritedProperties() {
$properties = array();
foreach ($this->allParents() as $parent) {
foreach($parent->all('^rdfs:domain') as $property) {
if ($property instanceof Phpspecgen_Term) {
array_push($properties, $property->termLink());
}
}
}
return $properties;
}
protected function allParents($depth=0) {
$parents = array();
foreach ($this->all('rdfs:subClassOf') as $parent) {
if (!$parent instanceof Phpspecgen_Class)
continue;
array_push($parents, $parent);
if ($depth < 10)
$parents = array_merge($parents, $parent->allParents($depth));
}
return $parents;
}
}
class Phpspecgen_Property extends Phpspecgen_Term
{
public function termType() {
return 'Property';
}
}
class Phpspecgen_Vocab extends EasyRdf_Resource
{
protected function propertyDefinition($title, $property) {
$values = $this->all($property);
if (count($values) < 1)
return '';
$html = array();
foreach($values as $value) {
if ($value instanceof EasyRdf_Literal) {
array_push($html, htmlspecialchars($value));
} else if ($value->get('foaf:homepage')) {
array_push($html, $value->get('foaf:homepage')->htmlLink( $value->label() ));
} else {
if ($value->isBnode()) {
array_push($html, htmlspecialchars($value->label()) );
} else {
array_push($html, $value->htmlLink($value->label()) );
}
}
}
return "<dt>$title</dt><dd>".implode(', ', array_unique($html))."</dd>\n";
}
public function htmlHeader() {
$html = "<h1>".htmlspecialchars($this->label())."</h1>\n";
$html .= "<em>".htmlspecialchars($this->get('dc:description|dc11:description|rdfs:comment'))."</em>\n";
$html .= "<dl>\n";
$html .= "<dt>Latest Version</dt><dd>".$this->htmlLink()."</dd>\n";
$html .= $this->propertyDefinition('Created', 'dc:created|dc11:created');
$html .= $this->propertyDefinition('Date', 'dc:date|dc11:date');
$html .= $this->propertyDefinition('Revision', 'owl:versionInfo');
$html .= $this->propertyDefinition('Authors', 'foaf:maker|dc:creator|dc11:creator');
$html .= $this->propertyDefinition('Contributors', 'dc:contributor|dc11:contributor');
$html .= "</dl>\n";
return $html;
}
public function htmlSummaryOfTerms() {
$html = "<h2 id=\"sec-summary\">Summary of Terms</h2>\n";
$classCount = 0;
$properyCount = 0;
foreach($this->all("^rdfs:isDefinedBy") as $term) {
if ($term instanceof Phpspecgen_Class)
$classCount++;
if ($term instanceof Phpspecgen_Property)
$properyCount++;
}
$html .= "<p>This vocabulary defines";
if ($classCount == 0) {
$html .= " no classes";
} else if ($classCount == 1) {
$html .= " one class";
} else {
$html .= " $classCount classes";
}
if ($properyCount == 0) {
$html .= " and no properties.";
} else if ($properyCount == 1) {
$html .= " and one property.";
} else {
$html .= " and $properyCount properties.";
}
$html .= "</p>\n";
$html .= "<table>\n";
$html .= "<tr><th>Term Name</th><th>Type</th><th>Definition</th></tr>\n";
foreach($this->all("^rdfs:isDefinedBy") as $term) {
if ($term instanceof Phpspecgen_Term) {
$html .= "<tr>";
$html .= "<td>".$term->termLink()."</td>";
$html .= "<td>".$term->termType()."</td>";
$html .= "<td>".$term->getLiteral('rdfs:comment|rdfs:label')."</td>";
$html .= "</tr>\n";
}
}
$html .= "</table>\n";
return $html;
}
public function htmlTerms($type, $title) {
$html = '';
$id = strtolower(str_replace(' ','-',$title));
$html .= "<h2 id=\"sec-$id\">$title</h2>\n";
foreach($this->all("^rdfs:isDefinedBy") as $term) {
if (!$term instanceof $type)
continue;
$name = htmlspecialchars($term->localName());
$html .= "<h3 id=\"term-$name\">$name</h3\n";
$html .= "<p>".htmlspecialchars($term->get('rdfs:comment'))."</p>\n";
$html .= "<table>\n";
$html .= " <tr><th>URI:</th> <td>".$term->htmlLink()."</td></tr>\n";
$html .= $term->propertyRow("Label", "rdfs:label");
$html .= $term->propertyRow("Status", "vs:term_status");
$html .= $term->propertyRow("Subclasses", "^rdfs:subClassOf");
$html .= $term->propertyRow("Parent Class", "rdfs:subClassOf");
$html .= $term->propertyRow("Properties", "^rdfs:domain");
if ($term instanceof Phpspecgen_Class) {
$properties = $term->inheritedProperties();
if (count($properties) > 0)
$html .= " <tr><th>Inherited Properties:</th> ".
"<td>".join(', ', $properties)."</td></tr>\n";
}
$html .= $term->propertyRow("Range", "rdfs:range");
$html .= $term->propertyRow("Domain", "rdfs:domain");
$html .= $term->propertyRow("See Also", "rdfs:seeAlso");
$html .= "</table>\n";
$html .= "</div>\n";
}
return $html;
}
}
# Extra namespaces we use
EasyRdf_Namespace::set('vann', 'http://purl.org/vocab/vann/');
EasyRdf_Namespace::set('vs', 'http://www.w3.org/2003/06/sw-vocab-status/ns#');
## Add mappings
EasyRdf_TypeMapper::set('owl:Ontology', 'Phpspecgen_Vocab');
EasyRdf_TypeMapper::set('owl:Class', 'Phpspecgen_Class');
EasyRdf_TypeMapper::set('rdfs:Class', 'Phpspecgen_Class');
EasyRdf_TypeMapper::set('owl:Property', 'Phpspecgen_Property');
EasyRdf_TypeMapper::set('owl:DatatypeProperty', 'Phpspecgen_Property');
EasyRdf_TypeMapper::set('owl:ObjectProperty', 'Phpspecgen_Property');
EasyRdf_TypeMapper::set('owl:InverseFunctionalProperty', 'Phpspecgen_Property');
EasyRdf_TypeMapper::set('owl:FunctionalProperty', 'Phpspecgen_Property');
EasyRdf_TypeMapper::set('rdf:Property', 'Phpspecgen_Property');
?>
<html>
<head>
<title>phpspecgen</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<?php
$options = getopt('u:');
if (!empty($_REQUEST['uri'])) {
$uri = $_REQUEST['uri'];
} elseif(array_key_exists('u', $options)) {
$uri = $options['u'];
}
if (!empty($uri)) {
// Parse the document
$graph = new EasyRdf_Graph($uri);
$graph->load($uri);
// Get the first ontology in the document
$vocab = $graph->get('owl:Ontology', '^rdf:type');
if (!isset($vocab)) {
print "<p>Error: No OWL ontologies defined at that URL.</p>\n";
} else {
// FIXME: register the preferredNamespacePrefix
print $vocab->htmlHeader();
print $vocab->htmlSummaryOfTerms();
print $vocab->htmlTerms('Phpspecgen_Class', 'Classes');
print $vocab->htmlTerms('Phpspecgen_Property', 'Properties');
print $graph->dump();
}
} else {
$examples = array(
'FOAF' => 'http://xmlns.com/foaf/spec/',
'DOAP' => 'http://usefulinc.com/ns/doap#',
'LODE' => 'http://linkedevents.org/ontology/',
'Ordered List Ontology' => 'http://purl.org/ontology/olo/core#',
'Whisky Vocabulary' => 'http://vocab.org/whisky/terms.rdf',
'Sport Ontology' => 'http://www.bbc.co.uk/ontologies/sport/2011-02-17.rdf',
'Music Ontology' => 'http://purl.org/ontology/mo/',
'Programme Ontology' => 'http://www.bbc.co.uk/ontologies/programmes/2009-09-07.rdf'
);
print "<h1>phpspecgen</h1>\n";
print "<form method='get' action='?'><div>";
print "<div><label for='uri'>URI of a vocabulary (OWL or RDFS):</label>\n";
print "<input type='text' id='uri' name='uri' size='40' />\n";
print "<input type='submit' value='Submit' />\n";
print "</div></form>\n";
print "<p>Or pick an example:</p>\n";
print "<ul>\n";
foreach ($examples as $name => $uri) {
print "<li><a href='".htmlspecialchars('?uri='.urlencode($uri))."'>".
htmlspecialchars($name)."</a></li>\n";
}
print "</ul>\n";
}
?>
</body>
</html>