Page.can
Page.can() determines whether the logged-in user (or the anonymous user, if not logged in) can perform a particular action on this page. The result depends both on the user's privileges and the protection level of the page. For example, the following code tests if the user can edit the article on grapefruit:
page = site.Pages['Grapefruit']
if page.can('edit'):
text = page.edit()
text += "\n[[Category:Fruits]]"
page.save(text, summary='Add to category Fruits')
else:
print 'Cannot edit Grapefruit article'
It can be helpful in converting exception-handling code into more straightforward procedural code. However, it should be used with caution; see Notes.
- action: a user right (string) - see list of permissions for options
- Not all rights are applicable. Typical rights are 'read', 'edit', 'move', 'delete', and 'protect'.
Returns true if the logged-in user can perform the specified action on this page at the present time, or false otherwise.
Cannot raise exceptions.
It is possible, although improbable, for the protection status of a page to change between the call to can()
and a future call accessing the page, even if they are called one immediately after the other. Therefore the call accessing the page may still fail and throw an exception, and this exception must still be caught and handled.
The can()
method is invoked internally by calls that access pages, such as Page.edit, Page.save, Page.move, and Page.delete, and they will throw an exception if the user is not permitted to access the page. Therefore protecting such calls with can()
is redundant.
With administrative users, it may be reported that they cannot move a protected page when in fact they can.
This page was originally imported from the old mwclient wiki at SourceForge. The imported version was dated from 05:11, 6 February 2012, and its only editor was Derrickcoetzee (@dcoetzee).
- Querying Wikipedia with mwclient
- Replacing a string for every page in a category
- Creating a page listing all pages in a category
Note: the red links below are pages yet to be created. Feel free to add them!