module MediaWiki::Gateway::Users

Public Instance Methods

contributions(user, count = nil, options = {}) click to toggle source

Get user contributions

user

The user name

count

Maximum number of contributions to retrieve, or nil for all

options

Optional hash of options, eg. { 'ucnamespace' => 4 }. See www.mediawiki.org/wiki/API:Usercontribs

Returns array of hashes containing the “item” attributes defined here: www.mediawiki.org/wiki/API:Usercontribs

   # File lib/media_wiki/gateway/users.rb
45 def contributions(user, count = nil, options = {})
46   result = []
47 
48   iterate_query('usercontribs', '//item', nil, 'uccontinue', options.merge(
49     'ucuser'  => user,
50     'uclimit' => @options[:limit]
51   )) { |element|
52     result << hash = {}
53     element.attributes.each { |key, value| hash[key] = value }
54     break if count && result.size >= count
55   }
56 
57   count ? result.take(count) : result
58 end
create_account(options) click to toggle source

Create a new account

options

is Hash passed as query arguments. See www.mediawiki.org/wiki/API:Account_creation#Parameters for more information.

   # File lib/media_wiki/gateway/users.rb
83 def create_account(options)
84   send_request(options.merge('action' => 'createaccount'))
85 end
email_user(user, subject, text, options = {}) click to toggle source

Sends e-mail to a user

user

Username to send mail to (name only: eg. 'Bob', not 'User:Bob')

subject

Subject of message

content

Content of message

options

Hash of additional options

Will raise a 'noemail' APIError if the target user does not have a confirmed email address, see www.mediawiki.org/wiki/API:E-mail for details.

   # File lib/media_wiki/gateway/users.rb
68 def email_user(user, subject, text, options = {})
69   res = send_request(options.merge(
70     'action'  => 'emailuser',
71     'target'  => user,
72     'subject' => subject,
73     'text'    => text,
74     'token'   => get_token('email', "User:#{user}")
75   ))
76 
77   res.elements['emailuser'].attributes['result'] == 'Success'
78 end
login(username, password, domain = 'local', options = {}) click to toggle source

Login to MediaWiki

username

Username

password

Password

domain

Domain for authentication plugin logins (eg. LDAP), optional – defaults to 'local' if not given

options

Hash of additional options

Throws MediaWiki::Unauthorized if login fails

   # File lib/media_wiki/gateway/users.rb
15 def login(username, password, domain = 'local', options = {})
16   send_request(options.merge(
17     'action'     => 'login',
18     'lgname'     => username,
19     'lgpassword' => password,
20     'lgdomain'   => domain
21   ))
22 
23   @password = password
24   @username = username
25 end
options(changes = {}, optionname = nil, optionvalue = nil, reset = false, options = {}) click to toggle source

Sets options for currenlty logged in user

changes

a Hash that will be transformed into an equal sign and pipe-separated key value parameter

optionname

a String indicating which option to change (optional)

optionvalue

the new value for optionname - allows pipe characters (optional)

reset

a Boolean indicating if all preferences should be reset to site defaults (optional)

options

Hash of additional options

    # File lib/media_wiki/gateway/users.rb
 94 def options(changes = {}, optionname = nil, optionvalue = nil, reset = false, options = {})
 95   form_data = options.merge(
 96     'action' => 'options',
 97     'token'  => get_options_token
 98   )
 99 
100   if changes && !changes.empty?
101     form_data['change'] = changes.map { |key, value| "#{key}=#{value}" }.join('|')
102   end
103 
104   if optionname && !optionname.empty?
105     form_data[optionname] = optionvalue
106   end
107 
108   if reset
109     form_data['reset'] = true
110   end
111 
112   send_request(form_data)
113 end
set_groups(user, groups_to_add = [], groups_to_remove = [], comment = '', options = {}) click to toggle source

Set groups for a user

user

Username of user to modify

groups_to_add

Groups to add user to, as an array or a string if a single group (optional)

groups_to_remove

Groups to remove user from, as an array or a string if a single group (optional)

options

Hash of additional options

    # File lib/media_wiki/gateway/users.rb
121 def set_groups(user, groups_to_add = [], groups_to_remove = [], comment = '', options = {})
122   token = get_userrights_token(user)
123   userrights(user, token, groups_to_add, groups_to_remove, comment, options)
124 end
users(options = {}) click to toggle source

Get a list of users

options

Optional hash of options, eg. { 'augroup' => 'sysop' }. See www.mediawiki.org/wiki/API:Allusers

Returns array of user names (empty if no matches)

   # File lib/media_wiki/gateway/users.rb
32 def users(options = {})
33   iterate_query('allusers', '//u', 'name', 'aufrom', options.merge(
34     'aulimit' => @options[:limit]
35   ))
36 end

Private Instance Methods

get_options_token() click to toggle source
    # File lib/media_wiki/gateway/users.rb
152 def get_options_token
153   send_request('action' => 'tokens', 'type' => 'options')
154     .elements['tokens'].attributes['optionstoken']
155 end
get_userrights_token(user) click to toggle source

User rights management (aka group assignment)

    # File lib/media_wiki/gateway/users.rb
129 def get_userrights_token(user)
130   res = send_request(
131     'action'  => 'query',
132     'list'    => 'users',
133     'ustoken' => 'userrights',
134     'ususers' => user
135   )
136 
137   token = res.elements['query/users/user'].attributes['userrightstoken']
138 
139   @log.debug("RESPONSE: #{res.to_s}")
140 
141   unless token
142     if res.elements['query/users/user'].attributes['missing']
143       raise APIError.new('invaliduser', "User '#{user}' was not found (get_userrights_token)")
144     else
145       raise Unauthorized.new("User '#{@username}' is not permitted to perform this operation: get_userrights_token")
146     end
147   end
148 
149   token
150 end
userrights(user, token, groups_to_add, groups_to_remove, reason, options = {}) click to toggle source
    # File lib/media_wiki/gateway/users.rb
157 def userrights(user, token, groups_to_add, groups_to_remove, reason, options = {})
158   # groups_to_add and groups_to_remove can be a string or an array. Turn them into MediaWiki's pipe-delimited list format.
159   if groups_to_add.is_a?(Array)
160     groups_to_add = groups_to_add.join('|')
161   end
162 
163   if groups_to_remove.is_a?(Array)
164     groups_to_remove = groups_to_remove.join('|')
165   end
166 
167   send_request(options.merge(
168     'action' => 'userrights',
169     'user'   => user,
170     'token'  => token,
171     'add'    => groups_to_add,
172     'remove' => groups_to_remove,
173     'reason' => reason
174   ))
175 end