src/Controller/OntoController.php line 371

Open in your IDE?
  1. <?php
  2. // src/Controller/OntoController.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 OntoController  extends DMAbstractController
  8. {
  9.     function getAnswerDescription($endpoint, $sparqltemplate, $entityuri)
  10.     {
  11.        $format = "application/sparql-results+json";
  12.        $query =  str_replace ( "__uri__" , $entityuri , $sparqltemplate);
  13.        $searchUrl = $endpoint.'?'
  14.           .'query='.urlencode($query)
  15.           .'&format='.urlencode($format);
  16.           
  17.        return $searchUrl;
  18.     }
  19.     
  20.     function request($url, $auth){ 
  21.        // is curl installed?
  22.        if (!function_exists('curl_init')){ 
  23.           die('CURL is not installed!');
  24.        }
  25.        // get curl handle
  26.        $ch= curl_init();
  27.        // set request url
  28.        curl_setopt($ch, 
  29.           CURLOPT_URL, 
  30.           $url);
  31.        // return response, don't print/echo
  32.        curl_setopt($ch, 
  33.           CURLOPT_RETURNTRANSFER, 
  34.           TRUE);
  35.           // 2 lignes suivantes dangereuses, voir comment gérer des certificats
  36.        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  37.        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  38.        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  39.        curl_setopt($ch, CURLOPT_USERPWD, $auth); 
  40.        /*
  41.        Here you find more options for curl:
  42.        http://www.php.net/curl_setopt
  43.        */        
  44.        $response = curl_exec($ch);
  45.         if (curl_error($ch)) {
  46.             $error_msg = curl_error($ch);
  47.         }
  48.        curl_close($ch);
  49.         if ($response === FALSE) {
  50.             return "erreur ".$error_msg." dans l'appel de ".$url;
  51.         }
  52.        return $response;
  53.     }
  54.     function siteStructurer($descstruct, $description) {
  55.         $descstruct["sameAs"] = array();
  56.         $descstruct["class"] = array();
  57.         $descstruct["altLabel"] = array();
  58.         $descstruct["entityid"] = $descstruct["ville"] = $descstruct["longitude"] = $descstruct["latitude"] ="";
  59.         foreach ($description["results"]["bindings"] as $item) {
  60.             if ($item["p"]["value"]==="http://www.w3.org/2004/02/skos/core#prefLabel") //"http://www.w3.org/2000/01/rdf-schema#label")
  61.             {
  62.                 if(array_key_exists("xml:lang", $item["o"])) {
  63.                     $lang = $item["o"]["xml:lang"];
  64.                     if ($lang=="en") $descstruct["entityid"] = $item["o"]["value"];
  65.                     if ($lang=="fr") $descstruct["entityid"] = $item["o"]["value"];
  66.                 } else {
  67.                     $reflabel = $item["o"]["value"];
  68.                 }
  69.             }
  70.             if ($item["p"]["value"]==="http://www.w3.org/2003/01/geo/wgs84_pos#lat") {
  71.                 $descstruct["latitude"] = $item["o"]["value"];
  72.             }
  73.             if ($item["p"]["value"]==="http://www.w3.org/2003/01/geo/wgs84_pos#long") {
  74.                 $descstruct["longitude"] = $item["o"]["value"];
  75.             }
  76.             if ($item["p"]["value"]==="http://www.w3.org/2002/07/owl#sameAs") {
  77.                 if(!in_array($item["o"]["value"], $descstruct["sameAs"])){
  78.                     array_push($descstruct["sameAs"], $item["o"]["value"]);
  79.                 }
  80.             }
  81.             if ($item["p"]["value"]==="http://www.w3.org/2004/02/skos/core#altLabel") {
  82.                 if(!in_array($item["o"]["value"], $descstruct["altLabel"])) {
  83.                     array_push($descstruct["altLabel"], $item["o"]["value"]);
  84.                 }
  85.             }
  86.             if ($item["p"]["value"]==="http://www.wikidata.org/prop/direct/P31") {
  87.                 if(array_key_exists("xml:lang", $item["label"])) {
  88.                     $lang = $item["label"]["xml:lang"];
  89.                     if ($lang=="en") $descstruct["class"][$item["o"]["value"]] = [ "class"=> $item["o"]["value"], "label" =>$item["label"]["value"], "lang"=>$lang];
  90.                 }
  91.                 if(array_key_exists("super", $item)){
  92.                     if(array_key_exists("xml:lang", $item["superlabel"])) $lang = $item["superlabel"]["xml:lang"]; else $lang = "";
  93.                     if ($lang=="en")
  94.                         $descstruct["class"][$item["super"]["value"]] = [ "class"=> $item["super"]["value"], "label" =>$item["superlabel"]["value"], "lang"=>$lang];
  95.                 }
  96.             }
  97.             if (array_key_exists("cityname", $item)) {
  98.                 $descstruct["ville"] = $item["cityname"]["value"];
  99.             }
  100.             if (array_key_exists("city", $item)) {
  101.                 $descstruct["uriville"] = $item["city"]["value"];
  102.             }
  103.         }
  104.         if ($descstruct["ville"]=="") {
  105.             $descstruct["ville"] = $descstruct["uriville"];
  106.         }
  107.         if ($descstruct["entityid"]=="") {
  108.             $descstruct["entityid"] = $reflabel;
  109.         }
  110.         return $descstruct;
  111.     }
  112.     function newSiteDescription($desc, $debug=false)
  113.     {
  114.     }
  115.     function siteDescription($desc, $debug=false)
  116.     {
  117.         $uri = $desc["uri"];
  118.         $auth = "student:igr2018%"; // le dataset dmsites devrait être en libre accès
  119.         $format = "application/sparql-results+json";
  120.         $prefixes = <<<txt
  121. PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
  122. PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  123. PREFIX wdt: <http://www.wikidata.org/prop/direct/>
  124. PREFIX owl: <http://www.w3.org/2002/07/owl#>
  125. prefix cmn: <http://givingsense.eu/datamusee/onto/musee/cmn/>
  126. txt;
  127.         $sparqlTemplate = $prefixes . <<<txt
  128. SELECT * 
  129. WHERE 
  130. { 
  131. {
  132.     {
  133.         <__uri__> ?p ?o . 
  134.         optional { <__uri__>  <http://dbpedia.org/ontology/city> ?city .
  135.             optional { ?city skos:prefLabel ?cityname } } 
  136.         } UNION
  137.         {
  138.             ?s owl:sameAs <__uri__> . 
  139.             ?s ?p ?o .
  140.             optional { ?s  <http://dbpedia.org/ontology/city> ?city .
  141.                 optional { 
  142.                     ?city skos:prefLabel ?cityname
  143.                     filter(lang(?cityname)='fr' || lang(?cityname)="")
  144.                  } 
  145.             } 
  146.     }
  147.     optional {
  148.         <__uri__> wdt:P31 ?o .
  149.         ?o rdfs:label ?label .
  150.         filter(lang(?label)='en' ||lang(?label)='fr' || lang(?label)="")
  151.     }     
  152. }
  153. }
  154. txt;
  155.         $descstruct = array();
  156.         $descstruct["entityid"] = $descstruct["altLabel"] = $descstruct["uriville"] =
  157.         $descstruct["latitude"] = $descstruct["longitude"] = $descstruct["sameAs"] =
  158.         $descstruct["ville"] = $descstruct["class"] = ""; // tempo
  159.         $mainquery =  str_replace( "__uri__" , $uri , $sparqlTemplate);
  160.         $requestURL = $desc["endpoint"].'?'
  161.             .'query='.urlencode($mainquery)
  162.             .'&format='.urlencode($format);
  163.         $description = json_decode(self::request($requestURL, $auth), true);
  164.         // $description = self::request($requestURL, $auth);
  165.         $descstruct = self::siteStructurer($descstruct, $description);
  166.         $count = self::getGuestBookContribCount("https://kg.grains-de-culture.fr/dm/sparql",$uri);
  167.         $descstruct["countcontrib"] = $count["countcontrib"];
  168.         $descstruct["countuser"] =  $count["countuser"];
  169.         $descstruct["countcontrib"] = $count["countcontrib"];
  170.         $descstruct["salescount"] =  0;
  171.         $descstruct["productscount"] = 0;
  172.         $descstruct["buyerscount"] =  0;
  173.         $classTemplate = $prefixes . <<<txt
  174. SELECT distinct ?o ?label ?super ?superlabel 
  175. WHERE { 
  176.   <__uri__> wdt:P31 ?o .
  177.       ?o  <http://www.wikidata.org/prop/direct/P279>* ?super.
  178.   ?o rdfs:label ?label .
  179.   ?super rdfs:label ?superlabel .
  180.   bind(lang(?superlabel) as ?lang)
  181.   filter((lang(?superlabel)='en') ||(lang(?superlabel)='en') ) 
  182.      }
  183.   
  184. txt;
  185.         $queryClasses =  str_replace( "__uri__" , $uri , $classTemplate);
  186.         $requestURL = $desc["endpoint"].'?'
  187.             .'query='.urlencode($queryClasses)
  188.             .'&format='.urlencode($format);
  189.         $classes = json_decode(self::request($requestURL, $auth), true);
  190.          foreach ($classes["results"]["bindings"] as $item) {
  191.              $lang = "";
  192.              if(array_key_exists("xml:lang", $item["label"]))
  193.                  $lang = $item["label"]["xml:lang"];
  194.             if ($lang=="en") $descstruct["class"][$item["o"]["value"]] = [ "class"=> $item["o"]["value"], "label" =>$item["label"]["value"], "lang"=>$lang];
  195.             if(array_key_exists("super", $item)){
  196.                 if(array_key_exists("xml:lang", $item["superlabel"])) $lang = $item["superlabel"]["xml:lang"]; else $lang = "";
  197.                 if ($lang=="en")
  198.                     $descstruct["class"][$item["super"]["value"]] = [ "class"=> $item["super"]["value"], "label" =>$item["superlabel"]["value"], "lang"=>$lang];
  199.             }
  200.         }
  201.         $descstruct["dataset"] = "dmsites";
  202.         $descstruct["triplestore"] = "https://kg.grains-de-culture.fr/";
  203.         $descstruct["query"] = $mainquery;
  204.         $descstruct["jsonarray"] = $description;
  205.         $descstruct["url"] = $requestURL;
  206.         $descstruct["uri"] = $uri;
  207.         $descstruct["debug"] = $mainquery;
  208.         $descstruct["entitytype"] = "Site culturel";
  209.         $template = 'site/dmcmnsite.html.twig';
  210.         if ($debug) $template = "testTemplateSite.html.twig";
  211.         return $this->render($template, [
  212.         //return $this->render('testTemplateSite.html.twig', [
  213.             'desc' => $desc,
  214.             'descstruct' => $descstruct,
  215.             'entitytype' => $descstruct["entitytype"],
  216.             'description' => $description
  217.         ]);
  218.     }
  219.     function getGuestBookContribCount($endpoint, $urisite)
  220.     {
  221.         $sparqltemplate = <<<txt
  222. PREFIX schema: <http://schema.org/>
  223. select (count(distinct ?s) as ?cl)(count(distinct ?user) as ?cu) 
  224. where
  225. {
  226.   values ?graph { <http://datamusee.givingsense.eu/livredor/parismusees> <http://datamusee.givingsense.eu/livredor/cmn> }
  227.     graph ?graph {
  228.     ?app schema:about <__uri__> .
  229.     ?s a <http://datamusee.givingsense.eu/onto/contribution>; 
  230.        <http://datamusee.givingsense.eu/onto/contribution/hasUser> ?user;
  231.        <http://datamusee.givingsense.eu/onto/contribution/hasAppli> ?app . 
  232.   }
  233. } 
  234. txt;
  235.         $format = "application/sparql-results+json";
  236.         $query =  str_replace( "__uri__" , $urisite , $sparqltemplate);
  237.         $auth = "student:igr2018%"; // le dataset dmmeta devrait être en libre accès
  238.         $requestURL = $endpoint.'?'
  239.             .'query='.urlencode($query)
  240.             .'&format='.urlencode($format);
  241.         $res = json_decode(self::request($requestURL, $auth), true);
  242.         $count = array(); $count["countcontrib"] = $count["countuser"] = 0;
  243.         if ($res) {
  244.             $count["countcontrib"]  = $res["results"]["bindings"][0]["cl"]["value"];
  245.             $count["countuser"] = $res["results"]["bindings"][0]["cu"]["value"];
  246.         }
  247.         //return $query;
  248.         return $count;
  249.     }
  250.     /*
  251.     http://givingsense.eu/datamusee/onto/cmn/categories
  252.      */
  253.     /**
  254.     @Route("/onto/events")
  255.      */
  256.     public function events()
  257.     {
  258.         $desc = array();
  259.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/datamusee/query";
  260.         $desc["dataset"] = "datamusee";
  261.         $desc["graph"] = "http://givingsense.eu/datamusee/onto/events";
  262.         $desc["comment"] = "Graphe servant de test à l'aspiration de données sur des événements. Un dataset spécifique va être constitué. ";
  263.         return self::minGraphDescription($desc);
  264.     }
  265.     /**
  266.     @Route("/onto/sameAs")
  267.      */
  268.     public function sameAs()
  269.     {
  270.         $desc = array();
  271.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/datamusee/query";
  272.         $desc["dataset"] = "datamusee";
  273.         $desc["graph"] = "http://givingsense.eu/datamusee/onto/sameAs";
  274.         $desc["comment"] = "Graphe destiné à stocker les relations d'identité entre des entités utiles à Data&Musée. Par exemple, 'http://fr.dbpedia.org/resource/Paris' est identique à 'https://www.wikidata.org/entity/Q90'";
  275.         return self::minGraphDescription($desc);
  276.     }
  277.     /**
  278.     @Route("/onto/contextgraph18022019")
  279.      */
  280.     public function contextgraph()
  281.     {
  282.         $desc = array();
  283.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/datamusee/query";
  284.         $desc["dataset"] = "datamusee";
  285.         $desc["graph"] = "http://givingsense.eu/datamusee/onto/contextgraph18022019";
  286.         $desc["comment"] = "Graphe de contexte de Paris Musées, issu de fr-DBpedia; données liées aux directement ou indirectement aux musées de Paris Musées. ";
  287.         return self::minGraphDescription($desc);
  288.     }
  289.     /**
  290.     @Route("/onto/joconde18022019")
  291.      */
  292.     public function joconde()
  293.     {
  294.         $desc = array();
  295.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/datamusee/query";
  296.         $desc["dataset"] = "datamusee";
  297.         $desc["graph"] = "http://givingsense.eu/datamusee/onto/joconde18022019";
  298.         $desc["comment"] = "Représentation en graphe de connaissances par transcription de la base Joconde. ";
  299.         return self::minGraphDescription($desc);
  300.     }
  301.     /**
  302.     @Route("/onto/datatourisme")
  303.      */
  304.     public function datatourisme()
  305.     {
  306.         $desc = array();
  307.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/datamusee/query";
  308.         $desc["dataset"] = "datamusee";
  309.         $desc["graph"] = "http://givingsense.eu/datamusee/onto/datatourisme";
  310.         $desc["comment"] = "Extrait de la base DataTourisme; données issues des offices de tourisme français; extrait pour prise en main du contenu. ";
  311.         return self::minGraphDescription($desc);
  312.     }
  313.     /**
  314.     @Route("/onto/geonames")
  315.      */
  316.     public function geonames()
  317.     {
  318.         $desc = array();
  319.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/datamusee/query";
  320.         $desc["dataset"] = "datamusee";
  321.         $desc["graph"] = "http://givingsense.eu/datamusee/onto/geonames";
  322.         $desc["comment"] = "Extrait de la base geonames; données sur les entités géographiques françaises. ";
  323.         return self::minGraphDescription($desc);
  324.     }
  325.     function minGraphDescription($desc)
  326.     {
  327.         $desc["triplestore"] = "https://kg.grains-de-culture.fr/";
  328.         return $this->render("graph/dmminimalgraph.html.twig", [
  329.             'descstruct' => $desc,
  330.             'entitytype' => "Graphe",
  331.         ]);
  332.     }
  333.     /**
  334.     @Route("/onto/billetterieExtraitPM")
  335.      */
  336.     public function ontoTech()
  337.     {
  338.         return new Response(
  339.             '<!doctype html>
  340.                 <html lang="fr">
  341.                 <head>
  342.                 <meta charset="UTF-8"></head><body>Graphe à usage technique interne. Pas de description publique.</body></html>'
  343.         );
  344.     }
  345.     /**
  346.     @Route("/onto")
  347.      */
  348.     public function onto()
  349.     {
  350.         return new Response(
  351.             '<!doctype html>
  352.                 <html lang="fr">
  353.                 <head>
  354.                 <meta charset="UTF-8"></head><body>Base du déréférencement des URIs créées pour le projet Data&Musée</body></html>'
  355.         );
  356.     }
  357.     function contribStructurer($description)
  358.     {
  359.         $descstruct = array();
  360.         $descstruct["predicates"] = array();
  361.         $descstruct["questions"] = array();
  362.         $descstruct["reponses"] = array();
  363.         $descstruct["possiblevalues"] = array();
  364.         $lienQuestionAnswer = array();
  365.         $labelQuestion = array();
  366.         foreach ($description["results"]["bindings"] as $item) {
  367.             $predicate = $item["p"]["value"];
  368.             if (!in_array($predicate, $descstruct["predicates"], TRUE)) {
  369.               array_push($descstruct["predicates"], $predicate);
  370.             } 
  371.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/contribution/hasQuestion") 
  372.             {
  373.               if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/question/hasLabel") 
  374.               {
  375.                 if ($item["v"]["xml:lang"]==="fr") 
  376.                 {
  377.                   $labelQuestion[$item["o"]["value"]] = $item["v"]["value"];
  378.                 }
  379.               }
  380.             }
  381.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/contribution/hasAnswer") {
  382.               if (array_key_exists("pv", $item)) 
  383.               {
  384.                 if ($item["pv"]["xml:lang"]==="fr") 
  385.                 {
  386.                     $labelReponses[$item["o"]["value"]] =  $item["pv"]["value"];  // idreponse -> valeur reponse
  387.                 }
  388.               }  elseif ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/answer/hasValue") 
  389.               {
  390.                   $labelReponses[$item["o"]["value"]] =  $item["v"]["value"];  // idreponse -> valeur reponse
  391.               }
  392.               if   ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/answer/belongQuestion") 
  393.               {
  394.                   $lienQuestionAnswer[$item["v"]["value"]] = $item["o"]["value"]; // uriquestion->urireponse
  395.               }
  396.             }
  397.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/contribution/hasAppli") {
  398.                 if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/application/appName") {
  399.                   $descstruct["appli"] = $item["o"]["value"];
  400.                   $descstruct["appliname"] = $item["v"]["value"];
  401.                 }
  402.                 if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/application/clientName") {
  403.                   $descstruct["clientname"] = $item["v"]["value"];
  404.                 }
  405.                 if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/application/hasId") {
  406.                   $descstruct["idappli"] = $item["v"]["value"];
  407.                 }
  408.             }
  409.         }
  410.         foreach ($lienQuestionAnswer as $idquestion => $idreponse) {
  411.             if (array_key_exists($idquestion, $lienQuestionAnswer) )
  412.             {
  413.               if ($labelReponses[$lienQuestionAnswer[$idquestion]]==="-1")
  414.               { 
  415.                 $strrep = "Pas de réponse";    $urirep = "";
  416.               } 
  417.               else  
  418.               {
  419.                   $strrep= $labelReponses[$lienQuestionAnswer[$idquestion]];      $urirep =  $lienQuestionAnswer[$idquestion];
  420.               } 
  421.               $rep = [ "uriquestion" => $idquestion, "question" => $labelQuestion[$idquestion], "urireponse" => $urirep, "reponse" => $strrep ];
  422.             } 
  423.             else
  424.             {
  425.               $rep = [ "uriquestion" => $idquestion, "question" => $labelQuestion[$idquestion], "urireponse" => "", "reponse" => "" ];
  426.             }
  427.             array_push($descstruct["questions"], $rep);
  428.         }
  429.       return $descstruct;
  430.     }
  431.     function questionStructurer($description)
  432.     {
  433.         $descstruct = array();
  434.         $descstruct["predicates"] = array();
  435.         $descstruct["labels"] = array();
  436.         $descstruct["reponses"] = array();
  437.         $descstruct["category"] = "??";
  438.         $descstruct["uriquestion"] = "??";
  439.         $descstruct["question"] = "??";
  440.         foreach ($description["results"]["bindings"] as $item) {
  441.             $predicate = $item["p"]["value"];
  442.             if (!in_array($predicate, $descstruct["predicates"], TRUE)) {
  443.               array_push($descstruct["predicates"], $predicate);
  444.             } 
  445.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/question/hasLabel") {
  446.                 array_push($descstruct["labels"], ["lang"=> $item["o"]["xml:lang"], "label"=> $item["o"]["value"] ]);
  447.             }
  448.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/question/hasPossibleValue") {
  449.               $descstruct["posval"] = $item["o"]["value"]; 
  450.               if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/possibleValue/hasLabel") { 
  451.                     array_push($descstruct["reponses"],  [ "label" =>$item["v"]["value"], "lang"=>$item["v"]["xml:lang"]]);
  452.               }
  453.             }
  454.         }
  455.       return $descstruct;
  456.     }
  457.     function appliStructurer($description)
  458.     {
  459.         $descstruct = array();
  460.         $descstruct["predicates"] = array();
  461.         $descstruct["reponses"] = array();
  462.         $descstruct["possiblevalues"] = array();
  463.         // $descstruct["category"] = "??";
  464.         //$descstruct["uriquestion"] = "??";
  465.         //$descstruct["question"] = "??";
  466.         foreach ($description["results"]["bindings"] as $item) {
  467.             $predicate = $item["p"]["value"];
  468.             if (!in_array($predicate, $descstruct["predicates"], TRUE)) {
  469.               array_push($descstruct["predicates"], $predicate);
  470.             } 
  471.         }
  472.       return $descstruct;
  473.     }
  474.     
  475.     function answerStructurer($description)
  476.     {
  477.         $descstruct = array();
  478.         $descstruct["predicates"] = array();
  479.         $descstruct["reponses"] = array();
  480.         $descstruct["possiblevalues"] = array();
  481.         foreach ($description["results"]["bindings"] as $item) {
  482.             $predicate = $item["p"]["value"];
  483.             if (!in_array($predicate, $descstruct["predicates"], TRUE)) {
  484.               array_push($descstruct["predicates"], $predicate);
  485.             } 
  486.             if ($item["p"]["value"]==="http://www.w3.org/1999/02/22-rdf-syntax-ns#type") {
  487.                   $descstruct["typelink"] = $item["o"]["value"];
  488.             }
  489.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/answer/belongQuestion") {
  490.               if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/question/hasLabel") 
  491.               {
  492.                 if ($item["v"]["xml:lang"]==="fr") 
  493.                 {
  494.                   $descstruct["question"] = $item["v"]["value"];
  495.                   $descstruct["uriquestion"] = $item["o"]["value"];
  496.                 }
  497.               }
  498.               if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/question/hasType") 
  499.               {
  500.                 $descstruct["category"] = $item["v"]["value"];
  501.               }
  502.             }
  503.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/answer/hasPossibleValue") {
  504.               if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/possibleValue/hasLabel") { 
  505.                 array_push($descstruct["reponses"], ["lang"=> $item["v"]["xml:lang"], "label"=> $item["v"]["value"] ]);
  506.               }
  507.             }
  508.         }
  509.       return $descstruct;
  510.     }
  511.     function posvalStructurer($description)
  512.     {
  513.         $descstruct = array();
  514.         $descstruct["predicates"] = array();
  515.         $descstruct["labels"] = array();
  516.         $descstruct["possiblevalues"] = array();
  517.         foreach ($description["results"]["bindings"] as $item) {
  518.             $predicate = $item["p"]["value"];
  519.             if (!in_array($predicate, $descstruct["predicates"], TRUE)) {
  520.               array_push($descstruct["predicates"], $predicate);
  521.             } 
  522.             if ($item["p"]["value"]==="http://www.w3.org/1999/02/22-rdf-syntax-ns#type") {
  523.                   $descstruct["typelink"] = $item["o"]["value"];
  524.             }
  525.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/possibleValue/hasLabel") { 
  526.                 array_push($descstruct["labels"], ["lang"=> $item["o"]["xml:lang"], "label"=> $item["o"]["value"] ]);
  527.             }
  528.         }
  529.       return $descstruct;
  530.     }
  531.     function entityLivredorDisplay($entityid)
  532.     {
  533.         // exemple: http://givingsense.eu/datamusee/onto/livredor/ANSW_5ad8ad19d1c52ba751ccab5c
  534.         // ici, je devrais récupérer le type de l'entité et choisir un template en fonction du type
  535.         // mais pour l'instant (16/6/2019) Tuan n'a pas mis de type sur les entités
  536.         // donc je vais ma baser sur la chaîne avant _ dans l'entityid
  537.         $templateSelector = array(
  538.             "CONTRIB"=> [
  539.                 "template" => 'onto/dmlivredorcontrib.html.twig' ,
  540.                 "typestring"=>'Contribution à un sondage',
  541.                 "endpoint" => "https://kg.grains-de-culture.fr/livredor/sparql",
  542.                 "sparql"=>       "SELECT * WHERE { graph ?g { <__uri__> ?p ?o . OPTIONAL {?o ?q ?v . OPTIONAL { ?o <http://givingsense.eu/datamusee/onto/livredor/answer/hasPossibleValue> ?a . ?a <http://givingsense.eu/datamusee/onto/livredor/possibleValue/hasLabel> ?pv }}}}",
  543.                 "structurer" => "contribStructurer"
  544.             ],
  545.             "POSVAL"=> [
  546.                 "template" => 'onto/dmlivredorposval.html.twig' ,
  547.                 "typestring"=>'Valeurs possibles en réponse à une question',
  548.                 "endpoint" => "https://kg.grains-de-culture.fr/livredor/sparql",
  549.                 "sparql"=>       "SELECT * WHERE { graph ?g { <__uri__> ?p ?o . OPTIONAL {?o ?q ?v }}}",
  550.                 "structurer" => "posvalStructurer"
  551.             ],
  552.             "ANSW"=> [
  553.                 "template" => 'onto/dmlivredoransw.html.twig' ,
  554.                 "typestring"=>'Réponse à une question',
  555.                 "endpoint" => "https://kg.grains-de-culture.fr/livredor/sparql",
  556.                 "sparql"=>       "SELECT * WHERE { graph ?g { <__uri__> ?p ?o . OPTIONAL {?o ?q ?v }. }}",
  557.                 "structurer" => "answerStructurer"
  558.             ],
  559.             "APP"=> [
  560.                 "template" => 'onto/dmlivredorappli.html.twig' ,
  561.                 "typestring"=>'Application',
  562.                 "endpoint" => "https://kg.grains-de-culture.fr/livredor/sparql",
  563.                 //"sparql"=>       "SELECT * WHERE { graph ?g { <__uri__> ?p ?o . OPTIONAL {?o ?q ?v }. OPTIONAL { ?v ?l ?w }}}",
  564.                 "sparql"=>       "SELECT * WHERE { graph ?g { <__uri__> ?p ?o . OPTIONAL {?o ?q ?v }. }}",
  565.                 "structurer" => "appliStructurer"
  566.             ],
  567.             "QUEST"=> [ 
  568.                 "template" => 'onto/dmlivredorquest.html.twig' ,
  569.                 "typestring"=>'Question',
  570.                 "endpoint" => "https://kg.grains-de-culture.fr/livredor/sparql",
  571.                 "sparql"=>       "SELECT * WHERE { graph ?g { <__uri__> ?p ?o . OPTIONAL {?o ?q ?v }. }}",
  572.                 "structurer" => "questionStructurer"
  573.             ]);
  574.         $entitytype = strtok($entityid, "_");
  575.         $uri = "http://givingsense.eu/datamusee/onto/livredor/".$entityid;
  576.         $auth = "student:igr2018%"; //$username . ":" . $password;
  577.         $requestURL = self::getAnswerDescription($templateSelector[$entitytype]["endpoint"], 
  578.                                 $templateSelector[$entitytype]["sparql"],
  579.                                 $uri);
  580.         $description = json_decode(self::request($requestURL, $auth), true);
  581.         $descstruct = $this->{$templateSelector[$entitytype]["structurer"]}($description);         
  582.         $descstruct["jsonarray"] = $description;
  583.         $descstruct["url"] = $requestURL;
  584.         $descstruct["uri"] = $uri;
  585.         $descstruct["entityid"] = $entityid;
  586.         $descstruct["entitytype"] = $templateSelector[$entitytype]["typestring"];
  587.         return $this->render($templateSelector[$entitytype]["template"], [
  588.             'descstruct' => $descstruct,
  589.             'entitytype' => $templateSelector[$entitytype]["typestring"],
  590.         ]);    
  591.         //    'description' => self::printArray($description),
  592.     }
  593.     /**
  594.     @Route("/onto/livredor/{entityid}")
  595.     */
  596.     public function livredor($entityid)
  597.     {
  598.         // exemple: http://givingsense.eu/datamusee/onto/livredor/ANSW_5ad8ad19d1c52ba751ccab5c
  599.         // ici, je devrais récupérer le type de l'entité et choisir un template en fonction du type
  600.         // mais pour l'instant (16/6/2019) Tuan n'a pas mis de type sur les entités
  601.         // donc je vais ma baser sur la chaîne avant _ dans l'entityid
  602.         return self::entityLivredorDisplay($entityid);
  603.     }
  604. }