Permalink
Browse files

Fix login with newer MediaWiki using wpLoginToken

git-svn-id: svn://78.47.249.61/ruby-mediawiki/trunk@79 ba9c31aa-a806-0410-9a81-9f13d15ee83b
  • Loading branch information...
1 parent 813a3dd commit f8894b9183ab05a18151fb32843e98d1c9e25ea4 neingeist committed May 20, 2010
Showing with 39 additions and 2 deletions.
  1. +29 −2 lib/mediawiki.rb
  2. +10 −0 lib/mediawiki/minibrowser.rb
View
@@ -20,6 +20,12 @@
require 'uri'
require 'logger'
+begin
+ require 'htree'
+rescue LoadError
+ MediaWiki::logger.warn("htree library missing. Cannot sanitize HTML.")
+ require 'rexml/document'
+end
# Logger is required by article.rb
module MediaWiki
@@ -91,9 +97,20 @@ def initialize(url, user = nil, password = nil, loglevel = Logger::WARN)
#
# May raise an exception if cannot authenticate
def login( username, password )
- data = {'wpName' => username, 'wpPassword' => password, 'wpLoginattempt' => 'Log in'}
+ # Get wpLoginToken
+ data = @browser.get_content( @url.path + 'index.php?title=Special:UserLogin' )
+ doc = to_rexml( data )
+ wp_logintoken = nil
+ if form = doc.elements['//form[@name="userlogin"]']
+ form.each_element('input') { |e|
+ wp_logintoken = e.attributes['value'] if e.attributes['name'] == 'wpLoginToken'
+ }
+ end
+ MediaWiki::logger.debug("Logging in with wpLoginToken=#{wp_logintoken}")
+
+ data = {'wpName' => username, 'wpPassword' => password, 'wpLoginattempt' => 'Log in', 'wpRemember' => 1, 'wpLoginToken' => wp_logintoken}
data = @browser.post_content( @url.path + 'index.php?title=Special:Userlogin&action=submitlogin', data )
- if data =~ /<p class='error'>/
+ if data =~ /<p class='error'>/ or data =~ /<div class="errorbox">/
raise "Unable to authenticate as #{username}"
end
end
@@ -163,6 +180,16 @@ def full_article_url(name, section=nil)
uri.path, uri.query = article_url(name, section).split(/\?/, 2)
uri.to_s
end
+
+ protected
+ def to_rexml( html )
+ if Class.constants.member?( 'HTree' )
+ rexml = HTree( html ).to_rexml
+ else
+ rexml = REXML::Document.new( html )
+ end
+ rexml.root
+ end
end
@@ -90,8 +90,18 @@ def get_content(url)
case response
when Net::HTTPSuccess, Net::HTTPNotFound then
+ begin
+ add_cookie( response.get_fields('Set-Cookie') ) if response['Set-Cookie']
+ rescue NoMethodError
+ add_cookie( response['Set-Cookie'] ) if response['Set-Cookie']
+ end
return response.body
when Net::HTTPRedirection then
+ begin
+ add_cookie( response.get_fields('Set-Cookie') ) if response['Set-Cookie']
+ rescue NoMethodError
+ add_cookie( response['Set-Cookie'] ) if response['Set-Cookie']
+ end
MediaWiki::logger.debug("Redirecting to #{response['Location']}")
retries -= 1
url = response['Location']

0 comments on commit f8894b9

Please sign in to comment.