wiki
Classes for use by a client to interact with a MediaWiki instance's API
Wiki
General wiki-interfacing functionality and config data
__del__()
Finalizer, releases resources used by the internal requests session
__init__(domain='en.wikipedia.org', username=None, password=None, cookie_jar=Path('.'), api_endpoint=None)
Initializer, creates a new Wiki object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
domain |
str
|
The shorthand domain of the Wiki to target. Defaults to "en.wikipedia.org". |
'en.wikipedia.org'
|
username |
str
|
The username to login as. If |
None
|
password |
str
|
The password to use when logging in. Does nothing if |
None
|
cookie_jar |
Path
|
The directory to save/read cookies to/from. Disable by setting this to |
Path('.')
|
api_endpoint |
str
|
The base API endpoint on your wiki. This usually looks something like |
None
|
Raises:
Type | Description |
---|---|
RuntimeError
|
If |
__repr__()
Generate a str representation of this Wiki object. Useful for logging.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A str representation of this Wiki object. |
all_users(groups=[])
Lists all users on a wiki. Can filter users by right(s) they have been assigned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
groups |
Union[list[str], str]
|
The group(s) to filter by (e.g. |
[]
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: a |
categories_on_page(title)
Fetch the categories used on a page.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to query. |
required |
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: The |
category_members(title, ns=[])
Fetches the elements in a category.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title of the category to fetch elements from. Must include |
required |
ns |
Union[list[Union[NS, str]], NS, str]
|
Only return results that are in these namespaces. Optional, set empty list to disable. Defaults to []. |
[]
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: a |
category_size(title)
Queries the wiki and gets the number of pages categorized in title
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The category to get the size of. Must start with |
required |
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The number of pages in this category. |
clear_cookies()
Deletes any saved cookies from disk.
contribs(user, older_first=False, ns=[])
Fetches contributions of a user. Warning: this fetches all of user
's contributions!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user |
str
|
The username to query, excluding the |
required |
older_first |
bool
|
Set |
False
|
ns |
list[Union[NS, str]]
|
Only return results that are in these namespaces. Optional, set empty list to disable. Defaults to []. |
[]
|
Returns:
Type | Description |
---|---|
list[Contrib]
|
list[Contrib]: The contributions of |
convert_ns(title, ns)
Converts the namespace of the specified title to another namespace. PRECONDITION: title
is well-formed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to convert |
required |
ns |
Union[str, NS]
|
The namespace to convert |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
|
delete(title, reason)
Deletes a page. PRECONDITION: wiki
must be logged in and have the ability to delete pages for this to work.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to delete |
required |
reason |
str
|
The reason for deleting this page. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
|
deleted_revisions(title, older_first=False, include_text=False)
Fetches all the deleted revisions of title
. Plan accordingly when querying pages that have many deleted revisions! PRECONDITION: You must be logged in and have admin rights in order for this to work.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to get deleted revisions for. |
required |
older_first |
bool
|
Set |
False
|
include_text |
bool
|
If |
False
|
Returns:
Type | Description |
---|---|
list[Revision]
|
list[Revision]: A |
duplicate_files(title, local_only=True)
Find dupliates of title
if possible.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to get duplicates of. Must start with |
required |
local_only |
bool
|
Set |
True
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: The |
edit(title, text=None, summary='', prepend=None, append=None, minor=False)
Attempts to edit a page on the Wiki. Can replace text or append/prepend text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to edit. |
required |
text |
str
|
Text to replace the current page's contents with. Mutually exclusive with |
None
|
summary |
str
|
The edit summary to use. Defaults to "". |
''
|
prepend |
str
|
Text to prepend to the page. Mutually exclusive with |
None
|
append |
str
|
Text to append to the page. Mutually exclusive with |
None
|
minor |
bool
|
Set |
False
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
|
exists(title)
Query the wiki and determine if title
exists on the wiki.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
the title to query. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
|
external_links(title)
Fetches external links on a page.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to query |
required |
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: The |
file_usage(title)
Fetch the titles of all pages embedding/displaying title
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The file to query. |
required |
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: The |
filter_by_ns(titles, *nsl)
Creates a copy of titles
and strips out any title that isn't in the namespaces specified in nsl
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
titles |
list[str]
|
The list of titles to process. |
required |
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: A copy of |
first_editor_of(title)
Gets the user who created title
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title of the page to query |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The username of the user who created |
global_usage(title)
Fetch the global file usage of a media file. Only works with wikis that utilize a shared media respository wiki.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The file to get global usage usage of. Must start with |
required |
Returns:
Type | Description |
---|---|
list[tuple]
|
list[tuple]: A |
image_info(title)
Fetch image (file) info for media files. This is basically image metadata for each uploaded media file under the specified title. See dwrap.ImageInfo
for details.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The file to get image info of. Must start with the |
required |
Returns:
Type | Description |
---|---|
list[ImageInfo]
|
list[ImageInfo]: The |
images_on_page(title)
Fetch images/media files used on a page.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to query |
required |
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: The |
in_ns(title, ns)
Checks if a title belongs to a namespace or namespaces. This is a lexical operation only, so title
must be well-formed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to check. |
required |
ns |
Union[int, NS, str, tuple[Union[int, NS, str]]]
|
The namespace or namespaces (pass as a |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if |
is_talk_page(title)
Determines if title
is part of a talk page namespace. This is a lexical operation and does not perform an api query.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to check |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
|
last_editor_of(title)
Gets the user who most recently edited title
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title of the page to query |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The username of the user who most recently edited |
links_on_page(title, ns=[])
Fetch wiki links on a page.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to query |
required |
ns |
Union[list[Union[NS, str]], NS, str]
|
Restrict returned output to titles in these namespaces. Optional, set to empty list to disable. Defaults to []. |
[]
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: The |
list_duplicate_files()
List files on a wiki which have duplicates by querying the Special page Special:ListDuplicatedFiles
. This reads the entire list, and may return up to 5000 elements.
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: A |
list_user_rights(username=None)
Lists user rights for the specified user.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username |
str
|
The user to get rights for. Usernames must be well formed (e.g. no wacky capitalization), and must not contain the |
None
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: The rights for the specified user. |
login(username, password=None)
Attempts to login this Wiki object. If successful, all future calls will be automatically include authentication. If password
is not set, then the
method will try looking using an environment variable of the form <username>_PW
, where <username>
is username
capitalized, with all spaces replaced with _
.
If username
and/or password
are evaluated to be empty/None
, then False
will be returned immediately.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
username |
str
|
The username to login with |
required |
password |
str
|
The password to login with. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
|
logs(title=None, log_type=None, log_action=None, user=None, ns=None, tag=None, start=None, end=None, older_first=False)
Fetches Special:Log
entries from a wiki. PRECONDITION: if start
and end
are both set, then start
must occur before end
. WARNING: Not recommended to call this with no arguments on large Wikis, this methods returns all matching logs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title of the page to get logs for, if applicable. Defaults to None. |
None
|
log_type |
str
|
The type of log to fetch (e.g. |
None
|
log_action |
str
|
The type and sub-action of the log to fetch (e.g. |
None
|
user |
str
|
The user associated with the log action, if applicable. Do not include |
None
|
ns |
Union[NS, str]
|
Only return results that are in this namespace. Defaults to None. |
None
|
tag |
str
|
Only return results that are tagged with this tag. Defaults to None. |
None
|
start |
datetime
|
Set to filter out revisions older than this date. If no timezone is specified in the datetime, then UTC is assumed. Defaults to None. |
None
|
end |
datetime
|
Set to filter out revisions newer than this date. If no timezone is specified in the datetime, then UTC is assumed. Defaults to None. |
None
|
older_first |
bool
|
Set to |
False
|
Returns:
Type | Description |
---|---|
list[Log]
|
list[Log]: A |
normalize_title(title)
Normalizes titles to match their canonical versions. Usually this means fixing capitalization or replacing underscores with spaces.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to normalize. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The normalized version of |
not_in_ns(title, ns)
Checks if a title does not belong to a namespace or namespaces. This is a lexical operation only, so title
must be well-formed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to check. |
required |
ns |
Union[int, NS, str, tuple[Union[int, NS, str]]]
|
The namespace or namespaces (pass as a |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if |
nss(title)
Strips the namespace prefix from a title.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to remove the namespace from. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
|
page_of(title)
Gets the content page associated with title
. If title
is a content page, then None
will be returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The content page associated with |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The content page associated with |
page_text(title)
Queries the Wiki for the text of title
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to get page text of. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The text of |
parse(title=None, text=None)
Parses the title or text into WikiText
/WTemplate
objects. If title
and text
are both specified, then text
will be parsed as if it was on title
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to use. If |
None
|
text |
str
|
The text to parse. If |
None
|
Returns:
Name | Type | Description |
---|---|---|
WikiText |
WikiText
|
The result of the parsing operation. |
prefix_index(ns, prefix)
Performs a prefix index query and returns all matching titles.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ns |
Union[NS, str]
|
The namespace to search in. |
required |
prefix |
str
|
Fetches all titles in the specified namespace that start with this str. To return subpages only, append a |
required |
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: A |
purge(titles)
Attempts to purge the server-side caches of titles
. Exits and outputs messages to standard out on the first failure.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
titles |
Iterable[str]
|
The titles to purge |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
|
random(ns=[])
Fetches a random page from the wiki.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ns |
list[Union[NS, str]]
|
Only return results that are in these namespaces. Optional, set empty list to disable. Defaults to []. |
[]
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
A random page from the wiki as specified, or |
replace_text(title, pattern, replacement='', summary='')
Convenience method, edits title
and replaces all instances of target_text
with replacement
in its text.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The page to edit. |
required |
pattern |
Union[Pattern, str]
|
The regular expression describing the text to replace on |
required |
replacement |
str
|
The text to replace all matches of |
''
|
summary |
str
|
The edit summary to use. Defaults to "". |
''
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the operation was successful. |
resolve_redirect(title)
Fetch the target of a redirect page.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to query |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The redirect target. If |
revisions(title, older_first=False, start=None, end=None, include_text=False)
Fetches all the revisions of title
. Plan accordingly when querying pages that have many revisions!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to get revisions for. |
required |
older_first |
bool
|
Set |
False
|
start |
datetime
|
Set to filter out revisions older than this date. If no timezone is specified in the datetime, then UTC is assumed. Defaults to None. |
None
|
end |
datetime
|
Set to filter out revisions newer than this date. If no timezone is specified in the datetime, then UTC is assumed. Defaults to None. |
None
|
include_text |
bool
|
If |
False
|
Returns:
Type | Description |
---|---|
list[Revision]
|
list[Revision]: A |
save_cookies()
Write the cookies of the Wiki object to disk, so they can be used in the future. Does nothing if self.cookie_jar
is set to None
. Raises RuntimeError
if the Wiki
is not logged in.
search(phrase, ns=[])
Perform a search on the wiki.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
phrase |
str
|
The phrase to query with |
required |
ns |
list[Union[NS, str]]
|
Only return results that are in these namespaces. Optional, set empty list to disable. Defaults to []. |
[]
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: A |
stashed_files()
Fetch the user's stashed files. PRECONDITION: You must be logged in for this to work
Returns:
Type | Description |
---|---|
list[tuple[str, int, str]]
|
list[tuple[str, int]]: a |
talk_page_of(title)
Gets the talk page of title
. If title
is a talk page, then None
will be returned.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The talk page associated with |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The talk page of |
templates_on_page(title)
Fetch templates transcluded on a page.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to query |
required |
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: A list of tempalates transcluded on |
undelete(title, reason, revs=None)
Undeletes a page. PRECONDITION: wiki
must be logged in and have the ability to restore pages for this to work.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to restore |
required |
reason |
str
|
The reason for restoring this page. |
required |
revs |
list[Revision]
|
A list of revisions to restore. If not set, then all deleted revisions will be restored. Defaults to None. |
None
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
|
upload(path, title, desc='', summary='', max_retries=5)
Uploads a file to the target Wiki.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path |
Path
|
the local path on your computer pointing to the file to upload |
required |
title |
str
|
The title to upload the file to, excluding the " |
required |
desc |
str
|
The text to go on the file description page. Defaults to "". |
''
|
summary |
str
|
The upload log summary to use. Defaults to "". |
''
|
max_retries |
int
|
The maximum number of retry attempts in the event of an error. Defaults to 5. |
5
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
|
uploadable_filetypes()
Queries the Wiki for all acceptable file types which may be uploaded to this Wiki. PRECONDITION: the target Wiki permits file uploads.
Returns:
Name | Type | Description |
---|---|---|
set |
set
|
A |
user_uploads(user)
Gets the uploads of a user.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
user |
str
|
The username to query, without the |
required |
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: A |
what_links_here(title, redirects_only=False, ns=[])
Fetch pages that wiki link (locally) to a page.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to query |
required |
redirects_only |
bool
|
Set |
False
|
ns |
Union[list[Union[NS, str]], NS, str]
|
Restrict returned output to titles in these namespaces. Optional, set to empty list to disable. Defaults to []. |
[]
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: The list of pages that link to |
what_transcludes_here(title, ns=[])
Fetch pages that translcude a page. If querying for templates, you must include the Template:
prefix.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to query |
required |
ns |
Union[list[Union[NS, str]], NS, str]
|
Restrict returned output to titles in these namespaces. Optional, set to empty list to disable. Defaults to []. |
[]
|
Returns:
Type | Description |
---|---|
list[str]
|
list[str]: The list of pages that transclude |
which_ns(title)
Determines which namespace a title belongs to.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
title |
str
|
The title to get the namespace of |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The namespace, without it's |
whoami()
Get this Wiki's username from the server. If not logged in, then this will return your external IP address.
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
If logged in, this Wiki's username. Otherwise, the external IP address of your device. |