3 * ARC2 MicroRDF Serializer
5 * @author Benjamin Nowack
6 * @license <http://arc.semsol.org/license>
7 * @homepage <http://arc.semsol.org/>
12 ARC2::inc('RDFSerializer');
14 class ARC2_MicroRDFSerializer extends ARC2_RDFSerializer {
16 function __construct($a = '', &$caller) {
17 parent::__construct($a, $caller);
20 function ARC2_MicroRDFSerializer($a = '', &$caller) {/* ns */
21 $this->__construct($a, $caller);
26 $this->content_header = 'text/html';
27 $this->label_store = $this->v('label_store', '', $this->a);
32 function getLabel($res, $ps = '') {
33 if (!$ps) $ps = array();
34 foreach ($ps as $p => $os) {
35 if (preg_match('/[\/\#](name|label|summary|title|fn)$/i', $p)) {
36 return $os[0]['value'];
39 if (preg_match('/^\_\:/', $res)) return "An unnamed resource";
40 return $this->extractTermLabel($res);
41 return preg_replace("/^(.*[\/\#])([^\/\#]+)$/", '\\2', str_replace('_', ' ', $res));
44 function getSerializedIndex($index, $res = '') {
47 if ($res) $index = array($res => $index[$res]);
48 //return Trice::dump($index);
49 $types = $this->v($this->expandPName('rdf:type'), array(), $index);
50 $main_type = $types ? $types[0]['value'] : '';
51 foreach ($index as $s => $ps) {
54 <div class="rdf-item" ' . $this->mdAttrs($s, $main_type) . '>
55 <h3 class="rdf-itemlabel"><a href="' . $s . '">' . ucfirst($this->getLabel($s, $ps)) . '</a></h3>
58 foreach ($ps as $p => $os) {
59 $p_cls = strtolower($this->getPName($p));
60 $p_cls = str_replace(':', '-', $p_cls);
62 <div class="rdf-prop ' . $p_cls . '">
63 <a class="rdf-proplabel" href="' . $p . '">' . ucfirst($this->getLabel($p)) . ':</a>
64 <ul class="rdf-values">
67 foreach ($os as $i => $o) {
68 $val = $this->getObjectValue($o, $p);
70 if ($i == 0) $cls .= ($cls ? ' ' : '') . 'first';
71 if ($i == $oc - 1) $cls .= ($cls ? ' ' : '') . 'last';
72 $r .= $n . '<li' . ($cls ? ' class="' . $cls . '"' : '') . '>' . $val . '</li>';
76 <div class="clb"></div>
82 <div class="clb"></div>
89 function getObjectValue($o, $p) {
90 if ($o['type'] == 'uri') {
91 if (preg_match('/(jpe?g|gif|png)$/i', $o['value'])) {
92 return $this->getImageObjectValue($o, $p);
94 return $this->getURIObjectValue($o, $p);
96 if ($o['type'] == "bnode") {
97 return $this->getBNodeObjectValue($o, $p);
99 return $this->getLiteralObjectValue($o, $p);
102 function getImageObjectValue($o, $p) {
103 return '<img class="rdf-value" itemprop="' . $p. '" src="' . htmlspecialchars($o['value']) . '" alt="img" />';
106 function getURIObjectValue($o, $p) {
107 $id = htmlspecialchars($o['value']);
108 $label = $this->getObjectLabel($o['value']);
110 $href = htmlspecialchars($this->v('href', $o['value'], $o));
112 return '<a class="rdf-value" itemprop="' . $p. '" href="' . $id . '" onclick="location.href=\'' . $href . '\';return false">' . $label . '</a>';
114 return '<a class="rdf-value" itemprop="' . $p. '" href="' . $id . '">' . $label . '</a>';
115 //$label = $o['value'];
116 //$label = preg_replace('/^https?\:\/\/(www\.)?/', '', $label);
119 function getBNodeObjectValue($o, $p) {
120 return '<div class="rdf-value" itemprop="' . $p. '" itemscope="">' . $o['value'] . '</div>';
121 return '<div class="rdf-value" itemprop="' . $p. '" itemscope="">An unnamed resource</div>';
124 function getLiteralObjectValue($o, $p) {
125 return '<div class="rdf-value" itemprop="' . $p. '">' . $o['value'] . '</div>';
130 function getObjectLabel($id) {
131 $r = $this->extractTermLabel($id);
132 if (!$this->label_store) return $r;
135 <' . $id . '> ?p ?val .
136 FILTER(REGEX(str(?p), "(label|title|name|summary)$"))
139 $row = $this->label_store->query($q, 'row');
140 return $row ? $row['val'] : $r;