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
+ 10
7
Compare changes
  • Side-by-side
  • Inline
+ 94
87
<?php
<?php declare(strict_types=1);
namespace uib\ub\loadspeakr;
class Endpoint
use UnexpectedValueException;
final class Endpoint
{
private $sparqlUrl;
private $params;
private string $sparqlUrl;
private array $params;
public function __construct($sparqlUrl, array $params)
public function __construct(string $sparqlUrl, array $params)
{
$this->sparqlUrl = $sparqlUrl;
$this->params = $params;
@@ -17,112 +19,117 @@ class Endpoint
public function query($q, $output = 'json')
{
global $conf;
if (!empty($this->params['output'])) {
$auxoutput = $this->params['output'];
}
$accept = 'application/sparql-results+json';
if ($output != null) {
$this->params['output'] = $output;
}
if ($output == 'xml') {
$modified = 0;
$aux = '';
if ($output === 'xml') {
$accept = 'application/sparql-results+xml';
} elseif ($output == 'rdf') {
} elseif ($output === 'rdf') {
$accept = 'application/rdf+xml';
}
$modified = 0;
$now = time();
$cacheFile = "";
if (!empty($conf['cache']['global']) && is_int($conf['cache']['global']) && $conf['cache']['global'] > 0) {
$cacheFile = $conf['home'] . "cache/query" . md5($this->sparqlUrl . $q);
$cacheFile = $conf['home'] . "cache/query" . md5($this->sparqlUrl . $q);
if ($this->caching($conf)) {
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";
Logging::log($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) {
Logging::log(
"Error executing SPARQL query (" . $this->sparqlUrl . "): " . curl_error($c),
E_USER_ERROR
);
echo("Error executing SPARQL query (" . $this->sparqlUrl . "): " . curl_error($c));
}
}
$http_status = intval(curl_getinfo($c, CURLINFO_HTTP_CODE));
curl_close($c);
if (!empty($auxoutput)) {
$this->params['output'] = $auxoutput;
}
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);
}
/**
* If we have cached content lets use it.
*/
if ($modified + $conf['cache']['global'] > time()) {
$aux = file_get_contents($cacheFile);
}
}
if (preg_match("/select/i", $q)) {
$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 $r;
if (!$aux) {
$result = $this->curl($q, $accept, $conf);
$aux = $result['result'];
$this->updateCache($result['status'], $cacheFile, $aux, $conf);
}
if (!$aux) {
return $aux;
}
return $this->result($q, $aux);
}
private function result(string $q, string $aux)
{
if (false !== stripos($q, "select")) {
return json_decode($aux, true);
}
if (preg_match("/describe/i", $q)) {
if (false !== stripos($q, "describe")) {
return $aux;
}
if (preg_match("/construct/i", $q)) {
if (false !== stripos($q, "construct")) {
return $aux;
}
if (preg_match("/ask/i", $q)) {
$r = json_decode($aux, true);
return $r;
if (false !== stripos($q, "ask")) {
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;
}
public function queryPost($q)
private function updateCache($http_status, $cacheFile, $aux, $conf): void
{
if ($http_status === 200 && $this->caching($conf)) {
$result = file_put_contents($cacheFile, ($aux), LOCK_EX);
if (!$result) {
throw new UnexpectedValueException(
'Unable to update cache: ' . $cacheFile
);
}
}
}
private function curl(string $request, $accept, array $conf): array
{
$resource = curl_init();
$context = ['Connection: close', 'Accept: ' . $accept];
$params = $this->params;
$params['query'] = $q;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->sparqlUrl);
curl_setopt($ch, CURLOPT_POST, count($params));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//execute post
$result = curl_exec($ch);
return $result;
$params['query'] = $request;
$url = $this->sparqlUrl . '?' . http_build_query($params, '', '&');
curl_setopt($resource, CURLOPT_URL, $url);
curl_setopt($resource, CURLOPT_HTTPHEADER, $context);
curl_setopt($resource, CURLOPT_USERAGENT, "LODSPeaKr version " . $conf['version']);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($resource);
$http_status = (int)curl_getinfo($resource, CURLINFO_HTTP_CODE);
curl_close($resource);
return [
'result' => $result,
'status' => $http_status,
];
}
public function queryPost(string $request)
{
$params = $this->params;
$params['query'] = $request;
$resource = curl_init();
curl_setopt($resource, CURLOPT_URL, $this->sparqlUrl);
curl_setopt($resource, CURLOPT_POST, count($params));
curl_setopt($resource, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($resource, CURLOPT_RETURNTRANSFER, 1);
return curl_exec($resource);
}
public function getSparqlURL()
public function getSparqlURL(): string
{
return $this->sparqlUrl;
}
Loading