Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi.

You need to do GET request first, to get the value of input field wpLoginToken.

my $req; my $response; $req = "$wikiurl?title=Special:UserLogin&returnto=Main+Page"; print "req = GET $req\n"; $response = $ua->request( GET $req, ); # look for for cookie-set request from server, ie. mediawiki_isg_sess +ion=954d4797c18ca9054f14c2675af3255e; path=/; HttpOnly # foreach (keys %{$response->{'_headers'}}) { if ($_ =~ /^set-cookie$/i) { # server attempting to set a cookie my $c = $response->{'_headers'}->{$_}; print "Server header set-cookie = $c\n"; } } my @lines = split /\n/, $response->content(); foreach (@lines) { if (/name="wpLoginToken" value="([0-9a-z]+)"/) { my $token = $1; $params{'wpLoginToken'} = $token; print "found token=$token, line=$_\n"; } } $cookie_jar->extract_cookies( $response );

Then, you need to pass this in your next request when you are actually logging in, when submitting the username and password

$req = $wikiurl."?title=Special:UserLogin&action=submitlogin&type=lo +gin&returnto=Main+Page"; print "req = POST $req\n"; $params{'wpLoginAttempt'} = "Log in"; $response = $ua->request( POST $req, Content_Type => 'application/x-www-form-urlencoded' , Content => [ %params ] ); $loggedIn = 0; foreach (keys %{$response->{'_headers'}}) { # print "ServerHeader: ".$_."\n"; if ($_ =~ /^set-cookie$/i) { # server attempting to set a cookie my $a = $response->{'_headers'}->{$_}; if ($a =~ /^ARRAY(.+)$/) { foreach (@{$a}) { print "Server header set-cookie: ======== $_\n"; if (/UserID=\d+\;/i) { $loggedIn = 1; # Success! last; } } } } } print "Login result = $loggedIn\n";

Now, you will need the get and keep passing the editToken

my $LastEditToken = undef; my $body = $response->content(); if ($body =~ /value="([0-9a-z\+\\]+)"\s+name="editToken"/) { $LastEditToken = $1; print "found token=$LastEditToken\n"; }

Whenever you want to do anything further with the wiki, keep pasing the editToken, eg. for importing XML dumps:

sub importXML { if (not defined $LastEditToken) { my $response = $ua->request( GET "$wikiurl?title=Special:Import", ); if ($response->content() =~ /value="([0-9a-z\+\\]+)"\s+name="editT +oken"/) { $LastEditToken = $1; print "==found token=$LastEditToken\n"; } } my $url = "$wikiurl?title=Special:Import&action=submit"; print "Sending request to $url,\n using token $LastEditToken\n"; my $response = $ua->request( POST "$url", Content_Type => 'multipart/form-data', Content => [ 'action' => 'submit', 'source' => 'upload', 'editToken' => $LastEditToken, 'MAX_FILE_SIZE' => $MaxXmlSize, 'xmlimport' => [$filepath], ] ); if ($response->is_success) { if ($response->content() =~ /value="([0-9a-z\+\\]+)"\s+name="editT +oken"/) { $LastEditToken = $1; print "==found token=$LastEditToken\n"; } return 1; } else { return 0; print $response->content(); } }

In reply to Re: LWP UserAgent to script Mediawiki reads by PikMaster
in thread LWP UserAgent to script Mediawiki reads by Sabalon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-20 03:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found