Class: MediawikiApi::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mediawiki_api/client.rb

Overview

high level client for MediaWiki

Constant Summary collapse

FORMAT =
'json'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, log = false) ⇒ Client

Returns a new instance of Client.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mediawiki_api/client.rb', line 18

def initialize(url, log = false)
  @cookies = HTTP::CookieJar.new

  @conn = Faraday.new(url: url) do |faraday|
    faraday.request :multipart
    faraday.request :url_encoded
    faraday.response :logger if log
    faraday.use :cookie_jar, jar: @cookies
    faraday.adapter Faraday.default_adapter
  end

  @logged_in = false
  @tokens = {}
end

Instance Attribute Details

#cookiesObject (readonly)

Returns the value of attribute cookies.



13
14
15
# File 'lib/mediawiki_api/client.rb', line 13

def cookies
  @cookies
end

#logged_inObject Also known as: logged_in?

Returns the value of attribute logged_in.



14
15
16
# File 'lib/mediawiki_api/client.rb', line 14

def logged_in
  @logged_in
end

Instance Method Details

#action(name, params = {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/mediawiki_api/client.rb', line 33

def action(name, params = {})
  raw_action(name, params)
rescue ApiError => e
  if e.code == 'badtoken'
    @tokens.clear # ensure fresh token on re-try
    raw_action(name, params) # no rescue this time; only re-try once.
  else
    raise # otherwise, propagate the exception
  end
end

#create_account(username, password) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mediawiki_api/client.rb', line 44

def (username, password)
  params = { modules: 'createaccount', token_type: false }
  d = action(:paraminfo, params).data
  params = d['modules'] && d['modules'][0] && d['modules'][0]['parameters']
  if !params || !params.map
    raise CreateAccountError, 'unexpected API response format'
  end
  params = params.map{ |o| o['name'] }

  if params.include? 'requests'
    (username, password)
  else
    (username, password)
  end
end

#create_account_new(username, password) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mediawiki_api/client.rb', line 60

def (username, password)
  # post-AuthManager
  data = action(:query, { meta: 'tokens', type: 'createaccount', token_type: false }).data
  token = data['tokens'] && data['tokens']['createaccounttoken']
  unless token
    raise CreateAccountError, 'failed to get createaccount API token'
  end

  data = action(:createaccount, {
    username: username,
    password: password,
    retype: password,
    createreturnurl: 'http://example.com', # won't be used but must be a valid URL
    createtoken: token,
    token_type: false
  }).data
  raise CreateAccountError, data['message'] if data['status'] != 'PASS'
  data
end

#create_account_old(username, password, token = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/mediawiki_api/client.rb', line 80

def (username, password, token = nil)
  # pre-AuthManager
  params = { name: username, password: password, token_type: false }
  params[:token] = token unless token.nil?

  data = action(:createaccount, params).data

  case data['result']
  when 'Success'
    @logged_in = true
    @tokens.clear
  when 'NeedToken'
    data = (username, password, data['token'])
  else
    raise CreateAccountError, data['result']
  end

  data
end

#create_page(title, content) ⇒ Object



100
101
102
# File 'lib/mediawiki_api/client.rb', line 100

def create_page(title, content)
  edit(title: title, text: content)
end

#delete_page(title, reason) ⇒ Object



104
105
106
# File 'lib/mediawiki_api/client.rb', line 104

def delete_page(title, reason)
  action(:delete, title: title, reason: reason)
end

#edit(params = {}) ⇒ Object

Raises:



108
109
110
111
112
# File 'lib/mediawiki_api/client.rb', line 108

def edit(params = {})
  response = action(:edit, params)
  raise EditError, response if response.data['result'] == 'Failure'
  response
end

#get_wikitext(title) ⇒ Object



114
115
116
# File 'lib/mediawiki_api/client.rb', line 114

def get_wikitext(title)
  @conn.get '/w/index.php', action: 'raw', title: title
end

#list(type, params = {}) ⇒ Object



118
119
120
# File 'lib/mediawiki_api/client.rb', line 118

def list(type, params = {})
  subquery(:list, type, params)
end

#log_in(username, password, token = nil) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/mediawiki_api/client.rb', line 122

def (username, password, token = nil)
  params = { lgname: username, lgpassword: password, token_type: false }
  params[:lgtoken] = token unless token.nil?

  data = action(:login, params).data

  case data['result']
  when 'Success'
    @logged_in = true
    @tokens.clear
  when 'NeedToken'
    raise LoginError, "failed to log in with the returned token '#{token}'" unless token.nil?
    data = (username, password, data['token'])
  else
    raise LoginError, data['result']
  end

  data
end

#meta(type, params = {}) ⇒ Object



142
143
144
# File 'lib/mediawiki_api/client.rb', line 142

def meta(type, params = {})
  subquery(:meta, type, params)
end

#prop(type, params = {}) ⇒ Object



146
147
148
# File 'lib/mediawiki_api/client.rb', line 146

def prop(type, params = {})
  subquery(:prop, type, params)
end

#protect_page(title, reason, protections = 'edit=sysop|move=sysop') ⇒ Object



150
151
152
# File 'lib/mediawiki_api/client.rb', line 150

def protect_page(title, reason, protections = 'edit=sysop|move=sysop')
  action(:protect, title: title, reason: reason, protections: protections)
end

#query(params = {}) ⇒ Object



154
155
156
# File 'lib/mediawiki_api/client.rb', line 154

def query(params = {})
  action(:query, { token_type: false, http_method: :get }.merge(params))
end

#unwatch_page(title) ⇒ Object



158
159
160
# File 'lib/mediawiki_api/client.rb', line 158

def unwatch_page(title)
  action(:watch, token_type: 'watch', titles: title, unwatch: true)
end

#upload_image(filename, path, comment, ignorewarnings) ⇒ Object



162
163
164
165
166
167
# File 'lib/mediawiki_api/client.rb', line 162

def upload_image(filename, path, comment, ignorewarnings)
  file = Faraday::UploadIO.new(path, 'image/png')
  action(:upload,
         filename: filename, file: file, comment: comment,
         ignorewarnings: ignorewarnings)
end

#watch_page(title) ⇒ Object



169
170
171
# File 'lib/mediawiki_api/client.rb', line 169

def watch_page(title)
  action(:watch, token_type: 'watch', titles: title)
end