-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathUndo.pm
251 lines (231 loc) · 7.51 KB
/
Undo.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
#!/usr/bin/perl
# Undo.pm
# -------
#
# Implements undo operations
#
# Part of the "osmtools" suite of programs
# Originally written by Frederik Ramm <[email protected]>; public domain
package Undo;
use strict;
use warnings;
use Util;
use OsmApi;
# undoes one change by one user (or within one changeset)
#
# if the user has multiple changes at the current end of the
# history, all of them are going to be undone (unless a
# specific changeset is given). Likewise, if the object
# has been changed multiple times in the same changeset,
# then all of these changes will be reverted.
#
# fails if the object has been last changed by someone else.
# (but see "force" flag)
#
# parameters:
# $what: 'node', 'way', or 'relation'
# $id: object id
# $undo_user: user whose operation should be undone
# (this may also be a hash reference containing multiple user
# names as keys, with any non-null value)
# (this may be undef)
# $undo_changeset: changeset whose operation should be undone
# (this may also be a hash reference containing multiple changeset
# ids as keys, with any non-null value)
# (this may be undef)
# $changeset: id of changeset to use for undo operation
# $force: 1 if subsequent changes should be overridden
# return:
# success=1 failure=undef no action necessary=0
sub undo
{
my ($what, $id, $undo_user, $undo_changeset, $undo_version, $changeset, $force) = @_;
my ($action, $xml) =
determine_undo_action($what, $id, $undo_user, $undo_changeset, $undo_version, $changeset, $force);
return 0 unless defined ($action);
# set this to 1 if you want the undo script to trim ways or relations by
# removing members that are unavailable
my $use_available_members = 0;
if ($action eq "modify")
{
my $resp = OsmApi::put("$what/$id", "<osm version='0.6'>\n$xml</osm>");
if (!$resp->is_success)
{
if ($resp->code == 412 && $use_available_members && ($what ne "node"))
{
my $newxml = remove_unavailable_members($xml);
if ($newxml ne $xml)
{
$resp = OsmApi::put("$what/$id", "<osm version='0.6'>\n$newxml</osm>");
if (!$resp->is_success)
{
print STDERR "$what $id cannot be uploaded (after trimming): ".$resp->status_line."\n";
return 0;
}
print STDERR "$what $id successfully uploaded after trimming\n";
return 1;
}
}
print STDERR "$what $id cannot be uploaded: ".$resp->status_line."\n";
return undef;
}
}
elsif ($action eq "delete")
{
my $resp = OsmApi::delete("$what/$id", "<osm version='0.6'>$xml</osm>");
if (!$resp->is_success)
{
print STDERR "$what $id cannot be deleted: ".$resp->status_line."\n";
return ($resp->code == 410) ? 0 : undef;
}
}
else
{
die "assertion failed";
}
return 1;
}
# the undo workhorse; finds out which XML to upload to the API to
# make a certain edit undone.
#
# Parameters:
# see sub undo.
#
# Returns:
# undef on error, else a two-element array where the first element is
# either "modify" or "delete" depending on the action to be taken, and
# the second element is the bare XML to send to the API. The XML has to
# be wrapped in <osm>...</osm> or inside a <modify>...</modify> or
# <delete>...</delete> block in a changeset upload.
sub determine_undo_action
{
my ($what, $id, $undo_users, $undo_changesets, $undo_versions, $changeset, $force) = @_;
# backwards compatibility
if (ref($undo_users) ne "HASH" && defined($undo_users))
{
$undo_users = { $undo_users => 1 };
}
if (ref($undo_changesets) ne "HASH" && defined($undo_changesets))
{
$undo_changesets = { $undo_changesets => 1 };
}
if (ref($undo_versions) ne "HASH" && defined($undo_versions))
{
$undo_versions = { $undo_versions => 1 };
}
my $undo=0;
my $copy=0;
my $out = "";
my $current_xml = "";
my $lastedit;
my $lastcs;
my $undo_version;
my $restore_version;
my $override_version;
my $override = 0;
my $resp = OsmApi::get("$what/$id/history");
if (!$resp->is_success)
{
print STDERR "$what $id cannot be retrieved: ".$resp->status_line."\n";
return undef;
}
foreach (split(/\n/, $resp->content()))
{
if (/<$what/)
{
$current_xml = "";
/\sid="([^"]+)"/ or die;
die unless $id eq $1;
/\sversion="([^"]+)"/ or die;
my $version = $1;
/user="([^"]+)/;
my $user=$1;
/changeset="(\d+)/;
my $cs=$1;
if ((!defined($undo_users) || defined($undo_users->{$user})) && (!defined($undo_changesets) || defined($undo_changesets->{$cs})) && (!defined($undo_versions) || defined($undo_versions->{$version})))
{
$undo=1;
$copy=0;
$undo_version = $version;
}
else
{
#print "user=$user not in undo_users\ncs=$cs not in undo_cs\nver=$version not in undo_ver\n";
if ($undo && $force)
{
$override = 1;
$override_version = $version;
}
else
{
$undo=0;
$copy=1;
$out=$_ . "\n";
$restore_version = $version;
$lastedit = $user;
$lastcs = $cs;
}
}
}
elsif ($copy)
{
$out.=$_ . "\n";
$copy=0 if (/<\/$what/);
}
$current_xml .= "$_\n";
};
if ($undo || $override)
{
if (Util::object_equal($out, $current_xml))
{
print STDERR "$what $id: no change necessary as v$restore_version equals current\n";
return undef;
}
if ($override)
{
$undo_version = $override_version unless ($undo_version > $override_version);
print STDERR "$what $id: overriding subsequent changes\n";
}
if (length($out))
{
print STDERR "$what $id last edited as v$undo_version; restoring previous version $restore_version by '$lastedit'\n";
$out =~ s/version="$restore_version"/version="$undo_version"/;
$out =~ s/changeset="\d+"/changeset="$changeset"/;
print STDERR $out;
return ( "modify", $out );
}
else
{
print STDERR "$what $id was created; deleting\n";
return ( "delete", "<$what id='$id' changeset='$changeset' version='$undo_version' lat='0' lon='0' />\n" );
}
}
else
{
print STDERR "$what $id last edited in another changeset/by another user ($lastedit/$lastcs)\n";
return undef;
}
}
sub remove_unavailable_members
{
my $xml = shift;
my $out;
foreach my $line(split(/\n/, $xml))
{
print STDERR ">>$line<<\n";
if ($line =~ /nd ref="(\d+)"/)
{
$out .= $line."\n" if (OsmApi::exists("node/$1"));
}
elsif ($line =~ /member type="(\S+)" ref="(\d+)"/)
{
$out .= $line."\n" if (OsmApi::exists("$1/$2"));
}
else
{
$out .= $line."\n";
}
}
return $out;
}
1;