Skip to content
Snippets Groups Projects

Resolve "Remove all use of GLOBALS"

Merged Stein Magne Bjorklund requested to merge 13-remove-all-use-of-globals into master
1 file
+ 37
45
Compare changes
  • Side-by-side
  • Inline
+ 37
45
@@ -17,13 +17,8 @@ final class Endpoint
public function query($q, $output = 'json')
{
global $conf;
if (!empty($this->params['output'])) {
$auxoutput = $this->params['output'];
}
$accept = 'application/sparql-results+json';
$this->params['output'] = $output;
$modified = 0;
if ($output === 'xml') {
$accept = 'application/sparql-results+xml';
@@ -31,11 +26,7 @@ final class Endpoint
$accept = 'application/rdf+xml';
}
$modified = 0;
$now = time();
$cacheFile = "";
if (!empty($conf['cache']['global']) && is_int($conf['cache']['global']) && $conf['cache']['global'] > 0) {
if ($this->caching($conf)) {
$cacheFile = $conf['home'] . "cache/query" . md5($this->sparqlUrl . $q);
if (file_exists($cacheFile)) {
$modified = filemtime($cacheFile);
@@ -43,46 +34,23 @@ final class Endpoint
}
/**
* If we have cached content use it if not make a https request.
* If we have cached content use it, if not make a https request.
*/
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";
Logging::log($msg);
echo $msg;
}
if ($this->caching($conf) && $modified + $conf['cache']['global'] > time()) {
$aux = (file_get_contents($cacheFile));
} else {
$result = $this->curl($q, $accept, $conf);
$aux = $result['result'];
$http_status = $result['status'];
if (is_int($conf['cache']['global']) && $conf['cache']['global'] > 0 && $http_status == 200) {
file_put_contents($cacheFile, ($aux), LOCK_EX);
if ($conf['debug']) {
$msg = "Notice: Writing results in " . $cacheFile . "\n";
Logging::log($msg, E_USER_NOTICE);
echo($msg);
}
}
$this->updateCache($result['status'], $cacheFile, $aux, $conf);
}
if (false !== stripos($q, "select")) {
$r = json_decode($aux, true);
if ($conf['debug']) {
if ($r == false) {
Logging::log("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 $this->result($q, $aux);
}
return $r;
private function result($q, $aux)
{
if (false !== stripos($q, "select")) {
return json_decode($aux, true);
}
if (false !== stripos($q, "describe")) {
@@ -94,9 +62,33 @@ final class Endpoint
}
if (false !== stripos($q, "ask")) {
$r = json_decode($aux, true);
return $r;
return json_decode($aux, true);
}
return $aux;
}
private function caching(array $conf): bool
{
return !empty($conf['cache']['global']) && is_int($conf['cache']['global']) && $conf['cache']['global'] > 0;
}
private function updateCache($http_status, $cacheFile, $aux, $conf)
{
$result = false;
if ($this->caching($conf) && $http_status == 200) {
$result = file_put_contents($cacheFile, ($aux), LOCK_EX);
if (!$result) {
throw new \UnexpectedValueException(
'Unable to update cache: ' . $cacheFile
);
}
}
return $result;
}
private function curl(string $request, $accept, array $conf): array
Loading