-
Notifications
You must be signed in to change notification settings - Fork 13
/
BatchRedaction.pm
164 lines (145 loc) · 4.43 KB
/
BatchRedaction.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
#!/usr/bin/perl
# BatchRedaction.pm
# -----------------
#
# Implements redaction operations on the OSM API by reading element ids/versions from a file
#
# Part of the "osmtools" suite of programs
# public domain
package BatchRedaction;
use strict;
use warnings;
use OsmApi;
# -----------------------------------------------------------------------------
# Views specific versions of elements listed in a file
# Parameters: filename, request suffix
# request suffix is usually empty or ".json"
# Returns: 1 on success, undef on failure
sub view
{
my ($filename, $suffix) = @_;
$suffix //= "";
open(FH, '<', $filename) or return undef;
sub print_content
{
my ($content) = @_;
chomp $content;
print "$content\n\n";
}
while(<FH>)
{
chomp;
my $resp = OsmApi::get($_.$suffix);
my $code = $resp->code;
if ($resp->is_success) {
print "# $_ not redacted\n\n";
print_content $resp->content;
} elsif ($code == 404) {
print "# $_ not found\n\n";
} elsif ($code == 403) {
my $resp2 = OsmApi::get($_.$suffix."?show_redactions=true", undef, 1);
if ($resp2->is_success) {
print "# $_ redacted and can be revealed\n\n";
print_content $resp2->content;
} else {
print "# $_ redacted and cannot be revealed\n\n";
}
} else {
print "# $_ encountered unknown error\n\n";
}
}
close(FH);
return 1;
}
# -----------------------------------------------------------------------------
# Redacts specific versions of elements listed in a file
# Parameters: filename, Redaction ID
# use empty redaction id to unredact
# Returns: 1 on success, undef on failure
# stops on the first failed redaction
sub apply
{
my ($filename, $rid, $skip_errors) = @_;
my $done_exists = -e "$filename.done";
my $left_exists = -e "$filename.left";
if ($done_exists || $left_exists)
{
my $file_word = (($done_exists && $left_exists) ? "files" : "file");
print "Cannot continue because $file_word produced by an interrupted batch redaction exist:\n";
if ($done_exists)
{
print " $filename.done with elements that have redaction calls completed\n";
print " - you may put this file away where you store completed redactions\n";
}
if ($left_exists)
{
print " $filename.left with elements that have redaction calls failed or not attempted\n";
print " - you may replace the original file $filename with this one and run again\n";
}
print "Please review and rename/remove the $file_word before continuing.\n";
return undef;
}
my @elements;
open(FH, '<', $filename) or return undef;
while(<FH>)
{
chomp;
push @elements, $_;
}
close(FH);
my (@done_elements, @left_elements);
my $quit = 0;
local $SIG{INT} = sub {
print " - interrupting on next element";
$quit = 1;
};
foreach (@elements)
{
if ($quit)
{
push @left_elements, $_;
next;
}
print "redacting $_";
my $path = "$_/redact";
$path .= "?redaction=$rid" if $rid;
my $resp = OsmApi::post($path, undef, 1);
if ($resp->is_success)
{
push @done_elements, $_;
print " - done " . @done_elements . "/" . @elements . " elements\n";
}
else
{
print " - failed\n";
push @left_elements, $_;
my $m = $resp->content;
$m =~ s/\s+/ /g;
print STDERR "cannot redact $_: ".$resp->status_line.": $m\n";
$quit = 1 unless $skip_errors;
}
}
sub write_elements_file
{
my ($output_filename, @output_elements) = @_;
my $output_fh;
return if (!@output_elements);
if (!open $output_fh, '>', $output_filename)
{
print STDERR "cannot open interrupted batch redaction file $output_filename";
return
}
foreach (@output_elements)
{
print $output_fh "$_\n";
}
close $output_fh;
}
if (@left_elements)
{
write_elements_file "$filename.done", @done_elements;
write_elements_file "$filename.left", @left_elements;
}
return !@left_elements;
}
1;