mediawiki-butt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e92d6477086ed671ae506b7dab213c47777e004c
4
+ data.tar.gz: 3bcb59b71c5d084cb8f27f365934710e2e60015f
5
+ SHA512:
6
+ metadata.gz: 4048a9e68a138b23e8412e4c991504fb894a20632f0fe673b4c9b62ee169d03e8a965991e912a61d23c4466a6fe60de97a21b0db2c10f2becad7a727468e5bc9
7
+ data.tar.gz: 2cc73a41fae56f3e0c535f589e3ee9fecde5f6b74892e7bd968035739126d818e17f935b8aaf6c2a1494aca5b65aa89369c2dc9d693f086fe1110084eb267d44
data/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # Changelog
2
+ ## Version 0
3
+ ### Version 0.1.0
4
+ * Initial version.
@@ -0,0 +1 @@
1
+ require_relative 'mediawiki/butt'
@@ -0,0 +1,185 @@
1
+ require_relative 'exceptions'
2
+
3
+ module MediaWiki
4
+ module Auth
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.
16
+ def check_login(result, secondtry)
17
+ if result == "Success"
18
+ @logged_in = true
19
+ return true
20
+ elsif result == "NeedToken" && secondtry == true
21
+ raise MediaWiki::Butt::NeedTokenMoreThanOnceError
22
+ return false
23
+ elsif result == "NoName"
24
+ raise MediaWiki::Butt::NoNameError
25
+ return false
26
+ elsif result == "Illegal"
27
+ raise MediaWiki::Butt::IllegalUsernameError
28
+ return false
29
+ elsif result == "NotExists"
30
+ raise MediaWiki::Butt::UsernameNotExistsError
31
+ return false
32
+ elsif result == "EmptyPass"
33
+ raise MediaWiki::Butt::EmptyPassError
34
+ return false
35
+ elsif result == "WrongPass"
36
+ raise MediaWiki::Butt::WrongPassError
37
+ return false
38
+ elsif result == "WrongPluginPass"
39
+ raise MediaWiki::Butt::WrongPluginPassError
40
+ return false
41
+ elsif result == "CreateBlocked"
42
+ raise MediaWiki::Butt::CreateBlockedError
43
+ return false
44
+ elsif result == "Throttled"
45
+ raise MediaWiki::Butt::ThrottledError
46
+ return false
47
+ elsif result == "Blocked"
48
+ raise MediaWiki::Butt::BlockedError
49
+ return false
50
+ end
51
+ 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")
63
+ def login(username, password)
64
+ params = {
65
+ action: 'login',
66
+ lgname: username,
67
+ lgpassword: password,
68
+ format: 'json'
69
+ }
70
+
71
+ result = post(params, true, false)
72
+ if check_login(result["login"]["result"], false) == true
73
+ @logged_in = true
74
+ @tokens.clear
75
+ elsif result["login"]["result"] == "NeedToken" && result["login"]["token"] != nil
76
+ token = result["login"]["token"]
77
+ token_params = {
78
+ action: 'login',
79
+ lgname: username,
80
+ lgpassword: password,
81
+ format: 'json',
82
+ lgtoken: token
83
+ }
84
+
85
+ #Consider refactor the @cookie initialization.
86
+ @cookie = "#{result["login"]["cookieprefix"]}Session=#{result["login"]["sessionid"]}"
87
+ result = post(token_params, true, true)
88
+ return check_login(result["login"]["result"], true)
89
+ end
90
+ end
91
+
92
+ # Logs the current user out
93
+ #
94
+ # ==== Examples
95
+ #
96
+ # => butt.login("MyUsername", "My5up3r53cur3P@55w0rd")
97
+ # => # do stuff
98
+ # => butt.logout
99
+ def logout
100
+ if @logged_in == true
101
+ params = {
102
+ action: 'logout'
103
+ }
104
+
105
+ post(params)
106
+ @logged_in = false
107
+ @tokens.clear
108
+ end
109
+ end
110
+
111
+ # 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")
123
+ def create_account(username, password, language = 'en', *reason)
124
+ params = {
125
+ name: username,
126
+ password: password,
127
+ reason: reason,
128
+ language: language,
129
+ token: ''
130
+ }
131
+
132
+ result = post(params)
133
+
134
+ if result["createaccount"]["result"] == "Success"
135
+ @tokens.clear
136
+ elsif result["createaccount"]["result"] == "NeedToken"
137
+ params = {
138
+ name: username,
139
+ password: password,
140
+ reason: reason,
141
+ language: language,
142
+ token: result["createaccount"]["token"]
143
+ }
144
+ end
145
+ end
146
+
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")
159
+ def create_account_email(username, email, language = 'en', *reason)
160
+ params = {
161
+ name: username,
162
+ email: email,
163
+ mailpassword: 'value',
164
+ reason: reason,
165
+ language: language,
166
+ token: ''
167
+ }
168
+
169
+ result = post(params)
170
+
171
+ if result["createaccount"]["result"] == "Success"
172
+ @tokens.clear
173
+ elsif result["createaccount"]["result"] == "NeedToken"
174
+ params = {
175
+ name: username,
176
+ email: email,
177
+ mailpassword: 'value',
178
+ reason: reason,
179
+ language: language,
180
+ token: result["createaccount"]["token"]
181
+ }
182
+ end
183
+ end
184
+ end
185
+ end
@@ -0,0 +1,92 @@
1
+ require_relative 'auth'
2
+ require_relative 'query'
3
+ require 'httpclient'
4
+ require 'json'
5
+
6
+ module MediaWiki
7
+ class Butt
8
+ include MediaWiki::Auth
9
+ include MediaWiki::Query::Meta
10
+ include MediaWiki::Query::Properties
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")
23
+ #
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)
26
+ def initialize(url, use_ssl = true)
27
+ if url =~ /api.php$/
28
+ @url = url
29
+ else
30
+ @url = "#{url}/api.php"
31
+ end
32
+
33
+ @client = HTTPClient.new
34
+ @uri = URI.parse(@url)
35
+ @ssl = use_ssl
36
+ @logged_in = false
37
+ @tokens = {}
38
+ end
39
+
40
+ # 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
+ #
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'})
51
+ def post(params, autoparse = true, setcookie = false)
52
+ if setcookie == true
53
+ header = { 'Set-Cookie' => @cookie }
54
+ response = @client.post(@uri, params, header)
55
+ else
56
+ response = @client.post(@uri, params)
57
+ end
58
+
59
+ if autoparse == true
60
+ return JSON.parse(response.body)
61
+ else
62
+ return response
63
+ end
64
+ end
65
+
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.
68
+ def is_current_user_bot()
69
+ if @logged_in == true
70
+ params = {
71
+ action: 'query',
72
+ meta: 'userinfo',
73
+ uiprop: 'groups',
74
+ format: 'json'
75
+ }
76
+
77
+ ret = Array.new
78
+ response = post(params)
79
+ response["query"]["userinfo"]["groups"].each do |g|
80
+ if g == "bot"
81
+ return true
82
+ else
83
+ next
84
+ end
85
+ end
86
+ return false
87
+ else
88
+ return false
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,65 @@
1
+ module MediaWiki
2
+ class Butt
3
+ class AuthenticationError < StandardError; end
4
+
5
+ class NeedTokenMoreThanOnceError < AuthenticationError
6
+ def message
7
+ "You tried to get the token more than once. You likely have some problem with your login call."
8
+ end
9
+ end
10
+
11
+ class NoNameError < AuthenticationError
12
+ def message
13
+ "You did not set the lgname parameter."
14
+ end
15
+ end
16
+
17
+ class IllegalUsernameError < AuthenticationError
18
+ def message
19
+ "You provided an illegal username."
20
+ end
21
+ end
22
+
23
+ class UsernameNotExistsError < AuthenticationError
24
+ def message
25
+ "You provided a username that does not exist."
26
+ end
27
+ end
28
+
29
+ class EmptyPassError < AuthenticationError
30
+ def message
31
+ "You did not set the lgpassword paremeter."
32
+ end
33
+ end
34
+
35
+ class WrongPassError < AuthenticationError
36
+ def message
37
+ "The password you provided is not correct."
38
+ end
39
+ end
40
+
41
+ class WrongPluginPassError < AuthenticationError
42
+ def message
43
+ "A plugin (not MediaWiki) claims your password is not correct."
44
+ end
45
+ end
46
+
47
+ class CreateBlockedError < AuthenticationError
48
+ def message
49
+ "MediaWiki tried to automatically create an account for you, but your IP is blocked from account creation."
50
+ end
51
+ end
52
+
53
+ class ThrottledError < AuthenticationError
54
+ def message
55
+ "You've logged in too many times."
56
+ end
57
+ end
58
+
59
+ class BlockedError < AuthenticationError
60
+ def message
61
+ "User is blocked."
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,154 @@
1
+ module MediaWiki
2
+ module Query
3
+ #TODO: Actually decide on a good way to deal with meta information queries.
4
+ # The metainformation could probably be handled in a much less verbose way.
5
+ module Meta
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()
12
+ def get_filerepo_names()
13
+ params = {
14
+ action: 'query',
15
+ meta: 'filerepoinfo',
16
+ friprop: 'name',
17
+ format: 'json'
18
+ }
19
+
20
+ result = post(params)
21
+ array = Array.new
22
+ result["query"]["repos"].each do |repo|
23
+ array.push(repo["name"])
24
+ end
25
+ return array
26
+ end
27
+ end
28
+
29
+ module Properties
30
+
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")
40
+ def get_text(title)
41
+ params = {
42
+ action: 'query',
43
+ prop: 'revisions',
44
+ rvprop: 'content',
45
+ format: 'json',
46
+ titles: title
47
+ }
48
+
49
+ response = post(params)
50
+ response["query"]["pages"].each do |revid, data|
51
+ $revid = revid
52
+ end
53
+
54
+ if response["query"]["pages"][$revid]["missing"] == ""
55
+ return nil
56
+ else
57
+ return response["query"]["pages"][$revid]["revisions"][0]["*"]
58
+ end
59
+ end
60
+ end
61
+
62
+ 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
76
+ def what_links_here(title, limit = 500)
77
+ params = {
78
+ action: 'query',
79
+ bltitle: title,
80
+ format: 'json'
81
+ }
82
+
83
+ if limit > 500
84
+ if is_current_user_bot() == true
85
+ if limit > 5000
86
+ params[:bllimit] = 5000
87
+ else
88
+ params[:bllimit] = limit
89
+ end
90
+ else
91
+ params[:bllimit] = 500
92
+ end
93
+ else
94
+ params[:bllimit] = limit
95
+ end
96
+
97
+ ret = Array.new
98
+ response = post(params)
99
+ response["query"]["backlinks"].each do |bl|
100
+ ret.push(bl["title"])
101
+ end
102
+ return ret
103
+ 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
117
+ def get_category_members(category, limit = 500)
118
+ params = {
119
+ action: 'query',
120
+ list: 'categorymembers',
121
+ #cmprop: 'title',
122
+ format: 'json'
123
+ }
124
+
125
+ if category =~ /Category\:/
126
+ params[:cmtitle] = category
127
+ else
128
+ params[:cmtitle] = "Category:#{category}"
129
+ end
130
+
131
+ if limit > 500
132
+ if is_current_user_bot() == true
133
+ if limit > 5000
134
+ params[:cmlimit] = 5000
135
+ else
136
+ params[:cmlimit] = limit
137
+ end
138
+ else
139
+ params[:cmlimit] = 500
140
+ end
141
+ else
142
+ params[:cmlimit] = limit
143
+ end
144
+
145
+ ret = Array.new
146
+ response = post(params)
147
+ response["query"]["categorymembers"].each do |cm|
148
+ ret.push(cm["title"])
149
+ end
150
+ return ret
151
+ end
152
+ end
153
+ end
154
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mediawiki-butt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eli Foster
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpclient
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: |2
42
+ MediaWiki::Butt is a Ruby Gem that provides a fully-featured MediaWiki API interface.
43
+ email: elifosterwy@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - CHANGELOG.md
49
+ - lib/mediawiki-butt.rb
50
+ - lib/mediawiki/auth.rb
51
+ - lib/mediawiki/butt.rb
52
+ - lib/mediawiki/exceptions.rb
53
+ - lib/mediawiki/query.rb
54
+ homepage: https://github.com/ftb-gamepedia/mediawiki-butt-ruby
55
+ licenses:
56
+ - CC-BY-NC-ND-4.0
57
+ metadata:
58
+ issue_tracker: https://github.com/ftb-gamepedia/mediawiki-butt-ruby/issues
59
+ post_install_message: ONE OF US! ONE OF US!
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '2.0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.4.5
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Interacting with the MediaWiki API
79
+ test_files: []