mediawiki-butt 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,124 @@
1
+ require_relative 'users'
2
+
3
+ module MediaWiki
4
+ module Query
5
+ module Lists
6
+ module RecentChanges
7
+ include MediaWiki::Query::Lists::Users
8
+
9
+ # Gets the RecentChanges log.
10
+ # @param user [String] See {MediaWiki::Query::Lists::Log#get_log}
11
+ # @param title [String] See {MediaWiki::Query::Lists::Log#get_log}
12
+ # @param start [DateTime] See {MediaWiki::Query::Lists::Log#get_log}
13
+ # @param stop [DateTime] See {MediaWiki::Query::Lists::Log#get_log}
14
+ # @param limit [Int] See {MediaWiki::Query::Lists::Log#get_log}
15
+ # @see https://www.mediawiki.org/wiki/API:RecentChanges MediaWiki
16
+ # RecentChanges API Docs
17
+ # @since 0.10.0
18
+ # @return [Array<Hash>] All of the changes, with the following keys:
19
+ # type, title, revid, old_revid, rcid, user, old_length, new_length,
20
+ # diff_length, timestamp, comment, parsed_comment, sha, new, minor,
21
+ # bot.
22
+ def get_recent_changes(user = nil, start = nil, stop = nil, limit = 500)
23
+ time_format = MediaWiki::Constants::TIME_FORMAT
24
+ prop = 'user|comment|parsedcomment|timestamp|title|ids|sha1|sizes' \
25
+ '|redirect|flags|loginfo'
26
+ rights = get_userrights
27
+ patrol = false
28
+ if rights != false && rights.include?('patrol')
29
+ prop << '|patrolled'
30
+ patrol = true
31
+ end
32
+ params = {
33
+ action: 'query',
34
+ list: 'recentchanges',
35
+ rcprop: prop,
36
+ rclimit: get_limited(limit)
37
+ }
38
+ params[:rcuser] = user unless user.nil?
39
+ params[:rcstart] = start.strftime(time_format) unless start.nil?
40
+ params[:rcend] = stop.strftime(time_format) unless stop.nil?
41
+
42
+ response = post(params)
43
+ ret = []
44
+ p response
45
+ response['query']['recentchanges'].each do |change|
46
+ old_length = change['oldlen']
47
+ new_length = change['newlen']
48
+ diff_length = new_length - old_length
49
+
50
+ hash = {
51
+ type: change['type'],
52
+ title: change['title'],
53
+ revid: change['revid'],
54
+ old_revid: change['old_revid'],
55
+ rcid: change['rcid'],
56
+ user: change['user'],
57
+ old_length: old_length,
58
+ new_length: new_length,
59
+ diff_length: diff_length,
60
+ timestamp: DateTime.strptime(change['timestamp'], time_format),
61
+ comment: change['comment'],
62
+ parsed_comment: change['parsedcomment'],
63
+ sha: change['sha1']
64
+ }
65
+ hash[:new] = change.key?('new')
66
+ hash[:minor] = change.key?('minor')
67
+ hash[:bot] = change.key?('bot')
68
+
69
+ hash[:patrolled] = change.key?('patrolled') if patrol
70
+
71
+ if change['type'] == 'log'
72
+ hash[:log_type] = change['logtype']
73
+ hash[:log_action] = change['logaction']
74
+ hash[:logid] = change['logid']
75
+ end
76
+
77
+ ret << hash
78
+ end
79
+
80
+ ret
81
+ end
82
+
83
+ # Gets the recent deleted revisions.
84
+ # @param user [String] See {#get_recent_changes}
85
+ # @param start [DateTime] See {#get_recent_changes}
86
+ # @param stop [DateTime] See {#get_recent_changes}
87
+ # @param limit [Int] See {#get_recent_changes}
88
+ # @see https://www.mediawiki.org/wiki/API:Deletedrevs MediaWiki
89
+ # Deletedrevs API Docs
90
+ # @since 0.10.0
91
+ # @return [Array<Hash>] All of the changes, with the following keys:
92
+ # timestamp, user, comment, title.
93
+ def get_recent_deleted_revisions(user = nil, start = nil, stop = nil,
94
+ limit = 500)
95
+ time_format = MediaWiki::Constants::TIME_FORMAT
96
+ prop = 'revid|parentid|user|comment|parsedcomment|minor|len|sh1|tags'
97
+ params = {
98
+ action: 'query',
99
+ list: 'deletedrevs',
100
+ drprop: prop,
101
+ limit: get_limited(limit)
102
+ }
103
+ params[:drstart] = start.strftime(time_format) unless start.nil?
104
+ params[:drend] = stop.strftime(time_format) unless stop.nil?
105
+
106
+ response = post(params)
107
+ ret = []
108
+ response['query']['deletedrevs'].each do |rev|
109
+ r = rev['revisions'][0]
110
+ hash = {
111
+ timestamp: DateTime.strptime(r['timestamp'], time_format),
112
+ user: r['user'],
113
+ comment: r['comment'],
114
+ title: rev['title']
115
+ }
116
+ ret << hash
117
+ end
118
+
119
+ ret
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,86 @@
1
+ module MediaWiki
2
+ module Query
3
+ module Lists
4
+ module Search
5
+ # Gets the amount of results for the search value.
6
+ # @param search_value [String] The thing to search for.
7
+ # @param namespace [Int] The namespace to search in.
8
+ # Defaults to 0 (the main namespace).
9
+ # @see https://www.mediawiki.org/wiki/API:Search MediaWiki Search API
10
+ # Docs
11
+ # @since 0.4.0
12
+ # @return [Int] The number of pages that matched the search.
13
+ def get_search_result_amount(search_value, namespace = 0)
14
+ params = {
15
+ action: 'query',
16
+ list: 'search',
17
+ srsearch: search_value
18
+ }
19
+
20
+ if MediaWiki::Constants::NAMEPSACES.value?(namespace)
21
+ params[:srnamespace] = namespace
22
+ else
23
+ params[:srnamespace] = 0
24
+ end
25
+
26
+ response = post(params)
27
+ response['query']['searchinfo']['totalhits']
28
+ end
29
+
30
+ # Gets an array containing page titles that matched the search.
31
+ # @param search_value [String] The thing to search for.
32
+ # @param namespace [Int] The namespace to search in.
33
+ # Defaults to 0 (the main namespace).
34
+ # @see https://www.mediawiki.org/wiki/API:Search MediaWiki Search API
35
+ # Docs
36
+ # @since 0.4.0
37
+ # @return [Array] The page titles that matched the search.
38
+ def get_search_results(search_value, namespace = 0)
39
+ params = {
40
+ action: 'query',
41
+ list: 'search',
42
+ srsearch: search_value
43
+ }
44
+
45
+ if MediaWiki::Constants::NAMESPACES.value?(namespace)
46
+ params[:srnamespace] = namespace
47
+ else
48
+ params[:srnamespace] = 0
49
+ end
50
+
51
+ response = post(params)
52
+
53
+ ret = []
54
+ response['query']['search'].each { |search| ret << search['title'] }
55
+
56
+ ret
57
+ end
58
+
59
+ # Searches the wiki by a prefix.
60
+ # @param prefix [String] The prefix.
61
+ # @param limit [Int] The maximum number of results to get, maximum of
62
+ # 100 for users and 200 for bots.
63
+ # @see https://www.mediawiki.org/wiki/API:Prefixsearch MediaWiki
64
+ # Prefixsearch API Docs
65
+ # @since 0.10.0
66
+ # @return [Array<String>] All of the page titles that match the search.
67
+ def get_prefix_search(prefix, limit = 100)
68
+ params = {
69
+ action: 'query',
70
+ list: 'prefixsearch',
71
+ pssearch: prefix,
72
+ limit: get_limited(limit, 100, 200)
73
+ }
74
+
75
+ response = post(params)
76
+ ret = []
77
+ response['query']['prefixsearch'].each do |result|
78
+ ret << result['title']
79
+ end
80
+
81
+ ret
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,210 @@
1
+ module MediaWiki
2
+ module Query
3
+ module Lists
4
+ module Users
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.
7
+ # @param prop [String] The usprop parameter.
8
+ # @param username [String] The username to get info for. Optional.
9
+ # Defaults to the currently logged in user if ommitted.
10
+ # @see https://www.mediawiki.org/wiki/API:Users MediaWiki User Lists API
11
+ # Docs
12
+ # @since 0.3.0
13
+ # @return [String] Parsed full response if successful.
14
+ # @return [Nil] If the username is nil and the Butt is not logged in.
15
+ def get_userlists(prop, username = nil)
16
+ if username.nil?
17
+ if @logged_in
18
+ response = get_current_user_meta(prop)
19
+ else
20
+ return false
21
+ end
22
+ else
23
+ params = {
24
+ action: 'query',
25
+ list: 'users',
26
+ usprop: prop,
27
+ ususers: username
28
+ }
29
+
30
+ response = post(params)
31
+ end
32
+
33
+ response
34
+ end
35
+
36
+ # Gets an array of all the user's groups.
37
+ # @param username [String] The username to get groups of. Optional.
38
+ # Defaults to the currently logged in user.
39
+ # @see get_userlists
40
+ # @since 0.3.0
41
+ # @return [Array] All of the user's groups.
42
+ # @return [Boolean] False if username is nil and not logged in.
43
+ def get_usergroups(username = nil)
44
+ ret = []
45
+ if username.nil?
46
+ if @logged_in
47
+ info = get_userlists('groups')
48
+ info['query']['userinfo']['groups'].each { |i| ret << i }
49
+ else
50
+ return false
51
+ end
52
+ else
53
+ info = get_userlists('groups', username)
54
+ info['query']['users'].each do |i|
55
+ i['groups'].each { |g| ret.push(g) }
56
+ end
57
+ end
58
+
59
+ ret
60
+ end
61
+
62
+ # Gets the user rights for the user.
63
+ # @param username [String] The user to get the rights for. Optional.
64
+ # Defaults to the currently logged in user.
65
+ # @see get_userlists
66
+ # @since 0.3.0
67
+ # @return [Array] All of the user's groups.
68
+ # @return [Boolean] False if username is nil and not logged in.
69
+ def get_userrights(username = nil)
70
+ ret = []
71
+ if username.nil?
72
+ if @logged_in
73
+ info = get_userlists('rights')
74
+ info['query']['userinfo']['rights'].each { |i| ret << i }
75
+ else
76
+ return false
77
+ end
78
+ else
79
+ info = get_userlists('rights', username)
80
+ info['query']['users'].each do |i|
81
+ i['rights'].each do |g|
82
+ ret.push(g)
83
+ end
84
+ end
85
+ end
86
+
87
+ ret
88
+ end
89
+
90
+ # Gets contribution count for the user.
91
+ # @param username [String] The username to get the contribution count of.
92
+ # Optional. Defaults to the currently logged in user.
93
+ # @see get_userlists
94
+ # @since 0.3.0
95
+ # @return [Boolean] False if username is nil and not logged in.
96
+ # @return [Int] The number of contributions the user has made.
97
+ def get_contrib_count(username = nil)
98
+ count = nil
99
+ if username.nil?
100
+ if @logged_in
101
+ info = get_userlists('editcount')
102
+ count = info['query']['userinfo']['editcount']
103
+ else
104
+ return false
105
+ end
106
+ else
107
+ info = get_userlists('editcount', username)
108
+ info['query']['users'].each { |i| count = i['editcount'] }
109
+ end
110
+
111
+ count
112
+ end
113
+
114
+ # Gets when the user registered.
115
+ # @param username [String] The username to get the registration date and
116
+ # time of. Optional. Defaults to the currently logged in user.
117
+ # @see get_userlists
118
+ # @since 0.4.0
119
+ # @return [DateTime] The registration date and time as a DateTime object.
120
+ def get_registration_time(username = nil)
121
+ time = nil
122
+ # Do note that in Userinfo, registration is called registrationdate.
123
+ if username.nil?
124
+ if @logged_in
125
+ info = get_userlists('registrationdate')
126
+ time = info['query']['userinfo']['registrationdate']
127
+ else
128
+ return false
129
+ end
130
+ else
131
+ info = get_userlists('registration', username)
132
+ info['query']['users'].each { |i| time = i['registration'] }
133
+ end
134
+
135
+ DateTime.strptime(time, '%Y-%m-%dT%T')
136
+ end
137
+
138
+ # Gets the gender for the provded user.
139
+ # @param username [String] The user.
140
+ # @see get_userlists
141
+ # @since 0.4.0
142
+ # @return [String] The gender. 'male', 'female', or 'unknown'.
143
+ def get_user_gender(username)
144
+ gender = nil
145
+ info = get_userlists('gender', username)
146
+ info['query']['users'].each { |i| gender = i['gender'] }
147
+
148
+ gender
149
+ end
150
+
151
+ # Gets the latest contributions by the user until the limit.
152
+ # @param user [String] The username.
153
+ # @param limit [Int] See #get_all_images.
154
+ # @see https://www.mediawiki.org/wiki/API:Usercontribs MediaWiki
155
+ # User Contributions API Docs
156
+ # @since 0.8.0
157
+ # @return [Hash] Each contribution by its revid, containing the title,
158
+ # summary, total contribution size, and the size change relative to the
159
+ # previous edit.
160
+ def get_user_contributions(user, limit = 500)
161
+ params = {
162
+ action: 'query',
163
+ list: 'usercontribs',
164
+ ucuser: user,
165
+ uclimit: get_limited(limit),
166
+ ucprop: 'ids|title|comment|size|sizediff|flags|patrolled'
167
+ }
168
+
169
+ response = post(params)
170
+
171
+ ret = {}
172
+ response['query']['usercontribs'].each do |item|
173
+ ret[item['revid']] = {
174
+ title: item['title'],
175
+ summary: item['comment'],
176
+ total_size: item['size'],
177
+ diff_size: item['sizediff']
178
+ }
179
+ end
180
+
181
+ ret
182
+ end
183
+
184
+ # Gets the user's full watchlist. If no user is provided, it will use the
185
+ # currently logged in user, according to the MediaWiki API.
186
+ # @param user [String] The username.
187
+ # @param limit [Int] See #get_all_images.
188
+ # @see https://www.mediawiki.org/wiki/API:Watchlist MediaWiki Watchlist
189
+ # API Docs
190
+ # @since 0.8.0
191
+ # @return [Array] All the watchlist page titles.
192
+ def get_full_watchlist(user = nil, limit = 500)
193
+ params = {
194
+ action: 'query',
195
+ list: 'watchlist',
196
+ wlprop: 'title'
197
+ }
198
+ params[:wluser] = user unless user.nil?
199
+
200
+ response = post(params)
201
+
202
+ ret = []
203
+ response['query']['watchlist'].each { |t| ret << t['title'] }
204
+
205
+ ret
206
+ end
207
+ end
208
+ end
209
+ end
210
+ end
@@ -1,5 +1,5 @@
1
1
  require_relative 'meta/meta'
2
- require_relative 'lists'
2
+ require_relative 'lists/lists'
3
3
  require_relative 'properties/properties'
4
4
 
5
5
  module MediaWiki
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.9.0
4
+ version: 0.10.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: 2015-12-19 00:00:00.000000000 Z
12
+ date: 2015-12-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httpclient
@@ -33,6 +33,7 @@ executables: []
33
33
  extensions: []
34
34
  extra_rdoc_files: []
35
35
  files:
36
+ - CHANGELOG.md
36
37
  - lib/mediawiki-butt.rb
37
38
  - lib/mediawiki/administration.rb
38
39
  - lib/mediawiki/auth.rb
@@ -40,7 +41,25 @@ files:
40
41
  - lib/mediawiki/constants.rb
41
42
  - lib/mediawiki/edit.rb
42
43
  - lib/mediawiki/exceptions.rb
43
- - lib/mediawiki/query/lists.rb
44
+ - lib/mediawiki/query/lists/all.rb
45
+ - lib/mediawiki/query/lists/backlinks.rb
46
+ - lib/mediawiki/query/lists/categories.rb
47
+ - lib/mediawiki/query/lists/lists.rb
48
+ - lib/mediawiki/query/lists/log/block.rb
49
+ - lib/mediawiki/query/lists/log/delete.rb
50
+ - lib/mediawiki/query/lists/log/import.rb
51
+ - lib/mediawiki/query/lists/log/log.rb
52
+ - lib/mediawiki/query/lists/log/merge.rb
53
+ - lib/mediawiki/query/lists/log/move.rb
54
+ - lib/mediawiki/query/lists/log/newusers.rb
55
+ - lib/mediawiki/query/lists/log/patrol.rb
56
+ - lib/mediawiki/query/lists/log/rights.rb
57
+ - lib/mediawiki/query/lists/log/upload.rb
58
+ - lib/mediawiki/query/lists/miscellaneous.rb
59
+ - lib/mediawiki/query/lists/querypage.rb
60
+ - lib/mediawiki/query/lists/recent_changes.rb
61
+ - lib/mediawiki/query/lists/search.rb
62
+ - lib/mediawiki/query/lists/users.rb
44
63
  - lib/mediawiki/query/meta/filerepoinfo.rb
45
64
  - lib/mediawiki/query/meta/meta.rb
46
65
  - lib/mediawiki/query/meta/siteinfo.rb