mediawiki-butt 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
1
+ module MediaWiki
2
+ module Query
3
+ module Properties
4
+ module Contributors
5
+ # Gets the total amount of contributors for the given page.
6
+ # @param title [String] The page title.
7
+ # @param limit [Int] The maximum number of users to get. Defaults to 500
8
+ # and cannot be greater than that unless the user is a bot. If the
9
+ # user is a bot, the limit cannot be greater than 5000.
10
+ # @return [Int] The number of contributors to that page.
11
+ def get_total_contributors(title, limit = 500)
12
+ anon_users = get_anonymous_contributors_count(title, limit)
13
+ users = get_logged_in_contributors(title, limit)
14
+
15
+ users.size + anon_users
16
+ end
17
+
18
+ # Gets the non-anonymous contributors for the given page.
19
+ # @param title [String] See #get_total_contributors
20
+ # @param limit [Int] See #get_total_contributors
21
+ # @return [Array] All usernames for the contributors.
22
+ def get_logged_in_contributors(title, limit = 500)
23
+ response = get_contributors_response(title, limit)
24
+ pageid = nil
25
+ response['query']['pages'].each { |r, _| pageid = r }
26
+ ret = []
27
+ if response['query']['pages'][pageid]['missing'] == ''
28
+ return nil
29
+ else
30
+ response['query']['pages'][pageid]['contributors'].each do |c|
31
+ ret.push(c['name'])
32
+ end
33
+ end
34
+
35
+ ret
36
+ end
37
+
38
+ private
39
+
40
+ # Gets the parsed response for the contributors property.
41
+ # @param title [String] See #get_total_contributors
42
+ # @param limit [Int] See #get_total_contributors
43
+ # @return [JSON] See #post
44
+ def get_contributors_response(title, limit = 500)
45
+ params = {
46
+ action: 'query',
47
+ prop: 'contributors',
48
+ titles: title,
49
+ pclimit: MediaWiki::Query.get_limited(limit)
50
+ }
51
+
52
+ post(params)
53
+ end
54
+
55
+ # Gets the total number of anonymous contributors for the given page.
56
+ # @param title [String] See #get_total_contributors
57
+ # @param limit [Int] See #get_total_contributors
58
+ # @return [Int] The number of anonymous contributors for the page.
59
+ def get_anonymous_contributors_count(title, limit = 500)
60
+ response = get_contributors_response(title, limit)
61
+ pageid = nil
62
+ response['query']['pages'].each { |r, _| pageid = r }
63
+ return nil if response['query']['pages'][pageid]['missing'] == ''
64
+
65
+ response['query']['pages'][pageid]['anoncontriburors'].to_i
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,95 @@
1
+ module MediaWiki
2
+ module Query
3
+ module Properties
4
+ module Files
5
+ # Gets the duplicated files of the title.
6
+ # @param title [String] The title to get duplicated files of.
7
+ # @param limit [Int] The maximum number of files to get.
8
+ # @return [Array] Array of all the duplicated file names.
9
+ # @return [Nil] If there aren't any duplicated files.
10
+ def get_duplicated_files_of(title, limit = 500)
11
+ params = {
12
+ action: 'query',
13
+ prop: 'duplicatefiles',
14
+ titles: title,
15
+ dflimit: MediaWiki::Query.get_limited(limit)
16
+ }
17
+
18
+ response = post(params)
19
+ ret = []
20
+ response['query']['pages'].each do |_, c|
21
+ return nil if c['duplicatefiles'].nil?
22
+ c['duplicatefiles'].each do |f|
23
+ ret.push(f['name'])
24
+ end
25
+ end
26
+ ret
27
+ end
28
+
29
+ # Gets all duplicated files on the wiki.
30
+ # @param limit [Int] The maximum number of files to get.
31
+ # @return [Array] All duplicate file titles on the wiki.
32
+ def get_all_duplicated_files(limit = 500)
33
+ params = {
34
+ action: 'query',
35
+ generator: 'allimages',
36
+ prop: 'duplicatefiles',
37
+ dflimit: MediaWiki::Query.get_limited(limit)
38
+ }
39
+
40
+ response = post(params)
41
+ ret = []
42
+ response['query']['pages'].each do |_, c|
43
+ ret.push(c['title'])
44
+ end
45
+ ret
46
+ end
47
+
48
+ # Gets the size of an image in bytes.
49
+ # @param image [String] The image to get info for.
50
+ # @return [Fixnum] The number of bytes.
51
+ # @return [Nil] If the image does not exist.
52
+ def get_image_bytes(image)
53
+ response = get_image_sizes(image)
54
+ return nil if response.nil?
55
+ response['size']
56
+ end
57
+
58
+ # Gets the dimensions of an image as width, height.
59
+ # @param image [String] The image to get info for.
60
+ # @return [Array] The dimensions as width, height.
61
+ # @return [Nil] If the image does not exist.
62
+ def get_image_dimensions(image)
63
+ response = get_image_sizes(image)
64
+ return nil if response.nil?
65
+ [response['width'], response['height']]
66
+ end
67
+
68
+ private
69
+
70
+ # Gets the imageinfo property 'size' for the image.
71
+ # @param image [String] The image to get info for.
72
+ # @return [Hash] A hash of the size, width, and height.
73
+ # @return [Nil] If the image does not exist.
74
+ def get_image_sizes(image)
75
+ params = {
76
+ action: 'query',
77
+ prop: 'imageinfo',
78
+ iiprop: 'size',
79
+ titles: image
80
+ }
81
+
82
+ response = post(params)
83
+ pageid = nil
84
+ response['query']['pages'].each { |r, _| pageid = r }
85
+ return nil if pageid == '-1'
86
+ ret = {}
87
+ response['query']['pages'][pageid]['imageinfo'].each do |i|
88
+ i.each { |k, v| ret[k] = v }
89
+ end
90
+ ret
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,445 @@
1
+ module MediaWiki
2
+ module Query
3
+ module Properties
4
+ module Pages
5
+ # Gets all categories in the page.
6
+ # @param title [String] The page title.
7
+ # @return [Array] All the categories
8
+ # @return [Nil] If the title does not exist.
9
+ def get_categories_in_page(title)
10
+ params = {
11
+ action: 'query',
12
+ prop: 'categories',
13
+ titles: title
14
+ }
15
+
16
+ response = post(params)
17
+ pageid = nil
18
+ ret = []
19
+ response['query']['pages'].each { |r, _| pageid = r }
20
+ if response['query']['pages'][pageid]['missing'] == ''
21
+ return nil
22
+ else
23
+ response['query']['pages'][pageid]['categories'].each do |c|
24
+ ret.push(c['title'])
25
+ end
26
+ end
27
+
28
+ ret
29
+ end
30
+
31
+ # Gets the wiki text for the given page. Returns nil if it for some
32
+ # reason cannot get the text, for example, if the page does not exist.
33
+ # @param title [String] The page title
34
+ # @return [String/nil] String containing page contents.
35
+ # @return [Nil] If the page does not exist.
36
+ def get_text(title)
37
+ params = {
38
+ action: 'query',
39
+ prop: 'revisions',
40
+ rvprop: 'content',
41
+ titles: title
42
+ }
43
+
44
+ response = post(params)
45
+ revid = nil
46
+ response['query']['pages'].each { |r, _| revid = r }
47
+
48
+ if response['query']['pages'][revid]['missing'] == ''
49
+ return nil
50
+ else
51
+ return response['query']['pages'][revid]['revisions'][0]['*']
52
+ end
53
+ end
54
+
55
+ # Gets the revision ID for the given page.
56
+ # @param title [String] The page title
57
+ # @return [Int/nil] The page's ID
58
+ # @return [Nil] If the page does not exist.
59
+ def get_id(title)
60
+ params = {
61
+ action: 'query',
62
+ prop: 'revisions',
63
+ rvprop: 'content',
64
+ titles: title
65
+ }
66
+
67
+ response = post(params)
68
+ response['query']['pages'].each do |revid, _|
69
+ if revid != '-1'
70
+ return revid.to_i
71
+ else
72
+ return nil
73
+ end
74
+ end
75
+ end
76
+
77
+ # Gets all the external links on a given page.
78
+ # @param page [String] The page title.
79
+ # @param limit [Int] The maximum number of members to get. Defaults to
80
+ # 500, and cannot be greater than that unless the user is a bot.
81
+ # If the user is a bot, the limit cannot be greater than 5000.
82
+ # @return [Array] All external link URLs.
83
+ def get_external_links(page, limit = 500)
84
+ params = {
85
+ action: 'query',
86
+ titles: page,
87
+ prop: 'extlinks',
88
+ ellimit: MediaWiki::Query.get_limited(limit)
89
+ }
90
+
91
+ response = post(params)
92
+ ret = []
93
+ response['query']['pages'].each do |revid, _|
94
+ if revid != '-1'
95
+ response['query']['pages'][revid]['extlinks'].each do |l|
96
+ ret.push(l['*'])
97
+ end
98
+ else
99
+ return nil
100
+ end
101
+ end
102
+
103
+ ret
104
+ end
105
+
106
+ # Gets whether the current user watches the page.
107
+ # @param page [String] The page title.
108
+ # @return [Boolean] Whether the user watches the page.
109
+ # @return [Boolean] False if the user is not logged in.
110
+ # @return [Nil] If the page does not exist.
111
+ def do_i_watch?(page)
112
+ if @logged_in
113
+ params = {
114
+ action: 'query',
115
+ titles: page,
116
+ prop: 'info',
117
+ inprop: 'watched'
118
+ }
119
+
120
+ response = post(params)
121
+ response['query']['pages'].each do |revid, _|
122
+ if revid != '-1'
123
+ return response['query']['pages'][revid].key?('watched')
124
+ else
125
+ return nil
126
+ end
127
+ end
128
+ else
129
+ return false
130
+ end
131
+ end
132
+
133
+ # Gets whether the current user (can be anonymous) can read the page.
134
+ # @param page [String] The page title.
135
+ # @return [Boolean] Whether the user can read the page.
136
+ # @return [Nil] If the page does not exist.
137
+ def can_i_read?(page)
138
+ params = {
139
+ action: 'query',
140
+ titles: page,
141
+ prop: 'info',
142
+ inprop: 'readable'
143
+ }
144
+
145
+ response = post(params)
146
+ response['query']['pages'].each do |revid, _|
147
+ if revid != '-1'
148
+ return response['query']['pages'][revid].key?('readable')
149
+ else
150
+ return nil
151
+ end
152
+ end
153
+ end
154
+
155
+ # Gets whether the given page is a redirect.
156
+ # @param page [String] The page title.
157
+ # @return [Boolean] Whether the page is a redirect.
158
+ # @return [Nil] If the page does not exist.
159
+ def page_redirect?(page)
160
+ params = {
161
+ action: 'query',
162
+ titles: page,
163
+ prop: 'info'
164
+ }
165
+
166
+ response = post(params)
167
+ response['query']['pages'].each do |revid, _|
168
+ if revid != '-1'
169
+ return response['query']['pages'][revid].key?('redirect')
170
+ else
171
+ return nil
172
+ end
173
+ end
174
+ end
175
+
176
+ # Gets whether the given page only has one edit.
177
+ # @param page [String] The page title.
178
+ # @return [Boolean] Whether the page only has one edit.
179
+ # @return [Nil] If the page does not exist.
180
+ def page_new?(page)
181
+ params = {
182
+ action: 'query',
183
+ titles: page,
184
+ prop: 'info'
185
+ }
186
+
187
+ response = post(params)
188
+ response['query']['pages'].each do |revid, _|
189
+ if revid != '-1'
190
+ return response['query']['pages'][revid].key?('new')
191
+ else
192
+ return nil
193
+ end
194
+ end
195
+ end
196
+
197
+ # Gets the number of users that watch the given page.
198
+ # @param page [String] The page title.
199
+ # @return [Fixnum] The number of watchers.
200
+ # @return [Nil] If the page does not exist.
201
+ def get_number_of_watchers(page)
202
+ params = {
203
+ action: 'query',
204
+ titles: page,
205
+ prop: 'info',
206
+ inprop: 'watchers'
207
+ }
208
+
209
+ response = post(params)
210
+ response['query']['pages'].each do |revid, _|
211
+ if revid != '-1'
212
+ return response['query']['pages'][revid]['watchers']
213
+ else
214
+ return nil
215
+ end
216
+ end
217
+ end
218
+
219
+ # Gets the way the title is actually displayed, after any in-page
220
+ # changes to its display, e.g., using a template to make the first
221
+ # letter lowercase, in cases like iPhone.
222
+ # @param page [String] The page title.
223
+ # @return [String] The page's display title.
224
+ # @return [Nil] If the page does not exist.
225
+ def get_display_title(page)
226
+ params = {
227
+ action: 'query',
228
+ titles: page,
229
+ prop: 'info',
230
+ inprop: 'displaytitle'
231
+ }
232
+
233
+ response = post(params)
234
+ response['query']['pages'].each do |revid, _|
235
+ if revid != '-1'
236
+ return response['query']['pages'][revid]['displaytitle']
237
+ else
238
+ return nil
239
+ end
240
+ end
241
+ end
242
+
243
+ # Gets the levels of protection on the page.
244
+ # @param page [String] The page title.
245
+ # @return [Array] Hashes of all the protection levels. Each has includes
246
+ # a 'type', a 'level', and an 'expiry'. Type refers to the type of
247
+ # change protected against, like 'edit'. Level refers to the usergroup
248
+ # that is needed to perform that type of edit, like 'sysop'. Expiry
249
+ # refers to when the protection will expire, if never, it will be
250
+ # 'infinity'.
251
+ # @return [Nil] If the page does not exist.
252
+ def get_protection_levels(page)
253
+ params = {
254
+ action: 'query',
255
+ titles: page,
256
+ prop: 'info',
257
+ inprop: 'protection'
258
+ }
259
+
260
+ response = post(params)
261
+ response['query']['pages'].each do |revid, _|
262
+ if revid != '-1'
263
+ protection = response['query']['pages'][revid]['protection']
264
+ protection.each do |p|
265
+ p.keys.each { |k| p[k.to_sym] = p.delete(k) }
266
+ end
267
+ return protection
268
+ else
269
+ return nil
270
+ end
271
+ end
272
+ end
273
+
274
+ # Gets the size, in bytes, of the page.
275
+ # @param page [String] The page title.
276
+ # @return [Fixnum] The number of bytes.
277
+ # @return [Nil] If the page does not exist.
278
+ def get_page_size(page)
279
+ params = {
280
+ action: 'query',
281
+ titles: page,
282
+ prop: 'info'
283
+ }
284
+
285
+ response = post(params)
286
+ response['query']['pages'].each do |revid, _|
287
+ if revid != '-1'
288
+ return response['query']['pages'][revid]['length']
289
+ else
290
+ return nil
291
+ end
292
+ end
293
+ end
294
+
295
+ # Gets all of the images in the given page.
296
+ # @param page [String] The page title.
297
+ # @param limit [Fixnum] See #get_external_links
298
+ # @return [Array] All of the image titles in the page.
299
+ # @return [Nil] If the page does not exist.
300
+ def get_images_in_page(page, limit = 500)
301
+ params = {
302
+ action: 'query',
303
+ prop: 'images',
304
+ titles: page,
305
+ imlimit: MediaWiki::Query.get_limited(limit)
306
+ }
307
+
308
+ response = post(params)
309
+ ret = []
310
+ response['query']['pages'].each do |revid, _|
311
+ if revid != '-1'
312
+ response['query']['pages'][revid]['images'].each do |img|
313
+ ret.push(img['title'])
314
+ end
315
+ else
316
+ return nil
317
+ end
318
+ end
319
+
320
+ ret
321
+ end
322
+
323
+ # Gets all of the templates in the given page.
324
+ # @param page [String] The page title.
325
+ # @param limit [Fixnum] See #get_external_links
326
+ # @return [Array] All of the templte titles in the page.
327
+ # @return [Nil] If the page does not exist.
328
+ def get_templates_in_page(page, limit = 500)
329
+ params = {
330
+ action: 'query',
331
+ prop: 'templates',
332
+ titles: page,
333
+ tllimit: MediaWiki::Query.get_limited(limit)
334
+ }
335
+
336
+ response = post(params)
337
+ ret = []
338
+ response['query']['pages'].each do |revid, _|
339
+ if revid != '-1'
340
+ response['query']['pages'][revid]['templates'].each do |tmp|
341
+ ret.push(tmp['title'])
342
+ end
343
+ else
344
+ return nil
345
+ end
346
+ end
347
+
348
+ ret
349
+ end
350
+
351
+ # Gets all of the interwiki links on the given page.
352
+ # @param page [String] The page title.
353
+ # @param limit [Fixnum] See #get_external_links.
354
+ # @return [Array] All interwiki link titles.
355
+ # @return [Nil] If the page does not exist.
356
+ def get_interwiki_links_in_page(page, limit = 500)
357
+ params = {
358
+ action: 'query',
359
+ prop: 'iwlinks',
360
+ titles: page,
361
+ tllimit: MediaWiki::Query.get_limited(limit)
362
+ }
363
+
364
+ response = post(params)
365
+ ret = []
366
+ response['query']['pages'].each do |revid, _|
367
+ if revid != '-1'
368
+ response['query']['pages'][revid]['iwlinks'].each do |l|
369
+ ret.push(l['*'])
370
+ end
371
+ else
372
+ return nil
373
+ end
374
+ end
375
+
376
+ ret
377
+ end
378
+
379
+ # Gets a hash of data for the page in every language that it is
380
+ # available in. This includes url, language name, autonym, and its
381
+ # title. This method does not work with the Translate extension.
382
+ # @param page [String] The page title.
383
+ # @param limit [Fixnum] See #get_external_links
384
+ # @return [Hash] The data described previously.
385
+ # @return [Nil] If the page does not exist.
386
+ def get_other_langs_of_page(page, limit = 500)
387
+ params = {
388
+ action: 'query',
389
+ prop: 'langlinks',
390
+ titles: page,
391
+ lllimit: MediaWiki::Query.get_limited(limit),
392
+ llprop: 'url|langname|autonym'
393
+ }
394
+
395
+ response = post(params)
396
+ ret = {}
397
+ response['query']['pages'].each do |revid, _|
398
+ if revid != '-1'
399
+ response['query']['pages'][revid]['langlinks'].each do |l|
400
+ ret[l['lang'].to_sym] = {
401
+ url: l['url'],
402
+ langname: l['langname'],
403
+ autonym: l['autonym'],
404
+ title: l['*']
405
+ }
406
+ end
407
+ else
408
+ return nil
409
+ end
410
+ end
411
+
412
+ ret
413
+ end
414
+
415
+ # Gets every single link in a page.
416
+ # @param page [String] The page title.
417
+ # @param limit [Fixnum] See #get_external_links.
418
+ # @return [Array] All link titles.
419
+ # @return [Nil] If the page does not exist.
420
+ def get_all_links_in_page(page, limit = 500)
421
+ params = {
422
+ action: 'query',
423
+ prop: 'links',
424
+ titles: page,
425
+ pllimit: MediaWiki::Query.get_limited(limit)
426
+ }
427
+
428
+ response = post(params)
429
+ ret = []
430
+ response['query']['pages'].each do |revid, _|
431
+ if revid != '-1'
432
+ response['query']['pages'][revid]['links'].each do |l|
433
+ ret.push(l['title'])
434
+ end
435
+ else
436
+ return nil
437
+ end
438
+ end
439
+
440
+ ret
441
+ end
442
+ end
443
+ end
444
+ end
445
+ end