-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUndo.pm
194 lines (177 loc) · 5.66 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
#!/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 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.
#
# 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
# return:
# success=1 failure=undef no action necessary=0
sub undo
{
my ($what, $id, $undo_user, $undo_changeset, $changeset) = @_;
my ($action, $xml) =
determine_undo_action($what, $id, $undo_user, $undo_changeset, $changeset);
return 0 unless defined ($action);
if ($action eq "modify")
{
my $resp = OsmApi::put("$what/$id", "<osm version='0.6'>\n$xml</osm>");
if (!$resp->is_success)
{
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, $changeset) = @_;
# 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 };
}
my $undo=0;
my $copy=0;
my $out = "";
my $lastedit;
my $lastcs;
my $undo_version;
my $restore_version;
my $override_version;
my $override = 0;
my $force = 0; # if this is set to 1, any object touched by the undo userwill be reverted even if there are later modifications by others
my $resp = OsmApi::get("$what/$id/history");
if (!$resp->is_success)
{
print STDERR "$what $id cannot be retrieved: ".$resp->status_line."\n";
return undef;
}
my $content=$resp->content();
warn "check $content";
foreach (split(/\n/, $content))
{
if (/<$what/)
{
/\sid='([^']+)'/ or die "mising id in $_";
die unless $id eq $1;
/\sversion='([^']+)'/ or die "missing version in $_";
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})))
{
$undo=1;
$copy=0;
$undo_version = $version;
}
else
{
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/);
}
};
if ($undo || $override)
{
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"/;
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;
}
}
1;