|
1 <?php |
|
2 namespace CorpusParole\Libraries\Sparql; |
|
3 |
|
4 use EasyRdf\Graph; |
|
5 use CorpusParole\Libraries\CorpusParoleException; |
|
6 use Config; |
|
7 use Log; |
|
8 |
|
9 /** |
|
10 * |
|
11 */ |
|
12 class SparqlClient { |
|
13 |
|
14 private $currentTransactionUrl = null; |
|
15 |
|
16 function __construct($httpClient, $sparqlClient) { |
|
17 $this->sparqlClient = $sparqlClient; |
|
18 $this->httpClient = $httpClient; |
|
19 } |
|
20 |
|
21 private function getHttpClient() { |
|
22 return $this->httpClient; |
|
23 } |
|
24 |
|
25 public function getCurrentTransactionUrl() { |
|
26 return $this->currentTransactionUrl; |
|
27 } |
|
28 |
|
29 /** |
|
30 * Start transaction. |
|
31 * @return boolean true if a transaction was started, false otherwise |
|
32 * @throws CorpusParoleException if the transaction could not be stared |
|
33 */ |
|
34 public function startTransaction() { |
|
35 if(!is_null($this->currentTransactionUrl)) { |
|
36 // We just continue the current transaction |
|
37 return false; |
|
38 } |
|
39 //Log::debug('http_client base uri: ' . $this->getHttpClient()->getConfig('base_uri')); |
|
40 $sesameRepository = config('corpusparole.sesame_repository'); |
|
41 $resp = $this->getHttpClient()->post("$sesameRepository/transactions"); |
|
42 //$resp = $this->getHttpClient()->post('transactions'); |
|
43 //TODO check errors |
|
44 if($resp->getStatusCode() != 201) { |
|
45 throw new CorpusParoleException("Error when starting transaction : " |
|
46 . $resp->getStatusCode() . " - " |
|
47 . $resp->getReasonPhrase() . " : " . $resp->getBody(), 1); |
|
48 } |
|
49 |
|
50 $this->currentTransactionUrl = $resp->getHeader('Location')[0]; |
|
51 |
|
52 return true; |
|
53 } |
|
54 |
|
55 /** |
|
56 * Rollback transaction. |
|
57 * @return boolean true if a transaction was started, false otherwise |
|
58 * @throws CorpusParoleException if the transaction could not be rollbacked |
|
59 */ |
|
60 public function rollback() { |
|
61 if(is_null($this->currentTransactionUrl)) { |
|
62 // We just continue the current transaction |
|
63 return false; |
|
64 } |
|
65 $resp = $this->getHttpClient()->delete($this->currentTransactionUrl); |
|
66 |
|
67 if($resp->getStatusCode() != 204) { |
|
68 throw new CorpusParoleException("Could not cancel transaction : " |
|
69 . $resp->getStatusCode() . " - " |
|
70 . $resp->getReasonPhrase() . " : " . $resp->getBody(), 1); |
|
71 } |
|
72 $this->currentTransactionUrl = null; |
|
73 |
|
74 return true; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Commit transaction. |
|
79 * @return boolean true if a transaction was started, false otherwise |
|
80 * @throws CorpusParoleException if the transaction could not be commited |
|
81 */ |
|
82 public function commit() { |
|
83 if(is_null($this->currentTransactionUrl)) { |
|
84 // We just continue the current transaction |
|
85 return false; |
|
86 } |
|
87 $resp = $this->getHttpClient()->put($this->currentTransactionUrl, ['query' => ['action' => 'COMMIT']]); |
|
88 if($resp->getStatusCode() != 200) { |
|
89 throw new CorpusParoleException("Could not commit transaction : " |
|
90 . $resp->getStatusCode() . " - " |
|
91 . $resp->getReasonPhrase() . " : " . $resp->getBody(), 1); |
|
92 } |
|
93 $this->currentTransactionUrl = null; |
|
94 return true; |
|
95 } |
|
96 |
|
97 protected function updateData($operation, Graph $graph) |
|
98 { |
|
99 $graphUri = $graph->getUri(); |
|
100 $query = "$operation DATA {"; |
|
101 if ($graphUri) { |
|
102 $query .= "GRAPH <$graphUri> {"; |
|
103 } |
|
104 $query .= $graph->serialise('ntriples'); |
|
105 if ($graphUri) { |
|
106 $query .= "}"; |
|
107 } |
|
108 $query .= '}'; |
|
109 |
|
110 $resp = $this->getHttpClient()->put( |
|
111 $this->currentTransactionUrl, [ |
|
112 'query' => ['action' => 'UPDATE'], |
|
113 'form_params'=> ['update' => $query], |
|
114 ] |
|
115 ); |
|
116 if($resp->getStatusCode() != 204) { |
|
117 throw new CorpusParoleException("Could not update in transaction with operation $operation: " |
|
118 . $resp->getStatusCode() . " - " |
|
119 . $resp->getReasonPhrase() . " : " . $resp->getBody(), 1); |
|
120 } |
|
121 } |
|
122 |
|
123 public function add(Graph $graph) { |
|
124 $this->updateData('INSERT', $graph); |
|
125 } |
|
126 |
|
127 public function delete(Graph $graph) { |
|
128 $this->updateData('DELETE', $graph); |
|
129 } |
|
130 |
|
131 /** Make a query to the SPARQL endpoint |
|
132 * |
|
133 * Just call and return EasyRdf\Sparql\Client::query |
|
134 * |
|
135 * @param string $query The query string to be executed |
|
136 * @return object EasyRdf\Sparql\Result|EasyRdf\Graph Result of the query. |
|
137 */ |
|
138 public function query($query) { |
|
139 return $this->sparqlClient->query($query); |
|
140 } |
|
141 |
|
142 } |