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