From Wikipedia, the free encyclopedia
package Wikipedia;
use strict;
use warnings;
our $VERSION = '0.01';
use constant LANGUAGE => 'en';
use constant SERVER => 'http://%s.wikipedia.org';
use constant LOGIN => '/w/api.php?action=login';
use constant LOGIN_POST => 'lgname=%s&lgpassword=%s';
use Wikipedia::Page;
use LWP::UserAgent qw//;
use HTTP::Cookies qw//;
use File::Temp qw/ tempfile tempdir /;
=head1 NAME
Wikipedia - Provides a layer of abstraction for automatic editing of Wikipedia
articles.
=head1 DESCRIPTION
FIXME
=head1 SYNOPSIS
use Wikipedia;
# These are optional. If left undefined, the client is not logged in.
my ($username, $password) = ('My wikipedia user', 'My password');
# The 'user_agent' is an optional L<LWP::UserAgent> object that will
# serve as the user agent.
my $user_agent = LWP::UserAgent->new(...);
# The 'cookie_jar' parameter is an optional L<HTTP::Cookies> object and
# is used if the 'user_agent' parameter is left undefined. Otherwise,
# it is ignored.
my $cookie_jar = HTTP::Cookies->new(...);
# The 'cookie_file' parameter is an optional filename of where you want
# to store your cookies. It is used if the 'cookie_jar' parameter is
# left undefined. Otherwise it is ignored.
my $cookie_file = 'my-cookies.txt';
# In that case that both 'cookie_jar' and 'cookie_file' are undefined,
# a temporary file will be created for cookies using L<File::Temp>.
# The 'server' parameter is to specify the Wikipedia server base. The
# trailing slash is not required, but will not break the program.
my $server = 'http://en.wikipedia.org';
# The 'language' parameter specifies the language code of the Wikipedia
# we will be editing. The default is shown here.
my $language = 'en';
# (this should be enough for most users)
my $wiki = Wikipedia->new(
'username' => $username,
'password' => $password,
);
# Save our cookies as well
my $wiki = Wikipedia->new(
'username' => $username,
'password' => $password,
'cookie_file' => $cookie_file,
);
# Edit Wikipedia in Esperanto
my $wiki = Wikipedia->new(
'username' => $username,
'password' => $password,
'language' => 'eo',
);
=head1 SUBROUTINES
=cut
sub new {
my $class = shift;
my %opts = @_;
my ($username, $password, $user_agent, $server) =
@opts{qw/ username password user_agent server /};
unless (defined $user_agent) {
# no user agent defined; let's create our own
my $cookie_jar = $opts{'cookie_jar'};
unless (defined $cookie_jar) {
# no cookie jar; create one
my $cookie_file = $opts{'cookie_file'};
unless (defined $cookie_file) {
# no cookie file; make a temporary file
$cookie_file = (&tempfile(
'DIR' => &tempdir('CLEANUP' => 1),
))[1];
}
# create the cookie jar
$cookie_jar = HTTP::Cookies->new(
'file' => $cookie_file,
'autosave' => 1,
);
}
# create the user agent
$user_agent = LWP::UserAgent->new(
'cookie_jar' => $cookie_jar,
);
}
unless (defined $server) {
# no server name defined
my $language = $opts{'language'};
$language = LANGUAGE unless defined $language;
$server = sprintf SERVER, $language;
}
# log in with given information
if (defined $user_agent && defined $username && defined $password) {
&_login($user_agent, $username, $password, $server);
}
bless {
'username' => $username,
'password' => $password,
'user_agent' => $user_agent,
'server' => $server,
}, $class
}
=head2 get_page
Creates a new L<Wikipedia::Page> object to edit the given page name.
my $page = $wiki->get_page('Main page');
my $wiki_source = $page->contents;
=cut
sub get_page {
my $self = shift;
my $pagename = shift;
Wikipedia::Page->new(
'pagename' => $pagename,
'user_agent' => $self->{'user_agent'},
'server' => $self->{'server'},
)
}
################
sub _login {
my ($user_agent, $username, $password, $server) = @_;
# post login to login page (should save authentication in cookie jar)
my $login_response = $user_agent->post(
$server.LOGIN,
'Content' => sprintf LOGIN_POST, $username, $password);
die "encountered a transmission error logging in"
unless $login_response->is_success;
# TODO: check to see if the bot ACTUALLY logged in
}
=head1 SEE ALSO
L<Wikipedia::Page>
=head1 AUTHOR
Part of project Tweakbot by Dan Church <amphetamachine@gmail.com>
=cut
1