Mercurial > hg > rr-repo
comparison sites/all/libraries/ARC2/arc/serializers/ARC2_MicroRDFSerializer.php @ 4:ce11bbd8f642
added modules
author | danieleb <danielebarchiesi@me.com> |
---|---|
date | Thu, 19 Sep 2013 10:38:44 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:b28be78d8160 | 4:ce11bbd8f642 |
---|---|
1 <?php | |
2 /** | |
3 * ARC2 MicroRDF Serializer | |
4 * | |
5 * @author Benjamin Nowack | |
6 * @license <http://arc.semsol.org/license> | |
7 * @homepage <http://arc.semsol.org/> | |
8 * @package ARC2 | |
9 * @version 2010-11-16 | |
10 */ | |
11 | |
12 ARC2::inc('RDFSerializer'); | |
13 | |
14 class ARC2_MicroRDFSerializer extends ARC2_RDFSerializer { | |
15 | |
16 function __construct($a, &$caller) { | |
17 parent::__construct($a, $caller); | |
18 } | |
19 | |
20 function __init() { | |
21 parent::__init(); | |
22 $this->content_header = 'text/html'; | |
23 $this->label_store = $this->v('label_store', '', $this->a); | |
24 } | |
25 | |
26 /* */ | |
27 | |
28 function getLabel($res, $ps = '') { | |
29 if (!$ps) $ps = array(); | |
30 foreach ($ps as $p => $os) { | |
31 if (preg_match('/[\/\#](name|label|summary|title|fn)$/i', $p)) { | |
32 return $os[0]['value']; | |
33 } | |
34 } | |
35 if (preg_match('/^\_\:/', $res)) return "An unnamed resource"; | |
36 return $this->extractTermLabel($res); | |
37 return preg_replace("/^(.*[\/\#])([^\/\#]+)$/", '\\2', str_replace('_', ' ', $res)); | |
38 } | |
39 | |
40 function getSerializedIndex($index, $res = '') { | |
41 $r = ''; | |
42 $n = "\n"; | |
43 if ($res) $index = array($res => $index[$res]); | |
44 //return Trice::dump($index); | |
45 $types = $this->v($this->expandPName('rdf:type'), array(), $index); | |
46 $main_type = $types ? $types[0]['value'] : ''; | |
47 foreach ($index as $s => $ps) { | |
48 /* node */ | |
49 $r .= ' | |
50 <div class="rdf-item" ' . $this->mdAttrs($s, $main_type) . '> | |
51 <h3 class="rdf-itemlabel"><a href="' . $s . '">' . ucfirst($this->getLabel($s, $ps)) . '</a></h3> | |
52 '; | |
53 /* arcs */ | |
54 foreach ($ps as $p => $os) { | |
55 $p_cls = strtolower($this->getPName($p)); | |
56 $p_cls = str_replace(':', '-', $p_cls); | |
57 $r .= ' | |
58 <div class="rdf-prop ' . $p_cls . '"> | |
59 <a class="rdf-proplabel" href="' . $p . '">' . ucfirst($this->getLabel($p)) . ':</a> | |
60 <ul class="rdf-values"> | |
61 '; | |
62 $oc = count($os); | |
63 foreach ($os as $i => $o) { | |
64 $val = $this->getObjectValue($o, $p); | |
65 $cls = ''; | |
66 if ($i == 0) $cls .= ($cls ? ' ' : '') . 'first'; | |
67 if ($i == $oc - 1) $cls .= ($cls ? ' ' : '') . 'last'; | |
68 $r .= $n . '<li' . ($cls ? ' class="' . $cls . '"' : '') . '>' . $val . '</li>'; | |
69 } | |
70 $r .= ' | |
71 </ul> | |
72 <div class="clb"></div> | |
73 </div> | |
74 '; | |
75 } | |
76 /* /node */ | |
77 $r .= ' | |
78 <div class="clb"></div> | |
79 </div> | |
80 '; | |
81 } | |
82 return $r; | |
83 } | |
84 | |
85 function getObjectValue($o, $p) { | |
86 if ($o['type'] == 'uri') { | |
87 if (preg_match('/(jpe?g|gif|png)$/i', $o['value'])) { | |
88 return $this->getImageObjectValue($o, $p); | |
89 } | |
90 return $this->getURIObjectValue($o, $p); | |
91 } | |
92 if ($o['type'] == "bnode") { | |
93 return $this->getBNodeObjectValue($o, $p); | |
94 } | |
95 return $this->getLiteralObjectValue($o, $p); | |
96 } | |
97 | |
98 function getImageObjectValue($o, $p) { | |
99 return '<img class="rdf-value" itemprop="' . $p. '" src="' . htmlspecialchars($o['value']) . '" alt="img" />'; | |
100 } | |
101 | |
102 function getURIObjectValue($o, $p) { | |
103 $id = htmlspecialchars($o['value']); | |
104 $label = $this->getObjectLabel($o['value']); | |
105 /* differing href */ | |
106 $href = htmlspecialchars($this->v('href', $o['value'], $o)); | |
107 if ($id != $href) { | |
108 return '<a class="rdf-value" itemprop="' . $p. '" href="' . $id . '" onclick="location.href=\'' . $href . '\';return false">' . $label . '</a>'; | |
109 } | |
110 return '<a class="rdf-value" itemprop="' . $p. '" href="' . $id . '">' . $label . '</a>'; | |
111 //$label = $o['value']; | |
112 //$label = preg_replace('/^https?\:\/\/(www\.)?/', '', $label); | |
113 } | |
114 | |
115 function getBNodeObjectValue($o, $p) { | |
116 return '<div class="rdf-value" itemprop="' . $p. '" itemscope="">' . $o['value'] . '</div>'; | |
117 return '<div class="rdf-value" itemprop="' . $p. '" itemscope="">An unnamed resource</div>'; | |
118 } | |
119 | |
120 function getLiteralObjectValue($o, $p) { | |
121 return '<div class="rdf-value" itemprop="' . $p. '">' . $o['value'] . '</div>'; | |
122 } | |
123 | |
124 /* */ | |
125 | |
126 function getObjectLabel($id) { | |
127 $r = $this->extractTermLabel($id); | |
128 if (!$this->label_store) return $r; | |
129 $q = ' | |
130 SELECT ?val WHERE { | |
131 <' . $id . '> ?p ?val . | |
132 FILTER(REGEX(str(?p), "(label|title|name|summary)$")) | |
133 } LIMIT 1 | |
134 '; | |
135 $row = $this->label_store->query($q, 'row'); | |
136 return $row ? $row['val'] : $r; | |
137 } | |
138 | |
139 /* */ | |
140 | |
141 } | |
142 |