Subject: | Error handling fixes |
Attached a patch to fix a number or error handling issues where I ran into
while trying the script on a custom mediawiki install.
Issue 1 was that the error message one gets when he forgets to prefix
the hostname with "http://" is misleading the messages generated were:
Use of uninitialized value in pattern match (m//) at /usr/lib/perl5/vendor_perl/5.8.5/HTTP/Cookies.pm line 45.
Saving cookie jar.
The cause is that Cookies.pm looks at the URI's scheme and without the http:// prefix, URI->scheme is uninitilized. Other functions require it
too. Since Cookies.pm does not support https://, http:// could be automatically prepended, but for now, I just changed the error reporting:
Use of uninitialized value in pattern match (m//) at /usr/lib/perl5/vendor_perl/5.8.5/HTTP/Cookies.pm line 45.
Login to bernhard.a11.private/mw/wiki.phtml?action=submit&title=Special:Userlogin failed
Error code: 400 URL must be absolute
Tip: If the hostname didn't start with 'http://' add it.
Issue 2: If a different problem prevented login (wrong user name, login path or password), no indication of an error was given, with the patch this error is given:
Logging in as WikiSysop
Login did not work, please check host, login path, user and password.
If another error, eg. one caused a mispelled URI scheme in the hostname occurs, then just the error message from the perl libary is reported to the user:
Login to htp://bernhard.a11.private/mw/wiki.phtml?action=submit&title=Special:Userlogin failed
Error code: 501 Protocol scheme 'htp' is not supported
Issue 3: Just beautified an error message:
0.23:
Couldn't fetch Main Page.wiki from the server.HTTP get failed with: 501 at /usr/bin/mvs line 83
with patch:
Couldn't fetch "Main Page.wiki" from the server.
HTTP get failed with: 501 Protocol scheme 'htp' is not supported at /usr/bin/mvs line 83
Detail about Issue 1-3:
In all the above cases, the login info was written, despite the
login was not successful, the patch fixes this too, (the info is
not useful without the login cookie).
Issue 4: If the server requires login for editing and a working .mediawiki file, but a failed login (no cookie) in .mediawiki_cookies.dat, the mvs update message looked like this:
mvs up 'Main Page.wiki'
Updating: Main Page.wiki
Fetching http://bernhard.a11.private/mw/wiki.phtml?action=edit&title=Main Page
Loading Main Page.wiki
Loading ./.Main Page.ref.wiki
A Main Page.wiki
So there was no error message, just the "A" flag looks strange because
the page exists on the server, but it could not be retrieved since
there is no login cookie.
With the patch, the messages look like this:
mvs up 'Main Page.wiki'
Updating: Main Page.wiki
Fetching http://bernhard.a11.private/mw/wiki.phtml?action=edit&title=Main Page
Error message from the server: Login required to edit
If the error message from the wikipedia server cannot be parsed,
the content of the server's answer is shown and the last message is:
Unknown error, the above is I got from the server.
I also did a update of the docmentation in the mvs script.
I also did a spelling fix, copyright notice expansion to 2004-2005
and added me as contributor, but these are only hints, I don't require
to be listed, I just added it for completeness.
--- lib/WWW/Mediawiki/Client.pm 2005/03/19 03:43:37 1.1
+++ lib/WWW/Mediawiki/Client.pm 2005/03/19 05:20:22
@@ -345,7 +345,7 @@
croak "Must have username and password to login."
unless $self->{username} && $self->{password};
print { $self->{info_fh} }
- "Loging in as " . $self->{username} . "\n";
+ "Logging in as " . $self->{username} . "\n";
my $url = $self->{site_url} . '/' . $self->{login_path};
my $username_tag = USERNAME_NAME;
my $password_tag = PASSWORD_NAME;
@@ -360,6 +360,19 @@
$submit_tag => $submitval,
]
);
+
+ if ($res->is_success) {
+ print STDERR "Login did not work, please check host, login path, user and password.\n";
+ return $self;
+ }
+ if ($res->code != 302) {
+ print STDERR "Login to ", $url, " failed\n";
+ print STDERR "Error code: ", $res->status_line, "\n";
+ if ($res->code == 400) {
+ print STDERR "Tip: If the hostname didn't start with 'http://' add it.\n";
+ }
+ return $self;
+ }
print { $self->{debug_fh} } "Saving cookie jar.\n";
$self->{ua}->cookie_jar->save
or croak "Could not save cookie jar.";
@@ -618,20 +631,27 @@
my $url = $self->_filename_to_edit_url($filename);
print { $self->{debug_fh} }"Fetching $url\n";
my $res = $self->{ua}->get($url);
- croak "Couldn't fetch $filename from the server."
- . "HTTP get failed with: " . $res->code
+ croak "Couldn't fetch \"$filename\" from the server."
+ . "\nHTTP get failed with: " . $res->status_line
unless $res->is_success;
my $doc = $res->content;
- my $text = $self->_get_wiki_text($doc);
+ my $text = $self->_get_wiki_tag($doc, "textarea");
$self->{server_date} = $self->_get_edit_date($doc);
$self->{server_token} = $self->_get_edit_token($doc);
+ if (!$self->{server_date}) {
+ my $headline1 = $self->_get_wiki_tag($doc, "h1");
+ die "Error message from the server: ", $headline1, "\n"
+ if ($headline1);
+ die "Could not identify the error, this is what I got:\n" . $res->content
+ . "Unknown error, the above is I got from the server.\n";
+ }
return $text;
}
-sub _get_wiki_text {
- my ($self, $doc) = @_;
+sub _get_wiki_tag {
+ my ($self, $doc, $tag) = @_;
my $p = HTML::TokeParser->new(\$doc);
- $p->get_tag("textarea");
+ $p->get_tag($tag);
my $text = $p->get_text;
$text =~ s/
//gs; # convert endlines
return $text;
--- bin/mvs 2005/03/19 03:31:33 1.1
+++ bin/mvs 2005/03/19 03:55:17
@@ -112,11 +112,17 @@
mvs [-q|-v] [-d <wikihost>] [-u <username>] [-p <password> ] login
+ If your mediawiki install uses a nonstandard login path, speciy
+ it on login:
+ -l 'mw/wiki.phtml?action=submit&title=Special:Userlogin'
+ You can change the edit and action paths in the created .mediawiki
+ file after successful login accordingly.
+
mvs [-q|-v] update [<file> ..]
mvs [-q|-v] up [<file> ..]
- mvs [-q|-v] commit [<file> ..]
- mvs [-q|-v] com [<file> ..]
+ mvs [-q|-v] -m "commit message" commit [<file> ..]
+ mvs [-q|-v] -m "commit message" com [<file> ..]
=head1 DESCRIPTION
@@ -397,10 +403,11 @@
=head1 AUTHORS
Mark Jaroski <mark@geekhive.net>
+Minor contributions (error handing) from Bernhard Kaindl <bkaindl@ffii.org>
=head1 COPYRIGHT
-© Copyright 2004, Mark Jaroski
+© Copyright 2004-2005, Mark Jaroski
All rights reserved. This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.