mediawiki-butt 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,90 @@
1
+ module MediaWiki
2
+ module Query
3
+ module Meta
4
+ module FileRepoInfo
5
+ # Gets FileRepoInfo for the property.
6
+ # @param prop [String] The friprop to get.
7
+ # @return [Response] The full parsed response.
8
+ def get_filerepoinfo(prop)
9
+ params = {
10
+ action: 'query',
11
+ meta: 'filerepoinfo',
12
+ friprop: prop
13
+ }
14
+
15
+ post(params)
16
+ end
17
+
18
+ # Returns an array of all the wiki's file repository names.
19
+ # @return [Array] All wiki's file repository names.
20
+ def get_filerepo_names
21
+ response = get_filerepoinfo('name|displayname')
22
+ ret = {}
23
+ response['query']['repos'].each { |n, dn| ret[n] = dn }
24
+ ret
25
+ end
26
+
27
+ # Gets the root URLs for the file repositories.
28
+ # @return [Hash] A hash containing keys of the names, and values of the
29
+ # root URLs.
30
+ def get_filerepo_rooturls
31
+ response = get_filerepoinfo('name|rootUrl')
32
+ ret = {}
33
+ response['query']['repos'].each { |n, r| ret[n] = r }
34
+ ret
35
+ end
36
+
37
+ # Gets an array containing all local repositories.
38
+ # @return [Array] All repositories that are marked as local.
39
+ def get_local_filerepos
40
+ response = get_filerepoinfo('name|local')
41
+ ret = []
42
+ response['query']['repos'].each do |n, l|
43
+ ret.push(n) if l == 'true'
44
+ end
45
+
46
+ ret
47
+ end
48
+
49
+ # Gets an array containing all repositories that aren't local.
50
+ # @return [Array] All repositories that are not marked as local.
51
+ def get_nonlocal_filerepos
52
+ response = get_filerepoinfo('name|local')
53
+ ret = []
54
+ response['query']['repos'].each do |n, l|
55
+ ret.push(n) if l == 'false'
56
+ end
57
+
58
+ ret
59
+ end
60
+
61
+ # Gets the repository names and their according URLs.
62
+ # @return [Hash] Names as the keys, with their URLs as the values.
63
+ def get_filerepo_urls
64
+ response = get_filerepoinfo('name|url')
65
+ ret = {}
66
+ response['query']['repos'].each { |n, u| ret[n] = u }
67
+ ret
68
+ end
69
+
70
+ # Gets the repository names and their accoring thumbnail URLs.
71
+ # @return [Hash] Names as the keys, with their URLs as the values.
72
+ def get_filerepo_thumburls
73
+ response = get_filerepoinfo('name|thumbUrl')
74
+ ret = {}
75
+ response['query']['repos'].each { |n, u| ret[n] = u }
76
+ ret
77
+ end
78
+
79
+ # Gets the repository names and their according favicon URLs.
80
+ # @return [Hash] Names as the keys, with their favicons as the values.
81
+ def get_filerepo_favicons
82
+ response = get_filerepoinfo('name|favicon')
83
+ ret = {}
84
+ response['query']['repos'].each { |n, f| ret[n] = f }
85
+ ret
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,13 @@
1
+ require_relative 'siteinfo'
2
+ require_relative 'filerepoinfo'
3
+ require_relative 'userinfo'
4
+
5
+ module MediaWiki
6
+ module Query
7
+ module Meta
8
+ include MediaWiki::Query::Meta::UserInfo
9
+ include MediaWiki::Query::Meta::FileRepoInfo
10
+ include MediaWiki::Query::Meta::UserInfo
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,196 @@
1
+ module MediaWiki
2
+ module Query
3
+ module Meta
4
+ module SiteInfo
5
+ # Gets wiki information. This method should rarely be used by
6
+ # normal users.
7
+ # @param prop [String] The siprop parameter.
8
+ # @return [Response] Parsed full response.
9
+ def get_siteinfo(prop)
10
+ params = {
11
+ action: 'query',
12
+ meta: 'siteinfo',
13
+ siprop: prop
14
+ }
15
+
16
+ post(params)
17
+ end
18
+
19
+ # Gets the statistics for the wiki.
20
+ # @return [Hash] The statistics and their according values.
21
+ def get_statistics
22
+ response = get_siteinfo('statistics')
23
+ ret = {}
24
+ response['query']['statistics'].each { |k, v| ret[k] = v }
25
+ ret
26
+ end
27
+
28
+ # Gets the general information for the wiki.
29
+ # @return [Hash] The general info and their according values.
30
+ def get_general
31
+ response = get_siteinfo('general')
32
+ ret = {}
33
+ response['query']['general'].each { |k, v| ret[k] = v }
34
+ ret
35
+ end
36
+
37
+ # Gets all extensions installed on the wiki.
38
+ # @return [Array] All extension names.
39
+ def get_extensions
40
+ response = get_siteinfo('extensions')
41
+ ret = []
42
+ response['query']['extensions'].each { |e| ret.push(e['name']) }
43
+ ret
44
+ end
45
+
46
+ # Gets all languages and their codes.
47
+ # @return [Hash] All languages. Hash key value pair formatted as
48
+ # code => name.
49
+ def get_languages
50
+ response = get_siteinfo('languages')
51
+ ret = {}
52
+ response['query']['languages'].each { |l| ret[l['code']] = l['*'] }
53
+ ret
54
+ end
55
+
56
+ # Gets all namespaces on the wiki and their IDs. Different from the
57
+ # Namespaces module.
58
+ # @return [Hash] All namespaces, formatted as ID => Name.
59
+ def get_namespaces
60
+ response = get_siteinfo('namespaces')
61
+ ret = {}
62
+ response['query']['namespaces'].each do |id, _|
63
+ idid = response['query']['namespaces'][id]['id']
64
+ name = response['query']['namespaces'][id]['*']
65
+ ret[idid] = name
66
+ end
67
+ ret
68
+ end
69
+
70
+ # Gets all namespace aliases and their IDs.
71
+ # @return [Hash] All aliases, formatted as ID => Alias.
72
+ def get_namespace_aliases
73
+ response = get_siteinfo('namespacealiases')
74
+ ret = {}
75
+ response['query']['namespacealiases'].each do |i|
76
+ ret[i['id']] = i['*']
77
+ end
78
+ ret
79
+ end
80
+
81
+ # Gets all special page aliases.
82
+ # @return [Hash] All aliases, formatted as RealName => Alias.
83
+ def get_special_page_aliases
84
+ response = get_siteinfo('specialpagealiases')
85
+ ret = {}
86
+ response['query']['specialpagealiases'].each do |i|
87
+ ret[i['realname']] = i['aliases']
88
+ end
89
+ ret
90
+ end
91
+
92
+ # Gets all magic words and their aliases.
93
+ # @return [Hash] All magic words, formatted as Name => Alias.
94
+ def get_magic_words
95
+ response = get_siteinfo('magicwords')
96
+ ret = {}
97
+ response['query']['magicwords'].each do |w|
98
+ ret[w['name']] = w['aliases']
99
+ end
100
+ ret
101
+ end
102
+
103
+ # Gets all user groups total.
104
+ # @return [Hash] All groups, formatted as Name => [Rights].
105
+ def get_all_usergroups
106
+ response = get_siteinfo('usergroups')
107
+ ret = {}
108
+ response['query']['usergroups'].each do |g|
109
+ ret[g['name']] = g['rights']
110
+ end
111
+ ret
112
+ end
113
+
114
+ # Gets all file extensions that are allowed to be uploaded.
115
+ # @return [Array] All file extensions.
116
+ def get_allowed_file_extensions
117
+ response = get_siteinfo('fileextensions')
118
+ ret = []
119
+ response['query']['fileextensions'].each do |e|
120
+ ret.push(e['ext'])
121
+ end
122
+ ret
123
+ end
124
+
125
+ # Gets the response for the restrictions siteinfo API. Not really for
126
+ # use by users, mostly for the other two restriction methods.
127
+ def get_restrictions_data
128
+ response = get_siteinfo('restrictions')
129
+ response['query']['restrictions']
130
+ end
131
+
132
+ # Gets all restriction/protection types.
133
+ # @return [Array] All protection types.
134
+ def get_restriction_types
135
+ restrictions = get_restrictions_data
136
+ ret = []
137
+ restrictions['types'].each { |t| ret.push(t) }
138
+ ret
139
+ end
140
+
141
+ # Gets all restriction/protection levels.
142
+ # @return [Array] All protection levels.
143
+ def get_restriction_levels
144
+ restrictions = get_restrictions_data
145
+ ret = []
146
+ restrictions['levels'].each { |l| ret.push(l) }
147
+ ret
148
+ end
149
+
150
+ # Gets all skins and their codes.
151
+ # @return [Hash] All skins, formatted as Code => Name
152
+ def get_skins
153
+ response = get_siteinfo('skins')
154
+ ret = {}
155
+ response['query']['skins'].each do |s|
156
+ ret[s['code']] = s['*']
157
+ end
158
+ ret
159
+ end
160
+
161
+ # Gets all HTML tags added by installed extensions.
162
+ # @return [Array] All extension tags.
163
+ def get_extension_tags
164
+ response = get_siteinfo('extensiontags')
165
+ ret = []
166
+ response['query']['extensiontags'].each do |t|
167
+ ret.push(t)
168
+ end
169
+ ret
170
+ end
171
+
172
+ # Gets all function hooks.
173
+ # @return [Array] All function hooks.
174
+ def get_function_hooks
175
+ response = get_siteinfo('functionhooks')
176
+ ret = []
177
+ response['query']['functionhooks'].each do |h|
178
+ ret.push(h)
179
+ end
180
+ ret
181
+ end
182
+
183
+ # Gets all variables that are usable on the wiki, such as NUMBEROFPAGES.
184
+ # @return [Array] All variable string values.
185
+ def get_variables
186
+ response = get_siteinfo('variables')
187
+ ret = []
188
+ response['query']['variables'].each do |v|
189
+ ret.push(v)
190
+ end
191
+ ret
192
+ end
193
+ end
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,111 @@
1
+ module MediaWiki
2
+ module Query
3
+ module Meta
4
+ module UserInfo
5
+ # Gets meta information for the currently logged in user.
6
+ # @param prop [String] The uiprop to get. Optional.
7
+ # @return [Response/Boolean] Either a full, parsed response, or false if
8
+ # not logged in.
9
+ def get_current_user_meta(prop = nil)
10
+ if @logged_in
11
+ params = {
12
+ action: 'query',
13
+ meta: 'userinfo',
14
+ uiprop: prop
15
+ }
16
+
17
+ post(params)
18
+ else
19
+ return false
20
+ end
21
+ end
22
+
23
+ # Gets the current user's username.
24
+ # @return [String/Boolean] Returns the username, or false.
25
+ def get_current_user_name
26
+ if !@name.nil?
27
+ return @name
28
+ else
29
+ name = get_current_user_meta
30
+ name = name['query']['userinfo']['name'] if name != false
31
+
32
+ name
33
+ end
34
+ end
35
+
36
+ # Returns whether or not the currently logged in user has any unread
37
+ # messages on their talk page.
38
+ # @return [Boolean] True if they have unreads, else false.
39
+ def current_user_hasmsg?
40
+ response = get_current_user_meta('hasmsg')
41
+ if response != false
42
+ if !response['query']['userinfo']['messages'] == ''
43
+ return false
44
+ else
45
+ return true
46
+ end
47
+ else
48
+ return false
49
+ end
50
+ end
51
+
52
+ # Gets a hash-of-arrays containing all the groups the user can add and
53
+ # remove people from.
54
+ # @return [Boolean/Hash] False if get_current_user_meta is false, else
55
+ # a hash containing arrays of all the groups the user can add/remove
56
+ # people from.
57
+ def get_changeable_groups
58
+ response = get_current_user_meta('changeablegroups')
59
+ if response != false
60
+ ret = {}
61
+ add = []
62
+ remove = []
63
+ addself = []
64
+ removeself = []
65
+ changeablegroups = response['query']['userinfo']['changeablegroups']
66
+ changeablegroups['add'].each { |g| add.push(g) }
67
+ changeablegroups['remove'].each { |g| remove.push(g) }
68
+ changeablegroups['add-self'].each { |g| addself.push(g) }
69
+ changeablegroups['remove-self'].each { |g| removeself.push(g) }
70
+ ret['add'] = add
71
+ ret['remove'] = remove
72
+ ret['addself'] = addself
73
+ ret['removeself'] = removeself
74
+ return ret
75
+ else
76
+ return false
77
+ end
78
+ end
79
+
80
+ # Gets the currently logged in user's real name.
81
+ # @return [String/Nil] Nil if they don't have a real name set, or their
82
+ # real name.
83
+ def get_realname
84
+ response = get_current_user_meta('realname')
85
+ if response['query']['userinfo']['realname'] == ''
86
+ return nil
87
+ else
88
+ return response['query']['userinfo']['realname']
89
+ end
90
+ end
91
+
92
+ # Gets the currently logged in user's email address.
93
+ # @return [String/Nil] Nil if their email is not set, or their email.
94
+ def get_email_address
95
+ response = get_current_user_meta('email')
96
+ if response['query']['userinfo']['email'] == ''
97
+ return nil
98
+ else
99
+ return response['query']['userinfo']['email']
100
+ end
101
+ end
102
+
103
+ def get_current_user_options
104
+ response = get_current_user_meta('options')
105
+ ret = {}
106
+ response['query']['userinfo']['options'].each { |k, v| ret[k] = v }
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,107 @@
1
+ module MediaWiki
2
+ module Query
3
+ module Properties
4
+ # Gets the wiki text for the given page. Returns nil if it for some
5
+ # reason cannot get the text, for example, if the page does not exist.
6
+ # @param title [String] The page title
7
+ # @return [String/nil] String containing page contents, or nil
8
+ def get_text(title)
9
+ params = {
10
+ action: 'query',
11
+ prop: 'revisions',
12
+ rvprop: 'content',
13
+ titles: title
14
+ }
15
+
16
+ response = post(params)
17
+ revid = nil
18
+ response['query']['pages'].each { |r, _| revid = r }
19
+
20
+ if response['query']['pages'][revid]['missing'] == ''
21
+ return nil
22
+ else
23
+ return response['query']['pages'][revid]['revisions'][0]['*']
24
+ end
25
+ end
26
+
27
+ # Gets the revision ID for the given page.
28
+ # @param title [String] The page title
29
+ # @return [Int/nil] the ID or nil
30
+ def get_id(title)
31
+ params = {
32
+ action: 'query',
33
+ prop: 'revisions',
34
+ rvprop: 'content',
35
+ titles: title
36
+ }
37
+
38
+ response = post(params)
39
+ response['query']['pages'].each do |revid, _|
40
+ if revid != '-1'
41
+ return revid.to_i
42
+ else
43
+ return nil
44
+ end
45
+ end
46
+ end
47
+
48
+ # Gets the token for the given type. This method should rarely be
49
+ # used by normal users.
50
+ # @param type [String] The type of token.
51
+ # @param title [String] The page title for the token. Optional.
52
+ # @return [String] The token. If the butt isn't logged in, it returns
53
+ # with '+\\'.
54
+ def get_token(type, title = nil)
55
+ if @logged_in == true
56
+ # There is some weird thing with MediaWiki where you must pass a valid
57
+ # inprop parameter in order to get any response at all. This is why
58
+ # there is a displaytitle inprop as well as gibberish in the titles
59
+ # parameter. And to avoid normalization, it's capitalized.
60
+ params = {
61
+ action: 'query',
62
+ prop: 'info',
63
+ inprop: 'displaytitle',
64
+ intoken: type
65
+ }
66
+
67
+ title = 'Somegibberish' if title.nil?
68
+ params[:titles] = title
69
+ response = post(params)
70
+ revid = nil
71
+ response['query']['pages'].each { |r, _| revid = r }
72
+
73
+ # URL encoding is not needed for some reason.
74
+ return response['query']['pages'][revid]["#{type}token"]
75
+ else
76
+ return '+\\'
77
+ end
78
+ end
79
+
80
+ # Gets all categories in the page.
81
+ # @param title [String] The page title.
82
+ # @return [Array/Nil] An array of all the categories, or nil if the title
83
+ # is not an actual page.
84
+ def get_categories_in_page(title)
85
+ params = {
86
+ action: 'query',
87
+ prop: 'categories',
88
+ titles: title
89
+ }
90
+
91
+ response = post(params)
92
+ pageid = nil
93
+ ret = []
94
+ response['query']['pages'].each { |r, _| pageid = r }
95
+ if response['query']['pages'][pageid]['missing'] == ''
96
+ return nil
97
+ else
98
+ response['query']['pages'][pageid]['categories'].each do |c|
99
+ ret.push(c['title'])
100
+ end
101
+ end
102
+
103
+ ret
104
+ end
105
+ end
106
+ end
107
+ end