<?php
// src/Controller/SiteController.php
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
class SiteController extends DMAbstractController
{
function jocondeSiteDescription($desc, $jsonld, $debug=false)
{
$lang = "fr";
$jsonDescription = json_decode($jsonld, TRUE);
$descstruct["uri"] = $desc["uri"];
$descstruct["jsonld"] = "";
$descstruct["entityid"] = "????";
$descstruct["altLabel"] = array();
$descstruct["triplestore"] = "https://kg.grains-de-culture.fr/";
$descstruct["dataset"] = "SemJoconde";
$descstruct["uriville"] = "";
$descstruct["ville"] = "?";
$descstruct["latitude"] = "?";
$descstruct["longitude"] = "?";
$descstruct["sameAs"] = array();
$descstruct["query"] = "";
if ($jsonDescription){
$descstruct = array();
// types devraient être ajoutés au json-ld comme attribut @type
$descstruct["@type"] = array();
if (array_key_exists("P31", $jsonDescription)) {
if (is_array($jsonDescription["P31"])) {
foreach ($jsonDescription["P31"] as $isA) {
array_push($descstruct["@type"], $isA);
}
} else array_push($descstruct["@type"], $jsonDescription["P31"]);
}
$descstruct["jsonld"] = $jsonld; //str_replace(""", '"', $jsonld);
if (array_key_exists("@id", $jsonDescription)){
$descstruct["uri"] = $jsonDescription["@id"];
}else {
$descstruct["uri"] = "";
}
$descstruct["entityid"] = "????";
if (array_key_exists("prefLabel", $jsonDescription)){
if(is_object($jsonDescription["prefLabel"])) {
foreach ($jsonDescription["prefLabel"] as $pLabel) {
$newlabel = "";
if (array_key_exists("@language", $pLabel)) {
if ($pLabel["@language"] == $lang) {
$newlabel = $pLabel["@value"];
break;
}
}
}
}
else {
// Checking if $jsonDescription["prefLabel"] is sequentialor asociative array
$arr = $jsonDescription["prefLabel"];
if (array_keys($arr) !== range(0, count($arr) - 1)) {
// associative array
$newlabel = "";
if (array_key_exists("@language", $jsonDescription["prefLabel"])) {
$newlabel = "titi";
if ($jsonDescription["prefLabel"]["@language"] == $lang) {
$newlabel = $jsonDescription["prefLabel"]["@value"];
}
}
} else {
// sequential array
foreach ($jsonDescription["prefLabel"] as $pLabel) {
$newlabel = "";
if (array_key_exists("@language", $pLabel)) {
if ($pLabel["@language"] == $lang) {
$newlabel = $pLabel["@value"];
break;
}
}
}
}
}
} else {
@$newlabel = "";
}
if ($newlabel!="") {
$descstruct["entityid"] = $newlabel;
}
$descstruct["altLabel"] = array();
if (array_key_exists("label", $jsonDescription)) {
foreach ($jsonDescription["label"] as $label) {
$newlabel = "";
if (is_array($label)) {
if ($label["@language"] == $lang) {
$newlabel = $label["@value"];
}
} else $newlabel = $label;
if (($newlabel != "") and (!in_array($newlabel, $descstruct["altLabel"]))) {
array_push($descstruct["altLabel"], $newlabel);
}
}
}
$descstruct["dataset"] = "SemJoconde";
$descstruct["triplestore"] = "https://kg.grains-de-culture.fr/";
$descstruct["uriville"] = "";
if (array_key_exists("city", $jsonDescription)) {
if (is_array($jsonDescription["city"])) $descstruct["ville"] = $jsonDescription["city"]["@value"];
else $descstruct["ville"] = $jsonDescription["city"];
} else {
$descstruct["ville"] = "";
}
if(array_key_exists("http://www.w3.org/2003/01/geo/wgs84_pos#lat", $jsonDescription)) {
$lat = $jsonDescription["http://www.w3.org/2003/01/geo/wgs84_pos#lat"];
echo($lat[0]);
if (is_array($lat)) $descstruct["latitude"] = "!"; //$lat[0];
else $descstruct["latitude"] = "!!";//$lat;
} else $descstruct["latitude"] = "?";
if(array_key_exists("http://www.w3.org/2003/01/geo/wgs84_pos#long", $jsonDescription)) {
$lon = $jsonDescription["http://www.w3.org/2003/01/geo/wgs84_pos#long"];
if (is_array($lon)) $descstruct["longitude"] = "&";//$lon[0];
else $descstruct["longitude"] = "&&";//$lon;
} else $descstruct["longitude"] = "?";
$descstruct["sameAs"] = array();
if(array_key_exists("sameAs", $jsonDescription)) {
if (is_array($jsonDescription["sameAs"])) {
foreach ($jsonDescription["sameAs"] as $same) {
array_push($descstruct["sameAs"], $same);
}
} else array_push($descstruct["sameAs"], $jsonDescription["sameAs"]);
}
}
$descstruct["countcontrib"] = 0;
$descstruct["countuser"] = 0;
$descstruct["countcontrib"] = 0;
$descstruct["entitytype"] = "Site culturel";
if ($jsonDescription) {
if (array_key_exists("@id", $jsonDescription)) {
$count = self::getGuestBookContribCount("https://kg.grains-de-culture.fr/dm/sparql", $jsonDescription["@id"]);
$descstruct["countcontrib"] = $count["countcontrib"];
$descstruct["countuser"] = $count["countuser"];
$descstruct["countcontrib"] = $count["countcontrib"];
}
/*
*
$sellcount = self::getSellActionCount("https://kg.grains-de-culture.fr/dm/query",
str_replace("http://datamusee.givingsense.eu/onto/site/",
"http://datamusee.givingsense.eu/graph/billetterie/site/",
$jsonDescription["@id"]));
$descstruct["salescount"] = $sellcount["salescount"];
$descstruct["productscount"] = $sellcount["productscount"];
$descstruct["buyerscount"] = $sellcount["buyerscount"];
*/
if (array_key_exists("@id", $jsonDescription)) {
$descstruct["class"] = $this->getClasses($jsonDescription["@id"], $desc["endpoint"]);
} else {
$descstruct["class"] = array();
}
$descstruct["urljson"] = $descstruct["uri"] . ".json";
$yasguilink = "";
if (array_key_exists("@id", $jsonDescription)) {
$yasguilink = "https://grains-de-culture.fr/fuseki/ui/#"
. 'query=' . urlencode(
str_replace("construct { ?s ?p ?o ; a ?t}", "select ?s ?p ?o", self::getElementaryQuery($jsonDescription["@id"])))
. '&contentTypeConstruct=' . urlencode("text/turtle")
. '&contentTypeSelect=' . urlencode("application/sparql-results+json")
. '&endpoint=' . urlencode($desc["endpoint"])
. '&requestMethod=POST'
. '&tabTitle=' . urlencode("Propriétés")
. '&headers=%7B%7D'
. '&outputFormat=table';
}
$descstruct["yasguilink"] = $yasguilink;
$descstruct["entitytype"] = "Site culturel";
$template = 'site/dmjocsite.html.twig';
}
if ($debug) $template = "testTemplateSite.html.twig";
$response = $this->render($template, [
//return $this->render('testTemplateSite.html.twig', [
'desc' => $desc,
'descstruct' => $descstruct,
'entitytype' => $descstruct["entitytype"],
'description' => ""//$description
]);
return $response;
}
function newSiteDescription($desc, $jsonld, $debug=false)
{
$lang = "fr";
$jsonDescription = json_decode($jsonld, TRUE);
$descstruct["uri"] = $desc["uri"];
$descstruct["uri"] = $desc["uri"];
$descstruct["jsonld"] = $jsonld;
$descstruct["entityid"] = "????";
$descstruct["altLabel"] = array();
$descstruct["triplestore"] = "https://kg.grains-de-culture.fr/";
$descstruct["dataset"] = "SemJoconde";
$descstruct["uriville"] = "";
$descstruct["ville"] = "?";
$descstruct["latitude"] = "?";
$descstruct["longitude"] = "?";
$descstruct["sameAs"] = array();
$descstruct["query"] = "";
if ($jsonDescription){
$descstruct = array();
$descstruct["query"] = $desc["query"];
// types devraient être ajoutés au json-ld comme attribut @type
$descstruct["@type"] = array();
if (array_key_exists("P31", $jsonDescription)) {
if (is_array($jsonDescription["P31"])) {
foreach ($jsonDescription["P31"] as $isA) {
array_push($descstruct["@type"], $isA);
}
} else array_push($descstruct["@type"], $jsonDescription["P31"]);
}
$descstruct["jsonld"] = $jsonld; //str_replace(""", '"', $jsonld);
if (array_key_exists("@id", $jsonDescription)){
$descstruct["uri"] = $jsonDescription["@id"];
} else {
$descstruct["uri"] = "";
}
$descstruct["entityid"] = "??????";
if (array_key_exists("skos:prefLabel", $jsonDescription)){
// $descstruct["entityid"] = "!!!!!!";
if(is_object($jsonDescription["skos:prefLabel"])) {
// $descstruct["entityid"] = "!?!?!?";
foreach ($jsonDescription["skos:prefLabel"] as $pLabel) {
$newlabel = "";
if (array_key_exists("@language", $pLabel)) {
if ($pLabel["@language"] == $lang) {
$newlabel = $pLabel["@value"];
break;
}
}
}
}
else {
//$descstruct["entityid"] = ";;;;;";
// Checking if $jsonDescription["prefLabel"] is sequentialor asociative array
$arr = $jsonDescription["skos:prefLabel"];
if (array_keys($arr) !== range(0, count($arr) - 1)) {
//$descstruct["entityid"] = "$$$$$$";
// associative array
$newlabel = "";
if (array_key_exists("@language", $jsonDescription["skos:prefLabel"])) {
$newlabel = "titi";
if ($jsonDescription["skos:prefLabel"]["@language"] == $lang) {
$newlabel = $jsonDescription["skos:prefLabel"]["@value"];
}
}
} else {
//$descstruct["entityid"] = "******";
// sequential array
foreach ($jsonDescription["skos:prefLabel"] as $pLabel) {
$newlabel = "";
if (is_array($pLabel)) {
if (array_key_exists("@language", $pLabel)) {
if ($pLabel["@language"] == $lang) {
$newlabel = $pLabel["@value"];
break;
}
}
} else {
$descstruct["entityid"] = $pLabel;
$newlabel = $pLabel;
}
}
}
}
} else {
@$newlabel = "";
}
if ($newlabel!="") {
$descstruct["entityid"] = $newlabel;
}
$descstruct["altLabel"] = array();
if (array_key_exists("rdfs:label", $jsonDescription)) {
foreach ($jsonDescription["rdfs:label"] as $label) {
$newlabel = "";
if (is_array($label)) {
if ($label["@language"] == $lang) {
$newlabel = $label["@value"];
}
} else $newlabel = $label;
if (($newlabel != "") and (!in_array($newlabel, $descstruct["altLabel"]))) {
array_push($descstruct["altLabel"], $newlabel);
}
}
}
$descstruct["dataset"] = "dmsites";
$descstruct["triplestore"] = "https://kg.grains-de-culture.fr/";
$descstruct["uriville"] = "_";
if (array_key_exists("city", $jsonDescription)) {
if (is_array($jsonDescription["city"])) $descstruct["ville"] = $jsonDescription["city"]["@value"];
else $descstruct["ville"] = $jsonDescription["city"];
} else {
$descstruct["ville"] = "?";
}
if(array_key_exists("http://www.w3.org/2003/01/geo/wgs84_pos#lat", $jsonDescription)) {
$lat = $jsonDescription["http://www.w3.org/2003/01/geo/wgs84_pos#lat"];
if (is_array($lat)) $descstruct["latitude"] = floatval($lat["@value"]);
else $descstruct["latitude"] = floatval($lat["@value"]);
} else $descstruct["latitude"] = "?";
if(array_key_exists("http://www.w3.org/2003/01/geo/wgs84_pos#long", $jsonDescription)) {
$lon = $jsonDescription["http://www.w3.org/2003/01/geo/wgs84_pos#long"];
if (is_array($lon)) $descstruct["longitude"] = floatval($lon["@value"]);
else $descstruct["longitude"] = floatval($lon["@value"]);
} else $descstruct["longitude"] = "?";
$descstruct["sameAs"] = array();
if(array_key_exists("sameAs", $jsonDescription)) {
if (is_array($jsonDescription["sameAs"])) {
foreach ($jsonDescription["sameAs"] as $same) {
array_push($descstruct["sameAs"], $same);
}
} else array_push($descstruct["sameAs"], $jsonDescription["sameAs"]);
}
}
$descstruct["countcontrib"] = 0;
$descstruct["countuser"] = 0;
$descstruct["countcontrib"] = 0;
if(array_key_exists("@id", $jsonDescription)) {
$count = self::getGuestBookContribCount("https://kg.grains-de-culture.fr/dm/sparql", $jsonDescription["@id"]);
$descstruct["countcontrib"] = $count["countcontrib"];
$descstruct["countuser"] = $count["countuser"];
$descstruct["countcontrib"] = $count["countcontrib"];
}
/*
*
$sellcount = self::getSellActionCount("https://kg.grains-de-culture.fr/dm/query",
str_replace("http://datamusee.givingsense.eu/onto/site/",
"http://datamusee.givingsense.eu/graph/billetterie/site/",
$jsonDescription["@id"]));
$descstruct["salescount"] = $sellcount["salescount"];
$descstruct["productscount"] = $sellcount["productscount"];
$descstruct["buyerscount"] = $sellcount["buyerscount"];
*/
if(array_key_exists("@id", $jsonDescription)) {
$descstruct["class"] = $this->getClasses($jsonDescription["@id"], $desc["endpoint"]);
} else {
$descstruct["class"] = array();
}
$descstruct["urljson"] = $descstruct["uri"].".json";
$yasguilink = "";
if(array_key_exists("@id", $jsonDescription)) {
$yasguilink = "https://grains-de-culture.fr/fuseki/ui/#"
. 'query=' . urlencode(
str_replace("construct { ?s ?p ?o ; a ?t}", "select ?s ?p ?o", self::getElementaryQuery($jsonDescription["@id"])))
. '&contentTypeConstruct=' . urlencode("text/turtle")
. '&contentTypeSelect=' . urlencode("application/sparql-results+json")
. '&endpoint=' . urlencode($desc["endpoint"])
. '&requestMethod=POST'
. '&tabTitle=' . urlencode("Propriétés")
. '&headers=%7B%7D'
. '&outputFormat=table';
}
$descstruct["yasguilink"] = $yasguilink;
$descstruct["entitytype"] = "Site culturel";
$template = 'site/dmsite.html.twig';
if ($debug) $template = "testTemplateSite.html.twig";
$response = $this->render($template, [
//return $this->render('testTemplateSite.html.twig', [
'desc' => $desc,
'descstruct' => $descstruct,
'entitytype' => $descstruct["entitytype"],
'description' => ""//$description
]);
return $response;
}
function getPrefixes()
{
$prefixes = <<<txt
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
prefix cmn: <http://givingsense.eu/datamusee/onto/musee/cmn/>
txt;
return $prefixes;
}
function getClasses($uriclass, $endpoint)
{
$user = $this->getParameter('app.sparql_user');
$pass = $this->getParameter('app.sparql_pass');
$auth = $user . ":" . $pass; // le dataset dmsites devrait être en libre accès
$classTemplate = self::getPrefixes() . <<<txt
SELECT distinct ?o ?label ?super ?superlabel
WHERE {
<__uri__> wdt:P31 ?o .
?o <http://www.wikidata.org/prop/direct/P279>* ?super.
?o rdfs:label ?label .
?super rdfs:label ?superlabel .
bind(lang(?superlabel) as ?lang)
filter((lang(?superlabel)='en') ||(lang(?superlabel)='en') )
}
txt;
$queryClasses = str_replace( "__uri__" , $uriclass , $classTemplate);
$requestURL = $endpoint.'?'
.'query='.urlencode($queryClasses)
.'&format=json';
$classes = json_decode(self::request($requestURL, $auth), true);
$classArray = array();
foreach ($classes["results"]["bindings"] as $item) {
$lang = "";
if(array_key_exists("xml:lang", $item["label"]))
$lang = $item["label"]["xml:lang"];
if ($lang=="en") $classArray[$item["o"]["value"]] = [ "class"=> $item["o"]["value"], "label" =>$item["label"]["value"], "lang"=>$lang];
if(array_key_exists("super", $item)){
if(array_key_exists("xml:lang", $item["superlabel"])) $lang = $item["superlabel"]["xml:lang"]; else $lang = "";
if ($lang=="en")
$classArray[$item["super"]["value"]] = [ "class"=> $item["super"]["value"], "label" =>$item["superlabel"]["value"], "lang"=>$lang];
}
}
return $classArray;
//return array($queryClasses);
}
function getGuestBookContribCount($endpoint, $urisite)
{
$sparqltemplate = <<<txt
PREFIX schema: <http://schema.org/>
select (count(distinct ?s) as ?cl)(count(distinct ?user) as ?cu)
where
{
values ?graph { <http://datamusee.givingsense.eu/livredor/parismusees> <http://datamusee.givingsense.eu/livredor/cmn> }
graph ?graph {
?app schema:about <__uri__> .
?s a <http://datamusee.givingsense.eu/onto/contribution>;
<http://datamusee.givingsense.eu/onto/contribution/hasUser> ?user;
<http://datamusee.givingsense.eu/onto/contribution/hasAppli> ?app .
}
}
txt;
$format = "application/sparql-results+json";
$query = str_replace( "__uri__" , $urisite , $sparqltemplate);
$user = $this->getParameter('app.sparql_user');
$pass = $this->getParameter('app.sparql_pass');
$auth = $user . ":" . $pass; // le dataset dmsites devrait être en libre accès
$requestURL = $endpoint.'?'
.'query='.urlencode($query)
.'&format='.urlencode($format);
$res = json_decode(self::request($requestURL, $auth), true);
$count = array(); $count["countcontrib"] = $count["countuser"] = 0;
if ($res) {
$count["countcontrib"] = $res["results"]["bindings"][0]["cl"]["value"];
$count["countuser"] = $res["results"]["bindings"][0]["cu"]["value"];
}
//return $query;
return $count;
}
function getSellActionCount($endpoint, $graph)
{
$sparqltemplate = <<<txt
PREFIX schema: <http://schema.org/>
select (count(distinct ?s) as ?sellactioncount) (count(distinct ?buyer) as ?buyerscount) (count(distinct ?product) as ?productscount)
{
graph <__uri__> {
?s a schema:SellAction ;
schema:buyer ?buyer ;
<http://datamusee.givingsense.eu/onto/product/hasProduct> ?product
}
}
txt;
$format = "application/sparql-results+json";
$query = str_replace( "__uri__" , $graph , $sparqltemplate);
$user = $this->getParameter('app.sparql_user');
$pass = $this->getParameter('app.sparql_pass');
$auth = $user . ":" . $pass; // le dataset dmsites devrait être en libre accès
$requestURL = $endpoint.'?'
.'query='.urlencode($query)
.'&format='.urlencode($format);
$res = json_decode(self::request($requestURL, $auth), true);
$count = array(); $count["countcontrib"] = $count["countuser"] = 0;
if ($res) {
$count["salescount"] = $res["results"]["bindings"][0]["sellactioncount"]["value"];
$count["buyerscount"] = $res["results"]["bindings"][0]["buyerscount"]["value"];
$count["productscount"] = $res["results"]["bindings"][0]["productscount"]["value"];
}
return $count;
}
/**
@Route("/onto/sites/debug", name="sitesd")
*/
public function ontoListeSitesDebug()
{
$desc = array();
$desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
$desc["dataset"] = "dmsites";
$desc["graph"] = "";
$desc["dmticketinggraph"] = "https://kg.grains-de-culture.fr/dmmeta/sparql";
$desc["comment"] = "Sites du CMN et de Paris Musées";
$desc["uri"] = "http://datamusee.givingsense.efu/onto/sites";
$uri = $desc["uri"];
$sparqltemplate = <<<txt
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT distinct ?s ?label
WHERE
{
values ?g { <http://datamusee.givingsense.eu/graph/sites/cmn> <http://datamusee.givingsense.eu/graph/sites/parismusees>}
graph ?g {
?s skos:prefLabel ?label .
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "fr"))
filter(contains(str(?s),"http://datamusee.givingsense.eu/onto/site/"))
filter not exists { ?q <http://www.wikidata.org/prop/direct/P31> ?s }
filter not exists { ?q <http://www.wikidata.org/prop/direct/P279> ?s }
}
}
txt;
$query = str_replace("__uri__", $uri, $sparqltemplate);
$descstruct["query"] = $query;
$description = "";
return $this->render('testTemplateSite.html.twig', [
'desc' => $desc,
'descstruct' => $descstruct,
'description' => $description
]);
}
/**
@Route("/onto/sites.{_format}", defaults={"_format": "html"}, name="sites")
*/
public function ontoListeSites(Request $request)
{
// $lang = $request->getLocale();
$format = $this->getFormat($request);
/*
$format = $request->getRequestFormat();
$requestedContentTypes = $request->getAcceptableContentTypes();
foreach ($requestedContentTypes as $rType) {
if ($rType == 'application/json') { $format = 'json'; break; }
if ($rType == 'application/x-turtle') { $format = 'ttl'; break; }
if ($rType == 'application/rdf+xml') { $format = 'xml'; break; }
}
*/
$desc = array();
$user = $this->getParameter('app.sparql_user');
$pass = $this->getParameter('app.sparql_pass');
// $auth = "student:igr2018%"; // le dataset dmsites devrait être en libre accès
$auth = $user . ":" . $pass; // le dataset dmsites devrait être en libre accès
$desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
$desc["dataset"] = "dmsites";
$desc["graph"] = "";
$desc["dmticketinggraph"] = "";
$desc["comment"] = "Sites du CMN et de Paris Musées";
$desc["uri"] = "http://datamusee.givingsense.eu/onto/sites";
$uri = $desc["uri"];
$sformat = "application/sparql-results+json";
$sparqltemplate = <<<txt
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT distinct ?s ?label
WHERE
{
values ?g { <http://datamusee.givingsense.eu/graph/sites/cmn> <http://datamusee.givingsense.eu/graph/sites/parismusees>}
graph ?g {
?s skos:prefLabel ?label .
FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "fr"))
filter(contains(str(?s),"http://datamusee.givingsense.eu/onto/site/"))
filter not exists { ?q <http://www.wikidata.org/prop/direct/P31> ?s }
filter not exists { ?q <http://www.wikidata.org/prop/direct/P279> ?s }
}
}
txt;
$query = str_replace( "__uri__" , $uri , $sparqltemplate);
$requestURL = $desc["endpoint"].'?'
.'query='.urlencode($query)
.'&format='.urlencode($sformat);
$sites = json_decode(self::request($requestURL, $auth), true);
$descstruct["dataset"] = "dmsites";
$descstruct["triplestore"] = "https://kg.grains-de-culture.fr/";
$descstruct["query"] = $query;
$descstruct["jsonarray"] = self::request($requestURL, $auth);
$descstruct["url"] = $requestURL;
$descstruct["uri"] = $uri;
$descstruct["debug"] = $query;
$descstruct["sites"] = $sites["results"]["bindings"];
$descstruct["entitytype"] = "Sites partenaire de Data&Musée";
switch($format) {
case 'json':
//case 'ttl':
case 'xml':
$call = $desc["endpoint"].'?'
.'query='.urlencode($query)
.'&format='.$this->mimetype($format)
.'&output='.$format;
$mime = $this->mimetype($format);
//$ch = curl_init($call);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_HEADER, false);
//$result .= curl_exec($ch);
//curl_close($ch);
$result =self::request($call, $auth);
$response = new Response($result,
Response::HTTP_OK,
array('content-type' => $mime)
);
break;
case 'gql':
$gqlquery = <<<txt
{sites{
edges { node {id
prefLabel}}}}
txt;
$gqldesc["query"] = $gqlquery;
$json = json_decode($this->graphqlQuery('http://dm.givingsense.eu/graphql4/graphql', $gqlquery, ""), true, 512, JSON_UNESCAPED_UNICODE);
$descstruct["sites"] = array();
foreach ($json["data"]["sites"]["edges"] as $edge) {
$site = array();
$site["s"] = array();
$site["s"]["value"] = $edge["node"]["id"];
$site["label"] = array();
$site["label"]["value"] = $edge["node"]["prefLabel"];
array_push($descstruct["sites"], $site);
}
$response = $this->render('site/dmsites.html.twig', [
//$response = $this->render('testGraphQL.html.twig', [
'desc' => $desc,
'descstruct' => $descstruct,
'entitytype' => $descstruct["entitytype"]
]);
break;
default:
$response = $this->render('site/dmsites.html.twig', [
'desc' => $desc,
'descstruct' => $descstruct,
'entitytype' => $descstruct["entitytype"],
'description' => $sites
]);
break;
}
$response->headers->addCacheControlDirective('must-revalidate', true);
$response->setSharedMaxAge(3600);
//$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
@Route("onto/site/{id}/debug")
*/
public function cmnsitenewDebug($id)
{
$desc = array();
$desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
$desc["dataset"] = "dmsites";
$desc["graph"] = "http://givingsense.eu/datamusee/onto/dmsites";
$desc["dmticketinggraph"] = "http://datamusee.givingsense.eu/graph/billetterie/site/".$id;
$desc["comment"] = "Site référencé par Data&Musée";
$desc["id"] = $id;
$desc["uri"] = "http://datamusee.givingsense.eu/onto/site/".$desc["id"];
$desc["query"]= $this->getElementaryQuery($desc["uri"]);
$result = $this->getElementaryDescription($desc["endpoint"], $desc["uri"], "json");
return self::newSiteDescription($desc, $result, true);
//return self::jocondeSiteDescription($desc, $result, true);
}
public function getElementaryQuery($uri)
{
$sparqlTemplate = self::getPrefixes() . <<<txt
prefix owl: <http://www.w3.org/2002/07/owl#>
construct { ?s ?p ?o ; a ?t}
WHERE
{
{
{
bind(<__uri__> as ?s) .
?s ?p ?o .
?s <http://www.wikidata.org/prop/direct/P31> ?t .
} UNION
{
bind(<__uri__> as ?sameas) .
?s owl:sameAs ?sameas .
?s ?p ?o .
?s <http://www.wikidata.org/prop/direct/P31> ?t .
}
}
}
txt;
$query = str_replace( "__uri__" , $uri , $sparqlTemplate);
return $query;
}
public function getElementaryDescription($endpoint, $uri, $format)
{
$user = $this->getParameter('app.sparql_user');
$pass = $this->getParameter('app.sparql_pass');
$auth = $user . ":" . $pass; // le dataset dmsites devrait être en libre accès
//#graph <http://datamusee.givingsense.eu/graph/sites/cmn>
$query = $this->getElementaryQuery($uri); //str_replace( "__uri__" , $uri , $sparqlTemplate);
$call = $endpoint.'?'
.'query='.urlencode($query)
. '&format=' . $this->mimetype($format)
.'&output='.$format;
$result =self::request($call, $auth);
//$mime = $this->mimetype($format);
//$response = new Response($result,
// Response::HTTP_OK,
// array('content-type' => $mime)
//);
//return $response;
//echo htmlentities($query);
return $result;
}
/**
@Route("/onto/site/{id}.{_format}", defaults={"_format": "html"}, requirements={
* "_format": "html|json|xml|ttl"
* })
*/
public function cmnsitenew(Request $request, $id)
{
$format = $this->getFormat($request);
// initialize some keys
$desc = array();
# echo intval($id);
if ((intval($id)!=0)or($id[0]=='M')or($id[0]=='P')) { // expected to be a Data&Musée site
$desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
$desc["ticketendpoint"] = "https://kg.grains-de-culture.fr/dm/sparql";
$desc["dataset"] = "dmsites";
$desc["graph"] = "";
$desc["ticketdataset"] = "dm";
$desc["dmticketinggraph"] = "http://datamusee.givingsense.eu/graph/billetterie/site/" . $id;
$desc["comment"] = "Site membre du CMN";
$desc["id"] = $id;
$desc["uri"] = "http://datamusee.givingsense.eu/onto/site/" . $desc["id"];
$desc["query"]= $this->getElementaryQuery($desc["uri"]);
$elemDescMethod = 'getElementaryDescription';
$descMethod = 'newSiteDescription';
} else { // expected to be a SemJoconde site
$desc["endpoint"] = "https://kg.grains-de-culture.fr/SemJoconde/sparql";
$desc["ticketendpoint"] = "";
$desc["dataset"] = "SemJoconde";
$desc["graph"] = "http://joconde.givingsense.eu/graph/dmsites";
$desc["ticketdataset"] = "";
$desc["dmticketinggraph"] = "";
$desc["comment"] = "Site référencé dans la base Joconde";
$desc["id"] = $id;
$desc["uri"] = "http://datamusee.givingsense.eu/onto/site/" . $desc["id"];
$elemDescMethod = 'getElementaryDescription';
$descMethod = 'jocondeSiteDescription';
}
switch($format) {
case 'json': case 'ttl': case 'xml':
// syntaxe suivante supposée permettre l'appel d'une méthode, donnée par soon nom, de la classe désignée par $this
// $result = [$this, $elemDescMethod]($desc["endpoint"], $desc["uri"], $format);
$result = $this->{$elemDescMethod}($desc["endpoint"], $desc["uri"], $format);
$mime = $this->mimetype($format);
$response = new Response($result,
Response::HTTP_OK,
array('content-type' => $mime)
);
break;
default:
// $result = [$this, $elemDescMethod]($desc["endpoint"], $desc["uri"], "json");
$result = $this->{$elemDescMethod}($desc["endpoint"], $desc["uri"], "json");
// echo $result;
// $response = [$this, $descMethod]($desc, $result);
$response = $this->{$descMethod}($desc, $result);
$response->headers->set('Content-Type', 'text/html');
break;
}
$response->headers->addCacheControlDirective('must-revalidate', true);
$response->setSharedMaxAge(3600);
return $response;
}
/**
@Route("/onto/musee/cmn/{id}")
*/
public function cmnsite($id)
{
$desc = array();
$desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
$desc["dataset"] = "dm";
$desc["graph"] = "";
$desc["dmticketinggraph"] = "http://datamusee.givingsense.eu/graph/billetterie/site/".$id;
$desc["comment"] = "Site du CMN";
$desc["id"] = $id;
$desc["uri"] = "http://givingsense.eu/datamusee/onto/musee/cmn/".$desc["id"];
return self::siteDescription($desc);
}
}