Skip to content
Snippets Groups Projects
Commit 189d1800 authored by alvaro's avatar alvaro
Browse files

Merge remote-tracking branch 'upstream/master'

parents 59b65d87 52a9f5b0
No related branches found
No related tags found
No related merge requests found
...@@ -9,59 +9,88 @@ class Endpoint{ ...@@ -9,59 +9,88 @@ class Endpoint{
$this->params = $params; $this->params = $params;
} }
public function query($q, $output = 'json'){ public function query($q, $output = 'json'){
global $conf; global $conf;
$auxoutput = $this->params['output']; $auxoutput = $this->params['output'];
$accept = 'application/sparql-results+json'; $accept = 'application/sparql-results+json';
if($output != null){ if($output != null){
$this->params['output'] = $output; $this->params['output'] = $output;
} }
if($output == 'xml'){ if($output == 'xml'){
$accept = 'application/sparql-results+xml'; $accept = 'application/sparql-results+xml';
}elseif($output == 'rdf'){ }elseif($output == 'rdf'){
$accept = 'application/rdf+xml'; $accept = 'application/rdf+xml';
}
$aux = "";
$modified = 0;
$now = time();
$cacheFile = "";
if(is_int($conf['cache']['global']) && $conf['cache']['global'] > 0){
$cacheFile = $conf['home']."cache/query".md5($this->sparqlUrl.$q);
if(file_exists($cacheFile)){
$modified = filemtime($cacheFile);
}
}
if(is_int($conf['cache']['global']) && $conf['cache']['global'] > 0 && $modified + $conf['cache']['global'] > $now){
if($conf['debug']){
$msg = "Taking data from cache ($cacheFile). Renewal in ".($modified + $conf['cache']['global'] - $now)." seconds\n";
trigger_error($msg);
echo $msg;
}
$aux = (file_get_contents($cacheFile));
}else{
$c = curl_init();
$context = array();
$context[0] = 'Connection: close';
$context[1] = 'Accept: '.$accept;
$params = $this->params;
$params['query'] = $q;
$url = $this->sparqlUrl.'?'.http_build_query($params, '', '&');
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_HTTPHEADER, $context);
curl_setopt($c, CURLOPT_USERAGENT, "LODSPeaKr version ".$conf['version']);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$aux = curl_exec($c); // execute the curl command
if($conf['debug']){
if($aux == false){
trigger_error("Error executing SPARQL query (".$this->sparqlUrl."): ".curl_error($c), E_USER_ERROR);
echo("Error executing SPARQL query (".$this->sparqlUrl."): ".curl_error($c));
} }
$c = curl_init(); }
$context = array(); curl_close($c);
$context[0] = 'Connection: close'; $this->params['output'] = $auxoutput;
$context[1] = 'Accept: '.$accept; if(is_int($conf['cache']['global']) && $conf['cache']['global'] > 0){
$params = $this->params; file_put_contents($cacheFile,($aux), LOCK_EX);
$params['query'] = $q;
$url = $this->sparqlUrl.'?'.http_build_query($params, '', '&');
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_HTTPHEADER, $context);
curl_setopt($c, CURLOPT_USERAGENT, "LODSPeaKr version ".$conf['version']);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$aux = curl_exec($c); // execute the curl command
if($conf['debug']){ if($conf['debug']){
if($aux == false){ $msg = "Notice: Writing results in ".$cacheFile."\n";
trigger_error("Error executing SPARQL query (".$this->sparqlUrl."): ".curl_error($c), E_USER_ERROR); trigger_error($msg, E_USER_NOTICE);
echo("Error executing SPARQL query (".$this->sparqlUrl."): ".curl_error($c)); echo($msg);
}
}
curl_close($c);
$this->params['output'] = $auxoutput;
if(preg_match("/select/i", $q)){
$r = json_decode($aux, true);
if($conf['debug']){
if($r == false){
trigger_error("Warning: Results from a SELECT sparql query couldn't get parsed", E_USER_WARNING);
echo("Warning: Results from a SELECT sparql query couldn't get parsed");
}
}
return $r;
}
if(preg_match("/describe/i", $q)){
return $aux;
} }
if(preg_match("/construct/i", $q)){ }
return $aux; }
} if(preg_match("/select/i", $q)){
if(preg_match("/ask/i", $q)){ $r = json_decode($aux, true);
$r = json_decode($aux, true); if($conf['debug']){
return $r; if($r == false){
trigger_error("Warning: Results from a SELECT sparql query couldn't get parsed", E_USER_WARNING);
echo("Warning: Results from a SELECT sparql query couldn't get parsed");
} }
}
return $r;
}
if(preg_match("/describe/i", $q)){
return $aux;
}
if(preg_match("/construct/i", $q)){
return $aux;
} }
if(preg_match("/ask/i", $q)){
$r = json_decode($aux, true);
return $r;
}
}
public function queryPost($q){ public function queryPost($q){
$params = $this->params; $params = $this->params;
......
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