/Output/ViewSemantics.php
PHP | 255 lines | 117 code | 36 blank | 102 comment | 16 complexity | c0c74909f4a8e89c3bd97ba3e7e3d642 MD5 | raw file
- <?php
- /*
- * Plugin Name: ViewSemantics
- * Description: Display the RDF Data in the semantics tag as a HTML table
- * @Version: 1.0
- * @Author Falk Stehmann
- * @Author Ivan Krishanik
- */
-
- // ARC2 static class inclusion
- //include_once('semsol-arc2-495d10b/ARC2.php');
-
-
-
- class ViewSemantics
- {
-
- function ViewSemantics()
- {
- add_filter('the_content', array($this, 'runShortcode'), 2);
- }
-
- /**
- * Macht ShortCode "semantics" vor allen anderen Filtern.
- *
- * @param string $content
- * Der Inhalt des Posts.
- *
- * @return string
- * Gefilterter Inhalt des Posts.
- *
- * @test_author Sergej Sintschilin
- * @test_date 20.06.2011
- * @test_result OK
- */
- public function runShortcode($content)
- {
- remove_all_shortcodes();
- add_shortcode('semantics', array($this, 'semanticsToHtml'));
- $content = do_shortcode($content);
- return $content;
- }
-
- /**
- * converts ARC2 triples into a HTML table
- * @param string $triples
- * ARC2 triples
- * @return string
- * HTML table
- *
- * @test_author Ivan Krishanik
- * @test_date 31.05.2011
- * @test_result OK
- *
- * @test_author Falk Stehmann
- * @test_date 13.06.2011
- * @test_result OK
- */
- public static function triplesToHtmlTable($triples)
- {
-
-
- $table = array();
- //iterate over triples and merge same predicates
- foreach ( $triples as $triple)
- {
- // get predicate and object
- $p = ViewSemantics::filterUri($triple['p']);
- $o = ViewSemantics::filterObject($triple);
-
- if(isset($table[$p])){
- $table[$p] = $table[$p].', '.$o;
- }else{
- $table[$p] = $o;
- }
- }
-
- $result .= '<table>';
-
- foreach( $table as $p => $o ){
- $result .= "<tr>";
- $result.= "<td>".$p."</td><td>".$o."</td>";
- $result .= "</tr>";
-
- }
-
- $result .= "</table>";
-
- $result=ViewSemantics::addExportLinks($result);
-
- return $result;
- }
- /**
- * converts the uri in a readable form
- * kurzum: added $firstToUpper
- * FIXME: This Code Smells
- *
- * @param array $u
- * the uri
- * @param boolean $firstToUpper=true
- * prints the first letter of the link text in upper case
- * @return string
- * HTML link
- *
- * @test_author Falk Stehmann
- * @test_date 23.06.2011
- * @test_result OK
- */
- public static function filterUri($u, $firstToUpper=true)
- {
- $x1 = substr(strrchr($u,"/"),1);
- if($x1 == '') {
- $x1= substr(strrchr(substr($u, 0, -1),"/"),1);
- }
- $x2 = substr(strrchr($u,"#"),1);
-
- if(true){
- $x1 = ucfirst($x1);
- $x2 = ucfirst($x2);
- ucfirst($p);
- }
-
- $uri= '<a href="'.$u.'">';
-
- if ( $x1 == "" ) $uri = $p.$x2;
- else
- if ( $x2 == "" ) $uri .= $x1;
- else
- if(strlen($x1) < strlen($x2)) $uri .= $x1;
- else $uri .= $x2;
- return $uri.'</a>';
- }
- /**
- * converts the object of the triple in a readable form
- * @param array $t
- * the triple
- * @return string
- * filtered object
- *
- * @test_author Falk Stehmann
- * @test_date 23.06.2011
- * @test_result OK
- */
- public static function filterObject($triple)
- {
- //for these predicates, an image tag will be added to their objects
- $images = array("thumbnail", "picture", "http://catalogus-professorum.org/cpm/picture",
- "http://pcai042.informatik.uni-leipzig.de/thumbnail", "http://www.klappstuhlclub.de/wp/posts/thumbnail");
-
- $o=$triple['o'];
-
- if ($triple['o_datatype'] == 'http://www.w3.org/2001/XMLSchema#datetime')
- {
- $date = DateTime::createFromFormat(DateTime::W3C, $triple['o']);
- if ($date)
- {
- //$o = $date->format(DateTime::RSS);
- $o = $date->format('d.m.Y H:i');
- }
- }
-
- if($triple['o_type'] == 'uri')
- {
- $o=ViewSemantics::filterUri($o);
- }
- if (in_array($triple['p'], $images)) // add img tag
- {
- $o = '<img src="'.$triple['o'].'">';
- }
-
- return $o;
- }
-
- /**
- * adds Export links to the parameter for RDF, JSON, N3 and Ntriple format
- * @param string $result
- * table without links
- * @return string
- * table with links added
- *
- * @test_author Falk Stehmann
- * @test_date 23.06.2011
- * @test_result OK
- */
- public static function addExportLinks($result)
- {
- //RDF Output
- $result .= '<a href="'.WP_PLUGIN_URL.'/RDF2WP/Output/linkedDataScript.php?id='.get_the_id().'&format=application/rdf%2Bxml">RDF</a> | ';
- //JSON output
- $result .= '<a href="'.WP_PLUGIN_URL.'/RDF2WP/Output/linkedDataScript.php?id='.get_the_id().'&format=application/json%2Brdf">JSON</a> | ';
- //N3 output
- $result .= '<a href="'.WP_PLUGIN_URL.'/RDF2WP/Output/linkedDataScript.php?id='.get_the_id().'&format=text/rdf%2Bn3">N3</a> | ';
- //NTriples output
- $result .= '<a href="'.WP_PLUGIN_URL.'/RDF2WP/Output/linkedDataScript.php?id='.get_the_id().'&format=text/plain">NTriples</a>';
-
- return $result;
- }
- /**
- * converts semantic data in a given format into an HTML table
- *
- * @param string $atts
- * attributes to the semantics tag
- * @param string $content
- * content in between the semantics tags
- * @return string
- * HTML table
- *
- * @test_author Falk Stehmann
- * @test_date 13.06.2011
- * @test_result OK
- */
- public static function semanticsToHtml($atts, $content)
- {
- $triples = array();
-
- //is called by the parser to pass the triples
-
- $handleTriple = function($triple) use (&$triples)
- {
- array_push($triples, $triple);
-
- };
-
- // choose the right parser
-
- switch($atts['format'])
- {
- case("infobox"):
- {
- $parser = new KSCParser($content, get_permalink(get_the_id()));
- $parser->parse($handleTriple);
- break;
- }
- case("prof"):
- {
- $parser = new ProfParser($content);
- $parser->parse($handleTriple);
- break;
- }
-
- default:
- return $content;
-
- }
-
-
-
- // convert triples to html
- return ViewSemantics::triplesToHtmlTable($triples);
- }
- }
-
- // bind ViewSemantics to the semantics tag
- //add_shortcode('semantics',array('ViewSemantics', 'semanticsToHtml'));
- ?>