PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/Output/ViewSemantics.php

https://bitbucket.org/aksw/rdf2wp
PHP | 255 lines | 117 code | 36 blank | 102 comment | 16 complexity | c0c74909f4a8e89c3bd97ba3e7e3d642 MD5 | raw file
  1. <?php
  2. /*
  3. * Plugin Name: ViewSemantics
  4. * Description: Display the RDF Data in the semantics tag as a HTML table
  5. * @Version: 1.0
  6. * @Author Falk Stehmann
  7. * @Author Ivan Krishanik
  8. */
  9. // ARC2 static class inclusion
  10. //include_once('semsol-arc2-495d10b/ARC2.php');
  11. class ViewSemantics
  12. {
  13. function ViewSemantics()
  14. {
  15. add_filter('the_content', array($this, 'runShortcode'), 2);
  16. }
  17. /**
  18. * Macht ShortCode "semantics" vor allen anderen Filtern.
  19. *
  20. * @param string $content
  21. * Der Inhalt des Posts.
  22. *
  23. * @return string
  24. * Gefilterter Inhalt des Posts.
  25. *
  26. * @test_author Sergej Sintschilin
  27. * @test_date 20.06.2011
  28. * @test_result OK
  29. */
  30. public function runShortcode($content)
  31. {
  32. remove_all_shortcodes();
  33. add_shortcode('semantics', array($this, 'semanticsToHtml'));
  34. $content = do_shortcode($content);
  35. return $content;
  36. }
  37. /**
  38. * converts ARC2 triples into a HTML table
  39. * @param string $triples
  40. * ARC2 triples
  41. * @return string
  42. * HTML table
  43. *
  44. * @test_author Ivan Krishanik
  45. * @test_date 31.05.2011
  46. * @test_result OK
  47. *
  48. * @test_author Falk Stehmann
  49. * @test_date 13.06.2011
  50. * @test_result OK
  51. */
  52. public static function triplesToHtmlTable($triples)
  53. {
  54. $table = array();
  55. //iterate over triples and merge same predicates
  56. foreach ( $triples as $triple)
  57. {
  58. // get predicate and object
  59. $p = ViewSemantics::filterUri($triple['p']);
  60. $o = ViewSemantics::filterObject($triple);
  61. if(isset($table[$p])){
  62. $table[$p] = $table[$p].', '.$o;
  63. }else{
  64. $table[$p] = $o;
  65. }
  66. }
  67. $result .= '<table>';
  68. foreach( $table as $p => $o ){
  69. $result .= "<tr>";
  70. $result.= "<td>".$p."</td><td>".$o."</td>";
  71. $result .= "</tr>";
  72. }
  73. $result .= "</table>";
  74. $result=ViewSemantics::addExportLinks($result);
  75. return $result;
  76. }
  77. /**
  78. * converts the uri in a readable form
  79. * kurzum: added $firstToUpper
  80. * FIXME: This Code Smells
  81. *
  82. * @param array $u
  83. * the uri
  84. * @param boolean $firstToUpper=true
  85. * prints the first letter of the link text in upper case
  86. * @return string
  87. * HTML link
  88. *
  89. * @test_author Falk Stehmann
  90. * @test_date 23.06.2011
  91. * @test_result OK
  92. */
  93. public static function filterUri($u, $firstToUpper=true)
  94. {
  95. $x1 = substr(strrchr($u,"/"),1);
  96. if($x1 == '') {
  97. $x1= substr(strrchr(substr($u, 0, -1),"/"),1);
  98. }
  99. $x2 = substr(strrchr($u,"#"),1);
  100. if(true){
  101. $x1 = ucfirst($x1);
  102. $x2 = ucfirst($x2);
  103. ucfirst($p);
  104. }
  105. $uri= '<a href="'.$u.'">';
  106. if ( $x1 == "" ) $uri = $p.$x2;
  107. else
  108. if ( $x2 == "" ) $uri .= $x1;
  109. else
  110. if(strlen($x1) < strlen($x2)) $uri .= $x1;
  111. else $uri .= $x2;
  112. return $uri.'</a>';
  113. }
  114. /**
  115. * converts the object of the triple in a readable form
  116. * @param array $t
  117. * the triple
  118. * @return string
  119. * filtered object
  120. *
  121. * @test_author Falk Stehmann
  122. * @test_date 23.06.2011
  123. * @test_result OK
  124. */
  125. public static function filterObject($triple)
  126. {
  127. //for these predicates, an image tag will be added to their objects
  128. $images = array("thumbnail", "picture", "http://catalogus-professorum.org/cpm/picture",
  129. "http://pcai042.informatik.uni-leipzig.de/thumbnail", "http://www.klappstuhlclub.de/wp/posts/thumbnail");
  130. $o=$triple['o'];
  131. if ($triple['o_datatype'] == 'http://www.w3.org/2001/XMLSchema#datetime')
  132. {
  133. $date = DateTime::createFromFormat(DateTime::W3C, $triple['o']);
  134. if ($date)
  135. {
  136. //$o = $date->format(DateTime::RSS);
  137. $o = $date->format('d.m.Y H:i');
  138. }
  139. }
  140. if($triple['o_type'] == 'uri')
  141. {
  142. $o=ViewSemantics::filterUri($o);
  143. }
  144. if (in_array($triple['p'], $images)) // add img tag
  145. {
  146. $o = '<img src="'.$triple['o'].'">';
  147. }
  148. return $o;
  149. }
  150. /**
  151. * adds Export links to the parameter for RDF, JSON, N3 and Ntriple format
  152. * @param string $result
  153. * table without links
  154. * @return string
  155. * table with links added
  156. *
  157. * @test_author Falk Stehmann
  158. * @test_date 23.06.2011
  159. * @test_result OK
  160. */
  161. public static function addExportLinks($result)
  162. {
  163. //RDF Output
  164. $result .= '<a href="'.WP_PLUGIN_URL.'/RDF2WP/Output/linkedDataScript.php?id='.get_the_id().'&format=application/rdf%2Bxml">RDF</a>&nbsp;|&nbsp;';
  165. //JSON output
  166. $result .= '<a href="'.WP_PLUGIN_URL.'/RDF2WP/Output/linkedDataScript.php?id='.get_the_id().'&format=application/json%2Brdf">JSON</a>&nbsp;|&nbsp;';
  167. //N3 output
  168. $result .= '<a href="'.WP_PLUGIN_URL.'/RDF2WP/Output/linkedDataScript.php?id='.get_the_id().'&format=text/rdf%2Bn3">N3</a>&nbsp;|&nbsp;';
  169. //NTriples output
  170. $result .= '<a href="'.WP_PLUGIN_URL.'/RDF2WP/Output/linkedDataScript.php?id='.get_the_id().'&format=text/plain">NTriples</a>';
  171. return $result;
  172. }
  173. /**
  174. * converts semantic data in a given format into an HTML table
  175. *
  176. * @param string $atts
  177. * attributes to the semantics tag
  178. * @param string $content
  179. * content in between the semantics tags
  180. * @return string
  181. * HTML table
  182. *
  183. * @test_author Falk Stehmann
  184. * @test_date 13.06.2011
  185. * @test_result OK
  186. */
  187. public static function semanticsToHtml($atts, $content)
  188. {
  189. $triples = array();
  190. //is called by the parser to pass the triples
  191. $handleTriple = function($triple) use (&$triples)
  192. {
  193. array_push($triples, $triple);
  194. };
  195. // choose the right parser
  196. switch($atts['format'])
  197. {
  198. case("infobox"):
  199. {
  200. $parser = new KSCParser($content, get_permalink(get_the_id()));
  201. $parser->parse($handleTriple);
  202. break;
  203. }
  204. case("prof"):
  205. {
  206. $parser = new ProfParser($content);
  207. $parser->parse($handleTriple);
  208. break;
  209. }
  210. default:
  211. return $content;
  212. }
  213. // convert triples to html
  214. return ViewSemantics::triplesToHtmlTable($triples);
  215. }
  216. }
  217. // bind ViewSemantics to the semantics tag
  218. //add_shortcode('semantics',array('ViewSemantics', 'semanticsToHtml'));
  219. ?>