1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
homepage: http://arc.semsol.org/ |
4
|
|
|
license: http://arc.semsol.org/license |
5
|
|
|
|
6
|
|
|
class: ARC2 eRDF Extractor (w/o link title generation) |
7
|
|
|
author: Benjamin Nowack |
8
|
|
|
version: 2009-02-09 (Tweak: getRootNode returns 1st node if html tag is not found) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
ARC2::inc('RDFExtractor'); |
12
|
|
|
|
13
|
|
|
class ARC2_ErdfExtractor extends ARC2_RDFExtractor { |
14
|
|
|
|
15
|
|
|
function __construct($a = '', &$caller) { |
16
|
|
|
parent::__construct($a, $caller); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
function ARC2_ErdfExtractor($a = '', &$caller) { |
20
|
|
|
$this->__construct($a, $caller); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
function __init() { |
24
|
|
|
parent::__init(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/* */ |
28
|
|
|
|
29
|
|
|
function extractRDF() { |
30
|
|
|
if (!isset($this->caller->detected_formats['erdf'])) return 0; |
31
|
|
|
$root_node = $this->getRootNode(); |
32
|
|
|
$base = $this->getDocBase(); |
33
|
|
|
$ns = $this->getNamespaces(); |
34
|
|
|
$context = array( |
35
|
|
|
'base' => $base, |
36
|
|
|
'prev_res' => $base, |
37
|
|
|
'cur_res' => $base, |
38
|
|
|
'ns' => $ns, |
39
|
|
|
'lang' => '', |
40
|
|
|
); |
41
|
|
|
$this->processNode($root_node, $context); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/* */ |
45
|
|
|
|
46
|
|
|
function getRootNode() { |
47
|
|
|
foreach ($this->nodes as $id => $node) { |
48
|
|
|
if ($node['tag'] == 'html') { |
49
|
|
|
return $node; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
return $this->nodes[0]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
function getNamespaces() { |
56
|
|
|
$r = array( |
57
|
|
|
'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', |
58
|
|
|
'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#' |
59
|
|
|
); |
60
|
|
|
foreach ($this->nodes as $id => $node) { |
61
|
|
|
if (preg_match('/^(link|a)$/', $node['tag']) && isset($node['a']['rel']) && preg_match('/schema\.([^\s]+)/is', $node['a']['rel'], $m) && isset($node['a']['href uri'])) { |
62
|
|
|
$r[$m[1]] = $node['a']['href uri']; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
return $r; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/* */ |
69
|
|
|
|
70
|
|
|
function processNode($n, $ct) { |
71
|
|
|
/* context */ |
72
|
|
|
//$ct['lang'] = $this->v('xml:lang', $ct['lang'], $n['a']); |
73
|
|
|
$ct['lang'] = ''; |
74
|
|
|
$ct['prop_uris'] = $this->getPropertyURIs($n, $ct); |
75
|
|
|
$ct['prev_res'] = $ct['cur_res']; |
76
|
|
|
$ct['cur_res'] = $this->getCurrentResourceURI($n, $ct); |
77
|
|
|
$ct['cur_obj_id'] = $this->getCurrentObjectID($n, $ct); |
78
|
|
|
$ct['cur_obj_literal'] = $this->getCurrentObjectLiteral($n, $ct); |
79
|
|
|
/* triple production (http://research.talis.com/2005/erdf/wiki/Main/SummaryOfTripleProductionRules) */ |
80
|
|
|
foreach ($ct['prop_uris'] as $type => $uris) { |
81
|
|
|
foreach ($uris as $uri) { |
82
|
|
|
$rdf_type = preg_match('/^ /', $uri) ? 1 : 0; |
83
|
|
|
/* meta + name */ |
84
|
|
|
if (($type == 'name') && ($n['tag'] == 'meta')) { |
85
|
|
|
$t = array( |
86
|
|
|
's' => $ct['cur_res'], |
87
|
|
|
's_type' => 'uri', |
88
|
|
|
'p' => $uri, |
89
|
|
|
'o' => $ct['cur_obj_literal']['value'], |
90
|
|
|
'o_type' => 'literal', |
91
|
|
|
'o_lang' => $ct['cur_obj_literal']['datatype'] ? '' : $ct['cur_obj_literal']['lang'], |
92
|
|
|
'o_datatype' => $ct['cur_obj_literal']['datatype'], |
93
|
|
|
); |
94
|
|
|
$this->addT($t); |
95
|
|
|
} |
96
|
|
|
/* class */ |
97
|
|
|
if ($type == 'class') { |
98
|
|
|
if ($rdf_type) { |
99
|
|
|
$s = $this->v('href uri', $ct['cur_res'], $n['a']); |
100
|
|
|
$s = $this->v('src uri', $s, $n['a']); |
101
|
|
|
$t = array( |
102
|
|
|
's' => $s, |
103
|
|
|
's_type' => 'uri', |
104
|
|
|
'p' => $ct['ns']['rdf'] . 'type', |
105
|
|
|
'o' => trim($uri), |
106
|
|
|
'o_type' => 'uri', |
107
|
|
|
'o_lang' => '', |
108
|
|
|
'o_datatype' => '', |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
elseif (isset($n['a']['id'])) {/* used as object */ |
112
|
|
|
$t = array( |
113
|
|
|
's' => $ct['prev_res'], |
114
|
|
|
's_type' => 'uri', |
115
|
|
|
'p' => $uri, |
116
|
|
|
'o' => $ct['cur_res'], |
117
|
|
|
'o_type' => 'uri', |
118
|
|
|
'o_lang' => '', |
119
|
|
|
'o_datatype' => '', |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
else { |
123
|
|
|
$t = array( |
124
|
|
|
's' => $ct['cur_res'], |
125
|
|
|
's_type' => 'uri', |
126
|
|
|
'p' => $uri, |
127
|
|
|
'o' => $ct['cur_obj_literal']['value'], |
128
|
|
|
'o_type' => 'literal', |
129
|
|
|
'o_lang' => $ct['cur_obj_literal']['datatype'] ? '' : $ct['cur_obj_literal']['lang'], |
130
|
|
|
'o_datatype' => $ct['cur_obj_literal']['datatype'], |
131
|
|
|
); |
132
|
|
|
if (($o = $this->v('src uri', '', $n['a'])) || ($o = $this->v('href uri', '', $n['a']))) { |
133
|
|
|
if (!$ct['prop_uris']['rel'] && !$ct['prop_uris']['rev']) { |
134
|
|
|
$t['o'] = $o; |
135
|
|
|
$t['o_type'] = 'uri'; |
136
|
|
|
$t['o_lang'] = ''; |
137
|
|
|
$t['o_datatype'] = ''; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
$this->addT($t); |
142
|
|
|
} |
143
|
|
|
/* rel */ |
144
|
|
|
if ($type == 'rel') { |
145
|
|
|
if (($o = $this->v('src uri', '', $n['a'])) || ($o = $this->v('href uri', '', $n['a']))) { |
146
|
|
|
$t = array( |
147
|
|
|
's' => $ct['cur_res'], |
148
|
|
|
's_type' => 'uri', |
149
|
|
|
'p' => $uri, |
150
|
|
|
'o' => $o, |
151
|
|
|
'o_type' => 'uri', |
152
|
|
|
'o_lang' => '', |
153
|
|
|
'o_datatype' => '', |
154
|
|
|
); |
155
|
|
|
$this->addT($t); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
/* rev */ |
159
|
|
|
if ($type == 'rev') { |
160
|
|
|
if (($s = $this->v('src uri', '', $n['a'])) || ($s = $this->v('href uri', '', $n['a']))) { |
161
|
|
|
$t = array( |
162
|
|
|
's' => $s, |
163
|
|
|
's_type' => 'uri', |
164
|
|
|
'p' => $uri, |
165
|
|
|
'o' => $ct['cur_res'], |
166
|
|
|
'o_type' => 'uri', |
167
|
|
|
'o_lang' => '', |
168
|
|
|
'o_datatype' => '', |
169
|
|
|
); |
170
|
|
|
$this->addT($t); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
/* imgs */ |
176
|
|
|
if ($n['tag'] == 'img') { |
177
|
|
|
if (($s = $this->v('src uri', '', $n['a'])) && $ct['cur_obj_literal']['value']) { |
178
|
|
|
$t = array( |
179
|
|
|
's' => $s, |
180
|
|
|
's_type' => 'uri', |
181
|
|
|
'p' => $ct['ns']['rdfs'] . 'label', |
182
|
|
|
'o' => $ct['cur_obj_literal']['value'], |
183
|
|
|
'o_type' => 'literal', |
184
|
|
|
'o_lang' => $ct['cur_obj_literal']['datatype'] ? '' : $ct['cur_obj_literal']['lang'], |
185
|
|
|
'o_datatype' => $ct['cur_obj_literal']['datatype'], |
186
|
|
|
); |
187
|
|
|
$this->addT($t); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
/* anchors */ |
191
|
|
|
if ($n['tag'] == 'a') { |
192
|
|
|
if (($s = $this->v('href uri', '', $n['a'])) && $ct['cur_obj_literal']['value']) { |
193
|
|
|
$t = array( |
194
|
|
|
's' => $s, |
195
|
|
|
's_type' => 'uri', |
196
|
|
|
'p' => $ct['ns']['rdfs'] . 'label', |
197
|
|
|
'o' => $ct['cur_obj_literal']['value'], |
198
|
|
|
'o_type' => 'literal', |
199
|
|
|
'o_lang' => $ct['cur_obj_literal']['datatype'] ? '' : $ct['cur_obj_literal']['lang'], |
200
|
|
|
'o_datatype' => $ct['cur_obj_literal']['datatype'], |
201
|
|
|
); |
202
|
|
|
$this->addT($t); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
/* recurse */ |
206
|
|
|
if ($n['tag'] == 'a') { |
207
|
|
|
$ct['cur_res'] = $ct['cur_obj_id']; |
208
|
|
|
} |
209
|
|
|
$sub_nodes = $this->getSubNodes($n); |
210
|
|
|
foreach ($sub_nodes as $sub_node) { |
211
|
|
|
$this->processNode($sub_node, $ct); |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/* */ |
216
|
|
|
|
217
|
|
|
function getPropertyURIs($n, $ct) { |
218
|
|
|
$r = array(); |
219
|
|
|
foreach (array('rel', 'rev', 'class', 'name', 'src') as $type) { |
220
|
|
|
$r[$type] = array(); |
221
|
|
|
$vals = $this->v($type . ' m', array(), $n['a']); |
222
|
|
|
foreach ($vals as $val) { |
223
|
|
|
if (!trim($val)) continue; |
224
|
|
|
list($uri, $sub_v) = $this->xQname(trim($val, '- '), $ct['base'], $ct['ns'], $type); |
|
|
|
|
225
|
|
|
if (!$uri) continue; |
226
|
|
|
$rdf_type = preg_match('/^-/', trim($val)) ? 1 : 0; |
227
|
|
|
$r[$type][] = $rdf_type ? ' ' . $uri : $uri; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
return $r; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
function getCurrentResourceURI($n, $ct) { |
234
|
|
|
if (isset($n['a']['id'])) { |
235
|
|
|
list($r, $sub_v) = $this->xURI('#' . $n['a']['id'], $ct['base'], $ct['ns']); |
|
|
|
|
236
|
|
|
return $r; |
237
|
|
|
} |
238
|
|
|
return $ct['cur_res']; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
function getCurrentObjectID($n, $ct) { |
242
|
|
|
foreach (array('href', 'src') as $a) { |
243
|
|
|
if (isset($n['a'][$a])) { |
244
|
|
|
list($r, $sub_v) = $this->xURI($n['a'][$a], $ct['base'], $ct['ns']); |
|
|
|
|
245
|
|
|
return $r; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
return $this->createBnodeID(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
function getCurrentObjectLiteral($n, $ct) { |
252
|
|
|
$r = array('value' => '', 'lang' => $ct['lang'], 'datatype' => ''); |
253
|
|
|
if (isset($n['a']['content'])) { |
254
|
|
|
$r['value'] = $n['a']['content']; |
255
|
|
|
} |
256
|
|
|
elseif (isset($n['a']['title'])) { |
257
|
|
|
$r['value'] = $n['a']['title']; |
258
|
|
|
} |
259
|
|
|
else { |
260
|
|
|
$r['value'] = $this->getPlainContent($n); |
261
|
|
|
} |
262
|
|
|
return $r; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/* */ |
266
|
|
|
|
267
|
|
|
function xURI($v, $base, $ns, $attr_type = '') { |
268
|
|
|
if ((list($sub_r, $sub_v) = $this->xQname($v, $base, $ns)) && $sub_r) { |
269
|
|
|
return array($sub_r, $sub_v); |
270
|
|
|
} |
271
|
|
|
if (preg_match('/^(rel|rev|class|name)$/', $attr_type) && preg_match('/^[a-z0-9]+$/', $v)) { |
272
|
|
|
return array(0, $v); |
273
|
|
|
} |
274
|
|
|
return array($this->calcURI($v, $base), ''); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
function xQname($v, $base, $ns) { |
278
|
|
|
if ($sub_r = $this->x('([a-z0-9\-\_]+)[\-\.]([a-z0-9\-\_]+)', $v)) { |
279
|
|
|
if (isset($ns[$sub_r[1]])) { |
280
|
|
|
return array($ns[$sub_r[1]] . $sub_r[2], ''); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
return array(0, $v); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/* */ |
287
|
|
|
|
288
|
|
|
} |
289
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.