Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
elements.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 Elements 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 node
23  *
24  * @param integer $changeset Changeset id
25  * @param float $latitude Latitude of the node
26  * @param float $longitude Longitude of the node
27  * @param arary $tags Array of tags for a node
28  *
29  * @return array The XML response
30  *
31  * @since 13.1
32  */
33  public function createNode($changeset, $latitude, $longitude, $tags)
34  {
35  $token = $this->oauth->getToken();
36 
37  // Set parameters.
38  $parameters = array(
39  'oauth_token' => $token['key']
40  );
41 
42  // Set the API base
43  $base = 'node/create';
44 
45  // Build the request path.
46  $path = $this->getOption('api.url') . $base;
47 
48  $tag_list = '';
49 
50  // Create XML node
51  if (!empty($tags))
52  {
53  foreach ($tags as $key => $value)
54  {
55  $tag_list .= '<tag k="' . $key . '" v="' . $value . '"/>';
56  }
57  }
58 
59  $xml = '<?xml version="1.0" encoding="UTF-8"?>
60  <osm version="0.6" generator="JOpenstreetmap">
61  <node changeset="' . $changeset . '" lat="' . $latitude . '" lon="' . $longitude . '">'
62  . $tag_list .
63  '</node>
64  </osm>';
65 
66  $header['Content-Type'] = 'text/xml';
67 
68  // Send the request.
69  $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
70 
71  return $response->body;
72  }
73 
74  /**
75  * Method to create a way
76  *
77  * @param integer $changeset Changeset id
78  * @param array $tags Array of tags for a way
79  * @param array $nds Node ids to refer
80  *
81  * @return array The XML response
82  *
83  * @since 13.1
84  */
85  public function createWay($changeset, $tags, $nds)
86  {
87  $token = $this->oauth->getToken();
88 
89  // Set parameters.
90  $parameters = array(
91  'oauth_token' => $token['key']
92  );
93 
94  // Set the API base
95  $base = 'way/create';
96 
97  // Build the request path.
98  $path = $this->getOption('api.url') . $base;
99 
100  $tag_list = '';
101 
102  // Create XML node
103  if (!empty($tags))
104  {
105  foreach ($tags as $key => $value)
106  {
107  $tag_list .= '<tag k="' . $key . '" v="' . $value . '"/>';
108  }
109  }
110 
111  $nd_list = '';
112 
113  if (!empty($nds))
114  {
115  foreach ($nds as $value)
116  {
117  $nd_list .= '<nd ref="' . $value . '"/>';
118  }
119  }
120 
121  $xml = '<?xml version="1.0" encoding="UTF-8"?>
122  <osm version="0.6" generator="JOpenstreetmap">
123  <way changeset="' . $changeset . '">'
124  . $tag_list
125  . $nd_list .
126  '</way>
127  </osm>';
128 
129  $header['Content-Type'] = 'text/xml';
130 
131  // Send the request.
132  $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
133 
134  return $response->body;
135  }
136 
137  /**
138  * Method to create a relation
139  *
140  * @param integer $changeset Changeset id
141  * @param array $tags Array of tags for a relation
142  * @param array $members Array of members for a relation
143  * eg: $members = array(array("type"=>"node", "role"=>"stop", "ref"=>"123"), array("type"=>"way", "ref"=>"123"))
144  *
145  * @return array The XML response
146  *
147  * @since 13.1
148  */
149  public function createRelation($changeset, $tags, $members)
150  {
151  $token = $this->oauth->getToken();
152 
153  // Set parameters.
154  $parameters = array(
155  'oauth_token' => $token['key']
156  );
157 
158  // Set the API base
159  $base = 'relation/create';
160 
161  // Build the request path.
162  $path = $this->getOption('api.url') . $base;
163 
164  $tag_list = '';
165 
166  // Create XML node
167  if (!empty($tags))
168  {
169  foreach ($tags as $key => $value)
170  {
171  $tag_list .= '<tag k="' . $key . '" v="' . $value . '"/>';
172  }
173  }
174 
175  // Members
176  $member_list = '';
177 
178  if (!empty($members))
179  {
180  foreach ($members as $member)
181  {
182  if ($member['type'] == "node")
183  {
184  $member_list .= '<member type="' . $member['type'] . '" role="' . $member['role'] . '" ref="' . $member['ref'] . '"/>';
185  }
186  elseif ($member['type'] == "way")
187  {
188  $member_list .= '<member type="' . $member['type'] . '" ref="' . $member['ref'] . '"/>';
189  }
190  }
191  }
192 
193  $xml = '<?xml version="1.0" encoding="UTF-8"?>
194  <osm version="0.6" generator="JOpenstreetmap">
195  <relation relation="' . $changeset . '" >'
196  . $tag_list
197  . $member_list .
198  '</relation>
199  </osm>';
200 
201  $header['Content-Type'] = 'text/xml';
202 
203  // Send the request.
204  $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
205 
206  return $response->body;
207  }
208 
209  /**
210  * Method to read an element [node|way|relation]
211  *
212  * @param string $element [node|way|relation]
213  * @param integer $id Element identifier
214  *
215  * @return array The XML response
216  *
217  * @since 13.1
218  * @throws DomainException
219  */
220  public function readElement($element, $id)
221  {
222  if ($element != 'node' && $element != 'way' && $element != 'relation')
223  {
224  throw new DomainException("Element should be a node, a way or a relation");
225  }
226 
227  // Set the API base
228  $base = $element . '/' . $id;
229 
230  // Build the request path.
231  $path = $this->getOption('api.url') . $base;
232 
233  // Send the request.
234  $xml_string = $this->sendRequest($path);
235 
236  return $xml_string->$element;
237  }
238 
239  /**
240  * Method to update an Element [node|way|relation]
241  *
242  * @param string $element [node|way|relation]
243  * @param string $xml Full reperentation of the element with a version number
244  * @param integer $id Element identifier
245  *
246  * @return array The xml response
247  *
248  * @since 13.1
249  * @throws DomainException
250  */
251  public function updateElement($element, $xml, $id)
252  {
253  if ($element != 'node' && $element != 'way' && $element != 'relation')
254  {
255  throw new DomainException("Element should be a node, a way or a relation");
256  }
257 
258  $token = $this->oauth->getToken();
259 
260  // Set parameters.
261  $parameters = array(
262  'oauth_token' => $token['key']
263  );
264 
265  // Set the API base
266  $base = $element . '/' . $id;
267 
268  // Build the request path.
269  $path = $this->getOption('api.url') . $base;
270 
271  $header['Content-Type'] = 'text/xml';
272 
273  // Send the request.
274  $response = $this->oauth->oauthRequest($path, 'PUT', $parameters, $xml, $header);
275 
276  return $response->body;
277  }
278 
279  /**
280  * Method to delete an element [node|way|relation]
281  *
282  * @param string $element [node|way|relation]
283  * @param integer $id Element identifier
284  * @param integer $version Element version
285  * @param integer $changeset Changeset identifier
286  * @param float $latitude Latitude of the element
287  * @param float $longitude Longitude of the element
288  *
289  * @return array The XML response
290  *
291  * @since 13.1
292  * @throws DomainException
293  */
294  public function deleteElement($element, $id, $version, $changeset, $latitude = null, $longitude = null)
295  {
296  if ($element != 'node' && $element != 'way' && $element != 'relation')
297  {
298  throw new DomainException("Element should be a node, a way or a relation");
299  }
300 
301  $token = $this->oauth->getToken();
302 
303  // Set parameters.
304  $parameters = array(
305  'oauth_token' => $token['key']
306  );
307 
308  // Set the API base
309  $base = $element . '/' . $id;
310 
311  // Build the request path.
312  $path = $this->getOption('api.url') . $base;
313 
314  // Create xml
315  $xml = '<?xml version="1.0" encoding="UTF-8"?>
316  <osm version="0.6" generator="JOpenstreetmap">
317  <' . $element . ' id="' . $id . '" version="' . $version . '" changeset="' . $changeset . '"';
318 
319  if (!empty($latitude) && !empty($longitude))
320  {
321  $xml .= ' lat="' . $latitude . '" lon="' . $longitude . '"';
322  }
323 
324  $xml .= '/></osm>';
325 
326  $header['Content-Type'] = 'text/xml';
327 
328  // Send the request.
329  $response = $this->oauth->oauthRequest($path, 'DELETE', $parameters, $xml, $header);
330 
331  return $response->body;
332  }
333 
334  /**
335  * Method to get history of an element [node|way|relation]
336  *
337  * @param string $element [node|way|relation]
338  * @param integer $id Element identifier
339  *
340  * @return array The XML response
341  *
342  * @since 13.1
343  * @throws DomainException
344  */
345  public function historyOfElement($element, $id)
346  {
347  if ($element != 'node' && $element != 'way' && $element != 'relation')
348  {
349  throw new DomainException("Element should be a node, a way or a relation");
350  }
351 
352  // Set the API base
353  $base = $element . '/' . $id . '/history';
354 
355  // Build the request path.
356  $path = $this->getOption('api.url') . $base;
357 
358  // Send the request.
359  $xml_string = $this->sendRequest($path);
360 
361  return $xml_string->$element;
362  }
363 
364  /**
365  * Method to get details about a version of an element [node|way|relation]
366  *
367  * @param string $element [node|way|relation]
368  * @param integer $id Element identifier
369  * @param integer $version Element version
370  *
371  * @return array The XML response
372  *
373  * @since 13.1
374  * @throws DomainException
375  */
376  public function versionOfElement($element, $id ,$version)
377  {
378  if ($element != 'node' && $element != 'way' && $element != 'relation')
379  {
380  throw new DomainException("Element should be a node, a way or a relation");
381  }
382 
383  // Set the API base
384  $base = $element . '/' . $id . '/' . $version;
385 
386  // Build the request path.
387  $path = $this->getOption('api.url') . $base;
388 
389  // Send the request.
390  $xml_string = $this->sendRequest($path);
391 
392  return $xml_string->$element;
393  }
394 
395  /**
396  * Method to get data about multiple ids of an element [node|way|relation]
397  *
398  * @param string $element [nodes|ways|relations] - use plural word
399  * @param string $params Comma separated list of ids belonging to type $element
400  *
401  * @return array The XML response
402  *
403  * @since 13.1
404  * @throws DomainException
405  */
406  public function multiFetchElements($element, $params)
407  {
408  if ($element != 'nodes' && $element != 'ways' && $element != 'relations')
409  {
410  throw new DomainException("Element should be nodes, ways or relations");
411  }
412 
413  // Get singular word
414  $single_element = substr($element, 0, strlen($element) - 1);
415 
416  // Set the API base, $params is a string with comma seperated values
417  $base = $element . '?' . $element . "=" . $params;
418 
419  // Build the request path.
420  $path = $this->getOption('api.url') . $base;
421 
422  // Send the request.
423  $xml_string = $this->sendRequest($path);
424 
425  return $xml_string->$single_element;
426  }
427 
428  /**
429  * Method to get relations for an Element [node|way|relation]
430  *
431  * @param string $element [node|way|relation]
432  * @param integer $id Element identifier
433  *
434  * @return array The XML response
435  *
436  * @since 13.1
437  * @throws DomainException
438  */
439  public function relationsForElement($element, $id)
440  {
441  if ($element != 'node' && $element != 'way' && $element != 'relation')
442  {
443  throw new DomainException("Element should be a node, a way or a relation");
444  }
445 
446  // Set the API base
447  $base = $element . '/' . $id . '/relations';
448 
449  // Build the request path.
450  $path = $this->getOption('api.url') . $base;
451 
452  // Send the request.
453  $xml_string = $this->sendRequest($path);
454 
455  return $xml_string->$element;
456  }
457 
458  /**
459  * Method to get ways for a Node element
460  *
461  * @param integer $id Node identifier
462  *
463  * @return array The XML response
464  *
465  * @since 13.1
466  */
467  public function waysForNode($id)
468  {
469  // Set the API base
470  $base = 'node/' . $id . '/ways';
471 
472  // Build the request path.
473  $path = $this->getOption('api.url') . $base;
474 
475  // Send the request.
476  $xml_string = $this->sendRequest($path);
477 
478  return $xml_string->way;
479  }
480 
481  /**
482  * Method to get full information about an element [way|relation]
483  *
484  * @param string $element [way|relation]
485  * @param integer $id Identifier
486  *
487  * @return array The XML response
488  *
489  * @since 13.1
490  * @throws DomainException
491  */
492  public function fullElement($element, $id)
493  {
494  if ($element != 'way' && $element != 'relation')
495  {
496  throw new DomainException("Element should be a way or a relation");
497  }
498 
499  // Set the API base
500  $base = $element . '/' . $id . '/full';
501 
502  // Build the request path.
503  $path = $this->getOption('api.url') . $base;
504 
505  // Send the request.
506  $xml_string = $this->sendRequest($path);
507 
508  return $xml_string->node;
509  }
510 
511  /**
512  * Method used by the DWG to hide old versions of elements containing data privacy or copyright infringements
513  *
514  * @param string $element [node|way|relation]
515  * @param integer $id Element identifier
516  * @param integer $version Element version
517  * @param integer $redaction_id Redaction id
518  *
519  * @return array The xml response
520  *
521  * @since 13.1
522  * @throws DomainException
523  */
524  public function redaction($element, $id, $version, $redaction_id)
525  {
526  if ($element != 'node' && $element != 'way' && $element != 'relation')
527  {
528  throw new DomainException("Element should be a node, a way or a relation");
529  }
530 
531  $token = $this->oauth->getToken();
532 
533  // Set parameters.
534  $parameters = array(
535  'oauth_token' => $token['key']
536  );
537 
538  // Set the API base
539  $base = $element . '/' . $id . '/' . $version . '/redact?redaction=' . $redaction_id;
540 
541  // Build the request path.
542  $path = $this->getOption('api.url') . $base;
543 
544  // Send the request.
545  $response = $this->oauth->oauthRequest($path, 'PUT', $parameters);
546 
547  $xml_string = simplexml_load_string($response->body);
548 
549  return $xml_string;
550  }
551 }