mediawiki-butt 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc43e07782836b5480c6da51986099f1109c1a7d
4
- data.tar.gz: 5e8e4ac85f0d95c6e11a522b8a00546b3dbb429a
3
+ metadata.gz: e97e8c9e254cb0248cb26d98b459c20358559351
4
+ data.tar.gz: 37596137417cd8e6ddadf842a867ee3034f3c898
5
5
  SHA512:
6
- metadata.gz: 0b770ca63c32c5476df31441d6c8571f20abe7c2234daf003609c3efc1ffb287cfdb659eb2448eea18f24791986e2c12b901a24dcf612a9aeacbf9fd87906f44
7
- data.tar.gz: 1778b3fc82b77b5decba13a5fedc779829d50a70800126571646932af0ce2f0954309178cca71d0a3a1a69eb9b0c9714d132a1387531141fe8684b9b2808ee75
6
+ metadata.gz: 9478f34c90244e78b979521da4f3867a92f9c77cdbe7f326aa92323f1739467d9ed87adb5c722a3e64f6d583eb975365de02fd9de501be6ebca4896deae5e264
7
+ data.tar.gz: 5056fa282df5d7581c4a82bd77572abb2d69d49c016960f94dc551cecd1f8d0d18abae5ccacdcaa5529f83ede9880b13a1b107404692352092f611807bc62dc1
data/CHANGELOG.md CHANGED
@@ -1,5 +1,45 @@
1
1
  # Changelog
2
2
  ## Version 0
3
+ ### Version 0.6.0
4
+ * Slightly expanded Gem description.
5
+ * Finished all Meta modules and their methods, except for the allmessages meta query. [#6](https://github.com/ftb-gamepedia/mediawiki-butt-ruby/issues/6)
6
+ * New get_variables method.
7
+ * New get_function_hooks method.
8
+ * New get_extension_tags method.
9
+ * New get_skins method.
10
+ * New get_restriction_levels method.
11
+ * New get_restriction_types method.
12
+ * New get_restrictions_data method for the above methods.
13
+ * New get_allowed_file_extensions method, and refactored #upload to only allow files with those extensions.
14
+ * New get_all_usergroups method.
15
+ * New get_magic_words method.
16
+ * New get_special_page_aliases method.
17
+ * New get_namespace_aliases method.
18
+ * New get_namespaces method.
19
+ * New get_filerepo_favicons method.
20
+ * New get_filerepo_thumburls method.
21
+ * New get_nonlocal_filerepos method.
22
+ * New get_local_filerepos method.
23
+ * New get_filerepo_urls method.
24
+ * New get_filerepo_rooturls method.
25
+ * Refactor get_filerepo_names to use new get_filerepoinfo method.
26
+ * New get_filerepoinfo method, in a similar style to get_userlists.
27
+ * New get_current_user_options for getting a hash containing all of the currently logged in user's preferences.
28
+ * New get_email_address method for getting the currently logged in user's email address.
29
+ * New get_realname method for getting the currently logged in user's real name.
30
+ * New get_changeable_groups method for getting the currently logged in user's groups that they can change (add/remove people from)
31
+ * New current_user_hasmsg? method for checking if the user has any unread messages.
32
+ * check_login no longer returns false, ever, because any code after a fail is unreachable.
33
+ * prop parameter in get_current_user_meta is now optional, for get_current_user_name.
34
+ * New get_current_user_name method, for something fairly obvious.
35
+ * New get_siteinfo method, in a similar style to get_userlists.
36
+ * New get_statistics method, for getting a hash of the wiki's statistics.
37
+ * New get_general method, for getting hash of the 'general' wiki information.
38
+ * New get_extensions method, for getting an array of all extension names installed.
39
+ * New get_languages method, for getting a hash of all the languages, formatted as code => name.
40
+ * 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
+
42
+
3
43
  ### Version 0.5.0
4
44
  * New Administration module for administrative methods.
5
45
  * New block and unblock methods, for (un)blocking users.
@@ -7,44 +7,32 @@ module MediaWiki
7
7
  # @param result [String] The parsed version of the result.
8
8
  # @param secondtry [Boolean] Whether this login is the first or second try.
9
9
  # False for first, true for second.
10
- # @return [Boolean] true if successful, else false.
10
+ # @return [Boolean] true if successful. Does not return anything if not.
11
11
  def check_login(result, secondtry)
12
12
  case result
13
13
  when 'Success'
14
14
  @logged_in = true
15
15
  return true
16
16
  when 'NeedToken'
17
- if secondtry == true
18
- fail MediaWiki::Butt::NeedTokenMoreThanOnceError
19
- return false
20
- end
17
+ fail MediaWiki::Butt::NeedTokenMoreThanOnceError if secondtry == true
21
18
  when 'NoName'
22
19
  fail MediaWiki::Butt::NoNameError
23
- return false
24
20
  when 'Illegal'
25
21
  fail MediaWiki::Butt::IllegalUsernameError
26
- return false
27
22
  when 'NotExists'
28
23
  fail MediaWiki::Butt::UsernameNotExistsError
29
- return false
30
24
  when 'EmptyPass'
31
25
  fail MediaWiki::Butt::EmptyPassError
32
- return false
33
26
  when 'WrongPass'
34
27
  fail MediaWiki::Butt::WrongPassError
35
- return false
36
28
  when 'WrongPluginPass'
37
29
  fail MediaWiki::Butt::WrongPluginPassError
38
- return false
39
30
  when 'CreateBlocked'
40
31
  fail MediaWiki::Butt::CreateBlockedError
41
- return false
42
32
  when 'Throttled'
43
33
  fail MediaWiki::Butt::ThrottledError
44
- return false
45
34
  when 'Blocked'
46
35
  fail MediaWiki::Butt::BlockedError
47
- return false
48
36
  end
49
37
  end
50
38
 
@@ -113,7 +101,7 @@ module MediaWiki
113
101
  @cookie = "#{result['login']['cookieprefix']}" \
114
102
  "Session=#{result['login']['sessionid']}"
115
103
  result = post(token_params, true, 'Set-Cookie' => @cookie)
116
- check_login(result['login']['result'], true)
104
+ @name = username if check_login(result['login']['result'], true)
117
105
  end
118
106
  end
119
107
 
@@ -9,7 +9,9 @@ require 'json'
9
9
  module MediaWiki
10
10
  class Butt
11
11
  include MediaWiki::Auth
12
- include MediaWiki::Query::Meta
12
+ include MediaWiki::Query::Meta::SiteInfo
13
+ include MediaWiki::Query::Meta::FileRepoInfo
14
+ include MediaWiki::Query::Meta::UserInfo
13
15
  include MediaWiki::Query::Properties
14
16
  include MediaWiki::Query::Lists
15
17
  include MediaWiki::Constants::Namespaces
@@ -52,13 +54,16 @@ module MediaWiki
52
54
  # until there is a fix in the gem.
53
55
 
54
56
  params[:format] = 'json'
57
+ header = {} if header.nil?
55
58
 
56
- if header.nil?
57
- res = @client.post(@uri, params)
59
+ if @logged_in == false
60
+ header['User-Agent'] = 'NotLoggedIn/MediaWiki::Butt'
58
61
  else
59
- res = @client.post(@uri, params, header)
62
+ header['User-Agent'] = "#{@name}/MediaWiki::Butt"
60
63
  end
61
64
 
65
+ res = @client.post(@uri, params, header)
66
+
62
67
  if autoparse
63
68
  return JSON.parse(res.body)
64
69
  else
@@ -73,7 +73,7 @@ module MediaWiki
73
73
  # through regex. Optional. If ommitted, it will be everything after
74
74
  # the last slash in the URL.
75
75
  # @return [Boolean/String] true if the upload was successful, else the
76
- # warning's key.
76
+ # warning's key. Returns false if the file extension is not valid.
77
77
  def upload(url, *filename)
78
78
  params = {
79
79
  action: 'upload',
@@ -87,15 +87,22 @@ module MediaWiki
87
87
  filename = url.split('/')[-1]
88
88
  end
89
89
 
90
- token = get_token('edit', filename)
91
- params[:filename] = filename
92
- params[:token] = token
90
+ ext = filename.split('.')[-1]
91
+ allowed_extensions = get_allowed_file_extensions
92
+ if allowed_extensions.include? ext
93
93
 
94
- response = post(params)
95
- if response['upload']['result'] == 'Success'
96
- return true
97
- elsif response['upload']['result'] == 'Warning'
98
- return response['upload']['warnings'].keys[0]
94
+ token = get_token('edit', filename)
95
+ params[:filename] = filename
96
+ params[:token] = token
97
+
98
+ response = post(params)
99
+ if response['upload']['result'] == 'Success'
100
+ return true
101
+ elsif response['upload']['result'] == 'Warning'
102
+ return response['upload']['warnings'].keys[0]
103
+ end
104
+ else
105
+ return false
99
106
  end
100
107
  end
101
108
 
@@ -3,41 +3,389 @@ 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
8
- # way. Perhaps we should get hashes instead?
9
6
  module Meta
10
- # Returns an array of all the wiki's file repository names.
11
- # @return [Array] All wiki's file repository names.
12
- def get_filerepo_names
13
- params = {
14
- action: 'query',
15
- meta: 'filerepoinfo',
16
- friprop: 'name'
17
- }
7
+ module SiteInfo
8
+ # Gets wiki information. This method should rarely be used by
9
+ # normal users.
10
+ # @param prop [String] The siprop parameter.
11
+ # @return [Response] Parsed full response.
12
+ def get_siteinfo(prop)
13
+ params = {
14
+ action: 'query',
15
+ meta: 'siteinfo',
16
+ siprop: prop
17
+ }
18
18
 
19
- result = post(params)
20
- ret = []
21
- result['query']['repos'].each { |repo| ret.push(repo['name']) }
19
+ post(params)
20
+ end
22
21
 
23
- ret
22
+ # Gets the statistics for the wiki.
23
+ # @return [Hash] The statistics and their according values.
24
+ def get_statistics
25
+ response = get_siteinfo('statistics')
26
+ ret = {}
27
+ response['query']['statistics'].each { |k, v| ret[k] = v }
28
+ ret
29
+ end
30
+
31
+ # Gets the general information for the wiki.
32
+ # @return [Hash] The general info and their according values.
33
+ def get_general
34
+ response = get_siteinfo('general')
35
+ ret = {}
36
+ response['query']['general'].each { |k, v| ret[k] = v }
37
+ ret
38
+ end
39
+
40
+ # Gets all extensions installed on the wiki.
41
+ # @return [Array] All extension names.
42
+ def get_extensions
43
+ response = get_siteinfo('extensions')
44
+ ret = []
45
+ response['query']['extensions'].each { |e| ret.push(e['name']) }
46
+ ret
47
+ end
48
+
49
+ # Gets all languages and their codes.
50
+ # @return [Hash] All languages. Hash key value pair formatted as
51
+ # code => name.
52
+ def get_languages
53
+ response = get_siteinfo('languages')
54
+ ret = {}
55
+ response['query']['languages'].each { |l| ret[l['code']] = l['*'] }
56
+ ret
57
+ end
58
+
59
+ # Gets all namespaces on the wiki and their IDs. Different from the
60
+ # Namespaces module.
61
+ # @return [Hash] All namespaces, formatted as ID => Name.
62
+ def get_namespaces
63
+ response = get_siteinfo('namespaces')
64
+ ret = {}
65
+ response['query']['namespaces'].each do |id, hash|
66
+ idid = response['query']['namespaces'][id]['id']
67
+ name = response['query']['namespaces'][id]['*']
68
+ ret[idid] = name
69
+ end
70
+ ret
71
+ end
72
+
73
+ # Gets all namespace aliases and their IDs.
74
+ # @return [Hash] All aliases, formatted as ID => Alias.
75
+ def get_namespace_aliases
76
+ response = get_siteinfo('namespacealiases')
77
+ ret = {}
78
+ response['query']['namespacealiases'].each do |a|
79
+ ret[i['id']] = i['*']
80
+ end
81
+ ret
82
+ end
83
+
84
+ # Gets all special page aliases.
85
+ # @return [Hash] All aliases, formatted as RealName => Alias.
86
+ def get_special_page_aliases
87
+ response = get_siteinfo('specialpagealiases')
88
+ ret = {}
89
+ response['query']['specialpagealiases'].each do |a|
90
+ ret[i['realname']] = i['aliases']
91
+ end
92
+ ret
93
+ end
94
+
95
+ # Gets all magic words and their aliases.
96
+ # @return [Hash] All magic words, formatted as Name => Alias.
97
+ def get_magic_words
98
+ response = get_siteinfo('magicwords')
99
+ ret = {}
100
+ response['query']['magicwords'].each do |w|
101
+ ret[w['name']] = w['aliases']
102
+ end
103
+ ret
104
+ end
105
+
106
+ # Gets all user groups total.
107
+ # @return [Hash] All groups, formatted as Name => [Rights].
108
+ def get_all_usergroups
109
+ response = get_siteinfo('usergroups')
110
+ ret = {}
111
+ response['query']['usergroups'].each do |g|
112
+ ret[g['name']] = g['rights']
113
+ end
114
+ ret
115
+ end
116
+
117
+ # Gets all file extensions that are allowed to be uploaded.
118
+ # @return [Array] All file extensions.
119
+ def get_allowed_file_extensions
120
+ response = get_siteinfo('fileextensions')
121
+ ret = []
122
+ response['query']['fileextensions'].each do |e|
123
+ ret.push(e['ext'])
124
+ end
125
+ ret
126
+ end
127
+
128
+ # Gets the response for the restrictions siteinfo API. Not really for
129
+ # use by users, mostly for the other two restriction methods.
130
+ def get_restrictions_data
131
+ response = get_siteinfo('restrictions')
132
+ return response['query']['restrictions']
133
+ end
134
+
135
+ # Gets all restriction/protection types.
136
+ # @return [Array] All protection types.
137
+ def get_restriction_types
138
+ restrictions = get_restrictions_data
139
+ ret = []
140
+ restrictions['types'].each { |t| ret.push(t) }
141
+ ret
142
+ end
143
+
144
+ # Gets all restriction/protection levels.
145
+ # @return [Array] All protection levels.
146
+ def get_restriction_levels
147
+ restrictions = get_restrictions_data
148
+ ret = []
149
+ restrictions['levels'].each { |l| ret.push(l) }
150
+ ret
151
+ end
152
+
153
+ # Gets all skins and their codes.
154
+ # @return [Hash] All skins, formatted as Code => Name
155
+ def get_skins
156
+ response = get_siteinfo('skins')
157
+ ret = {}
158
+ response['query']['skins'].each do |s|
159
+ ret[s['code']] = s['*']
160
+ end
161
+ ret
162
+ end
163
+
164
+ # Gets all HTML tags added by installed extensions.
165
+ # @return [Array] All extension tags.
166
+ def get_extension_tags
167
+ response = get_siteinfo('extensiontags')
168
+ ret = []
169
+ response['query']['extensiontags'].each do |t|
170
+ ret.push(t)
171
+ end
172
+ ret
173
+ end
174
+
175
+ # Gets all function hooks.
176
+ # @return [Array] All function hooks.
177
+ def get_function_hooks
178
+ response = get_siteinfo('functionhooks')
179
+ ret = []
180
+ response['query']['functionhooks'].each do |h|
181
+ ret.push(h)
182
+ end
183
+ ret
184
+ end
185
+
186
+ # Gets all variables that are usable on the wiki, such as NUMBEROFPAGES.
187
+ # @return [Array] All variable string values.
188
+ def get_variables
189
+ response = get_siteinfo('variables')
190
+ ret = []
191
+ response['query']['variables'].each do |v|
192
+ ret.push(v)
193
+ end
194
+ ret
195
+ end
24
196
  end
25
197
 
26
- # Gets meta information for the currently logged in user.
27
- # @param prop [String] The uiprop to get.
28
- # @return [Response/Boolean] Either a full, parsed response, or false if
29
- # not logged in.
30
- def get_current_user_meta(prop)
31
- if @logged_in
198
+ module FileRepoInfo
199
+ # Gets FileRepoInfo for the property.
200
+ # @param prop [String] The friprop to get.
201
+ # @return [Response] The full parsed response.
202
+ def get_filerepoinfo(prop)
32
203
  params = {
33
204
  action: 'query',
34
- meta: 'userinfo',
35
- uiprop: prop
205
+ meta: 'filerepoinfo',
206
+ friprop: prop
36
207
  }
37
208
 
38
- return post(params)
39
- else
40
- return false
209
+ post(params)
210
+ end
211
+
212
+ # Returns an array of all the wiki's file repository names.
213
+ # @return [Array] All wiki's file repository names.
214
+ def get_filerepo_names
215
+ response = get_filerepoinfo('name|displayname')
216
+ ret = {}
217
+ response['query']['repos'].each { |n, dn| ret[n] = dn }
218
+ ret
219
+ end
220
+
221
+ # Gets the root URLs for the file repositories.
222
+ # @return [Hash] A hash containing keys of the names, and values of the
223
+ # root URLs.
224
+ def get_filerepo_rooturls
225
+ response = get_filerepoinfo('name|rootUrl')
226
+ ret = {}
227
+ response['query']['repos'].each { |n, r| ret[n] = r }
228
+ ret
229
+ end
230
+
231
+ # Gets an array containing all local repositories.
232
+ # @return [Array] All repositories that are marked as local.
233
+ def get_local_filerepos
234
+ response = get_filerepoinfo('name|local')
235
+ ret = []
236
+ response['query']['repos'].each do |n, l|
237
+ ret.push(n) if l == 'true'
238
+ end
239
+
240
+ ret
241
+ end
242
+
243
+ # Gets an array containing all repositories that aren't local.
244
+ # @return [Array] All repositories that are not marked as local.
245
+ def get_nonlocal_filerepos
246
+ response = get_filerepoinfo('name|local')
247
+ ret = []
248
+ response['query']['repos'].each do |n, l|
249
+ ret.push(n) if l == 'false'
250
+ end
251
+
252
+ ret
253
+ end
254
+
255
+ # Gets the repository names and their according URLs.
256
+ # @return [Hash] Names as the keys, with their URLs as the values.
257
+ def get_filerepo_urls
258
+ response = get_filerepoinfo('name|url')
259
+ ret = {}
260
+ response['query']['repos'].each { |n, u| ret[n] = u }
261
+ ret
262
+ end
263
+
264
+ # Gets the repository names and their accoring thumbnail URLs.
265
+ # @return [Hash] Names as the keys, with their URLs as the values.
266
+ def get_filerepo_thumburls
267
+ response = get_filerepoinfo('name|thumbUrl')
268
+ ret = {}
269
+ response['query']['repos'].each { |n, u| ret[n] = u }
270
+ ret
271
+ end
272
+
273
+ # Gets the repository names and their according favicon URLs.
274
+ # @return [Hash] Names as the keys, with their favicons as the values.
275
+ def get_filerepo_favicons
276
+ response = get_filerepoinfo('name|favicon')
277
+ ret = {}
278
+ response['query']['repos'].each { |n, f| ret[n] = f }
279
+ ret
280
+ end
281
+ end
282
+
283
+ module UserInfo
284
+ # Gets meta information for the currently logged in user.
285
+ # @param prop [String] The uiprop to get. Optional.
286
+ # @return [Response/Boolean] Either a full, parsed response, or false if
287
+ # not logged in.
288
+ def get_current_user_meta(prop = nil)
289
+ if @logged_in
290
+ params = {
291
+ action: 'query',
292
+ meta: 'userinfo',
293
+ uiprop: prop
294
+ }
295
+
296
+ post(params)
297
+ else
298
+ return false
299
+ end
300
+ end
301
+
302
+ # Gets the current user's username.
303
+ # @return [String/Boolean] Returns the username, or false.
304
+ def get_current_user_name
305
+ if !@name.nil?
306
+ return @name
307
+ else
308
+ name = get_current_user_meta
309
+ if name != false
310
+ name = name['query']['userinfo']['name']
311
+ end
312
+
313
+ name
314
+ end
315
+ end
316
+
317
+ # Returns whether or not the currently logged in user has any unread
318
+ # messages on their talk page.
319
+ # @return [Boolean] True if they have unreads, else false.
320
+ def current_user_hasmsg?
321
+ response = get_current_user_meta('hasmsg')
322
+ if response != false
323
+ if !response['query']['userinfo']['messages'] == ''
324
+ return false
325
+ else
326
+ return true
327
+ end
328
+ else
329
+ return false
330
+ end
331
+ end
332
+
333
+ # Gets a hash-of-arrays containing all the groups the user can add and
334
+ # remove people from.
335
+ # @return [Boolean/Hash] False if get_current_user_meta is false, else
336
+ # a hash containing arrays of all the groups the user can add/remove
337
+ # people from.
338
+ def get_changeable_groups
339
+ response = get_current_user_meta('changeablegroups')
340
+ if response != false
341
+ ret = {}
342
+ add = []
343
+ remove = []
344
+ addself = []
345
+ removeself = []
346
+ changeablegroups = response['query']['userinfo']['changeablegroups']
347
+ puts changeablegroups
348
+ changeablegroups['add'].each { |g| puts g; add.push(g) }
349
+ changeablegroups['remove'].each { |g| puts g; remove.push(g) }
350
+ changeablegroups['add-self'].each { |g| puts g; addself.push(g) }
351
+ changeablegroups['remove-self'].each { |g| puts g; removeself.push(g) }
352
+ ret['add'] = add
353
+ ret['remove'] = remove
354
+ ret['addself'] = addself
355
+ ret['removeself'] = removeself
356
+ return ret
357
+ else
358
+ return false
359
+ end
360
+ end
361
+
362
+ # Gets the currently logged in user's real name.
363
+ # @return [String/Nil] Nil if they don't have a real name set, or their
364
+ # real name.
365
+ def get_realname
366
+ response = get_current_user_meta('realname')
367
+ if response['query']['userinfo']['realname'] == ''
368
+ return nil
369
+ else
370
+ return response['query']['userinfo']['realname']
371
+ end
372
+ end
373
+
374
+ # Gets the currently logged in user's email address.
375
+ # @return [String/Nil] Nil if their email is not set, or their email.
376
+ def get_email_address
377
+ response = get_current_user_meta('email')
378
+ if response['query']['userinfo']['email'] == ''
379
+ return nil
380
+ else
381
+ return response['query']['userinfo']['email']
382
+ end
383
+ end
384
+
385
+ def get_current_user_options
386
+ response = get_current_user_meta('options')
387
+ ret = {}
388
+ response['query']['userinfo']['options'].each { |k, v| ret[k] = v }
41
389
  end
42
390
  end
43
391
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mediawiki-butt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eli Foster
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-10-06 00:00:00.000000000 Z
12
+ date: 2015-10-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: string-utility
@@ -40,7 +40,8 @@ dependencies:
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  description: " MediaWiki::Butt is a Ruby Gem that provides a fully-featured MediaWiki
43
- API interface.\n"
43
+ API interface. It includes methods for changing wiki content, authentication,
44
+ \ and queries.\n\n"
44
45
  email: elifosterwy@gmail.com
45
46
  executables: []
46
47
  extensions: []