mediawiki-butt 1.0.0 → 1.1.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: 16daad9c28f5960f5820657cf6ec6b646976b0c0
4
- data.tar.gz: f0a165472aad7b3864507a351581abeb40be25a5
3
+ metadata.gz: 7f401612e4a15f409ee096a93af7db54d7b1a622
4
+ data.tar.gz: 95f0582b2eb5c83c84ed37c911e2b17ae3f1684a
5
5
  SHA512:
6
- metadata.gz: 1d944b95cea6f5b6319db90607a117288ec244def26000ce018b2d14301cc3c11b9c1ed49d36628d39cb8e2e300cbbd47d8437770be12affb7a5526716eeebaf
7
- data.tar.gz: 0a82a742586f999f7dcbf1055fbbda20ff6c3b22f183cd249c045b0f960be0d36741b273b09d35736ff60700326573ef5d7ae7f2177fc1700f76fd7d2bb1d329
6
+ metadata.gz: 74a04d30b93a548662119f85118ba0b6261e7b8ed4bab1d57db7e8b0303cdee2e19d4b23548c9a5d499a4e16dde1554e37855b68a542ff5e70028f27063d1251
7
+ data.tar.gz: 4eaaa0362c1620c9e62c928f8d7916c204514b4b6ef745b12555cfdd6a20f1eda21f4a603cbbabb6b3618fe77a19958fc483cd82ed86b2d3235e2ec017986404
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
  ## Version 1
3
+ ### Version 1.1.0
4
+ * Support for the continuation API (Issue #9 and PR #47)
5
+
3
6
  ### Version 1.0.0
4
7
  * Important: MediaWiki::Butt now depends on Ruby 2.3. If you aren't using that yet, use it. It's worth it.
5
8
  * MediaWiki::Butt.new now takes two parameters, `url`, and an options hash. See the documentation for initialize for more details.
@@ -22,6 +22,7 @@ module MediaWiki
22
22
  include MediaWiki::Watch
23
23
 
24
24
  attr_accessor :query_limit_default
25
+ attr_accessor :use_continuation
25
26
 
26
27
  # Creates a new instance of MediaWiki::Butt.
27
28
  # @param url [String] The FULL wiki URL. api.php can be omitted, but it will make harsh assumptions about
@@ -32,6 +33,7 @@ module MediaWiki
32
33
  # the various query methods. In other words, if you pass a limit parameter to the valid query methods, it will
33
34
  # use that, otherwise, it will use this. Defaults to 500. It can be set to 'max' to use MW's default max for
34
35
  # each API.
36
+ # @option opts [Boolean] :use_continuation Whether to use the continuation API on queries.
35
37
  def initialize(url, opts = {})
36
38
  @url = url =~ /api.php$/ ? url : "#{url}/api.php"
37
39
  @query_limit_default = opts[:query_limit_default] || 500
@@ -39,6 +41,7 @@ module MediaWiki
39
41
  @uri = URI.parse(@url)
40
42
  @logged_in = false
41
43
  @custom_agent = opts[:custom_agent]
44
+ @use_continuation = opts[:use_continuation]
42
45
  end
43
46
 
44
47
  # Performs a generic HTTP POST request and provides the response. This method generally should not be used by the
@@ -62,6 +65,39 @@ module MediaWiki
62
65
  autoparse ? JSON.parse(res.body) : res
63
66
  end
64
67
 
68
+ # Performs a Mediawiki API query and provides the response, dealing with continuation as necessary.
69
+ # @param params [Hash] A hash containing MediaWiki API parameters.
70
+ # @param base_return [Any] The return value to start with. Defaults to an empty array.
71
+ # @yield [base_return, query] Provides the value provided to the base_return parameter, and the value in the
72
+ # 'query' key in the API response. See the API documentation for more information on this. If the base_return
73
+ # value is modified in the block, its modifications will persist across each continuation loop.
74
+ # @return [Any] The base_return value as modified by the yielded block.
75
+ def query(params, base_return = [])
76
+ params[:action] = 'query'
77
+ params[:continue] = ''
78
+
79
+ loop do
80
+ result = post(params)
81
+ yield(base_return, result['query']) if result.key?('query')
82
+ break unless @use_continuation || result.key?('continue')
83
+ continue = result['continue']
84
+ continue.each do |key, val|
85
+ params[key.to_sym] = val
86
+ end
87
+ end
88
+
89
+ base_return
90
+ end
91
+
92
+ # Helper method for query methods that return an array built from the query objects.
93
+ # @param params [Hash] A hash containing MediaWiki API parameters.
94
+ # @param
95
+ def query_ary(params, base_response_key, property_key)
96
+ query(params) do |return_val, query|
97
+ query[base_response_key].each { |obj| return_val << obj[property_key] }
98
+ end
99
+ end
100
+
65
101
  # Gets whether the currently logged in user is a bot.
66
102
  # @param username [String] The username to check. Optional. Defaults to
67
103
  # the currently logged in user if nil.
@@ -10,17 +10,11 @@ module MediaWiki
10
10
  # @return [Array<String>] An array of all categories.
11
11
  def get_all_categories(limit = @query_limit_default)
12
12
  params = {
13
- action: 'query',
14
13
  list: 'allcategories',
15
14
  aclimit: get_limited(limit)
16
15
  }
17
16
 
18
- response = post(params)
19
-
20
- ret = []
21
- response['query']['allcategories'].each { |c| ret << c['*'] }
22
-
23
- ret
17
+ query_ary(params, 'allcategories', '*')
24
18
  end
25
19
 
26
20
  # Gets all the images on the wiki.
@@ -30,17 +24,11 @@ module MediaWiki
30
24
  # @return [Array<String>] An array of all images.
31
25
  def get_all_images(limit = @query_limit_default)
32
26
  params = {
33
- action: 'query',
34
27
  list: 'allimages',
35
28
  ailimit: get_limited(limit)
36
29
  }
37
30
 
38
- response = post(params)
39
-
40
- ret = []
41
- response['query']['allimages'].each { |i| ret << i['name'] }
42
-
43
- ret
31
+ query_ary(params, 'allimages', 'name')
44
32
  end
45
33
 
46
34
  # Gets all pages within a namespace integer.
@@ -51,18 +39,12 @@ module MediaWiki
51
39
  # @return [Array<String>] An array of all page titles.
52
40
  def get_all_pages_in_namespace(namespace, limit = @query_limit_default)
53
41
  params = {
54
- action: 'query',
55
42
  list: 'allpages',
56
43
  apnamespace: namespace,
57
44
  aplimit: get_limited(limit)
58
45
  }
59
46
 
60
- response = post(params)
61
-
62
- ret = []
63
- response['query']['allpages'].each { |p| ret << p['title'] }
64
-
65
- ret
47
+ query_ary(params, 'allpages', 'title')
66
48
  end
67
49
 
68
50
  # Gets all users, or all users in a group.
@@ -73,18 +55,14 @@ module MediaWiki
73
55
  # @return [Hash<String, Fixnum>] A hash of all users, names are keys, IDs are values.
74
56
  def get_all_users(group = nil, limit = @query_limit_default)
75
57
  params = {
76
- action: 'query',
77
58
  list: 'allusers',
78
59
  aulimit: get_limited(limit)
79
60
  }
80
61
  params[:augroup] = group unless group.nil?
81
62
 
82
- response = post(params)
83
-
84
- ret = {}
85
- response['query']['allusers'].each { |u| ret[u['name']] = u['userid'] }
86
-
87
- ret
63
+ query(params, {}) do |return_val, query|
64
+ query['allusers'].each { |u| return_val[u['name']] = u['userid']}
65
+ end
88
66
  end
89
67
 
90
68
  # Gets all block IDs on the wiki. It seems like this only gets non-IP blocks, but the MediaWiki docs are a
@@ -95,18 +73,12 @@ module MediaWiki
95
73
  # @return [Array<Fixnum>] All block IDs as strings.
96
74
  def get_all_blocks(limit = @query_limit_default)
97
75
  params = {
98
- action: 'query',
99
76
  list: 'blocks',
100
77
  bklimit: get_limited(limit),
101
78
  bkprop: 'id'
102
79
  }
103
80
 
104
- response = post(params)
105
-
106
- ret = []
107
- response['query']['blocks'].each { |b| ret.push(b['id']) }
108
-
109
- ret
81
+ query_ary(params, 'blocks', 'id')
110
82
  end
111
83
 
112
84
  # Gets all page titles that transclude a given page.
@@ -117,18 +89,12 @@ module MediaWiki
117
89
  # @return [Array<String>] All transcluder page titles.
118
90
  def get_all_transcluders(page, limit = @query_limit_default)
119
91
  params = {
120
- action: 'query',
121
92
  list: 'embeddedin',
122
93
  eititle: page,
123
94
  eilimit: get_limited(limit)
124
95
  }
125
96
 
126
- response = post(params)
127
-
128
- ret = []
129
- response['query']['embeddedin'].each { |e| ret << e['title'] }
130
-
131
- ret
97
+ query_ary(params, 'embeddedin', 'title')
132
98
  end
133
99
 
134
100
  # Gets an array of all deleted or archived files on the wiki.
@@ -138,17 +104,11 @@ module MediaWiki
138
104
  # @return [Array<String>] All deleted file names. These are not titles, so they do not include "File:".
139
105
  def get_all_deleted_files(limit = @query_limit_default)
140
106
  params = {
141
- action: 'query',
142
107
  list: 'filearchive',
143
108
  falimit: get_limited(limit)
144
109
  }
145
110
 
146
- response = post(params)
147
-
148
- ret = []
149
- response['query']['filearchive'].each { |f| ret.push(f['name']) }
150
-
151
- ret
111
+ query_ary(params, 'filearchive', 'name')
152
112
  end
153
113
 
154
114
  # Gets a list of all protected pages, by protection level if provided.
@@ -159,18 +119,12 @@ module MediaWiki
159
119
  # @return [Array<String>] All protected page titles.
160
120
  def get_all_protected_titles(protection_level = nil, limit = @query_limit_default)
161
121
  params = {
162
- action: 'query',
163
122
  list: 'protectedtitles',
164
123
  ptlimit: get_limited(limit)
165
124
  }
166
125
  params[:ptlevel] = protection_level unless protection_level.nil?
167
126
 
168
- response = post(params)
169
-
170
- ret = []
171
- response['query']['protectedtitles'].each { |t| ret << t['title'] }
172
-
173
- ret
127
+ query_ary(params, 'protectedtitles', 'title')
174
128
  end
175
129
  end
176
130
  end
@@ -11,17 +11,12 @@ module MediaWiki
11
11
  # @return [Array<String>] All backlinks until the limit
12
12
  def what_links_here(title, limit = @query_limit_default)
13
13
  params = {
14
- action: 'query',
15
14
  list: 'backlinks',
16
15
  bltitle: title,
17
16
  bllimit: get_limited(limit)
18
17
  }
19
18
 
20
- ret = []
21
- response = post(params)
22
- response['query']['backlinks'].each { |bl| ret << bl['title'] }
23
-
24
- ret
19
+ query_ary(params, 'backlinks', 'title')
25
20
  end
26
21
 
27
22
  # Gets interwiki backlinks by the prefix and title.
@@ -33,18 +28,13 @@ module MediaWiki
33
28
  # @return [Array<String>] All interwiki backlinking page titles.
34
29
  def get_interwiki_backlinks(prefix = nil, title = nil, limit = @query_limit_default)
35
30
  params = {
36
- action: 'query',
37
31
  list: 'iwbacklinks',
38
32
  iwbllimit: get_limited(limit)
39
33
  }
40
34
  params[:iwblprefix] = prefix unless prefix.nil?
41
35
  params[:iwbltitle] = title unless title.nil?
42
36
 
43
- ret = []
44
- response = post(params)
45
- response['query']['iwbacklinks'].each { |bl| ret << bl['title'] }
46
-
47
- ret
37
+ query_ary(params, 'iwbacklinks', 'title')
48
38
  end
49
39
 
50
40
  # Gets language backlinks by the language and title.
@@ -56,18 +46,13 @@ module MediaWiki
56
46
  def get_language_backlinks(language = nil, title = nil, limit = @query_limit_default)
57
47
  language.downcase! if language.match(/[^A-Z]*/)[0].empty?
58
48
  params = {
59
- action: 'query',
60
49
  list: 'langbacklinks',
61
50
  lbltitle: get_limited(limit)
62
51
  }
63
52
  params[:lbllang] = language unless language.nil?
64
53
  params[:lbltitle] = title unless title.nil?
65
54
 
66
- ret = []
67
- response = post(params)
68
- response['query']['langbacklinks'].each { |bl| ret << bl['title'] }
69
-
70
- ret
55
+ query_ary(params, 'langbacklinks', 'title')
71
56
  end
72
57
 
73
58
  # Gets image backlinks, or the pages that use a given image.
@@ -80,20 +65,14 @@ module MediaWiki
80
65
  # @return [Array<String>] All page titles that fit the requirements.
81
66
  def get_image_backlinks(title, list_redirects = nil, thru_redir = false, limit = @query_limit_default)
82
67
  params = {
83
- action: 'query',
84
68
  list: 'imageusage',
85
69
  iutitle: title,
86
70
  iulimit: get_limited(limit)
87
71
  }
88
-
89
72
  params[:iufilterredir] = list_redirects.nil? ? 'all' : list_redirects
90
73
  params[:iuredirect] = '1' if thru_redir
91
74
 
92
- response = post(params)
93
- ret = []
94
- response['query']['imageusage'].each { |bl| ret << bl['title'] }
95
-
96
- ret
75
+ query_ary(params, 'imageusage', 'title')
97
76
  end
98
77
 
99
78
  # Gets all external link page titles.
@@ -106,27 +85,14 @@ module MediaWiki
106
85
  # hash, url and title.
107
86
  def get_url_backlinks(url = nil, limit = @query_limit_default)
108
87
  params = {
109
- action: 'query',
110
88
  list: 'exturlusage',
111
89
  eulimit: get_limited(limit)
112
90
  }
113
- params[:euquery] = url unless url.nil?
91
+ params[:euquery] = url if url
114
92
 
115
- response = post(params)
116
- ret = []
117
- response['query']['exturlusage'].each do |bl|
118
- if url.nil?
119
- hash = {
120
- url: bl['url'],
121
- title: bl['title']
122
- }
123
- ret << hash
124
- else
125
- ret << bl['title']
126
- end
93
+ query(params) do |return_val, query|
94
+ query['exturlusage'].each { |bl| return_val << url ? bl['title'] : { url: bl['url'], title: bl['title'] } }
127
95
  end
128
-
129
- ret
130
96
  end
131
97
  end
132
98
  end
@@ -13,7 +13,6 @@ module MediaWiki
13
13
  # @return [Array<String>] All category members until the limit
14
14
  def get_category_members(category, limit = @query_limit_default, type = 'page')
15
15
  params = {
16
- action: 'query',
17
16
  list: 'categorymembers',
18
17
  cmprop: 'title',
19
18
  cmlimit: get_limited(limit),
@@ -21,11 +20,8 @@ module MediaWiki
21
20
  }
22
21
 
23
22
  params[:cmtitle] = category =~ /[Cc]ategory:/ ? category : "Category:#{category}"
24
- ret = []
25
- response = post(params)
26
- response['query']['categorymembers'].each { |cm| ret << cm['title'] }
27
23
 
28
- ret
24
+ query_ary(params, 'categorymembers', 'title')
29
25
  end
30
26
 
31
27
  # Gets the subcategories of a given category.
@@ -4,23 +4,23 @@ module MediaWiki
4
4
  module Miscellaneous
5
5
  # Returns an array of random pages titles.
6
6
  # @param limit [Fixnum] The number of articles to get. Defaults to 1. Cannot be greater than 10 for normal
7
- # users, or 20 for bots. This method does *not* use the query_limit_default attribute.
7
+ # users, or 20 for bots. This method does *not* use the query_limit_default attribute. This method does not
8
+ # use continuation.
8
9
  # @param namespace [Fixnum] The namespace ID. Defaults to 0 (the main namespace).
9
10
  # @see https://www.mediawiki.org/wiki/API:Random MediaWiki Random API Docs
10
11
  # @since 0.2.0
11
12
  # @return [Array<String>] All members
12
13
  def get_random_pages(limit = 1, namespace = 0)
13
14
  params = {
14
- action: 'query',
15
15
  list: 'random',
16
16
  rnlimit: get_limited(limit, 10, 20),
17
17
  rnnamespace: validate_namespace(namespace)
18
18
  }
19
19
 
20
- ret = []
21
- responce = post(params)
22
- responce['query']['random'].each { |a| ret << a['title'] }
23
-
20
+ continue = @use_continuation
21
+ @use_continuation = false
22
+ ret = query_ary(params, 'random', 'title')
23
+ @use_continuation = continue
24
24
  ret
25
25
  end
26
26
 
@@ -31,14 +31,11 @@ module MediaWiki
31
31
  # @return [Array<String>] All tag names.
32
32
  def get_tags(limit = @query_limit_default)
33
33
  params = {
34
- action: 'query',
35
34
  list: 'tags',
36
35
  limit: get_limited(limit)
37
36
  }
38
- response = post(params)
39
- ret = []
40
- response['query']['tags'].each { |tag| ret << tag['name'] }
41
- ret
37
+
38
+ query_ary(params, 'tags', 'name')
42
39
  end
43
40
  end
44
41
  end
@@ -89,11 +89,7 @@ module MediaWiki
89
89
  # @return [Nil] If the user does not have the necessary rights.
90
90
  def get_unwatchedpages_page(limit = @query_limit_default)
91
91
  rights = get_userrights
92
- if rights != false && rights.include?('unwatchedpages')
93
- get_querypage('Unwatchedpages', limit)
94
- else
95
- return nil
96
- end
92
+ rights && rights.include?('unwatchedpages') ? get_querypage('Unwatchedpages', limit) : nil
97
93
  end
98
94
 
99
95
  # @since 0.10.0
@@ -182,18 +178,14 @@ module MediaWiki
182
178
  # @return [Array<String>] All of the page titles in the querypage.
183
179
  def get_querypage(page, limit = @query_limit_default)
184
180
  params = {
185
- action: 'query',
186
181
  list: 'querypage',
187
182
  qppage: page,
188
183
  qplimit: get_limited(limit)
189
184
  }
190
- response = post(params)
191
- ret = []
192
- response['query']['querypage']['results'].each do |result|
193
- ret << result['title']
194
- end
195
185
 
196
- ret
186
+ query(params) do |return_val, query|
187
+ query['querypage']['results'].each { |result| return_val << result['title'] }
188
+ end
197
189
  end
198
190
  end
199
191
  end
@@ -28,18 +28,12 @@ module MediaWiki
28
28
  # @return [Array<String>] The page titles that matched the search.
29
29
  def get_search_results(search_value, namespace = 0)
30
30
  params = {
31
- action: 'query',
32
31
  list: 'search',
33
32
  srsearch: search_value,
34
33
  srnamespace: validate_namespace(namespace)
35
34
  }
36
35
 
37
- response = post(params)
38
-
39
- ret = []
40
- response['query']['search'].each { |search| ret << search['title'] }
41
-
42
- ret
36
+ query_ary(params, 'search', 'title')
43
37
  end
44
38
 
45
39
  # Searches the wiki by a prefix.
@@ -51,19 +45,12 @@ module MediaWiki
51
45
  # @return [Array<String>] All of the page titles that match the search.
52
46
  def get_prefix_search(prefix, limit = 100)
53
47
  params = {
54
- action: 'query',
55
48
  list: 'prefixsearch',
56
49
  pssearch: prefix,
57
50
  pslimit: get_limited(limit, 100, 200)
58
51
  }
59
52
 
60
- response = post(params)
61
- ret = []
62
- response['query']['prefixsearch'].each do |result|
63
- ret << result['title']
64
- end
65
-
66
- ret
53
+ query_ary(params, 'prefixsearch', 'title')
67
54
  end
68
55
  end
69
56
  end
@@ -3,7 +3,7 @@ module MediaWiki
3
3
  module Lists
4
4
  module Users
5
5
  # Gets user information. This method should rarely be used by
6
- # normal users, unless they want a huge amount of user data at once.
6
+ # normal users, unless they want a huge amount of usnot g data at once.
7
7
  # @param prop [String] The usprop parameter.
8
8
  # @param username [String] The username to get info for. Optional. Defaults to the currently logged in user
9
9
  # if omitted.
@@ -144,26 +144,22 @@ module MediaWiki
144
144
  # the size change relative to the previous edit.
145
145
  def get_user_contributions(user, limit = @query_limit_default)
146
146
  params = {
147
- action: 'query',
148
147
  list: 'usercontribs',
149
148
  ucuser: user,
150
149
  uclimit: get_limited(limit),
151
150
  ucprop: 'ids|title|comment|size|sizediff|flags|patrolled'
152
151
  }
153
152
 
154
- response = post(params)
155
-
156
- ret = {}
157
- response['query']['usercontribs'].each do |item|
158
- ret[item['revid']] = {
159
- title: item['title'],
160
- summary: item['comment'],
161
- total_size: item['size'],
162
- diff_size: item['sizediff']
163
- }
153
+ query(params, {}) do |return_val, query|
154
+ query['usercontribs'].each do |item|
155
+ return_val[item['revid']] = {
156
+ title: item['title'],
157
+ summary: item['comment'],
158
+ total_size: item['size'],
159
+ diff_size: item['sizediff']
160
+ }
161
+ end
164
162
  end
165
-
166
- ret
167
163
  end
168
164
 
169
165
  # Gets the user's full watchlist. If no user is provided, it will use the currently logged in user, according
@@ -175,19 +171,13 @@ module MediaWiki
175
171
  # @return [Array<String>] All the watchlist page titles.
176
172
  def get_full_watchlist(user = nil, limit = @query_limit_default)
177
173
  params = {
178
- action: 'query',
179
174
  list: 'watchlist',
180
175
  wlprop: 'title',
181
176
  wllimit: get_limited(limit)
182
177
  }
183
- params[:wluser] = user unless user.nil?
178
+ params[:wluser] = user if user
184
179
 
185
- response = post(params)
186
-
187
- ret = []
188
- response['query']['watchlist'].each { |t| ret << t['title'] }
189
-
190
- ret
180
+ query_ary(params, 'watchlist', 'title')
191
181
  end
192
182
  end
193
183
  end
@@ -12,10 +12,12 @@ module MediaWiki
12
12
  # @see #get_logged_in_contributors
13
13
  # @since 0.8.0
14
14
  # @return [Fixnum] The number of contributors to that page.
15
+ # @return [Nil] If the page does not exist.
15
16
  def get_total_contributors(title, limit = @query_limit_default)
16
17
  anon_users = get_anonymous_contributors_count(title, limit)
17
18
  users = get_logged_in_contributors(title, limit)
18
19
 
20
+ return if users.nil?
19
21
  users.size + anon_users
20
22
  end
21
23
 
@@ -23,21 +25,14 @@ module MediaWiki
23
25
  # @param (see #get_total_contributors)
24
26
  # @see #get_contributors_response
25
27
  # @since 0.8.0
26
- # @return [Array] All usernames for the contributors.
28
+ # @return [Array<String>] All usernames for the contributors.
27
29
  def get_logged_in_contributors(title, limit = @query_limit_default)
28
- response = get_contributors_response(title, limit)
29
- pageid = nil
30
- response['query']['pages'].each { |r, _| pageid = r }
31
- ret = []
32
- if response['query']['pages'][pageid]['missing'] == ''
33
- return nil
34
- else
35
- response['query']['pages'][pageid]['contributors'].each do |c|
36
- ret.push(c['name'])
37
- end
30
+ get_contributors_response(title, limit) do |return_val, query|
31
+ pageid = nil
32
+ query['pages'].each { |r, _| pageid = r }
33
+ return if query['pages'][pageid].key?('missing')
34
+ query['pages'][pageid]['contributors'].each { |c| return_val << c['name'] }
38
35
  end
39
-
40
- ret
41
36
  end
42
37
 
43
38
  private
@@ -46,16 +41,15 @@ module MediaWiki
46
41
  # @param (see #get_total_contributors)
47
42
  # @see https://www.mediawiki.org/wiki/API:Contributors MediaWiki Contributors Property API Docs
48
43
  # @since 0.8.0
49
- # @return [Hash] See {MediaWiki::Butt#post}
44
+ # @return [Hash] See {MediaWiki::Butt#query}
50
45
  def get_contributors_response(title, limit = @query_limit_default)
51
46
  params = {
52
- action: 'query',
53
47
  prop: 'contributors',
54
48
  titles: title,
55
49
  pclimit: get_limited(limit)
56
50
  }
57
51
 
58
- post(params)
52
+ query(params) { |return_val, query| yield(return_val, query) }
59
53
  end
60
54
 
61
55
  # Gets the total number of anonymous contributors for the given page.
@@ -65,12 +59,16 @@ module MediaWiki
65
59
  # @return [Fixnum] The number of anonymous contributors for the page.
66
60
  # @return [Nil] If title is not a valid page.
67
61
  def get_anonymous_contributors_count(title, limit = @query_limit_default)
68
- response = get_contributors_response(title, limit)
69
- pageid = nil
70
- response['query']['pages'].each { |r, _| pageid = r }
71
- return nil if response['query']['pages'][pageid]['missing'] == ''
62
+ ret = 0
72
63
 
73
- response['query']['pages'][pageid]['anoncontriburors'].to_i
64
+ get_contributors_response(title, limit) do |_, query|
65
+ pageid = nil
66
+ query['pages'].each { |r, __| pageid = r }
67
+ return if query['pages'][pageid].key?('missing')
68
+ ret += query['pages'][pageid]['anoncontributors'].to_i
69
+ end
70
+
71
+ ret
74
72
  end
75
73
  end
76
74
  end
@@ -10,24 +10,19 @@ module MediaWiki
10
10
  # @see https://www.mediawiki.org/wiki/API:Duplicatefiles MediaWiki Duplicate Files API Docs
11
11
  # @since 0.8.0
12
12
  # @return [Array<String>] Array of all the duplicated file names.
13
- # @return [Nil] If there aren't any duplicated files.
14
13
  def get_duplicated_files_of(title, limit = @query_limit_default)
15
14
  params = {
16
- action: 'query',
17
15
  prop: 'duplicatefiles',
18
16
  titles: title,
19
17
  dflimit: get_limited(limit)
20
18
  }
21
19
 
22
- response = post(params)
23
- ret = []
24
- response['query']['pages'].each do |_, c|
25
- return nil if c['duplicatefiles'].nil?
26
- c['duplicatefiles'].each do |f|
27
- ret << f['name']
20
+ query(params) do |return_val, query|
21
+ query['pages'].each do |_, c|
22
+ next unless c['duplicatefies']
23
+ c['duplicatefiles'].each { |f| return_val << f['name'] }
28
24
  end
29
25
  end
30
- ret
31
26
  end
32
27
 
33
28
  # Gets all duplicated files on the wiki.
@@ -37,18 +32,12 @@ module MediaWiki
37
32
  # @return [Array<String>] All duplicate file titles on the wiki.
38
33
  def get_all_duplicated_files(limit = @query_limit_default)
39
34
  params = {
40
- action: 'query',
41
35
  generator: 'allimages',
42
- prop: 'duplicatefiles',
36
+ prop: 'duplicatedfiles',
43
37
  dflimit: get_limited(limit)
44
38
  }
45
39
 
46
- response = post(params)
47
- ret = []
48
- response['query']['pages'].each do |_, c|
49
- ret << c['title']
50
- end
51
- ret
40
+ query_ary(params, 'pages', 'title')
52
41
  end
53
42
 
54
43
  # Gets the size of an image in bytes.
@@ -12,18 +12,20 @@ module MediaWiki
12
12
  # @return [Nil] If the title does not exist.
13
13
  def get_categories_in_page(title)
14
14
  params = {
15
- action: 'query',
16
15
  prop: 'categories',
17
16
  titles: title
18
17
  }
19
18
 
20
- response = post(params)
21
- pageid = nil
22
- ret = []
23
- response['query']['pages'].each { |r, _| pageid = r }
24
- return if response['query']['pages'][pageid]['missing'] == ''
19
+ query(params) do |return_val, query|
20
+ pageid = nil
21
+ query['pages'].each do |r, _|
22
+ pageid = r
23
+ break
24
+ end
25
25
 
26
- response['query']['pages'][pageid].fetch('categories', []).map { |c| c['title'] }
26
+ return if query['pages'][pageid].key?('missing')
27
+ query['pages'][pageid].fetch('categories', []).each { |c| return_val << c['title'] }
28
+ end
27
29
  end
28
30
 
29
31
  # Gets the wiki text for the given page. Returns nil if it for some reason cannot get the text, for example,
@@ -85,16 +87,12 @@ module MediaWiki
85
87
  ellimit: get_limited(limit)
86
88
  }
87
89
 
88
- response = post(params)
89
- ret = []
90
- response['query']['pages'].each do |revid, _|
91
- return if revid == '-1'
92
- response['query']['pages'][revid]['extlinks'].each do |l|
93
- ret << l['*']
90
+ query(params) do |return_val, query|
91
+ query['pages'].each do |revid, _|
92
+ return if revid == '-1'
93
+ query['pages'][revid]['extlinks'].each { |l| return_val << l['*'] }
94
94
  end
95
95
  end
96
-
97
- ret
98
96
  end
99
97
 
100
98
  # Gets whether the current user watches the page.
@@ -273,22 +271,17 @@ module MediaWiki
273
271
  # @return [Nil] If the page does not exist.
274
272
  def get_images_in_page(title, limit = @query_limit_default)
275
273
  params = {
276
- action: 'query',
277
274
  prop: 'images',
278
275
  titles: title,
279
276
  imlimit: get_limited(limit)
280
277
  }
281
278
 
282
- response = post(params)
283
- ret = []
284
- response['query']['pages'].each do |revid, _|
285
- return if revid == '-1'
286
- response['query']['pages'][revid]['images'].each do |img|
287
- ret << img['title']
279
+ query(params) do |return_val, query|
280
+ query['pages'].each do |revid, _|
281
+ return if revid == '-1'
282
+ query['pages'][revid]['images'].each { |img| return_val << img['title'] }
288
283
  end
289
284
  end
290
-
291
- ret
292
285
  end
293
286
 
294
287
  # Gets all of the templates in the given page.
@@ -299,48 +292,40 @@ module MediaWiki
299
292
  # @return [Nil] If the page does not exist.
300
293
  def get_templates_in_page(title, limit = @query_limit_default)
301
294
  params = {
302
- action: 'query',
303
295
  prop: 'templates',
304
296
  titles: title,
305
297
  tllimit: get_limited(limit)
306
298
  }
307
299
 
308
- response = post(params)
309
- ret = []
310
- response['query']['pages'].each do |revid, _|
311
- return if revid == '-1'
312
- response['query']['pages'][revid]['templates'].each do |tmp|
313
- ret << tmp['title']
300
+ query(params) do |return_val, query|
301
+ query['pages'].each do |revid, _|
302
+ return if revid == '-1'
303
+ query['pages'][revid]['templates'].each { |template| return_val << template['title'] }
314
304
  end
315
305
  end
316
-
317
- ret
318
306
  end
319
307
 
320
308
  # Gets all of the interwiki links on the given page.
321
309
  # @param (see #get_external_links)
322
310
  # @see https://www.mediawiki.org/wiki/API:Iwlinks MediaWiki Interwiki Links API Docs
323
311
  # @since 0.8.0
324
- # @return [Array<String>] All interwiki link titles.
312
+ # @return [Array<Hash<Symbol, String>>] All interwiki links. Each hash has a :prefix and :title
325
313
  # @return [Nil] If the page does not exist.
326
314
  def get_interwiki_links_in_page(title, limit = @query_limit_default)
327
315
  params = {
328
- action: 'query',
329
316
  prop: 'iwlinks',
330
317
  titles: title,
331
318
  tllimit: get_limited(limit)
332
319
  }
333
320
 
334
- response = post(params)
335
- ret = []
336
- response['query']['pages'].each do |revid, _|
337
- return if revid == '-1'
338
- response['query']['pages'][revid]['iwlinks'].each do |l|
339
- ret << l['*']
321
+ query(params) do |return_val, query|
322
+ query['pages'].each do |revid, _|
323
+ return if revid == '-1'
324
+ query['pages'][revid].fetch('iwlinks', []).each do |l|
325
+ return_val << { prefix: l['prefix'], title: l['*'] }
326
+ end
340
327
  end
341
328
  end
342
-
343
- ret
344
329
  end
345
330
 
346
331
  # Gets a hash of data for the page in every language that it is available in. This includes url, language
@@ -352,28 +337,25 @@ module MediaWiki
352
337
  # @return [Nil] If the page does not exist.
353
338
  def get_other_langs_of_page(title, limit = @query_limit_default)
354
339
  params = {
355
- action: 'query',
356
340
  prop: 'langlinks',
357
341
  titles: title,
358
342
  lllimit: get_limited(limit),
359
343
  llprop: 'url|langname|autonym'
360
344
  }
361
345
 
362
- response = post(params)
363
- ret = {}
364
- response['query']['pages'].each do |revid, _|
365
- return if revid == '-1'
366
- response['query']['pages'][revid]['langlinks'].each do |l|
367
- ret[l['lang'].to_sym] = {
368
- url: l['url'],
369
- langname: l['langname'],
370
- autonym: l['autonym'],
371
- title: l['*']
372
- }
346
+ query(params) do |return_val, query|
347
+ query['pages'].each do |revid, _|
348
+ return if revid == '-1'
349
+ query['pages'][revid]['langlinks'].each do |l|
350
+ return_val[l['lang'].to_sym] = {
351
+ url: l['url'],
352
+ langname: l['langname'],
353
+ autonym: l['autonym'],
354
+ title: l['*']
355
+ }
356
+ end
373
357
  end
374
358
  end
375
-
376
- ret
377
359
  end
378
360
 
379
361
  # Gets every single link in a page.
@@ -384,22 +366,17 @@ module MediaWiki
384
366
  # @return [Nil] If the page does not exist.
385
367
  def get_all_links_in_page(title, limit = @query_limit_default)
386
368
  params = {
387
- action: 'query',
388
369
  prop: 'links',
389
370
  titles: title,
390
371
  pllimit: get_limited(limit)
391
372
  }
392
373
 
393
- response = post(params)
394
- ret = []
395
- response['query']['pages'].each do |revid, _|
396
- return if revid == '-1'
397
- response['query']['pages'][revid]['links'].each do |l|
398
- ret << l['title']
374
+ query(params) do |return_val, query|
375
+ query['pages'].each do |revid, _|
376
+ return if revid == '-1'
377
+ query['pages'][revid]['links'].each { |l| return_val << l['title'] }
399
378
  end
400
379
  end
401
-
402
- ret
403
380
  end
404
381
  end
405
382
  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: 1.0.0
4
+ version: 1.1.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: 2016-07-30 00:00:00.000000000 Z
12
+ date: 2016-08-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httpclient