mediawiki-butt 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/lib/mediawiki/administration.rb +54 -0
- data/lib/mediawiki/auth.rb +95 -87
- data/lib/mediawiki/butt.rb +39 -18
- data/lib/mediawiki/constants.rb +124 -124
- data/lib/mediawiki/edit.rb +85 -23
- data/lib/mediawiki/exceptions.rb +22 -20
- data/lib/mediawiki/query.rb +133 -124
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc43e07782836b5480c6da51986099f1109c1a7d
|
4
|
+
data.tar.gz: 5e8e4ac85f0d95c6e11a522b8a00546b3dbb429a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b770ca63c32c5476df31441d6c8571f20abe7c2234daf003609c3efc1ffb287cfdb659eb2448eea18f24791986e2c12b901a24dcf612a9aeacbf9fd87906f44
|
7
|
+
data.tar.gz: 1778b3fc82b77b5decba13a5fedc779829d50a70800126571646932af0ce2f0954309178cca71d0a3a1a69eb9b0c9714d132a1387531141fe8684b9b2808ee75
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,16 @@
|
|
1
1
|
# Changelog
|
2
2
|
## Version 0
|
3
|
+
### Version 0.5.0
|
4
|
+
* New Administration module for administrative methods.
|
5
|
+
* New block and unblock methods, for (un)blocking users.
|
6
|
+
* Refactor token stuff. It still doesn't work exactly how I'd like yet, but it's better than it was before. Ideally I'd like to have it get the login-specific tokens on login and set them to some well-named instance variables. Then clear those on logout.
|
7
|
+
* Single-line each `do end` loops have been converted into `{...}` style loops.
|
8
|
+
* New delete method for deleting pages.
|
9
|
+
* New move method for moving pages.
|
10
|
+
|
3
11
|
### Version 0.4.1
|
12
|
+
* params[:format] is now automatically set to 'json', so it no longer needs to be defined in each method.
|
13
|
+
* Fixed a lot of styling issues thanks to Rubocop.
|
4
14
|
* check_login and check_create now use case/when statements instead of elsifs.
|
5
15
|
* check_create no longer returns anything.
|
6
16
|
* Update minimum Ruby version to 2.1, for refinements.
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module MediaWiki
|
2
|
+
module Administration
|
3
|
+
# Blocks the user.
|
4
|
+
# @param user [String] The user to block.
|
5
|
+
# @param expiry [String] The expiry timestamp using a relative expiry time.
|
6
|
+
# @param reason [String] The reason to show in the block log.
|
7
|
+
# @param nocreate [Boolean] Whether to allow them to create an account.
|
8
|
+
# @return [String/Int] The error code, or the block ID.
|
9
|
+
def block(user, expiry = '2 weeks', reason = nil, nocreate = true)
|
10
|
+
params = {
|
11
|
+
action: 'block',
|
12
|
+
user: user,
|
13
|
+
expiry: expiry
|
14
|
+
}
|
15
|
+
|
16
|
+
token = get_token('block')
|
17
|
+
params[:reason] = reason unless reason.nil?
|
18
|
+
params[:nocreate] = '1' if nocreate == true
|
19
|
+
params[:token] = token
|
20
|
+
|
21
|
+
response = post(params)
|
22
|
+
puts response
|
23
|
+
|
24
|
+
if !response['error'].nil?
|
25
|
+
return response['error']['code']
|
26
|
+
else
|
27
|
+
return response['id'].to_i
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Unblocks the user.
|
32
|
+
# @param user [String] The user to unblock.
|
33
|
+
# @param reason [String] The reason to show in the block log.
|
34
|
+
# @return [String/Int] The error code, or the block ID.
|
35
|
+
def unblock(user, reason = nil)
|
36
|
+
params = {
|
37
|
+
action: 'unblock',
|
38
|
+
user: user
|
39
|
+
}
|
40
|
+
token = get_token('unblock')
|
41
|
+
params[:reason] = reason unless reason.nil?
|
42
|
+
params[:token] = token
|
43
|
+
|
44
|
+
response = post(params)
|
45
|
+
puts response
|
46
|
+
|
47
|
+
if !response['error'].nil?
|
48
|
+
return response['error']['code']
|
49
|
+
else
|
50
|
+
return response['id'].to_i
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/mediawiki/auth.rb
CHANGED
@@ -2,85 +2,88 @@ require_relative 'exceptions'
|
|
2
2
|
|
3
3
|
module MediaWiki
|
4
4
|
module Auth
|
5
|
-
|
6
|
-
#
|
5
|
+
# Checks the login result for errors. Returns true if it is successful,
|
6
|
+
# else false with an error raised.
|
7
7
|
# @param result [String] The parsed version of the result.
|
8
|
-
# @param secondtry [Boolean] Whether this login is the first or second try.
|
8
|
+
# @param secondtry [Boolean] Whether this login is the first or second try.
|
9
|
+
# False for first, true for second.
|
9
10
|
# @return [Boolean] true if successful, else false.
|
10
11
|
def check_login(result, secondtry)
|
11
12
|
case result
|
12
|
-
when
|
13
|
+
when 'Success'
|
13
14
|
@logged_in = true
|
14
15
|
return true
|
15
|
-
when
|
16
|
+
when 'NeedToken'
|
16
17
|
if secondtry == true
|
17
|
-
|
18
|
+
fail MediaWiki::Butt::NeedTokenMoreThanOnceError
|
18
19
|
return false
|
19
20
|
end
|
20
|
-
when
|
21
|
-
|
21
|
+
when 'NoName'
|
22
|
+
fail MediaWiki::Butt::NoNameError
|
22
23
|
return false
|
23
|
-
when
|
24
|
-
|
24
|
+
when 'Illegal'
|
25
|
+
fail MediaWiki::Butt::IllegalUsernameError
|
25
26
|
return false
|
26
|
-
when
|
27
|
-
|
27
|
+
when 'NotExists'
|
28
|
+
fail MediaWiki::Butt::UsernameNotExistsError
|
28
29
|
return false
|
29
|
-
when
|
30
|
-
|
30
|
+
when 'EmptyPass'
|
31
|
+
fail MediaWiki::Butt::EmptyPassError
|
31
32
|
return false
|
32
|
-
when
|
33
|
-
|
33
|
+
when 'WrongPass'
|
34
|
+
fail MediaWiki::Butt::WrongPassError
|
34
35
|
return false
|
35
|
-
when
|
36
|
-
|
36
|
+
when 'WrongPluginPass'
|
37
|
+
fail MediaWiki::Butt::WrongPluginPassError
|
37
38
|
return false
|
38
|
-
when
|
39
|
-
|
39
|
+
when 'CreateBlocked'
|
40
|
+
fail MediaWiki::Butt::CreateBlockedError
|
40
41
|
return false
|
41
|
-
when
|
42
|
-
|
42
|
+
when 'Throttled'
|
43
|
+
fail MediaWiki::Butt::ThrottledError
|
43
44
|
return false
|
44
|
-
when
|
45
|
-
|
45
|
+
when 'Blocked'
|
46
|
+
fail MediaWiki::Butt::BlockedError
|
46
47
|
return false
|
47
48
|
end
|
48
49
|
end
|
49
50
|
|
50
|
-
# Checks the account creation result's error and raises the corresponding
|
51
|
-
#
|
51
|
+
# Checks the account creation result's error and raises the corresponding
|
52
|
+
# exception.
|
53
|
+
# @param error [String] The parsed error code string
|
52
54
|
def check_create(error)
|
53
55
|
case error
|
54
|
-
when
|
55
|
-
|
56
|
-
when
|
57
|
-
|
58
|
-
when
|
59
|
-
|
60
|
-
when
|
61
|
-
|
62
|
-
when
|
63
|
-
|
64
|
-
when
|
65
|
-
|
66
|
-
when
|
67
|
-
|
68
|
-
when
|
69
|
-
|
70
|
-
when
|
71
|
-
|
72
|
-
when
|
73
|
-
|
74
|
-
when
|
75
|
-
|
76
|
-
when
|
77
|
-
|
78
|
-
when
|
79
|
-
|
56
|
+
when 'noname'
|
57
|
+
fail MediaWiki::Butt::NoNameError
|
58
|
+
when 'userexists'
|
59
|
+
fail MediaWiki::Butt::UserExistsError
|
60
|
+
when 'password-name-match'
|
61
|
+
fail MediaWiki::Butt::UserPassMatchError
|
62
|
+
when 'password-login-forbidden'
|
63
|
+
fail MediaWiki::Butt::PasswordLoginForbiddenError
|
64
|
+
when 'noemailtitle'
|
65
|
+
fail MediaWiki::Butt::NoEmailTitleError
|
66
|
+
when 'invalidemailaddress'
|
67
|
+
fail MediaWiki::Butt::InvalidEmailAddressError
|
68
|
+
when 'passwordtooshort'
|
69
|
+
fail MediaWiki::Butt::PasswordTooShortError
|
70
|
+
when 'noemail'
|
71
|
+
fail MediaWiki::Butt::NoEmailError
|
72
|
+
when 'acct_creation_throttle_hit'
|
73
|
+
fail MediaWiki::Butt::ThrottledError
|
74
|
+
when 'aborted'
|
75
|
+
fail MediaWiki::Butt::AbortedError
|
76
|
+
when 'blocked'
|
77
|
+
fail MediaWiki::Butt::BlockedError
|
78
|
+
when 'permdenied-createaccount'
|
79
|
+
fail MediaWiki::Butt::PermDeniedError
|
80
|
+
when 'createaccount-hook-aborted'
|
81
|
+
fail MediaWiki::Butt::HookAbortedError
|
80
82
|
end
|
81
83
|
end
|
82
84
|
|
83
|
-
# Logs the user into the wiki. This is generally required for editing and
|
85
|
+
# Logs the user into the wiki. This is generally required for editing and
|
86
|
+
# getting restricted data. Will return the result of #check_login
|
84
87
|
# @param username [String] The username
|
85
88
|
# @param password [String] The password
|
86
89
|
# @return [Boolean] True if the login was successful, false if not.
|
@@ -88,39 +91,39 @@ module MediaWiki
|
|
88
91
|
params = {
|
89
92
|
action: 'login',
|
90
93
|
lgname: username,
|
91
|
-
lgpassword: password
|
92
|
-
format: 'json'
|
94
|
+
lgpassword: password
|
93
95
|
}
|
94
96
|
|
95
97
|
result = post(params)
|
96
|
-
if check_login(result[
|
98
|
+
if check_login(result['login']['result'], false)
|
97
99
|
@logged_in = true
|
98
100
|
@tokens.clear
|
99
101
|
true
|
100
|
-
elsif result[
|
101
|
-
|
102
|
+
elsif result['login']['result'] == 'NeedToken' &&
|
103
|
+
!result['login']['token'].nil?
|
104
|
+
token = result['login']['token']
|
102
105
|
token_params = {
|
103
106
|
action: 'login',
|
104
107
|
lgname: username,
|
105
108
|
lgpassword: password,
|
106
|
-
format: 'json',
|
107
109
|
lgtoken: token
|
108
110
|
}
|
109
111
|
|
110
|
-
#Consider refactor the @cookie initialization.
|
111
|
-
@cookie = "#{result[
|
112
|
-
|
113
|
-
|
112
|
+
# Consider refactor the @cookie initialization.
|
113
|
+
@cookie = "#{result['login']['cookieprefix']}" \
|
114
|
+
"Session=#{result['login']['sessionid']}"
|
115
|
+
result = post(token_params, true, 'Set-Cookie' => @cookie)
|
116
|
+
check_login(result['login']['result'], true)
|
114
117
|
end
|
115
118
|
end
|
116
119
|
|
117
120
|
# Logs the current user out.
|
118
|
-
# @return [Boolean] True if it was able to log anyone out, false if not
|
121
|
+
# @return [Boolean] True if it was able to log anyone out, false if not
|
122
|
+
# (basically, if someone was logged in, it returns true).
|
119
123
|
def logout
|
120
124
|
if @logged_in
|
121
125
|
params = {
|
122
|
-
action: 'logout'
|
123
|
-
format: 'json'
|
126
|
+
action: 'logout'
|
124
127
|
}
|
125
128
|
|
126
129
|
post(params)
|
@@ -135,8 +138,11 @@ module MediaWiki
|
|
135
138
|
# Creates an account using the standard procedure.
|
136
139
|
# @param username [String] The desired username.
|
137
140
|
# @param password [String] The desired password.
|
138
|
-
# @param language [String] The language code to be set as default for the
|
139
|
-
#
|
141
|
+
# @param language [String] The language code to be set as default for the
|
142
|
+
# account. Defaults to 'en', or English. Use the language code, not
|
143
|
+
# the name.
|
144
|
+
# @param reason [String] The reason for creating the account, as shown in
|
145
|
+
# the account creation log. Optional.
|
140
146
|
# @return [Boolean] True if successful, false if not.
|
141
147
|
def create_account(username, password, language = 'en', *reason)
|
142
148
|
params = {
|
@@ -148,28 +154,28 @@ module MediaWiki
|
|
148
154
|
}
|
149
155
|
|
150
156
|
result = post(params)
|
151
|
-
|
152
|
-
check_create(result[
|
157
|
+
unless result['error'].nil?
|
158
|
+
check_create(result['error']['code'])
|
153
159
|
return false
|
154
160
|
end
|
155
161
|
|
156
|
-
if result[
|
162
|
+
if result['createaccount']['result'] == 'Success'
|
157
163
|
@tokens.clear
|
158
164
|
return true
|
159
|
-
elsif result[
|
165
|
+
elsif result['createaccount']['result'] == 'NeedToken'
|
160
166
|
params = {
|
161
167
|
name: username,
|
162
168
|
password: password,
|
163
169
|
reason: reason,
|
164
170
|
language: language,
|
165
|
-
token: result[
|
171
|
+
token: result['createaccount']['token']
|
166
172
|
}
|
167
173
|
|
168
174
|
result = post(params, true, true)
|
169
|
-
if result[
|
170
|
-
check_create(result[
|
175
|
+
if !result['error'].nil?
|
176
|
+
check_create(result['error']['code'])
|
171
177
|
return false
|
172
|
-
elsif result[
|
178
|
+
elsif result['createaccount']['result'] == 'Success'
|
173
179
|
return true
|
174
180
|
else
|
175
181
|
return false
|
@@ -180,8 +186,11 @@ module MediaWiki
|
|
180
186
|
# Creates an account using the random password sent by email procedure.
|
181
187
|
# @param username [String] The desired username
|
182
188
|
# @param email [String] The desired email address
|
183
|
-
# @param language [String] The language code to be set as default for the
|
184
|
-
#
|
189
|
+
# @param language [String] The language code to be set as default for the
|
190
|
+
# account. Defaults to 'en', or English. Use the language code, not
|
191
|
+
# the name.
|
192
|
+
# @param reason [String] The reason for creating the account, as shown in
|
193
|
+
# the account creation log. Optional.
|
185
194
|
# @return [Boolean] True if successful, false if not.
|
186
195
|
def create_account_email(username, email, language = 'en', *reason)
|
187
196
|
params = {
|
@@ -194,30 +203,29 @@ module MediaWiki
|
|
194
203
|
}
|
195
204
|
|
196
205
|
result = post(params)
|
197
|
-
result
|
198
|
-
|
199
|
-
check_create(result["error"]["code"])
|
206
|
+
unless result['error'].nil?
|
207
|
+
check_create(result['error']['code'])
|
200
208
|
return false
|
201
209
|
end
|
202
210
|
|
203
|
-
if result[
|
211
|
+
if result['createaccount']['result'] == 'Success'
|
204
212
|
@tokens.clear
|
205
213
|
return true
|
206
|
-
elsif result[
|
214
|
+
elsif result['createaccount']['result'] == 'NeedToken'
|
207
215
|
params = {
|
208
216
|
name: username,
|
209
217
|
email: email,
|
210
218
|
mailpassword: 'value',
|
211
219
|
reason: reason,
|
212
220
|
language: language,
|
213
|
-
token: result[
|
221
|
+
token: result['createaccount']['token']
|
214
222
|
}
|
215
223
|
|
216
224
|
result = post(params, true, true)
|
217
|
-
if result[
|
218
|
-
check_create(result[
|
225
|
+
if !result['error'].nil?
|
226
|
+
check_create(result['error']['code'])
|
219
227
|
return false
|
220
|
-
elsif result[
|
228
|
+
elsif result['createaccount']['result'] == 'Success'
|
221
229
|
return true
|
222
230
|
else
|
223
231
|
return false
|
data/lib/mediawiki/butt.rb
CHANGED
@@ -2,6 +2,7 @@ require_relative 'auth'
|
|
2
2
|
require_relative 'query'
|
3
3
|
require_relative 'constants'
|
4
4
|
require_relative 'edit'
|
5
|
+
require_relative 'administration'
|
5
6
|
require 'httpclient'
|
6
7
|
require 'json'
|
7
8
|
|
@@ -13,10 +14,12 @@ module MediaWiki
|
|
13
14
|
include MediaWiki::Query::Lists
|
14
15
|
include MediaWiki::Constants::Namespaces
|
15
16
|
include MediaWiki::Edit
|
17
|
+
include MediaWiki::Administration
|
16
18
|
|
17
|
-
# Creates a new instance of MediaWiki::Butt. To work with any
|
18
|
-
#
|
19
|
-
# @param url [String] The FULL wiki URL. api.php can be omitted, but it
|
19
|
+
# Creates a new instance of MediaWiki::Butt. To work with any
|
20
|
+
# MediaWiki::Butt methods, you must first create an instance of it.
|
21
|
+
# @param url [String] The FULL wiki URL. api.php can be omitted, but it
|
22
|
+
# will make harsh assumptions about your wiki configuration.
|
20
23
|
# @param use_ssl [Boolean] Whether or not to use SSL. Will default to true.
|
21
24
|
# @return [MediaWiki::Butt] new instance of MediaWiki::Butt
|
22
25
|
def initialize(url, use_ssl = true)
|
@@ -29,18 +32,32 @@ module MediaWiki
|
|
29
32
|
@tokens = {}
|
30
33
|
end
|
31
34
|
|
32
|
-
# Performs a generic HTTP POST action and provides the response. This
|
33
|
-
#
|
34
|
-
#
|
35
|
-
# @param
|
35
|
+
# Performs a generic HTTP POST action and provides the response. This
|
36
|
+
# method generally should not be used by the user, unless there is not a
|
37
|
+
# method provided by the Butt developers for a particular action.
|
38
|
+
# @param params [Hash] A basic hash containing MediaWiki API parameters.
|
39
|
+
# Please see the MediaWiki API for more information.
|
40
|
+
# @param autoparse [Boolean] Whether or not to provide a parsed version
|
41
|
+
# of the response's JSON. Will default to true.
|
36
42
|
# @param header [Hash] The header hash. Optional.
|
37
|
-
# @return [JSON/HTTPMessage] Parsed JSON if autoparse is true, or raw
|
43
|
+
# @return [JSON/HTTPMessage] Parsed JSON if autoparse is true, or raw
|
44
|
+
# response if not.
|
38
45
|
def post(params, autoparse = true, header = nil)
|
39
|
-
# Note that defining the header argument as a splat argument (*header)
|
40
|
-
# We must use header.nil? rather than a
|
41
|
-
#
|
42
|
-
#
|
43
|
-
|
46
|
+
# Note that defining the header argument as a splat argument (*header)
|
47
|
+
# causes errors in HTTPClient. We must use header.nil? rather than a
|
48
|
+
# splat argument and defined? header due to this error. For those
|
49
|
+
# interested, the error is:
|
50
|
+
# undefined method `downcase' for {"Set-Cookie"=>"cookie"}:Hash
|
51
|
+
# This is obvisouly an error in HTTPClient, but we must work around it
|
52
|
+
# until there is a fix in the gem.
|
53
|
+
|
54
|
+
params[:format] = 'json'
|
55
|
+
|
56
|
+
if header.nil?
|
57
|
+
res = @client.post(@uri, params)
|
58
|
+
else
|
59
|
+
res = @client.post(@uri, params, header)
|
60
|
+
end
|
44
61
|
|
45
62
|
if autoparse
|
46
63
|
return JSON.parse(res.body)
|
@@ -49,14 +66,18 @@ module MediaWiki
|
|
49
66
|
end
|
50
67
|
end
|
51
68
|
|
52
|
-
# Returns true if the currently logged in user is in the "bot" group.
|
53
|
-
#
|
54
|
-
#
|
55
|
-
|
69
|
+
# Returns true if the currently logged in user is in the "bot" group.
|
70
|
+
# This can be helpful to some developers, but it is mostly for use
|
71
|
+
# internally in MediaWiki::Butt.
|
72
|
+
# @param username [String] The username to check. Optional. Defaults to
|
73
|
+
# the currently logged in user if nil.
|
74
|
+
# @return [Boolean] true if logged in as a bot, false if not logged in or
|
75
|
+
# logged in as a non-bot
|
76
|
+
def user_bot?(*username)
|
56
77
|
groups = defined? username ? get_usergroups(username) : get_usergroups
|
57
78
|
|
58
79
|
if groups != false
|
59
|
-
return groups.include?(
|
80
|
+
return groups.include?('bot')
|
60
81
|
else
|
61
82
|
return false
|
62
83
|
end
|