diff --git a/classes/BasicSpecialFunction.php b/classes/BasicSpecialFunction.php index 8258fa7519b8267ed8e837e0469d642bc3c01705..99f6ba3b1562acfb1897ecd36b7f14612cae1ab2 100644 --- a/classes/BasicSpecialFunction.php +++ b/classes/BasicSpecialFunction.php @@ -6,7 +6,7 @@ class SpecialFunction extends AbstractSpecialFunction{ protected function getFunction($uri){ global $conf; $count = 1; - $prefixUri = $conf['basedir'].$conf['special']['uri']; + $prefixUri = $conf['basedir'].$conf['special']['uri']."/"; $aux = str_replace($prefixUri, '', $uri, $count); $functionAndParams = explode('/', $aux); return $functionAndParams[0]; @@ -26,54 +26,45 @@ class SpecialFunction extends AbstractSpecialFunction{ } public function execute($uri, $context){ + global $conf; $f = $this->getFunction($uri); $params = array(); $params = $this->getParams($uri); $params[] = $context; + $acceptContentType = Utils::getBestContentType($_SERVER['HTTP_ACCEPT']); + $extension = Utils::getExtension($acceptContentType); try{ - $methods = get_class_methods($this); - if(!in_array($f, $methods)){ + $viewFile = $conf['view']['directory'].$conf['special']['uri'].".".$f.$conf['view']['extension'].".".$extension; + $modelFile = $conf['model']['directory'].$conf['special']['uri'].".".$f.$conf['model']['extension'].".".$extension; + if(!is_file($modelFile) || !is_file($viewFile)){ throw new Exception('Method does not exist!'); - Utils::send500($uri); + Utils::send404($uri); + } + $e = $context['endpoint']; + $query = file_get_contents($modelFile); + array_pop($params); + array_shift($params); + $queryHeader = ""; + $prefixHeader = array(); + for($i=0;$i<sizeof($params);$i++){ + $auxPrefix = Utils::getPrefix($params[$i]); + if($auxPrefix['ns'] != NULL){ + $prefixHeader[$auxPrefix['ns']] = $auxPrefix['prefix']; + } + $query = preg_replace("|%".$i."|", $params[$i], $query); } - call_user_func_array(array($this, $f), $params); - }catch (Exception $e){ - Utils::send404($uri); + foreach($prefixHeader as $n => $p){ + $queryHeader .= "PREFIX $p: <$n> \n"; + } + $data['query'] =$queryHeader . $query; + $data['results'] = $e->query($data['query'], Utils::getResultsType($query)); + Utils::processDocument($uri, $acceptContentType, $data, $viewFile); + }catch (Exception $ex){ + Utils::send500($uri); } } - private function index($curie, $context){ - global $conf; - //Not sure if I should use files like for - //templating instances - //For now it will be "built-in" in the class - $uri = Utils::curie2uri($curie); - if(!isset($curie) || $curie == ""){ - $uri = "Class"; - $query = "SELECT DISTINCT ?resource WHERE{ - { - [] a ?resource - }UNION{ - ?resource a rdfs:Class - }UNION{ - ?resource a owl:Class - }UNION{ - ?resource rdfs:subClassOf [] - } - }"; - }else{ - $query = file_get_contents($conf['model']['directory'].'special.index.model.html'); - $query = preg_replace("|".$conf['resource']['url_delimiter']."|", "<".$uri.">", $query); - } - - $viewFile = $conf['view']['directory'].'special.index.view.html'; - $contentType = $context['contentType']; - $e = $context['endpoint']; - $data['results'] = $e->query($query, Utils::getResultsType($query)); - $data['query'] = $query; - Utils::processDocument($uri, $contentType, $data, $viewFile); - } } ?> diff --git a/classes/Utils.php b/classes/Utils.php index 773315ac3ea906afd38f5000ba560ea14c47f5e8..ff69178ee3fb9bef354e0dd4d9c30878c0e1c3ac 100644 --- a/classes/Utils.php +++ b/classes/Utils.php @@ -52,6 +52,17 @@ class Utils{ return $ns[$parts[0]].$parts[1]; } + public static function getPrefix($curie){ + global $conf; + $ns = $conf['ns']; + $parts = explode(':', $curie); + //Avoid if we have a namespace prefix called 'http' + if(preg_match('|^//|', $parts[1])){ + return $curie; + } + return array('ns' => $ns[$parts[0]], 'prefix' => $parts[0]);; + } + public static function getTemplate($uri){ $filename = str_replace(":", "_", $uri); if(file_exists ($filename)){ diff --git a/common.inc.php b/common.inc.php index e7feef23a6fe6b1cd5b7da8222cbe1ba8e3a315e..70ccb8771dc6c233cdbd957858b65a86f7b80066 100644 --- a/common.inc.php +++ b/common.inc.php @@ -26,7 +26,7 @@ $conf['http_accept']['ttl'] = array('text/n3', 'application/x-turtle', 'applica $conf['http_accept']['nt'] = array('text/plain'); -$conf['special']['uri'] = 'special/'; +$conf['special']['uri'] = 'special'; $conf['special']['class'] = 'classes/BasicSpecialFunction.php'; //Frontpage when user goes to http://example.org/ diff --git a/models/special.index.model.html b/models/special.index.model.html index e69381eac66c7b978b9a48449e1213f7379f60c8..e32cb2a17ecb5248772260b936078ae00a52624b 100644 --- a/models/special.index.model.html +++ b/models/special.index.model.html @@ -1,3 +1,3 @@ SELECT DISTINCT ?resource WHERE { - ?resource a %u . + [] a ?resource . } diff --git a/models/special.instances.model.html b/models/special.instances.model.html new file mode 100644 index 0000000000000000000000000000000000000000..e1d8da8469addee6864231e77b83093c267695f1 --- /dev/null +++ b/models/special.instances.model.html @@ -0,0 +1,3 @@ +SELECT DISTINCT ?resource WHERE { + ?resource a %0 . +} diff --git a/views/special.index.view.html b/views/special.index.view.html index af716e7e608d3dde09ebd6609b5ed5409797d3f4..5e20316140177942e5a105a414ab57b3ad0f0036 100644 --- a/views/special.index.view.html +++ b/views/special.index.view.html @@ -12,7 +12,7 @@ <ul> {% for row in r %} {% if row.value.resource != null %} - <li><a href='{{ row.value.resource }}'>{{row.curie.resource}}</a></li> + <li><a href='{{base.baseUrl}}special/instances/{{ row.curie.resource }}'>{{row.curie.resource}}</a></li> {% endif %} {% endfor %} </ul> diff --git a/views/special.instances.view.html b/views/special.instances.view.html new file mode 100644 index 0000000000000000000000000000000000000000..af716e7e608d3dde09ebd6609b5ed5409797d3f4 --- /dev/null +++ b/views/special.instances.view.html @@ -0,0 +1,21 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" + "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd"> + <html xmlns="http://www.w3.org/1999/xhtml" {% for i, ns in base.ns %}xmlns:{{i}}="{{ns}}" + {%endfor%}version="XHTML+RDFa 1.0" xml:lang="en"> + <head> + <title>Instance of {{base.this.curie}}</title> + <link href="{{base.baseUrl}}/lodspeakr/css/basic.css" rel="stylesheet" type="text/css" media="screen" /> + </head> + <body> + <h1>Instances of {{base.this.curie}}</h1> + <ul> + {% for row in r %} + {% if row.value.resource != null %} + <li><a href='{{ row.value.resource }}'>{{row.curie.resource}}</a></li> + {% endif %} + {% endfor %} + </ul> + </body> +</html> +