PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/rdfapi-php/rap-pubby/RAPpubbyHTMLSer.php

https://github.com/koja13/DSi2.0
PHP | 361 lines | 215 code | 86 blank | 60 comment | 28 complexity | b5cb52db7d145ec68dd23827190d1536 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?PHP
  2. // ----------------------------------------------------------------------------------
  3. // RAP_Pubby - A Linked Data Frontend for RAP
  4. // ----------------------------------------------------------------------------------
  5. /**
  6. * Installation information is found in the RAP_Pubby documentation.
  7. *
  8. * @author Radoslaw Oldakowski <radol@gmx.de>
  9. * @version 1.0, 19.12.2007
  10. * @package rap-pubby
  11. */
  12. include_once(RDFAPI_INCLUDE_DIR.PACKAGE_UTILITY);
  13. Class RAPpubbyHTMLSer extends Object {
  14. var $htmlTemplate;
  15. var $htmlTemplate_404;
  16. var $placeholders = array(
  17. '##_templInclDirURL_##' => '',
  18. '##_projetName_##' => '',
  19. '##_projectHomepage_##' => '',
  20. '##_resURI_##' => '',
  21. '##_repURIdata_##' => '',
  22. '##_resourceLabel_##' => '',
  23. '##_shortDescription_##' => '',
  24. '##_imgLink_##' => '',
  25. '##_prop_tableRows_##' => '',
  26. );
  27. /**
  28. *
  29. */
  30. function RAPpubbyHTMLSer ($template = PUBBY_HTML_SER__TEMPLATE, $template_404 = PUBBY_HTML_SER__TEMPLATE_404) {
  31. $this->loadTemplates($template, $template_404);
  32. $this->placeholders['##_templInclDirURL_##'] = PUBBY_WEBBASE .PUBBY_HTML_SER__TEMPL_INCL_DIR;
  33. $this->placeholders['##_projectName_##'] = PUBBY_HTML_SER__PROJECT_NAME;
  34. $this->placeholders['##_projectHomepage_##'] = PUBBY_HTML_SER__PROJECT_HOMEPAGE;
  35. }
  36. /**
  37. *
  38. */
  39. function serialize (&$rd) {
  40. if ($rd->isEmpty()) {
  41. $html = $this->htmlTemplate_404;
  42. }
  43. else {
  44. $html = $this->htmlTemplate;
  45. }
  46. $ph = $this->generatePlaceholderValues($rd);
  47. $html = str_ireplace(array_keys($ph), $ph, $html);
  48. return $html;
  49. }
  50. /**
  51. *
  52. */
  53. function loadTemplates($t, $t_404) {
  54. if (file_exists($t)) {
  55. $file = fopen($t, "r");
  56. if ($file) {
  57. $this->htmlTemplate = fread($file,filesize($t));
  58. fclose($file);
  59. }
  60. }
  61. else {
  62. trigger_error("Could not find template file: $t, ");
  63. $this->htmlTemplate = $this->getBasicTemplate();
  64. }
  65. if (file_exists($t_404)) {
  66. $file = fopen($t_404, "r");
  67. if ($file) {
  68. $this->htmlTemplate_404 = fread($file,filesize($t_404));
  69. fclose($file);
  70. }
  71. }
  72. else {
  73. trigger_error("Could not find template file: $t_404, ");
  74. $this->htmlTemplate_404 = $this->getBasicTemplate_404();
  75. }
  76. }
  77. /**
  78. *
  79. */
  80. function getBasicTemplate() {
  81. return '<h1>##_resURI_##</h1>
  82. <table>
  83. <tr><td><h3>Property</h3></td> <td><h3>Value</h3></td></tr>
  84. ##_prop_tableRows_##
  85. </table>';
  86. }
  87. function getBasicTemplate_404() {
  88. return '<h1>##_resURI_##</h1>
  89. <p>The requested resource does not exist at this server, or no information about it is available.</p>';
  90. }
  91. /**
  92. *
  93. */
  94. function generatePlaceholderValues (&$rd) {
  95. $res = & $rd->getResource();
  96. $m = & $rd->getModel();
  97. $ph = $this->placeholders;
  98. $ph['##_resURI_##'] = $res->getURI();
  99. $ph['##_repURIdata_##'] = $rd->getDataURI();
  100. $ph['##_resourceLabel_##'] = $this->findLabel($m, $res);
  101. $ph['##_shortDescription_##'] = $this->findComment($m, $res);
  102. $ph['##_imgLink_##'] = $this->findImgLink($m, $res);
  103. $ph['##_prop_tableRows_##'] = $this->renderResourceProperties($m, $res);
  104. return $ph;
  105. }
  106. /**
  107. * find only the first occurance of the label
  108. */
  109. function findLabel(&$m, &$res) {
  110. global $_PUBBY_HTML_SER;
  111. return $this->findLiteralValueLang($m, $res, $_PUBBY_HTML_SER['labelProperty']);
  112. }
  113. /**
  114. * find only the first occurance of the comment
  115. */
  116. function findComment(&$m, &$res) {
  117. global $_PUBBY_HTML_SER;
  118. return $this->findLiteralValueLang($m, $res, $_PUBBY_HTML_SER['commentProperty']);
  119. }
  120. /**
  121. *
  122. */
  123. function findImgLink (&$m, &$res) {
  124. global $_PUBBY_HTML_SER;
  125. foreach ($_PUBBY_HTML_SER['imageProperty'] as $prop) {
  126. $triple = $m->findFirstMatchingStatement($res, $prop, NULL);
  127. if ($triple) {
  128. return $triple->getObject()->getURI();
  129. }
  130. }
  131. return '';
  132. }
  133. /**
  134. * find only the first occurance of the Literal from an array of properties
  135. */
  136. function findLiteralValueLang(&$m, &$res, &$prop_array) {
  137. $tmp = '';
  138. // if no default lang is specified find any literal value
  139. if (PUBBY_HTML_SER__DEFAULT_LANG == '') {
  140. foreach ($prop_array as $prop) {
  141. $triple = $m->findFirstMatchingStatement($res, $prop, NULL);
  142. if ($triple) {
  143. return $triple->getObject()->getLabel();
  144. }
  145. }
  146. }
  147. else {
  148. foreach ($prop_array as $prop) {
  149. $result = $m->find($res, $prop, NULL);
  150. $iter = $result->getStatementIterator();
  151. while ($iter->hasNext()) {
  152. $triple = $iter->next();
  153. if ($triple->getObject()->getLanguage() == PUBBY_HTML_SER__DEFAULT_LANG) {
  154. return $triple->getObject()->getLabel();
  155. }
  156. }
  157. // if no label with default lang was found take the first one
  158. // and store it in $tmp because another property could contain the default lang
  159. if (!$result->isEmpty()) {
  160. $triple = $result->findFirstMatchingStatement($res, $prop, NULL);
  161. if ($triple) {
  162. $tmp = $triple->getObject()->getLabel();
  163. }
  164. }
  165. }
  166. }
  167. return $tmp;
  168. }
  169. /**
  170. *
  171. */
  172. function renderResourceProperties (&$m, &$res) {
  173. $prop = array();
  174. // find properties
  175. $result = $m->find($res, NULL, NULL);
  176. $iter = $result->getStatementIterator();
  177. while ($iter->hasNext()) {
  178. $triple = $iter->next();
  179. $propURI = $triple->getPredicate()->getURI();
  180. $prop[$propURI][false][] = $triple->getObject();
  181. }
  182. // find inverse properteis (backlinks)
  183. $result = $m->find(NULL, NULL, $res);
  184. $iter = $result->getStatementIterator();
  185. while ($iter->hasNext()) {
  186. $triple = $iter->next();
  187. $propURI = $triple->getPredicate()->getURI();
  188. // inverse the property --> get subject instead of object
  189. $prop[$propURI][true][] = $triple->getSubject();
  190. }
  191. // sort properties
  192. uksort($prop, array($this, '_sortURIbyQName'));
  193. // render table rows with resource properties
  194. $nsPrefix = $m->getParsedNamespaces(); if (!$nsPrefix) $nsPrefix = array();
  195. $html_tableRows = '';
  196. $tr_class = array(true => 'odd', false => 'even');
  197. $odd = true;
  198. foreach ($prop as $propURI => $propType) {
  199. foreach ($propType as $inverseProp => $valArray) {
  200. $tr='
  201. <tr class="' .$tr_class[$odd] .'">
  202. <td class="property">'
  203. .$this->renderPropURI($propURI, $nsPrefix, $inverseProp)
  204. .'</td>
  205. <td>
  206. <ul>';
  207. foreach ($valArray as $propValue) {
  208. if (is_a($propValue, "Literal")) {
  209. $tr .='
  210. <li>'
  211. .$this->renderLiteral($propValue)
  212. .'</li>';
  213. }
  214. else {
  215. $tr .='
  216. <li>'
  217. .$this->renderURI($propValue->getURI(), $nsPrefix)
  218. .'</li>';
  219. }
  220. }
  221. $tr .='
  222. </ul>
  223. </td>
  224. </tr>';
  225. $odd = !$odd;
  226. $html_tableRows .= $tr;
  227. }
  228. }
  229. return $html_tableRows;
  230. }
  231. /**
  232. *
  233. */
  234. function renderLiteral ($literal) {
  235. $html = '<span class="literal">' .$literal->getLabel();
  236. if ($literal->getLanguage()) {
  237. $html .= '<small> (' .$literal->getLanguage() .')</small>';
  238. }
  239. return $html .= '</span>';
  240. }
  241. /**
  242. *
  243. */
  244. function renderPropURI ($URI, &$nsPrefix, $inverseProp=false) {
  245. if ($inverseProp) {
  246. return '<small>is </small>' .$this->renderURI($URI, $nsPrefix) .'<small> of</small>';
  247. }
  248. else {
  249. return $this->renderURI($URI, $nsPrefix);
  250. }
  251. }
  252. /**
  253. *
  254. */
  255. function renderURI($URI, &$nsPrefix) {
  256. $ns = RDFUtil::guessNamespace($URI);
  257. $qName = RDFUtil::guessName($URI);
  258. $html = '<a class="uri" href="' .$URI .'" title="' .$URI .'">';
  259. if (array_key_exists($ns, $nsPrefix)) {
  260. $html .= '<small>' .$nsPrefix[$ns] .':</small>' .$qName;
  261. }
  262. else {
  263. $html .= $URI;
  264. }
  265. $html .= '</a>';
  266. return $html;
  267. }
  268. /**
  269. * Call-back function for uksort()
  270. */
  271. function _sortURIbyQName ($a, $b) {
  272. return strcasecmp(RDFUtil::guessName($a), RDFUtil::guessName($b));
  273. }
  274. }
  275. ?>