8 * Copyright (c) 2009-2013 Nicholas J Humfrey. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * 3. The name of the author 'Nicholas J Humfrey" may be used to endorse or
18 * promote products derived from this software without specific prior
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 * @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
35 * @license http://www.opensource.org/licenses/bsd-license.php
39 * A class for fetching, saving and deleting graphs to a Graph Store.
40 * Implementation of the SPARQL 1.1 Graph Store HTTP Protocol.
43 * @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
44 * @license http://www.opensource.org/licenses/bsd-license.php
46 class EasyRdf_GraphStore
49 * Use to reference default graph of triplestore
51 const DEFAULT_GRAPH = 'urn:easyrdf:default-graph';
53 /** The address of the GraphStore endpoint */
55 private $parsedUri = null;
58 /** Create a new SPARQL Graph Store client
60 * @param string $uri The address of the graph store endpoint
62 public function __construct($uri)
65 $this->parsedUri = new EasyRdf_ParsedUri($uri);
68 /** Get the URI of the graph store
70 * @return string The URI of the graph store
72 public function getUri()
77 /** Fetch a named graph from the graph store
79 * The URI can either be a full absolute URI or
80 * a URI relative to the URI of the graph store.
82 * @param string $uriRef The URI of graph desired
83 * @return EasyRdf_Graph The graph requested
85 public function get($uriRef)
87 if ($uriRef === self::DEFAULT_GRAPH) {
88 $dataUrl = $this->urlForGraph(self::DEFAULT_GRAPH);
89 $graph = new EasyRdf_Graph();
91 $graphUri = $this->parsedUri->resolve($uriRef)->toString();
92 $dataUrl = $this->urlForGraph($graphUri);
94 $graph = new EasyRdf_Graph($graphUri);
97 $graph->load($dataUrl);
103 * Fetch default graph from the graph store
104 * @return EasyRdf_Graph
106 public function getDefault()
108 return $this->get(self::DEFAULT_GRAPH);
111 /** Send some graph data to the graph store
113 * This method is used by insert() and replace()
117 protected function sendGraph($method, $graph, $uriRef, $format)
119 if (is_object($graph) and $graph instanceof EasyRdf_Graph) {
120 if ($uriRef === null) {
121 $uriRef = $graph->getUri();
123 $data = $graph->serialise($format);
128 if ($uriRef === null) {
129 throw new InvalidArgumentException('Graph IRI is not specified');
132 $formatObj = EasyRdf_Format::getFormat($format);
133 $mimeType = $formatObj->getDefaultMimeType();
135 if ($uriRef === self::DEFAULT_GRAPH) {
136 $dataUrl = $this->urlForGraph(self::DEFAULT_GRAPH);
138 $graphUri = $this->parsedUri->resolve($uriRef)->toString();
139 $dataUrl = $this->urlForGraph($graphUri);
142 $client = EasyRdf_Http::getDefaultHttpClient();
143 $client->resetParameters(true);
144 $client->setUri($dataUrl);
145 $client->setMethod($method);
146 $client->setRawData($data);
147 $client->setHeaders('Content-Type', $mimeType);
149 $response = $client->request();
151 if (!$response->isSuccessful()) {
152 throw new EasyRdf_Exception(
153 "HTTP request for {$dataUrl} failed: ".$response->getMessage()
160 /** Replace the contents of a graph in the graph store with new data
162 * The $graph parameter is the EasyRdf_Graph object to be sent to the
163 * graph store. Alternatively it can be a string, already serialised.
165 * The URI can either be a full absolute URI or
166 * a URI relative to the URI of the graph store.
168 * The $format parameter can be given to specify the serialisation
169 * used to send the graph data to the graph store.
171 * @param EasyRdf_Graph|string $graph Data
172 * @param string $uriRef The URI of graph to be replaced
173 * @param string $format The format of the data to send to the graph store
174 * @return EasyRdf_Http_Response The response from the graph store
176 public function replace($graph, $uriRef = null, $format = 'ntriples')
178 return $this->sendGraph('PUT', $graph, $uriRef, $format);
182 * Replace the contents of default graph in the graph store with new data
184 * The $graph parameter is the EasyRdf_Graph object to be sent to the
185 * graph store. Alternatively it can be a string, already serialised.
187 * The $format parameter can be given to specify the serialisation
188 * used to send the graph data to the graph store.
190 * @param EasyRdf_Graph|string $graph Data
191 * @param string $format The format of the data to send to the graph store
192 * @return EasyRdf_Http_Response The response from the graph store
194 public function replaceDefault($graph, $format = 'ntriples')
196 return self::replace($graph, self::DEFAULT_GRAPH, $format);
199 /** Add data to a graph in the graph store
201 * The $graph parameter is the EasyRdf_Graph object to be sent to the
202 * graph store. Alternatively it can be a string, already serialised.
204 * The URI can either be a full absolute URI or
205 * a URI relative to the URI of the graph store.
207 * The $format parameter can be given to specify the serialisation
208 * used to send the graph data to the graph store.
210 * @param EasyRdf_Graph|string $graph Data
211 * @param string $uriRef The URI of graph to be added to
212 * @param string $format The format of the data to send to the graph store
213 * @return object EasyRdf_Http_Response The response from the graph store
215 public function insert($graph, $uriRef = null, $format = 'ntriples')
217 return $this->sendGraph('POST', $graph, $uriRef, $format);
221 * Add data to default graph of the graph store
223 * The $graph parameter is the EasyRdf_Graph object to be sent to the
224 * graph store. Alternatively it can be a string, already serialised.
226 * The $format parameter can be given to specify the serialisation
227 * used to send the graph data to the graph store.
229 * @param EasyRdf_Graph|string $graph Data
230 * @param string $format The format of the data to send to the graph store
231 * @return object EasyRdf_Http_Response The response from the graph store
233 public function insertIntoDefault($graph, $format = 'ntriples')
235 return $this->insert($graph, self::DEFAULT_GRAPH, $format);
238 /** Delete named graph content from the graph store
240 * The URI can either be a full absolute URI or
241 * a URI relative to the URI of the graph store.
243 * @param string $uriRef The URI of graph to be added to
245 * @throws EasyRdf_Exception
246 * @return EasyRdf_Http_Response The response from the graph store
248 public function delete($uriRef)
250 if ($uriRef === self::DEFAULT_GRAPH) {
251 $dataUrl = $this->urlForGraph(self::DEFAULT_GRAPH);
253 $graphUri = $this->parsedUri->resolve($uriRef)->toString();
254 $dataUrl = $this->urlForGraph($graphUri);
257 $client = EasyRdf_Http::getDefaultHttpClient();
258 $client->resetParameters(true);
259 $client->setUri($dataUrl);
260 $client->setMethod('DELETE');
261 $response = $client->request();
263 if (!$response->isSuccessful()) {
264 throw new EasyRdf_Exception(
265 "HTTP request to delete {$dataUrl} failed: ".$response->getMessage()
273 * Delete default graph content from the graph store
275 * @return EasyRdf_Http_Response
276 * @throws EasyRdf_Exception
278 public function deleteDefault()
280 return $this->delete(self::DEFAULT_GRAPH);
283 /** Work out the full URL for a graph store request.
284 * by checking if if it is a direct or indirect request.
287 protected function urlForGraph($url)
289 if ($url === self::DEFAULT_GRAPH) {
290 $url = $this->uri.'?default';
291 } elseif (strpos($url, $this->uri) === false) {
292 $url = $this->uri."?graph=".urlencode($url);
298 /** Magic method to return URI of the graph store when casted to string
300 * @return string The URI of the graph store
302 public function __toString()
304 return empty($this->uri) ? '' : $this->uri;