author | ymh <ymh.work@gmail.com> |
Tue, 20 Mar 2018 15:02:40 +0100 | |
changeset 573 | 25f3d28f51b2 |
parent 405 | f239c8c5bb94 |
permissions | -rw-r--r-- |
4 | 1 |
<?php |
2 |
namespace CorpusParole\Libraries\Sparql; |
|
3 |
||
4 |
use EasyRdf\Graph; |
|
5 |
use CorpusParole\Libraries\CorpusParoleException; |
|
6 |
use Config; |
|
7 |
use Log; |
|
109
d22ed5792f8e
Correct transaction management. cf. bug https://openrdf.atlassian.net/browse/SES-2295 and tomcat default management of PUT request with form data (application/x-www-form-urlencoded encoded)
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
8 |
use GuzzleHttp\Psr7\Request; |
4 | 9 |
|
10 |
/** |
|
11 |
* |
|
12 |
*/ |
|
13 |
class SparqlClient { |
|
14 |
||
15 |
private $currentTransactionUrl = null; |
|
16 |
||
17 |
function __construct($httpClient, $sparqlClient) { |
|
18 |
$this->sparqlClient = $sparqlClient; |
|
19 |
$this->httpClient = $httpClient; |
|
20 |
} |
|
21 |
||
22 |
private function getHttpClient() { |
|
23 |
return $this->httpClient; |
|
24 |
} |
|
25 |
||
26 |
public function getCurrentTransactionUrl() { |
|
27 |
return $this->currentTransactionUrl; |
|
28 |
} |
|
29 |
||
30 |
/** |
|
31 |
* Start transaction. |
|
32 |
* @return boolean true if a transaction was started, false otherwise |
|
33 |
* @throws CorpusParoleException if the transaction could not be stared |
|
34 |
*/ |
|
35 |
public function startTransaction() { |
|
36 |
if(!is_null($this->currentTransactionUrl)) { |
|
37 |
// We just continue the current transaction |
|
38 |
return false; |
|
39 |
} |
|
40 |
//Log::debug('http_client base uri: ' . $this->getHttpClient()->getConfig('base_uri')); |
|
405
f239c8c5bb94
migrate to rdf4j (2.1.1) from sesame (4.1.1)
ymh <ymh.work@gmail.com>
parents:
387
diff
changeset
|
41 |
$rdf4jRepository = config('corpusparole.rdf4j_repository'); |
f239c8c5bb94
migrate to rdf4j (2.1.1) from sesame (4.1.1)
ymh <ymh.work@gmail.com>
parents:
387
diff
changeset
|
42 |
$resp = $this->getHttpClient()->post("$rdf4jRepository/transactions", ['query' => ['isolation-level' => 'http://www.openrdf.org/schema/sesame#SNAPSHOT_READ']]); |
4 | 43 |
//$resp = $this->getHttpClient()->post('transactions'); |
44 |
//TODO check errors |
|
45 |
if($resp->getStatusCode() != 201) { |
|
46 |
throw new CorpusParoleException("Error when starting transaction : " |
|
47 |
. $resp->getStatusCode() . " - " |
|
48 |
. $resp->getReasonPhrase() . " : " . $resp->getBody(), 1); |
|
49 |
} |
|
50 |
||
51 |
$this->currentTransactionUrl = $resp->getHeader('Location')[0]; |
|
52 |
||
53 |
return true; |
|
54 |
} |
|
55 |
||
56 |
/** |
|
57 |
* Rollback transaction. |
|
58 |
* @return boolean true if a transaction was started, false otherwise |
|
59 |
* @throws CorpusParoleException if the transaction could not be rollbacked |
|
60 |
*/ |
|
61 |
public function rollback() { |
|
62 |
if(is_null($this->currentTransactionUrl)) { |
|
63 |
// We just continue the current transaction |
|
64 |
return false; |
|
65 |
} |
|
66 |
$resp = $this->getHttpClient()->delete($this->currentTransactionUrl); |
|
67 |
||
68 |
if($resp->getStatusCode() != 204) { |
|
69 |
throw new CorpusParoleException("Could not cancel transaction : " |
|
70 |
. $resp->getStatusCode() . " - " |
|
71 |
. $resp->getReasonPhrase() . " : " . $resp->getBody(), 1); |
|
72 |
} |
|
73 |
$this->currentTransactionUrl = null; |
|
74 |
||
75 |
return true; |
|
76 |
} |
|
77 |
||
78 |
/** |
|
79 |
* Commit transaction. |
|
80 |
* @return boolean true if a transaction was started, false otherwise |
|
81 |
* @throws CorpusParoleException if the transaction could not be commited |
|
82 |
*/ |
|
83 |
public function commit() { |
|
84 |
if(is_null($this->currentTransactionUrl)) { |
|
85 |
// We just continue the current transaction |
|
86 |
return false; |
|
87 |
} |
|
88 |
$resp = $this->getHttpClient()->put($this->currentTransactionUrl, ['query' => ['action' => 'COMMIT']]); |
|
89 |
if($resp->getStatusCode() != 200) { |
|
90 |
throw new CorpusParoleException("Could not commit transaction : " |
|
91 |
. $resp->getStatusCode() . " - " |
|
92 |
. $resp->getReasonPhrase() . " : " . $resp->getBody(), 1); |
|
93 |
} |
|
94 |
$this->currentTransactionUrl = null; |
|
95 |
return true; |
|
96 |
} |
|
97 |
||
98 |
protected function updateData($operation, Graph $graph) |
|
99 |
{ |
|
100 |
$graphUri = $graph->getUri(); |
|
109
d22ed5792f8e
Correct transaction management. cf. bug https://openrdf.atlassian.net/browse/SES-2295 and tomcat default management of PUT request with form data (application/x-www-form-urlencoded encoded)
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
101 |
|
d22ed5792f8e
Correct transaction management. cf. bug https://openrdf.atlassian.net/browse/SES-2295 and tomcat default management of PUT request with form data (application/x-www-form-urlencoded encoded)
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
102 |
$query = $graph->serialise('ntriples'); |
4 | 103 |
if ($graphUri) { |
109
d22ed5792f8e
Correct transaction management. cf. bug https://openrdf.atlassian.net/browse/SES-2295 and tomcat default management of PUT request with form data (application/x-www-form-urlencoded encoded)
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
104 |
$query = "GRAPH <$graphUri> { $query }"; |
4 | 105 |
} |
109
d22ed5792f8e
Correct transaction management. cf. bug https://openrdf.atlassian.net/browse/SES-2295 and tomcat default management of PUT request with form data (application/x-www-form-urlencoded encoded)
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
106 |
$query = "$operation DATA { $query }"; |
d22ed5792f8e
Correct transaction management. cf. bug https://openrdf.atlassian.net/browse/SES-2295 and tomcat default management of PUT request with form data (application/x-www-form-urlencoded encoded)
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
107 |
|
4 | 108 |
|
109
d22ed5792f8e
Correct transaction management. cf. bug https://openrdf.atlassian.net/browse/SES-2295 and tomcat default management of PUT request with form data (application/x-www-form-urlencoded encoded)
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
109 |
// doc : http://rdf4j.org/doc/4/articles/REST-API/transaction-operations.docbook?view |
d22ed5792f8e
Correct transaction management. cf. bug https://openrdf.atlassian.net/browse/SES-2295 and tomcat default management of PUT request with form data (application/x-www-form-urlencoded encoded)
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
110 |
// cf. bug : https://openrdf.atlassian.net/browse/SES-2295 |
405
f239c8c5bb94
migrate to rdf4j (2.1.1) from sesame (4.1.1)
ymh <ymh.work@gmail.com>
parents:
387
diff
changeset
|
111 |
// and PR https://bitbucket.org/openrdf/rdf4j/commits/62b680d8650caca7bc1673f6aaac48a5b6e85d23?at=2.8.x |
f239c8c5bb94
migrate to rdf4j (2.1.1) from sesame (4.1.1)
ymh <ymh.work@gmail.com>
parents:
387
diff
changeset
|
112 |
// The put form has been chosen over the post, because it seems that this is the form choosed in the rdf4j http client |
4 | 113 |
$resp = $this->getHttpClient()->put( |
114 |
$this->currentTransactionUrl, [ |
|
109
d22ed5792f8e
Correct transaction management. cf. bug https://openrdf.atlassian.net/browse/SES-2295 and tomcat default management of PUT request with form data (application/x-www-form-urlencoded encoded)
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
115 |
'headers' => ["Content-Type" => "application/sparql-update; charset=utf-8"], |
4 | 116 |
'query' => ['action' => 'UPDATE'], |
109
d22ed5792f8e
Correct transaction management. cf. bug https://openrdf.atlassian.net/browse/SES-2295 and tomcat default management of PUT request with form data (application/x-www-form-urlencoded encoded)
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
117 |
'body' => $query, |
4 | 118 |
] |
119 |
); |
|
109
d22ed5792f8e
Correct transaction management. cf. bug https://openrdf.atlassian.net/browse/SES-2295 and tomcat default management of PUT request with form data (application/x-www-form-urlencoded encoded)
ymh <ymh.work@gmail.com>
parents:
28
diff
changeset
|
120 |
|
4 | 121 |
} |
122 |
||
123 |
public function add(Graph $graph) { |
|
158
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
124 |
return $this->updateData('INSERT', $graph); |
4 | 125 |
} |
126 |
||
127 |
public function delete(Graph $graph) { |
|
158
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
128 |
return $this->updateData('DELETE', $graph); |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
129 |
} |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
130 |
|
307 | 131 |
public function deleteWhere($whereClauses, $graphUri = null) { |
158
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
132 |
|
277
bd4bc1db4f40
add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents:
158
diff
changeset
|
133 |
if(empty($whereClauses)) { |
bd4bc1db4f40
add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents:
158
diff
changeset
|
134 |
return; |
158
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
135 |
} |
277
bd4bc1db4f40
add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents:
158
diff
changeset
|
136 |
if(is_array($whereClauses)) { |
bd4bc1db4f40
add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents:
158
diff
changeset
|
137 |
$whereClauses = implode(" .", $whereClauses); |
bd4bc1db4f40
add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents:
158
diff
changeset
|
138 |
} |
bd4bc1db4f40
add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents:
158
diff
changeset
|
139 |
|
bd4bc1db4f40
add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents:
158
diff
changeset
|
140 |
$query = "DELETE { ?s ?p ?o } WHERE { $whereClauses }"; |
158
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
141 |
|
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
142 |
if($graphUri) { |
277
bd4bc1db4f40
add blank node save and geoinfo to back model
ymh <ymh.work@gmail.com>
parents:
158
diff
changeset
|
143 |
$query = "WITH <$graphUri> $query"; |
158
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
144 |
} |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
145 |
|
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
146 |
|
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
147 |
// doc : http://rdf4j.org/doc/4/articles/REST-API/transaction-operations.docbook?view |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
148 |
// cf. bug : https://openrdf.atlassian.net/browse/SES-2295 |
405
f239c8c5bb94
migrate to rdf4j (2.1.1) from sesame (4.1.1)
ymh <ymh.work@gmail.com>
parents:
387
diff
changeset
|
149 |
// and PR https://bitbucket.org/openrdf/rdf4j/commits/62b680d8650caca7bc1673f6aaac48a5b6e85d23?at=2.8.x |
f239c8c5bb94
migrate to rdf4j (2.1.1) from sesame (4.1.1)
ymh <ymh.work@gmail.com>
parents:
387
diff
changeset
|
150 |
// The put form has been chosen over the post, because it seems that this is the form choosed in the rdf4j http client |
158
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
151 |
$resp = $this->getHttpClient()->put( |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
152 |
$this->currentTransactionUrl, [ |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
153 |
'headers' => ["Content-Type" => "application/sparql-update; charset=utf-8"], |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
154 |
'query' => ['action' => 'UPDATE'], |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
155 |
'body' => $query, |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
156 |
] |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
157 |
); |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
158 |
|
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
159 |
return $resp; |
366509ae2f37
Add controller for themes count + upgrade ember for app-client
ymh <ymh.work@gmail.com>
parents:
109
diff
changeset
|
160 |
|
4 | 161 |
} |
162 |
||
163 |
/** Make a query to the SPARQL endpoint |
|
164 |
* |
|
165 |
* Just call and return EasyRdf\Sparql\Client::query |
|
166 |
* |
|
167 |
* @param string $query The query string to be executed |
|
168 |
* @return object EasyRdf\Sparql\Result|EasyRdf\Graph Result of the query. |
|
169 |
*/ |
|
387 | 170 |
public function query($query, $timeout=0) { |
171 |
return $this->sparqlClient->query($query, $timeout); |
|
4 | 172 |
} |
173 |
||
174 |
} |