-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathChangeset.pm
423 lines (361 loc) · 10.9 KB
/
Changeset.pm
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
#!/usr/bin/perl
# Changeset.pm
# ------------
#
# Implements changeset operations on the OSM API
#
# Part of the "osmtools" suite of programs
# Originally written by Frederik Ramm <[email protected]>; public domain
package Changeset;
use strict;
use warnings;
use OsmApi;
use URI::Escape;
# -----------------------------------------------------------------------------
# Creates new changeset.
# Parameters: optionally, a comment
# Returns: changeset id, or undef in case of error (will write error to stderr)
sub create
{
my $commit_comment = shift;
my $resp = OsmApi::put("changeset/create", "<osm version='0.6'>".
xmlnode(-1, $commit_comment). "</osm>");
if (!$resp->is_success)
{
print STDERR "cannot create changeset: ".$resp->status_line."\n";
return undef;
}
return $resp->content();
}
# -----------------------------------------------------------------------------
# Adds (discussion) comment to existing, closed changeset
# Parameters: changeset id, comment
# Returns: 1=ok, undef=error
sub comment($$)
{
my ($id, $comment) = @_;
$comment =~ s/&/&/g;
$comment =~ s/</</g;
$comment =~ s/>/>/g;
$comment =~ s/"/"/g;
my $resp = OsmApi::post("changeset/$id/comment", "text=".uri_escape($comment));
if (!$resp->is_success)
{
print STDERR "cannot comment on changeset: ".$resp->status_line."\n";
return undef;
}
}
# -----------------------------------------------------------------------------
# Hides (discussion) changeset comment
# Parameters: comment id
# Returns: 1=ok, undef=error
sub hide_comment($)
{
my $comment_id = shift;
return modify_comment($comment_id, "hide");
}
# -----------------------------------------------------------------------------
# Unhides (discussion) changeset comment
# Parameters: comment id
# Returns: 1=ok, undef=error
sub unhide_comment($)
{
my $comment_id = shift;
return modify_comment($comment_id, "unhide");
}
# -----------------------------------------------------------------------------
# Creates XML representation of changeset
# Parameters: changeset id and optionally a comment
# Returns: the XML representation
sub xmlnode
{
my ($id, $commit_comment) = @_;
my $xml_comment = "";
if (defined($commit_comment))
{
$commit_comment =~ s/&/&/g;
$commit_comment =~ s/</</g;
$commit_comment =~ s/>/>/g;
$commit_comment =~ s/"/"/g;
$xml_comment = "<tag k='comment' v=\"$commit_comment\" />";
}
my $revision = '$Revision: 30252 $';
my $revno = 0;
$revno = $1 if ($revision =~ /:\s*(\d+)/);
return <<EOF
<changeset id='$id'>
$xml_comment
<tag k='bot' v=\"yes\" />
<tag k='created_by' v='osmtools/$revno ($^O)' />
</changeset>
EOF
}
# -----------------------------------------------------------------------------
# Updates changeset metadata on server
# This would typically be used before closing to set a commit comment
# in case the comment wasn't set on opening already.
# Parameters: id of changeset, and optional comment
# Returns: 1=ok, undef=error
sub update($$)
{
my ($id, $commit_comment) = @_;
my $resp = OsmApi::put("changeset/$id", "<osm version='0.6'>".
xmlnode($id, $commit_comment)."</osm>");
if (!$resp->is_success)
{
print STDERR "cannot update changeset: ".$resp->status_line."\n";
print STDERR $resp->content;
return undef;
}
return 1;
}
# -----------------------------------------------------------------------------
# Closes changeset.
# Parameters: changeset id
# Returns: 1=success undef=error (will write error to stderr)
sub close($)
{
my $id = shift;
my $resp = OsmApi::put("changeset/$id/close");
if (!$resp->is_success)
{
print STDERR "cannot close changeset: ".$resp->status_line."\n";
return undef;
}
return 1;
}
# -----------------------------------------------------------------------------
# Uploads changeset.
# Paramters: changeset id, content
# replaces occurrences of changeset="something" with proper id
# Returns: 1=succes undef=error (will write to stderr)
sub upload($$)
{
my ($id, $content) = @_;
OsmApi::set_timeout(7200);
$content =~ s/changeset="[^"]*"/changeset="$id"/g;
my $resp = OsmApi::post("changeset/$id/upload", $content);
if (!$resp->is_success)
{
print STDERR "cannot upload changeset: ".$resp->status_line."\n";
print STDERR $resp->content."\n";
return undef;
}
print STDERR $resp->content."\n";
return 1;
}
# -----------------------------------------------------------------------------
# Downloads changeset.
# Paramters: changeset id
# Returns: changeset contents as string, undef on error
sub download($)
{
my $csid = shift;
my $resp = OsmApi::get("changeset/$csid/download?show_redactions=true", "", 1);
if (!$resp->is_success)
{
print STDERR "changeset $csid cannot be retrieved: ".$resp->status_line."\n";
return undef;
}
return $resp->content();
}
# -----------------------------------------------------------------------------
# Downloads changeset metadata.
# Paramters: changeset id
# Returns: changeset metadata contents as string, undef on error
sub get($)
{
my $csid = shift;
my $resp = OsmApi::get("changeset/$csid?include_discussion=true");
if (!$resp->is_success)
{
print STDERR "metadata of changeset $csid cannot be retrieved: ".$resp->status_line."\n";
return undef;
}
return $resp->content();
}
# -----------------------------------------------------------------------------
# Get element versions from changeset content
# Paramters: changeset content
# Returns: array of type/id/version, undef on error
sub get_element_versions($)
{
my ($content) = @_;
my @element_versions = ();
CORE::open my $fh, '<', \$content;
while (<$fh>)
{
next unless /<(node|way|relation)/;
my $type = $1;
/id="(\d+)"/;
my $id = $1;
/version="(\d+)"/;
my $version = $1;
push @element_versions, "$type/$id/$version";
}
CORE::close $fh;
return @element_versions;
}
# -----------------------------------------------------------------------------
# Get previous versions of elements, skipping newly created ones
# Paramters: array of type/id/version
# Returns: array of type/id/version, undef on error
sub get_previous_element_versions(@)
{
my @element_versions = @_;
my @previous_element_versions = ();
iterate_over_element_versions(\@element_versions, sub {
my ($type, $id, $version) = @_;
$version -= 1;
return if $version <= 0;
push @previous_element_versions, "$type/$id/$version";
});
return @previous_element_versions;
}
# -----------------------------------------------------------------------------
# Get next versions of elements, skipping those at their top version
# Paramters: array of type/id/version
# Returns: array of type/id/version, undef on error
sub get_next_element_versions(@)
{
my @element_versions = @_;
my %type_id_versions = ();
iterate_over_element_versions(\@element_versions, sub {
my ($type, $id, $version) = @_;
$type_id_versions{"$type/$id"} = $version;
});
my @queries = prepare_download_queries(0, @element_versions);
my $top_content = run_download_queries("top", @queries);
return undef unless defined($top_content);
my @next_element_versions = ();
my @top_element_versions = get_element_versions($top_content);
iterate_over_element_versions(\@top_element_versions, sub {
my ($type, $id, $next_version) = @_;
my $version = $type_id_versions{"$type/$id"};
if ($version < $next_version)
{
push @next_element_versions, "$type/$id/" . ($version + 1);
}
});
return @next_element_versions;
}
sub download_elements($@)
{
my $relation = shift;
my @element_versions = @_;
my @queries = prepare_download_queries(1, @element_versions);
return run_download_queries($relation, @queries);
}
sub get_changeset_summary($)
{
my ($content) = @_;
my %counts = ();
my %users = ();
my %uids = ();
CORE::open my $fh, '<', \$content;
while (<$fh>)
{
next unless /<(node|way|relation)/;
/changeset="([^"]*)"/;
my $changeset = $1;
$counts{$changeset}++;
/user="([^"]*)"/;
$users{$changeset} = $1;
/uid="([^"]*)"/;
$uids{$changeset} = $1;
}
CORE::close $fh;
my @result;
foreach my $changeset (sort { $b <=> $a } keys %counts)
{
push @result, $counts{$changeset} . "," . $changeset . "," . $uids{$changeset} . "," . $users{$changeset};
}
return @result;
}
###
sub modify_comment($$)
{
my ($comment_id, $action) = @_;
my $resp = OsmApi::post("changeset/comment/$comment_id/$action", "", 1);
if (!$resp->is_success)
{
print STDERR "cannot $action changeset comment: ".$resp->status_line."\n";
return undef;
}
return 1;
}
sub prepare_download_queries($@)
{
my $with_versions = shift;
my @element_versions = @_;
my %counts = ();
my %ivs = ();
my @queries = ();
local *flush = sub($) {
my ($type) = @_;
push @queries, $type . "s?" . $type . "s=" . $ivs{$type};
delete $counts{$type};
delete $ivs{$type};
};
iterate_over_element_versions(\@element_versions, sub {
my ($type, $id, $version) = @_;
$counts{$type}++;
$ivs{$type} .= "," if exists $ivs{$type};
$ivs{$type} .= $id;
$ivs{$type} .= "v" . $version if $with_versions;
flush($type) if $counts{$type} > 700 || length($ivs{$type}) > 7500;
});
flush($_) for keys %counts;
return @queries;
}
sub run_download_queries($@)
{
my $relation = shift;
my @queries = @_;
my @contents = ();
foreach my $query (@queries)
{
my $resp = OsmApi::get("$query&show_redactions=true", "", 1);
if (!$resp->is_success)
{
print STDERR "$relation element versions cannot be retrieved: ".$resp->status_line."\n";
return undef;
}
push @contents, $resp->content();
}
return merge_osm_contents(@contents);
}
sub merge_osm_contents(@)
{
my @contents = @_;
my $i = 0;
my $result = $_;
foreach my $content (@contents)
{
CORE::open my $fh, '<', \$content;
while (<$fh>)
{
next if $i > 0 && /<\?xml/;
next if $i > 0 && /<osm/;
next if /<\/osm>/;
$result .= $_;
}
CORE::close $fh;
$i++;
}
$result .= "</osm>\n";
return $result;
}
sub iterate_over_element_versions(\@&)
{
my ($element_versions, $handler) = @_;
foreach (@$element_versions)
{
next unless /(\w+)\/(\d+)\/(\d+)/;
my $type = $1;
my $id = $2;
my $version = $3;
$handler -> ($type, $id, $version);
}
}
1;