Permalink
Browse files

Fix depracations with actions requiring a token

  • Loading branch information...
MusikAnimal committed Sep 10, 2016
1 parent 8f4a47d commit 387e3bce5eb88ea1d1d18f835d548ebccdfddc9e
Showing with 37 additions and 32 deletions.
  1. +32 −27 lib/media_wiki/gateway.rb
  2. +1 −1 lib/media_wiki/version.rb
  3. +4 −4 mediawiki-gateway.gemspec
View
@@ -8,6 +8,17 @@ module MediaWiki
class Gateway
USER_AGENT = "#{self}/#{VERSION}"
META_TOKEN_TYPES = %w[
createaccount
csrf
deleteglobalaccount
login
patrol
rollback
setglobalaccountstatus
userrights
watch
]
class << self
@@ -69,12 +80,20 @@ def send_request(form_data, continue_xpath = nil)
# Fetch token (type 'delete', 'edit', 'email', 'import', 'move', 'protect')
def get_token(type, page_titles = nil)
res = send_request(
params = {
'action' => 'query',
'meta' => 'tokens'
)
}
if META_TOKEN_TYPES.include?(type)
params.merge!('type' => type)
end
res = send_request(params)
tokens = res.elements['query/tokens'].attributes
unless token = res.elements['query/tokens'].attributes["csrftoken"]
unless token = tokens['logintoken'] || tokens['csrftoken'] || tokens['token']
raise Unauthorized.new "User is not permitted to perform this operation: #{type}"
end
@@ -132,6 +151,15 @@ def make_api_request(form_data, continue_xpath = nil, retry_count = 1)
form_data.update('format' => 'xml', 'maxlag' => @options[:maxlag])
# some actions require a second request with a token received on the first request
if META_TOKEN_TYPES.include?(action = form_data['action'])
if action == 'login'
form_data['lgtoken'] = get_token('login')
else
form_data['token'] = get_token(action)
end
end
http_send(@wiki_url, form_data, @headers.merge(cookies: @cookies)) do |response|
if response.code == 503
retry_delay = @options[:retry_delay]
@@ -197,30 +225,7 @@ def make_api_request(form_data, continue_xpath = nil, retry_count = 1)
doc = response.doc
log.debug("RES: #{doc}")
# login and createaccount actions require a second request with a token received on the first request
if %w[login createaccount].include?(action = form_data['action'])
action_result = doc.elements[action].attributes['result']
@cookies.update(response.cookies)
case action_result.downcase
when 'success'
return [doc, false]
when 'needtoken'
token = doc.elements[action].attributes['token']
if action == 'login'
return make_api_request(form_data.merge('lgtoken' => token))
elsif action == 'createaccount'
return make_api_request(form_data.merge('token' => token))
end
else
if action == 'login'
raise Unauthorized.new("Login failed: #{action_result}")
elsif action == 'createaccount'
raise Unauthorized.new("Account creation failed: #{action_result}")
end
end
end
@cookies.update(response.cookies)
return [doc, (continue_xpath && doc.elements['query-continue']) ?
REXML::XPath.first(doc, continue_xpath) : nil]
@@ -3,7 +3,7 @@ module MediaWiki
module Version
MAJOR = 1
MINOR = 0
MINOR = 1
PATCH = 0
class << self
@@ -3,22 +3,22 @@
Gem::Specification.new do |s|
s.name = "mediawiki-gateway"
s.version = "1.0.8"
s.version = "1.1.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.require_paths = ["lib"]
s.authors = ["Jani Patokallio", "Jens Wille"]
s.authors = ["Jani Patokallio", "Jens Wille", "MusikAnimal"]
s.date = "2014-10-31"
s.description = "A Ruby framework for MediaWiki API manipulation."
s.email = ["jpatokal@iki.fi", "jens.wille@gmail.com"]
s.email = ["jpatokal@iki.fi", "jens.wille@gmail.com", "musikanimal@gmail.com"]
s.executables = ["mediawiki-gateway"]
s.extra_rdoc_files = ["COPYING", "ChangeLog", "README.md"]
s.files = ["COPYING", "ChangeLog", "README.md", "Rakefile", "bin/mediawiki-gateway", "lib/media_wiki.rb", "lib/media_wiki/exception.rb", "lib/media_wiki/fake_wiki.rb", "lib/media_wiki/gateway.rb", "lib/media_wiki/gateway/files.rb", "lib/media_wiki/gateway/pages.rb", "lib/media_wiki/gateway/query.rb", "lib/media_wiki/gateway/site.rb", "lib/media_wiki/gateway/users.rb", "lib/media_wiki/response.rb", "lib/media_wiki/utils.rb", "lib/media_wiki/version.rb", "lib/mediawiki-gateway.rb", "spec/data/import.xml", "spec/media_wiki/gateway/files_spec.rb", "spec/media_wiki/gateway/pages_spec.rb", "spec/media_wiki/gateway/query_spec.rb", "spec/media_wiki/gateway/site_spec.rb", "spec/media_wiki/gateway/users_spec.rb", "spec/media_wiki/gateway_spec.rb", "spec/media_wiki/live_gateway_spec.rb", "spec/media_wiki/utils_spec.rb", "spec/spec_helper.rb"]
s.homepage = "http://github.com/jpatokal/mediawiki-gateway"
s.licenses = ["MIT"]
# s.post_install_message = "\nmediawiki-gateway-1.0.0 [2014-10-31]:\n\n* <b>Required Ruby version is now 1.9.3 or higher.</b>\n* For better Unicode support, install the +unicode+ or +activesupport+ gem.\n* API methods are grouped into submodules of MediaWiki::Gateway.\n* MediaWiki::Utils has been added as a proper module.\n* MediaWiki::FakeWiki has been added as a first-class citizen.\n* MediaWiki::Config has been removed.\n* MediaWiki::Gateway#send_request allows generic API requests.\n* MediaWiki::Gateway::Query#custom_query has been made public.\n* MediaWiki::Gateway::new learned +user_agent+ option.\n* MediaWiki::Gateway#headers attribute has been exposed.\n* MediaWiki::Gateway#wiki_url attribute has been exposed.\n* Added +mediawiki-gateway+ command-line client.\n* Changed or removed some of the dependencies.\n* Housekeeping and internal refactoring.\n\n"
s.rdoc_options = ["--title", "mediawiki-gateway Application documentation (v1.0.0)", "--charset", "UTF-8", "--line-numbers", "--all", "--main", "README.md"]
s.required_ruby_version = Gem::Requirement.new(">= 1.9.3")
s.required_ruby_version = Gem::Requirement.new(">= 2.0.0")
s.rubygems_version = "2.4.2"
s.summary = "Connect to the MediaWiki API."

0 comments on commit 387e3bc

Please sign in to comment.