mediawiki-butt 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,561 +0,0 @@
1
- require_relative '../constants'
2
- require_relative 'query'
3
-
4
- module MediaWiki
5
- module Query
6
- module Lists
7
- # Gets an array of backlinks to a given title.
8
- # @param title [String] The page to get the backlinks of.
9
- # @param limit [Int] The maximum number of pages to get. Defaults to 500,
10
- # and cannot be greater than that unless the user is a bot. If the user
11
- # is a bot, the limit cannot be greater than 5000.
12
- # @see https://www.mediawiki.org/wiki/API:Backlinks MediaWiki Backlinks
13
- # API Docs
14
- # @since 0.1.0
15
- # @return [Array] All backlinks until the limit
16
- def what_links_here(title, limit = 500)
17
- params = {
18
- action: 'query',
19
- list: 'backlinks',
20
- bltitle: title,
21
- bllimit: get_limited(limit)
22
- }
23
-
24
- ret = []
25
- response = post(params)
26
- response['query']['backlinks'].each { |bl| ret.push(bl['title']) }
27
-
28
- ret
29
- end
30
-
31
- # Returns an array of all page titles that belong to a given category. By
32
- # default, it will only get the pages. Files and subcategories can be
33
- # gotten through {#get_subcategories} and {#get_files_in_category} or
34
- # setting the type parameter.
35
- # @param category [String] The category title. It can include "Category:",
36
- # or not, it doesn't really matter because we will add it if it is
37
- # missing.
38
- # @param limit [Int] The maximum number of members to get. Defaults to
39
- # 500, and cannot be greater than that unless the user is a bot.
40
- # If the user is a bot, the limit cannot be greater than 5000.
41
- # @param type [String] The type of stuff to get. There are 3 valid values:
42
- # page, file, and subcat. Separate these with a pipe character, e.g.,
43
- # 'page|file|subcat'.
44
- # @see https://www.mediawiki.org/wiki/API:Categorymembers MediaWiki
45
- # Category Members API Docs
46
- # @since 0.1.0
47
- # @return [Array] All category members until the limit
48
- def get_category_members(category, limit = 500, type = 'page')
49
- params = {
50
- action: 'query',
51
- list: 'categorymembers',
52
- cmprop: 'title',
53
- cmlimit: get_limited(limit),
54
- cmtype: type
55
- }
56
-
57
- if category =~ /[Cc]ategory\:/
58
- params[:cmtitle] = category
59
- else
60
- params[:cmtitle] = "Category:#{category}"
61
- end
62
- ret = []
63
- response = post(params)
64
- response['query']['categorymembers'].each { |cm| ret.push(cm['title']) }
65
-
66
- ret
67
- end
68
-
69
- # Gets the subcategories of a given category.
70
- # @param category [String] See {#get_category_members}
71
- # @param limit [Int] See {#get_category_members}
72
- # @see {#get_category_members}
73
- # @since 0.9.0
74
- # @return [Array<String>] All subcategories.
75
- def get_subcategories(category, limit = 500)
76
- get_category_members(category, limit, 'subcat')
77
- end
78
-
79
- # Gets all of the files in a given category.
80
- # @param category [String] See {#get_category_members}
81
- # @param limit [Int] See {#get_category_members}
82
- # @see {#get_category_members}
83
- # @since 0.9.0
84
- # @return [Array<String>] All files in the category.
85
- def get_files_in_category(category, limit = 500)
86
- get_category_members(category, limit, 'file')
87
- end
88
-
89
- # Returns an array of random pages titles.
90
- # @param number_of_pages [Int] The number of articles to get.
91
- # Defaults to 1. Cannot be greater than 10 for normal users,
92
- # or 20 for bots.
93
- # @param namespace [Int] The namespace ID. Defaults to
94
- # 0 (the main namespace).
95
- # @see https://www.mediawiki.org/wiki/API:Random MediaWiki Random API Docs
96
- # @since 0.2.0
97
- # @return [Array] All members
98
- def get_random_pages(number_of_pages = 1, namespace = 0)
99
- params = {
100
- action: 'query',
101
- list: 'random',
102
- rnlimit: get_limited(number_of_pages, 10, 20)
103
- }
104
-
105
- if MediaWiki::Constants::NAMESPACES.value?(namespace)
106
- params[:rnnamespace] = namespace
107
- else
108
- params[:rnnamespace] = 0
109
- end
110
-
111
- ret = []
112
- responce = post(params)
113
- responce['query']['random'].each { |a| ret.push(a['title']) }
114
-
115
- ret
116
- end
117
-
118
- # Gets user information. This method should rarely be used by
119
- # normal users, unless they want a huge amount of user data at once.
120
- # @param prop [String] The usprop parameter.
121
- # @param username [String] The username to get info for. Optional.
122
- # Defaults to the currently logged in user if ommitted.
123
- # @see https://www.mediawiki.org/wiki/API:Users MediaWiki User Lists API
124
- # Docs
125
- # @since 0.3.0
126
- # @return [String] Parsed full response if successful.
127
- # @return [Nil] If the username is nil and the Butt is not logged in.
128
- def get_userlists(prop, username = nil)
129
- if username.nil?
130
- if @logged_in
131
- response = get_current_user_meta(prop)
132
- else
133
- return false
134
- end
135
- else
136
- params = {
137
- action: 'query',
138
- list: 'users',
139
- usprop: prop,
140
- ususers: username
141
- }
142
-
143
- response = post(params)
144
- end
145
-
146
- response
147
- end
148
-
149
- # Gets an array of all the user's groups.
150
- # @param username [String] The username to get groups of. Optional.
151
- # Defaults to the currently logged in user.
152
- # @see get_userlists
153
- # @since 0.3.0
154
- # @return [Array] All of the user's groups.
155
- # @return [Boolean] False if username is nil and not logged in.
156
- def get_usergroups(username = nil)
157
- ret = []
158
- if username.nil?
159
- if @logged_in
160
- info = get_userlists('groups')
161
- info['query']['userinfo']['groups'].each { |i| ret.push(i) }
162
- else
163
- return false
164
- end
165
- else
166
- info = get_userlists('groups', username)
167
- info['query']['users'].each do |i|
168
- i['groups'].each { |g| ret.push(g) }
169
- end
170
- end
171
-
172
- ret
173
- end
174
-
175
- # Gets the user rights for the user.
176
- # @param username [String] The user to get the rights for. Optional.
177
- # Defaults to the currently logged in user.
178
- # @see get_userlists
179
- # @since 0.3.0
180
- # @return [Array] All of the user's groups.
181
- # @return [Boolean] False if username is nil and not logged in.
182
- def get_userrights(username = nil)
183
- ret = []
184
- if username.nil?
185
- if @logged_in
186
- info = get_userlists('rights')
187
- info['query']['userinfo']['rights'].each { |i| ret.push(i) }
188
- else
189
- return false
190
- end
191
- else
192
- info = get_userlists('rights', username)
193
- info['query']['users'].each do |i|
194
- i['rights'].each do |g|
195
- ret.push(g)
196
- end
197
- end
198
- end
199
-
200
- ret
201
- end
202
-
203
- # Gets contribution count for the user.
204
- # @param username [String] The username to get the contribution count of.
205
- # Optional. Defaults to the currently logged in user.
206
- # @see get_userlists
207
- # @since 0.3.0
208
- # @return [Boolean] False if username is nil and not logged in.
209
- # @return [Int] The number of contributions the user has made.
210
- def get_contrib_count(username = nil)
211
- count = nil
212
- if username.nil?
213
- if @logged_in
214
- info = get_userlists('editcount')
215
- count = info['query']['userinfo']['editcount']
216
- else
217
- return false
218
- end
219
- else
220
- info = get_userlists('editcount', username)
221
- info['query']['users'].each { |i| count = i['editcount'] }
222
- end
223
-
224
- count
225
- end
226
-
227
- # Gets when the user registered.
228
- # @param username [String] The username to get the registration date and
229
- # time of. Optional. Defaults to the currently logged in user.
230
- # @see get_userlists
231
- # @since 0.4.0
232
- # @return [DateTime] The registration date and time as a DateTime object.
233
- def get_registration_time(username = nil)
234
- time = nil
235
- # Do note that in Userinfo, registration is called registrationdate.
236
- if username.nil?
237
- if @logged_in
238
- info = get_userlists('registrationdate')
239
- time = info['query']['userinfo']['registrationdate']
240
- else
241
- return false
242
- end
243
- else
244
- info = get_userlists('registration', username)
245
- info['query']['users'].each { |i| time = i['registration'] }
246
- end
247
-
248
- # %Y: Year including century
249
- # %m: Month num
250
- # %d: day of month
251
- # %T: Time as HH:MM:SS
252
- timeformat = '%Y-%m-%dT%T'
253
- time = DateTime.strptime(time, timeformat)
254
-
255
- time
256
- end
257
-
258
- # Gets the gender for the provded user.
259
- # @param username [String] The user.
260
- # @see get_userlists
261
- # @since 0.4.0
262
- # @return [String] The gender. 'male', 'female', or 'unknown'.
263
- def get_user_gender(username)
264
- gender = nil
265
- info = get_userlists('gender', username)
266
- info['query']['users'].each { |i| gender = i['gender'] }
267
-
268
- gender
269
- end
270
-
271
- # Gets the amount of results for the search value.
272
- # @param search_value [String] The thing to search for.
273
- # @param namespace [Int] The namespace to search in.
274
- # Defaults to 0 (the main namespace).
275
- # @see https://www.mediawiki.org/wiki/API:Search MediaWiki Search API Docs
276
- # @since 0.4.0
277
- # @return [Int] The number of pages that matched the search.
278
- def get_search_result_amount(search_value, namespace = 0)
279
- params = {
280
- action: 'query',
281
- list: 'search',
282
- srsearch: search_value
283
- }
284
-
285
- if MediaWiki::Constants::NAMEPSACES.value?(namespace)
286
- params[:srnamespace] = namespace
287
- else
288
- params[:srnamespace] = 0
289
- end
290
-
291
- response = post(params)
292
- response['query']['searchinfo']['totalhits']
293
- end
294
-
295
- # Gets an array containing page titles that matched the search.
296
- # @param search_value [String] The thing to search for.
297
- # @param namespace [Int] The namespace to search in.
298
- # Defaults to 0 (the main namespace).
299
- # @see https://www.mediawiki.org/wiki/API:Search MediaWiki Search API Docs
300
- # @since 0.4.0
301
- # @return [Array] The page titles that matched the search.
302
- def get_search_results(search_value, namespace = 0)
303
- params = {
304
- action: 'query',
305
- list: 'search',
306
- srsearch: search_value
307
- }
308
-
309
- if MediaWiki::Constants::NAMESPACES.value?(namespace)
310
- params[:srnamespace] = namespace
311
- else
312
- params[:srnamespace] = 0
313
- end
314
-
315
- response = post(params)
316
-
317
- ret = []
318
- response['query']['search'].each { |search| ret.push(search['title']) }
319
-
320
- ret
321
- end
322
-
323
- # Gets all categories on the entire wiki.
324
- # @param limit [Int] The maximum number of categories to get. Defaults to
325
- # 500. Cannot be greater than 500 for normal users, or 5000 for bots.
326
- # @see https://www.mediawiki.org/wiki/API:Allcategories MediaWiki
327
- # Allcategories API Docs
328
- # @since 0.7.0
329
- # @return [Array] An array of all categories.
330
- def get_all_categories(limit = 500)
331
- params = {
332
- action: 'query',
333
- list: 'allcategories',
334
- aclimit: get_limited(limit)
335
- }
336
-
337
- response = post(params)
338
-
339
- ret = []
340
- response['query']['allcategories'].each { |c| ret.push(c['*']) }
341
-
342
- ret
343
- end
344
-
345
- # Gets all the images on the wiki.
346
- # @param limit [Int] The maximum number of images to get. Defaults to 500.
347
- # Cannot be greater than 500 for normal users, or 5000 for bots.
348
- # @see https://www.mediawiki.org/wiki/API:Allimages MediaWiki Allimages
349
- # API Docs
350
- # @since 0.7.0
351
- # @return [Array] An array of all images.
352
- def get_all_images(limit = 500)
353
- params = {
354
- action: 'query',
355
- list: 'allimages',
356
- ailimit: get_limited(limit)
357
- }
358
-
359
- response = post(params)
360
-
361
- ret = []
362
- response['query']['allimages'].each { |i| ret.push(i['name']) }
363
-
364
- ret
365
- end
366
-
367
- # Gets all pages within a namespace integer.
368
- # @param namespace [Int] The namespace ID.
369
- # @param limit [Int] See #get_all_images
370
- # @see https://www.mediawiki.org/wiki/API:Allpages MediaWiki Allpages
371
- # API Docs
372
- # @since 0.8.0
373
- # @return [Array] An array of all page titles.
374
- def get_all_pages_in_namespace(namespace, limit = 500)
375
- params = {
376
- action: 'query',
377
- list: 'allpages',
378
- apnamespace: namespace,
379
- aplimit: get_limited(limit)
380
- }
381
-
382
- response = post(params)
383
-
384
- ret = []
385
- response['query']['allpages'].each { |p| ret.push(p['title']) }
386
-
387
- ret
388
- end
389
-
390
- # Gets all users, or all users in a group.
391
- # @param group [String] The group to limit this query to.
392
- # @param limit [Int] See #get_all_images.
393
- # @see https://www.mediawiki.org/wiki/API:Allusers MediaWiki Allusers
394
- # API Docs
395
- # @since 0.8.0
396
- # @return [Hash] A hash of all users, names are keys, IDs are values.
397
- def get_all_users(group = nil, limit = 500)
398
- params = {
399
- action: 'query',
400
- list: 'allusers',
401
- aulimit: get_limited(limit)
402
- }
403
- params[:augroup] = group unless group.nil?
404
-
405
- response = post(params)
406
-
407
- ret = {}
408
- response['query']['allusers'].each { |u| ret[u['name']] = u['userid'] }
409
-
410
- ret
411
- end
412
-
413
- # Gets all block IDs on the wiki. It seems like this only gets non-IP
414
- # blocks, but the MediaWiki docs are a bit unclear.
415
- # @param limit [Int] See #get_all_images.
416
- # @see https://www.mediawiki.org/wiki/API:Blocks MediaWiki Blocks API Docs
417
- # @since 0.8.0
418
- # @return [Array] All block IDs as strings.
419
- def get_all_blocks(limit = 500)
420
- params = {
421
- action: 'query',
422
- list: 'blocks',
423
- bklimit: get_limited(limit),
424
- bkprop: 'id'
425
- }
426
-
427
- response = post(params)
428
-
429
- ret = []
430
- response['query']['blocks'].each { |b| ret.push(b['id']) }
431
-
432
- ret
433
- end
434
-
435
- # Gets all page titles that transclude a given page.
436
- # @param page [String] The page name.
437
- # @param limit [Int] See #get_all_images.
438
- # @see https://www.mediawiki.org/wiki/API:Embeddedin MediaWiki Embeddedin
439
- # API Docs
440
- # @since 0.8.0
441
- # @return [Array] All transcluder page titles.
442
- def get_all_transcluders(page, limit = 500)
443
- params = {
444
- action: 'query',
445
- list: 'embeddedin',
446
- eititle: page,
447
- eilimit: get_limited(limit)
448
- }
449
-
450
- response = post(params)
451
-
452
- ret = []
453
- response['query']['embeddedin'].each { |e| ret.push(e['title']) }
454
-
455
- ret
456
- end
457
-
458
- # Gets an array of all deleted or archived files on the wiki.
459
- # @param limit [Int] See #get_all_images
460
- # @see https://www.mediawiki.org/wiki/API:Filearchive MediaWiki
461
- # Filearchive API Docs
462
- # @since 0.8.0
463
- # @return [Array] All deleted file names. These are not titles, so they do
464
- # not include "File:".
465
- def get_all_deleted_files(limit = 500)
466
- params = {
467
- action: 'query',
468
- list: 'filearchive',
469
- falimit: get_limited(limit)
470
- }
471
-
472
- response = post(params)
473
-
474
- ret = []
475
- response['query']['filearchive'].each { |f| ret.push(f['name']) }
476
-
477
- ret
478
- end
479
-
480
- # Gets a list of all protected pages, by protection level if provided.
481
- # @param protection_level [String] The protection level, e.g., sysop
482
- # @param limit [Int] See #get_all_images.
483
- # @see https://www.mediawiki.org/wiki/API:Protectedtitles MediaWiki
484
- # Protectedtitles API Docs
485
- # @since 0.8.0
486
- # @return [Array] All protected page titles.
487
- def get_all_protected_titles(protection_level = nil, limit = 500)
488
- params = {
489
- action: 'query',
490
- list: 'protectedtitles',
491
- ptlimit: get_limited(limit)
492
- }
493
- params[:ptlevel] = protection_level unless protection_level.nil?
494
-
495
- response = post(params)
496
-
497
- ret = []
498
- response['query']['protectedtitles'].each { |t| ret.push(t['title']) }
499
-
500
- ret
501
- end
502
-
503
- # Gets the latest contributions by the user until the limit.
504
- # @param user [String] The username.
505
- # @param limit [Int] See #get_all_images.
506
- # @see https://www.mediawiki.org/wiki/API:Usercontribs MediaWiki
507
- # User Contributions API Docs
508
- # @since 0.8.0
509
- # @return [Hash] Each contribution by its revid, containing the title,
510
- # summary, total contribution size, and the size change relative to the
511
- # previous edit.
512
- def get_user_contributions(user, limit = 500)
513
- params = {
514
- action: 'query',
515
- list: 'usercontribs',
516
- ucuser: user,
517
- uclimit: get_limited(limit),
518
- ucprop: 'ids|title|comment|size|sizediff|flags|patrolled'
519
- }
520
-
521
- response = post(params)
522
-
523
- ret = {}
524
- response['query']['usercontribs'].each do |item|
525
- ret[item['revid']] = {
526
- title: item['title'],
527
- summary: item['comment'],
528
- total_size: item['size'],
529
- diff_size: item['sizediff']
530
- }
531
- end
532
-
533
- ret
534
- end
535
-
536
- # Gets the user's full watchlist. If no user is provided, it will use the
537
- # currently logged in user, according to the MediaWiki API.
538
- # @param user [String] The username.
539
- # @param limit [Int] See #get_all_images.
540
- # @see https://www.mediawiki.org/wiki/API:Watchlist MediaWiki Watchlist
541
- # API Docs
542
- # @since 0.8.0
543
- # @return [Array] All the watchlist page titles.
544
- def get_full_watchlist(user = nil, limit = 500)
545
- params = {
546
- action: 'query',
547
- list: 'watchlist',
548
- wlprop: 'title'
549
- }
550
- params[:wluser] = user unless user.nil?
551
-
552
- response = post(params)
553
-
554
- ret = []
555
- response['query']['watchlist'].each { |t| ret.push(t['title']) }
556
-
557
- ret
558
- end
559
- end
560
- end
561
- end