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.        $chcurl_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($chCURLOPT_SSL_VERIFYHOST0);
  37.        curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
  38.        curl_setopt($chCURLOPT_HTTPAUTHCURLAUTH_BASIC);
  39.        curl_setopt($chCURLOPT_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.         <__uri__> ?p ?o . 
  133.         optional { <__uri__>  <http://dbpedia.org/ontology/city> ?city .
  134.             optional { ?city skos:prefLabel ?cityname } } 
  135.         } UNION
  136.         {
  137.             ?s owl:sameAs <__uri__> . 
  138.             ?s ?p ?o .
  139.             optional { ?s  <http://dbpedia.org/ontology/city> ?city .
  140.                 optional { 
  141.                     ?city skos:prefLabel ?cityname
  142.                     filter(lang(?cityname)='fr' || lang(?cityname)="")
  143.                  } 
  144.             } 
  145.     }
  146.     optional {
  147.         <__uri__> wdt:P31 ?o .
  148.         ?o rdfs:label ?label .
  149.         filter(lang(?label)='en' ||lang(?label)='fr' || lang(?label)="")
  150.     }     
  151. }
  152. }
  153. txt;
  154.         $descstruct = array();
  155.         $descstruct["entityid"] = $descstruct["altLabel"] = $descstruct["uriville"] =
  156.         $descstruct["latitude"] = $descstruct["longitude"] = $descstruct["sameAs"] =
  157.         $descstruct["ville"] = $descstruct["class"] = ""// tempo
  158.         $mainquery =  str_replace"__uri__" $uri $sparqlTemplate);
  159.         $requestURL $desc["endpoint"].'?'
  160.             .'query='.urlencode($mainquery)
  161.             .'&format='.urlencode($format);
  162.         $description json_decode(self::request($requestURL$auth), true);
  163.         // $description = self::request($requestURL, $auth);
  164.         $descstruct self::siteStructurer($descstruct$description);
  165.         $count self::getGuestBookContribCount("https://kg.grains-de-culture.fr/dm/sparql",$uri);
  166.         $descstruct["countcontrib"] = $count["countcontrib"];
  167.         $descstruct["countuser"] =  $count["countuser"];
  168.         $descstruct["countcontrib"] = $count["countcontrib"];
  169.         $descstruct["salescount"] =  0;
  170.         $descstruct["productscount"] = 0;
  171.         $descstruct["buyerscount"] =  0;
  172.         $classTemplate $prefixes . <<<txt
  173. SELECT distinct ?o ?label ?super ?superlabel 
  174. WHERE { 
  175.   <__uri__> wdt:P31 ?o .
  176.       ?o  <http://www.wikidata.org/prop/direct/P279>* ?super.
  177.   ?o rdfs:label ?label .
  178.   ?super rdfs:label ?superlabel .
  179.   bind(lang(?superlabel) as ?lang)
  180.   filter((lang(?superlabel)='en') ||(lang(?superlabel)='en') ) 
  181.      }
  182.   
  183. txt;
  184.         $queryClasses =  str_replace"__uri__" $uri $classTemplate);
  185.         $requestURL $desc["endpoint"].'?'
  186.             .'query='.urlencode($queryClasses)
  187.             .'&format='.urlencode($format);
  188.         $classes json_decode(self::request($requestURL$auth), true);
  189.          foreach ($classes["results"]["bindings"] as $item) {
  190.              $lang "";
  191.              if(array_key_exists("xml:lang"$item["label"]))
  192.                  $lang $item["label"]["xml:lang"];
  193.             if ($lang=="en"$descstruct["class"][$item["o"]["value"]] = [ "class"=> $item["o"]["value"], "label" =>$item["label"]["value"], "lang"=>$lang];
  194.             if(array_key_exists("super"$item)){
  195.                 if(array_key_exists("xml:lang"$item["superlabel"])) $lang $item["superlabel"]["xml:lang"]; else $lang "";
  196.                 if ($lang=="en")
  197.                     $descstruct["class"][$item["super"]["value"]] = [ "class"=> $item["super"]["value"], "label" =>$item["superlabel"]["value"], "lang"=>$lang];
  198.             }
  199.         }
  200.         $descstruct["dataset"] = "dmsites";
  201.         $descstruct["triplestore"] = "https://kg.grains-de-culture.fr/";
  202.         $descstruct["query"] = $mainquery;
  203.         $descstruct["jsonarray"] = $description;
  204.         $descstruct["url"] = $requestURL;
  205.         $descstruct["uri"] = $uri;
  206.         $descstruct["debug"] = $mainquery;
  207.         $descstruct["entitytype"] = "Site culturel";
  208.         $template 'site/dmcmnsite.html.twig';
  209.         if ($debug$template "testTemplateSite.html.twig";
  210.         return $this->render($template, [
  211.         //return $this->render('testTemplateSite.html.twig', [
  212.             'desc' => $desc,
  213.             'descstruct' => $descstruct,
  214.             'entitytype' => $descstruct["entitytype"],
  215.             'description' => $description
  216.         ]);
  217.     }
  218.     function getGuestBookContribCount($endpoint$urisite)
  219.     {
  220.         $sparqltemplate = <<<txt
  221. PREFIX schema: <http://schema.org/>
  222. select (count(distinct ?s) as ?cl)(count(distinct ?user) as ?cu) 
  223. where
  224. {
  225.   values ?graph { <http://datamusee.givingsense.eu/livredor/parismusees> <http://datamusee.givingsense.eu/livredor/cmn> }
  226.     graph ?graph {
  227.     ?app schema:about <__uri__> .
  228.     ?s a <http://datamusee.givingsense.eu/onto/contribution>; 
  229.        <http://datamusee.givingsense.eu/onto/contribution/hasUser> ?user;
  230.        <http://datamusee.givingsense.eu/onto/contribution/hasAppli> ?app . 
  231.   }
  232. txt;
  233.         $format "application/sparql-results+json";
  234.         $query =  str_replace"__uri__" $urisite $sparqltemplate);
  235.         $auth "student:igr2018%"// le dataset dmmeta devrait être en libre accès
  236.         $requestURL $endpoint.'?'
  237.             .'query='.urlencode($query)
  238.             .'&format='.urlencode($format);
  239.         $res json_decode(self::request($requestURL$auth), true);
  240.         $count = array(); $count["countcontrib"] = $count["countuser"] = 0;
  241.         if ($res) {
  242.             $count["countcontrib"]  = $res["results"]["bindings"][0]["cl"]["value"];
  243.             $count["countuser"] = $res["results"]["bindings"][0]["cu"]["value"];
  244.         }
  245.         //return $query;
  246.         return $count;
  247.     }
  248.     /*
  249.     http://givingsense.eu/datamusee/onto/cmn/categories
  250.      */
  251.     /**
  252.     @Route("/onto/events")
  253.      */
  254.     public function events()
  255.     {
  256.         $desc = array();
  257.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/datamusee/query";
  258.         $desc["dataset"] = "datamusee";
  259.         $desc["graph"] = "http://givingsense.eu/datamusee/onto/events";
  260.         $desc["comment"] = "Graphe servant de test à l'aspiration de données sur des événements. Un dataset spécifique va être constitué. ";
  261.         return self::minGraphDescription($desc);
  262.     }
  263.     /**
  264.     @Route("/onto/sameAs")
  265.      */
  266.     public function sameAs()
  267.     {
  268.         $desc = array();
  269.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/datamusee/query";
  270.         $desc["dataset"] = "datamusee";
  271.         $desc["graph"] = "http://givingsense.eu/datamusee/onto/sameAs";
  272.         $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'";
  273.         return self::minGraphDescription($desc);
  274.     }
  275.     /**
  276.     @Route("/onto/contextgraph18022019")
  277.      */
  278.     public function contextgraph()
  279.     {
  280.         $desc = array();
  281.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/datamusee/query";
  282.         $desc["dataset"] = "datamusee";
  283.         $desc["graph"] = "http://givingsense.eu/datamusee/onto/contextgraph18022019";
  284.         $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. ";
  285.         return self::minGraphDescription($desc);
  286.     }
  287.     /**
  288.     @Route("/onto/joconde18022019")
  289.      */
  290.     public function joconde()
  291.     {
  292.         $desc = array();
  293.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/datamusee/query";
  294.         $desc["dataset"] = "datamusee";
  295.         $desc["graph"] = "http://givingsense.eu/datamusee/onto/joconde18022019";
  296.         $desc["comment"] = "Représentation en graphe de connaissances par transcription de la base Joconde. ";
  297.         return self::minGraphDescription($desc);
  298.     }
  299.     /**
  300.     @Route("/onto/datatourisme")
  301.      */
  302.     public function datatourisme()
  303.     {
  304.         $desc = array();
  305.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/datamusee/query";
  306.         $desc["dataset"] = "datamusee";
  307.         $desc["graph"] = "http://givingsense.eu/datamusee/onto/datatourisme";
  308.         $desc["comment"] = "Extrait de la base DataTourisme; données issues des offices de tourisme français; extrait pour prise en main du contenu. ";
  309.         return self::minGraphDescription($desc);
  310.     }
  311.     /**
  312.     @Route("/onto/geonames")
  313.      */
  314.     public function geonames()
  315.     {
  316.         $desc = array();
  317.         $desc["endpoint"] = "https://kg.grains-de-culture.fr/datamusee/query";
  318.         $desc["dataset"] = "datamusee";
  319.         $desc["graph"] = "http://givingsense.eu/datamusee/onto/geonames";
  320.         $desc["comment"] = "Extrait de la base geonames; données sur les entités géographiques françaises. ";
  321.         return self::minGraphDescription($desc);
  322.     }
  323.     function minGraphDescription($desc)
  324.     {
  325.         $desc["triplestore"] = "https://kg.grains-de-culture.fr/";
  326.         return $this->render("graph/dmminimalgraph.html.twig", [
  327.             'descstruct' => $desc,
  328.             'entitytype' => "Graphe",
  329.         ]);
  330.     }
  331.     /**
  332.     @Route("/onto/billetterieExtraitPM")
  333.      */
  334.     public function ontoTech()
  335.     {
  336.         return new Response(
  337.             '<!doctype html>
  338.                 <html lang="fr">
  339.                 <head>
  340.                 <meta charset="UTF-8"></head><body>Graphe à usage technique interne. Pas de description publique.</body></html>'
  341.         );
  342.     }
  343.     /**
  344.     @Route("/onto")
  345.      */
  346.     public function onto()
  347.     {
  348.         return new Response(
  349.             '<!doctype html>
  350.                 <html lang="fr">
  351.                 <head>
  352.                 <meta charset="UTF-8"></head><body>Base du déréférencement des URIs créées pour le projet Data&Musée</body></html>'
  353.         );
  354.     }
  355.     function contribStructurer($description)
  356.     {
  357.         $descstruct = array();
  358.         $descstruct["predicates"] = array();
  359.         $descstruct["questions"] = array();
  360.         $descstruct["reponses"] = array();
  361.         $descstruct["possiblevalues"] = array();
  362.         $lienQuestionAnswer = array();
  363.         $labelQuestion = array();
  364.         foreach ($description["results"]["bindings"] as $item) {
  365.             $predicate $item["p"]["value"];
  366.             if (!in_array($predicate$descstruct["predicates"], TRUE)) {
  367.               array_push($descstruct["predicates"], $predicate);
  368.             } 
  369.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/contribution/hasQuestion"
  370.             {
  371.               if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/question/hasLabel"
  372.               {
  373.                 if ($item["v"]["xml:lang"]==="fr"
  374.                 {
  375.                   $labelQuestion[$item["o"]["value"]] = $item["v"]["value"];
  376.                 }
  377.               }
  378.             }
  379.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/contribution/hasAnswer") {
  380.               if (array_key_exists("pv"$item)) 
  381.               {
  382.                 if ($item["pv"]["xml:lang"]==="fr"
  383.                 {
  384.                     $labelReponses[$item["o"]["value"]] =  $item["pv"]["value"];  // idreponse -> valeur reponse
  385.                 }
  386.               }  elseif ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/answer/hasValue"
  387.               {
  388.                   $labelReponses[$item["o"]["value"]] =  $item["v"]["value"];  // idreponse -> valeur reponse
  389.               }
  390.               if   ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/answer/belongQuestion"
  391.               {
  392.                   $lienQuestionAnswer[$item["v"]["value"]] = $item["o"]["value"]; // uriquestion->urireponse
  393.               }
  394.             }
  395.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/contribution/hasAppli") {
  396.                 if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/application/appName") {
  397.                   $descstruct["appli"] = $item["o"]["value"];
  398.                   $descstruct["appliname"] = $item["v"]["value"];
  399.                 }
  400.                 if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/application/clientName") {
  401.                   $descstruct["clientname"] = $item["v"]["value"];
  402.                 }
  403.                 if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/application/hasId") {
  404.                   $descstruct["idappli"] = $item["v"]["value"];
  405.                 }
  406.             }
  407.         }
  408.         foreach ($lienQuestionAnswer as $idquestion => $idreponse) {
  409.             if (array_key_exists($idquestion$lienQuestionAnswer) )
  410.             {
  411.               if ($labelReponses[$lienQuestionAnswer[$idquestion]]==="-1")
  412.               { 
  413.                 $strrep "Pas de réponse";    $urirep "";
  414.               } 
  415.               else  
  416.               {
  417.                   $strrep$labelReponses[$lienQuestionAnswer[$idquestion]];      $urirep =  $lienQuestionAnswer[$idquestion];
  418.               } 
  419.               $rep = [ "uriquestion" => $idquestion"question" => $labelQuestion[$idquestion], "urireponse" => $urirep"reponse" => $strrep ];
  420.             } 
  421.             else
  422.             {
  423.               $rep = [ "uriquestion" => $idquestion"question" => $labelQuestion[$idquestion], "urireponse" => """reponse" => "" ];
  424.             }
  425.             array_push($descstruct["questions"], $rep);
  426.         }
  427.       return $descstruct;
  428.     }
  429.     function questionStructurer($description)
  430.     {
  431.         $descstruct = array();
  432.         $descstruct["predicates"] = array();
  433.         $descstruct["labels"] = array();
  434.         $descstruct["reponses"] = array();
  435.         $descstruct["category"] = "??";
  436.         $descstruct["uriquestion"] = "??";
  437.         $descstruct["question"] = "??";
  438.         foreach ($description["results"]["bindings"] as $item) {
  439.             $predicate $item["p"]["value"];
  440.             if (!in_array($predicate$descstruct["predicates"], TRUE)) {
  441.               array_push($descstruct["predicates"], $predicate);
  442.             } 
  443.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/question/hasLabel") {
  444.                 array_push($descstruct["labels"], ["lang"=> $item["o"]["xml:lang"], "label"=> $item["o"]["value"] ]);
  445.             }
  446.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/question/hasPossibleValue") {
  447.               $descstruct["posval"] = $item["o"]["value"]; 
  448.               if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/possibleValue/hasLabel") { 
  449.                     array_push($descstruct["reponses"],  [ "label" =>$item["v"]["value"], "lang"=>$item["v"]["xml:lang"]]);
  450.               }
  451.             }
  452.         }
  453.       return $descstruct;
  454.     }
  455.     function appliStructurer($description)
  456.     {
  457.         $descstruct = array();
  458.         $descstruct["predicates"] = array();
  459.         $descstruct["reponses"] = array();
  460.         $descstruct["possiblevalues"] = array();
  461.         // $descstruct["category"] = "??";
  462.         //$descstruct["uriquestion"] = "??";
  463.         //$descstruct["question"] = "??";
  464.         foreach ($description["results"]["bindings"] as $item) {
  465.             $predicate $item["p"]["value"];
  466.             if (!in_array($predicate$descstruct["predicates"], TRUE)) {
  467.               array_push($descstruct["predicates"], $predicate);
  468.             } 
  469.         }
  470.       return $descstruct;
  471.     }
  472.     
  473.     function answerStructurer($description)
  474.     {
  475.         $descstruct = array();
  476.         $descstruct["predicates"] = array();
  477.         $descstruct["reponses"] = array();
  478.         $descstruct["possiblevalues"] = array();
  479.         foreach ($description["results"]["bindings"] as $item) {
  480.             $predicate $item["p"]["value"];
  481.             if (!in_array($predicate$descstruct["predicates"], TRUE)) {
  482.               array_push($descstruct["predicates"], $predicate);
  483.             } 
  484.             if ($item["p"]["value"]==="http://www.w3.org/1999/02/22-rdf-syntax-ns#type") {
  485.                   $descstruct["typelink"] = $item["o"]["value"];
  486.             }
  487.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/answer/belongQuestion") {
  488.               if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/question/hasLabel"
  489.               {
  490.                 if ($item["v"]["xml:lang"]==="fr"
  491.                 {
  492.                   $descstruct["question"] = $item["v"]["value"];
  493.                   $descstruct["uriquestion"] = $item["o"]["value"];
  494.                 }
  495.               }
  496.               if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/question/hasType"
  497.               {
  498.                 $descstruct["category"] = $item["v"]["value"];
  499.               }
  500.             }
  501.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/answer/hasPossibleValue") {
  502.               if ($item["q"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/possibleValue/hasLabel") { 
  503.                 array_push($descstruct["reponses"], ["lang"=> $item["v"]["xml:lang"], "label"=> $item["v"]["value"] ]);
  504.               }
  505.             }
  506.         }
  507.       return $descstruct;
  508.     }
  509.     function posvalStructurer($description)
  510.     {
  511.         $descstruct = array();
  512.         $descstruct["predicates"] = array();
  513.         $descstruct["labels"] = array();
  514.         $descstruct["possiblevalues"] = array();
  515.         foreach ($description["results"]["bindings"] as $item) {
  516.             $predicate $item["p"]["value"];
  517.             if (!in_array($predicate$descstruct["predicates"], TRUE)) {
  518.               array_push($descstruct["predicates"], $predicate);
  519.             } 
  520.             if ($item["p"]["value"]==="http://www.w3.org/1999/02/22-rdf-syntax-ns#type") {
  521.                   $descstruct["typelink"] = $item["o"]["value"];
  522.             }
  523.             if ($item["p"]["value"]==="http://givingsense.eu/datamusee/onto/livredor/possibleValue/hasLabel") { 
  524.                 array_push($descstruct["labels"], ["lang"=> $item["o"]["xml:lang"], "label"=> $item["o"]["value"] ]);
  525.             }
  526.         }
  527.       return $descstruct;
  528.     }
  529.     function entityLivredorDisplay($entityid)
  530.     {
  531.         // exemple: http://givingsense.eu/datamusee/onto/livredor/ANSW_5ad8ad19d1c52ba751ccab5c
  532.         // ici, je devrais récupérer le type de l'entité et choisir un template en fonction du type
  533.         // mais pour l'instant (16/6/2019) Tuan n'a pas mis de type sur les entités
  534.         // donc je vais ma baser sur la chaîne avant _ dans l'entityid
  535.         $templateSelector = array(
  536.             "CONTRIB"=> [
  537.                 "template" => 'onto/dmlivredorcontrib.html.twig' ,
  538.                 "typestring"=>'Contribution à un sondage',
  539.                 "endpoint" => "https://kg.grains-de-culture.fr/livredor/sparql",
  540.                 "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 }}}}",
  541.                 "structurer" => "contribStructurer"
  542.             ],
  543.             "POSVAL"=> [
  544.                 "template" => 'onto/dmlivredorposval.html.twig' ,
  545.                 "typestring"=>'Valeurs possibles en réponse à une question',
  546.                 "endpoint" => "https://kg.grains-de-culture.fr/livredor/sparql",
  547.                 "sparql"=>       "SELECT * WHERE { graph ?g { <__uri__> ?p ?o . OPTIONAL {?o ?q ?v }}}",
  548.                 "structurer" => "posvalStructurer"
  549.             ],
  550.             "ANSW"=> [
  551.                 "template" => 'onto/dmlivredoransw.html.twig' ,
  552.                 "typestring"=>'Réponse à une question',
  553.                 "endpoint" => "https://kg.grains-de-culture.fr/livredor/sparql",
  554.                 "sparql"=>       "SELECT * WHERE { graph ?g { <__uri__> ?p ?o . OPTIONAL {?o ?q ?v }. }}",
  555.                 "structurer" => "answerStructurer"
  556.             ],
  557.             "APP"=> [
  558.                 "template" => 'onto/dmlivredorappli.html.twig' ,
  559.                 "typestring"=>'Application',
  560.                 "endpoint" => "https://kg.grains-de-culture.fr/livredor/sparql",
  561.                 //"sparql"=>       "SELECT * WHERE { graph ?g { <__uri__> ?p ?o . OPTIONAL {?o ?q ?v }. OPTIONAL { ?v ?l ?w }}}",
  562.                 "sparql"=>       "SELECT * WHERE { graph ?g { <__uri__> ?p ?o . OPTIONAL {?o ?q ?v }. }}",
  563.                 "structurer" => "appliStructurer"
  564.             ],
  565.             "QUEST"=> [ 
  566.                 "template" => 'onto/dmlivredorquest.html.twig' ,
  567.                 "typestring"=>'Question',
  568.                 "endpoint" => "https://kg.grains-de-culture.fr/livredor/sparql",
  569.                 "sparql"=>       "SELECT * WHERE { graph ?g { <__uri__> ?p ?o . OPTIONAL {?o ?q ?v }. }}",
  570.                 "structurer" => "questionStructurer"
  571.             ]);
  572.         $entitytype strtok($entityid"_");
  573.         $uri "http://givingsense.eu/datamusee/onto/livredor/".$entityid;
  574.         $auth "student:igr2018%"//$username . ":" . $password;
  575.         $requestURL self::getAnswerDescription($templateSelector[$entitytype]["endpoint"], 
  576.                                 $templateSelector[$entitytype]["sparql"],
  577.                                 $uri);
  578.         $description json_decode(self::request($requestURL$auth), true);
  579.         $descstruct $this->{$templateSelector[$entitytype]["structurer"]}($description);         
  580.         $descstruct["jsonarray"] = $description;
  581.         $descstruct["url"] = $requestURL;
  582.         $descstruct["uri"] = $uri;
  583.         $descstruct["entityid"] = $entityid;
  584.         $descstruct["entitytype"] = $templateSelector[$entitytype]["typestring"];
  585.         return $this->render($templateSelector[$entitytype]["template"], [
  586.             'descstruct' => $descstruct,
  587.             'entitytype' => $templateSelector[$entitytype]["typestring"],
  588.         ]);    
  589.         //    'description' => self::printArray($description),
  590.     }
  591.     /**
  592.     @Route("/onto/livredor/{entityid}")
  593.     */
  594.     public function livredor($entityid)
  595.     {
  596.         // exemple: http://givingsense.eu/datamusee/onto/livredor/ANSW_5ad8ad19d1c52ba751ccab5c
  597.         // ici, je devrais récupérer le type de l'entité et choisir un template en fonction du type
  598.         // mais pour l'instant (16/6/2019) Tuan n'a pas mis de type sur les entités
  599.         // donc je vais ma baser sur la chaîne avant _ dans l'entityid
  600.         return self::entityLivredorDisplay($entityid);
  601.     }
  602. }