Skip to content
Snippets Groups Projects
Commit f4ae66ca authored by Alvaro Graves's avatar Alvaro Graves
Browse files

Rewrite functions as classes

Very limited functionality so far but it works.

TODO: Debug flag
parent ed641565
No related branches found
No related tags found
No related merge requests found
<?
class Endpoint{
private $sparqlUrl;
private $params;
public function __construct($sparqlUrl, $params){
$this->sparqlUrl = $sparqlUrl;
$this->params = $params;
}
public function query($q){
$context = stream_context_create(array(
'http' => array('header'=>'Connection: close')));
$params = $this->params;
$params['query'] = $q;
$url = $this->sparqlUrl.'?'.http_build_query($params, '', '&');
$aux = file_get_contents($url, false,$context);
return json_decode($aux, true);
}
}
?>
<?
class Queries{
public static function uriExist($uri, $e){
$q = "SELECT * WHERE{
{<$uri> ?p1 ?o1}
UNION
{?s1 <$uri> ?o2}
UNION
{?s2 ?p2 <$uri>}
}LIMIT 1";
$r = $e->query($q);
if(sizeof($r['results']['bindings'])>0){
return true;
}
return false;
}
public static function getClass($uri, $e){
$q = "SELECT DISTINCT ?class WHERE{
<$uri> a ?class .
} LIMIT 1";
$r = $e->query($q);
if(sizeof($r['results']['bindings'])>0){
return $r['results']['bindings'][0]['class']['value'];
}
return NULL;
}
public static function getMetaData($uri, $e){
global $conf;
$named_graph = $conf['metaendpoint']['config']['named_graph'];
$q = <<<QUERY
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?page ?uri WHERE{
GRAPH <$named_graph>{
?s ?p ?o . #Stupid dummy triple to make it work with ARC2
OPTIONAL{?page foaf:primaryTopic <$uri>} .
OPTIONAL{<$uri> foaf:primaryTopic ?uri} .
}
}LIMIT 1
QUERY;
$r = $e->query($q);
if(sizeof($r['results']['bindings'])>0){
$u = (isset($r['results']['bindings'][0]['uri']))?$r['results']['bindings'][0]['uri']['value']:NULL;
$p = (isset($r['results']['bindings'][0]['page']))?$r['results']['bindings'][0]['page']['value']:NULL;
return array($u, $p);
}
return NULL;
}
}
?>
<?
class Utils{
public static function send404($uri){
header("HTTP/1.0 404 Not Found");
echo "I could not find ".$uri." or information about it";
exit(0);
}
public static function send303($uri){
header("HTTP/1.0 303 See Other");
header("Location: ".$uri);
exit(0);
}
public static function uri2curie($uri){
global $conf;
$ns = $conf['ns'];
$curie = $uri;
foreach($ns as $k => $v){
$curie = preg_replace("|^$v|", "$k:", $uri);
if($curie != $uri){
break;
}
}
return $curie;
}
public static function getTemplate($uri){
$filename = str_replace(":", "_", $uri);
if(file_exists ($filename)){
include_once($filename);
}
}
public static function showView($uri, $data, $view){
global $conf;
$body = file_get_contents($view);
$r = $data['results']['bindings'];
if(sizeof($r)> 0){
foreach($r as $v){
foreach($v as $k => $w){
$body = preg_replace("|%".$k."|", $w['value'], $body);
echo $html;
}
}
}
$html = preg_replace("|".$conf['resource']['url_delimiter']."|", $uri, $body);
echo $html;
}
}
?>
<?
include_once('settings.inc.php');
function query($q){
global $conf;
$endpoint = $conf['endpoint']['host'];
$params = $conf['endpoint']['config'];
$params['query'] = $q;
$url = $endpoint.'?'.http_build_query($params, '', '&');
return json_decode(file_get_contents($url), true);
}
function getClass($uri){
$q = "SELECT DISTINCT ?class WHERE{
<$uri> a ?class .
} LIMIT 1";
$r = query($q);
if(sizeof($r['results']['bindings'])>0){
return $r['results']['bindings'][0]['class']['value'];
}
return NULL;
}
function uri2curie($uri){
global $conf;
$ns = $conf['ns'];
$curie = $uri;
foreach($ns as $k => $v){
$curie = preg_replace("|^$v|", "$k:", $uri);
if($curie != $uri){
break;
}
}
return $curie;
}
function getTemplate($uri){
$filename = str_replace(":", "_", $uri);
if(file_exists ($filename)){
include_once($filename);
}
}
function send404($uri){
header("HTTP/1.0 404 Not Found");
echo "I could not find ".$uri." or information about it";
exit(0);
}
function showView($uri, $body){
global $conf;
$html = preg_replace("|".$conf['resource']['url_delimiter']."|", $uri, $body);
echo $html;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment