2004-10-17 02:19:54 +00:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
#
|
2006-06-01 23:42:26 +00:00
|
|
|
# Copyright (C) 2004, 2005, 2006 Alex Schroeder <alex@emacswiki.org>
|
2004-10-17 02:19:54 +00:00
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
2016-08-16 18:01:26 +02:00
|
|
|
# the Free Software Foundation; either version 3 of the License, or
|
2004-10-17 02:19:54 +00:00
|
|
|
# (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2016-08-16 18:01:26 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2004-10-17 02:19:54 +00:00
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2016-08-16 18:01:26 +02:00
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2004-10-17 02:19:54 +00:00
|
|
|
|
2006-06-01 23:42:26 +00:00
|
|
|
require LWP;
|
2004-10-17 02:19:54 +00:00
|
|
|
use Getopt::Std;
|
|
|
|
|
|
2006-06-01 23:42:26 +00:00
|
|
|
our ($opt_v, $opt_w);
|
|
|
|
|
|
|
|
|
|
# We make our own specialization of LWP::UserAgent that asks for
|
|
|
|
|
# user/password if document is protected.
|
|
|
|
|
{
|
|
|
|
|
package RequestAgent;
|
|
|
|
|
@ISA = qw(LWP::UserAgent);
|
|
|
|
|
|
|
|
|
|
sub new {
|
|
|
|
|
my $self = LWP::UserAgent::new(@_);
|
|
|
|
|
$self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub get_basic_credentials {
|
|
|
|
|
my($self, $realm, $uri) = @_;
|
|
|
|
|
return split(':', $main::opt_w, 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
2005-02-02 23:06:41 +00:00
|
|
|
|
2006-06-01 22:57:30 +00:00
|
|
|
my $usage = qq{$0 [-i URL] [-d STRING] [-t SECONDS]
|
2006-06-01 23:47:38 +00:00
|
|
|
\t[-u USERNAME] [-p PASSWORD] [-w USERNAME:PASSWORD]
|
2013-03-05 23:10:35 +01:00
|
|
|
\t[-q QUESTION] [-a ANSWER] [-z SECRET]
|
2006-06-01 23:47:38 +00:00
|
|
|
\t[SOURCE] TARGET
|
2006-06-01 22:57:30 +00:00
|
|
|
|
|
|
|
|
SOURCE and TARGET are the base URLs for the two wikis. Visiting these
|
|
|
|
|
two URLs should show you the respective homepages.
|
|
|
|
|
|
|
|
|
|
Provide the page names to copy on STDIN or use -i to point to a page.
|
|
|
|
|
You can use the index action with the raw parameter from the source
|
|
|
|
|
wiki to copy all the pages. See example below.
|
|
|
|
|
|
|
|
|
|
The list of page names should use the MIME type text/plain.
|
|
|
|
|
|
|
|
|
|
By default, wikicopy will copy a page every five seconds. Use -t to
|
|
|
|
|
override this. SECONDS is the number of seconds to wait between
|
|
|
|
|
requests.
|
|
|
|
|
|
|
|
|
|
If you use -d instead of providing a SOURCE, all the pages will be
|
|
|
|
|
replaced with STRING. This is useful when replacing the page content
|
|
|
|
|
with "DeletedPage", for example.
|
|
|
|
|
|
2013-03-05 23:10:35 +01:00
|
|
|
-d Delete target pages instead of providing SOURCE (default: none)
|
|
|
|
|
-s The summary for RecentChanges (default: none)
|
|
|
|
|
-u The username for RecentChanges (default: none)
|
|
|
|
|
-p The password to use for locked pages (default: none)
|
|
|
|
|
-w The username:password combo for basic authentication (default:none)
|
|
|
|
|
-q The question number to answer (default: 0, ie. the first question)
|
|
|
|
|
-a The answer to the question (default: none)
|
|
|
|
|
-z Alternatively, the secret key (default: question)
|
|
|
|
|
-v Verbose output for debugging (default: none)
|
2006-06-01 23:47:38 +00:00
|
|
|
|
2006-06-01 22:57:30 +00:00
|
|
|
Examples:
|
|
|
|
|
|
|
|
|
|
wikicopy -i 'http://www.emacswiki.org/cgi-bin/alex?action=index;raw=1' \\
|
|
|
|
|
http://www.emacswiki.org/cgi-bin/alex \\
|
|
|
|
|
http://localhost/cgi-bin/wiki.pl
|
|
|
|
|
|
2006-06-01 23:47:38 +00:00
|
|
|
wikicopy -d DeletedPage http://localhost/cgi-bin/wiki.pl < list.txt
|
|
|
|
|
|
|
|
|
|
wikicopy -v -u 'ElGordo' -w 'simple:mind' \\
|
|
|
|
|
-i 'http://www.communitywiki.org/odd/LosAngelesEcoVillage?action=index;raw=1' \\
|
|
|
|
|
'http://www.communitywiki.org/odd/LosAngelesEcoVillage' \\
|
|
|
|
|
'http://www.tentacle.net/~eeio/cgi/wiki.cgi'
|
2006-06-01 22:57:30 +00:00
|
|
|
};
|
2004-10-17 02:19:54 +00:00
|
|
|
|
|
|
|
|
sub UrlEncode {
|
|
|
|
|
my $str = shift;
|
|
|
|
|
return '' unless $str;
|
|
|
|
|
my @letters = split(//, $str);
|
|
|
|
|
my @safe = ('a' .. 'z', 'A' .. 'Z', '0' .. '9', '-', '_', '.', '!', '~', '*', "'", '(', ')', '#');
|
|
|
|
|
foreach my $letter (@letters) {
|
|
|
|
|
my $pattern = quotemeta($letter);
|
|
|
|
|
if (not grep(/$pattern/, @safe)) {
|
|
|
|
|
$letter = sprintf("%%%02x", ord($letter));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return join('', @letters);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub GetRaw {
|
2004-10-17 02:25:02 +00:00
|
|
|
my ($uri) = @_;
|
2006-06-01 23:42:26 +00:00
|
|
|
my $ua = RequestAgent->new;
|
2004-10-17 02:25:02 +00:00
|
|
|
my $response = $ua->get($uri);
|
2005-02-02 23:06:41 +00:00
|
|
|
print "no response\n" unless $response->code;
|
|
|
|
|
print "GET ", $response->code, " ", $response->message, "\n" if $opt_v;
|
2004-10-17 02:19:54 +00:00
|
|
|
return $response->content if $response->is_success;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-05 23:10:35 +01:00
|
|
|
sub post {
|
|
|
|
|
my ($uri, $id, $data, $minor, $summary, $username, $password,
|
|
|
|
|
$question, $answer, $secret) = @_;
|
2006-06-01 23:42:26 +00:00
|
|
|
my $ua = RequestAgent->new;
|
2013-03-05 23:10:35 +01:00
|
|
|
my %params = (title=>$id, text=>$data, raw=>1,
|
|
|
|
|
username=>$username, pwd=>$password,
|
|
|
|
|
summary=>$summary, question_num=>$question,
|
|
|
|
|
answer=>$answer, $secret=>1,
|
|
|
|
|
recent_edit=>$minor);
|
|
|
|
|
if ($opt_v) {
|
|
|
|
|
foreach my $key (keys %params) {
|
|
|
|
|
my $value = $params{$key} || '(none)';
|
|
|
|
|
$value = substr($value,0,50) . '...'
|
|
|
|
|
if $key eq 'text' and length($value) > 53;
|
|
|
|
|
warn "$key: $value\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
my $response = $ua->post($uri, \%params);
|
2005-02-02 00:08:18 +00:00
|
|
|
my $status = $response->code . ' ' . $response->message;
|
|
|
|
|
warn "POST $id failed: $status.\n" unless $response->is_success;
|
2004-10-17 02:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub copy {
|
2013-03-05 23:10:35 +01:00
|
|
|
my ($source, $replacement, $target, $interval, $minor, $summary,
|
|
|
|
|
$username, $password, $question, $answer, $secret,
|
2006-06-01 22:57:30 +00:00
|
|
|
@pages) = @_;
|
2005-02-02 00:08:18 +00:00
|
|
|
foreach my $id (@pages) {
|
|
|
|
|
print "$id\n";
|
|
|
|
|
my $page = UrlEncode ($id);
|
2005-02-02 23:06:41 +00:00
|
|
|
# fix URL for other wikis
|
|
|
|
|
my $data = $replacement || GetRaw("$source?action=browse;id=$page;raw=1");
|
2005-02-02 00:08:18 +00:00
|
|
|
next unless $data;
|
2013-03-05 23:10:35 +01:00
|
|
|
post($target, $id, $data, $minor, $summary, $username, $password,
|
|
|
|
|
$question, $answer, $secret);
|
2004-10-17 02:19:54 +00:00
|
|
|
sleep($interval);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub main {
|
2013-03-05 23:10:35 +01:00
|
|
|
our($opt_m, $opt_i, $opt_t, $opt_d, $opt_s, $opt_u, $opt_p,
|
|
|
|
|
$opt_q, $opt_a, $opt_z);
|
|
|
|
|
getopts('mi:t:d:s:u:p:q:a:z:w:v');
|
2004-10-17 02:19:54 +00:00
|
|
|
my $interval = $opt_t ? $opt_t : 5;
|
2005-02-02 23:06:41 +00:00
|
|
|
my $replacement = $opt_d;
|
|
|
|
|
my ($source, $target);
|
|
|
|
|
$source = shift(@ARGV) unless $replacement;
|
|
|
|
|
$target = shift(@ARGV);
|
|
|
|
|
die $usage if not $target or @ARGV; # not enough or too many
|
2004-10-17 02:19:54 +00:00
|
|
|
my @pages = ();
|
|
|
|
|
if ($opt_i) {
|
|
|
|
|
my $data = GetRaw($opt_i);
|
|
|
|
|
@pages = split(/\n/, $data);
|
|
|
|
|
} else {
|
|
|
|
|
print "List of pages:\n";
|
|
|
|
|
while (<STDIN>) {
|
2005-02-02 23:06:41 +00:00
|
|
|
chomp;
|
2004-10-17 02:19:54 +00:00
|
|
|
push(@pages, $_);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
die "The list of pages is missing. Did you use -i?\n" unless @pages;
|
2013-03-05 23:10:35 +01:00
|
|
|
copy($source, $replacement, $target, $interval, $opt_m ? 'on' : '', $opt_s,
|
|
|
|
|
$opt_u, $opt_p, $opt_q, $opt_a, $opt_z||'question',
|
2006-06-01 22:57:30 +00:00
|
|
|
@pages);
|
2004-10-17 02:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main();
|