| 1 |
#!/usr/bin/perl -w |
| 2 |
use strict; |
| 3 |
use Getopt::Long; |
| 4 |
|
| 5 |
my ($dbtype, $dbname, $dbuser, $dbpass, $dbhost, $dbport, $help, $date, $time ); |
| 6 |
GetOptions( "type=s" => \$dbtype, |
| 7 |
"name=s" => \$dbname, |
| 8 |
"user=s" => \$dbuser, |
| 9 |
"pass=s" => \$dbpass, |
| 10 |
"host=s" => \$dbhost, |
| 11 |
"port=s" => \$dbport, |
| 12 |
"help" => \$help, |
| 13 |
"date=s" => \$date, |
| 14 |
"time=s" => \$time, |
| 15 |
); |
| 16 |
|
| 17 |
unless (defined($dbtype)) { |
| 18 |
print "You must supply a database type with the --type option.\n"; |
| 19 |
print "Further help can be found by typing 'perldoc $0'\n"; |
| 20 |
exit 1; |
| 21 |
} |
| 22 |
|
| 23 |
unless (defined($dbname)) { |
| 24 |
print "You must supply a database name with the --name option.\n"; |
| 25 |
print "Further help can be found by typing 'perldoc $0'\n"; |
| 26 |
exit 1; |
| 27 |
} |
| 28 |
|
| 29 |
unless (defined($date)) { |
| 30 |
print "You must supply the date with the --date option.\n"; |
| 31 |
print "Further help can be found by typing 'perldoc $0'\n"; |
| 32 |
exit 1; |
| 33 |
} |
| 34 |
|
| 35 |
unless ($date =~ /^\d{4}\-\d{2}\-\d{2}$/) { |
| 36 |
print "You must supply the date with --date in the format YYYY-MM-DD.\n"; |
| 37 |
print "Further help can be found by typing 'perldoc $0'\n"; |
| 38 |
exit 1; |
| 39 |
} |
| 40 |
unless (!$time || $time =~ /^\d{2}:\d{2}:\d{2}$/) { |
| 41 |
print "You must supply either no time, or the time in the format HH:MM:SS.\n"; |
| 42 |
print "Further help can be found by typing 'perldoc $0'\n"; |
| 43 |
exit 1; |
| 44 |
} |
| 45 |
|
| 46 |
if ($help) { |
| 47 |
print "Help can be found by typing 'perldoc $0'\n"; |
| 48 |
exit 0; |
| 49 |
} |
| 50 |
|
| 51 |
my %setup_modules = ( postgres => "Wiki::Toolkit::Store::Pg", |
| 52 |
mysql => "Wiki::Toolkit::Store::MySQL", |
| 53 |
sqlite => "Wiki::Toolkit::Store::SQLite" |
| 54 |
); |
| 55 |
|
| 56 |
unless ( defined($setup_modules{$dbtype}) ) { |
| 57 |
print "dbtype must be one of 'postgres', 'mysql', and 'sqlite'\n"; |
| 58 |
print "further help can be found by typing 'perldoc $0'\n"; |
| 59 |
exit 1; |
| 60 |
} |
| 61 |
|
| 62 |
# Load classes |
| 63 |
require Wiki::Toolkit; |
| 64 |
my $class = $setup_modules{$dbtype}; |
| 65 |
eval "require $class"; |
| 66 |
if ( $@ ) { |
| 67 |
print "Couldn't 'use' $class: $@\n"; |
| 68 |
exit 1; |
| 69 |
} |
| 70 |
|
| 71 |
# Create a store instance |
| 72 |
my $store; |
| 73 |
my $args = "dbname=>'$dbname', dbuser=>'$dbuser'"; |
| 74 |
if($dbpass) { |
| 75 |
$args .= ", dbpass=>'$dbpass'"; |
| 76 |
} |
| 77 |
if($dbhost) { |
| 78 |
$args .= ", dbhost=>'$dbhost'"; |
| 79 |
} |
| 80 |
if($dbport) { |
| 81 |
$args .= ", dbport=>'$dbport'"; |
| 82 |
} |
| 83 |
eval "\$store = $class->new($args);"; |
| 84 |
|
| 85 |
# Create a Wiki instance |
| 86 |
my $wiki = Wiki::Toolkit->new(store=>$store); |
| 87 |
|
| 88 |
# Grab the state as of then |
| 89 |
if($time) { |
| 90 |
$date .= " ".$time; |
| 91 |
} |
| 92 |
print "Reverting to the state as of $date\n"; |
| 93 |
|
| 94 |
my @nodes = $wiki->list_last_version_before($date); |
| 95 |
foreach my $node (@nodes) { |
| 96 |
my %newnode = $wiki->retrieve_node($node->{name}); |
| 97 |
my $thenver = $node->{version}; |
| 98 |
if($thenver) { |
| 99 |
$thenver = sprintf("v%02d", $thenver); } |
| 100 |
else { |
| 101 |
$thenver = "(d)"; |
| 102 |
} |
| 103 |
|
| 104 |
print sprintf(' %03d - %s (now v%02d) - %s', $node->{id}, $thenver, $newnode{version}, $node->{name})."\n"; |
| 105 |
} |
| 106 |
|
| 107 |
print "\nProceed? (y/n)\n"; |
| 108 |
my $ok = <>; |
| 109 |
chomp $ok; |
| 110 |
unless($ok eq "y") { |
| 111 |
die("Aborting revert\n"); |
| 112 |
} |
| 113 |
|
| 114 |
# Revert each node |
| 115 |
foreach my $node (@nodes) { |
| 116 |
if($node->{version}) { |
| 117 |
# Delete versions between now and then |
| 118 |
my %newnode = $wiki->retrieve_node($node->{name}); |
| 119 |
for (my $ver=$newnode{version}; $ver>$node->{version}; $ver--) { |
| 120 |
$wiki->delete_node( |
| 121 |
name=>$node->{name}, |
| 122 |
version=>$ver, |
| 123 |
wiki=>$wiki |
| 124 |
); |
| 125 |
print sprintf('Deleted node v%02d of %03d - %s',$ver, $node->{id},$node->{name})."\n"; |
| 126 |
} |
| 127 |
} else { |
| 128 |
# No version then, delete |
| 129 |
$wiki->delete_node( |
| 130 |
name=>$node->{name}, |
| 131 |
wiki=>$wiki |
| 132 |
); |
| 133 |
print sprintf('Deleted node %03d - %s',$node->{id},$node->{name})."\n"; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
# All done |
| 138 |
print "\nDone revert to $date\n"; |
| 139 |
|
| 140 |
=head1 NAME |
| 141 |
|
| 142 |
wiki-toolkit-revert-to-date - Revert the state of a Wiki::Toolkit instance to an earlier point in time. |
| 143 |
|
| 144 |
=head1 SYNOPSIS |
| 145 |
|
| 146 |
# Removes any changes made to a Wiki::Toolkit instance since a given date |
| 147 |
# (and optionally time), restoring it to the state at that point. |
| 148 |
|
| 149 |
wiki-toolkit-revert-to-date --type postgres |
| 150 |
--name mywiki \ |
| 151 |
--user wiki \ |
| 152 |
--pass wiki \ |
| 153 |
--host 'db.example.com' \ |
| 154 |
--port 1234 \ |
| 155 |
--date 2007-01-05 \ |
| 156 |
--time 11:23:21 |
| 157 |
|
| 158 |
=head1 DESCRIPTION |
| 159 |
|
| 160 |
Takes three mandatory arguments: |
| 161 |
|
| 162 |
=over 4 |
| 163 |
|
| 164 |
=item type |
| 165 |
|
| 166 |
The database type. Should be one of 'postgres', 'mysql' and 'sqlite'. |
| 167 |
|
| 168 |
=item name |
| 169 |
|
| 170 |
The database name. |
| 171 |
|
| 172 |
=item date |
| 173 |
|
| 174 |
The date to revert the state back to, in the format YYYY-MM-DD |
| 175 |
|
| 176 |
=back |
| 177 |
|
| 178 |
five optional arguments: |
| 179 |
|
| 180 |
=over 4 |
| 181 |
|
| 182 |
=item time |
| 183 |
|
| 184 |
The time (on the specified date) to revert the state back to, in the |
| 185 |
format hh:mm:ss. If not specified, will use midnight. |
| 186 |
|
| 187 |
=item user |
| 188 |
|
| 189 |
The user that connects to the database. It must have permission |
| 190 |
to create and drop tables in the database. |
| 191 |
|
| 192 |
=item pass |
| 193 |
|
| 194 |
The user's database password. |
| 195 |
|
| 196 |
=item host |
| 197 |
|
| 198 |
The hostname of the machine the database server is running on (omit |
| 199 |
for local databases). |
| 200 |
|
| 201 |
=item port |
| 202 |
|
| 203 |
The port number that the database server is expecting connections to. |
| 204 |
|
| 205 |
=back |
| 206 |
|
| 207 |
=head1 AUTHOR |
| 208 |
|
| 209 |
Nick Burch |
| 210 |
|
| 211 |
=head1 COPYRIGHT |
| 212 |
|
| 213 |
Copyright (C) 2006 Nick Burch. All Rights Reserved. |
| 214 |
|
| 215 |
This code is free software; you can redistribute it and/or modify it |
| 216 |
under the same terms as Perl itself. |
| 217 |
|
| 218 |
=head1 SEE ALSO |
| 219 |
|
| 220 |
L<Wiki::Toolkit> |
| 221 |
|
| 222 |
=cut |
| 223 |
|
| 224 |
1; |