mediawiki-butt 0.2.1 → 0.3.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: 6ae51034d623d7cf169caf9f5047b7c045657efc
4
- data.tar.gz: ff71d9958a84604227b5d97bb57f4dfe23fb28b6
3
+ metadata.gz: a8d1d049adaacdee763a3b3a741ae4d8c7641813
4
+ data.tar.gz: 36994491428f1eb43fc994530267b16a3bcf58a2
5
5
  SHA512:
6
- metadata.gz: 96bb531f1dec544ad84bba39dfa73a13c77b6efbaa7628fb8733972909814e834a700f400490445953f14eb0f2ac2bd3379dece7c87b9294060733bd7fcd1b0d
7
- data.tar.gz: c7d7aaa8f0c2cbef183ac3cc79ba3ba68605e6735a59eb60584fcd24745b88c629ab2f1917dcf20123641db7fe3580ce5b1b7db97df4330b2360447b26084a12
6
+ metadata.gz: 167e862fd9eef9e7212e861c1bad91bca497eaf3c96a3fcd0f16684ea017dbe7bb6d858ac7b29e3760c3a3a2e69062c4dfa22342c54535e2e54197fba2545f20
7
+ data.tar.gz: dc16fd0e84563e8ca0e2db74ac7604e38ff1dda67f94f242a2dedb511ed884ab940a3aad216ade4a931a9b1b8b82b2f9a50116f9b83a8901e87d2c2ad33d76d7
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
  ## Version 0
3
+ ### Version 0.3.0
4
+ * New upload method to upload by URL.
5
+ * New create_page method.
6
+ * New get_userlists method.
7
+ * New get_usergroups method.
8
+ * New get_contrib_count method.
9
+ * Refactor get_usergroups and is_current_user_bot? to use new get_userinfo method.
10
+ * Minor refactors to make optional arguments more Ruby-like with splat arguments.
11
+ * #edit no longer prints the edit token, because that's stupid.
12
+ * #edit no longer sets the summary if it is nil.
13
+
3
14
  ### Version 0.2.1
4
15
  * Fix gemspec. You should actually have the new stuff now.
5
16
 
@@ -40,7 +40,11 @@ module MediaWiki
40
40
  # @param header [Hash] The header hash. Optional.
41
41
  # @return [JSON/HTTPMessage] Parsed JSON if autoparse is true, or raw response if not.
42
42
  def post(params, autoparse = true, header = nil)
43
- if header == nil
43
+ # Note that defining the header argument as a splat argument (*header) causes errors in HTTPClient.
44
+ # We must use header.nil? rather than a splat argument and defined? header due to this error.
45
+ # For those interested, the error is: undefined method `downcase' for {"Set-Cookie"=>"cookie"}:Hash (NoMethodError)
46
+ # This is obvisouly an error in HTTPClient, but we must work around it until there is a fix in the gem.
47
+ if header.nil?
44
48
  response = @client.post(@uri, params)
45
49
  else
46
50
  response = @client.post(@uri, params, header)
@@ -54,10 +58,16 @@ module MediaWiki
54
58
  end
55
59
 
56
60
  # 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.
61
+ # @param username [String] The username to check. Optional. Defaults to the currently logged in user if nil.
57
62
  # @return [Boolean] true if logged in as a bot, false if not logged in or logged in as a non-bot
58
- def is_current_user_bot?
59
- if @logged_in == true
63
+ def is_user_bot?(*username)
64
+ if defined? username
65
+ groups = get_usergroups(username)
66
+ else
60
67
  groups = get_usergroups
68
+ end
69
+
70
+ if groups != false
61
71
  if groups.include? "bot"
62
72
  return true
63
73
  else
@@ -3,23 +3,22 @@ module MediaWiki
3
3
  # Performs a standard edit. It cannot create pages. Please use create_page for that.
4
4
  # @param title [String] The page title.
5
5
  # @param text [String] The new content.
6
- # @param summary [String] The edit summary. Optional.
7
6
  # @param minor [Boolean] Will mark the edit as minor if true. Defaults to false.
8
7
  # @param bot [Boolean] Will mark the edit as bot edit if true. Defualts to true, for your convenience, bot developers.
8
+ # @param summary [String] The edit summary. Optional.
9
9
  # @return [String] The new revision ID, or if it failed, the error code.
10
- def edit(title, text, summary = nil, minor = false, bot = true)
10
+ def edit(title, text, minor = false, bot = true, *summary)
11
11
  params = {
12
12
  action: 'edit',
13
13
  title: title,
14
- summary: summary,
15
14
  text: text,
16
15
  nocreate: 1,
17
16
  format: 'json'
18
17
  }
19
18
 
20
19
  token = get_edit_token(title)
21
- puts token
22
20
 
21
+ params[:summary] = summary if defined? summary
23
22
  params[:minor] = '1' if minor == true
24
23
  params[:bot] = '1' if bot == true
25
24
  params[:token] = token
@@ -32,5 +31,66 @@ module MediaWiki
32
31
  return response["error"]["code"]
33
32
  end
34
33
  end
34
+
35
+ # Creates a new page.
36
+ # @param title [String] The new page's title.
37
+ # @param text [String] The new page's content.
38
+ # @param summary [String] The edit summary. Defaults to "New page".
39
+ # @param bot [Boolean] Will mark the edit as a bot edit if true. Defaults to true, for your convenience, bot developers.
40
+ # @return [String] The new page ID, or if it failed, the error code.
41
+ def create_page(title, text, summary = "New page", bot = true)
42
+ params = {
43
+ action: 'edit',
44
+ title: title,
45
+ summary: summary,
46
+ text: text,
47
+ createonly: 1,
48
+ format: 'json'
49
+ }
50
+
51
+ token = get_edit_token(title)
52
+
53
+ params[:bot] = '1' if bot == true
54
+ params[:token] = token
55
+
56
+ response = post(params)
57
+
58
+ if response["error"]["code"].nil?
59
+ return response["edit"]["pageid"]
60
+ else
61
+ return response["error"]["code"]
62
+ end
63
+ end
64
+
65
+ # Uploads a file from a URL.
66
+ # @param url [String] The URL to the file.
67
+ # @param filename [String] The preferred filename. This can include File: at the beginning, but it will be removed through regex. Optional. If ommitted, it will be everything after the last slash in the URL.
68
+ # @return [Boolean/String] true if the upload was successful, else the warning key.
69
+ def upload(url, *filename)
70
+ params = {
71
+ action: 'upload',
72
+ url: url,
73
+ format: 'json'
74
+ }
75
+
76
+ if defined? filename
77
+ filename = filename.sub(/$File:/, "")
78
+ else
79
+ filename = url.split('/')[-1]
80
+ end
81
+
82
+ token = get_edit_token(filename)
83
+
84
+ params[:filename] = filename
85
+ params[:token] = token
86
+
87
+ response = post(params)
88
+ if response["upload"]["result"] == "Success"
89
+ return true
90
+ elsif response["upload"]["result"] == "Warning"
91
+ warnings = response["upload"]["warnings"]
92
+ return warnings.keys[0]
93
+ end
94
+ end
35
95
  end
36
96
  end
@@ -1,3 +1,5 @@
1
+ require 'string-utility'
2
+
1
3
  module MediaWiki
2
4
  module Query
3
5
  #TODO: Actually decide on a good way to deal with meta information queries.
@@ -21,28 +23,6 @@ module MediaWiki
21
23
  end
22
24
  return ret
23
25
  end
24
-
25
- # Gets an array of all the currently logged in user's groups.
26
- # @return [Array/Boolean] All of the user's groups, or false if not logged in.
27
- def get_usergroups
28
- if @logged_in == true
29
- params = {
30
- action: 'query',
31
- meta: 'userinfo',
32
- uiprop: 'groups',
33
- format: 'json'
34
- }
35
-
36
- result = post(params)
37
- ret = Array.new
38
- result["query"]["userinfo"]["groups"].each do |g|
39
- ret.push(g)
40
- end
41
- return ret
42
- else
43
- return false
44
- end
45
- end
46
26
  end
47
27
 
48
28
  module Properties
@@ -133,7 +113,7 @@ module MediaWiki
133
113
  }
134
114
 
135
115
  if limit > 500
136
- if is_current_user_bot? == true
116
+ if is_user_bot? == true
137
117
  if limit > 5000
138
118
  params[:bllimit] = 5000
139
119
  else
@@ -162,7 +142,7 @@ module MediaWiki
162
142
  params = {
163
143
  action: 'query',
164
144
  list: 'categorymembers',
165
- #cmprop: 'title',
145
+ cmprop: 'title',
166
146
  format: 'json'
167
147
  }
168
148
 
@@ -173,7 +153,7 @@ module MediaWiki
173
153
  end
174
154
 
175
155
  if limit > 500
176
- if is_current_user_bot? == true
156
+ if is_user_bot? == true
177
157
  if limit > 5000
178
158
  params[:cmlimit] = 5000
179
159
  else
@@ -208,7 +188,7 @@ module MediaWiki
208
188
  params[:rnnamespace] = namespace if namespace != nil
209
189
 
210
190
  if namespace > 10
211
- if is_current_user_bot? == true
191
+ if is_user_bot? == true
212
192
  if limit > 20
213
193
  params[:rnlimit] = 20
214
194
  else
@@ -228,6 +208,82 @@ module MediaWiki
228
208
  end
229
209
  return ret
230
210
  end
211
+
212
+ # Gets user information. This method should rarely be used by normal users.
213
+ # @param prop [String] The usprop parameter.
214
+ # @param username [String] The username to get info for. Optional. Defaults to the currently logged in user if ommitted.
215
+ # @return [String/Nil] Parsed full response if successful, nil if the username is nil and the Butt is not logged in.
216
+ def get_userlists(prop, username = nil)
217
+ params = {
218
+ action: 'query',
219
+ list: 'users',
220
+ usprop: prop,
221
+ format: 'json'
222
+ }
223
+
224
+ if username.nil?
225
+ if @logged_in == true
226
+ response = post(params)
227
+ else
228
+ return nil
229
+ end
230
+ else
231
+ params[:ususers] = username
232
+ response = post(params)
233
+ end
234
+
235
+ return response
236
+ end
237
+
238
+ # Gets an array of all the currently logged in user's groups.
239
+ # @param username [String] The username to get groups of. Optional. Defaults to the currently logged in user.
240
+ # @return [Array/Boolean] All of the user's groups, or false if username is nil and Butt is not logged in.
241
+ def get_usergroups(username = nil)
242
+ if username.nil?
243
+ if @logged_in == true
244
+ info = get_userlists('groups')
245
+ else
246
+ return false
247
+ end
248
+ else
249
+ info = get_userlists('groups', username)
250
+ end
251
+
252
+ ret = Array.new
253
+ info["query"]["users"].each do |i|
254
+ i["groups"].each do |g|
255
+ ret.push(g)
256
+ end
257
+ end
258
+ return ret
259
+ end
260
+
261
+ # Gets contribution count for the user.
262
+ # @param username [String] The username to get the contribution count of. Optional. Defaults to the currently logged in user.
263
+ # @param autoparse [Boolean] Whether to automatically format the string with commas. Defaults to true.
264
+ # @return [Boolean/Int/String] False if username is nil and Butt is not logged in. An integer value of the contribution count if autoparse is false. A formatted string version of the contribution count if autoparse is true.
265
+ def get_contrib_count(username = nil, autoparse = true)
266
+ if username.nil?
267
+ if @logged_in == true
268
+ info = get_userlists('editcount')
269
+ else
270
+ return false
271
+ end
272
+ else
273
+ info = get_userlists('editcount', username)
274
+ end
275
+
276
+ count = nil
277
+ info["query"]["users"].each do |i|
278
+ count = i["editcount"]
279
+ end
280
+
281
+ if autoparse == true
282
+ countstring = StringUtility.separate_with(count.to_s)
283
+ return countstring
284
+ end
285
+ return count
286
+ end
231
287
  end
232
288
  end
233
289
  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.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eli Foster
@@ -9,10 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-09-30 00:00:00.000000000 Z
12
+ date: 2015-10-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: httpclient
15
+ name: string-utility
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 1.0.1
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 1.0.1
28
+ - !ruby/object:Gem::Dependency
29
+ name: json
16
30
  requirement: !ruby/object:Gem::Requirement
17
31
  requirements:
18
32
  - - ">="
@@ -26,7 +40,7 @@ dependencies:
26
40
  - !ruby/object:Gem::Version
27
41
  version: '0'
28
42
  - !ruby/object:Gem::Dependency
29
- name: json
43
+ name: httpclient
30
44
  requirement: !ruby/object:Gem::Requirement
31
45
  requirements:
32
46
  - - ">="