mediawiki-butt 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,122 +4,124 @@ module MediaWiki
4
4
 
5
5
  class NeedTokenMoreThanOnceError < AuthenticationError
6
6
  def message
7
- "You tried to get the token more than once. You likely have some problem with your login call."
7
+ 'You tried to get the token more than once. You likely have some' \
8
+ 'problem with your login call.'
8
9
  end
9
10
  end
10
11
 
11
12
  class NoNameError < AuthenticationError
12
13
  def message
13
- "You did not set the lgname parameter."
14
+ 'You did not set the lgname parameter.'
14
15
  end
15
16
  end
16
17
 
17
18
  class IllegalUsernameError < AuthenticationError
18
19
  def message
19
- "You provided an illegal username."
20
+ 'You provided an illegal username.'
20
21
  end
21
22
  end
22
23
 
23
24
  class UsernameNotExistsError < AuthenticationError
24
25
  def message
25
- "You provided a username that does not exist."
26
+ 'You provided a username that does not exist.'
26
27
  end
27
28
  end
28
29
 
29
30
  class EmptyPassError < AuthenticationError
30
31
  def message
31
- "You did not set the lgpassword paremeter."
32
+ 'You did not set the lgpassword paremeter.'
32
33
  end
33
34
  end
34
35
 
35
36
  class WrongPassError < AuthenticationError
36
37
  def message
37
- "The password you provided is not correct."
38
+ 'The password you provided is not correct.'
38
39
  end
39
40
  end
40
41
 
41
42
  class WrongPluginPassError < AuthenticationError
42
43
  def message
43
- "A plugin (not MediaWiki) claims your password is not correct."
44
+ 'A plugin (not MediaWiki) claims your password is not correct.'
44
45
  end
45
46
  end
46
47
 
47
48
  class CreateBlockedError < AuthenticationError
48
49
  def message
49
- "MediaWiki tried to automatically create an account for you, but your IP is blocked from account creation."
50
+ 'MediaWiki tried to automatically create an account for you, but your' \
51
+ 'IP is blocked from account creation.'
50
52
  end
51
53
  end
52
54
 
53
55
  class ThrottledError < AuthenticationError
54
56
  def message
55
- "You've logged in too many times."
57
+ 'You\'ve logged in too many times.'
56
58
  end
57
59
  end
58
60
 
59
61
  class BlockedError < AuthenticationError
60
62
  def message
61
- "User is blocked."
63
+ 'User is blocked.'
62
64
  end
63
65
  end
64
66
 
65
67
  # Start creation-specific errors
66
68
  class UserExistsError < AuthenticationError
67
69
  def message
68
- "Username entered is already in use."
70
+ 'Username entered is already in use.'
69
71
  end
70
72
  end
71
73
 
72
74
  class UserPassMatchError < AuthenticationError
73
75
  def message
74
- "Your password must be different from your username."
76
+ 'Your password must be different from your username.'
75
77
  end
76
78
  end
77
79
 
78
80
  class PasswordLoginForbiddenError < AuthenticationError
79
81
  def message
80
- "The use of this username and password has been forbidden."
82
+ 'The use of this username and password has been forbidden.'
81
83
  end
82
84
  end
83
85
 
84
86
  class NoEmailTitleError < AuthenticationError
85
87
  def message
86
- "No email address."
88
+ 'No email address.'
87
89
  end
88
90
  end
89
91
 
90
92
  class InvalidEmailAddressError < AuthenticationError
91
93
  def message
92
- "The email address is invalid."
94
+ 'The email address is invalid.'
93
95
  end
94
96
  end
95
97
 
96
98
  class PasswordTooShortError < AuthenticationError
97
99
  def message
98
- "The password was shorter than the value of $wgMinimalPasswordLength"
100
+ 'The password was shorter than the value of $wgMinimalPasswordLength'
99
101
  end
100
102
  end
101
103
 
102
104
  class NoEmailError < AuthenticationError
103
105
  def message
104
- "There is no email address recorded for the user."
106
+ 'There is no email address recorded for the user.'
105
107
  end
106
108
  end
107
109
 
108
110
  class AbortedError < AuthenticationError
109
111
  def message
110
- "Aborted by an extension."
112
+ 'Aborted by an extension.'
111
113
  end
112
114
  end
113
115
 
114
116
  class PermDeniedError < AuthenticationError
115
117
  def message
116
- "You do not have the right to make an account."
118
+ 'You do not have the right to make an account.'
117
119
  end
118
120
  end
119
121
 
120
122
  class HookAbortedError < AuthenticationError
121
123
  def message
122
- "An extension aborted the account creation."
124
+ 'An extension aborted the account creation.'
123
125
  end
124
126
  end
125
127
  end
@@ -3,42 +3,39 @@ require_relative 'constants'
3
3
 
4
4
  module MediaWiki
5
5
  module Query
6
- #TODO: Actually decide on a good way to deal with meta information queries.
7
- # The metainformation could probably be handled in a much less verbose way.
8
- # Perhaps we should get hashes instead?
6
+ # TODO: Actually decide on a good way to deal with meta information queries.
7
+ # The metainformation could probably be handled in a much less verbose
8
+ # way. Perhaps we should get hashes instead?
9
9
  module Meta
10
-
11
10
  # Returns an array of all the wiki's file repository names.
12
11
  # @return [Array] All wiki's file repository names.
13
12
  def get_filerepo_names
14
13
  params = {
15
14
  action: 'query',
16
15
  meta: 'filerepoinfo',
17
- friprop: 'name',
18
- format: 'json'
16
+ friprop: 'name'
19
17
  }
20
18
 
21
19
  result = post(params)
22
- ret = Array.new
23
- result["query"]["repos"].each do |repo|
24
- ret.push(repo["name"])
25
- end
26
- return ret
20
+ ret = []
21
+ result['query']['repos'].each { |repo| ret.push(repo['name']) }
22
+
23
+ ret
27
24
  end
28
25
 
29
26
  # Gets meta information for the currently logged in user.
30
27
  # @param prop [String] The uiprop to get.
31
- # @return [Response/Boolean] Either a full, parsed response, or false if not logged in.
28
+ # @return [Response/Boolean] Either a full, parsed response, or false if
29
+ # not logged in.
32
30
  def get_current_user_meta(prop)
33
31
  if @logged_in
34
32
  params = {
35
33
  action: 'query',
36
34
  meta: 'userinfo',
37
- uiprop: prop,
38
- format: 'json'
35
+ uiprop: prop
39
36
  }
40
37
 
41
- return response = post(params)
38
+ return post(params)
42
39
  else
43
40
  return false
44
41
  end
@@ -46,8 +43,8 @@ module MediaWiki
46
43
  end
47
44
 
48
45
  module Properties
49
-
50
- # Gets the wiki text for the given page. Returns nil if it for some reason cannot get the text, for example, if the page does not exist. Returns a string.
46
+ # Gets the wiki text for the given page. Returns nil if it for some
47
+ # reason cannot get the text, for example, if the page does not exist.
51
48
  # @param title [String] The page title
52
49
  # @return [String/nil] String containing page contents, or nil
53
50
  def get_text(title)
@@ -55,19 +52,17 @@ module MediaWiki
55
52
  action: 'query',
56
53
  prop: 'revisions',
57
54
  rvprop: 'content',
58
- format: 'json',
59
55
  titles: title
60
56
  }
61
57
 
62
58
  response = post(params)
63
- response["query"]["pages"].each do |revid, data|
64
- $revid = revid
65
- end
59
+ revid = nil
60
+ response['query']['pages'].each { |r, _| revid = r }
66
61
 
67
- if response["query"]["pages"][$revid]["missing"] == ""
62
+ if response['query']['pages'][revid]['missing'] == ''
68
63
  return nil
69
64
  else
70
- return response["query"]["pages"][$revid]["revisions"][0]["*"]
65
+ return response['query']['pages'][revid]['revisions'][0]['*']
71
66
  end
72
67
  end
73
68
 
@@ -79,13 +74,12 @@ module MediaWiki
79
74
  action: 'query',
80
75
  prop: 'revisions',
81
76
  rvprop: 'content',
82
- format: 'json',
83
77
  titles: title
84
78
  }
85
79
 
86
80
  response = post(params)
87
- response["query"]["pages"].each do |revid, data|
88
- if revid != "-1"
81
+ response['query']['pages'].each do |revid, _|
82
+ if revid != '-1'
89
83
  return revid.to_i
90
84
  else
91
85
  return nil
@@ -93,28 +87,35 @@ module MediaWiki
93
87
  end
94
88
  end
95
89
 
96
- # Gets the edit token for the given page. This method should rarely be used by normal users.
97
- # @param page_name [String] The page title that you are going to be editing.
98
- # @return [String] The edit token. If the butt isn't logged in, it returns with '+\\'.
99
- def get_edit_token(page_name)
90
+ # Gets the token for the given type. This method should rarely be
91
+ # used by normal users.
92
+ # @param type [String] The type of token.
93
+ # @param title [String] The page title for the token. Optional.
94
+ # @return [String] The token. If the butt isn't logged in, it returns
95
+ # with '+\\'.
96
+ def get_token(type, title = nil)
100
97
  if @logged_in == true
98
+ # There is some weird thing with MediaWiki where you must pass a valid
99
+ # inprop parameter in order to get any response at all. This is why
100
+ # there is a displaytitle inprop as well as gibberish in the titles
101
+ # parameter. And to avoid normalization, it's capitalized.
101
102
  params = {
102
103
  action: 'query',
103
104
  prop: 'info',
104
- intoken: 'edit',
105
- format: 'json',
106
- titles: page_name
105
+ inprop: 'displaytitle',
106
+ intoken: type
107
107
  }
108
108
 
109
+ title = 'Somegibberish' if title.nil?
110
+ params[:titles] = title
109
111
  response = post(params)
110
- response["query"]["pages"].each do |revid, data|
111
- $revid = revid
112
- end
112
+ revid = nil
113
+ response['query']['pages'].each { |r, _| revid = r }
113
114
 
114
115
  # URL encoding is not needed for some reason.
115
- return response["query"]["pages"][$revid]["edittoken"]
116
+ return response['query']['pages'][revid]["#{type}token"]
116
117
  else
117
- return "+\\"
118
+ return '+\\'
118
119
  end
119
120
  end
120
121
  end
@@ -124,13 +125,14 @@ module MediaWiki
124
125
 
125
126
  # Gets an array of backlinks to a given title.
126
127
  # @param title [String] The page to get the backlinks of.
127
- # @param limit [Int] The maximum number of pages to get. Defaults to 500, and cannot be greater than that unless the user is a bot. If the user is a bot, the limit cannot be greater than 5000.
128
+ # @param limit [Int] The maximum number of pages to get. Defaults to 500,
129
+ # and cannot be greater than that unless the user is a bot. If the user
130
+ # is a bot, the limit cannot be greater than 5000.
128
131
  # @return [Array] All backlinks until the limit
129
132
  def what_links_here(title, limit = 500)
130
133
  params = {
131
134
  action: 'query',
132
- bltitle: title,
133
- format: 'json'
135
+ bltitle: title
134
136
  }
135
137
 
136
138
  if limit > 500
@@ -147,24 +149,26 @@ module MediaWiki
147
149
  params[:bllimit] = limit
148
150
  end
149
151
 
150
- ret = Array.new
152
+ ret = []
151
153
  response = post(params)
152
- response["query"]["backlinks"].each do |bl|
153
- ret.push(bl["title"])
154
- end
155
- return ret
154
+ response['query']['backlinks'].each { |bl| ret.push(bl['title']) }
155
+
156
+ ret
156
157
  end
157
158
 
158
159
  # Returns an array of all page titles that belong to a given category.
159
- # @param category [String] The category title. It can include "Category:", or not, it doesn't really matter because we will add it if it is missing.
160
- # @param limit [Int] The maximum number of members to get. Defaults to 500, and cannot be greater than that unless the user is a bot. If the user is a bot, the limit cannot be greater than 5000.
160
+ # @param category [String] The category title. It can include "Category:",
161
+ # or not, it doesn't really matter because we will add it if it is
162
+ # missing.
163
+ # @param limit [Int] The maximum number of members to get. Defaults to
164
+ # 500, and cannot be greater than that unless the user is a bot.
165
+ # If the user is a bot, the limit cannot be greater than 5000.
161
166
  # @return [Array] All category members until the limit
162
167
  def get_category_members(category, limit = 500)
163
168
  params = {
164
169
  action: 'query',
165
170
  list: 'categorymembers',
166
- cmprop: 'title',
167
- format: 'json'
171
+ cmprop: 'title'
168
172
  }
169
173
 
170
174
  if category =~ /[Cc]ategory\:/
@@ -187,26 +191,27 @@ module MediaWiki
187
191
  params[:cmlimit] = limit
188
192
  end
189
193
 
190
- ret = Array.new
194
+ ret = []
191
195
  response = post(params)
192
- response["query"]["categorymembers"].each do |cm|
193
- ret.push(cm["title"])
194
- end
196
+ response['query']['categorymembers'].each { |cm| ret.push(cm['title']) }
197
+
195
198
  ret
196
199
  end
197
200
 
198
201
  # Returns an array of random pages titles.
199
- # @param number_of_pages [Int] The number of articles to get. Defaults to 1. Cannot be greater than 10 for normal users, or 20 for bots.
200
- # @param namespace [Int] The namespace ID. Defaults to '0' (the main namespace). Set to nil for all namespaces.
202
+ # @param number_of_pages [Int] The number of articles to get.
203
+ # Defaults to 1. Cannot be greater than 10 for normal users,
204
+ # or 20 for bots.
205
+ # @param namespace [Int] The namespace ID. Defaults to
206
+ # 0 (the main namespace).
201
207
  # @return [Array] All members
202
208
  def get_random_pages(number_of_pages = 1, namespace = 0)
203
209
  params = {
204
210
  action: 'query',
205
- list: 'random',
206
- format: 'json'
211
+ list: 'random'
207
212
  }
208
213
 
209
- if $namespaces.has_value?(namespace)
214
+ if $namespaces.value?(namespace)
210
215
  params[:rnnamespace] = namespace
211
216
  else
212
217
  params[:rnnamespace] = 0
@@ -226,18 +231,20 @@ module MediaWiki
226
231
  params[:rnlimit] = number_of_pages
227
232
  end
228
233
 
229
- ret = Array.new
234
+ ret = []
230
235
  responce = post(params)
231
- responce["query"]["random"].each do |a|
232
- ret.push(a["title"])
233
- end
234
- return ret
236
+ responce['query']['random'].each { |a| ret.push(a['title']) }
237
+
238
+ ret
235
239
  end
236
240
 
237
- # Gets user information. This method should rarely be used by normal users.
241
+ # Gets user information. This method should rarely be used by
242
+ # normal users.
238
243
  # @param prop [String] The usprop parameter.
239
- # @param username [String] The username to get info for. Optional. Defaults to the currently logged in user if ommitted.
240
- # @return [String/Nil] Parsed full response if successful, nil if the username is nil and the Butt is not logged in.
244
+ # @param username [String] The username to get info for. Optional.
245
+ # Defaults to the currently logged in user if ommitted.
246
+ # @return [String/Nil] Parsed full response if successful, nil if
247
+ # the username is nil and the Butt is not logged in.
241
248
  def get_userlists(prop, username = nil)
242
249
  if username.nil?
243
250
  if @logged_in
@@ -250,97 +257,99 @@ module MediaWiki
250
257
  action: 'query',
251
258
  list: 'users',
252
259
  usprop: prop,
253
- ususers: username,
254
- format: 'json'
260
+ ususers: username
255
261
  }
256
262
 
257
263
  response = post(params)
258
264
  end
259
265
 
260
- return response
266
+ response
261
267
  end
262
268
 
263
269
  # Gets an array of all the user's groups.
264
- # @param username [String] The username to get groups of. Optional. Defaults to the currently logged in user.
265
- # @return [Array/Boolean] All of the user's groups, or false if username is nil and Butt is not logged in.
270
+ # @param username [String] The username to get groups of. Optional.
271
+ # Defaults to the currently logged in user.
272
+ # @return [Array/Boolean] All of the user's groups, or false if username
273
+ # is nil and Butt is not logged in.
266
274
  def get_usergroups(username = nil)
267
- ret = Array.new
275
+ ret = []
268
276
  if username.nil?
269
277
  if @logged_in
270
278
  info = get_userlists('groups')
271
- info["query"]["userinfo"]["groups"].each do |i|
272
- ret.push(i)
273
- end
279
+ info['query']['userinfo']['groups'].each { |i| ret.push(i) }
274
280
  else
275
281
  return false
276
282
  end
277
283
  else
278
284
  info = get_userlists('groups', username)
279
- info["query"]["users"].each do |i|
280
- i["groups"].each do |g|
281
- ret.push(g)
282
- end
285
+ info['query']['users'].each do |i|
286
+ i['groups'].each { |g| ret.push(g) }
283
287
  end
284
288
  end
285
289
 
286
- return ret
290
+ ret
287
291
  end
288
292
 
289
293
  # Gets the user rights for the user.
290
- # @param username [String] The user to get the rights for. Optional. Defaults to the currently logged in user.
291
- # @return [Array/Boolean] All of the user's groups, or false if username is nil and Butt is not logged in.
294
+ # @param username [String] The user to get the rights for. Optional.
295
+ # Defaults to the currently logged in user.
296
+ # @return [Array/Boolean] All of the user's groups, or false if username
297
+ # is nil and Butt is not logged in.
292
298
  def get_userrights(username = nil)
293
- ret = Array.new
299
+ ret = []
294
300
  if username.nil?
295
301
  if @logged_in
296
302
  info = get_userlists('rights')
297
- info["query"]["userinfo"]["rights"].each do |i|
298
- ret.push(i)
299
- end
303
+ info['query']['userinfo']['rights'].each { |i| ret.push(i) }
300
304
  else
301
305
  return false
302
306
  end
303
307
  else
304
308
  info = get_userlists('rights', username)
305
- info["query"]["users"].each do |i|
306
- i["rights"].each do |g|
309
+ info['query']['users'].each do |i|
310
+ i['rights'].each do |g|
307
311
  ret.push(g)
308
312
  end
309
313
  end
310
314
  end
311
315
 
312
- return ret
316
+ ret
313
317
  end
314
318
 
315
319
  # Gets contribution count for the user.
316
- # @param username [String] The username to get the contribution count of. Optional. Defaults to the currently logged in user.
317
- # @param autoparse [Boolean] Whether to automatically format the string with commas. Defaults to true.
318
- # @return [Boolean/Int/String] False if username is nil and Butt is not logged in. An integer value of the contribution count if autoparse is false. A formatted string version of the contribution count if autoparse is true.
320
+ # @param username [String] The username to get the contribution count of.
321
+ # Optional. Defaults to the currently logged in user.
322
+ # @param autoparse [Boolean] Whether to automatically format the string
323
+ # with commas using string-utility. Defaults to true.
324
+ # @return [Boolean/Int/String] False if username is nil and Butt is not
325
+ # logged in. An integer value of the contribution count if autoparse is
326
+ # false. A formatted string version of the contribution count if
327
+ # autoparse is true.
319
328
  def get_contrib_count(username = nil, autoparse = true)
320
329
  count = nil
321
330
  if username.nil?
322
331
  if @logged_in
323
332
  info = get_userlists('editcount')
324
- count = info["query"]["userinfo"]["editcount"]
333
+ count = info['query']['userinfo']['editcount']
325
334
  else
326
335
  return false
327
336
  end
328
337
  else
329
338
  info = get_userlists('editcount', username)
330
- info["query"]["users"].each do |i|
331
- count = i["editcount"]
332
- end
339
+ info['query']['users'].each { |i| count = i['editcount'] }
333
340
  end
334
341
 
335
342
  if autoparse
336
343
  countstring = count.to_s.separate
337
344
  return countstring
338
345
  end
339
- return count
346
+
347
+ count
340
348
  end
341
349
 
342
350
  # Gets when the user registered.
343
- # @param username [String] The username to get the registration date and time of. Optional. Defaults to the currently logged in user.
351
+ # @param username [String] The username to get the registration date and
352
+ # time of. Optional. Defaults to the currently logged in user.
344
353
  # @return [DateTime] The registration date and time as a DateTime object.
345
354
  def get_registration_time(username = nil)
346
355
  time = nil
@@ -348,48 +357,50 @@ module MediaWiki
348
357
  if username.nil?
349
358
  if @logged_in
350
359
  info = get_userlists('registrationdate')
351
- time = info["query"]["userinfo"]["registrationdate"]
360
+ time = info['query']['userinfo']['registrationdate']
352
361
  else
353
362
  return false
354
363
  end
355
364
  else
356
365
  info = get_userlists('registration', username)
357
- info["query"]["users"].each do |i|
358
- time = i["registration"]
359
- end
366
+ info['query']['users'].each { |i| time = i['registration'] }
360
367
  end
361
368
 
362
- # %Y: Year including century, %m: Month num, %d day of month, %T Time as H:M:S
363
- timeformat = "%Y-%m-%dT%T"
369
+ # %Y: Year including century
370
+ # %m: Month num
371
+ # %d: day of month
372
+ # %T: Time as HH:MM:SS
373
+ timeformat = '%Y-%m-%dT%T'
364
374
  time = DateTime.strptime(time, timeformat)
375
+
376
+ time
365
377
  end
366
378
 
367
379
  # Gets the gender for the provded user.
368
380
  # @param username [String] The user.
369
- # @return [String] The gender. "male", "female", or "unknown".
381
+ # @return [String] The gender. 'male', 'female', or 'unknown'.
370
382
  def get_user_gender(username)
371
383
  gender = nil
372
384
  info = get_userlists('gender', username)
373
- info["query"]["users"].each do |i|
374
- gender = i["gender"]
375
- end
385
+ info['query']['users'].each { |i| gender = i['gender'] }
376
386
 
377
- return gender
387
+
388
+ gender
378
389
  end
379
390
 
380
391
  # Gets the amount of results for the search value.
381
392
  # @param search_value [String] The thing to search for.
382
- # @param namespace [Int] The namespace to search in. Defaults to the main namespace.
393
+ # @param namespace [Int] The namespace to search in.
394
+ # Defaults to 0 (the main namespace).
383
395
  # @return [Int] The number of pages that matched the search.
384
396
  def get_search_result_amount(search_value, namespace = 0)
385
397
  params = {
386
398
  action: 'query',
387
399
  list: 'search',
388
- srsearch: search_value,
389
- format: 'json'
400
+ srsearch: search_value
390
401
  }
391
402
 
392
- if $namespaces.has_value?(namespace)
403
+ if $namespaces.value?(namespace)
393
404
  params[:srnamespace] = namespace
394
405
  else
395
406
  params[:srnamespace] = 0
@@ -397,22 +408,22 @@ module MediaWiki
397
408
 
398
409
  response = post(params)
399
410
 
400
- return response["query"]["searchinfo"]["totalhits"]
411
+ response['query']['searchinfo']['totalhits']
401
412
  end
402
413
 
403
414
  # Gets an array containing page titles that matched the search.
404
415
  # @param search_value [String] The thing to search for.
405
- # @param namespace [Int] The namespace to search in. Defaults to the main namespace.
416
+ # @param namespace [Int] The namespace to search in.
417
+ # Defaults to 0 (the main namespace).
406
418
  # @return [Array] The page titles that matched the search.
407
419
  def get_search_results(search_value, namespace = 0)
408
420
  params = {
409
421
  action: 'query',
410
422
  list: 'search',
411
- srsearch: search_value,
412
- format: 'json'
423
+ srsearch: search_value
413
424
  }
414
425
 
415
- if $namespaces.has_value?(namespace)
426
+ if $namespaces.value?(namespace)
416
427
  params[:srnamespace] = namespace
417
428
  else
418
429
  params[:srnamespace] = 0
@@ -420,12 +431,10 @@ module MediaWiki
420
431
 
421
432
  response = post(params)
422
433
 
423
- ret = Array.new
424
- response["query"]["search"].each do |search|
425
- ret.push(search["title"])
426
- end
434
+ ret = []
435
+ response['query']['search'].each { |search| ret.push(search['title']) }
427
436
 
428
- return ret
437
+ ret
429
438
  end
430
439
  end
431
440
  end