Skip to content
alexz-enwp edited this page Jan 1, 2015 · 1 revision

A User object corresponds to a user on the wiki. It can be used to get a users groups or block status or block/unblock the user. User objects are hashable and can be compared. They're considered to be equal if their wiki objects are equal and they have the same username.

Note that unlike Files and Categories, User is not a subclass of Page, though userpages can be retrieved through the object.

Constructor

User has 3 constructor parameters, 2 of which are required.

  • site - A Wiki object (required)
  • name - The username - do not include a "User:" prefix (required)
  • check - Whether or not to do API queries to set basic information about the user. If the user is an IP address, it won't do the checks regardless, as it won't return useful information. Default: True

####Note about IP addresses User objects can be created for IP addresses/unregistered users, but they will always be considered to not exist regardless of whether they have any edits. To distiguish between an unregistered username and an IP address, use the isIP instance variable, which will be True if the User is an IPv4 or IPv6 address.

Methods

setUserInfo

setUserInfo()

Sets basic information about the user. Checks whether the account exists. If it does, gets userid, block status, user groups, and editcount. Called automatically by the constructor if check=True. Returns the User object.

getUserPage

getUserPage(check=True, followRedir=False)

Get the Page object for the user's user page. check and followRedir have the same meanings as in the Page constructor. Note that followRedir is False by default in this case.

getTalkPage

getTalkPage(check=True, followRedir=False)

Get the Page object for the user's talk page. check and followRedir have the same meanings as in the Page constructor. Note that followRedir is False by default in this case.

isBlocked

isBlocked(force=False)

Returns True or False if the user is blocked or not. By default it stores the data after it has been loaded once. Set force to True to get it from the server again.

block

block(reason='', expiry=None, anononly=True, nocreate=True, autoblock=True, noemail=False, hidename=False, allowusertalk=True, reblock=False, watchuser=True)

Blocks the user.

  • reason - Block log summary/block reason displayed to the blocked user
  • expiry - Length of block, expiration date, or "infinite" (Default: '' - equivelent to "infinite")
  • anononly - Registered users on the IP address can still edit, only affects IP blocks (Default: True)
  • nocreate - Blocks account creation on the IP address (Default: True)
  • autoblock - Automatically block the IP address used by the user (Default: True)
  • noemail - Prevent the user from sending email through Special:Emailuser (Default: False)
  • hidename - Hide the username in the block log, requres "hideuser" right (Default: False)
  • allowusertalk - Allow the user to edit their talk page (Default: True)
  • reblock - Override an existing block (Default: False)
  • watchuser - Watch the user's userpage/talk page

unblock

unblock(reason='')

Unblock the user with a reason for the block log.

Instance variables

Public

  • site - The Wiki object passed to the constructor.
  • name - The username or IP address. IPv6 addresses will be automatically changed to their most compact form
  • exists - For user accounts, None if existence hasn't been checked with setUserInfo(), otherwise True or False if the page exists on the wiki. Will always be False for IPs
  • blocked - None if not checked, otherwise True or False if the user is blocked. Use isBlocked to check
  • editcount - For registered users, their editcount. Will always be 0 for IPs
  • groups - A list of groups the user is in. Will be empty for unregistered accounts and ['*'] for IPs
  • id - The userid of the user, set by setUserInfo(), 0 for unregistered/unchecked accounts and IPs
  • isIP - True or False, whether the user is a valid IPv4 or IPv6 address.

Examples

Get some information about a user

from wikitools import wiki, user
site = wiki.Wiki("https://www.mediawiki.org/w/api.php")
u = user.User(site, "Mr.Z-man")
print(u.editcount)
print(u.groups)

Outputs

240
['coder', 'sysop', '*', 'user', 'autoconfirmed']

Block a user, and notify them on their talk page

from wikitools import wiki, user
site = wiki.Wiki("https://www.mediawiki.org/w/api.php")
u = user.User(site, "A Vandal")
u.block(reason="Vandalism", expiry="infinite")
talk = u.getTalkPage()
notice = """
==Blocked==
You're a vandal, so I blocked you. ~~~~
"""
talk.edit(appendtext=notice, summary="Blocked")
Clone this wiki locally