3 homepage: http://arc.semsol.org/
4 license: http://arc.semsol.org/license
6 class: ARC2 RDF Extractor
7 author: Benjamin Nowack
8 version: 2008-11-18 (Fix: Skip comments. Thanks to Masahide Kanzaki)
13 class ARC2_RDFExtractor extends ARC2_Class {
15 function __construct($a = '', &$caller) {
16 parent::__construct($a, $caller);
19 function ARC2_RDFExtractor($a = '', &$caller) {
20 $this->__construct($a, $caller);
25 $this->nodes = $this->caller->getNodes();
26 $this->index = $this->caller->getNodeIndex();
27 $this->bnode_prefix = $this->v('bnode_prefix', 'arc' . substr(md5(uniqid(rand())), 0, 4) . 'b', $this->a);
29 $this->keep_cdata_ws = $this->v('keep_cdata_whitespace', 0, $this->a);
30 if (!isset($this->a['ns'])) $this->a['ns'] = array('rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#');
35 function x($re, $v, $options = 'si') {
36 return ARC2::x($re, $v, $options);
39 function camelCase($v) {
41 while (preg_match('/^(.*)[\-\_ ](.*)$/', $r, $m)) {
42 $r = $m[1] . ucfirst($m[2]);
47 function createBnodeID(){
49 return '_:' . $this->bnode_prefix . $this->bnode_id;
54 function extractRDF() {
61 $this->caller->addT($t);
66 return $this->caller->addT($t);
71 function getSubNodes($n) {
72 return $this->v($n['id'], array(), $this->index);
75 function getParentNode($n) {
76 return isset($this->nodes[$n['p_id']]) ? $this->nodes[$n['p_id']] : 0;
81 function getSubNodesByClass($n, $cls, $skip_self = 0) {
82 if (!$skip_self && $this->hasClass($n, $cls)) {
86 $sns = $this->getSubNodes($n);
87 foreach ($sns as $sn) {
88 if ($sub_r = $this->getSubNodesByClass($sn, $cls, 0)) {
89 $r = array_merge($r, $sub_r);
95 function getSubNodeByClass($n, $cls, $skip_self = 0) {
96 if (!$skip_self && $this->hasClass($n, $cls)) {
99 $sns = $this->getSubNodes($n);
100 foreach ($sns as $sn) {
101 if ($sub_r = $this->getSubNodeByClass($sn, $cls, 0)) {
108 function getParentNodeByClass($n, $cls, $skip_self = 0) {
109 if (!$skip_self && $this->hasClass($n, $cls)) {
112 if ($pn = $this->getParentNode($n)) {
113 if ($sub_r = $this->getParentNodeByClass($pn, $cls, 0)) {
122 function hasAttribute($a, $n, $v) {
123 $vs = is_array($v) ? $v : array($v);
124 $a_vs = $this->v($a . ' m', array(), $n['a']);
125 return array_intersect($vs, $a_vs) ? 1 : 0;
128 function hasClass($n, $v) {
129 return $this->hasAttribute('class', $n, $v);
132 function hasRel($n, $v) {
133 return $this->hasAttribute('rel', $n, $v);
138 function getDocBase() {
139 $root_node = $this->getRootNode();
140 $r = $root_node['doc_base'];
141 foreach ($this->getSubNodes($root_node) as $root_child) {
142 if ($root_child['tag'] == 'head') {
143 foreach ($this->getSubNodes($root_child) as $head_child) {
144 if ($head_child['tag'] == 'base') {
145 $r = $head_child['a']['href'];
156 function getPlainContent($n, $trim = 1, $use_img_alt = 1) {
157 if ($n['tag'] == 'comment') {
160 elseif ($n['tag'] == 'cdata') {
161 $r = $n['a']['value'];
163 elseif (trim($this->v('cdata', '', $n))) {
165 $sub_nodes = $this->getSubNodes($n);
166 foreach ($sub_nodes as $sub_n) {
167 $r .= $this->getPlainContent($sub_n, 0, $use_img_alt);
170 elseif (($n['tag'] == 'img') && $use_img_alt && isset($n['a']['alt'])) {
175 $sub_nodes = $this->getSubNodes($n);
176 foreach ($sub_nodes as $sub_n) {
177 $r .= $this->getPlainContent($sub_n, 0, $use_img_alt);
180 $r = preg_replace('/\s/s', ' ', $r);
181 $r = preg_replace('/\s\s*/s', ' ', $r);
182 return $trim ? trim($r) : $r;
185 function getContent($n, $outer = 0, $trim = 1) {
186 //echo '<pre>' . htmlspecialchars(print_r($n, 1)) . '</pre>';
187 if ($n['tag'] == 'comment') {
188 $r = '<!-- ' . $n['a']['value'] . ' -->';
190 elseif ($n['tag'] == 'cdata') {
191 $r = $n['a']['value'];
196 $r .= '<' . $n['tag'];
198 if (isset($n['a']['xmlns']) && $n['a']['xmlns']['']) {
199 $r .= ' xmlns="' . $n['a']['xmlns'][''] . '"';
201 foreach ($n['a'] as $a => $val) {
202 if (!is_array($val) && isset($n['a'][$a . ' uri'])) $val = $n['a'][$a . ' uri'];
203 $r .= preg_match('/^[^\s]+$/', $a) && !is_array($val) ? ' ' . $a . '="' . addslashes($val) . '"' : '';
205 $r .= $n['empty'] ? '/>' : '>';
208 $r .= $this->v('cdata', '', $n);
209 $sub_nodes = $this->getSubNodes($n);
210 foreach ($sub_nodes as $sub_n) {
211 $r .= $this->getContent($sub_n, 1, 0);
214 $r .= '</' . $n['tag'] . '>';
218 return ($trim && !$this->keep_cdata_ws) ? trim($r) : $r;
223 function getDocID($n) {
226 if (!isset($this->caller->cache[$k])) {
227 $this->caller->cache[$k] = $n['doc_url'];
229 return $this->caller->cache[$k];
232 function getDocOwnerID($n) {
233 return '_:owner_of_' . $this->normalize($this->getDocID($n));
238 function normalize($v) {
239 $v = preg_replace('/[\W\s]+/is', '_', strip_tags(strtolower($v)));
240 $v = preg_replace('/http/', '', $v);
241 $v = preg_replace('/[\_]+/', '_', $v);
242 //$v = substr($v, 0, 30);