src/Controller/GraphController.php line 60

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\Bundle\FrameworkBundle\Controller\AbstractController;
  7. class GraphController  extends AbstractController
  8. {
  9.     function request($url$auth){ 
  10.        // is curl installed?
  11.        if (!function_exists('curl_init')){ 
  12.           die('CURL is not installed!');
  13.        }
  14.        // get curl handle
  15.        $chcurl_init();
  16.        // set request url
  17.        curl_setopt($ch
  18.           CURLOPT_URL
  19.           $url);
  20.        // return response, don't print/echo
  21.        curl_setopt($ch
  22.           CURLOPT_RETURNTRANSFER
  23.           TRUE);
  24.           // 2 lignes suivantes dangereuses, voir comment gérer des certificats
  25.        curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
  26.        curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
  27.        curl_setopt($chCURLOPT_HTTPAUTHCURLAUTH_BASIC);
  28.        curl_setopt($chCURLOPT_USERPWD$auth); 
  29.        /*
  30.        Here you find more options for curl:
  31.        http://www.php.net/curl_setopt
  32.        */        
  33.        $response curl_exec($ch);
  34.         if (curl_error($ch)) {
  35.             $error_msg curl_error($ch);
  36.         }
  37.        curl_close($ch);
  38.         if ($response === FALSE) {
  39.             return "erreur ".$error_msg." dans l'appel de ".$url;
  40.         }
  41.        return $response;
  42.     }
  43.     /**
  44.     @Route("/graph/jocondevoc}")
  45.      */
  46.     public function mccVocabulaireDAutoriteGraphBySite($entityid)
  47.     {
  48.         return self::graphDescriptionDisplay($entityid"MCCJOCONDEVOCABULARY");
  49.         # return self::graphDescriptionDisplay($entityid, "GENERICVOID"); to be done: a generic display from auto-description of a graph (ex:VOID or DCAT)
  50.     }
  51.     /**
  52.     @Route("/graph/billetterie/site/{entityid}")
  53.      */
  54.     public function billetterieGraphBySite($entityid)
  55.     {
  56.         return self::graphDescriptionDisplay($entityid"GRAPHTICKETINGBYSITE");
  57.     }
  58.     /**
  59.     @Route("/billetterie/{entityid}")
  60.     */
  61.     public function billetterieGraph($entityid)
  62.     {
  63.         return self::graphDescriptionDisplay($entityid"GRAPHBILLETTERIE");
  64.     }
  65.     function graphStructurer($description)
  66.     {
  67.         $descstruct = array();
  68.           $descstruct["predicates"] = array();
  69.         foreach ($description["results"]["bindings"] as $item) {
  70.             $predicate $item["p"]["value"];
  71.             if (!in_array($predicate$descstruct["predicates"], TRUE)) {
  72.               array_push($descstruct["predicates"], $predicate);
  73.             }
  74.         }
  75.       return $descstruct;
  76.     }
  77.     /**
  78.     @Route("/livredor/{entityid}")
  79.     */
  80.     public function livredorGraph($entityid)
  81.     {
  82.         // exemple: http://givingsense.eu/datamusee/onto/livredor/ANSW_5ad8ad19d1c52ba751ccab5c
  83.         // ici, je devrais récupérer le type de l'entité et choisir un template en fonction du type
  84.         // mais pour l'instant (16/6/2019) Tuan n'a pas mis de type sur les entités
  85.         // donc je vais ma baser sur la chaîne avant _ dans l'entityid
  86.         return self::graphDescriptionDisplay($entityid"GRAPHLIVREDOR");
  87.     }
  88.     function getRequestUrl($endpoint$query)
  89.     {
  90.         $format "application/sparql-results+json";
  91.         $searchUrl $endpoint.'?'
  92.             .'query='.urlencode($query)
  93.             .'&format='.urlencode($format);
  94.         return $searchUrl;
  95.     }
  96.     function graphDescriptionDisplay($entityid$graphtype)
  97.     {
  98.         // exemple: http://datamusee.givingsense.eu/livredor/parismusees
  99.         $templateSelector = array(
  100.             "MCCJOCONDEVOCABULARY"=>[
  101.                 "template" => 'graph/mccjocondegraph.html.twig' ,
  102.                 "subdir" => "",
  103.                 "typestring"=>"Graphe de données des vocabulaires de référence (listes d'autorité) du Ministère de la Culture Français",
  104.                 "endpoint" => "https://kg.grains-de-culture.fr/JocondeSkosMCC/sparql",
  105.                 "sparqltype"=>  "http://datamusee.givingsense.eu/onto/contribution",
  106.                 "structurer" => "graphStructurer" # à vérifier
  107.             ],
  108.             "GRAPHLIVREDOR"=> [
  109.                 "template" => 'graph/dmlivredorgraph.html.twig' ,
  110.                 "subdir" => "livredor",
  111.                 "typestring"=>"Graphe de données de livres d'or",
  112.                 "endpoint" => "https://kg.grains-de-culture.fr/dm/sparql",
  113.                 "sparqltype"=>  "http://datamusee.givingsense.eu/onto/contribution",
  114.                 "structurer" => "graphStructurer"
  115.             ],
  116.             "GRAPHBILLETTERIE"=> [
  117.                 "template" => 'graph/dmbilletteriegraph.html.twig' ,
  118.                 "subdir" => "billetterie",
  119.                 "typestring"=>"Graphe de données de billetterie",
  120.                 "endpoint" => "https://kg.grains-de-culture.fr/dm/sparql",
  121.                 "sparqltype"=>  "http://schema.org/SellAction",
  122.                 "structurer" => "graphStructurer"
  123.             ],
  124.             "GRAPHTICKETINGBYSITE"=> [
  125.                 "template" => 'graph/dmticketingbysitegraph.html.twig' ,
  126.                 "subdir" => "graph/billetterie/site",
  127.                 "typestring"=>"Graphe de données de billetterie",
  128.                 "endpoint" => "https://kg.grains-de-culture.fr/dm/sparql",
  129.                 "sparqltype"=>  "http://schema.org/SellAction",
  130.                 "structurer" => "graphStructurer"
  131.             ]
  132.         );
  133.         $graphname "http://datamusee.givingsense.eu/".$templateSelector[$graphtype]["subdir"]."/".$entityid;
  134.         $auth "student:igr2018%"//$username . ":" . $password;
  135.         $sparqlquerypredicates "SELECT distinct ?p  WHERE { graph <".$graphname."> { ?s a <".$templateSelector[$graphtype]["sparqltype"].">; ?p [] }} ";
  136.         $sparqlquerycount "SELECT  (count(distinct ?s) as ?c) WHERE { graph <".$graphname."> { ?s a <".$templateSelector[$graphtype]["sparqltype"]."> }}";
  137.         $sparqlquerysite "SELECT distinct ?urisite ?sitename WHERE { graph <" $graphname "> { ?s a <" $templateSelector[$graphtype]["sparqltype"] . ">;  <http://datamusee.givingsense.eu/onto/site/hasSite> ?urisite } graph ?g { ?urisite <http://schema.org/name> ?sitename }}";
  138.         $sitename "";
  139.         $urisite "";
  140.         if (strcmp($graphtype,"GRAPHTICKETINGBYSITE")==0) {
  141.             $requestURL self::getRequestUrl($templateSelector[$graphtype]["endpoint"], $sparqlquerysite);
  142.             $descsite json_decode(self::request($requestURL$auth), true);
  143.             foreach ($descsite["results"]["bindings"] as $item) {
  144.                 $sitename $item["sitename"]["value"];
  145.                 $urisite $item["urisite"]["value"];
  146.             }
  147.         }
  148.         $uri "http://datamusee.givingsense.eu/".$templateSelector[$graphtype]["subdir"]."/".$entityid;
  149.         $requestURL self::getRequestUrl($templateSelector[$graphtype]["endpoint"], $sparqlquerypredicates);
  150.         $description json_decode(self::request($requestURL$auth), true);
  151.         $descstruct $this->{$templateSelector[$graphtype]["structurer"]}($description);
  152.         $requestURL self::getRequestUrl($templateSelector[$graphtype]["endpoint"], $sparqlquerycount);
  153.         $description json_decode(self::request($requestURL$auth), true);
  154.         foreach ($description["results"]["bindings"] as $item) {
  155.             $descstruct["count"] = $item["c"]["value"];
  156.         }
  157.         $descstruct["triplestore"] = "https://kg.grains-de-culture.fr/";
  158.         $descstruct["endpoint"] = $templateSelector[$graphtype]["endpoint"];
  159.         $descstruct["dataset"] = "dm";
  160.         $descstruct["jsonarray"] = $description;
  161.         $descstruct["url"] = $requestURL;
  162.         $descstruct["uri"] = $uri;
  163.         $descstruct["sitename"] = $sitename;
  164.         $descstruct["urisite"] = $urisite;
  165.         $descstruct["entityid"] = $entityid;
  166.         $descstruct["entitytype"] = $templateSelector[$graphtype]["typestring"];
  167.         return $this->render($templateSelector[$graphtype]["template"], [
  168.             'descstruct' => $descstruct,
  169.             'entitytype' => $templateSelector[$graphtype]["typestring"],
  170.         ]);
  171.         //    'description' => self::printArray($description),
  172.     }
  173. }