Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
changesets.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Openstreetmap
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE
8  */
9 
10 defined('JPATH_PLATFORM') or die();
11 
12 /**
13  * Openstreetmap API Changesets class for the Joomla Platform
14  *
15  * @package Joomla.Platform
16  * @subpackage Openstreetmap
17  * @since 13.1
18  */
20 {
21  /**
22  * Method to create a changeset
23  *
24  * @param array $changesets Array which contains changeset data
25  *
26  * @return array The XML response
27  *
28  * @since 13.1
29  */
30  public function createChangeset($changesets=array())
31  {
32  $token = $this->oauth->getToken();
33 
34  // Set parameters.
35  $parameters = array(
36  'oauth_token' => $token['key'],
37  'oauth_token_secret' => $token['secret']
38  );
39 
40  // Set the API base
41  $base = 'changeset/create';
42 
43  // Build the request path.
44  $path = $this->getOption('api.url') . $base;
45 
46  $xml = '<?xml version="1.0" encoding="UTF-8"?>
47  <osm version="0.6" generator="JOpenstreetmap">';
48 
49  if (!empty($changesets))
50  {
51  // Create Changeset element for every changeset
52  foreach ($changesets as $tags)
53  {
54  $xml .= '<changeset>';
55 
56  if (!empty($tags))
57  {
58  // Create a list of tags for each changeset
59  foreach ($tags as $key => $value)
60  {
61  $xml .= '<tag k="' . $key . '" v="' . $value . '"/>';
62  }
63  }
64 
65  $xml .= '</changeset>';
66  }
67  }
68 
69  $xml .= '</osm>';
70 
71  $header['Content-Type'] = 'text/xml';
72 
73  // Send the request.
74  $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
75 
76  return $response->body;
77  }
78 
79  /**
80  * Method to read a changeset
81  *
82  * @param integer $id identifier of the changeset
83  *
84  * @return array The XML response about a changeset
85  *
86  * @since 13.1
87  */
88  public function readChangeset($id)
89  {
90  // Set the API base
91  $base = 'changeset/' . $id;
92 
93  // Build the request path.
94  $path = $this->getOption('api.url') . $base;
95 
96  // Send the request.
97  $xml_string = $this->sendRequest($path);
98 
99  return $xml_string->changeset;
100  }
101 
102  /**
103  * Method to update a changeset
104  *
105  * @param integer $id Identifier of the changeset
106  * @param array $tags Array of tags to update
107  *
108  * @return array The XML response of updated changeset
109  *
110  * @since 13.1
111  */
112  public function updateChangeset($id, $tags = array())
113  {
114  $token = $this->oauth->getToken();
115 
116  // Set parameters.
117  $parameters = array(
118  'oauth_token' => $token['key']
119  );
120 
121  // Set the API base
122  $base = 'changeset/' . $id;
123 
124  // Build the request path.
125  $path = $this->getOption('api.url') . $base;
126 
127  // Create a list of tags to update changeset
128  $tag_list = '';
129 
130  if (!empty($tags))
131  {
132  foreach ($tags as $key => $value)
133  {
134  $tag_list .= '<tag k="' . $key . '" v="' . $value . '"/>';
135  }
136  }
137 
138  $xml = '<?xml version="1.0" encoding="UTF-8"?>
139  <osm version="0.6" generator="JOpenstreetmap">
140  <changeset>'
141  . $tag_list .
142  '</changeset>
143  </osm>';
144 
145  $header['Content-Type'] = 'text/xml';
146 
147  // Send the request.
148  $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
149 
150  $xml_string = simplexml_load_string($response->body);
151 
152  return $xml_string->changeset;
153  }
154 
155  /**
156  * Method to close a changeset
157  *
158  * @param integer $id identifier of the changeset
159  *
160  * @return void
161  *
162  * @since 13.1
163  */
164  public function closeChangeset($id)
165  {
166  $token = $this->oauth->getToken();
167 
168  // Set parameters.
169  $parameters = array(
170  'oauth_token' => $token['key']
171  );
172 
173  // Set the API base
174  $base = 'changeset/' . $id . '/close';
175 
176  // Build the request path.
177  $path = $this->getOption('api.url') . $base;
178 
179  $header['format'] = 'text/xml';
180 
181  // Send the request.
182  $this->oauth->oauthRequest($path, 'PUT', $parameters, $header);
183  }
184 
185  /**
186  * Method to download a changeset
187  *
188  * @param integer $id Identifier of the changeset
189  *
190  * @return array The XML response of requested changeset
191  *
192  * @since 13.1
193  */
194  public function downloadChangeset($id)
195  {
196  // Set the API base
197  $base = 'changeset/' . $id . '/download';
198 
199  // Build the request path.
200  $path = $this->getOption('api.url') . $base;
201 
202  // Send the request.
203  $xml_string = $this->sendRequest($path);
204 
205  return $xml_string->create;
206  }
207 
208  /**
209  * Method to expand the bounding box of a changeset
210  *
211  * @param integer $id Identifier of the changeset
212  * @param array $nodes List of lat lon about nodes
213  *
214  * @return array The XML response of changed changeset
215  *
216  * @since 13.1
217  */
218  public function expandBBoxChangeset($id, $nodes)
219  {
220  $token = $this->oauth->getToken();
221 
222  // Set parameters.
223  $parameters = array(
224  'oauth_token' => $token['key']
225  );
226 
227  // Set the API base
228  $base = 'changeset/' . $id . '/expand_bbox';
229 
230  // Build the request path.
231  $path = $this->getOption('api.url') . $base;
232 
233  // Create a list of tags to update changeset
234  $node_list = '';
235 
236  if (!empty($nodes))
237  {
238  foreach ($nodes as $node)
239  {
240  $node_list .= '<node lat="' . $node[0] . '" lon="' . $node[1] . '"/>';
241  }
242  }
243 
244  $xml = '<?xml version="1.0" encoding="UTF-8"?>
245  <osm version="0.6" generator="JOpenstreetmap">
246  <changeset>'
247  . $node_list .
248  '</changeset>
249  </osm>';
250 
251  $header['Content-Type'] = 'text/xml';
252 
253  // Send the request.
254  $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
255 
256  $xml_string = simplexml_load_string($response->body);
257 
258  return $xml_string->changeset;
259  }
260 
261  /**
262  * Method to query on changesets
263  *
264  * @param string $param Parameters for query
265  *
266  * @return array The XML response
267  *
268  * @since 13.1
269  */
270  public function queryChangeset($param)
271  {
272  // Set the API base
273  $base = 'changesets/' . $param;
274 
275  // Build the request path.
276  $path = $this->getOption('api.url') . $base;
277 
278  // Send the request.
279  $xml_string = $this->sendRequest($path);
280 
281  return $xml_string->osm;
282  }
283 
284  /**
285  * Method to upload a diff to a changeset
286  *
287  * @param string $xml Diff data to upload
288  * @param integer $id Identifier of the changeset
289  *
290  * @return array The XML response of result
291  *
292  * @since 13.1
293  */
294  public function diffUploadChangeset($xml, $id)
295  {
296  $token = $this->oauth->getToken();
297 
298  // Set parameters.
299  $parameters = array(
300  'oauth_token' => $token['key']
301  );
302 
303  // Set the API base
304  $base = 'changeset/' . $id . '/upload';
305 
306  // Build the request path.
307  $path = $this->getOption('api.url') . $base;
308 
309  $header['Content-Type'] = 'text/xml';
310 
311  // Send the request.
312  $response = $this->oauth->oauthRequest($path, 'POST', $parameters, $xml, $header);
313 
314  $xml_string = simplexml_load_string($response->body);
315 
316  return $xml_string->diffResult;
317  }
318 }