mediawiki-butt 0.7.0 → 0.8.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.
@@ -72,8 +72,9 @@ module MediaWiki
72
72
  # This can include File: at the beginning, but it will be removed
73
73
  # through regex. Optional. If ommitted, it will be everything after
74
74
  # the last slash in the URL.
75
- # @return [Boolean/String] true if the upload was successful, else the
76
- # warning's key. Returns false if the file extension is not valid.
75
+ # @return [Boolean] True if the upload was successful, false if the
76
+ # file extension is not valid.
77
+ # @return [String] The warning's key if it was unsuccessful.
77
78
  def upload(url, filename = nil)
78
79
  params = {
79
80
  action: 'upload',
@@ -114,7 +115,8 @@ module MediaWiki
114
115
  # @param talk [Boolean] Whether to move the associated talk page.
115
116
  # Defaults to true.
116
117
  # @param redirect [Boolean] Whether to create a redirect. Defaults to false.
117
- # @return [Boolean/String] true if successful, the error code if not.
118
+ # @return [Boolean] True if it was successful.
119
+ # @return [String] The error code if it was unsuccessful.
118
120
  def move(from, to, reason = nil, talk = true, redirect = false)
119
121
  params = {
120
122
  action: 'move',
@@ -139,7 +141,8 @@ module MediaWiki
139
141
  # Deletes a page.
140
142
  # @param title [String] The page to delete.
141
143
  # @param reason [String] The reason to be displayed in logs. Optional.
142
- # @return [Boolean/String] true if successful, the error code if not.
144
+ # @return [Boolean] True if successful.
145
+ # @return [String] The error code if it was not successful.
143
146
  def delete(title, reason = nil)
144
147
  params = {
145
148
  action: 'delete',
@@ -1,11 +1,8 @@
1
1
  require_relative '../constants'
2
- require 'string-utility'
3
2
 
4
3
  module MediaWiki
5
4
  module Query
6
5
  module Lists
7
- using StringUtility
8
-
9
6
  # Gets an array of backlinks to a given title.
10
7
  # @param title [String] The page to get the backlinks of.
11
8
  # @param limit [Int] The maximum number of pages to get. Defaults to 500,
@@ -15,23 +12,11 @@ module MediaWiki
15
12
  def what_links_here(title, limit = 500)
16
13
  params = {
17
14
  action: 'query',
18
- bltitle: title
15
+ list: 'backlinks',
16
+ bltitle: title,
17
+ bllimit: MediaWiki::Query.get_limited(limit)
19
18
  }
20
19
 
21
- if limit > 500
22
- if user_bot? == true
23
- if limit > 5000
24
- params[:bllimit] = 5000
25
- else
26
- params[:bllimit] = limit
27
- end
28
- else
29
- params[:bllimit] = 500
30
- end
31
- else
32
- params[:bllimit] = limit
33
- end
34
-
35
20
  ret = []
36
21
  response = post(params)
37
22
  response['query']['backlinks'].each { |bl| ret.push(bl['title']) }
@@ -51,7 +36,8 @@ module MediaWiki
51
36
  params = {
52
37
  action: 'query',
53
38
  list: 'categorymembers',
54
- cmprop: 'title'
39
+ cmprop: 'title',
40
+ cmlimit: MediaWiki::Query.get_limited(limit)
55
41
  }
56
42
 
57
43
  if category =~ /[Cc]ategory\:/
@@ -59,21 +45,6 @@ module MediaWiki
59
45
  else
60
46
  params[:cmtitle] = "Category:#{category}"
61
47
  end
62
-
63
- if limit > 500
64
- if user_bot?
65
- if limit > 5000
66
- params[:cmlimit] = 5000
67
- else
68
- params[:cmlimit] = limit
69
- end
70
- else
71
- params[:cmlimit] = 500
72
- end
73
- else
74
- params[:cmlimit] = limit
75
- end
76
-
77
48
  ret = []
78
49
  response = post(params)
79
50
  response['query']['categorymembers'].each { |cm| ret.push(cm['title']) }
@@ -91,29 +62,16 @@ module MediaWiki
91
62
  def get_random_pages(number_of_pages = 1, namespace = 0)
92
63
  params = {
93
64
  action: 'query',
94
- list: 'random'
65
+ list: 'random',
66
+ rnlimit: MediaWiki::Query.get_limited(number_of_pages, 10, 20)
95
67
  }
96
68
 
97
- if $namespaces.value?(namespace)
69
+ if @namespaces.value?(namespace)
98
70
  params[:rnnamespace] = namespace
99
71
  else
100
72
  params[:rnnamespace] = 0
101
73
  end
102
74
 
103
- if number_of_pages > 10
104
- if user_bot?
105
- if limit > 20
106
- params[:rnlimit] = 20
107
- else
108
- params[:rnlimit] = limit
109
- end
110
- else
111
- params[:rnlimit] = 10
112
- end
113
- else
114
- params[:rnlimit] = number_of_pages
115
- end
116
-
117
75
  ret = []
118
76
  responce = post(params)
119
77
  responce['query']['random'].each { |a| ret.push(a['title']) }
@@ -126,8 +84,8 @@ module MediaWiki
126
84
  # @param prop [String] The usprop parameter.
127
85
  # @param username [String] The username to get info for. Optional.
128
86
  # Defaults to the currently logged in user if ommitted.
129
- # @return [String/Nil] Parsed full response if successful, nil if
130
- # the username is nil and the Butt is not logged in.
87
+ # @return [String] Parsed full response if successful.
88
+ # @return [Nil] If the username is nil and the Butt is not logged in.
131
89
  def get_userlists(prop, username = nil)
132
90
  if username.nil?
133
91
  if @logged_in
@@ -152,8 +110,8 @@ module MediaWiki
152
110
  # Gets an array of all the user's groups.
153
111
  # @param username [String] The username to get groups of. Optional.
154
112
  # Defaults to the currently logged in user.
155
- # @return [Array/Boolean] All of the user's groups, or false if username
156
- # is nil and Butt is not logged in.
113
+ # @return [Array] All of the user's groups.
114
+ # @return [Boolean] False if username is nil and not logged in.
157
115
  def get_usergroups(username = nil)
158
116
  ret = []
159
117
  if username.nil?
@@ -176,8 +134,8 @@ module MediaWiki
176
134
  # Gets the user rights for the user.
177
135
  # @param username [String] The user to get the rights for. Optional.
178
136
  # Defaults to the currently logged in user.
179
- # @return [Array/Boolean] All of the user's groups, or false if username
180
- # is nil and Butt is not logged in.
137
+ # @return [Array] All of the user's groups.
138
+ # @return [Boolean] False if username is nil and not logged in.
181
139
  def get_userrights(username = nil)
182
140
  ret = []
183
141
  if username.nil?
@@ -202,13 +160,9 @@ module MediaWiki
202
160
  # Gets contribution count for the user.
203
161
  # @param username [String] The username to get the contribution count of.
204
162
  # Optional. Defaults to the currently logged in user.
205
- # @param autoparse [Boolean] Whether to automatically format the string
206
- # with commas using string-utility. Defaults to true.
207
- # @return [Boolean/Int/String] False if username is nil and Butt is not
208
- # logged in. An integer value of the contribution count if autoparse is
209
- # false. A formatted string version of the contribution count if
210
- # autoparse is true.
211
- def get_contrib_count(username = nil, autoparse = true)
163
+ # @return [Boolean] False if username is nil and not logged in.
164
+ # @return [Int] The number of contributions the user has made.
165
+ def get_contrib_count(username = nil)
212
166
  count = nil
213
167
  if username.nil?
214
168
  if @logged_in
@@ -222,11 +176,6 @@ module MediaWiki
222
176
  info['query']['users'].each { |i| count = i['editcount'] }
223
177
  end
224
178
 
225
- if autoparse
226
- countstring = count.to_s.separate
227
- return countstring
228
- end
229
-
230
179
  count
231
180
  end
232
181
 
@@ -267,7 +216,6 @@ module MediaWiki
267
216
  info = get_userlists('gender', username)
268
217
  info['query']['users'].each { |i| gender = i['gender'] }
269
218
 
270
-
271
219
  gender
272
220
  end
273
221
 
@@ -283,14 +231,13 @@ module MediaWiki
283
231
  srsearch: search_value
284
232
  }
285
233
 
286
- if $namespaces.value?(namespace)
234
+ if MediaWiki::Constants::NAMEPSACES.value?(namespace)
287
235
  params[:srnamespace] = namespace
288
236
  else
289
237
  params[:srnamespace] = 0
290
238
  end
291
239
 
292
240
  response = post(params)
293
-
294
241
  response['query']['searchinfo']['totalhits']
295
242
  end
296
243
 
@@ -306,7 +253,7 @@ module MediaWiki
306
253
  srsearch: search_value
307
254
  }
308
255
 
309
- if $namespaces.value?(namespace)
256
+ if MediaWiki::Constants::NAMESPACES.value?(namespace)
310
257
  params[:srnamespace] = namespace
311
258
  else
312
259
  params[:srnamespace] = 0
@@ -327,23 +274,10 @@ module MediaWiki
327
274
  def get_all_categories(limit = 500)
328
275
  params = {
329
276
  action: 'query',
330
- list: 'allcategories'
277
+ list: 'allcategories',
278
+ aclimit: MediaWiki::Query.get_limited(limit)
331
279
  }
332
280
 
333
- if limit > 500
334
- if user_bot?
335
- if limit > 5000
336
- params[:aclimit] = 5000
337
- else
338
- params[:aclimit] = limit
339
- end
340
- else
341
- params[:aclimit] = 500
342
- end
343
- else
344
- params[:aclimit] = limit
345
- end
346
-
347
281
  response = post(params)
348
282
 
349
283
  ret = []
@@ -359,27 +293,184 @@ module MediaWiki
359
293
  def get_all_images(limit = 500)
360
294
  params = {
361
295
  action: 'query',
362
- list: 'allimages'
296
+ list: 'allimages',
297
+ ailimit: MediaWiki::Query.get_limited(limit)
363
298
  }
364
299
 
365
- if limit > 500
366
- if user_bot?
367
- if limit > 5000
368
- params[:ailimit] = 5000
369
- else
370
- params[:ailimit] = limit
371
- end
372
- else
373
- params[:ailimit] = 500
374
- end
375
- else
376
- params[:ailimit] = limit
300
+ response = post(params)
301
+
302
+ ret = []
303
+ response['query']['allimages'].each { |i| ret.push(i['name']) }
304
+
305
+ ret
306
+ end
307
+
308
+ # Gets all pages within a namespace integer.
309
+ # @param namespace [Int] The namespace ID.
310
+ # @param limit [Int] See #get_all_images
311
+ # @return [Array] An array of all page titles.
312
+ def get_all_pages_in_namespace(namespace, limit = 500)
313
+ params = {
314
+ action: 'query',
315
+ list: 'allpages',
316
+ apnamespace: namespace,
317
+ aplimit: MediaWiki::Query.get_limited(limit)
318
+ }
319
+
320
+ response = post(params)
321
+
322
+ ret = []
323
+ response['query']['allpages'].each { |p| ret.push(p['title']) }
324
+
325
+ ret
326
+ end
327
+
328
+ # Gets all users, or all users in a group.
329
+ # @param group [String] The group to limit this query to.
330
+ # @param limit [Int] See #get_all_images.
331
+ # @return [Hash] A hash of all users, names are keys, IDs are values.
332
+ def get_all_users(group = nil, limit = 500)
333
+ params = {
334
+ action: 'query',
335
+ list: 'allusers',
336
+ aulimit: MediaWiki::Query.get_limited(limit)
337
+ }
338
+ params[:augroup] = group unless group.nil?
339
+
340
+ response = post(params)
341
+
342
+ ret = {}
343
+ response['query']['allusers'].each { |u| ret[u['name']] = u['userid'] }
344
+
345
+ ret
346
+ end
347
+
348
+ # Gets all block IDs on the wiki. It seems like this only gets non-IP
349
+ # blocks, but the MediaWiki docs are a bit unclear.
350
+ # @param limit [Int] See #get_all_images.
351
+ # @return [Array] All block IDs as strings.
352
+ def get_all_blocks(limit = 500)
353
+ params = {
354
+ action: 'query',
355
+ list: 'blocks',
356
+ bklimit: MediaWiki::Query.get_limited(limit),
357
+ bkprop: 'id'
358
+ }
359
+
360
+ response = post(params)
361
+
362
+ ret = []
363
+ response['query']['blocks'].each { |b| ret.push(b['id']) }
364
+
365
+ ret
366
+ end
367
+
368
+ # Gets all page titles that transclude a given page.
369
+ # @param page [String] The page name.
370
+ # @param limit [Int] See #get_all_images.
371
+ # @return [Array] All transcluder page titles.
372
+ def get_all_transcluders(page, limit = 500)
373
+ params = {
374
+ action: 'query',
375
+ list: 'embeddedin',
376
+ eititle: page,
377
+ eilimit: MediaWiki::Query.get_limited(limit)
378
+ }
379
+
380
+ response = post(params)
381
+
382
+ ret = []
383
+ response['query']['embeddedin'].each { |e| ret.push(e['title']) }
384
+
385
+ ret
386
+ end
387
+
388
+ # Gets an array of all deleted or archived files on the wiki.
389
+ # @param limit [Int] See #get_all_images
390
+ # @return [Array] All deleted file names. These are not titles, so they do
391
+ # not include "File:".
392
+ def get_all_deleted_files(limit = 500)
393
+ params = {
394
+ action: 'query',
395
+ list: 'filearchive',
396
+ falimit: MediaWiki::Query.get_limited(limit)
397
+ }
398
+
399
+ response = post(params)
400
+
401
+ ret = []
402
+ response['query']['filearchive'].each { |f| ret.push(f['name']) }
403
+
404
+ ret
405
+ end
406
+
407
+ # Gets a list of all protected pages, by protection level if provided.
408
+ # @param protection_level [String] The protection level, e.g., sysop
409
+ # @param limit [Int] See #get_all_images.
410
+ # @return [Array] All protected page titles.
411
+ def get_all_protected_titles(protection_level = nil, limit = 500)
412
+ params = {
413
+ action: 'query',
414
+ list: 'protectedtitles',
415
+ ptlimit: MediaWiki::Query.get_limited(limit)
416
+ }
417
+ params[:ptlevel] = protection_level unless protection_level.nil?
418
+
419
+ response = post(params)
420
+
421
+ ret = []
422
+ response['query']['protectedtitles'].each { |t| ret.push(t['title']) }
423
+
424
+ ret
425
+ end
426
+
427
+ # Gets the latest contributions by the user until the limit.
428
+ # @param user [String] The username.
429
+ # @param limit [Int] See #get_all_images.
430
+ # @return [Hash] Each contribution by its revid, containing the title,
431
+ # summary, total contribution size, and the size change relative to the
432
+ # previous edit.
433
+ def get_user_contributions(user, limit = 500)
434
+ params = {
435
+ action: 'query',
436
+ list: 'usercontribs',
437
+ ucuser: user,
438
+ uclimit: MediaWiki::Query.get_limited(limit),
439
+ ucprop: 'ids|title|comment|size|sizediff|flags|patrolled'
440
+ }
441
+
442
+ response = post(params)
443
+
444
+ ret = {}
445
+ response['query']['usercontribs'].each do |item|
446
+ ret[item['revid']] = {
447
+ title: item['title'],
448
+ summary: item['comment'],
449
+ total_size: item['size'],
450
+ diff_size: item['sizediff']
451
+ }
377
452
  end
378
453
 
454
+ ret
455
+ end
456
+
457
+ # Gets the user's full watchlist. If no user is provided, it will use the
458
+ # currently logged in user, according to the MediaWiki API.
459
+ # @param user [String] The username.
460
+ # @param limit [Int] See #get_all_images.
461
+ # @return [Array] All the watchlist page titles.
462
+ def get_full_watchlist(user = nil, limit = 500)
463
+ params = {
464
+ action: 'query',
465
+ list: 'watchlist',
466
+ wlprop: 'title'
467
+ }
468
+ params[:wluser] = user unless user.nil?
469
+
379
470
  response = post(params)
380
471
 
381
472
  ret = []
382
- response['query']['allimages'].each { |i| ret.push(i['name']) }
473
+ response['query']['watchlist'].each { |t| ret.push(t['title']) }
383
474
 
384
475
  ret
385
476
  end
@@ -4,8 +4,8 @@ module MediaWiki
4
4
  module UserInfo
5
5
  # Gets meta information for the currently logged in user.
6
6
  # @param prop [String] The uiprop to get. Optional.
7
- # @return [Response/Boolean] Either a full, parsed response, or false if
8
- # not logged in.
7
+ # @return [Response/Boolean] Either a full, parsed response.
8
+ # @return [Boolean] False if not logged in.
9
9
  def get_current_user_meta(prop = nil)
10
10
  if @logged_in
11
11
  params = {
@@ -21,7 +21,8 @@ module MediaWiki
21
21
  end
22
22
 
23
23
  # Gets the current user's username.
24
- # @return [String/Boolean] Returns the username, or false.
24
+ # @return [String] Returns the username.
25
+ # @return [Boolean] False if...
25
26
  def get_current_user_name
26
27
  if !@name.nil?
27
28
  return @name
@@ -51,9 +52,9 @@ module MediaWiki
51
52
 
52
53
  # Gets a hash-of-arrays containing all the groups the user can add and
53
54
  # remove people from.
54
- # @return [Boolean/Hash] False if get_current_user_meta is false, else
55
- # a hash containing arrays of all the groups the user can add/remove
56
- # people from.
55
+ # @return [Boolean] False if get_current_user_meta is false
56
+ # @return [Hash] All the groups that the user can add, remove, add-self,
57
+ # and remove-self.
57
58
  def get_changeable_groups
58
59
  response = get_current_user_meta('changeablegroups')
59
60
  if response != false
@@ -78,8 +79,8 @@ module MediaWiki
78
79
  end
79
80
 
80
81
  # Gets the currently logged in user's real name.
81
- # @return [String/Nil] Nil if they don't have a real name set, or their
82
- # real name.
82
+ # @return [String] The user's real name.
83
+ # @return [Nil] If they don't have a real name set.
83
84
  def get_realname
84
85
  response = get_current_user_meta('realname')
85
86
  if response['query']['userinfo']['realname'] == ''
@@ -90,7 +91,8 @@ module MediaWiki
90
91
  end
91
92
 
92
93
  # Gets the currently logged in user's email address.
93
- # @return [String/Nil] Nil if their email is not set, or their email.
94
+ # @return [String] The user's email address.
95
+ # @return [Nil] If their email address is not set.
94
96
  def get_email_address
95
97
  response = get_current_user_meta('email')
96
98
  if response['query']['userinfo']['email'] == ''
@@ -100,6 +102,8 @@ module MediaWiki
100
102
  end
101
103
  end
102
104
 
105
+ # Gets the user's options.
106
+ # @return [Hash] The user's options.
103
107
  def get_current_user_options
104
108
  response = get_current_user_meta('options')
105
109
  ret = {}