src/Controller/SiteController.php line 329

Open in your IDE?
  1. <?php
  2. // src/Controller/SiteController.php
  3. namespace App\Controller;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request;
  7. class SiteController  extends DMAbstractController
  8. {
  9.     function jocondeSiteDescription($desc$jsonld$debug=false)
  10.     {
  11.         $lang "fr";
  12.         $jsonDescription json_decode($jsonldTRUE);
  13.         $descstruct["uri"] = $desc["uri"];
  14.         $descstruct["jsonld"] = "";
  15.         $descstruct["entityid"]  = "????";
  16.         $descstruct["altLabel"]  = array();
  17.         $descstruct["triplestore"] = "https://kg.grains-de-culture.fr/";
  18.         $descstruct["dataset"] = "SemJoconde";
  19.         $descstruct["uriville"]  = "";
  20.         $descstruct["ville"] = "?";
  21.         $descstruct["latitude"] = "?";
  22.         $descstruct["longitude"] = "?";
  23.         $descstruct["sameAs"] = array();
  24.         $descstruct["query"] = "";
  25.         if  ($jsonDescription){
  26.             $descstruct = array();
  27.             // types devraient être ajoutés au json-ld comme attribut @type
  28.             $descstruct["@type"] = array();
  29.             if (array_key_exists("P31"$jsonDescription)) {
  30.                 if (is_array($jsonDescription["P31"])) {
  31.                     foreach ($jsonDescription["P31"] as $isA) {
  32.                         array_push($descstruct["@type"], $isA);
  33.                     }
  34.                 } else array_push($descstruct["@type"], $jsonDescription["P31"]);
  35.             }
  36.             $descstruct["jsonld"] = $jsonld//str_replace("&quot;", '"', $jsonld);
  37.             if (array_key_exists("@id"$jsonDescription)){
  38.                 $descstruct["uri"] = $jsonDescription["@id"];
  39.             }else {
  40.                 $descstruct["uri"] = "";
  41.             }
  42.             $descstruct["entityid"]  = "????";
  43.             if (array_key_exists("prefLabel"$jsonDescription)){
  44.                 if(is_object($jsonDescription["prefLabel"])) {
  45.                     foreach ($jsonDescription["prefLabel"] as $pLabel) {
  46.                         $newlabel "";
  47.                         if (array_key_exists("@language"$pLabel)) {
  48.                             if ($pLabel["@language"] == $lang) {
  49.                                 $newlabel $pLabel["@value"];
  50.                                 break;
  51.                             }
  52.                         }
  53.                     }
  54.                 }
  55.                 else {
  56.                     // Checking if $jsonDescription["prefLabel"] is sequentialor asociative array
  57.                     $arr $jsonDescription["prefLabel"];
  58.                     if (array_keys($arr) !== range(0count($arr) - 1)) {
  59.                         // associative array
  60.                         $newlabel "";
  61.                         if (array_key_exists("@language"$jsonDescription["prefLabel"])) {
  62.                             $newlabel "titi";
  63.                             if ($jsonDescription["prefLabel"]["@language"] == $lang) {
  64.                                 $newlabel $jsonDescription["prefLabel"]["@value"];
  65.                             }
  66.                         }
  67.                     } else {
  68.                         // sequential array
  69.                         foreach ($jsonDescription["prefLabel"] as $pLabel) {
  70.                             $newlabel "";
  71.                             if (array_key_exists("@language"$pLabel)) {
  72.                                 if ($pLabel["@language"] == $lang) {
  73.                                     $newlabel $pLabel["@value"];
  74.                                     break;
  75.                                 }
  76.                             }
  77.                         }
  78.                     }
  79.                 }
  80.             } else {
  81.                 @$newlabel "";
  82.             }
  83.             if ($newlabel!="") {
  84.                 $descstruct["entityid"]  = $newlabel;
  85.             }
  86.             $descstruct["altLabel"]  = array();
  87.             if (array_key_exists("label"$jsonDescription)) {
  88.                 foreach ($jsonDescription["label"] as $label) {
  89.                     $newlabel "";
  90.                     if (is_array($label)) {
  91.                         if ($label["@language"] == $lang) {
  92.                             $newlabel $label["@value"];
  93.                         }
  94.                     } else $newlabel $label;
  95.                     if (($newlabel != "") and (!in_array($newlabel$descstruct["altLabel"]))) {
  96.                         array_push($descstruct["altLabel"], $newlabel);
  97.                     }
  98.                 }
  99.             }
  100.             $descstruct["dataset"] = "SemJoconde";
  101.             $descstruct["triplestore"] = "https://kg.grains-de-culture.fr/";
  102.             $descstruct["uriville"]  = "";
  103.             if (array_key_exists("city"$jsonDescription)) {
  104.                 if (is_array($jsonDescription["city"])) $descstruct["ville"] = $jsonDescription["city"]["@value"];
  105.                 else $descstruct["ville"] = $jsonDescription["city"];
  106.             } else {
  107.                 $descstruct["ville"] = "";
  108.             }
  109.             if(array_key_exists("http://www.w3.org/2003/01/geo/wgs84_pos#lat"$jsonDescription)) {
  110.                 $lat =  $jsonDescription["http://www.w3.org/2003/01/geo/wgs84_pos#lat"];
  111.                 echo($lat[0]);
  112.                 if (is_array($lat)) $descstruct["latitude"] = "!"//$lat[0];
  113.                 else $descstruct["latitude"] = "!!";//$lat;
  114.             } else $descstruct["latitude"] = "?";
  115.             if(array_key_exists("http://www.w3.org/2003/01/geo/wgs84_pos#long"$jsonDescription)) {
  116.                 $lon =  $jsonDescription["http://www.w3.org/2003/01/geo/wgs84_pos#long"];
  117.                 if (is_array($lon)) $descstruct["longitude"] = "&";//$lon[0];
  118.                 else $descstruct["longitude"] = "&&";//$lon;
  119.             } else $descstruct["longitude"] = "?";
  120.             $descstruct["sameAs"] = array();
  121.             if(array_key_exists("sameAs"$jsonDescription)) {
  122.                 if (is_array($jsonDescription["sameAs"])) {
  123.                     foreach ($jsonDescription["sameAs"] as $same) {
  124.                         array_push($descstruct["sameAs"], $same);
  125.                     }
  126.                 } else array_push($descstruct["sameAs"], $jsonDescription["sameAs"]);
  127.             }
  128.         }
  129.         $descstruct["countcontrib"] = 0;
  130.         $descstruct["countuser"] =  0;
  131.         $descstruct["countcontrib"] = 0;
  132.         $descstruct["entitytype"] = "Site culturel";
  133.         if ($jsonDescription) {
  134.             if (array_key_exists("@id"$jsonDescription)) {
  135.                 $count self::getGuestBookContribCount("https://kg.grains-de-culture.fr/dm/sparql"$jsonDescription["@id"]);
  136.                 $descstruct["countcontrib"] = $count["countcontrib"];
  137.                 $descstruct["countuser"] = $count["countuser"];
  138.                 $descstruct["countcontrib"] = $count["countcontrib"];
  139.             }
  140.             /*
  141.              *
  142.              $sellcount = self::getSellActionCount("https://kg.grains-de-culture.fr/dm/query",
  143.                                         str_replace("http://datamusee.givingsense.eu/onto/site/",
  144.                                             "http://datamusee.givingsense.eu/graph/billetterie/site/",
  145.                                             $jsonDescription["@id"]));
  146.             $descstruct["salescount"] =  $sellcount["salescount"];
  147.             $descstruct["productscount"] =  $sellcount["productscount"];
  148.             $descstruct["buyerscount"] =  $sellcount["buyerscount"];
  149.             */
  150.             if (array_key_exists("@id"$jsonDescription)) {
  151.                 $descstruct["class"] = $this->getClasses($jsonDescription["@id"], $desc["endpoint"]);
  152.             } else {
  153.                 $descstruct["class"] = array();
  154.             }
  155.             $descstruct["urljson"] = $descstruct["uri"] . ".json";
  156.             $yasguilink "";
  157.             if (array_key_exists("@id"$jsonDescription)) {
  158.                 $yasguilink "https://grains-de-culture.fr/fuseki/ui/#"
  159.                     'query=' urlencode(
  160.                         str_replace("construct { ?s ?p ?o ; a ?t}""select ?s ?p ?o"self::getElementaryQuery($jsonDescription["@id"])))
  161.                     . '&contentTypeConstruct=' urlencode("text/turtle")
  162.                     . '&contentTypeSelect=' urlencode("application/sparql-results+json")
  163.                     . '&endpoint=' urlencode($desc["endpoint"])
  164.                     . '&requestMethod=POST'
  165.                     '&tabTitle=' urlencode("Propriétés")
  166.                     . '&headers=%7B%7D'
  167.                     '&outputFormat=table';
  168.             }
  169.             $descstruct["yasguilink"] = $yasguilink;
  170.             $descstruct["entitytype"] = "Site culturel";
  171.             $template 'site/dmjocsite.html.twig';
  172.         }
  173.         if ($debug$template "testTemplateSite.html.twig";
  174.         $response $this->render($template, [
  175.             //return $this->render('testTemplateSite.html.twig', [
  176.             'desc' => $desc,
  177.             'descstruct' => $descstruct,
  178.             'entitytype' => $descstruct["entitytype"],
  179.             'description' => ""//$description
  180.         ]);
  181.         return  $response;
  182.     }
  183.     function newSiteDescription($desc$jsonld$debug=false)
  184.     {
  185.         $lang "fr";
  186.         $jsonDescription json_decode($jsonldTRUE);
  187.         $descstruct["uri"] = $desc["uri"];
  188.         $descstruct["uri"] = $desc["uri"];
  189.         $descstruct["jsonld"] = $jsonld;
  190.         $descstruct["entityid"]  = "????";
  191.         $descstruct["altLabel"]  = array();
  192.         $descstruct["triplestore"] = "https://kg.grains-de-culture.fr/";
  193.         $descstruct["dataset"] = "SemJoconde";
  194.         $descstruct["uriville"]  = "";
  195.         $descstruct["ville"] = "?";
  196.         $descstruct["latitude"] = "?";
  197.         $descstruct["longitude"] = "?";
  198.         $descstruct["sameAs"] = array();
  199.         $descstruct["query"] = "";
  200.         if  ($jsonDescription){
  201.             $descstruct = array();
  202.             $descstruct["query"] = $desc["query"];
  203.             // types devraient être ajoutés au json-ld comme attribut @type
  204.             $descstruct["@type"] = array();
  205.             if (array_key_exists("P31"$jsonDescription)) {
  206.                 if (is_array($jsonDescription["P31"])) {
  207.                     foreach ($jsonDescription["P31"] as $isA) {
  208.                         array_push($descstruct["@type"], $isA);
  209.                     }
  210.                 } else array_push($descstruct["@type"], $jsonDescription["P31"]);
  211.             }
  212.             $descstruct["jsonld"] = $jsonld//str_replace("&quot;", '"', $jsonld);
  213.             if (array_key_exists("@id"$jsonDescription)){
  214.                 $descstruct["uri"] = $jsonDescription["@id"];
  215.             } else {
  216.                 $descstruct["uri"] = "";
  217.             }
  218.             $descstruct["entityid"]  = "??????";
  219.             if (array_key_exists("skos:prefLabel"$jsonDescription)){
  220.                 // $descstruct["entityid"]  = "!!!!!!";
  221.                 if(is_object($jsonDescription["skos:prefLabel"])) {
  222.                     // $descstruct["entityid"]  = "!?!?!?";
  223.                     foreach ($jsonDescription["skos:prefLabel"] as $pLabel) {
  224.                         $newlabel "";
  225.                         if (array_key_exists("@language"$pLabel)) {
  226.                             if ($pLabel["@language"] == $lang) {
  227.                                 $newlabel $pLabel["@value"];
  228.                                 break;
  229.                             }
  230.                         }
  231.                     }
  232.                 }
  233.                 else {
  234.                     //$descstruct["entityid"]  = ";;;;;";
  235.                     // Checking if $jsonDescription["prefLabel"] is sequentialor asociative array
  236.                     $arr $jsonDescription["skos:prefLabel"];
  237.                     if (array_keys($arr) !== range(0count($arr) - 1)) {
  238.                         //$descstruct["entityid"]  = "$$$$$$";
  239.                         // associative array
  240.                         $newlabel "";
  241.                         if (array_key_exists("@language"$jsonDescription["skos:prefLabel"])) {
  242.                             $newlabel "titi";
  243.                             if ($jsonDescription["skos:prefLabel"]["@language"] == $lang) {
  244.                                 $newlabel $jsonDescription["skos:prefLabel"]["@value"];
  245.                             }
  246.                         }
  247.                     } else {
  248.                         //$descstruct["entityid"]  = "******";
  249.                         // sequential array
  250.                         foreach ($jsonDescription["skos:prefLabel"] as $pLabel) {
  251.                             $newlabel "";
  252.                             if (is_array($pLabel)) {
  253.                                 if (array_key_exists("@language"$pLabel)) {
  254.                                     if ($pLabel["@language"] == $lang) {
  255.                                         $newlabel $pLabel["@value"];
  256.                                         break;
  257.                                     }
  258.                                 }
  259.                             } else {
  260.                                 $descstruct["entityid"]  = $pLabel;
  261.                                 $newlabel $pLabel;
  262.                             }
  263.                         }
  264.                     }
  265.                 }
  266.             } else {
  267.                 @$newlabel "";
  268.             }
  269.             if ($newlabel!="") {
  270.                 $descstruct["entityid"]  = $newlabel;
  271.             }
  272.             $descstruct["altLabel"]  = array();
  273.             if (array_key_exists("rdfs:label"$jsonDescription)) {
  274.                 foreach ($jsonDescription["rdfs:label"] as $label) {
  275.                     $newlabel "";
  276.                     if (is_array($label)) {
  277.                         if ($label["@language"] == $lang) {
  278.                             $newlabel $label["@value"];
  279.                         }
  280.                     } else $newlabel $label;
  281.                     if (($newlabel != "") and (!in_array($newlabel$descstruct["altLabel"]))) {
  282.                         array_push($descstruct["altLabel"], $newlabel);
  283.                     }
  284.                 }
  285.             }
  286.             $descstruct["dataset"] = "dmsites";
  287.             $descstruct["triplestore"] = "https://kg.grains-de-culture.fr/";
  288.             $descstruct["uriville"]  = "_";
  289.             if (array_key_exists("city"$jsonDescription)) {
  290.                 if (is_array($jsonDescription["city"])) $descstruct["ville"] = $jsonDescription["city"]["@value"];
  291.                 else $descstruct["ville"] = $jsonDescription["city"];
  292.             } else {
  293.                 $descstruct["ville"] = "?";
  294.             }
  295.             if(array_key_exists("http://www.w3.org/2003/01/geo/wgs84_pos#lat"$jsonDescription)) {
  296.                 $lat =  $jsonDescription["http://www.w3.org/2003/01/geo/wgs84_pos#lat"];
  297.                 if (is_array($lat)) $descstruct["latitude"] = floatval($lat["@value"]);
  298.                 else $descstruct["latitude"] = floatval($lat["@value"]);
  299.             } else $descstruct["latitude"] = "?";
  300.             if(array_key_exists("http://www.w3.org/2003/01/geo/wgs84_pos#long"$jsonDescription)) {
  301.                 $lon =  $jsonDescription["http://www.w3.org/2003/01/geo/wgs84_pos#long"];
  302.                 if (is_array($lon)) $descstruct["longitude"] = floatval($lon["@value"]);
  303.                 else $descstruct["longitude"] = floatval($lon["@value"]);
  304.             } else $descstruct["longitude"] = "?";
  305.             $descstruct["sameAs"] = array();
  306.             if(array_key_exists("sameAs"$jsonDescription)) {
  307.                 if (is_array($jsonDescription["sameAs"])) {
  308.                     foreach ($jsonDescription["sameAs"] as $same) {
  309.                         array_push($descstruct["sameAs"], $same);
  310.                     }
  311.                 } else array_push($descstruct["sameAs"], $jsonDescription["sameAs"]);
  312.             }
  313.         }
  314.         $descstruct["countcontrib"] = 0;
  315.         $descstruct["countuser"] =  0;
  316.         $descstruct["countcontrib"] = 0;
  317.         if(array_key_exists("@id"$jsonDescription)) {
  318.             $count self::getGuestBookContribCount("https://kg.grains-de-culture.fr/dm/sparql"$jsonDescription["@id"]);
  319.             $descstruct["countcontrib"] = $count["countcontrib"];
  320.             $descstruct["countuser"] =  $count["countuser"];
  321.             $descstruct["countcontrib"] = $count["countcontrib"];
  322.         }
  323.         /*
  324.          *
  325.          $sellcount = self::getSellActionCount("https://kg.grains-de-culture.fr/dm/query",
  326.                                     str_replace("http://datamusee.givingsense.eu/onto/site/",
  327.                                         "http://datamusee.givingsense.eu/graph/billetterie/site/",
  328.                                         $jsonDescription["@id"]));
  329.         $descstruct["salescount"] =  $sellcount["salescount"];
  330.         $descstruct["productscount"] =  $sellcount["productscount"];
  331.         $descstruct["buyerscount"] =  $sellcount["buyerscount"];
  332.         */
  333.         if(array_key_exists("@id"$jsonDescription)) {
  334.             $descstruct["class"] = $this->getClasses($jsonDescription["@id"], $desc["endpoint"]);
  335.         } else {
  336.             $descstruct["class"] = array();
  337.         }
  338.         $descstruct["urljson"] = $descstruct["uri"].".json";
  339.         $yasguilink "";
  340.         if(array_key_exists("@id"$jsonDescription)) {
  341.             $yasguilink "https://grains-de-culture.fr/fuseki/ui/#"
  342.                 'query=' urlencode(
  343.                     str_replace("construct { ?s ?p ?o ; a ?t}""select ?s ?p ?o"self::getElementaryQuery($jsonDescription["@id"])))
  344.                 . '&contentTypeConstruct=' urlencode("text/turtle")
  345.                 . '&contentTypeSelect=' urlencode("application/sparql-results+json")
  346.                 . '&endpoint=' urlencode($desc["endpoint"])
  347.                 . '&requestMethod=POST'
  348.                 '&tabTitle=' urlencode("Propriétés")
  349.                 . '&headers=%7B%7D'
  350.                 '&outputFormat=table';
  351.         }
  352.         $descstruct["yasguilink"] = $yasguilink;
  353.         $descstruct["entitytype"] = "Site culturel";
  354.         $template 'site/dmsite.html.twig';
  355.         if ($debug$template "testTemplateSite.html.twig";
  356.         $response $this->render($template, [
  357.             //return $this->render('testTemplateSite.html.twig', [
  358.             'desc' => $desc,
  359.             'descstruct' => $descstruct,
  360.             'entitytype' => $descstruct["entitytype"],
  361.             'description' => ""//$description
  362.         ]);
  363.         return  $response;
  364.     }
  365.     function getPrefixes()
  366.     {
  367.         $prefixes = <<<txt
  368. PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
  369. PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  370. PREFIX wdt: <http://www.wikidata.org/prop/direct/>
  371. PREFIX owl: <http://www.w3.org/2002/07/owl#>
  372. prefix cmn: <http://givingsense.eu/datamusee/onto/musee/cmn/>
  373. txt;
  374.         return $prefixes;
  375.     }
  376.     function getClasses($uriclass$endpoint)
  377.     {
  378.         $user $this->getParameter('app.sparql_user');
  379.         $pass $this->getParameter('app.sparql_pass');
  380.         $auth $user ":" $pass// le dataset dmsites devrait être en libre accès
  381.         $classTemplate self::getPrefixes() . <<<txt
  382. SELECT distinct ?o ?label ?super ?superlabel 
  383. WHERE { 
  384.   <__uri__> wdt:P31 ?o .
  385.       ?o  <http://www.wikidata.org/prop/direct/P279>* ?super.
  386.   ?o rdfs:label ?label .
  387.   ?super rdfs:label ?superlabel .
  388.   bind(lang(?superlabel) as ?lang)
  389.   filter((lang(?superlabel)='en') ||(lang(?superlabel)='en') ) 
  390.      }
  391.   
  392. txt;
  393.         $queryClasses =  str_replace"__uri__" $uriclass $classTemplate);
  394.         $requestURL $endpoint.'?'
  395.             .'query='.urlencode($queryClasses)
  396.             .'&format=json';
  397.         $classes json_decode(self::request($requestURL$auth), true);
  398.         $classArray = array();
  399.         foreach ($classes["results"]["bindings"] as $item) {
  400.             $lang "";
  401.             if(array_key_exists("xml:lang"$item["label"]))
  402.                 $lang $item["label"]["xml:lang"];
  403.             if ($lang=="en"$classArray[$item["o"]["value"]] = [ "class"=> $item["o"]["value"], "label" =>$item["label"]["value"], "lang"=>$lang];
  404.             if(array_key_exists("super"$item)){
  405.                 if(array_key_exists("xml:lang"$item["superlabel"])) $lang $item["superlabel"]["xml:lang"]; else $lang "";
  406.                 if ($lang=="en")
  407.                     $classArray[$item["super"]["value"]] = [ "class"=> $item["super"]["value"], "label" =>$item["superlabel"]["value"], "lang"=>$lang];
  408.             }
  409.         }
  410.         return $classArray;
  411.         //return array($queryClasses);
  412.     }
  413.     function getGuestBookContribCount($endpoint$urisite)
  414.     {
  415.         $sparqltemplate = <<<txt
  416. PREFIX schema: <http://schema.org/>
  417. select (count(distinct ?s) as ?cl)(count(distinct ?user) as ?cu) 
  418. where
  419. {
  420.   values ?graph { <http://datamusee.givingsense.eu/livredor/parismusees> <http://datamusee.givingsense.eu/livredor/cmn> }
  421.     graph ?graph {
  422.     ?app schema:about <__uri__> .
  423.     ?s a <http://datamusee.givingsense.eu/onto/contribution>; 
  424.        <http://datamusee.givingsense.eu/onto/contribution/hasUser> ?user;
  425.        <http://datamusee.givingsense.eu/onto/contribution/hasAppli> ?app . 
  426.   }
  427. txt;
  428.         $format "application/sparql-results+json";
  429.         $query =  str_replace"__uri__" $urisite $sparqltemplate);
  430.         $user $this->getParameter('app.sparql_user');
  431.         $pass $this->getParameter('app.sparql_pass');
  432.         $auth $user ":" $pass// le dataset dmsites devrait être en libre accès
  433.        $requestURL $endpoint.'?'
  434.             .'query='.urlencode($query)
  435.             .'&format='.urlencode($format);
  436.         $res json_decode(self::request($requestURL$auth), true);
  437.         $count = array(); $count["countcontrib"] = $count["countuser"] = 0;
  438.         if ($res) {
  439.             $count["countcontrib"]  = $res["results"]["bindings"][0]["cl"]["value"];
  440.             $count["countuser"] = $res["results"]["bindings"][0]["cu"]["value"];
  441.         }
  442.         //return $query;
  443.         return $count;
  444.     }
  445.     function getSellActionCount($endpoint$graph)
  446.     {
  447.         $sparqltemplate = <<<txt
  448. PREFIX schema: <http://schema.org/>
  449. select (count(distinct ?s) as ?sellactioncount) (count(distinct ?buyer) as ?buyerscount) (count(distinct ?product) as ?productscount) 
  450. {
  451.   graph <__uri__> {
  452.     ?s a schema:SellAction ;
  453.     schema:buyer ?buyer ;
  454.     <http://datamusee.givingsense.eu/onto/product/hasProduct> ?product
  455.   }
  456. }
  457. txt;
  458.         $format "application/sparql-results+json";
  459.         $query =  str_replace"__uri__" $graph $sparqltemplate);
  460.         $user $this->getParameter('app.sparql_user');
  461.         $pass $this->getParameter('app.sparql_pass');
  462.         $auth $user ":" $pass// le dataset dmsites devrait être en libre accès
  463.         $requestURL $endpoint.'?'
  464.             .'query='.urlencode($query)
  465.             .'&format='.urlencode($format);
  466.         $res json_decode(self::request($requestURL$auth), true);
  467.         $count = array(); $count["countcontrib"] = $count["countuser"] = 0;
  468.         if ($res) {
  469.             $count["salescount"]  = $res["results"]["bindings"][0]["sellactioncount"]["value"];
  470.             $count["buyerscount"] = $res["results"]["bindings"][0]["buyerscount"]["value"];
  471.             $count["productscount"] = $res["results"]["bindings"][0]["productscount"]["value"];
  472.         }
  473.         return $count;
  474.     }
  475.     /**
  476.     @Route("/onto/sites/debug", name="sitesd")
  477.      */
  478.     public function ontoListeSitesDebug()
  479.     {
  480.         $desc = array();
  481.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
  482.         $desc["dataset"] = "dmsites";
  483.         $desc["graph"] = "";
  484.         $desc["dmticketinggraph"] = "https://kg.grains-de-culture.fr/dmmeta/sparql";
  485.         $desc["comment"] = "Sites du CMN et de Paris Musées";
  486.         $desc["uri"] = "http://datamusee.givingsense.efu/onto/sites";
  487.         $uri $desc["uri"];
  488.         $sparqltemplate = <<<txt
  489. PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
  490. PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  491. SELECT distinct ?s ?label
  492. WHERE 
  493.     values ?g { <http://datamusee.givingsense.eu/graph/sites/cmn> <http://datamusee.givingsense.eu/graph/sites/parismusees>}
  494.     graph ?g {
  495.     ?s  skos:prefLabel ?label .
  496.     FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "fr"))
  497.     filter(contains(str(?s),"http://datamusee.givingsense.eu/onto/site/"))
  498.     filter not exists { ?q <http://www.wikidata.org/prop/direct/P31> ?s } 
  499.     filter not exists { ?q <http://www.wikidata.org/prop/direct/P279> ?s }
  500.     }
  501. }
  502. txt;
  503.         $query str_replace("__uri__"$uri$sparqltemplate);
  504.         $descstruct["query"] = $query;
  505.         $description "";
  506.         return $this->render('testTemplateSite.html.twig', [
  507.             'desc' => $desc,
  508.             'descstruct' => $descstruct,
  509.             'description' => $description
  510.         ]);
  511.     }
  512.     /**
  513.     @Route("/onto/sites.{_format}", defaults={"_format": "html"}, name="sites")
  514.      */
  515.     public function ontoListeSites(Request $request)
  516.     {
  517.         // $lang = $request->getLocale();
  518.         $format $this->getFormat($request);
  519.         /*
  520.         $format = $request->getRequestFormat();
  521.         $requestedContentTypes = $request->getAcceptableContentTypes();
  522.         foreach ($requestedContentTypes as $rType) {
  523.             if ($rType == 'application/json') { $format = 'json'; break; }
  524.             if ($rType == 'application/x-turtle') { $format = 'ttl'; break; }
  525.             if ($rType == 'application/rdf+xml') { $format = 'xml'; break; }
  526.         }
  527.         */
  528.         $desc = array();
  529.         $user $this->getParameter('app.sparql_user');
  530.         $pass $this->getParameter('app.sparql_pass');
  531.         // $auth = "student:igr2018%"; // le dataset dmsites devrait être en libre accès
  532.         $auth $user ":" $pass// le dataset dmsites devrait être en libre accès
  533.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
  534.         $desc["dataset"] = "dmsites";
  535.         $desc["graph"] = "";
  536.         $desc["dmticketinggraph"] = "";
  537.         $desc["comment"] = "Sites du CMN et de Paris Musées";
  538.         $desc["uri"] = "http://datamusee.givingsense.eu/onto/sites";
  539.         $uri $desc["uri"];
  540.         $sformat "application/sparql-results+json";
  541.         $sparqltemplate = <<<txt
  542. PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
  543. PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  544. SELECT distinct ?s ?label
  545. WHERE 
  546.     values ?g { <http://datamusee.givingsense.eu/graph/sites/cmn> <http://datamusee.givingsense.eu/graph/sites/parismusees>}
  547.     graph ?g {
  548.     ?s  skos:prefLabel ?label .
  549.     FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "fr"))
  550.     filter(contains(str(?s),"http://datamusee.givingsense.eu/onto/site/"))
  551.     filter not exists { ?q <http://www.wikidata.org/prop/direct/P31> ?s } 
  552.     filter not exists { ?q <http://www.wikidata.org/prop/direct/P279> ?s }
  553.     }
  554. }
  555. txt;
  556.         $query =  str_replace"__uri__" $uri $sparqltemplate);
  557.         $requestURL $desc["endpoint"].'?'
  558.             .'query='.urlencode($query)
  559.             .'&format='.urlencode($sformat);
  560.         $sites json_decode(self::request($requestURL$auth), true);
  561.         $descstruct["dataset"] = "dmsites";
  562.         $descstruct["triplestore"] = "https://kg.grains-de-culture.fr/";
  563.         $descstruct["query"] = $query;
  564.         $descstruct["jsonarray"] = self::request($requestURL$auth);
  565.         $descstruct["url"] = $requestURL;
  566.         $descstruct["uri"] = $uri;
  567.         $descstruct["debug"] = $query;
  568.         $descstruct["sites"] = $sites["results"]["bindings"];
  569.         $descstruct["entitytype"] = "Sites partenaire de Data&Musée";
  570.         switch($format) {
  571.             case 'json':
  572.             //case 'ttl':
  573.             case 'xml':
  574.                 $call $desc["endpoint"].'?'
  575.                     .'query='.urlencode($query)
  576.                     .'&format='.$this->mimetype($format)
  577.                     .'&output='.$format;
  578.                 $mime $this->mimetype($format);
  579.                 //$ch = curl_init($call);
  580.                 //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  581.                 //curl_setopt($ch, CURLOPT_HEADER, false);
  582.                 //$result .= curl_exec($ch);
  583.                 //curl_close($ch);
  584.                 $result =self::request($call$auth);
  585.                 $response = new Response($result,
  586.                     Response::HTTP_OK,
  587.                     array('content-type' => $mime)
  588.                 );
  589.                 break;
  590.             case 'gql':
  591.                 $gqlquery = <<<txt
  592. {sites{ 
  593.     edges {     node {id
  594.         prefLabel}}}}
  595. txt;
  596.                 $gqldesc["query"] = $gqlquery;
  597.                 $json json_decode($this->graphqlQuery('http://dm.givingsense.eu/graphql4/graphql'$gqlquery""), true512JSON_UNESCAPED_UNICODE);
  598.                 $descstruct["sites"] = array();
  599.                 foreach ($json["data"]["sites"]["edges"] as $edge) {
  600.                     $site = array();
  601.                     $site["s"] = array();
  602.                     $site["s"]["value"] = $edge["node"]["id"];
  603.                     $site["label"] = array();
  604.                     $site["label"]["value"] = $edge["node"]["prefLabel"];
  605.                     array_push($descstruct["sites"], $site);
  606.                 }
  607.                 $response $this->render('site/dmsites.html.twig', [
  608.                 //$response = $this->render('testGraphQL.html.twig', [
  609.                     'desc' => $desc,
  610.                     'descstruct' => $descstruct,
  611.                     'entitytype' => $descstruct["entitytype"]
  612.                 ]);
  613.                 break;
  614.             default:
  615.                 $response $this->render('site/dmsites.html.twig', [
  616.                     'desc' => $desc,
  617.                     'descstruct' => $descstruct,
  618.                     'entitytype' => $descstruct["entitytype"],
  619.                     'description' => $sites
  620.                 ]);
  621.                 break;
  622.         }
  623.         $response->headers->addCacheControlDirective('must-revalidate'true);
  624.         $response->setSharedMaxAge(3600);
  625.         //$response->headers->set('Content-Type', 'application/json');
  626.         return $response;
  627.     }
  628.     /**
  629.     @Route("onto/site/{id}/debug")
  630.      */
  631.     public function cmnsitenewDebug($id)
  632.     {
  633.         $desc = array();
  634.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
  635.         $desc["dataset"] = "dmsites";
  636.         $desc["graph"] = "http://givingsense.eu/datamusee/onto/dmsites";
  637.         $desc["dmticketinggraph"] = "http://datamusee.givingsense.eu/graph/billetterie/site/".$id;
  638.         $desc["comment"] = "Site référencé par Data&Musée";
  639.         $desc["id"] = $id;
  640.         $desc["uri"] = "http://datamusee.givingsense.eu/onto/site/".$desc["id"];
  641.         $desc["query"]= $this->getElementaryQuery($desc["uri"]);
  642.         $result $this->getElementaryDescription($desc["endpoint"], $desc["uri"], "json");
  643.         return self::newSiteDescription($desc$resulttrue);
  644.         //return self::jocondeSiteDescription($desc, $result, true);
  645.     }
  646.     public function getElementaryQuery($uri)
  647.     {
  648.         $sparqlTemplate self::getPrefixes() . <<<txt
  649. prefix owl: <http://www.w3.org/2002/07/owl#>
  650. construct { ?s ?p ?o ; a ?t} 
  651. WHERE 
  652.     {
  653.         {
  654.             bind(<__uri__> as ?s) .
  655.            ?s ?p ?o .
  656.            ?s <http://www.wikidata.org/prop/direct/P31> ?t .
  657.         } UNION
  658.         {
  659.             bind(<__uri__> as ?sameas) .
  660.             ?s owl:sameAs ?sameas . 
  661.             ?s ?p ?o .
  662.             ?s <http://www.wikidata.org/prop/direct/P31> ?t .
  663.         }
  664.     }
  665. txt;
  666.         $query =  str_replace"__uri__" $uri $sparqlTemplate);
  667.         return $query;
  668.     }
  669.     public function getElementaryDescription($endpoint$uri$format)
  670.     {
  671.         $user $this->getParameter('app.sparql_user');
  672.         $pass $this->getParameter('app.sparql_pass');
  673.         $auth $user ":" $pass// le dataset dmsites devrait être en libre accès
  674.         //#graph <http://datamusee.givingsense.eu/graph/sites/cmn>
  675.         $query =  $this->getElementaryQuery($uri); //str_replace( "__uri__" , $uri , $sparqlTemplate);
  676.         $call $endpoint.'?'
  677.             .'query='.urlencode($query)
  678.             . '&format=' $this->mimetype($format)
  679.             .'&output='.$format;
  680.         $result =self::request($call$auth);
  681.         //$mime = $this->mimetype($format);
  682.         //$response = new Response($result,
  683.         //    Response::HTTP_OK,
  684.         //    array('content-type' => $mime)
  685.         //);
  686.         //return $response;
  687.         //echo htmlentities($query);
  688.         return $result;
  689.     }
  690.     /**
  691.     @Route("/onto/site/{id}.{_format}", defaults={"_format": "html"}, requirements={
  692.      *         "_format": "html|json|xml|ttl"
  693.      *     })
  694.      */
  695.     public function cmnsitenew(Request $request$id)
  696.     {
  697.         $format $this->getFormat($request);
  698.         // initialize some keys
  699.         $desc = array();
  700.         # echo intval($id);
  701.         if ((intval($id)!=0)or($id[0]=='M')or($id[0]=='P')) { // expected to be a Data&Musée site
  702.             $desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
  703.             $desc["ticketendpoint"] = "https://kg.grains-de-culture.fr/dm/sparql";
  704.             $desc["dataset"] = "dmsites";
  705.             $desc["graph"] = "";
  706.             $desc["ticketdataset"] = "dm";
  707.             $desc["dmticketinggraph"] = "http://datamusee.givingsense.eu/graph/billetterie/site/" $id;
  708.             $desc["comment"] = "Site membre du CMN";
  709.             $desc["id"] = $id;
  710.             $desc["uri"] = "http://datamusee.givingsense.eu/onto/site/" $desc["id"];
  711.             $desc["query"]= $this->getElementaryQuery($desc["uri"]);
  712.             $elemDescMethod 'getElementaryDescription';
  713.             $descMethod 'newSiteDescription';
  714.         } else { // expected to be a SemJoconde site
  715.             $desc["endpoint"] = "https://kg.grains-de-culture.fr/SemJoconde/sparql";
  716.             $desc["ticketendpoint"] = "";
  717.             $desc["dataset"] = "SemJoconde";
  718.             $desc["graph"] = "http://joconde.givingsense.eu/graph/dmsites";
  719.             $desc["ticketdataset"] = "";
  720.             $desc["dmticketinggraph"] = "";
  721.             $desc["comment"] = "Site référencé dans la base Joconde";
  722.             $desc["id"] = $id;
  723.             $desc["uri"] = "http://datamusee.givingsense.eu/onto/site/" $desc["id"];
  724.             $elemDescMethod 'getElementaryDescription';
  725.             $descMethod 'jocondeSiteDescription';
  726.         }
  727.         switch($format) {
  728.             case 'json': case 'ttl': case 'xml':
  729.                 // syntaxe suivante supposée permettre l'appel d'une méthode, donnée par soon nom, de la classe désignée par $this
  730.                 // $result = [$this, $elemDescMethod]($desc["endpoint"], $desc["uri"], $format);
  731.                 $result $this->{$elemDescMethod}($desc["endpoint"], $desc["uri"], $format);
  732.                 $mime $this->mimetype($format);
  733.                 $response = new Response($result,
  734.                     Response::HTTP_OK,
  735.                     array('content-type' => $mime)
  736.                 );
  737.                 break;
  738.             default:
  739.                 // $result = [$this, $elemDescMethod]($desc["endpoint"], $desc["uri"], "json");
  740.                 $result $this->{$elemDescMethod}($desc["endpoint"], $desc["uri"], "json");
  741.                 // echo $result;
  742.                 // $response = [$this, $descMethod]($desc, $result);
  743.                 $response $this->{$descMethod}($desc$result);
  744.                 $response->headers->set('Content-Type''text/html');
  745.                 break;
  746.         }
  747.         $response->headers->addCacheControlDirective('must-revalidate'true);
  748.         $response->setSharedMaxAge(3600);
  749.         return $response;
  750.     }
  751.     /**
  752.     @Route("/onto/musee/cmn/{id}")
  753.      */
  754.     public function cmnsite($id)
  755.     {
  756.         $desc = array();
  757.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
  758.         $desc["dataset"] = "dm";
  759.         $desc["graph"] = "";
  760.         $desc["dmticketinggraph"] = "http://datamusee.givingsense.eu/graph/billetterie/site/".$id;
  761.         $desc["comment"] = "Site du CMN";
  762.         $desc["id"] = $id;
  763.         $desc["uri"] = "http://givingsense.eu/datamusee/onto/musee/cmn/".$desc["id"];
  764.         return self::siteDescription($desc);
  765.     }
  766. }