-
Notifications
You must be signed in to change notification settings - Fork 13
/
UserChangesets.pm
503 lines (446 loc) · 17 KB
/
UserChangesets.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
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
#!/usr/bin/perl
package UserChangesets;
use utf8;
use strict;
use warnings;
use File::Path qw(make_path);
use URI::Escape;
use HTTP::Date qw(str2time time2isoz);
use XML::Twig;
use OsmApi;
use OsmData;
use Changeset;
# -----------------------------------------------------------------------------
# Converts date string from script arguments to timestamp
# Returns undefined if date format is not recognized
sub parse_date
{
my ($date) = @_;
return undef unless defined($date);
$date = "$1-$2-01" if $date =~ /^(\d\d\d\d)-(\d\d)$/;
$date = "$1-01-01" if $date =~ /^(\d\d\d\d)$/;
return str2time($date, "GMT");
}
# -----------------------------------------------------------------------------
sub print_date_range
{
my ($from_timestamp, $to_timestamp) = @_;
print "limiting results to time range ";
print time2isoz($from_timestamp);
print " .. ";
print defined($to_timestamp) ? time2isoz($to_timestamp) : "now";
print "\n";
}
# -----------------------------------------------------------------------------
# Downloads given user's changeset metadata (open/close dates, bboxes, tags, ...)
# Parameters: metadata directory for output, user argument, from timestamp, to timestamp
# user argument is either display_name=... or user=... with urlencoded display name or id
sub download_metadata
{
my ($metadata_dirname, $user_arg, $from_timestamp, $to_timestamp) = @_;
my $updated_to_timestamp = $to_timestamp;
my %visited_changesets = ();
my $download_more = 1;
# existing metadata check phase
foreach my $list_filename (list_osm_filenames($metadata_dirname))
{
my $bottom_created_at;
iterate_over_changesets($list_filename, sub {
my ($id, $created_at, $closed_at) = @_;
$bottom_created_at = $created_at;
if (!$visited_changesets{$id}) {
$visited_changesets{$id} = 1;
}
});
($updated_to_timestamp, $download_more) = update_to_timestamp($updated_to_timestamp, $bottom_created_at) if defined($bottom_created_at);
}
# new metadata download phase
while ($download_more)
{
my $time_arg = "";
if (defined($updated_to_timestamp))
{
$time_arg = "time=" . make_http_date_from_timestamp($from_timestamp) . "," . make_http_date_from_timestamp($updated_to_timestamp);
print "requesting changeset metadata down from " . time2isoz($updated_to_timestamp) . "\n";
}
else
{
$time_arg = "time=" . make_http_date_from_timestamp($from_timestamp);
print "requesting changeset metadata down from current moment\n";
}
my $resp = OsmApi::get("changesets?$user_arg&$time_arg");
if (!$resp->is_success)
{
die "changeset metadata fetch failed: " . $resp->status_line;
}
my $list = $resp->content;
my ($top_created_at, $bottom_created_at);
my $new_changesets_count = 0;
iterate_over_changesets(\$list, sub {
my ($id, $created_at, $closed_at) = @_;
$bottom_created_at = $created_at;
$top_created_at = $created_at unless defined($top_created_at);
if (!$visited_changesets{$id}) {
$new_changesets_count++;
$visited_changesets{$id} = 1;
}
});
if (defined($top_created_at))
{
make_path($metadata_dirname);
my $list_filename = "$metadata_dirname/" . make_filename_from_date_attr_value($top_created_at);
open(my $list_fh, '>', $list_filename) or die "can't open changeset list file '$list_filename' for writing";
print $list_fh $list;
close $list_fh;
}
last if $new_changesets_count == 0;
($updated_to_timestamp) = update_to_timestamp($updated_to_timestamp, $bottom_created_at);
}
}
# -----------------------------------------------------------------------------
# Downloads changeset changes (elements) matching provided metadata and date rande
# Parameters: metadata directory to be scanned, changes directory for output, from timestamp, to timestamp
sub download_changes
{
my ($metadata_dirname, $changes_dirname, $from_timestamp, $to_timestamp) = @_;
my %changesets_in_range = ();
my %changesets_downloaded = ();
my %changesets_to_download = ();
my @changesets_queue;
foreach my $list_filename (list_osm_filenames($metadata_dirname))
{
iterate_over_changesets_in_time_range($list_filename, $from_timestamp, $to_timestamp, sub {
my ($id) = @_;
$changesets_in_range{$id} = 1;
my $changes_filename = "$changes_dirname/$id.osc";
if (-f $changes_filename)
{
$changesets_downloaded{$id} = 1;
}
else
{
push @changesets_queue, $id unless $changesets_to_download{$id};
$changesets_to_download{$id} = 1;
}
});
}
if (@changesets_queue > 0)
{
print((0 + @changesets_queue) . " changesets left to download\n");
}
else
{
print("all " . (keys %changesets_in_range) . " changesets already downloaded\n");
}
foreach my $id (@changesets_queue)
{
make_path($changes_dirname);
print("downloading $id.osc (" . (1 + keys %changesets_downloaded) . "/" . (keys %changesets_in_range) . ")\n");
my $changes_filename = "$changes_dirname/$id.osc";
my $osc = Changeset::download($id) or die "failed to download changeset $id";
open(my $fh, '>', $changes_filename) or die "can't open changes file '$changes_filename' for writing";
print $fh $osc;
close $fh;
delete $changesets_to_download{$id};
$changesets_downloaded{$id} = 1;
}
}
# -----------------------------------------------------------------------------
sub download_previous
{
my ($metadata_dirname, $changes_dirname, $previous_dirname, $store_dirname, $from_timestamp, $to_timestamp) = @_;
my $changesets = read_metadata($metadata_dirname, $from_timestamp, $to_timestamp);
my @ids_of_missing = grep { !-f "$previous_dirname/$_.osm" } keys %$changesets;
if (scalar @ids_of_missing == 0)
{
print "all previous osm files already present\n";
return;
}
my @ids = sort {$changesets->{$b}{created_at_timestamp} <=> $changesets->{$a}{created_at_timestamp}} @ids_of_missing;
my $data = read_changes($changes_dirname, $store_dirname, @ids);
write_previous($previous_dirname, $store_dirname, $data, @ids);
}
# -----------------------------------------------------------------------------
# Count downloaded changesets inside given date range
# Parameters: metadata directory, changes directory, from timestamp, to timestamp
sub count
{
my ($metadata_dirname, $changes_dirname, $from_timestamp, $to_timestamp) = @_;
my %visited_changesets = ();
my $metadata_count = 0;
my $changes_count = 0;
foreach my $list_filename (list_osm_filenames($metadata_dirname))
{
iterate_over_changesets_in_time_range($list_filename, $from_timestamp, $to_timestamp, sub {
my ($id) = @_;
return if $visited_changesets{$id};
$visited_changesets{$id} = 1;
$metadata_count++;
if (defined($changes_dirname)) {
my $changes_filename = "$changes_dirname/$id.osc";
$changes_count++ if -e $changes_filename;
}
});
}
print "downloaded $metadata_count changeset metadata records\n";
print "downloaded $changes_count changeset change files\n" if defined($changes_dirname);
}
# -----------------------------------------------------------------------------
sub read_metadata
{
my ($metadata_dirname, $from_timestamp, $to_timestamp) = @_;
my $changesets = {};
foreach my $metadata_filename (list_osm_filenames($metadata_dirname))
{
print STDERR "reading metadata file $metadata_filename\n" if $OsmApi::prefs->{'debug'};
my $twig = XML::Twig->new()->parsefile($metadata_filename);
foreach my $changeset_element ($twig->root->children)
{
my $id = $changeset_element->att('id');
next if $changesets->{$id};
my $created_at = $changeset_element->att('created_at');
my $closed_at = $changeset_element->att('closed_at');
next if (str2time($closed_at) < $from_timestamp);
next if (defined($to_timestamp) && str2time($created_at) >= $to_timestamp);
my $comment_tag = $changeset_element->first_child('tag[@k="comment"]');
my $comment = $comment_tag ? $comment_tag->att('v') : "";
my $changeset = {
created_at_timestamp => str2time($created_at),
created_at => $created_at,
closed_at => $closed_at,
comment => $comment,
};
for my $key ('changes_count', 'min_lat', 'max_lat', 'min_lon', 'max_lon')
{
$changeset->{$key} = $changeset_element->att($key);
}
$changesets->{$id} = $changeset;
}
}
return $changesets;
}
sub read_changes
{
my ($changes_dirname, $store_dirname, @ids) = @_;
my $data = OsmData::blank_data();
OsmData::read_store_files("$store_dirname/changes", $data) if defined($store_dirname);
my @ids_to_parse = ();
my $bytes_to_parse = 0;
foreach my $id (@ids)
{
my $changes_filename = "$changes_dirname/$id.osc";
next unless -f $changes_filename;
my $timestamp = (stat $changes_filename)[9];
next if exists $data->{changesets}{$id} && $data->{changesets}{$id}[OsmData::DOWNLOAD_TIMESTAMP] <= $timestamp;
$bytes_to_parse += (stat $changes_filename)[7];
push @ids_to_parse, $id;
}
my $files_to_parse = scalar(@ids_to_parse);
return $data if $files_to_parse == 0;
print STDERR "going to parse $files_to_parse files, $bytes_to_parse bytes\n";
my $new_data = OsmData::blank_data();
my $have_changes_to_store = 0;
my $files_parsed = 0;
my $bytes_parsed = 0;
my $quit = 0;
local $SIG{INT} = sub {
print STDERR "will interrupt after parsing and storing the current changes file\n";
$quit = 1;
};
foreach my $id (@ids_to_parse)
{
last if $quit;
my $changes_filename = "$changes_dirname/$id.osc";
print STDERR "reading changes file $changes_filename ($files_parsed/$files_to_parse files) ($bytes_parsed/$bytes_to_parse bytes)\n" if $OsmApi::prefs->{'debug'};
my $timestamp = (stat $changes_filename)[9];
my $any_changes = OsmData::parse_changes_file($new_data, $id, $changes_filename, $timestamp);
$have_changes_to_store ||= $any_changes;
$bytes_parsed += (stat $changes_filename)[7];
$files_parsed++;
}
if (defined($store_dirname) && $have_changes_to_store) {
OsmData::write_store_file("$store_dirname/changes", $new_data);
}
die "interrupting" if $quit;
OsmData::merge_data($data, $new_data);
return $data;
}
sub merge_previous
{
my ($store_dirname, $data) = @_;
# unlike changes, previous versions are always parsed
OsmData::read_store_files("$store_dirname/previous", $data) if defined($store_dirname);
}
sub write_previous
{
my ($previous_dirname, $store_dirname, $data, @ids) = @_;
OsmData::read_store_files("$store_dirname/previous", $data) if defined($store_dirname);
my %data_to_write = ();
foreach my $id (@ids)
{
my @changes = @{$data->{changesets}{$id}[OsmData::CHANGES]};
my $eivs_in_changeset = [];
foreach (@changes)
{
my ($e, $i, $v) = @$_;
$eivs_in_changeset->[$e]{$i}{$v} = 1;
}
my @eivs_to_write = ();
foreach (@changes)
{
my ($e, $i, $v) = @$_;
next if $v <= 1;
my $w = $v - 1;
next if exists $eivs_in_changeset->[$e]{$i}{$w};
push @eivs_to_write, [$e, $i, $w];
}
$data_to_write{$id} = \@eivs_to_write;
}
my @download_queues = ([], [], []);
my @changeset_remaining_in_queue_counts = (scalar @ids) x 3;
my %changeset_element_types_remaining = ();
foreach my $id (@ids)
{
$changeset_element_types_remaining{$id} = 0b111;
foreach (@{$data_to_write{$id}})
{
my ($e, $i, $v) = @$_;
next if exists $data->{elements}[$e]{$i}{$v};
push @{$download_queues[$e]}, [$i, $v];
}
push @{$download_queues[$_]}, [$id, 0] foreach (0..2);
}
make_path $previous_dirname;
my $new_data = OsmData::blank_data();
my $have_changes_to_store = 0;
my $quit = 0;
local $SIG{INT} = sub {
print STDERR "will interrupt after downloading, parsing and storing the current batch of elements\n";
$quit = 1;
};
while (1)
{
last if $quit;
my $selected_queue_number = 0;
my $selected_queue_remaining_count = $changeset_remaining_in_queue_counts[0];
for my $e (1..2)
{
next if $selected_queue_remaining_count >= $changeset_remaining_in_queue_counts[$e];
$selected_queue_number = $e;
$selected_queue_remaining_count = $changeset_remaining_in_queue_counts[$e];
}
last if $selected_queue_remaining_count <= 0;
my @changesets_ready_for_writing = ();
my $selected_queue = $download_queues[$selected_queue_number];
my $query = "";
while (my $queue_item = shift @$selected_queue)
{
my ($i, $v) = @$queue_item;
if ($v == 0)
{
$changeset_remaining_in_queue_counts[$selected_queue_number]--;
$changeset_element_types_remaining{$i} &= ~(1 << $selected_queue_number);
if ($changeset_element_types_remaining{$i} == 0)
{
push @changesets_ready_for_writing, $i;
}
next;
}
$query .= "," if length($query) > 0;
$query .= $i."v".$v;
if (length($query) > 7500)
{
unshift @$selected_queue, $queue_item;
last;
}
}
if (length($query) > 0)
{
my $element = OsmData::element_string($selected_queue_number);
$query = $element."s?".$element."s=".$query;
my $resp = OsmApi::get("$query&show_redactions=true", "", 1);
if (!$resp->is_success)
{
die "previous element versions cannot be retrieved: ".$resp->status_line."\n"; # TODO bisection fallback, esp. for redacted elements w/o moderator role
}
my $new_data_chunk = OsmData::blank_data();
my $any_changes = OsmData::parse_elements($new_data_chunk, $resp->content());
$have_changes_to_store ||= $any_changes;
OsmData::merge_data($new_data, $new_data_chunk);
OsmData::merge_data($data, $new_data_chunk);
}
for my $id (@changesets_ready_for_writing)
{
OsmData::write_osm_file("$previous_dirname/$id.osm", $data, @{$data_to_write{$id}});
}
}
if (defined($store_dirname) && $have_changes_to_store)
{
OsmData::write_store_file("$store_dirname/previous", $new_data);
}
}
sub list_osm_filenames
{
my $dirname = shift;
return reverse glob qq{"$dirname/*.osm"};
}
sub iterate_over_changesets
{
my ($list_source, $handler) = @_;
open my $list_fh, '<', $list_source;
while (<$list_fh>)
{
next unless /<changeset/;
/id="(\d+)"/;
my $id = $1;
/created_at="([^"]*)"/;
my $created_at = $1;
/closed_at="([^"]*)"/;
my $closed_at = $1;
next unless defined($id) && defined($created_at) && defined($closed_at);
$handler->($id, $created_at, $closed_at);
}
close $list_fh;
}
sub iterate_over_changesets_in_time_range
{
my ($list_source, $from_timestamp, $to_timestamp, $handler) = @_;
iterate_over_changesets($list_source, sub {
my ($id, $created_at, $closed_at) = @_;
return if (str2time($closed_at) < $from_timestamp);
return if (defined($to_timestamp) && str2time($created_at) >= $to_timestamp);
$handler->($id);
});
}
sub update_to_timestamp
{
my ($to_timestamp, $bottom_created_at) = @_;
my $new_timestamp = str2time($bottom_created_at) + 1;
my $updated = !defined($to_timestamp) || $new_timestamp < $to_timestamp;
if ($updated)
{
$to_timestamp = $new_timestamp;
}
return ($to_timestamp, $updated);
}
sub make_http_date_from_timestamp
{
my $timestamp = shift;
my $date = time2isoz($timestamp);
return uri_escape(make_compact_date($date));
}
sub make_filename_from_date_attr_value
{
my $date_attr_value = shift;
my $timestamp = str2time($date_attr_value);
die "invalid date format in xml date attribute ($date_attr_value)" unless defined($timestamp);
return make_compact_date(time2isoz($timestamp)) . ".osm";
}
sub make_compact_date
{
my $date = shift;
$date =~ s/ /T/;
$date =~ tr/-://d;
return $date;
}
1;