mediawiki-butt 0.6.0 → 0.7.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e97e8c9e254cb0248cb26d98b459c20358559351
4
- data.tar.gz: 37596137417cd8e6ddadf842a867ee3034f3c898
3
+ metadata.gz: f3bb16cf6fe52c692e7ca6aea0db8761fa4a3af0
4
+ data.tar.gz: 6e5132d8fcc1972b89ccc3ad6ca666a0fd11446d
5
5
  SHA512:
6
- metadata.gz: 9478f34c90244e78b979521da4f3867a92f9c77cdbe7f326aa92323f1739467d9ed87adb5c722a3e64f6d583eb975365de02fd9de501be6ebca4896deae5e264
7
- data.tar.gz: 5056fa282df5d7581c4a82bd77572abb2d69d49c016960f94dc551cecd1f8d0d18abae5ccacdcaa5529f83ede9880b13a1b107404692352092f611807bc62dc1
6
+ metadata.gz: 6f2b25dfd88316f079dd30055e4b16788a16bc2397a9fa743aac8b6194961dad881628c12c60733e179f53150ab0785c3492865748753fdb48cc232721a570a4
7
+ data.tar.gz: 0fd0998139a5687b931a74b125f9b60ed4aef78bac71544e87fd7c2e295ef155f1c900e349bf09c2011af2849257034f3933d4d74337c37f7af5ae41ce3d7a2e
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
  ## Version 0
3
+ ### Version 0.7.0
4
+ * upload's filename argument is no longer splat, because arrays.
5
+ * Fix incorrect regex $ when ^ should be used in upload.
6
+ * New get_all_categories method.
7
+ * New get_all_images method.
8
+ * Fix some user_bot? calls.
9
+ * user_bot? returns false when not logged in and username is not set.
10
+ * Refactor Query module to have its own folder, and subfolder for meta. This shouldn't change anything on the user's end.
11
+ * A couple methods no longer stupidly print stuff.
12
+ * New get_categories_in_page method.
13
+
3
14
  ### Version 0.6.0
4
15
  * Slightly expanded Gem description.
5
16
  * Finished all Meta modules and their methods, except for the allmessages meta query. [#6](https://github.com/ftb-gamepedia/mediawiki-butt-ruby/issues/6)
@@ -39,7 +50,6 @@
39
50
  * New get_languages method, for getting a hash of all the languages, formatted as code => name.
40
51
  * User-Agent header is now set for each post. It defaults to 'NotLoggedIn/MediaWiki::Butt', or "#{name}/MediaWiki::Butt" if logged in. This might cause some slight performance issues ([#5](https://github.com/FTB-Gamepedia/MediaWiki-Butt-Ruby/issues/5))
41
52
 
42
-
43
53
  ### Version 0.5.0
44
54
  * New Administration module for administrative methods.
45
55
  * New block and unblock methods, for (un)blocking users.
@@ -19,7 +19,6 @@ module MediaWiki
19
19
  params[:token] = token
20
20
 
21
21
  response = post(params)
22
- puts response
23
22
 
24
23
  if !response['error'].nil?
25
24
  return response['error']['code']
@@ -42,7 +41,6 @@ module MediaWiki
42
41
  params[:token] = token
43
42
 
44
43
  response = post(params)
45
- puts response
46
44
 
47
45
  if !response['error'].nil?
48
46
  return response['error']['code']
@@ -1,5 +1,5 @@
1
1
  require_relative 'auth'
2
- require_relative 'query'
2
+ require_relative 'query/query'
3
3
  require_relative 'constants'
4
4
  require_relative 'edit'
5
5
  require_relative 'administration'
@@ -79,7 +79,13 @@ module MediaWiki
79
79
  # @return [Boolean] true if logged in as a bot, false if not logged in or
80
80
  # logged in as a non-bot
81
81
  def user_bot?(*username)
82
- groups = defined? username ? get_usergroups(username) : get_usergroups
82
+ groups = false
83
+
84
+ if defined? username
85
+ groups = get_usergroups(username)
86
+ else
87
+ groups = get_usergroups if @logged_in
88
+ end
83
89
 
84
90
  if groups != false
85
91
  return groups.include?('bot')
@@ -74,17 +74,17 @@ module MediaWiki
74
74
  # the last slash in the URL.
75
75
  # @return [Boolean/String] true if the upload was successful, else the
76
76
  # warning's key. Returns false if the file extension is not valid.
77
- def upload(url, *filename)
77
+ def upload(url, filename = nil)
78
78
  params = {
79
79
  action: 'upload',
80
80
  url: url,
81
81
  format: 'json'
82
82
  }
83
83
 
84
- if defined? filename
85
- filename = filename.sub(/$File:/, '')
86
- else
84
+ if filename.nil?
87
85
  filename = url.split('/')[-1]
86
+ else
87
+ filename = filename.sub(/^File:/, '')
88
88
  end
89
89
 
90
90
  ext = filename.split('.')[-1]
@@ -0,0 +1,388 @@
1
+ require_relative '../constants'
2
+ require 'string-utility'
3
+
4
+ module MediaWiki
5
+ module Query
6
+ module Lists
7
+ using StringUtility
8
+
9
+ # Gets an array of backlinks to a given title.
10
+ # @param title [String] The page to get the backlinks of.
11
+ # @param limit [Int] The maximum number of pages to get. Defaults to 500,
12
+ # and cannot be greater than that unless the user is a bot. If the user
13
+ # is a bot, the limit cannot be greater than 5000.
14
+ # @return [Array] All backlinks until the limit
15
+ def what_links_here(title, limit = 500)
16
+ params = {
17
+ action: 'query',
18
+ bltitle: title
19
+ }
20
+
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
+ ret = []
36
+ response = post(params)
37
+ response['query']['backlinks'].each { |bl| ret.push(bl['title']) }
38
+
39
+ ret
40
+ end
41
+
42
+ # Returns an array of all page titles that belong to a given category.
43
+ # @param category [String] The category title. It can include "Category:",
44
+ # or not, it doesn't really matter because we will add it if it is
45
+ # missing.
46
+ # @param limit [Int] The maximum number of members to get. Defaults to
47
+ # 500, and cannot be greater than that unless the user is a bot.
48
+ # If the user is a bot, the limit cannot be greater than 5000.
49
+ # @return [Array] All category members until the limit
50
+ def get_category_members(category, limit = 500)
51
+ params = {
52
+ action: 'query',
53
+ list: 'categorymembers',
54
+ cmprop: 'title'
55
+ }
56
+
57
+ if category =~ /[Cc]ategory\:/
58
+ params[:cmtitle] = category
59
+ else
60
+ params[:cmtitle] = "Category:#{category}"
61
+ 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
+ ret = []
78
+ response = post(params)
79
+ response['query']['categorymembers'].each { |cm| ret.push(cm['title']) }
80
+
81
+ ret
82
+ end
83
+
84
+ # Returns an array of random pages titles.
85
+ # @param number_of_pages [Int] The number of articles to get.
86
+ # Defaults to 1. Cannot be greater than 10 for normal users,
87
+ # or 20 for bots.
88
+ # @param namespace [Int] The namespace ID. Defaults to
89
+ # 0 (the main namespace).
90
+ # @return [Array] All members
91
+ def get_random_pages(number_of_pages = 1, namespace = 0)
92
+ params = {
93
+ action: 'query',
94
+ list: 'random'
95
+ }
96
+
97
+ if $namespaces.value?(namespace)
98
+ params[:rnnamespace] = namespace
99
+ else
100
+ params[:rnnamespace] = 0
101
+ end
102
+
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
+ ret = []
118
+ responce = post(params)
119
+ responce['query']['random'].each { |a| ret.push(a['title']) }
120
+
121
+ ret
122
+ end
123
+
124
+ # Gets user information. This method should rarely be used by
125
+ # normal users.
126
+ # @param prop [String] The usprop parameter.
127
+ # @param username [String] The username to get info for. Optional.
128
+ # 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.
131
+ def get_userlists(prop, username = nil)
132
+ if username.nil?
133
+ if @logged_in
134
+ response = get_current_user_meta(prop)
135
+ else
136
+ return false
137
+ end
138
+ else
139
+ params = {
140
+ action: 'query',
141
+ list: 'users',
142
+ usprop: prop,
143
+ ususers: username
144
+ }
145
+
146
+ response = post(params)
147
+ end
148
+
149
+ response
150
+ end
151
+
152
+ # Gets an array of all the user's groups.
153
+ # @param username [String] The username to get groups of. Optional.
154
+ # 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.
157
+ def get_usergroups(username = nil)
158
+ ret = []
159
+ if username.nil?
160
+ if @logged_in
161
+ info = get_userlists('groups')
162
+ info['query']['userinfo']['groups'].each { |i| ret.push(i) }
163
+ else
164
+ return false
165
+ end
166
+ else
167
+ info = get_userlists('groups', username)
168
+ info['query']['users'].each do |i|
169
+ i['groups'].each { |g| ret.push(g) }
170
+ end
171
+ end
172
+
173
+ ret
174
+ end
175
+
176
+ # Gets the user rights for the user.
177
+ # @param username [String] The user to get the rights for. Optional.
178
+ # 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.
181
+ def get_userrights(username = nil)
182
+ ret = []
183
+ if username.nil?
184
+ if @logged_in
185
+ info = get_userlists('rights')
186
+ info['query']['userinfo']['rights'].each { |i| ret.push(i) }
187
+ else
188
+ return false
189
+ end
190
+ else
191
+ info = get_userlists('rights', username)
192
+ info['query']['users'].each do |i|
193
+ i['rights'].each do |g|
194
+ ret.push(g)
195
+ end
196
+ end
197
+ end
198
+
199
+ ret
200
+ end
201
+
202
+ # Gets contribution count for the user.
203
+ # @param username [String] The username to get the contribution count of.
204
+ # 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)
212
+ count = nil
213
+ if username.nil?
214
+ if @logged_in
215
+ info = get_userlists('editcount')
216
+ count = info['query']['userinfo']['editcount']
217
+ else
218
+ return false
219
+ end
220
+ else
221
+ info = get_userlists('editcount', username)
222
+ info['query']['users'].each { |i| count = i['editcount'] }
223
+ end
224
+
225
+ if autoparse
226
+ countstring = count.to_s.separate
227
+ return countstring
228
+ end
229
+
230
+ count
231
+ end
232
+
233
+ # Gets when the user registered.
234
+ # @param username [String] The username to get the registration date and
235
+ # time of. Optional. Defaults to the currently logged in user.
236
+ # @return [DateTime] The registration date and time as a DateTime object.
237
+ def get_registration_time(username = nil)
238
+ time = nil
239
+ # Do note that in Userinfo, registration is called registrationdate.
240
+ if username.nil?
241
+ if @logged_in
242
+ info = get_userlists('registrationdate')
243
+ time = info['query']['userinfo']['registrationdate']
244
+ else
245
+ return false
246
+ end
247
+ else
248
+ info = get_userlists('registration', username)
249
+ info['query']['users'].each { |i| time = i['registration'] }
250
+ end
251
+
252
+ # %Y: Year including century
253
+ # %m: Month num
254
+ # %d: day of month
255
+ # %T: Time as HH:MM:SS
256
+ timeformat = '%Y-%m-%dT%T'
257
+ time = DateTime.strptime(time, timeformat)
258
+
259
+ time
260
+ end
261
+
262
+ # Gets the gender for the provded user.
263
+ # @param username [String] The user.
264
+ # @return [String] The gender. 'male', 'female', or 'unknown'.
265
+ def get_user_gender(username)
266
+ gender = nil
267
+ info = get_userlists('gender', username)
268
+ info['query']['users'].each { |i| gender = i['gender'] }
269
+
270
+
271
+ gender
272
+ end
273
+
274
+ # Gets the amount of results for the search value.
275
+ # @param search_value [String] The thing to search for.
276
+ # @param namespace [Int] The namespace to search in.
277
+ # Defaults to 0 (the main namespace).
278
+ # @return [Int] The number of pages that matched the search.
279
+ def get_search_result_amount(search_value, namespace = 0)
280
+ params = {
281
+ action: 'query',
282
+ list: 'search',
283
+ srsearch: search_value
284
+ }
285
+
286
+ if $namespaces.value?(namespace)
287
+ params[:srnamespace] = namespace
288
+ else
289
+ params[:srnamespace] = 0
290
+ end
291
+
292
+ response = post(params)
293
+
294
+ response['query']['searchinfo']['totalhits']
295
+ end
296
+
297
+ # Gets an array containing page titles that matched the search.
298
+ # @param search_value [String] The thing to search for.
299
+ # @param namespace [Int] The namespace to search in.
300
+ # Defaults to 0 (the main namespace).
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 $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
+ # @return [Array] An array of all categories.
327
+ def get_all_categories(limit = 500)
328
+ params = {
329
+ action: 'query',
330
+ list: 'allcategories'
331
+ }
332
+
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
+ response = post(params)
348
+
349
+ ret = []
350
+ response['query']['allcategories'].each { |c| ret.push(c['*']) }
351
+
352
+ ret
353
+ end
354
+
355
+ # Gets all the images on the wiki.
356
+ # @param limit [Int] The maximum number of images to get. Defaults to 500.
357
+ # Cannot be greater than 500 for normal users, or 5000 for bots.
358
+ # @return [Array] An array of all images.
359
+ def get_all_images(limit = 500)
360
+ params = {
361
+ action: 'query',
362
+ list: 'allimages'
363
+ }
364
+
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
377
+ end
378
+
379
+ response = post(params)
380
+
381
+ ret = []
382
+ response['query']['allimages'].each { |i| ret.push(i['name']) }
383
+
384
+ ret
385
+ end
386
+ end
387
+ end
388
+ end