Minor dependency updates
[yaffs-website] / vendor / easyrdf / easyrdf / lib / EasyRdf / GraphStore.php
1 <?php
2
3 /**
4  * EasyRdf
5  *
6  * LICENSE
7  *
8  * Copyright (c) 2009-2013 Nicholas J Humfrey.  All rights reserved.
9  *
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
19  *    written permission.
20  *
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.
32  *
33  * @package    EasyRdf
34  * @copyright  Copyright (c) 2009-2013 Nicholas J Humfrey
35  * @license    http://www.opensource.org/licenses/bsd-license.php
36  */
37
38 /**
39  * A class for fetching, saving and deleting graphs to a Graph Store.
40  * Implementation of the SPARQL 1.1 Graph Store HTTP Protocol.
41  *
42  * @package    EasyRdf
43  * @copyright  Copyright (c) 2009-2013 Nicholas J Humfrey
44  * @license    http://www.opensource.org/licenses/bsd-license.php
45  */
46 class EasyRdf_GraphStore
47 {
48     /**
49      * Use to reference default graph of triplestore
50      */
51     const DEFAULT_GRAPH = 'urn:easyrdf:default-graph';
52
53     /** The address of the GraphStore endpoint */
54     private $uri = null;
55     private $parsedUri = null;
56
57
58     /** Create a new SPARQL Graph Store client
59      *
60      * @param string $uri The address of the graph store endpoint
61      */
62     public function __construct($uri)
63     {
64         $this->uri = $uri;
65         $this->parsedUri = new EasyRdf_ParsedUri($uri);
66     }
67
68     /** Get the URI of the graph store
69      *
70      * @return string The URI of the graph store
71      */
72     public function getUri()
73     {
74         return $this->uri;
75     }
76
77     /** Fetch a named graph from the graph store
78      *
79      * The URI can either be a full absolute URI or
80      * a URI relative to the URI of the graph store.
81      *
82      * @param string $uriRef The URI of graph desired
83      * @return EasyRdf_Graph The graph requested
84      */
85     public function get($uriRef)
86     {
87         if ($uriRef === self::DEFAULT_GRAPH) {
88             $dataUrl = $this->urlForGraph(self::DEFAULT_GRAPH);
89             $graph = new EasyRdf_Graph();
90         } else {
91             $graphUri = $this->parsedUri->resolve($uriRef)->toString();
92             $dataUrl = $this->urlForGraph($graphUri);
93
94             $graph = new EasyRdf_Graph($graphUri);
95         }
96
97         $graph->load($dataUrl);
98
99         return $graph;
100     }
101
102     /**
103      * Fetch default graph from the graph store
104      * @return EasyRdf_Graph
105      */
106     public function getDefault()
107     {
108         return $this->get(self::DEFAULT_GRAPH);
109     }
110
111     /** Send some graph data to the graph store
112      *
113      * This method is used by insert() and replace()
114      *
115      * @ignore
116      */
117     protected function sendGraph($method, $graph, $uriRef, $format)
118     {
119         if (is_object($graph) and $graph instanceof EasyRdf_Graph) {
120             if ($uriRef === null) {
121                 $uriRef = $graph->getUri();
122             }
123             $data = $graph->serialise($format);
124         } else {
125             $data = $graph;
126         }
127
128         if ($uriRef === null) {
129             throw new InvalidArgumentException('Graph IRI is not specified');
130         }
131
132         $formatObj = EasyRdf_Format::getFormat($format);
133         $mimeType = $formatObj->getDefaultMimeType();
134
135         if ($uriRef === self::DEFAULT_GRAPH) {
136             $dataUrl = $this->urlForGraph(self::DEFAULT_GRAPH);
137         } else {
138             $graphUri = $this->parsedUri->resolve($uriRef)->toString();
139             $dataUrl = $this->urlForGraph($graphUri);
140         }
141
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);
148
149         $response = $client->request();
150
151         if (!$response->isSuccessful()) {
152             throw new EasyRdf_Exception(
153                 "HTTP request for {$dataUrl} failed: ".$response->getMessage()
154             );
155         }
156
157         return $response;
158     }
159
160     /** Replace the contents of a graph in the graph store with new data
161      *
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.
164      *
165      * The URI can either be a full absolute URI or
166      * a URI relative to the URI of the graph store.
167      *
168      * The $format parameter can be given to specify the serialisation
169      * used to send the graph data to the graph store.
170      *
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
175      */
176     public function replace($graph, $uriRef = null, $format = 'ntriples')
177     {
178         return $this->sendGraph('PUT', $graph, $uriRef, $format);
179     }
180
181     /**
182      * Replace the contents of default graph in the graph store with new data
183      *
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.
186      *
187      * The $format parameter can be given to specify the serialisation
188      * used to send the graph data to the graph store.
189      *
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
193      */
194     public function replaceDefault($graph, $format = 'ntriples')
195     {
196         return self::replace($graph, self::DEFAULT_GRAPH, $format);
197     }
198
199     /** Add data to a graph in the graph store
200      *
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.
203      *
204      * The URI can either be a full absolute URI or
205      * a URI relative to the URI of the graph store.
206      *
207      * The $format parameter can be given to specify the serialisation
208      * used to send the graph data to the graph store.
209      *
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
214      */
215     public function insert($graph, $uriRef = null, $format = 'ntriples')
216     {
217         return $this->sendGraph('POST', $graph, $uriRef, $format);
218     }
219
220     /**
221      * Add data to default graph of the graph store
222      *
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.
225      *
226      * The $format parameter can be given to specify the serialisation
227      * used to send the graph data to the graph store.
228      *
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
232      */
233     public function insertIntoDefault($graph, $format = 'ntriples')
234     {
235         return $this->insert($graph, self::DEFAULT_GRAPH, $format);
236     }
237
238     /** Delete named graph content from the graph store
239      *
240      * The URI can either be a full absolute URI or
241      * a URI relative to the URI of the graph store.
242      *
243      * @param string $uriRef The URI of graph to be added to
244      *
245      * @throws EasyRdf_Exception
246      * @return EasyRdf_Http_Response The response from the graph store
247      */
248     public function delete($uriRef)
249     {
250         if ($uriRef === self::DEFAULT_GRAPH) {
251             $dataUrl = $this->urlForGraph(self::DEFAULT_GRAPH);
252         } else {
253             $graphUri = $this->parsedUri->resolve($uriRef)->toString();
254             $dataUrl = $this->urlForGraph($graphUri);
255         }
256
257         $client = EasyRdf_Http::getDefaultHttpClient();
258         $client->resetParameters(true);
259         $client->setUri($dataUrl);
260         $client->setMethod('DELETE');
261         $response = $client->request();
262
263         if (!$response->isSuccessful()) {
264             throw new EasyRdf_Exception(
265                 "HTTP request to delete {$dataUrl} failed: ".$response->getMessage()
266             );
267         }
268
269         return $response;
270     }
271
272     /**
273      * Delete default graph content from the graph store
274      *
275      * @return EasyRdf_Http_Response
276      * @throws EasyRdf_Exception
277      */
278     public function deleteDefault()
279     {
280         return $this->delete(self::DEFAULT_GRAPH);
281     }
282
283     /** Work out the full URL for a graph store request.
284      *  by checking if if it is a direct or indirect request.
285      *  @ignore
286      */
287     protected function urlForGraph($url)
288     {
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);
293         }
294
295         return $url;
296     }
297
298     /** Magic method to return URI of the graph store when casted to string
299      *
300      * @return string The URI of the graph store
301      */
302     public function __toString()
303     {
304         return empty($this->uri) ? '' : $this->uri;
305     }
306 }