server/src/app/Libraries/Sparql/SparqlClient.php
author ymh <ymh.work@gmail.com>
Tue, 20 Mar 2018 15:02:40 +0100
changeset 573 25f3d28f51b2
parent 405 f239c8c5bb94
permissions -rw-r--r--
Added tag 0.0.25 for changeset 190ae1dee68d

<?php
namespace CorpusParole\Libraries\Sparql;

use EasyRdf\Graph;
use CorpusParole\Libraries\CorpusParoleException;
use Config;
use Log;
use GuzzleHttp\Psr7\Request;

/**
 *
 */
class SparqlClient {

    private $currentTransactionUrl = null;

    function __construct($httpClient, $sparqlClient) {
        $this->sparqlClient = $sparqlClient;
        $this->httpClient = $httpClient;
    }

    private function getHttpClient() {
        return $this->httpClient;
    }

    public function getCurrentTransactionUrl() {
        return $this->currentTransactionUrl;
    }

    /**
     * Start transaction.
     * @return boolean true if a transaction was started, false otherwise
     * @throws CorpusParoleException if the  transaction could not be stared
     */
    public function startTransaction() {
        if(!is_null($this->currentTransactionUrl)) {
            // We just continue the current transaction
            return false;
        }
        //Log::debug('http_client base uri: ' . $this->getHttpClient()->getConfig('base_uri'));
        $rdf4jRepository = config('corpusparole.rdf4j_repository');
        $resp = $this->getHttpClient()->post("$rdf4jRepository/transactions", ['query' => ['isolation-level' => 'http://www.openrdf.org/schema/sesame#SNAPSHOT_READ']]);
        //$resp = $this->getHttpClient()->post('transactions');
        //TODO check errors
        if($resp->getStatusCode() != 201) {
            throw new CorpusParoleException("Error when starting transaction : "
                . $resp->getStatusCode() . " - "
                . $resp->getReasonPhrase() . " : " . $resp->getBody(), 1);
        }

        $this->currentTransactionUrl = $resp->getHeader('Location')[0];

        return true;
    }

    /**
     * Rollback transaction.
     * @return boolean true if a transaction was started, false otherwise
     * @throws CorpusParoleException if the  transaction could not be rollbacked
     */
    public function rollback() {
        if(is_null($this->currentTransactionUrl)) {
            // We just continue the current transaction
            return false;
        }
        $resp = $this->getHttpClient()->delete($this->currentTransactionUrl);

        if($resp->getStatusCode() != 204) {
            throw new CorpusParoleException("Could not cancel transaction : "
                . $resp->getStatusCode() . " - "
                . $resp->getReasonPhrase() . " : " . $resp->getBody(), 1);
        }
        $this->currentTransactionUrl = null;

        return true;
    }

    /**
     * Commit transaction.
     * @return boolean true if a transaction was started, false otherwise
     * @throws CorpusParoleException if the  transaction could not be commited
     */
    public function commit() {
        if(is_null($this->currentTransactionUrl)) {
            // We just continue the current transaction
            return false;
        }
        $resp = $this->getHttpClient()->put($this->currentTransactionUrl, ['query' => ['action' => 'COMMIT']]);
        if($resp->getStatusCode() != 200) {
            throw new CorpusParoleException("Could not commit transaction : "
                . $resp->getStatusCode() . " - "
                . $resp->getReasonPhrase() . " : " . $resp->getBody(), 1);
        }
        $this->currentTransactionUrl = null;
        return true;
    }

    protected function updateData($operation, Graph $graph)
    {
        $graphUri = $graph->getUri();

        $query = $graph->serialise('ntriples');
        if ($graphUri) {
            $query = "GRAPH <$graphUri> { $query }";
        }
        $query = "$operation DATA { $query }";


        // doc : http://rdf4j.org/doc/4/articles/REST-API/transaction-operations.docbook?view
        // cf. bug : https://openrdf.atlassian.net/browse/SES-2295
        // and PR https://bitbucket.org/openrdf/rdf4j/commits/62b680d8650caca7bc1673f6aaac48a5b6e85d23?at=2.8.x
        // The put form has been chosen over the post, because it seems that this is the form choosed in the rdf4j http client
        $resp = $this->getHttpClient()->put(
            $this->currentTransactionUrl, [
                'headers' => ["Content-Type" => "application/sparql-update; charset=utf-8"],
                'query' => ['action' => 'UPDATE'],
                'body' => $query,
            ]
        );

    }

    public function add(Graph $graph) {
        return $this->updateData('INSERT', $graph);
    }

    public function delete(Graph $graph) {
        return $this->updateData('DELETE', $graph);
    }

    public function deleteWhere($whereClauses, $graphUri = null) {

        if(empty($whereClauses)) {
            return;
        }
        if(is_array($whereClauses)) {
            $whereClauses = implode(" .", $whereClauses);
        }

        $query = "DELETE { ?s ?p ?o } WHERE { $whereClauses }";

        if($graphUri) {
            $query = "WITH <$graphUri> $query";
        }


        // doc : http://rdf4j.org/doc/4/articles/REST-API/transaction-operations.docbook?view
        // cf. bug : https://openrdf.atlassian.net/browse/SES-2295
        // and PR https://bitbucket.org/openrdf/rdf4j/commits/62b680d8650caca7bc1673f6aaac48a5b6e85d23?at=2.8.x
        // The put form has been chosen over the post, because it seems that this is the form choosed in the rdf4j http client
        $resp = $this->getHttpClient()->put(
            $this->currentTransactionUrl, [
                'headers' => ["Content-Type" => "application/sparql-update; charset=utf-8"],
                'query' => ['action' => 'UPDATE'],
                'body' => $query,
            ]
        );

        return $resp;

    }

    /** Make a query to the SPARQL endpoint
     *
     * Just call and return EasyRdf\Sparql\Client::query
     *
     * @param string $query The query string to be executed
     * @return object EasyRdf\Sparql\Result|EasyRdf\Graph Result of the query.
     */
    public function query($query, $timeout=0) {
        return $this->sparqlClient->query($query, $timeout);
    }

}