server/src/app/Services/BnfResolver.php
changeset 506 8a5bb4b48b85
parent 158 366509ae2f37
equal deleted inserted replaced
505:9175ea22f1b1 506:8a5bb4b48b85
     1 <?php
     1 <?php
     2 namespace CorpusParole\Services;
     2 namespace CorpusParole\Services;
     3 
     3 
     4 use Cache;
     4 use Cache;
     5 use CorpusParole\Services\BnfResolverInterface;
     5 use CorpusParole\Services\BnfResolverInterface;
       
     6 use CorpusParole\Services\BnfResolverTimeoutException;
       
     7 use EasyRdf;
     6 
     8 
     7 class BnfResolver implements BnfResolverInterface {
     9 class BnfResolver implements BnfResolverInterface {
     8 
    10 
     9     public function __construct($sparqlClient) {
    11     public function __construct($sparqlClient) {
    10         $this->sparqlClient = $sparqlClient;
    12         $this->sparqlClient = $sparqlClient;
    82             }
    84             }
    83             $query .= "{ <$bid> <http://www.w3.org/2004/02/skos/core#prefLabel> ?o. ?s <http://www.w3.org/2004/02/skos/core#prefLabel> ?o. FILTER(?s = <$bid> && lang(?o) = \"fr\")}";
    85             $query .= "{ <$bid> <http://www.w3.org/2004/02/skos/core#prefLabel> ?o. ?s <http://www.w3.org/2004/02/skos/core#prefLabel> ?o. FILTER(?s = <$bid> && lang(?o) = \"fr\")}";
    84         }
    86         }
    85         $query .= "}";
    87         $query .= "}";
    86 
    88 
    87         $docs = $this->sparqlClient->query($query);
    89         try {
       
    90             $docs = $this->sparqlClient->query($query);
       
    91         } catch (EasyRdf\Exception $e) {
       
    92             $code = 0;
       
    93             if(method_exists($e, 'getCode')) {
       
    94                 $code = $e->getCode();
       
    95             }
       
    96             $message = $e->getMessage();
       
    97             if($code == 400 || ($code == 0 && stripos($message, 'timed out')>=0) ) {
       
    98                 throw new BnfResolverTimeoutException("Query to bnf server timed out.");
       
    99             }
       
   100             // reraise the original exception
       
   101             throw $e;
       
   102         }
    88 
   103 
    89         $resultsRaw = [];
   104         $resultsRaw = [];
    90 
   105 
    91         foreach ($docs as $doc) {
   106         foreach ($docs as $doc) {
    92             $bnfid = $doc->s->getUri();
   107             $bnfid = $doc->s->getUri();