forked from pear/Services_Openstreetmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChangeset.php
925 lines (859 loc) · 28.1 KB
/
Changeset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
<?php
/**
* Changeset.php
* 25-Apr-2011
*
* PHP Version 5
*
* @category Services
* @package Services_OpenStreetMap
* @subpackage Services_OpenStreetMap_Object
* @author Ken Guest <[email protected]>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @version Release: @package_version@
* @link Changeset.php
*/
/**
* Services_OpenStreetMap_Changeset
*
* @category Services
* @package Services_OpenStreetMap
* @subpackage Services_OpenStreetMap_Object
* @author Ken Guest <[email protected]>
* @author Valery Khvalov <[email protected]>
* @license BSD http://www.opensource.org/licenses/bsd-license.php
* @link Changeset.php
*/
class Services_OpenStreetMap_Changeset extends Services_OpenStreetMap_Object
{
/**
* Array of tags in key/value format
*
* @var array
*/
protected $tags = [];
/**
* What type object this is.
*
* @var string
*/
protected $type = 'changeset';
/**
* Array containing members of what this changeset represents.
*
* @var array
*/
protected $members = [];
/**
* Array of the Ids of the members of what this changeset represents.
*
* @var array
*/
protected $membersIds = [];
/**
* Whether the changeset is open.
*
* @var bool
*/
protected $open = false;
/**
* The Id of this changeset.
*
* @var string|null
*/
protected $id = null;
/**
* The OsmChange XML for this changeset.
*
* @var string|null
*/
protected $osmChangeXml = null;
/**
* Used to keep track of Id updates.
*
* @var array
*/
protected $updateMap = [];
/**
* Atomic?
*
* @var bool
*/
protected $atomic;
/**
* The message set for the changeset
*
* @var string
*/
protected $message = '';
/**
* Set if requesting for someone to review uploaded changeset.
*
* @var mixed
*/
protected $reviewRequested = false;
const ATOMIC = 'atomic';
const MESSAGE = 'message';
const REVIEW_REQUESTED = 'reviewRequested';
/**
* Constructor
*
* @param array $settings Associative array of settings for the changeset.
*
* bool $atomic Whether changeset is atomic or not.
* string $message The changeset log message.
* bool $reviewRequested Set if a review of the changes is required.
*
* @return Services_OpenStreetMap_Changeset
*/
public function __construct($settings = [])
{
if (array_key_exists(self::ATOMIC, $settings)) {
$this->atomic = $settings[self::ATOMIC];
}
if (array_key_exists(self::MESSAGE, $settings)) {
$this->message = $settings[self::MESSAGE];
}
if (array_key_exists(self::REVIEW_REQUESTED, $settings)) {
$this->reviewRequested = $settings[self::REVIEW_REQUESTED];
}
}
/**
* Set tag to [new] key/value pair.
*
* The object is returned, supporting Fluent coding style.
*
* <code>
* $changeset->setTag('key', 'value')->setTag(...);
* </code>
*
* @param mixed $key key
* @param mixed $value value
*
* @return Services_OpenStreetMap_Changeset
*/
public function setTag($key, $value): Services_OpenStreetMap_Changeset
{
$this->tags[$key] = $value;
return $this;
}
/**
* Return the tags set for this changeset in question.
*
* @return array tags
*/
public function getTags(): array
{
return $this->tags;
}
/**
* Return value of specified tag as set against this changeset.
* If tag isn't set, return null.
*
* @param string $key Key value, For example, 'created_by', 'automatic' etc
*
* @return string|null
*/
public function getTag(string $key): ?string
{
if (isset($this->tags[$key])) {
return $this->tags[$key];
} else {
return null;
}
}
/**
* Begin changeset transaction.
*
* @param string $message The changeset log message. Overrides same as set in constructor.
*
* @return void
* @throws Services_OpenStreetMap_RuntimeException If either user or
* password are not set.
*/
public function begin(string $message): void
{
$response = null;
$code = null;
$this->members = [];
$this->open = true;
$configObj = $this->getConfig();
if ($message == '') {
$message = $this->message;
}
$userAgent = $configObj->getValue('User-Agent');
$doc = "<?xml version='1.0' encoding=\"UTF-8\"?>\n" .
'<osm version="0.6" generator="' . $userAgent . '">'
. "<changeset id='0' open='false'>";
$this->setTag('comment' , $message);
$this->setTag('created_by' , $userAgent . '/0.1');
$this->setTag('review_requested' , ($this->reviewRequested ? 'yes' : 'no'));
$tags = $this->getTags();
foreach ($tags as $key => $value) {
$doc .= '<tag k="' . $key . '" v="' . $value . '"/>';
}
$doc .= '</changeset></osm>';
$url = $configObj->getValue('server')
. 'api/'
. $configObj->getValue('api_version')
. '/changeset/create';
$user = $configObj->getValue('user');
$password = $configObj->getValue('password');
/* Oauth2 Field */
$oauth2_token = $configObj->getValue('oauth2_token');
/* Oauth1 Fields */
$oauth_consumer_key = $configObj->getValue('oauth_consumer_key');
$oauth_token = $configObj->getValue('oauth_token');
$consumer_secret = $configObj->getValue('consumer_secret');
$oauth_token_secret = $configObj->getValue('oauth_token_secret');
if ($user !== null && $password !== null) {
$response = $this->getTransport()->getResponse(
$url,
HTTP_Request2::METHOD_PUT,
$user,
$password,
$doc,
null,
[['Content-type', 'text/xml', true]]
);
} elseif (!empty($oauth2_token)) {
$response = $this->getTransport()->getResponse(
$url,
HTTP_Request2::METHOD_PUT,
null,
null,
$doc,
null,
[
['Content-type', 'text/xml', true],
['Authorization', 'Bearer ' . $oauth2_token, true]
]
);
} elseif (!empty($oauth_consumer_key)
&& !empty($oauth_token)
&& !empty($consumer_secret)
&& !empty($oauth_token_secret)
) {
include_once 'Services/OpenStreetMap/Helper/OAuth.php';
$timest = Services_OpenStreetMap_Helper_OAuth::getOauthTimestamp();
$nonce = Services_OpenStreetMap_Helper_OAuth::getOauthNonce();
$oAuthArray = [
'oauth_consumer_key' => $oauth_consumer_key,
'oauth_nonce' => $nonce,
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => $timest,
'oauth_token' => $oauth_token,
'oauth_version' => '1.0'
];
$authString = Services_OpenStreetMap_Helper_OAuth::assocArrayToString($oAuthArray);
$encoded = '';
if (is_string($authString)) {
$encoded = rawurlencode($authString);
}
$hashString = HTTP_Request2::METHOD_PUT . '&' . rawurlencode($url) . '&' . $encoded;
$oAuthArray['oauth_signature'] = Services_OpenStreetMap_Helper_OAuth::getOauthSignature(
$consumer_secret . '&' . $oauth_token_secret,
$hashString
);
$authStr = 'OAuth ' . Services_OpenStreetMap_Helper_OAuth::assocArrayToString(
$oAuthArray,
'=',
', ',
'"'
);
$response = $this->getTransport()->getResponse(
$url,
HTTP_Request2::METHOD_PUT,
null,
null,
$doc,
null,
[
['Content-type', 'text/xml', true],
['Authorization', $authStr, true]
]
);
} else {
try {
$this->_validateCredentials($user, $password);
} catch (Exception $ex) {
$message = $ex->getMessage();
throw new Services_OpenStreetMap_RuntimeException($message);
}
}
$code = $response->getStatus();
if (Services_OpenStreetMap_Transport::OK == $code) {
$trimmed = trim($response->getBody());
if (is_numeric($trimmed)) {
$this->id = $trimmed;
}
}
}
/**
* Add object to the changeset so changes can be transmitted to the server.
*
* @param Services_OpenStreetMap_Object $object OSM object
*
* @return void
* @throws Services_OpenStreetMap_RuntimeException If an object has already
* been added to the changeset
* or has been added to a
* closed changeset.
*/
public function add(Services_OpenStreetMap_Object $object): void
{
if (!$this->open) {
throw new Services_OpenStreetMap_RuntimeException(
'Object added to closed changeset'
);
}
$object->setChangesetId($this->getId());
$objectId = $object->getType() . $object->getId();
if (!in_array($objectId, $this->membersIds)) {
$this->members[] = $object;
$this->membersIds[] = $objectId;
} else {
throw new Services_OpenStreetMap_RuntimeException(
'Object added to changeset already'
);
}
}
/**
* Commit changeset, posting to server.
*
* Generate osmChange document and post it to the server, when successful
* close the changeset. Return true on success, false otherwise.
*
* https://wiki.openstreetmap.org/wiki/API_v0.6#Close:_PUT_/api/0.6/changeset/#id/close
*
* @return bool Success
* @link http://wiki.openstreetmap.org/wiki/OsmChange
* @throws Services_OpenStreetMap_RuntimeException If changeset Id is not
* numeric.
* @throws Services_OpenStreetMap_Exception If changeset is already
* closed.
*/
public function commit(): bool
{
if (!$this->open) {
throw new Services_OpenStreetMap_Exception(
'Attempt to commit a closed changeset'
);
}
$code = null;
// Generate URL that the osmChange document will be posted to
$cId = $this->getId();
$changesetValidator = new Services_OpenStreetMap_Validator_Changeset();
$changesetValidator->validate($cId);
$configObj = $this->getConfig();
$url = $configObj->getValue('server')
. 'api/'
. $configObj->getValue('api_version') .
"/changeset/{$cId}/upload";
$user = $configObj->getValue('user');
$password = $configObj->getValue('password');
/* Oauth2 Field */
$oauth2_token = $configObj->getValue('oauth2_token');
/* Oauth1 Fields */
$oauth_consumer_key = $configObj->getValue('oauth_consumer_key');
$oauth_token = $configObj->getValue('oauth_token');
$consumer_secret = $configObj->getValue('consumer_secret');
$oauth_token_secret = $configObj->getValue('oauth_token_secret');
// Post the osmChange document to the server
$response = null;
try {
$response = $this->_uploadWithUsernameAndPassword($url, $user, $password);
if ($response === null) {
$response = $this->_uploadWithOauth2($url, $oauth2_token);
}
if ($response === null) {
$response = $this->_uploadWithOauth($url, $oauth_consumer_key, $oauth_token, $consumer_secret, $oauth_token_secret);
}
if ($response === null) {
throw new Services_OpenStreetMap_RuntimeException(
"User & Password for user based auth " .
"OR oauth2_token " .
"OR oauth_consumer_key, oauth_token, consumer_secret, oauth_token_secret " .
"have to be defined to interact with OSM API"
);
}
$this->updateObjectIds($response->getBody());
} catch (Exception $ex) {
$code = $ex->getCode();
}
if (is_object($response)) {
$code = $response->getStatus();
}
$changesetValidator->validateChangesetPostedOk($code);
// Explicitly close the changeset
$url = $configObj->getValue('server')
. 'api/'
. $configObj->getValue('api_version')
. "/changeset/{$cId}/close";
$code = null;
$response = null;
try {
$response = $this->_closeWithUsernameAndPassword($url, $user, $password);
if ($response === null) {
$response = $this->_closeWithOauth2($url, $oauth2_token);
}
if ($response === null) {
$response = $this->_closeWithOauth(
$url,
$oauth_consumer_key,
$oauth_token,
$consumer_secret,
$oauth_token_secret
);
}
if ($response === null) {
throw new Services_OpenStreetMap_RuntimeException(
"User & Password for user based auth " .
"OR oauth2_token " .
"OR oauth_consumer_key, oauth_token, consumer_secret, oauth_token_secret " .
"have to be defined to interact with OSM API"
);
}
} catch (Exception $ex) {
$code = $ex->getCode();
}
if (is_object($response)) {
$code = $response->getStatus();
}
$changesetValidator->validateChangesetClosedOk($code);
$this->open = false;
return $code === 200;
}
/**
* Commit using username and password, if set.
*
* If username and password are both null, return null.
*
* @param string $url URL for uploading changeset
* @param string $user username
* @param string $password User's password
*
* @return void
*/
private function _uploadWithUsernameAndPassword($url, $user, $password)
{
if ($user === null && $password === null) {
return null;
}
return $this->getTransport()->getResponse(
$url,
HTTP_Request2::METHOD_POST,
$user,
$password,
$this->getOsmChangeXml(),
null,
[['Content-type', 'text/xml', true]]
);
}
/**
* Upload changeset via Oauth2 details
*
* @param string $url URL for uploading changeset
* @param string $oauth2_token Oauth2 token
*
* @return void
*/
private function _uploadWithOauth2($url, $oauth2_token)
{
if (!empty($oauth2_token)) {
$response = $this->getTransport()->getResponse(
$url,
HTTP_Request2::METHOD_POST,
null,
null,
$this->getOsmChangeXml(),
null,
[
['Content-type', 'text/xml', true],
['Authorization', 'Bearer ' . $oauth2_token, true]
]
);
return $response;
}
return null;
}
/**
* Upload changeset via Oauth details
*
* @param string $url URL for uploading changeset
* @param string $oauth_consumer_key Consumer key
* @param string $oauth_token Oauth token
* @param string $consumer_secret Consumer secret
* @param string $oauth_token_secret Oauth token secret
*
* @return void
*/
private function _uploadWithOauth($url, $oauth_consumer_key, $oauth_token, $consumer_secret, $oauth_token_secret)
{
if (!empty($oauth_consumer_key)
&& !empty($oauth_token)
&& !empty($consumer_secret)
&& !empty($oauth_token_secret)
) {
include_once 'Services/OpenStreetMap/Helper/OAuth.php';
$timest = Services_OpenStreetMap_Helper_OAuth::getOauthTimestamp();
$nonce = Services_OpenStreetMap_Helper_OAuth::getOauthNonce();
$oAuthArray = [
'oauth_consumer_key' => $oauth_consumer_key,
'oauth_nonce' => $nonce,
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => $timest,
'oauth_token' => $oauth_token,
'oauth_version' => '1.0'
];
$oAuthString = Services_OpenStreetMap_Helper_OAuth::assocArrayToString($oAuthArray);
$reUrl = rawurlencode($url);
$reAuthString = '';
if (is_string($oAuthString)) {
$reAuthString = rawurlencode($oAuthString);
}
$hashString = HTTP_Request2::METHOD_POST . '&' . $reUrl . '&' . $reAuthString;
$oAuthArray['oauth_signature'] = Services_OpenStreetMap_Helper_OAuth::getOauthSignature(
$consumer_secret . '&' . $oauth_token_secret,
$hashString
);
$authStr = 'OAuth ' . Services_OpenStreetMap_Helper_OAuth::assocArrayToString(
$oAuthArray,
'=',
', ',
'"'
);
$response = $this->getTransport()->getResponse(
$url,
HTTP_Request2::METHOD_POST,
null,
null,
$this->getOsmChangeXml(),
null,
[
['Content-type', 'text/xml', true],
['Authorization', $authStr, true]
]
);
return $response;
}
return null;
}
/**
* Close uploaded changeset with username and password
*
* @param string $url URL for closing uploaded changeset
* @param string $user Username
* @param string $password User's password
*
* @return void
*/
private function _closeWithUsernameAndPassword($url, $user, $password)
{
if ($user === null && $password === null) {
return null;
}
return $this->getTransport()->getResponse(
$url,
HTTP_Request2::METHOD_PUT,
$user,
$password,
null,
null,
[['Content-type', 'text/xml', true]]
);
}
/**
* Close uploaded changeset with Oauth2 details
*
* @param string $url URL for closing uploaded changeset
* @param string $oauth2_token Oauth2 token
*
* @return $request
*/
private function _closeWithOauth2($url, $oauth2_token)
{
if (!empty($oauth2_token)) {
$response = $this->getTransport()->getResponse(
$url,
HTTP_Request2::METHOD_PUT,
null,
null,
null,
null,
[
['Content-type', 'text/xml', true],
['Authorization', 'Bearer ' . $oauth2_token, true]
]
);
return $response;
}
return null;
}
/**
* Close uploaded changeset with Oauth details
*
* @param string $url URL for closing uploaded changeset
* @param string $oauth_consumer_key Consumer key
* @param string $oauth_token Oauth token
* @param string $consumer_secret Consumer secret
* @param string $oauth_token_secret Oauth token secret
*
* @return $request
*/
private function _closeWithOauth($url, $oauth_consumer_key, $oauth_token, $consumer_secret, $oauth_token_secret)
{
if (!empty($oauth_consumer_key)
&& !empty($oauth_token)
&& !empty($consumer_secret)
&& !empty($oauth_token_secret)
) {
include_once 'Services_OpenStreetMap_Helper_OAuth.php';
$timest = Services_OpenStreetMap_Helper_OAuth::getOauthTimestamp();
$nonce = Services_OpenStreetMap_Helper_OAuth::getOauthNonce();
$oAuthArray = [
'oauth_consumer_key' => $oauth_consumer_key,
'oauth_nonce' => $nonce,
'oauth_signature_method' => 'HMAC-SHA1',
'oauth_timestamp' => $timest,
'oauth_token' => $oauth_token,
'oauth_version' => '1.0'
];
$oauthString = Services_OpenStreetMap_Helper_OAuth::assocArrayToString($oAuthArray);
$reUrl = rawurlencode($url);
$reAuthString = '';
if (is_string($oauthString)) {
$reAuthString = rawurlencode($oauthString);
}
$hashString = '';
if (($reUrl !== '') && ($reAuthString !== '')) {
$hashString = HTTP_Request2::METHOD_PUT . '&' . $reUrl . '&' . $reAuthString;
}
$oAuthArray['oauth_signature'] = Services_OpenStreetMap_Helper_OAuth::getOauthSignature(
$consumer_secret . '&' . $oauth_token_secret,
$hashString
);
$authStr = 'OAuth ' . Services_OpenStreetMap_Helper_OAuth::assocArrayToString(
$oAuthArray,
'=',
', ',
'"'
);
$response = $this->getTransport()->getResponse(
$url,
HTTP_Request2::METHOD_PUT,
null,
null,
null,
null,
[
['Content-type', 'text/xml', true],
['Authorization', $authStr, true]
]
);
return $response;
}
return null;
}
/**
* Generate and return the OsmChange XML required to record the changes
* made to the object in question.
*
* @return string
* @link http://wiki.openstreetmap.org/wiki/OsmChange
*/
public function getOsmChangeXml(): string
{
if ($this->osmChangeXml === null) {
// Generate the osmChange document
$blocks = null;
foreach ($this->members as $member) {
$blocks .= $member->getOsmChangeXml() . "\n";
}
$this->setOsmChangeXml(
"<osmChange version='0.6' generator='Services_OpenStreetMap'>"
. $blocks . '</osmChange>'
);
}
return $this->osmChangeXml;
}
/**
* Set Change XML.
*
* @param string $xml OsmChange XML
*
* @return Services_OpenStreetMap_Changeset
*/
public function setOsmChangeXml(string $xml): Services_OpenStreetMap_Changeset
{
$this->osmChangeXml = $xml;
return $this;
}
/**
* Get CreatedAt time.
*
* @return string
*/
public function getCreatedAt(): string
{
return (string) $this->getAttributes()->created_at;
}
/**
* Get ClosedAt time.
*
* @return string
*/
public function getClosedAt(): string
{
return (string) $this->getAttributes()->closed_at;
}
/**
* Is the changeset open?
*
* @return boolean
*/
public function isOpen(): bool
{
$attribs = $this->getAttributes();
if ($attribs !== null) {
return $attribs->open == 'true';
}
return $this->open;
}
/**
* Return min longitude.
*
* @return float
*/
public function getMinLon(): float
{
return (float) $this->getAttributes()->min_lon;
}
/**
* Return min latitude value.
*
* @return float
*/
public function getMinLat(): float
{
return (float) $this->getAttributes()->min_lat;
}
/**
* Return max longitude value.
*
* @return float
*/
public function getMaxLon(): float
{
return (float) $this->getAttributes()->max_lon;
}
/**
* Return max latitude value.
*
* @return float
*/
public function getMaxLat(): float
{
return (float) $this->getAttributes()->max_lat;
}
/**
* Get changeset Id.
*
* @return null|string value or null if none set
*/
public function getId(): ?string
{
$p_id = parent::getId();
if ($p_id === null) {
return $this->id;
}
return $p_id;
}
/**
* Given diffResult xml, update Ids of objects that are members of the
* current changeset.
*
* @param string $body diffResult xml
*
* @return void
* @throws Services_OpenStreetMap_Exception If diffResult xml is invalid.
*/
public function updateObjectIds(string $body): void
{
$body = trim($body);
// should check here that body has expected form.
if (stripos($body, 'diffResult') === false) {
throw new Services_OpenStreetMap_Exception('Invalid diffResult XML');
}
$cxml = simplexml_load_string($body);
if ($cxml !== false) {
$obj = $cxml->xpath('//diffResult');
foreach ($obj[0]->children() as $child) {
$old_id = null;
$new_id = null;
$old_id = (string) $child->attributes()->old_id;
$new_id = (string) $child->attributes()->new_id;
$this->updateObjectId($child->getName(), $old_id, $new_id);
}
}
}
/**
* Update id of some type of object.
*
* @param string $type Object type
* @param string $oldId Old id
* @param string $newId New id
*
* @return void
*/
public function updateObjectId(string $type, string $oldId, string $newId): void
{
if ($oldId === $newId) {
return;
}
foreach ($this->members as $member) {
if ($member->getType() == $type && $member->getId() == $oldId) {
$member->setId($newId);
$this->updateMap[$oldId] = $newId;
}
}
}
/**
* Get update map.
*
* @return array
*/
public function getUpdateMap(): array
{
return $this->updateMap;
}
/**
* Validate credentials for using the API backend.
*
* @param string|null $user Username
* @param string|null $password Password
*
* @throws Services_OpenStreetMap_RuntimeException Thrown if user/password or password file not set
*
* @return void
*/
private function _validateCredentials($user, $password)
{
if ($user !== null && $password === null) {
throw new Services_OpenStreetMap_RuntimeException(
"Password must be set"
);
} elseif ($user === null && $password !== null) {
throw new Services_OpenStreetMap_RuntimeException(
"User must be set"
);
} elseif ($this->getConfig()->getValue('passwordfile') === null) {
throw new Services_OpenStreetMap_RuntimeException(
"User & Password for user based auth " .
"OR oauth2_token " .
"OR oauth_consumer_key, oauth_token, consumer_secret, oauth_token_secret " .
"have to be defined to interact with OSM API"
);
}
}
}