mediawiki-butt 0.11.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +28 -5
  3. data/lib/mediawiki/administration.rb +18 -17
  4. data/lib/mediawiki/auth.rb +52 -193
  5. data/lib/mediawiki/butt.rb +51 -32
  6. data/lib/mediawiki/constants.rb +127 -130
  7. data/lib/mediawiki/edit.rb +58 -71
  8. data/lib/mediawiki/exceptions.rb +2 -123
  9. data/lib/mediawiki/query/lists/all.rb +35 -45
  10. data/lib/mediawiki/query/lists/backlinks.rb +23 -34
  11. data/lib/mediawiki/query/lists/categories.rb +17 -28
  12. data/lib/mediawiki/query/lists/log/block.rb +17 -33
  13. data/lib/mediawiki/query/lists/log/delete.rb +8 -24
  14. data/lib/mediawiki/query/lists/log/import.rb +9 -24
  15. data/lib/mediawiki/query/lists/log/log.rb +36 -63
  16. data/lib/mediawiki/query/lists/log/merge.rb +5 -12
  17. data/lib/mediawiki/query/lists/log/move.rb +6 -20
  18. data/lib/mediawiki/query/lists/log/newusers.rb +13 -36
  19. data/lib/mediawiki/query/lists/log/patrol.rb +5 -13
  20. data/lib/mediawiki/query/lists/log/protect.rb +20 -50
  21. data/lib/mediawiki/query/lists/log/rights.rb +10 -24
  22. data/lib/mediawiki/query/lists/log/upload.rb +10 -24
  23. data/lib/mediawiki/query/lists/miscellaneous.rb +10 -19
  24. data/lib/mediawiki/query/lists/querypage.rb +60 -62
  25. data/lib/mediawiki/query/lists/recent_changes.rb +18 -30
  26. data/lib/mediawiki/query/lists/search.rb +14 -29
  27. data/lib/mediawiki/query/lists/users.rb +43 -61
  28. data/lib/mediawiki/query/meta/filerepoinfo.rb +18 -20
  29. data/lib/mediawiki/query/meta/siteinfo.rb +30 -33
  30. data/lib/mediawiki/query/meta/userinfo.rb +48 -66
  31. data/lib/mediawiki/query/properties/contributors.rb +18 -22
  32. data/lib/mediawiki/query/properties/files.rb +18 -20
  33. data/lib/mediawiki/query/properties/pages.rb +129 -211
  34. data/lib/mediawiki/query/properties/properties.rb +20 -25
  35. data/lib/mediawiki/query/query.rb +0 -25
  36. data/lib/mediawiki/utils.rb +9 -8
  37. data/lib/mediawiki/watch.rb +52 -0
  38. metadata +6 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6dbd003fbc5068efe0a5bc3492317db380f0ceb
4
- data.tar.gz: 92d56a75e87f81f30e58368eb4e7406d1b9ed638
3
+ metadata.gz: 16daad9c28f5960f5820657cf6ec6b646976b0c0
4
+ data.tar.gz: f0a165472aad7b3864507a351581abeb40be25a5
5
5
  SHA512:
6
- metadata.gz: b096731fa931d81c5c6eef8cfd2eab9c0ffc71a1af89277b79894a0b5cb6f88a6264bc27d3d9b5a7a33a0fd30dc23a2a4f0fbdcc0ebd511ff553f2301b096fce
7
- data.tar.gz: 3cee7c55b72f0296ec90d0f3198fdc69baabf66c8b7aea6042c6f54b1dd03f38ad49c9c4d28836f2ebf4894b57324055302d65f31becbea13b19a28283fc380e
6
+ metadata.gz: 1d944b95cea6f5b6319db90607a117288ec244def26000ce018b2d14301cc3c11b9c1ed49d36628d39cb8e2e300cbbd47d8437770be12affb7a5526716eeebaf
7
+ data.tar.gz: 0a82a742586f999f7dcbf1055fbbda20ff6c3b22f183cd249c045b0f960be0d36741b273b09d35736ff60700326573ef5d7ae7f2177fc1700f76fd7d2bb1d329
@@ -1,4 +1,27 @@
1
1
  # Changelog
2
+ ## Version 1
3
+ ### Version 1.0.0
4
+ * Important: MediaWiki::Butt now depends on Ruby 2.3. If you aren't using that yet, use it. It's worth it.
5
+ * MediaWiki::Butt.new now takes two parameters, `url`, and an options hash. See the documentation for initialize for more details.
6
+ * Improvements to the way we handle query limits (#28, #31, and #37):
7
+ * `get_recent_revisions`, `get_prefix_search`, and `get_full_watchlist` now properly set their limits.
8
+ * Most query lists that took a limit parameter now use the attribute `:query_limit_default` instead of each having a set default of 500.
9
+ * New `:query_limit_default` attribute accessor. This means you can change your default limits whenever you want!
10
+ * Support using 'max' as the limit.
11
+ * Major cleanup of documentation. The YARD server at ftb-gamepedia.github.io/MediaWiki-Butt-Ruby is now the recommended place for documentation.
12
+ * Improve testing slightly.
13
+ * Remove the TIME_FORMAT constant and instead use DateTime's methods properly.
14
+ * Fix FileRepoInfo methods returning hashes with nil values instead of arrays of hashes (#34).
15
+ * Add support for the MediaWiki Watching/Unwatching APIs (#38)
16
+ * Remove the unused `:tokens` instance variable. This should silence some errors when logging in/out.
17
+ * Improve authentication methods by making them recursive. `login` and `create_account(_email)` now take an optional third parameter, token.
18
+ * Fix error in `get_categories_in_page` (APerson241)
19
+ * Error handling improvements (APerson241 and elifoster) (#32):
20
+ * New EditError that is thrown when there is an error when editing. It contains the error code.
21
+ * `edit` returns false when there is no change (error: nochange), rather than throwing an error.
22
+ * New BlockError that is thrown when there is an error when blocking/unblocking.
23
+ * Removed AuthenticationError subclasses, as well as `check_login` and `check_create`. All authentication errors now return a generic AuthenticationError with the provided error message from MediaWiki.
24
+
2
25
  ## Version 0
3
26
  ### Version 0.11.1
4
27
  * Relicense under the MIT license.
@@ -197,7 +220,7 @@
197
220
  * New get_general method, for getting hash of the 'general' wiki information.
198
221
  * New get_extensions method, for getting an array of all extension names installed.
199
222
  * New get_languages method, for getting a hash of all the languages, formatted as code => name.
200
- * 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))
223
+ * 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))
201
224
 
202
225
  ### Version 0.5.0
203
226
  * New Administration module for administrative methods.
@@ -240,8 +263,8 @@
240
263
  * New get_contrib_count method.
241
264
  * Refactor get_usergroups and is_current_user_bot? to use new get_userinfo method.
242
265
  * Minor refactors to make optional arguments more Ruby-like with splat arguments.
243
- * #edit no longer prints the edit token, because that's stupid.
244
- * #edit no longer sets the summary if it is nil.
266
+ * `#edit` no longer prints the edit token, because that's stupid.
267
+ * `#edit` no longer sets the summary if it is nil.
245
268
 
246
269
  ### Version 0.2.1
247
270
  * Fix gemspec. You should actually have the new stuff now.
@@ -259,8 +282,8 @@
259
282
  * Got rid of pointless array in is_current_user_bot
260
283
  * Potentially fixed docs
261
284
  * Raise errors on unsuccessful account creation
262
- * #login properly returns true if the login is successful on the first try
263
- * #logout returns true/false if it logs the user out. Basically returns true if @logged_in is true, and false if not, because the logout action has no errors.
285
+ * `#login` properly returns true if the login is successful on the first try
286
+ * `#logout` returns true/false if it logs the user out. Basically returns true if @logged_in is true, and false if not, because the logout action has no errors.
264
287
  * Account creation stuff actually returns true/false on success/fail. It also handles errors now.
265
288
  * Better Category title regex in get_category_members
266
289
 
@@ -1,14 +1,15 @@
1
+ require_relative 'exceptions'
2
+
1
3
  module MediaWiki
2
4
  module Administration
3
5
  # Blocks the user.
4
- # @param user [String] The user to block.
6
+ # @param (see #unblock)
5
7
  # @param expiry [String] The expiry timestamp using a relative expiry time.
6
- # @param reason [String] The reason to show in the block log.
7
8
  # @param nocreate [Boolean] Whether to allow them to create an account.
8
9
  # @see https://www.mediawiki.org/wiki/API:Block MediaWiki Block API Docs
9
10
  # @since 0.5.0
10
- # @return [String] The error code.
11
- # @return [Int] The block ID.
11
+ # @raise [BlockError]
12
+ # @return (see #unblock)
12
13
  def block(user, expiry = '2 weeks', reason = nil, nocreate = true)
13
14
  params = {
14
15
  action: 'block',
@@ -17,42 +18,42 @@ module MediaWiki
17
18
  }
18
19
 
19
20
  token = get_token('block')
20
- params[:reason] = reason unless reason.nil?
21
+ params[:reason] = reason if reason
21
22
  params[:nocreate] = '1' if nocreate
22
23
  params[:token] = token
23
24
 
24
25
  response = post(params)
25
26
 
26
- if !response['error'].nil?
27
- return response['error']['code']
28
- else
29
- return response['id'].to_i
27
+ if response.key?('error')
28
+ raise MediaWiki::Butt::BlockError.new(response.dig('error', 'code') || 'Unknown error code')
30
29
  end
30
+
31
+ response['id'].to_i
31
32
  end
32
33
 
33
34
  # Unblocks the user.
34
- # @param user [String] The user to unblock.
35
+ # @param user [String] The user affected.
35
36
  # @param reason [String] The reason to show in the block log.
36
37
  # @see https://www.mediawiki.org/wiki/API:Block MediaWiki Block API Docs
37
38
  # @since 0.5.0
38
- # @return [String] The error code.
39
- # @return [Int] The block ID.
39
+ # @raise [BlockError]
40
+ # @return [Fixnum] The block ID.
40
41
  def unblock(user, reason = nil)
41
42
  params = {
42
43
  action: 'unblock',
43
44
  user: user
44
45
  }
45
46
  token = get_token('unblock')
46
- params[:reason] = reason unless reason.nil?
47
+ params[:reason] = reason if reason
47
48
  params[:token] = token
48
49
 
49
50
  response = post(params)
50
51
 
51
- if !response['error'].nil?
52
- return response['error']['code']
53
- else
54
- return response['id'].to_i
52
+ if response.key?('error')
53
+ raise MediaWiki::Butt::BlockError.new(response.dig('error', 'code') || 'Unknown error code')
55
54
  end
55
+
56
+ response['id'].to_i
56
57
  end
57
58
  end
58
59
  end
@@ -2,153 +2,48 @@ require_relative 'exceptions'
2
2
 
3
3
  module MediaWiki
4
4
  module Auth
5
- # Checks the login result for errors. Returns true if it is successful,
6
- # else false with an error raised.
7
- # @param result [String] The parsed version of the result.
8
- # @param secondtry [Boolean] Whether this login is the first or second try.
9
- # False for first, true for second.
10
- # @raise [NeedTokenMoreThanOnceError] When secondtry is true and the wiki
11
- # still returns 'NeedToken'
12
- # @raise [NoNameError] When the username is nil or undefined.
13
- # @raise [IllegalUsernameError] When the username is illegal.
14
- # @raise [UsernameNotExistsError] When the username does not exist.
15
- # @raise [EmptyPassError] When the password is nil or undefined.
16
- # @raise [WrongPassError] When the password is incorrect.
17
- # @raise [WrongPluginPassError] When an authentication plugin, not
18
- # MediaWiki, claims that the password is incorrect.
19
- # @raise [CreateBlockedError] When the wiki tries to automatically create
20
- # an account, but the user's IP address is already blocked.
21
- # @raise [ThrottledError] When the user has logged in, or tried to, too
22
- # much in a particular amount of time.
23
- # @raise [BlockedError] When the user is blocked from the wiki.
24
- # @since 0.1.0
25
- # @return [Boolean] true if successful. Does not return anything otherwise.
26
- def check_login(result, secondtry)
27
- case result
28
- when 'Success'
29
- @logged_in = true
30
- return true
31
- when 'NeedToken'
32
- fail MediaWiki::Butt::NeedTokenMoreThanOnceError if secondtry
33
- when 'NoName'
34
- fail MediaWiki::Butt::NoNameError
35
- when 'Illegal'
36
- fail MediaWiki::Butt::IllegalUsernameError
37
- when 'NotExists'
38
- fail MediaWiki::Butt::UsernameNotExistsError
39
- when 'EmptyPass'
40
- fail MediaWiki::Butt::EmptyPassError
41
- when 'WrongPass'
42
- fail MediaWiki::Butt::WrongPassError
43
- when 'WrongPluginPass'
44
- fail MediaWiki::Butt::WrongPluginPassError
45
- when 'CreateBlocked'
46
- fail MediaWiki::Butt::CreateBlockedError
47
- when 'Throttled'
48
- fail MediaWiki::Butt::ThrottledError
49
- when 'Blocked'
50
- fail MediaWiki::Butt::BlockedError
51
- end
52
- end
53
-
54
- # Checks the account creation result's error and raises the corresponding
55
- # exception.
56
- # @param error [String] The parsed error code string
57
- # @raise [NoNameError] When the username was either not provided or is
58
- # invalid.
59
- # @raise [UserExistsError] When the username is already in use.
60
- # @raise [UserPassMatchError] When the username and password are identical.
61
- # @raise [PasswordLoginForbiddenError] When the use of the username and
62
- # password has been forbidden.
63
- # @raise [NoEmailTitleError] When there is no provided email address.
64
- # @raise [InvalidEmailAddressError] When the email address has an invalid
65
- # format.
66
- # @raise [PasswordTooShortError] When the password is shorter than the
67
- # $wgMinimumPasswordLength option.
68
- # @raise [NoEmailError] When there is no email address set for the user.
69
- # @raise [ThrottledError] When the user has created too many accounts in one
70
- # day.
71
- # @raise [AbortedError] When an extension has aborted this action.
72
- # @raise [BlockedError] When the IP or logged in user is blocked.
73
- # @raise [PermDeniedError] When the user does not have the right to create
74
- # accounts.
75
- # @raise [HookAbortedError] Same as AbortedError.
76
- # @since 0.1.1
77
- # @return [void]
78
- def check_create(error)
79
- case error
80
- when 'noname'
81
- fail MediaWiki::Butt::NoNameError
82
- when 'userexists'
83
- fail MediaWiki::Butt::UserExistsError
84
- when 'password-name-match'
85
- fail MediaWiki::Butt::UserPassMatchError
86
- when 'password-login-forbidden'
87
- fail MediaWiki::Butt::PasswordLoginForbiddenError
88
- when 'noemailtitle'
89
- fail MediaWiki::Butt::NoEmailTitleError
90
- when 'invalidemailaddress'
91
- fail MediaWiki::Butt::InvalidEmailAddressError
92
- when 'passwordtooshort'
93
- fail MediaWiki::Butt::PasswordTooShortError
94
- when 'noemail'
95
- fail MediaWiki::Butt::NoEmailError
96
- when 'acct_creation_throttle_hit'
97
- fail MediaWiki::Butt::ThrottledError
98
- when 'aborted'
99
- fail MediaWiki::Butt::AbortedError
100
- when 'blocked'
101
- fail MediaWiki::Butt::BlockedError
102
- when 'permdenied-createaccount'
103
- fail MediaWiki::Butt::PermDeniedError
104
- when 'createaccount-hook-aborted'
105
- fail MediaWiki::Butt::HookAbortedError
106
- end
107
- end
108
-
109
- # Logs the user into the wiki. This is generally required for editing and
110
- # getting restricted data. Will return the result of #check_login
5
+ # Logs the user into the wiki. This is generally required for editing and getting restricted data.
111
6
  # @param username [String] The username
112
7
  # @param password [String] The password
113
- # @see check_login
8
+ # @param token [String] The login token to use. Only set this if you know what you are doing. You probably want
9
+ # to just let the function get the token and set it on its own.
10
+ # @see #check_login
114
11
  # @see https://www.mediawiki.org/wiki/API:Login MediaWiki Login API Docs
115
12
  # @since 0.1.0
116
- # @return [Boolean] True if the login was successful, false if not.
117
- def login(username, password)
13
+ # @raise [AuthenticationError]
14
+ # @return [Boolean] True if the login was successful.
15
+ def login(username, password, token = nil)
118
16
  params = {
119
17
  action: 'login',
120
18
  lgname: username,
121
19
  lgpassword: password
122
20
  }
21
+ params[:lgtoken] = token if token
22
+ header = {}
23
+ header['Set-Cookie'] = @cookie if @cookie
123
24
 
124
- result = post(params)
125
- if check_login(result['login']['result'], false)
126
- @logged_in = true
127
- @tokens.clear
128
- true
129
- elsif result['login']['result'] == 'NeedToken' &&
130
- !result['login']['token'].nil?
131
- token = result['login']['token']
132
- token_params = {
133
- action: 'login',
134
- lgname: username,
135
- lgpassword: password,
136
- lgtoken: token
137
- }
25
+ response = post(params, true, header)
26
+ result = response['login']['result']
138
27
 
139
- # Consider refactor the @cookie initialization.
140
- @cookie = "#{result['login']['cookieprefix']}" \
141
- "Session=#{result['login']['sessionid']}"
142
- result = post(token_params, true, 'Set-Cookie' => @cookie)
143
- @name = username if check_login(result['login']['result'], true)
28
+ if result == 'NeedToken'
29
+ token = response['login']['token']
30
+ return login(username, password, token)
144
31
  end
32
+
33
+ if result == 'Success'
34
+ @cookie = "#{response['login']['cookieprefix']}Session=#{response['login']['sessionid']}"
35
+ @name = username
36
+ @logged_in = true
37
+ return true
38
+ end
39
+
40
+ raise MediaWiki::Butt::AuthenticationError.new(result)
145
41
  end
146
42
 
147
43
  # Logs the current user out.
148
44
  # @see https://www.mediawiki.org/wiki/API:Logout MediaWiki Logout API Docs
149
45
  # @since 0.1.0
150
- # @return [Boolean] True if it was able to log anyone out, false if not
151
- # (basically, if someone was logged in, it returns true).
46
+ # @return [Boolean] True if it was able to log anyone out, false if not.
152
47
  def logout
153
48
  if @logged_in
154
49
  params = {
@@ -157,117 +52,81 @@ module MediaWiki
157
52
 
158
53
  post(params)
159
54
  @logged_in = false
160
- @tokens.clear
161
- return true
55
+
56
+ true
162
57
  else
163
- return false
58
+ false
164
59
  end
165
60
  end
166
61
 
167
62
  # Creates an account using the standard procedure.
168
63
  # @param username [String] The desired username.
169
64
  # @param password [String] The desired password.
170
- # @param language [String] The language code to be set as default for the
171
- # account. Defaults to 'en', or English. Use the language code, not
172
- # the name.
173
- # @param reason [String] The reason for creating the account, as shown in
174
- # the account creation log. Optional.
175
- # @see check_create
176
- # @see https://www.mediawiki.org/wiki/API:Account_creation MediaWiki Account
177
- # Creation Docs
65
+ # @param language [String] The language code to be set as default for the account. Defaults to 'en', or English.
66
+ # Use the language code, not the name.
67
+ # @param reason [String] The reason for creating the account, as shown in the account creation log. Optional.
68
+ # @param token [String] The account creation token to use. Only set this if you know what you are doing. You
69
+ # probably want to just let the function get the token and set it on its own.
70
+ # @see #check_create
71
+ # @see https://www.mediawiki.org/wiki/API:Account_creation MediaWiki Account Creation Docs
178
72
  # @since 0.1.0
179
73
  # @return [Boolean] True if successful, false if not.
180
- def create_account(username, password, language = 'en', reason = nil)
74
+ def create_account(username, password, language = 'en', reason = nil, token = '')
181
75
  params = {
182
76
  name: username,
183
77
  password: password,
184
78
  language: language,
185
- token: ''
79
+ token: token
186
80
  }
187
81
  params[:reason] = reason unless reason.nil?
188
82
 
189
83
  result = post(params)
190
84
  unless result['error'].nil?
191
- check_create(result['error']['code'])
192
- return false
85
+ raise MediaWiki::Butt::AuthenticationError.new(result['error']['code'])
193
86
  end
194
87
 
195
88
  if result['createaccount']['result'] == 'Success'
196
89
  @tokens.clear
90
+
197
91
  return true
198
92
  elsif result['createaccount']['result'] == 'NeedToken'
199
- params = {
200
- name: username,
201
- password: password,
202
- reason: reason,
203
- language: language,
204
- token: result['createaccount']['token']
205
- }
206
-
207
- result = post(params, true, true)
208
- if !result['error'].nil?
209
- check_create(result['error']['code'])
210
- return false
211
- elsif result['createaccount']['result'] == 'Success'
212
- return true
213
- else
214
- return false
215
- end
93
+ return create_account(username, password, language, reason, result['createaccount']['token'])
216
94
  end
95
+
96
+ false
217
97
  end
218
98
 
219
99
  # Creates an account using the random password sent by email procedure.
220
- # @param username [String] The desired username
221
100
  # @param email [String] The desired email address
222
- # @param language [String] The language code to be set as default for the
223
- # account. Defaults to 'en', or English. Use the language code, not
224
- # the name.
225
- # @param reason [String] The reason for creating the account, as shown in
226
- # the account creation log. Optional.
227
- # @see check_create
228
- # @see https://www.mediawiki.org/wiki/API:Account_creation MediaWiki Account
229
- # Creation Docs
101
+ # @param (see #create_account)
102
+ # @see #check_create
103
+ # @see https://www.mediawiki.org/wiki/API:Account_creation MediaWiki Account Creation Docs
230
104
  # @since 0.1.0
231
105
  # @return [Boolean] True if successful, false if not.
232
- def create_account_email(username, email, language = 'en', reason = nil)
106
+ def create_account_email(username, email, language = 'en', reason = nil, token = '')
233
107
  params = {
234
108
  name: username,
235
109
  email: email,
236
110
  mailpassword: 'value',
237
111
  language: language,
238
- token: ''
112
+ token: token
239
113
  }
240
114
  params[:reason] = reason unless reason.nil?
241
115
 
242
116
  result = post(params)
243
117
  unless result['error'].nil?
244
- check_create(result['error']['code'])
245
- return false
118
+ raise MediaWiki::Butt::AuthenticationError.new(result['error']['code'])
246
119
  end
247
120
 
248
121
  if result['createaccount']['result'] == 'Success'
249
122
  @tokens.clear
123
+
250
124
  return true
251
125
  elsif result['createaccount']['result'] == 'NeedToken'
252
- params = {
253
- name: username,
254
- email: email,
255
- mailpassword: 'value',
256
- reason: reason,
257
- language: language,
258
- token: result['createaccount']['token']
259
- }
260
-
261
- result = post(params, true, true)
262
- if !result['error'].nil?
263
- check_create(result['error']['code'])
264
- return false
265
- elsif result['createaccount']['result'] == 'Success'
266
- return true
267
- else
268
- return false
269
- end
126
+ return create_account_email(username, email, language, reason, result['createaccount']['token'])
270
127
  end
128
+
129
+ false
271
130
  end
272
131
  end
273
132
  end