Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
marcus-lodspeakr
Manage
Activity
Members
Labels
Plan
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
uib-ub
Spesialsamlingene
marcus-lodspeakr
Merge requests
!18
Resolve "Remove all use of GLOBALS"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resolve "Remove all use of GLOBALS"
13-remove-all-use-of-globals
into
master
Overview
0
Commits
72
Pipelines
0
Changes
1
Merged
Stein Magne Bjorklund
requested to merge
13-remove-all-use-of-globals
into
master
3 years ago
Overview
0
Commits
72
Pipelines
0
Changes
1
Expand
Closes #13
Edited
3 years ago
by
Stein Magne Bjorklund
0
0
Merge request reports
Viewing commit
f95c0383
Prev
Next
Show latest version
1 file
+
10
−
7
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
f95c0383
Refactor Endpoint::query() make sure caching is correctly set up
· f95c0383
Stein Magne Bjorklund
authored
3 years ago
classes/Endpoint.php
+
94
−
87
Options
<?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
;
}
p
ublic
function
queryPost
(
$q
)
p
rivate
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