server/src/app/Libraries/Sparql/SparqlClient.php
changeset 109 d22ed5792f8e
parent 28 b0b56e0f8c7f
child 158 366509ae2f37
equal deleted inserted replaced
108:be2d3b30b2e0 109:d22ed5792f8e
     3 
     3 
     4 use EasyRdf\Graph;
     4 use EasyRdf\Graph;
     5 use CorpusParole\Libraries\CorpusParoleException;
     5 use CorpusParole\Libraries\CorpusParoleException;
     6 use Config;
     6 use Config;
     7 use Log;
     7 use Log;
       
     8 use GuzzleHttp\Psr7\Request;
     8 
     9 
     9 /**
    10 /**
    10  *
    11  *
    11  */
    12  */
    12 class SparqlClient {
    13 class SparqlClient {
    36             // We just continue the current transaction
    37             // We just continue the current transaction
    37             return false;
    38             return false;
    38         }
    39         }
    39         //Log::debug('http_client base uri: ' . $this->getHttpClient()->getConfig('base_uri'));
    40         //Log::debug('http_client base uri: ' . $this->getHttpClient()->getConfig('base_uri'));
    40         $sesameRepository = config('corpusparole.sesame_repository');
    41         $sesameRepository = config('corpusparole.sesame_repository');
    41         $resp = $this->getHttpClient()->post("$sesameRepository/transactions");
    42         $resp = $this->getHttpClient()->post("$sesameRepository/transactions", ['query' => ['isolation-level' => 'http://www.openrdf.org/schema/sesame#SNAPSHOT_READ']]);
    42         //$resp = $this->getHttpClient()->post('transactions');
    43         //$resp = $this->getHttpClient()->post('transactions');
    43         //TODO check errors
    44         //TODO check errors
    44         if($resp->getStatusCode() != 201) {
    45         if($resp->getStatusCode() != 201) {
    45             throw new CorpusParoleException("Error when starting transaction : "
    46             throw new CorpusParoleException("Error when starting transaction : "
    46                 . $resp->getStatusCode() . " - "
    47                 . $resp->getStatusCode() . " - "
    95     }
    96     }
    96 
    97 
    97     protected function updateData($operation, Graph $graph)
    98     protected function updateData($operation, Graph $graph)
    98     {
    99     {
    99         $graphUri = $graph->getUri();
   100         $graphUri = $graph->getUri();
   100         $query = "$operation DATA {";
   101 
       
   102         $query = $graph->serialise('ntriples');
   101         if ($graphUri) {
   103         if ($graphUri) {
   102             $query .= "GRAPH <$graphUri> {";
   104             $query = "GRAPH <$graphUri> { $query }";
   103         }
   105         }
   104         $query .= $graph->serialise('ntriples');
   106         $query = "$operation DATA { $query }";
   105         if ($graphUri) {
       
   106             $query .= "}";
       
   107         }
       
   108         $query .= '}';
       
   109 
   107 
       
   108 
       
   109         // doc : http://rdf4j.org/doc/4/articles/REST-API/transaction-operations.docbook?view
       
   110         // cf. bug : https://openrdf.atlassian.net/browse/SES-2295
       
   111         // and PR https://bitbucket.org/openrdf/sesame/commits/62b680d8650caca7bc1673f6aaac48a5b6e85d23?at=2.8.x
       
   112         // The put form has been chosen over the post, because it seems that this is the form choosed in the sesame http client
   110         $resp = $this->getHttpClient()->put(
   113         $resp = $this->getHttpClient()->put(
   111             $this->currentTransactionUrl, [
   114             $this->currentTransactionUrl, [
       
   115                 'headers' => ["Content-Type" => "application/sparql-update; charset=utf-8"],
   112                 'query' => ['action' => 'UPDATE'],
   116                 'query' => ['action' => 'UPDATE'],
   113                 'form_params'=> ['update' => $query],
   117                 'body' => $query,
   114             ]
   118             ]
   115         );
   119         );
   116         if($resp->getStatusCode() != 204) {
   120 
   117             throw new CorpusParoleException("Could not update in transaction with operation $operation: "
       
   118                 . $resp->getStatusCode() . " - "
       
   119                 . $resp->getReasonPhrase() . " : " . $resp->getBody()
       
   120                 . " : $query" , 1);
       
   121         }
       
   122     }
   121     }
   123 
   122 
   124     public function add(Graph $graph) {
   123     public function add(Graph $graph) {
   125         $this->updateData('INSERT', $graph);
   124         $this->updateData('INSERT', $graph);
   126     }
   125     }