mediawiki-butt 0.1.0 → 0.1.1

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: e92d6477086ed671ae506b7dab213c47777e004c
4
- data.tar.gz: 3bcb59b71c5d084cb8f27f365934710e2e60015f
3
+ metadata.gz: 01b96931127f32d0a59b62876f5de8c80657cbc3
4
+ data.tar.gz: 3ef158346ca14fe0073eca30714e48c87b44684d
5
5
  SHA512:
6
- metadata.gz: 4048a9e68a138b23e8412e4c991504fb894a20632f0fe673b4c9b62ee169d03e8a965991e912a61d23c4466a6fe60de97a21b0db2c10f2becad7a727468e5bc9
7
- data.tar.gz: 2cc73a41fae56f3e0c535f589e3ee9fecde5f6b74892e7bd968035739126d818e17f935b8aaf6c2a1494aca5b65aa89369c2dc9d693f086fe1110084eb267d44
6
+ metadata.gz: 47a2a279b141b771ebef8a84702d7f42422ee0baa789e0ef2fd6f74e5b616c2861f9d2509df4fb5565145a1876334095965988fdaebdf3b59c0749c82f3bf862
7
+ data.tar.gz: 220e119d425714af14b50df518e02f6ff9bcacaccf799cb87c0d0ac52322fddf90fbd5a74bf580d44f97d5a7425307cb6bf6af3c600f47a10410e6f8ff25a5df
@@ -1,4 +1,13 @@
1
1
  # Changelog
2
2
  ## Version 0
3
+ ### Version 0.1.1
4
+ * Got rid of pointless array in is_current_user_bot
5
+ * Potentially fixed docs
6
+ * Raise errors on unsuccessful account creation
7
+ * #login properly returns true if the login is successful on the first try
8
+ * #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.
9
+ * Account creation stuff actually returns true/false on success/fail. It also handles errors now.
10
+ * Better Category title regex in get_category_members
11
+
3
12
  ### Version 0.1.0
4
13
  * Initial version.
@@ -3,16 +3,10 @@ require_relative 'exceptions'
3
3
  module MediaWiki
4
4
  module Auth
5
5
 
6
- # Checks the login result for errors. Return true if success, else false, and will raise an error if it is not successful.
7
- #
8
- # ==== Attributes
9
- #
10
- # * +result+ - The parsed version of the result parameter of the login action.
11
- # * +secondtry+ - Whether this login is the first or second try, false for first, true for second.
12
- #
13
- # ==== Examples
14
- #
15
- # This method should not be used by normal users.
6
+ # Checks the login result for errors. Returns true if it is successful, 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. False for first, true for second.
9
+ # @return [Boolean] true if successful, else false.
16
10
  def check_login(result, secondtry)
17
11
  if result == "Success"
18
12
  @logged_in = true
@@ -49,17 +43,57 @@ module MediaWiki
49
43
  return false
50
44
  end
51
45
  end
52
- # Logs the user in to the wiki. This is generally required for editing, or getting restricted data.
53
- # Will return the result of check_login.
54
- #
55
- # ==== Attributes
56
- #
57
- # * +username+ - The desired login handle
58
- # * +password+ - The password of that user
59
- #
60
- # ==== Examples
61
- #
62
- # => butt.login("MyUsername", "My5up3r53cur3P@55w0rd")
46
+
47
+ # Checks the account creation result's error and raises the corresponding error.
48
+ # @param error [String] The parsed error "code" string
49
+ # @return [Boolean] Always false
50
+ def check_create(error)
51
+ if error == "noname"
52
+ raise MediaWiki::Butt::NoNameError
53
+ return false
54
+ elsif error == "userexists"
55
+ raise MediaWiki::Butt::UserExistsError
56
+ return false
57
+ elsif error == "password-name-match"
58
+ raise MediaWiki::Butt::UserPassMatchError
59
+ return false
60
+ elsif error == "password-login-forbidden"
61
+ raise MediaWiki::Butt::PasswordLoginForbiddenError
62
+ return false
63
+ elsif error == "noemailtitle"
64
+ raise MediaWiki::Butt::NoEmailTitleError
65
+ return false
66
+ elsif error == "invalidemailaddress"
67
+ raise MediaWiki::Butt::InvalidEmailAddressError
68
+ return false
69
+ elsif error == "passwordtooshort"
70
+ raise MediaWiki::Butt::PasswordTooShortError
71
+ return false
72
+ elsif error == "noemail"
73
+ raise MediaWiki::Butt::NoEmailError
74
+ return false
75
+ elsif error == "acct_creation_throttle_hit"
76
+ raise MediaWiki::Butt::ThrottledError
77
+ return false
78
+ elsif error == "aborted"
79
+ raise MediaWiki::Butt::AbortedError
80
+ return false
81
+ elsif error == "blocked"
82
+ raise MediaWiki::Butt::BlockedError
83
+ return false
84
+ elsif error == "permdenied-createaccount"
85
+ raise MediaWiki::Butt::PermDeniedError
86
+ return false
87
+ elsif error == "createaccount-hook-aborted"
88
+ raise MediaWiki::Butt::HookAbortedError
89
+ return false
90
+ end
91
+ end
92
+
93
+ # Logs the user into the wiki. This is generally required for editing and getting restricted data. Will return the result of #check_login
94
+ # @param username [String] The username
95
+ # @param password [String] The password
96
+ # @return [Boolean] True if the login was successful, false if not.
63
97
  def login(username, password)
64
98
  params = {
65
99
  action: 'login',
@@ -68,10 +102,11 @@ module MediaWiki
68
102
  format: 'json'
69
103
  }
70
104
 
71
- result = post(params, true, false)
105
+ result = post(params)
72
106
  if check_login(result["login"]["result"], false) == true
73
107
  @logged_in = true
74
108
  @tokens.clear
109
+ return true
75
110
  elsif result["login"]["result"] == "NeedToken" && result["login"]["token"] != nil
76
111
  token = result["login"]["token"]
77
112
  token_params = {
@@ -89,13 +124,8 @@ module MediaWiki
89
124
  end
90
125
  end
91
126
 
92
- # Logs the current user out
93
- #
94
- # ==== Examples
95
- #
96
- # => butt.login("MyUsername", "My5up3r53cur3P@55w0rd")
97
- # => # do stuff
98
- # => butt.logout
127
+ # Logs the current user out.
128
+ # @return [Boolean] True if it was able to log anyone out, false if not (basically, if someone was logged in, it returns true).
99
129
  def logout
100
130
  if @logged_in == true
101
131
  params = {
@@ -105,21 +135,18 @@ module MediaWiki
105
135
  post(params)
106
136
  @logged_in = false
107
137
  @tokens.clear
138
+ return true
139
+ else
140
+ return false
108
141
  end
109
142
  end
110
143
 
111
144
  # Creates an account using the standard procedure.
112
- #
113
- # ==== Attributes
114
- #
115
- # *+username+ - The desired username
116
- # *+password+ - The desired password.
117
- # *+language+ - The language code to set as default for the account being created. Defaults to 'en' or English. Use the language code, not the name.
118
- # *+reason+ - The reason for creating the account, shown in the account creation log. Optional.
119
- #
120
- # ==== Examples
121
- #
122
- # => butt.create_account("MyUser", "password", "es", "MyEmailAddress@MailMain.com", "Quiero un nuevo acuenta sin embargo correocontraseña")
145
+ # @param username [String] The desired username.
146
+ # @param password [String] The desired password.
147
+ # @param language [String] The language code to be set as default for the account. Defaults to 'en', or English. Use the language code, not the name.
148
+ # @param reason [String] The reason for creating the account, as shown in the account creation log. Optional.
149
+ # @return [Boolean] True if successful, false if not.
123
150
  def create_account(username, password, language = 'en', *reason)
124
151
  params = {
125
152
  name: username,
@@ -130,9 +157,14 @@ module MediaWiki
130
157
  }
131
158
 
132
159
  result = post(params)
160
+ if result["error"] != nil
161
+ check_create(result["error"]["code"])
162
+ return false
163
+ end
133
164
 
134
165
  if result["createaccount"]["result"] == "Success"
135
166
  @tokens.clear
167
+ return true
136
168
  elsif result["createaccount"]["result"] == "NeedToken"
137
169
  params = {
138
170
  name: username,
@@ -141,21 +173,25 @@ module MediaWiki
141
173
  language: language,
142
174
  token: result["createaccount"]["token"]
143
175
  }
176
+
177
+ result = post(params, true, true)
178
+ if result["error"] != nil
179
+ check_create(result["error"]["code"])
180
+ return false
181
+ elsif result["createaccount"]["result"] == "Success"
182
+ return true
183
+ else
184
+ return false
185
+ end
144
186
  end
145
187
  end
146
188
 
147
- # Creates an account using the random-password-sent-by-email procedure.
148
- #
149
- # ==== Attributes
150
- #
151
- # *+username+ - The desired username
152
- # *+email+ - The desired email address.
153
- # *+reason+ - The reason for creating the account, shown in the account creation log. Optional.
154
- # *+language+ - The language code to set as default for the account being created. Defaults to 'en' or English. Use the language code, not the name.
155
- #
156
- # ==== Examples
157
- #
158
- # => butt.create_account_email("MyUser", "MyEmailAddress@Whatever.com", "es", "Quiero una nueva acuenta porque quiero a comer caca")
189
+ # Creates an account using the random password sent by email procedure.
190
+ # @param username [String] The desired username
191
+ # @param email [String] The desired email address
192
+ # @param language [String] The language code to be set as default for the account. Defaults to 'en', or English. Use the language code, not the name.
193
+ # @param reason [String] The reason for creating the account, as shown in the account creation log. Optional.
194
+ # @return [Boolean] True if successful, false if not.
159
195
  def create_account_email(username, email, language = 'en', *reason)
160
196
  params = {
161
197
  name: username,
@@ -167,9 +203,15 @@ module MediaWiki
167
203
  }
168
204
 
169
205
  result = post(params)
206
+ result = post(params)
207
+ if result["error"] != nil
208
+ check_create(result["error"]["code"])
209
+ return false
210
+ end
170
211
 
171
212
  if result["createaccount"]["result"] == "Success"
172
213
  @tokens.clear
214
+ return true
173
215
  elsif result["createaccount"]["result"] == "NeedToken"
174
216
  params = {
175
217
  name: username,
@@ -179,6 +221,16 @@ module MediaWiki
179
221
  language: language,
180
222
  token: result["createaccount"]["token"]
181
223
  }
224
+
225
+ result = post(params, true, true)
226
+ if result["error"] != nil
227
+ check_create(result["error"]["code"])
228
+ return false
229
+ elsif result["createaccount"]["result"] == "Success"
230
+ return true
231
+ else
232
+ return false
233
+ end
182
234
  end
183
235
  end
184
236
  end
@@ -9,20 +9,12 @@ module MediaWiki
9
9
  include MediaWiki::Query::Meta
10
10
  include MediaWiki::Query::Properties
11
11
  include MediaWiki::Query::Lists
12
- # Creates a new instance of MediaWiki::Butt
13
- #
14
- # ==== Attributes
15
- #
16
- # * +url+ - The FULL wiki URL. api.php can be omitted, but it will make harsh assumptions about your wiki configuration.
17
- # * +use_ssl+ - Whether or not to use SSL. Will default to true.
18
- #
19
- # ==== Examples
20
- #
21
- # The example below shows an ideal usage of the method.
22
- # => butt = MediaWiki::Butt.new("http://ftb.gamepedia.com/api.php")
12
+
13
+ # Creates a new instance of MediaWiki::Butt. To work with any MediaWiki::Butt methods, you must first create an instance of it.
23
14
  #
24
- # The example below shows a less than idea, but still functional, usage of the method. It is less than ideal because it has to assume that your API page is at /api.php, but it could easily be at /w/api.php, or even /wiki/api.php. It also does not use a secure connection.
25
- # => butt = MediaWiki::Butt.new("http://ftb.gamepedia.com", false)
15
+ # @param url [String] The FULL wiki URL. api.php can be omitted, but it will make harsh assumptions about your wiki configuration.
16
+ # @param use_ssl [Boolean] Whether or not to use SSL. Will default to true.
17
+ # @return [MediaWiki::Butt] new instance of MediaWiki::Butt
26
18
  def initialize(url, use_ssl = true)
27
19
  if url =~ /api.php$/
28
20
  @url = url
@@ -39,15 +31,10 @@ module MediaWiki
39
31
 
40
32
  # Performs a generic HTTP POST action and provides the response. This method generally should not be used by the user, unless there is not a method provided by the Butt developers for a particular action.
41
33
  #
42
- # ==== Attributes
43
- #
44
- # * +params+ - A basic hash containing MediaWiki API parameters. Please see mediawiki.org/wiki/API for more information.
45
- # * +autoparse+ - Whether or not to provide a parsed version of the response's JSON. Will default to true.
46
- # * +setcookie+ - Whether you want to set the auth cookie. Only used in authentication. Defaults to false.
47
- #
48
- # ==== Examples
49
- #
50
- # => login = butt.post({action: 'login', lgname: username, lgpassword: password, format: 'json'})
34
+ # @param params [Hash] A basic hash containing MediaWiki API parameters. Please see the MediaWiki API for more information.
35
+ # @param autoparse [Boolean] Whether or not to provide a parsed version of the response's JSON. Will default to true.
36
+ # @param setcookie [Boolean] Whether you want to set the auth cookie. Only used in authentication. Defaults to false.
37
+ # @return [JSON/Response] Parsed JSON if autoparse is true, or raw response if not.
51
38
  def post(params, autoparse = true, setcookie = false)
52
39
  if setcookie == true
53
40
  header = { 'Set-Cookie' => @cookie }
@@ -63,8 +50,8 @@ module MediaWiki
63
50
  end
64
51
  end
65
52
 
66
- # Returns true if the currently logged in user is in the "bot" group.
67
- # Will return false if the user is either not a bot or if they are not logged in.
53
+ # Returns true if the currently logged in user is in the "bot" group. This can be helpful to some developers, but it is mostly for use internally in MediaWiki::Butt.
54
+ # @return [Boolean] true if logged in as a bot, false if not logged in or logged in as a non-bot
68
55
  def is_current_user_bot()
69
56
  if @logged_in == true
70
57
  params = {
@@ -74,7 +61,6 @@ module MediaWiki
74
61
  format: 'json'
75
62
  }
76
63
 
77
- ret = Array.new
78
64
  response = post(params)
79
65
  response["query"]["userinfo"]["groups"].each do |g|
80
66
  if g == "bot"
@@ -61,5 +61,66 @@ module MediaWiki
61
61
  "User is blocked."
62
62
  end
63
63
  end
64
+
65
+ # Start creation-specific errors
66
+ class UserExistsError < AuthenticationError
67
+ def message
68
+ "Username entered is already in use."
69
+ end
70
+ end
71
+
72
+ class UserPassMatchError < AuthenticationError
73
+ def message
74
+ "Your password must be different from your username."
75
+ end
76
+ end
77
+
78
+ class PasswordLoginForbiddenError < AuthenticationError
79
+ def message
80
+ "The use of this username and password has been forbidden."
81
+ end
82
+ end
83
+
84
+ class NoEmailTitleError < AuthenticationError
85
+ def message
86
+ "No email address."
87
+ end
88
+ end
89
+
90
+ class InvalidEmailAddressError < AuthenticationError
91
+ def message
92
+ "The email address is invalid."
93
+ end
94
+ end
95
+
96
+ class PasswordTooShortError < AuthenticationError
97
+ def message
98
+ "The password was shorter than the value of $wgMinimalPasswordLength"
99
+ end
100
+ end
101
+
102
+ class NoEmailError < AuthenticationError
103
+ def message
104
+ "There is no email address recorded for the user."
105
+ end
106
+ end
107
+
108
+ class AbortedError < AuthenticationError
109
+ def message
110
+ "Aborted by an extension."
111
+ end
112
+ end
113
+
114
+ class PermDeniedError < AuthenticationError
115
+ def message
116
+ "You do not have the right to make an account."
117
+ end
118
+ end
119
+
120
+ class HookAbortedError < AuthenticationError
121
+ def message
122
+ "An extension aborted the account creation."
123
+ end
124
+ end
64
125
  end
65
126
  end
@@ -4,11 +4,8 @@ module MediaWiki
4
4
  # The metainformation could probably be handled in a much less verbose way.
5
5
  module Meta
6
6
 
7
- # Gets all raw names for the wiki's image repositories into an array.
8
- #
9
- # ==== Examples
10
- #
11
- # => names = butt.get_filerepo_names()
7
+ # Returns an array of all the wiki's file repository names.
8
+ # @return [Array] All wiki's file repository names.
12
9
  def get_filerepo_names()
13
10
  params = {
14
11
  action: 'query',
@@ -28,15 +25,9 @@ module MediaWiki
28
25
 
29
26
  module Properties
30
27
 
31
- # Gets the text for the given page. Returns nil if the page does not exist.
32
- #
33
- # ==== Attributes
34
- #
35
- # * +title+ - The page.
36
- #
37
- # ==== Examples
38
- #
39
- # => text = butt.get_text("User:Pendejo")
28
+ # Gets the wiki text for the given page. Returns nil if it for some reason cannot get the text, for example, if the page does not exist. Returns a string.
29
+ # @param title [String] The page title
30
+ # @return [String/nil] String containing page contents, or nil
40
31
  def get_text(title)
41
32
  params = {
42
33
  action: 'query',
@@ -60,19 +51,11 @@ module MediaWiki
60
51
  end
61
52
 
62
53
  module Lists
63
- # Gets the list of backlinks of the title.
64
- #
65
- # ==== Attributes
66
- #
67
- # * +title+ - List pages linking to this title.
68
- # * +limit+ - The maximum number of pages to get. Defaults to 500, and cannot be greater than 5000.
69
- #
70
- # ==== Examples
71
- #
72
- # => backlinks = butt.what_links_here("Title", 5)
73
- # => backlinks.each do |butt|
74
- # => puts butt
75
- # => end
54
+
55
+ # Gets an array of backlinks to a given title.
56
+ # @param title [String] The page to get the backlinks of.
57
+ # @param limit [Int] The maximum number of pages to get. Defaults to 500, and cannot be greater than that unless the user is a bot. If the user is a bot, the limit cannot be greater than 5000.
58
+ # @return [Array] All backlinks until the limit
76
59
  def what_links_here(title, limit = 500)
77
60
  params = {
78
61
  action: 'query',
@@ -101,19 +84,11 @@ module MediaWiki
101
84
  end
102
85
  return ret
103
86
  end
104
- # Returns an array of all pages (titles specifically, because IDs and namespace ints aren't very important) that belong to a given category. See API:Categorymembers
105
- #
106
- # ==== Attributes
107
- #
108
- # * +category+ - The category title.
109
- # * +limit+ - The maximum number of members to get. Defaults to 500, and cannot be greater than 5000.
110
- #
111
- # ==== Examples
112
- #
113
- # => members = butt.get_category_members("Category:Butts", 5000)
114
- # => members.each do |butts|
115
- # => puts butts
116
- # => end
87
+
88
+ # Returns an array of all pagee titles that belong to a given category.
89
+ # @param category [String] The category title. It can include "Category:", or not, it doesn't really matter because we will add it if it is missing.
90
+ # @param limit [Int] The maximum number of members to get. Defaults to 500, and cannot be greater than that unless the user is a bot. If the user is a bot, the limit cannot be greater than 5000.
91
+ # @return [Array] All category members until the limit
117
92
  def get_category_members(category, limit = 500)
118
93
  params = {
119
94
  action: 'query',
@@ -122,7 +97,7 @@ module MediaWiki
122
97
  format: 'json'
123
98
  }
124
99
 
125
- if category =~ /Category\:/
100
+ if category =~ /[Cc]ategory\:/
126
101
  params[:cmtitle] = category
127
102
  else
128
103
  params[:cmtitle] = "Category:#{category}"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mediawiki-butt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eli Foster
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-25 00:00:00.000000000 Z
11
+ date: 2015-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient