src/Controller/SiteController.php line 732

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("http://dbpedia.org/ontology/city"$jsonDescription)) {
  290.                 if (is_array($jsonDescription["http://dbpedia.org/ontology/city"])) $descstruct["ville"] = $jsonDescription["http://dbpedia.org/ontology/city"]["@value"];
  291.                 else $descstruct["ville"] = $jsonDescription["http://dbpedia.org/ontology/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.   graph ?g 
  385.   {  <__uri__> wdt:P31 ?o .
  386.       ?o  <http://www.wikidata.org/prop/direct/P279>* ?super.
  387.   ?o rdfs:label ?label .
  388.   ?super rdfs:label ?superlabel .
  389.   bind(lang(?superlabel) as ?lang)
  390.   filter((lang(?superlabel)='en') ||(lang(?superlabel)='en') ) 
  391.      }
  392.   }
  393. txt;
  394.         $queryClasses =  str_replace"__uri__" $uriclass $classTemplate);
  395.         $requestURL $endpoint.'?'
  396.             .'query='.urlencode($queryClasses)
  397.             .'&format=json';
  398.         $classes json_decode(self::request($requestURL$auth), true);
  399.         $classArray = array();
  400.         foreach ($classes["results"]["bindings"] as $item) {
  401.             $lang "";
  402.             if(array_key_exists("xml:lang"$item["label"]))
  403.                 $lang $item["label"]["xml:lang"];
  404.             if ($lang=="en"$classArray[$item["o"]["value"]] = [ "class"=> $item["o"]["value"], "label" =>$item["label"]["value"], "lang"=>$lang];
  405.             if(array_key_exists("super"$item)){
  406.                 if(array_key_exists("xml:lang"$item["superlabel"])) $lang $item["superlabel"]["xml:lang"]; else $lang "";
  407.                 if ($lang=="en")
  408.                     $classArray[$item["super"]["value"]] = [ "class"=> $item["super"]["value"], "label" =>$item["superlabel"]["value"], "lang"=>$lang];
  409.             }
  410.         }
  411.         return $classArray;
  412.         //return array($queryClasses);
  413.     }
  414.     function getGuestBookContribCount($endpoint$urisite)
  415.     {
  416.         $sparqltemplate = <<<txt
  417. PREFIX schema: <http://schema.org/>
  418. select (count(distinct ?s) as ?cl)(count(distinct ?user) as ?cu) 
  419. where
  420. {
  421.   values ?graph { <http://datamusee.givingsense.eu/livredor/parismusees> <http://datamusee.givingsense.eu/livredor/cmn> }
  422.     graph ?graph {
  423.     ?app schema:about <__uri__> .
  424.     ?s a <http://datamusee.givingsense.eu/onto/contribution>; 
  425.        <http://datamusee.givingsense.eu/onto/contribution/hasUser> ?user;
  426.        <http://datamusee.givingsense.eu/onto/contribution/hasAppli> ?app . 
  427.   }
  428. txt;
  429.         $format "application/sparql-results+json";
  430.         $query =  str_replace"__uri__" $urisite $sparqltemplate);
  431.         $user $this->getParameter('app.sparql_user');
  432.         $pass $this->getParameter('app.sparql_pass');
  433.         $auth $user ":" $pass// le dataset dmsites devrait être en libre accès
  434.        $requestURL $endpoint.'?'
  435.             .'query='.urlencode($query)
  436.             .'&format='.urlencode($format);
  437.         $res json_decode(self::request($requestURL$auth), true);
  438.         $count = array(); $count["countcontrib"] = $count["countuser"] = 0;
  439.         if ($res) {
  440.             $count["countcontrib"]  = $res["results"]["bindings"][0]["cl"]["value"];
  441.             $count["countuser"] = $res["results"]["bindings"][0]["cu"]["value"];
  442.         }
  443.         //return $query;
  444.         return $count;
  445.     }
  446.     function getSellActionCount($endpoint$graph)
  447.     {
  448.         $sparqltemplate = <<<txt
  449. PREFIX schema: <http://schema.org/>
  450. select (count(distinct ?s) as ?sellactioncount) (count(distinct ?buyer) as ?buyerscount) (count(distinct ?product) as ?productscount) 
  451. {
  452.   graph <__uri__> {
  453.     ?s a schema:SellAction ;
  454.     schema:buyer ?buyer ;
  455.     <http://datamusee.givingsense.eu/onto/product/hasProduct> ?product
  456.   }
  457. }
  458. txt;
  459.         $format "application/sparql-results+json";
  460.         $query =  str_replace"__uri__" $graph $sparqltemplate);
  461.         $user $this->getParameter('app.sparql_user');
  462.         $pass $this->getParameter('app.sparql_pass');
  463.         $auth $user ":" $pass// le dataset dmsites devrait être en libre accès
  464.         $requestURL $endpoint.'?'
  465.             .'query='.urlencode($query)
  466.             .'&format='.urlencode($format);
  467.         $res json_decode(self::request($requestURL$auth), true);
  468.         $count = array(); $count["countcontrib"] = $count["countuser"] = 0;
  469.         if ($res) {
  470.             $count["salescount"]  = $res["results"]["bindings"][0]["sellactioncount"]["value"];
  471.             $count["buyerscount"] = $res["results"]["bindings"][0]["buyerscount"]["value"];
  472.             $count["productscount"] = $res["results"]["bindings"][0]["productscount"]["value"];
  473.         }
  474.         return $count;
  475.     }
  476.     /**
  477.     @Route("/onto/sites/debug", name="sitesd")
  478.      */
  479.     public function ontoListeSitesDebug()
  480.     {
  481.         $desc = array();
  482.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
  483.         $desc["dataset"] = "dmsites";
  484.         $desc["graph"] = "";
  485.         $desc["dmticketinggraph"] = "https://kg.grains-de-culture.fr/dmmeta/sparql";
  486.         $desc["comment"] = "Sites du CMN et de Paris Musées";
  487.         $desc["uri"] = "http://datamusee.givingsense.efu/onto/sites";
  488.         $uri $desc["uri"];
  489.         $sparqltemplate = <<<txt
  490. PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
  491. PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  492. SELECT distinct ?s ?label
  493. WHERE 
  494.     values ?g { <http://datamusee.givingsense.eu/graph/sites/cmn> <http://datamusee.givingsense.eu/graph/sites/parismusees>}
  495.     graph ?g {
  496.     ?s  skos:prefLabel ?label .
  497.     FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "fr"))
  498.     filter(contains(str(?s),"http://datamusee.givingsense.eu/onto/site/"))
  499.     filter not exists { ?q <http://www.wikidata.org/prop/direct/P31> ?s } 
  500.     filter not exists { ?q <http://www.wikidata.org/prop/direct/P279> ?s }
  501.     }
  502. }
  503. txt;
  504.         $query str_replace("__uri__"$uri$sparqltemplate);
  505.         $descstruct["query"] = $query;
  506.         $description "";
  507.         return $this->render('testTemplateSite.html.twig', [
  508.             'desc' => $desc,
  509.             'descstruct' => $descstruct,
  510.             'description' => $description
  511.         ]);
  512.     }
  513.     /**
  514.     @Route("/onto/sites.{_format}", defaults={"_format": "html"}, name="sites")
  515.      */
  516.     public function ontoListeSites(Request $request)
  517.     {
  518.         // $lang = $request->getLocale();
  519.         $format $this->getFormat($request);
  520.         /*
  521.         $format = $request->getRequestFormat();
  522.         $requestedContentTypes = $request->getAcceptableContentTypes();
  523.         foreach ($requestedContentTypes as $rType) {
  524.             if ($rType == 'application/json') { $format = 'json'; break; }
  525.             if ($rType == 'application/x-turtle') { $format = 'ttl'; break; }
  526.             if ($rType == 'application/rdf+xml') { $format = 'xml'; break; }
  527.         }
  528.         */
  529.         $desc = array();
  530.         $user $this->getParameter('app.sparql_user');
  531.         $pass $this->getParameter('app.sparql_pass');
  532.         // $auth = "student:igr2018%"; // le dataset dmsites devrait être en libre accès
  533.         $auth $user ":" $pass// le dataset dmsites devrait être en libre accès
  534.         // temporaire cause redirection mal gérée $desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
  535.         $desc["endpoint"] = "http://137.194.211.97/test20240630/sparql";
  536.         $desc["dataset"] = "dmsites";
  537.         $desc["graph"] = "";
  538.         $desc["dmticketinggraph"] = "";
  539.         $desc["comment"] = "Sites du CMN et de Paris Musées";
  540.         $desc["uri"] = "http://datamusee.givingsense.eu/onto/sites";
  541.         $uri $desc["uri"];
  542.         $sformat "application/sparql-results+json";
  543.         $sparqltemplate = <<<txt
  544. PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
  545. PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  546. SELECT distinct ?s ?label
  547. WHERE 
  548.     values ?g { <http://datamusee.givingsense.eu/graph/sites/cmn> <http://datamusee.givingsense.eu/graph/sites/parismusees>}
  549.     graph ?g {
  550.     ?s  skos:prefLabel ?label .
  551.     FILTER(LANG(?label) = "" || LANGMATCHES(LANG(?label), "fr"))
  552.     filter(contains(str(?s),"http://datamusee.givingsense.eu/onto/site/"))
  553.     filter not exists { ?q <http://www.wikidata.org/prop/direct/P31> ?s } 
  554.     filter not exists { ?q <http://www.wikidata.org/prop/direct/P279> ?s }
  555.     }
  556. }
  557. txt;
  558.         $query =  str_replace"__uri__" $uri $sparqltemplate);
  559.         $requestURL $desc["endpoint"].'?'
  560.             .'query='.urlencode($query)
  561.             .'&format='.urlencode($sformat);
  562.         $sites json_decode(self::request($requestURL$auth), true);
  563.         $descstruct["dataset"] = "dmsites";
  564.         $descstruct["triplestore"] = "https://kg.grains-de-culture.fr/";
  565.         $descstruct["query"] = $query;
  566.         $descstruct["jsonarray"] = self::request($requestURL$auth);
  567.         $descstruct["url"] = $requestURL;
  568.         $descstruct["uri"] = $uri;
  569.         $descstruct["debug"] = $query;
  570.         $descstruct["sites"] = $sites["results"]["bindings"];
  571.         $descstruct["entitytype"] = "Sites partenaire de Data&Musée";
  572.         switch($format) {
  573.             case 'json':
  574.             //case 'ttl':
  575.             case 'xml':
  576.                 $call $desc["endpoint"].'?'
  577.                     .'query='.urlencode($query)
  578.                     .'&format='.$this->mimetype($format)
  579.                     .'&output='.$format;
  580.                 $mime $this->mimetype($format);
  581.                 //$ch = curl_init($call);
  582.                 //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  583.                 //curl_setopt($ch, CURLOPT_HEADER, false);
  584.                 //$result .= curl_exec($ch);
  585.                 //curl_close($ch);
  586.                 $result =self::request($call$auth);
  587.                 $response = new Response($result,
  588.                     Response::HTTP_OK,
  589.                     array('content-type' => $mime)
  590.                 );
  591.                 break;
  592.             case 'gql':
  593.                 $gqlquery = <<<txt
  594. {sites{ 
  595.     edges {     node {id
  596.         prefLabel}}}}
  597. txt;
  598.                 $gqldesc["query"] = $gqlquery;
  599.                 $json json_decode($this->graphqlQuery('http://dm.givingsense.eu/graphql4/graphql'$gqlquery""), true512JSON_UNESCAPED_UNICODE);
  600.                 $descstruct["sites"] = array();
  601.                 foreach ($json["data"]["sites"]["edges"] as $edge) {
  602.                     $site = array();
  603.                     $site["s"] = array();
  604.                     $site["s"]["value"] = $edge["node"]["id"];
  605.                     $site["label"] = array();
  606.                     $site["label"]["value"] = $edge["node"]["prefLabel"];
  607.                     array_push($descstruct["sites"], $site);
  608.                 }
  609.                 $response $this->render('site/dmsites.html.twig', [
  610.                 //$response = $this->render('testGraphQL.html.twig', [
  611.                     'desc' => $desc,
  612.                     'descstruct' => $descstruct,
  613.                     'entitytype' => $descstruct["entitytype"]
  614.                 ]);
  615.                 break;
  616.             default:
  617.                 $response $this->render('site/dmsites.html.twig', [
  618.                     'desc' => $desc,
  619.                     'descstruct' => $descstruct,
  620.                     'entitytype' => $descstruct["entitytype"],
  621.                     'description' => $sites
  622.                 ]);
  623.                 break;
  624.         }
  625.         $response->headers->addCacheControlDirective('must-revalidate'true);
  626.         $response->setSharedMaxAge(3600);
  627.         //$response->headers->set('Content-Type', 'application/json');
  628.         return $response;
  629.     }
  630.     /**
  631.     @Route("onto/site/{id}/debug")
  632.      */
  633.     public function cmnsitenewDebug($id)
  634.     {
  635.         $desc = array();
  636.         // temporaire cause redirection mal gérée $desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
  637.         $desc["endpoint"] = "http://137.194.211.97/test20240630/sparql";
  638.         $desc["dataset"] = "dmsites";
  639.         $desc["graph"] = "http://givingsense.eu/datamusee/onto/dmsites";
  640.         $desc["dmticketinggraph"] = "http://datamusee.givingsense.eu/graph/billetterie/site/".$id;
  641.         $desc["comment"] = "Site référencé par Data&Musée";
  642.         $desc["id"] = $id;
  643.         $desc["uri"] = "http://datamusee.givingsense.eu/onto/site/".$desc["id"];
  644.         $desc["query"]= $this->getElementaryQuery($desc["uri"]);
  645.         $result $this->getElementaryDescription($desc["endpoint"], $desc["uri"], "json");
  646.         return self::newSiteDescription($desc$resulttrue);
  647.         //return self::jocondeSiteDescription($desc, $result, true);
  648.     }
  649.     public function getElementaryQuery($uri)
  650.     {
  651.         $sparqlTemplate self::getPrefixes() . <<<txt
  652. prefix owl: <http://www.w3.org/2002/07/owl#>
  653. construct { ?s ?p ?o ; a ?t} 
  654. WHERE 
  655.     graph ?g
  656.     {
  657.         {
  658.             bind(<__uri__> as ?s) .
  659.            ?s ?p ?o .
  660.            ?s <http://www.wikidata.org/prop/direct/P31> ?t .
  661.         } UNION
  662.         {
  663.             bind(<__uri__> as ?sameas) .
  664.             ?s owl:sameAs ?sameas . 
  665.             ?s ?p ?o .
  666.             ?s <http://www.wikidata.org/prop/direct/P31> ?t .
  667.         }
  668.     }
  669. txt;
  670.         $query =  str_replace"__uri__" $uri $sparqlTemplate);
  671.         return $query;
  672.     }
  673.     public function getElementaryDescription($endpoint$uri$format)
  674.     {
  675.         $user $this->getParameter('app.sparql_user');
  676.         $pass $this->getParameter('app.sparql_pass');
  677.         $auth $user ":" $pass// le dataset dmsites devrait être en libre accès
  678.         //#graph <http://datamusee.givingsense.eu/graph/sites/cmn>
  679.         $query =  $this->getElementaryQuery($uri); //str_replace( "__uri__" , $uri , $sparqlTemplate);
  680.         $call $endpoint.'?'
  681.             .'query='.urlencode($query)
  682.             . '&format=' $this->mimetype($format)
  683.             .'&output='.$format;
  684.         $result =self::request($call$auth);
  685.         //$mime = $this->mimetype($format);
  686.         //$response = new Response($result,
  687.         //    Response::HTTP_OK,
  688.         //    array('content-type' => $mime)
  689.         //);
  690.         //return $response;
  691.         //echo htmlentities($query);
  692.         return $result;
  693.     }
  694.     /**
  695.     @Route("/onto/site/{id}.{_format}", defaults={"_format": "html"}, requirements={
  696.      *         "_format": "html|json|xml|ttl"
  697.      *     })
  698.      */
  699.     public function cmnsitenew(Request $request$id)
  700.     {
  701.         $format $this->getFormat($request);
  702.         // initialize some keys
  703.         $desc = array();
  704.         # echo intval($id);
  705.         if ((intval($id)!=0)or($id[0]=='M')or($id[0]=='P')) { // expected to be a Data&Musée site
  706.             // temporaire cause redirection mal gérée $desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
  707.             $desc["endpoint"] = "http://137.194.211.97/test20240630/sparql";
  708.             $desc["ticketendpoint"] = "https://kg.grains-de-culture.fr/dm/sparql";
  709.             $desc["dataset"] = "dmsites";
  710.             $desc["graph"] = "";
  711.             $desc["ticketdataset"] = "dm";
  712.             $desc["dmticketinggraph"] = "http://datamusee.givingsense.eu/graph/billetterie/site/" $id;
  713.             $desc["comment"] = "Site membre du CMN";
  714.             $desc["id"] = $id;
  715.             $desc["uri"] = "http://datamusee.givingsense.eu/onto/site/" $desc["id"];
  716.             $desc["query"]= $this->getElementaryQuery($desc["uri"]);
  717.             $elemDescMethod 'getElementaryDescription';
  718.             $descMethod 'newSiteDescription';
  719.         } else { // expected to be a SemJoconde site
  720.             // temporaire cause redirection mal gérée $desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
  721.             $desc["endpoint"] = "http://137.194.211.97/test20240630/sparql";
  722.             $desc["ticketendpoint"] = "";
  723.             $desc["dataset"] = "SemJoconde";
  724.             $desc["graph"] = "http://joconde.givingsense.eu/graph/dmsites";
  725.             $desc["ticketdataset"] = "";
  726.             $desc["dmticketinggraph"] = "";
  727.             $desc["comment"] = "Site référencé dans la base Joconde";
  728.             $desc["id"] = $id;
  729.             $desc["uri"] = "http://datamusee.givingsense.eu/onto/site/" $desc["id"];
  730.             $elemDescMethod 'getElementaryDescription';
  731.             $descMethod 'jocondeSiteDescription';
  732.         }
  733.         switch($format) {
  734.             case 'json': case 'ttl': case 'xml':
  735.                 // syntaxe suivante supposée permettre l'appel d'une méthode, donnée par soon nom, de la classe désignée par $this
  736.                 // $result = [$this, $elemDescMethod]($desc["endpoint"], $desc["uri"], $format);
  737.                 $result $this->{$elemDescMethod}($desc["endpoint"], $desc["uri"], $format);
  738.                 $mime $this->mimetype($format);
  739.                 $response = new Response($result,
  740.                     Response::HTTP_OK,
  741.                     array('content-type' => $mime)
  742.                 );
  743.                 break;
  744.             default:
  745.                 // $result = [$this, $elemDescMethod]($desc["endpoint"], $desc["uri"], "json");
  746.                 $result $this->{$elemDescMethod}($desc["endpoint"], $desc["uri"], "json");
  747.                 // echo $result;
  748.                 // $response = [$this, $descMethod]($desc, $result);
  749.                 $response $this->{$descMethod}($desc$result);
  750.                 $response->headers->set('Content-Type''text/html');
  751.                 break;
  752.         }
  753.         $response->headers->addCacheControlDirective('must-revalidate'true);
  754.         $response->setSharedMaxAge(3600);
  755.         return $response;
  756.     }
  757.     /**
  758.     @Route("/onto/musee/cmn/{id}")
  759.      */
  760.     public function cmnsite($id)
  761.     {
  762.         $desc = array();
  763.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/dmsites/sparql";
  764.         $desc["dataset"] = "dm";
  765.         $desc["graph"] = "";
  766.         $desc["dmticketinggraph"] = "http://datamusee.givingsense.eu/graph/billetterie/site/".$id;
  767.         $desc["comment"] = "Site du CMN";
  768.         $desc["id"] = $id;
  769.         $desc["uri"] = "http://givingsense.eu/datamusee/onto/musee/cmn/".$desc["id"];
  770.         return self::siteDescription($desc);
  771.     }
  772. }