site
— MediaWiki sites#
Library module representing MediaWiki sites (wikis).
BaseSite
— Base Class for Sites#
Objects with site methods independent of the communication interface.
- class pywikibot.site._basesite.BaseSite(code, fam=None, user=None)[source]#
Bases:
ComparableMixin
Site methods that are independent of the communication interface.
- Parameters:
code (str) – the site’s language code
fam (str or pywikibot.family.Family) – wiki family name (optional)
user (str) – bot user name (optional)
- property code#
The identifying code for this Site equal to the wiki prefix.
By convention, this is usually an ISO language code, but it does not have to be.
- property doc_subpage: tuple#
Return the documentation subpage for this Site.
- property family#
The Family object for this Site’s wiki family.
- isInterwikiLink(text)[source]#
Return True if text is in the form of an interwiki link.
If a link object constructed using “text” as the link text parses as belonging to a different site, this method returns True.
- property lang#
The ISO language code for this Site.
Presumed to be equal to the site code, but this can be overridden.
- lock_page(page, block=True)[source]#
Lock page for writing. Must be called before writing any page.
We don’t want different threads trying to write to the same page at the same time, even to different sections.
- Parameters:
page (Page) – the page to be locked
block (bool) – if true, wait until the page is available to be locked; otherwise, raise an exception if page can’t be locked
- property namespaces#
Return dict of valid namespaces on this wiki.
- ns_normalize(value)[source]#
Return canonical local form of namespace name.
- Parameters:
value (str) – A namespace name
- redirectRegex(pattern=None)[source]#
Return a compiled regular expression matching on redirect pages.
Group 1 in the regex match object will be the target title.
- Parameters:
pattern (str | None) –
- Return type:
Pattern[str]
- sametitle(title1, title2)[source]#
Return True if title1 and title2 identify the same wiki page.
title1 and title2 may be unequal but still identify the same page, if they use different aliases for the same namespace.
- Parameters:
title1 (str) –
title2 (str) –
- Return type:
bool
- property sitename#
String representing this Site’s name and code.
- property throttle#
Return this Site’s throttle. Initialize a new one if needed.
APISite
— API Interface for Sites#
Objects representing API interface to MediaWiki site.
- class pywikibot.site._apisite.APISite(code, fam=None, user=None)[source]#
Bases:
BaseSite
,EchoMixin
,FlowMixin
,GeneratorsMixin
,GeoDataMixin
,GlobalUsageMixin
,LinterMixin
,PageImagesMixin
,ProofreadPageMixin
,TextExtractsMixin
,ThanksFlowMixin
,ThanksMixin
,UrlShortenerMixin
,WikibaseClientMixin
API interface to MediaWiki site.
Do not instantiate directly; use
pywikibot.Site
function.- Parameters:
code (str) –
fam (str | pywikibot.family.Family | None) –
user (str | None) –
- class OnErrorExc(exception, on_new_page)#
Bases:
tuple
Create new instance of OnErrorExc(exception, on_new_page)
- exception#
Alias for field number 0
- on_new_page#
Alias for field number 1
- property article_path: str#
Get the nice article path without $1.
Deprecated since version 7.0: Replaced by
articlepath()
- property articlepath: str#
Get the nice article path with placeholder.
New in version 7.0: Replaces
article_path()
- static assert_valid_iter_params(msg_prefix, start, end, reverse, is_ts=True)[source]#
Validate iterating API parameters.
- Parameters:
msg_prefix (str) – The calling method name
start (datetime | int | str) – The start value to compare
end (datetime | int | str) – The end value to compare
reverse (bool) – The reverse option
is_ts (bool) – When comparing timestamps (with is_ts=True) the start is usually greater than end. Comparing titles this is vice versa.
- Raises:
AssertionError – start/end values are not comparabel types or are in the wrong order
- Return type:
None
- blockuser(user, expiry, reason, anononly=True, nocreate=True, autoblock=True, noemail=False, reblock=False, allowusertalk=False)[source]#
Block a user for certain amount of time and for a certain reason.
See also
- Parameters:
user (pywikibot.page.User) – The username/IP to be blocked without a namespace.
expiry (datetime | str | bool) –
The length or date/time when the block expires. If ‘never’, ‘infinite’, ‘indefinite’ it never does. If the value is given as a str it’s parsed by php’s strtotime function:
The relative format is described there:
It is recommended to not use a str if possible to be independent of the API.
reason (str) – The reason for the block.
anononly (bool) – Disable anonymous edits for this IP.
nocreate (bool) – Prevent account creation.
autoblock (bool) – Automatically block the last used IP address and all subsequent IP addresses from which this account logs in.
noemail (bool) – Prevent user from sending email through the wiki.
reblock (bool) – If the user is already blocked, overwrite the existing block.
allowusertalk (bool) – Whether the user can edit their talk page while blocked.
- Returns:
The data retrieved from the API request.
- Return type:
Dict[str, Any]
- categoryinfo(category)[source]#
Retrieve data on contents of category.
- Parameters:
category (Category) –
- Return type:
Dict[str, int]
- compare(old, diff, difftype='table')[source]#
Corresponding method to the ‘action=compare’ API action.
Hint
Use
diff.html_comparator()
function to parse result.See also
- Parameters:
- Returns:
Returns an HTML string of a diff between two revisions.
- Return type:
str
- data_repository()[source]#
Return the data repository connected to this site.
- Returns:
The data repository if one is connected or None otherwise.
- Return type:
DataSite | None
- delete(page, reason, *, deletetalk=False, oldimage=None)[source]#
Delete a page or a specific old version of a file from the wiki.
Requires appropriate privileges.
Page to be deleted can be given either as Page object or as pageid. To delete a specific version of an image the oldimage identifier must be provided.
New in version 6.1: renamed from
deletepage
Changed in version 6.1: keyword only parameter
oldimage
was added.Changed in version 7.1: keyword only parameter
deletetalk
was added.Changed in version 8.1: raises
exceptions.NoPageError
if page does not exist.- Parameters:
page (pywikibot.page.BasePage | int | str) – Page to be deleted or its pageid.
reason (str) – Deletion reason.
deletetalk (bool) – Also delete the talk page, if it exists.
oldimage (str | None) – oldimage id of the file version to be deleted. If a BasePage object is given with page parameter, it has to be a FilePage.
- Raises:
TypeError, ValueError – page has wrong type/value.
- Return type:
None
- deleterevs(targettype, ids, *, hide=None, show=None, reason='', target=None)[source]#
Delete or undelete specified page revisions, file versions or logs.
See also
If more than one target id is provided, the same action is taken for all of them.
New in version 6.0.
- Parameters:
targettype (str) – Type of target. One of “archive”, “filearchive”, “logging”, “oldimage”, “revision”.
ids (int | str | List[int | str]) – Identifiers for the revision, log, file version or archive.
hide (str | List[str] | None) – What to delete. Can be “comment”, “content”, “user” or a combination of them in pipe-separate form such as “comment|user”.
show (str | List[str] | None) – What to undelete. Can be “comment”, “content”, “user” or a combination of them in pipe-separate form such as “comment|user”.
reason (str) – Deletion reason.
target (pywikibot.page.Page | str | None) – Page object or page title, if required for the type.
- Return type:
None
- editpage(page, summary=None, minor=True, notminor=False, bot=True, recreate=True, createonly=False, nocreate=False, watch=None, **kwargs)[source]#
Submit an edit to be saved to the wiki.
See also
- Parameters:
page (pywikibot.page.BasePage) – The Page to be saved. By default its .text property will be used as the new text to be saved to the wiki
summary (str | None) – the edit summary
minor (bool) – if True (default), mark edit as minor
notminor (bool) – if True, override account preferences to mark edit as non-minor
recreate (bool) – if True (default), create new page even if this title has previously been deleted
createonly (bool) – if True, raise an error if this title already exists on the wiki
nocreate (bool) – if True, raise an error if the page does not exist
watch (str | None) – Specify how the watchlist is affected by this edit, set to one of “watch”, “unwatch”, “preferences”, “nochange”: * watch: add the page to the watchlist * unwatch: remove the page from the watchlist * preferences: use the preference settings (default) * nochange: don’t change the watchlist
bot (bool) – if True, mark edit with bot flag
kwargs (Any) –
- Keyword Arguments:
text – Overrides Page.text
section – Edit an existing numbered section or a new section (‘new’)
prependtext – Prepend text. Overrides Page.text
appendtext – Append text. Overrides Page.text.
undo – Revision id to undo. Overrides Page.text
- Returns:
True if edit succeeded, False if it failed
- Raises:
pywikibot.exceptions.Error – No text to be saved
pywikibot.exceptions.NoPageError – recreate is disabled and page does not exist
pywikibot.exceptions.CaptchaError – config.solve_captcha is False and saving the page requires solving a captcha
- Return type:
bool
- expand_text(text, title=None, includecomments=None)[source]#
Parse the given text for preprocessing and rendering.
e.g expand templates and strip comments if includecomments parameter is not True. Keeps text inside <nowiki></nowiki> tags unchanges etc. Can be used to parse magic parser words like {{CURRENTTIMESTAMP}}.
- Parameters:
text (str) – text to be expanded
title (str | None) – page title without section
includecomments (bool | None) – if True do not strip comments
- Return type:
str
- static fromDBName(dbname, site=None)[source]#
Create a site from a database name using the sitematrix.
Changed in version 8.3.3: changed from classmethod to staticmethod.
- get_globaluserinfo(user=None, force=False)[source]#
Retrieve globaluserinfo from site and cache it.
New in version 7.0.
- Parameters:
user (str | int | None) – The user name or user ID whose global info is retrieved. Defaults to the current user.
force (bool) – Whether the cache should be discarded.
- Returns:
A dict with the following keys and values:
id: user id (numeric str)
home: dbname of home wiki
registration: registration date as Timestamp
groups: list of groups (could be empty)
rights: list of rights (could be empty)
editcount: global editcount
- Raises:
TypeError – Inappropriate argument type of ‘user’
- Return type:
Dict[str, Any]
- get_parsed_page(page)[source]#
Retrieve parsed text of the page using action=parse.
Changed in version 7.1: raises KeyError instead of AssertionError
See also
- Parameters:
page (BasePage) –
- Return type:
str
- get_property_names(force=False)[source]#
Get property names for pages_with_property().
See also
- Parameters:
force (bool) – force to retrieve userinfo ignoring cache
- Return type:
List[str]
- get_searched_namespaces(force=False)[source]#
Retrieve the default searched namespaces for the user.
If no user is logged in, it returns the namespaces used by default. Otherwise it returns the user preferences. It caches the last result and returns it, if the username or login status hasn’t changed.
- Parameters:
force (bool) – Whether the cache should be discarded.
- Returns:
The namespaces which are searched by default.
- Return type:
Set[Namespace]
- get_tokens(types, *args, **kwargs)[source]#
Preload one or multiple tokens.
Usage
>>> site = pywikibot.Site() >>> tokens = site.get_tokens([]) # get all tokens >>> list(tokens.keys()) # result depends on user ['createaccount', 'login'] >>> tokens = site.get_tokens(['csrf', 'patrol']) >>> list(tokens.keys()) ['csrf', 'patrol'] >>> token = site.get_tokens(['csrf']).get('csrf') # get a single token >>> token 'a9f...0a0+\\' >>> token = site.get_tokens(['unknown']) # try an invalid token ... ... # invalid token names shows a warnig and the key is not in result ... WARNING: API warning (tokens) of unknown format: ... {'warnings': 'Unrecognized value for parameter "type": foo'} {}
You should not call this method directly, especially if you only need a specific token. Use
tokens
property instead.Changed in version 8.0:
all
parameter is deprecated. Use an empty list fortypes
instead.Note
args
andkwargs
are not used for deprecation warning only.See also
- Parameters:
types (List[str]) – the types of token (e.g., “csrf”, “login”, “patrol”). If the list is empty all available tokens are loaded. See API documentation for full list of types.
- Returns:
a dict with retrieved valid tokens.
- Return type:
Dict[str, str]
- getcategoryinfo(category)[source]#
Retrieve data on contents of category.
See also
- Parameters:
category (Category) –
- Return type:
None
- getcurrenttimestamp()[source]#
Return the server time as a MediaWiki timestamp string.
It calls
server_time
first so it queries the server to get the current server time.- Returns:
the server time (as ‘yyyymmddhhmmss’)
- Return type:
str
- getmagicwords(word)[source]#
Return list of localized “word” magic words for the site.
- Parameters:
word (str) –
- Return type:
List[str]
- getredirtarget(page)[source]#
Return page object for the redirect target of page.
- Parameters:
page (BasePage) – page to search redirects for
- Returns:
redirect target of page
- Raises:
pywikibot.exceptions.IsNotRedirectPageError – page is not a redirect
RuntimeError – no redirects found
pywikibot.exceptions.CircularRedirectError – page is a circular redirect
pywikibot.exceptions.InterwikiRedirectPageError – the redirect target is on another site
- Return type:
- property globaluserinfo: Dict[str, Any]#
Retrieve globaluserinfo of the current user from site.
To get globaluserinfo for a given user or user ID use
get_globaluserinfo()
method insteadNew in version 3.0.
- has_all_mediawiki_messages(keys, lang=None)[source]#
Confirm that the site defines a set of MediaWiki messages.
- Parameters:
keys (Iterable[str]) – names of MediaWiki messages
lang (str | None) – a language code, default is self.lang
- Return type:
bool
- property has_data_repository: bool#
Return True if site has a shared data repository like Wikidata.
- has_extension(name)[source]#
Determine whether extension
name
is loaded.- Parameters:
name (str) – The extension to check for, case sensitive
- Returns:
If the extension is loaded
- Return type:
bool
- has_group(group)[source]#
Return true if and only if the user is a member of specified group.
Possible values of ‘group’ may vary depending on wiki settings, but will usually include bot.
See also
- Parameters:
group (str) –
- Return type:
bool
- property has_image_repository: bool#
Return True if site has a shared image repository like Commons.
- has_mediawiki_message(key, lang=None)[source]#
Determine if the site defines a MediaWiki message.
- Parameters:
key (str) – name of MediaWiki message
lang (str | None) – a language code, default is self.lang
- Return type:
bool
- has_right(right)[source]#
Return true if and only if the user has a specific right.
Possible values of ‘right’ may vary depending on wiki settings.
See also
- Parameters:
right (str) – a specific right to be validated
- Return type:
bool
- image_repository()[source]#
Return Site object for image repository e.g. commons.
- Return type:
BaseSite | None
- interwiki(prefix)[source]#
Return the site for a corresponding interwiki prefix.
- Raises:
pywikibot.exceptions.SiteDefinitionError – if the url given in the interwiki table doesn’t match any of the existing families.
KeyError – if the prefix is not an interwiki prefix.
- Parameters:
prefix (str) –
- Return type:
- interwiki_prefix(site)[source]#
Return the interwiki prefixes going to that site.
The interwiki prefixes are ordered first by length (shortest first) and then alphabetically.
interwiki(prefix)
is not guaranteed to equalsite
(i.e. the parameter passed to this function).- Parameters:
site (BaseSite) – The targeted site, which might be it’s own.
- Raises:
KeyError – if there is no interwiki prefix for that site.
- Return type:
List[str]
- isBot(username)[source]#
Return True is username is a bot user.
- Parameters:
username (str) –
- Return type:
bool
- is_blocked(force=False)[source]#
Return True when logged in user is blocked.
To check whether a user can perform an action, the method has_right should be used.
See also
New in version 7.0: The
force
parameter- Parameters:
force (bool) – Whether the cache should be discarded.
- Return type:
bool
- is_image_repository()[source]#
Return True if Site object is the image repository.
- Return type:
bool
- is_locked(user=None, force=False)[source]#
Return True when given user is locked globally.
New in version 7.0.
- Parameters:
user (str | int | None) – The user name or user ID. Defaults to the current user.
force (bool) – Whether the cache should be discarded.
- Return type:
bool
- is_oauth_token_available()[source]#
Check whether OAuth token is set for this site.
- Return type:
bool
- is_uploaddisabled()[source]#
Return True if upload is disabled on site.
Example:
>>> site = pywikibot.Site('commons') >>> site.is_uploaddisabled() False >>> site = pywikibot.Site('wikidata') >>> site.is_uploaddisabled() True
- Return type:
bool
- property lang: str#
Return the code for the language of this Site.
- linktrail()[source]#
Build linktrail regex from siteinfo linktrail.
Letters that can follow a wikilink and are regarded as part of this link. This depends on the linktrail setting in LanguageXx.php
New in version 7.3.
- Returns:
The linktrail regex.
- Return type:
str
- list_to_text(args)[source]#
Convert a list of strings into human-readable text.
The MediaWiki messages ‘and’ and ‘word-separator’ are used as separator between the last two arguments. If more than two arguments are given, other arguments are joined using MediaWiki message ‘comma-separator’.
- Parameters:
args (Iterable[str]) – text to be expanded
- Return type:
str
- loadimageinfo(page, history=False, url_width=None, url_height=None, url_param=None)[source]#
Load image info from api and save in page attributes.
The following properties are loaded:
timestamp
,user
,comment
,url
,size
,sha1
,mime
,mediatype
,metadata
,archivename
andbitdepth
. If url_width, url_height or url_param is given, additional propertiesthumbwidth
,thumbheight
,thumburl
andresponsiveUrls
are given.Note
Parameters validation and error handling left to the API call.
Changed in version 8.2: mediatype and bitdepth properties were added.
See also
- Parameters:
history (bool) – if true, return the image’s version history
url_width (int | None) – get info for a thumbnail with given width
url_height (int | None) – get info for a thumbnail with given height
url_param (str | None) – get info for a thumbnail with given param
page (FilePage) –
- Return type:
None
- loadpageinfo(page, preload=False)[source]#
Load page info from api and store in page attributes.
See also
- Parameters:
page (BasePage) –
preload (bool) –
- Return type:
None
- loadpageprops(page)[source]#
Load page props for the given page.
- Parameters:
page (BasePage) –
- Return type:
None
- local_interwiki(prefix)[source]#
Return whether the interwiki prefix is local.
A local interwiki prefix is handled by the target site like a normal link. So if that link also contains an interwiki link it does follow it as long as it’s a local link.
- Raises:
pywikibot.exceptions.SiteDefinitionError – if the url given in the interwiki table doesn’t match any of the existing families.
KeyError – if the prefix is not an interwiki prefix.
- Parameters:
prefix (str) –
- Return type:
bool
- logged_in()[source]#
Verify the bot is logged into the site as the expected user.
The expected usernames are those provided as the user parameter at instantiation.
- Return type:
bool
- login(autocreate=False, user=None, *, cookie_only=False)[source]#
Log the user in if not already logged in.
Changed in version 8.0.0: lazy load cookies when logging in. This was dropped in 8.0.4
Changed in version 8.0.4: the cookie_only parameter was added and cookies are loaded whenever the site is initialized.
See also
- Parameters:
autocreate (bool) – if true, allow auto-creation of the account using unified login
user (str | None) – bot user name. Overrides the username set by BaseSite initializer parameter or user config setting
cookie_only (bool) – Only try to login from cookie but do not force to login with username/password settings.
- Raises:
pywikibot.exceptions.NoUsernameError – Username is not recognised by the site.
- Return type:
None
- logout()[source]#
Logout of the site and load details for the logged out user.
Also logs out of the global account if linked to the user.
See also
- Raises:
APIError – Logout is not available when OAuth enabled.
- Return type:
None
- property logtypes: Set[str]#
Return a set of log types available on current site.
- property maxlimit: int#
Get the maximum limit of pages to be retrieved.
New in version 7.0.
- mediawiki_message(key, lang=None)[source]#
Fetch the text for a MediaWiki message.
- Parameters:
key (str) – name of MediaWiki message
lang (str | None) – a language code, default is self.lang
- Return type:
str
- mediawiki_messages(keys, lang=None)[source]#
Fetch the text of a set of MediaWiki messages.
The returned dict uses each key to store the associated message.
See also
- Parameters:
keys (Iterable[str]) – MediaWiki messages to fetch
lang (str | None) – a language code, default is self.lang
- Return type:
OrderedDict[str, str]
- merge_history(source, dest, timestamp=None, reason=None)[source]#
Merge revisions from one page into another.
See also
page.BasePage.merge_history()
(should be preferred)
Revisions dating up to the given timestamp in the source will be moved into the destination page history. History merge fails if the timestamps of source and dest revisions overlap (all source revisions must be dated before the earliest dest revision).
- Parameters:
source (pywikibot.page.BasePage) – Source page from which revisions will be merged
dest (pywikibot.page.BasePage) – Destination page to which revisions will be merged
timestamp (Timestamp | None) – Revisions from this page dating up to this timestamp will be merged into the destination page (if not given or False, all revisions will be merged)
reason (str | None) – Optional reason for the history merge
- Raises:
APIError – unexpected APIError
Error – expected APIError or unexpected response
NoPageError – source or dest does not exist
PageSaveRelatedError – source is equal to dest
- Return type:
None
- messages()[source]#
Return true if the user has new messages, and false otherwise.
Deprecated since version 8.0: Replaced by
userinfo['messages']
.- Return type:
bool
- property months_names: List[Tuple[str, str]]#
Obtain month names from the site messages.
The list is zero-indexed, ordered by month in calendar, and should be in the original site language.
- Returns:
list of tuples (month name, abbreviation)
- movepage(page, newtitle, summary, movetalk=True, noredirect=False, movesubpages=True)[source]#
Move a Page to a new title.
See also
Changed in version 7.2: The
movesubpages
parameter was added- Parameters:
page (pywikibot.page.BasePage) – the Page to be moved (must exist)
newtitle (str) – the new title for the Page
summary (str) – edit summary (required!)
movetalk (bool) – if True (default), also move the talk page if possible
noredirect (bool) – if True, suppress creation of a redirect from the old title to the new one
movesubpages (bool) – Rename subpages, if applicable.
- Returns:
Page object with the new title
- Return type:
pywikibot.page.Page
- property mw_version: MediaWikiVersion#
Return self.version() as a MediaWikiVersion object.
Cache the result for 24 hours.
- namespace(num, all=False)[source]#
Return string containing local name of namespace ‘num’.
If optional argument ‘all’ is true, return all recognized values for this namespace.
- Parameters:
num (int) – Namespace constant.
all (bool) – If True return a Namespace object. Otherwise return the namespace name.
- Returns:
local name or Namespace object
- Return type:
str | Namespace
- nice_get_address(title)[source]#
Return shorter URL path to retrieve page titled ‘title’.
- Parameters:
title (str) –
- Return type:
str
- page_can_be_edited(page, action='edit')[source]#
Determine if the page can be modified.
Return True if the bot has the permission of needed restriction level for the given action type.
See also
page.BasePage.has_permission()
(should be preferred)- Parameters:
page (BasePage) – a pywikibot.page.BasePage object
action (str) – a valid restriction type like ‘edit’, ‘move’
- Raises:
ValueError – invalid action parameter
- Return type:
bool
- page_from_repository(item)[source]#
Return a Page for this site object specified by Wikibase item.
Usage:
>>> site = pywikibot.Site('wikipedia:zh') >>> page = site.page_from_repository('Q131303') >>> page.title() 'Hello World'
This method is able to upcast categories:
>>> site = pywikibot.Site('commons') >>> page = site.page_from_repository('Q131303') >>> page.title() 'Category:Hello World' >>> page Category('Category:Hello World')
It also works for wikibase repositories:
>>> site = pywikibot.Site('wikidata') >>> page = site.page_from_repository('Q5296') >>> page.title() 'Wikidata:Main Page'
If no page exists for a given site, None is returned:
>>> site = pywikibot.Site('wikidata') >>> page = site.page_from_repository('Q131303') >>> page is None True
Changed in version 7.7: No longer raise NotimplementedError if used with a Wikibase site.
- Parameters:
item (str) – id number of item, “Q###”,
- Returns:
Page, or Category object given by Wikibase item number for this site object.
- Raises:
pywikibot.exceptions.UnknownExtensionError – site has no Wikibase extension
- Return type:
Page | None
- page_isredirect(page)[source]#
Return True if and only if page is a redirect.
- Parameters:
page (BasePage) –
- Return type:
bool
- page_restrictions(page)[source]#
Return a dictionary reflecting page protections.
Example:
>>> site = pywikibot.Site('wikipedia:test') >>> page = pywikibot.Page(site, 'Main Page') >>> site.page_restrictions(page) {'edit': ('sysop', 'infinity'), 'move': ('sysop', 'infinity')}
See also
page.BasePage.protection()
(should be preferred)- Parameters:
page (BasePage) –
- Return type:
Dict[str, Tuple[str, str]]
- pagename2codes()[source]#
Return list of localized PAGENAMEE tags for the site.
- Return type:
List[str]
- pagenamecodes()[source]#
Return list of localized PAGENAME tags for the site.
- Return type:
List[str]
- protect(page, protections, reason, expiry=None, **kwargs)[source]#
(Un)protect a wiki page. Requires protect right.
- Parameters:
protections (Dict[str, str | None]) – A dict mapping type of protection to protection level of that type. Refer
protection_types()
for valid restriction types andprotection_levels()
for valid restriction levels. If None is given, however, that protection will be skipped.reason (str) – Reason for the action
expiry (datetime | str | None) – When the block should expire. This expiry will be applied to all protections. If
None
,'infinite'
,'indefinite'
,'never'
, or''
is given, there is no expiry.page (pywikibot.page.BasePage) –
kwargs (Any) –
- Return type:
None
- protection_levels()[source]#
Return the protection levels available on this site.
Example:
>>> site = pywikibot.Site('wikipedia:test') >>> sorted(site.protection_levels()) ['', 'autoconfirmed', ... 'sysop', 'templateeditor']
See also
Siteinfo._get_default()
- Returns:
protection types available
- Return type:
Set[str]
- protection_types()[source]#
Return the protection types available on this site.
Example:
>>> site = pywikibot.Site('wikipedia:test') >>> sorted(site.protection_types()) ['create', 'edit', 'move', 'upload']
See also
Siteinfo._get_default()
- Returns:
protection types available
- Return type:
Set[str]
- purgepages(pages, forcelinkupdate=False, forcerecursivelinkupdate=False, converttitles=False, redirects=False)[source]#
Purge the server’s cache for one or multiple pages.
- Parameters:
pages (List[pywikibot.page.BasePage]) – list of Page objects
redirects (bool) – Automatically resolve redirects.
converttitles (bool) – Convert titles to other variants if necessary. Only works if the wiki’s content language supports variant conversion.
forcelinkupdate (bool) – Update the links tables.
forcerecursivelinkupdate (bool) – Update the links table, and update the links tables for any page that uses this page as a template.
- Returns:
True if API returned expected response; False otherwise
- Return type:
bool
- redirectRegex()[source]#
Deprecated; use redirect_regex instead.
Return a compiled regular expression matching on redirect pages.
- Return type:
Pattern[str]
- property redirect_regex: Pattern[str]#
Return a compiled regular expression matching on redirect pages.
Group 1 in the regex match object will be the target title.
- rollbackpage(page, **kwargs)[source]#
Roll back page to version before last user’s edits.
See also
The keyword arguments are those supported by the rollback API.
As a precaution against errors, this method will fail unless the page history contains at least two revisions, and at least one that is not by the same user who made the last edit.
- Parameters:
page (pywikibot.page.BasePage) – the Page to be rolled back (must exist)
kwargs (Any) –
- Keyword Arguments:
user – the last user to be rollbacked; default is page.latest_revision.user
- Return type:
None
- server_time()[source]#
Return a Timestamp object representing the current server time.
It uses the ‘time’ property of the siteinfo ‘general’. It’ll force a reload before returning the time.
- Returns:
the current server time
- Return type:
- simple_request(**kwargs)[source]#
Create a request by defining all kwargs as parameters.
New in version 7.1:
_simple_request
becomes a public method- Parameters:
kwargs (Any) –
- Return type:
- stash_info(file_key, props=None)[source]#
Get the stash info for a given file key.
See also
- Parameters:
file_key (str) –
props (List[str] | None) –
- Return type:
Dict[str, Any]
- property tokens: TokenWallet#
Return the TokenWallet collection.
TokenWallet
collection holds all available tokens. The tokens are loaded viaget_tokens()
method with the first token request and is retained until the TokenWallet is cleared.Usage:
>>> site = pywikibot.Site() >>> token = site.tokens['csrf'] >>> token 'df8...9e6+\\' >>> 'csrf' in site.tokens ... # Check whether the token exists True >>> 'invalid' in site.tokens False >>> token = site.tokens['invalid'] Traceback (most recent call last): ... KeyError: "Invalid token 'invalid' for user ... >>> site.tokens.clear() # clears the internal cache >>> site.tokens['csrf'] ... # get a new token '1c8...9d3+\\' >>> del site.tokens # another variant to clear the cache
Changed in version 8.0:
tokens
attribute became a property to enable deleter.Warning
A deprecation warning is shown if the token name is outdated, see API:Tokens (action).
See also
API:Tokens for valid token types
- unblockuser(user, reason=None)[source]#
Remove the block for the user.
See also
- Parameters:
user (pywikibot.page.User) – The username/IP without a namespace.
reason (str | None) – Reason for the unblock.
- Return type:
Dict[str, Any]
- undelete(page, reason, *, revisions=None, fileids=None)[source]#
Undelete page from the wiki. Requires appropriate privilege level.
See also
New in version 6.1: renamed from
undelete_page
Changed in version 6.1:
fileids
parameter was added, keyword argument required forrevisions
.- Parameters:
page (pywikibot.page.BasePage) – Page to be deleted.
reason (str) – Undeletion reason.
revisions (List[str] | None) – List of timestamps to restore. If None, restores all revisions.
fileids (List[int | str] | None) – List of fileids to restore.
- Return type:
None
- upload(filepage, **kwargs)[source]#
Upload a file to the wiki.
See also
Either source_filename or source_url, but not both, must be provided.
Changed in version 6.0: keyword arguments required for all parameters except
filepage
Changed in version 6.2:: asynchronous upload is used if
asynchronous
parameter is set.For keyword arguments refer
pywikibot.site._upload.Uploader
- Parameters:
filepage (pywikibot.page.FilePage) – a FilePage object from which the wiki-name of the file will be obtained.
kwargs (Any) –
- Returns:
It returns True if the upload was successful and False otherwise.
- Return type:
bool
- property userinfo: Dict[str, Any]#
Retrieve userinfo from site and store in _userinfo attribute.
To force retrieving userinfo ignoring cache, just delete this property.
Usage
>>> site = pywikibot.Site('test') >>> info = site.userinfo >>> info['id'] # returns 0 if no ip user ... 0 >>> info['name'] # username or ip ... ... '92.198.174.192' >>> info['groups'] ['*'] >>> info['rights'] ['createaccount', 'read', 'edit', 'createpage', 'createtalk', ...] >>> info['messages'] False >>> del site.userinfo # delete userinfo cache >>> 'blockinfo' in site.userinfo False >>> 'anon' in site.userinfo True
Usefull alternatives to userinfo property
has_group()
to verify the group membershiphas_right()
to verify that the user has a given rightlogged_in()
to verify the user is loggend in to a site
See also
Changed in version 8.0: Use API formatversion 2.
- Returns:
A dict with the following keys and values:
id: user id (int)
name: username (if user is logged in)
anon: present if user is not logged in
groups: list of groups (could be empty)
rights: list of rights (could be empty)
messages: True if user has a new message on talk page (bool)
blockinfo: present if user is blocked (dict)
- validate_tokens(types)[source]#
Validate if requested tokens are acceptable.
Valid tokens may depend on mw version.
Deprecated since version 8.0.
- Parameters:
types (List[str]) –
- Return type:
List[str]
- version()[source]#
Return live project version number as a string.
Use
pywikibot.site.mw_version
to compare MediaWiki versions.- Return type:
str
- watch(pages, unwatch=False)[source]#
Add or remove pages from watchlist.
See also
- Parameters:
pages (pywikibot.page.BasePage | str | List[pywikibot.page.BasePage | str]) – A single page or a sequence of pages.
unwatch (bool) – If True, remove pages from watchlist; if False add them (default).
- Returns:
True if API returned expected response; False otherwise
- Raises:
KeyError – ‘watch’ isn’t in API response
- Return type:
bool
Objects representing API interface to MediaWiki site extenstions.
- class pywikibot.site._extensions.EchoMixin[source]#
Bases:
object
APISite mixin for Echo extension.
- notifications(**kwargs)[source]#
Yield Notification objects from the Echo extension.
- Keyword Arguments:
format (Optional[str]) – If specified, notifications will be returned formatted this way. Its value is either
model
,special
orNone
. Default isspecial
.
See also
API:Notifications for other keywords.
- class pywikibot.site._extensions.FlowMixin[source]#
Bases:
object
APISite mixin for Flow extension.
- create_new_topic(page, title, content, content_format)[source]#
Create a new topic on a Flow board.
- Parameters:
page (Board) – A Flow board
title (str) – The title of the new topic (must be in plaintext)
content (str) – The content of the topic’s initial post
content_format (str (either 'wikitext' or 'html')) – The content format of the supplied content
- Returns:
The metadata of the new topic
- Return type:
dict
- delete_post(post, reason)[source]#
Delete a Flow post.
- Parameters:
post (Post) – A Flow post
reason (str) – The reason to delete the post
- Returns:
Metadata returned by the API
- Return type:
dict
- delete_topic(page, reason)[source]#
Delete a Flow topic.
- Parameters:
page (Topic) – A Flow topic
reason (str) – The reason to delete the topic
- Returns:
Metadata returned by the API
- Return type:
dict
- hide_post(post, reason)[source]#
Hide a Flow post.
- Parameters:
post (Post) – A Flow post
reason (str) – The reason to hide the post
- Returns:
Metadata returned by the API
- Return type:
dict
- hide_topic(page, reason)[source]#
Hide a Flow topic.
- Parameters:
page (Topic) – A Flow topic
reason (str) – The reason to hide the topic
- Returns:
Metadata returned by the API
- Return type:
dict
- load_board(page)[source]#
Retrieve the data for a Flow board.
- Parameters:
page (Board) – A Flow board
- Returns:
A dict representing the board’s metadata.
- Return type:
dict
- load_post_current_revision(page, post_id, content_format)[source]#
Retrieve the data for a post to a Flow topic.
- Parameters:
page (Topic) – A Flow topic
post_id (str) – The UUID of the Post
content_format (str) – The content format used for the returned content; must be either ‘wikitext’, ‘html’, or ‘fixed-html’
- Returns:
A dict representing the post data for the given UUID.
- Return type:
dict
- load_topic(page, content_format)[source]#
Retrieve the data for a Flow topic.
- Parameters:
page (Topic) – A Flow topic
content_format (str) – The content format to request the data in. Must ne either ‘wikitext’, ‘html’, or ‘fixed-html’
- Returns:
A dict representing the topic’s data.
- Return type:
dict
- load_topiclist(page, *, content_format='wikitext', limit=100, sortby='newest', toconly=False, offset=None, offset_id=None, reverse=False, include_offset=False)[source]#
Retrieve the topiclist of a Flow board.
Changed in version 8.0: All parameters except page are keyword only parameters.
- Parameters:
page (pywikibot.flow.Board) – A Flow board
content_format (str) – The content format to request the data in. must be either ‘wikitext’, ‘html’, or ‘fixed-html’
limit (int) – The number of topics to fetch in each single request.
sortby (str) – Algorithm to sort topics by (‘newest’ or ‘updated’).
toconly (bool) – Whether to only include information for the TOC.
offset (Timestamp | str | None) – The timestamp to start at (when sortby is ‘updated’).
offset_id (str | None) – The topic UUID to start at (when sortby is ‘newest’).
reverse (bool) – Whether to reverse the topic ordering.
include_offset (bool) – Whether to include the offset topic.
- Returns:
A dict representing the board’s topiclist.
- Return type:
Dict[str, Any]
- lock_topic(page, lock, reason)[source]#
Lock or unlock a Flow topic.
- Parameters:
page (Topic) – A Flow topic
lock (bool (True corresponds to locking the topic.)) – Whether to lock or unlock the topic
reason (str) – The reason to lock or unlock the topic
- Returns:
Metadata returned by the API
- Return type:
dict
- moderate_post(post, state, reason)[source]#
Moderate a Flow post.
- Parameters:
post (Post) – A Flow post
state (str) – The new moderation state
reason (str) – The reason to moderate the topic
- Returns:
Metadata returned by the API
- Return type:
dict
- moderate_topic(page, state, reason)[source]#
Moderate a Flow topic.
- Parameters:
page (Topic) – A Flow topic
state (str) – The new moderation state
reason (str) – The reason to moderate the topic
- Returns:
Metadata returned by the API
- Return type:
dict
- reply_to_post(page, reply_to_uuid, content, content_format)[source]#
Reply to a post on a Flow topic.
- Parameters:
page (Topic) – A Flow topic
reply_to_uuid (str) – The UUID of the Post to create a reply to
content (str) – The content of the reply
content_format (str) – The content format used for the supplied content; must be either ‘wikitext’ or ‘html’
- Returns:
Metadata returned by the API
- Return type:
dict
- restore_post(post, reason)[source]#
Restore a Flow post.
- Parameters:
post (Post) – A Flow post
reason (str) – The reason to restore the post
- Returns:
Metadata returned by the API
- Return type:
dict
- restore_topic(page, reason)[source]#
Restore a Flow topic.
- Parameters:
page (Topic) – A Flow topic
reason (str) – The reason to restore the topic
- Returns:
Metadata returned by the API
- Return type:
dict
- class pywikibot.site._extensions.GeoDataMixin[source]#
Bases:
object
APISite mixin for GeoData extension.
- class pywikibot.site._extensions.GlobalUsageMixin[source]#
Bases:
object
APISite mixin for Global Usage extension.
- globalusage(page, total=None)[source]#
Iterate global image usage for a given FilePage.
- Parameters:
page (FilePage) – the page to return global image usage for.
total – iterate no more than this number of pages in total.
- Raises:
TypeError – input page is not a FilePage.
pywikibot.exceptions.SiteDefinitionError – Site could not be defined for a returned entry in API response.
- class pywikibot.site._extensions.LinterMixin[source]#
Bases:
object
APISite mixin for Linter extension.
- linter_pages(lint_categories=None, total=None, namespaces=None, pageids=None, lint_from=None)[source]#
Return a generator to pages containing linter errors.
- Parameters:
lint_categories (an iterable that returns values (str), or a pipe-separated string of values.) – categories of lint errors
total (int) – if not None, yielding this many items in total
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – only iterate pages in these namespaces
pageids (an iterable that returns pageids (str or int), or a comma- or pipe-separated string of pageids (e.g. '945097,1483753, 956608' or '945097|483753|956608')) – only include lint errors from the specified pageids
lint_from (str representing digit or integer) – Lint ID to start querying from
- Returns:
pages with Linter errors.
- Return type:
Iterable[Page]
- class pywikibot.site._extensions.PageImagesMixin[source]#
Bases:
object
APISite mixin for PageImages extension.
- class pywikibot.site._extensions.ProofreadPageMixin[source]#
Bases:
object
APISite mixin for ProofreadPage extension.
- property proofread_index_ns#
Return Index namespace for the ProofreadPage extension.
- property proofread_levels#
Return Quality Levels for the ProofreadPage extension.
- property proofread_page_ns#
Return Page namespace for the ProofreadPage extension.
- class pywikibot.site._extensions.TextExtractsMixin[source]#
Bases:
object
APISite mixin for TextExtracts extension.
New in version 7.1.
- extract(page, *, chars=None, sentences=None, intro=True, plaintext=True)[source]#
Retrieve an extract of a page.
- Parameters:
page (Page) – The Page object for which the extract is read
chars (int | None) – How many characters to return. Actual text returned might be slightly longer.
sentences (int | None) – How many sentences to return
intro (bool) – Return only content before the first section
plaintext (bool) – if True, return extracts as plain text instead of limited HTML
- Return type:
str
- class pywikibot.site._extensions.ThanksFlowMixin[source]#
Bases:
object
APISite mixin for Thanks and Flow extension.
- class pywikibot.site._extensions.ThanksMixin[source]#
Bases:
object
APISite mixin for Thanks extension.
- thank_revision(revid, source=None)[source]#
Corresponding method to the ‘action=thank’ API action.
- Parameters:
revid (int) – Revision ID for the revision to be thanked.
source (str) – A source for the thanking operation.
- Raises:
APIError – On thanking oneself or other API errors.
- Returns:
The API response.
- class pywikibot.site._extensions.UrlShortenerMixin[source]#
Bases:
object
APISite mixin for UrlShortener extension.
- create_short_link(url)[source]#
Return a shortened link.
Note that on Wikimedia wikis only metawiki supports this action, and this wiki can process links to all WM domains.
- Parameters:
url (str) – The link to reduce, with propotol prefix.
- Returns:
The reduced link, without protocol prefix.
- Return type:
str
- class pywikibot.site._extensions.WikibaseClientMixin[source]#
Bases:
object
APISite mixin for WikibaseClient extension.
Objects representing API generators to MediaWiki site.
- class pywikibot.site._generators.GeneratorsMixin[source]#
Bases:
object
API generators mixin to MediaWiki site.
- allcategories(start='!', prefix='', total=None, reverse=False, content=False)[source]#
Iterate categories used (which need not have a Category page).
Iterator yields Category objects. Note that, in practice, links that were found on pages that have been deleted may not have been removed from the database table, so this method can return false positives.
See also
- Parameters:
start (str) – Start at this category title (category need not exist).
prefix (str) – Only yield categories starting with this string.
reverse (bool) – if True, iterate in reverse Unicode lexigraphic order (default: iterate in forward order)
content (bool) – if True, load the current content of each iterated page (default False); note that this means the contents of the category description page, not the pages that are members of the category
- alldeletedrevisions(*, namespaces=None, reverse=False, content=False, total=None, **kwargs)[source]#
Iterate all deleted revisions.
See also
- Parameters:
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – Only iterate pages in these namespaces
reverse (bool) – Iterate oldest revisions first (default: newest)
content (bool) – If True, retrieve the content of each revision
total (int | None) – Number of revisions to retrieve
- Keyword Arguments:
from – Start listing at this title
to – Stop listing at this title
prefix – Search for all page titles that begin with this value
excludeuser – Exclude revisions by this user
tag – Only list revisions tagged with this tag
user – List revisions by this user
start – Iterate revisions starting at this Timestamp
end – Iterate revisions ending at this Timestamp
prop – Which properties to get. Defaults are ids, timestamp, flags, user, and comment (if you have the right to view).
- Return type:
Iterable[Dict[str, Any]]
- allimages(start='!', prefix='', minsize=None, maxsize=None, reverse=False, sha1=None, sha1base36=None, total=None, content=False)[source]#
Iterate all images, ordered by image title.
Yields FilePages, but these pages need not exist on the wiki.
See also
- Parameters:
start (str) – start at this title (name need not exist)
prefix (str) – only iterate titles starting with this substring
minsize – only iterate images of at least this many bytes
maxsize – only iterate images of no more than this many bytes
reverse (bool) – if True, iterate in reverse lexigraphic order
sha1 – only iterate image (it is theoretically possible there could be more than one) with this sha1 hash
sha1base36 – same as sha1 but in base 36
content (bool) – if True, load the current content of each iterated page (default False); note that this means the content of the image description page, not the image itself
- alllinks(start='!', prefix='', namespace=0, unique=False, fromids=False, total=None)[source]#
Iterate all links to pages (which need not exist) in one namespace.
Note that, in practice, links that were found on pages that have been deleted may not have been removed from the links table, so this method can return false positives.
See also
- Parameters:
start (str) – Start at this title (page need not exist).
prefix (str) – Only yield pages starting with this string.
namespace (int or Namespace) – Iterate pages from this (single) namespace
unique (bool) – If True, only iterate each link title once (default: iterate once for each linking page)
fromids (bool) – if True, include the pageid of the page containing each link (default: False) as the ‘_fromid’ attribute of the Page; cannot be combined with unique
- Raises:
KeyError – the namespace identifier was not resolved
TypeError – the namespace identifier has an inappropriate type such as bool, or an iterable with more than one namespace
- allpages(start='!', prefix='', namespace=0, filterredir=None, filterlanglinks=None, minsize=None, maxsize=None, protect_type=None, protect_level=None, reverse=False, total=None, content=False)[source]#
Iterate pages in a single namespace.
See also
- Parameters:
start (str) – Start at this title (page need not exist).
prefix (str) – Only yield pages starting with this string.
namespace (int or Namespace.) – Iterate pages from this (single) namespace
filterredir – if True, only yield redirects; if False (and not None), only yield non-redirects (default: yield both)
filterlanglinks – if True, only yield pages with language links; if False (and not None), only yield pages without language links (default: yield both)
minsize – if present, only yield pages at least this many bytes in size
maxsize – if present, only yield pages at most this many bytes in size
protect_type (str) – only yield pages that have a protection of the specified type
protect_level – only yield pages that have protection at this level; can only be used if protect_type is specified
reverse (bool) – if True, iterate in reverse Unicode lexigraphic order (default: iterate in forward order)
content (bool) – if True, load the current content of each iterated page (default False)
- Raises:
KeyError – the namespace identifier was not resolved
TypeError – the namespace identifier has an inappropriate type such as bool, or an iterable with more than one namespace
- allusers(start='!', prefix='', group=None, total=None)[source]#
Iterate registered users, ordered by username.
Iterated values are dicts containing ‘name’, ‘editcount’, ‘registration’, and (sometimes) ‘groups’ keys. ‘groups’ will be present only if the user is a member of at least 1 group, and will be a list of str; all the other values are str and should always be present.
See also
- Parameters:
start (str) – start at this username (name need not exist)
prefix (str) – only iterate usernames starting with this substring
group (str) – only iterate users that are members of this group
- ancientpages(total=None)[source]#
Yield Pages, datestamps from Special:Ancientpages.
- Parameters:
total – number of pages to return
- blocks(starttime=None, endtime=None, reverse=False, blockids=None, users=None, iprange=None, total=None)[source]#
Iterate all current blocks, in order of creation.
The iterator yields dicts containing keys corresponding to the block properties.
See also
Note
logevents only logs user blocks, while this method iterates all blocks including IP ranges.
Warning
iprange
parameter cannot be used together withusers
.- Parameters:
starttime (Timestamp) – start iterating at this Timestamp
endtime (Timestamp) – stop iterating at this Timestamp
reverse (bool) – if True, iterate oldest blocks first (default: newest)
blockids (str, tuple or list) – only iterate blocks with these id numbers. Numbers must be separated by ‘|’ if given by a str.
users (str, tuple or list) – only iterate blocks affecting these usernames or IPs
iprange (str | None) – a single IP or an IP range. Ranges broader than IPv4/16 or IPv6/19 are not accepted.
total (int | None) – total amount of block entries
- botusers(total=None)[source]#
Iterate bot users.
Iterated values are dicts containing ‘name’, ‘userid’, ‘editcount’, ‘registration’, and ‘groups’ keys. ‘groups’ will be present only if the user is a member of at least 1 group, and will be a list of str; all the other values are str and should always be present.
- broken_redirects(total=None)[source]#
Yield Pages with broken redirects from Special:BrokenRedirects.
- Parameters:
total – number of pages to return
- categorymembers(category, *, namespaces=None, sortby=None, reverse=False, starttime=None, endtime=None, total=None, startprefix=None, endprefix=None, content=False, member_type=None)[source]#
Iterate members of specified category.
You should not use this method directly; instead use one of the following:
Changed in version 4.0.0: parameters except category are keyword arguments only.
Changed in version 8.0.0: raises TypeError instead of Error if no Category is specified
See also
- Parameters:
category (Category) – The Category to iterate.
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – If present, only return category members from these namespaces. To yield subcategories or files, use parameter member_type instead.
sortby (str | None) – determines the order in which results are generated, valid values are “sortkey” (default, results ordered by category sort key) or “timestamp” (results ordered by time page was added to the category)
reverse (bool) – if True, generate results in reverse order (default False)
starttime (Timestamp | None) – if provided, only generate pages added after this time; not valid unless sortby=”timestamp”
endtime (Timestamp | None) – if provided, only generate pages added before this time; not valid unless sortby=”timestamp”
startprefix (str | None) – if provided, only generate pages >= this title lexically; not valid if sortby=”timestamp”
endprefix (str | None) – if provided, only generate pages < this title lexically; not valid if sortby=”timestamp”
content (bool) – if True, load the current content of each iterated page (default False)
member_type (str | Iterable[str] | None) – member type; values must be
page
,subcat
,file
. If member_type includespage
and is used in conjunction with sortby=”timestamp”, the API may limit results to only pages in the first 50 namespaces.total (int | None) –
- Raises:
KeyError – a namespace identifier was not resolved
TypeError – a namespace identifier has an inappropriate type such as NoneType or bool
TypeError – no Category is specified
ValueError – invalid values given
- Return type:
Iterable[Page]
- deadendpages(total=None)[source]#
Yield Page objects retrieved from Special:Deadendpages.
- Parameters:
total – number of pages to return
- deletedrevs(titles=None, start=None, end=None, reverse=False, content=False, total=None, **kwargs)[source]#
Iterate deleted revisions.
Each value returned by the iterator will be a dict containing the ‘title’ and ‘ns’ keys for a particular Page and a ‘revisions’ key whose value is a list of revisions in the same format as recentchanges plus a ‘content’ element with key ‘*’ if requested when ‘content’ parameter is set. For older wikis a ‘token’ key is also given with the content request.
See also
- Parameters:
- Keyword Arguments:
revids – Get revisions by their ID
Note
either titles or revids must be set but not both
- Parameters:
start – Iterate revisions starting at this Timestamp
end – Iterate revisions ending at this Timestamp
reverse (bool) – Iterate oldest revisions first (default: newest)
content (bool) – If True, retrieve the content of each revision
total – number of revisions to retrieve
- Keyword Arguments:
user – List revisions by this user
excludeuser – Exclude revisions by this user
tag – Only list revision tagged with this tag
prop – Which properties to get. Defaults are ids, user, comment, flags and timestamp
- double_redirects(total=None)[source]#
Yield Pages with double redirects from Special:DoubleRedirects.
- Parameters:
total – number of pages to return
- exturlusage(url=None, protocol=None, namespaces=None, total=None, content=False)[source]#
Iterate Pages that contain links to the given URL.
See also
- Parameters:
url (str | None) – The URL to search for (with or without the protocol prefix); this may include a ‘*’ as a wildcard, only at the start of the hostname
namespaces (list of int) – list of namespace numbers to fetch contribs from
total (int | None) – Maximum number of pages to retrieve in total
protocol (str | None) – Protocol to search for, likely http or https, http by default. Full list shown on Special:LinkSearch wikipage
content (bool) –
- filearchive(start=None, end=None, reverse=False, total=None, **kwargs)[source]#
Iterate archived files.
Yields dict of file archive informations.
See also
- Parameters:
start – start at this title (name need not exist)
end – end at this title (name need not exist)
reverse (bool) – if True, iterate in reverse lexigraphic order
total – maximum number of pages to retrieve in total
- Keyword Arguments:
prefix – only iterate titles starting with this substring
sha1 – only iterate image with this sha1 hash
sha1base36 – same as sha1 but in base 36
prop – Image information to get. Default is timestamp
- imageusage(image, *, namespaces=None, filterredir=None, total=None, content=False)[source]#
Iterate Pages that contain links to the given FilePage.
See also
Changed in version 7.2: all parameters except
image
are keyword only.- Parameters:
image (FilePage) – the image to search for (FilePage need not exist on the wiki)
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – If present, only iterate pages in these namespaces
filterredir (bool | None) – if True, only yield redirects; if False (and not None), only yield non-redirects (default: yield both)
total (int | None) – iterate no more than this number of pages in total
content (bool) – if True, load the current content of each iterated page (default False)
- Raises:
KeyError – a namespace identifier was not resolved
TypeError – a namespace identifier has an inappropriate type such as NoneType or bool
- load_pages_from_pageids(pageids)[source]#
Return a page generator from pageids.
Pages are iterated in the same order than in the underlying pageids.
Pageids are filtered and only one page is returned in case of duplicate pageids.
- Parameters:
pageids – an iterable that returns pageids (str or int), or a comma- or pipe-separated string of pageids (e.g. ‘945097,1483753, 956608’ or ‘945097|483753|956608’)
- loadrevisions(page, *, content=False, section=None, **kwargs)[source]#
Retrieve revision information and store it in page object.
By default, retrieves the last (current) revision of the page, unless any of the optional parameters revids, startid, endid, starttime, endtime, rvdir, user, excludeuser, or total are specified. Unless noted below, all parameters not specified default to False.
If rvdir is False or not specified, startid must be greater than endid if both are specified; likewise, starttime must be greater than endtime. If rvdir is True, these relationships are reversed.
See also
- Parameters:
page (Page) – retrieve revisions of this Page and hold the data.
content (bool) – if True, retrieve the wiki-text of each revision; otherwise, only retrieve the revision metadata (default)
section (int) – if specified, retrieve only this section of the text (content must be True); section must be given by number (top of the article is section 0), not name
- Keyword Arguments:
revids – retrieve only the specified revision ids (raise Exception if any of revids does not correspond to page)
startid – retrieve revisions starting with this revid
endid – stop upon retrieving this revid
starttime – retrieve revisions starting at this Timestamp
endtime – stop upon reaching this Timestamp
rvdir – if false, retrieve newest revisions first (default); if true, retrieve oldest first
user – retrieve only revisions authored by this user
excludeuser – retrieve all revisions not authored by this user
total – number of revisions to retrieve
- Raises:
ValueError – invalid startid/endid or starttime/endtime values
pywikibot.exceptions.Error – revids belonging to a different page
- logevents(logtype=None, user=None, page=None, namespace=None, start=None, end=None, reverse=False, tag=None, total=None)[source]#
Iterate all log entries.
See also
Note
logevents with
logtype='block'
only logs user blocks whereassite.blocks
iterates all blocks including IP ranges.- Parameters:
logtype (str | None) – only iterate entries of this type (see mediawiki api documentation for available types)
user (str | None) – only iterate entries that match this user name
page (Page or str) – only iterate entries affecting this page
namespace (int or Namespace or an iterable of them) – namespace(s) to retrieve logevents from
reverse (bool) –
tag (str | None) –
total (int | None) –
Note
due to an API limitation, if namespace param contains multiple namespaces, log entries from all namespaces will be fetched from the API and will be filtered later during iteration.
- Parameters:
start (Timestamp or ISO date string) – only iterate entries from and after this Timestamp
end (Timestamp or ISO date string) – only iterate entries up to and through this Timestamp
reverse (bool) – if True, iterate oldest entries first (default: newest)
tag (str | None) – only iterate entries tagged with this tag
total (int | None) – maximum number of events to iterate
logtype (str | None) –
user (str | None) –
- Return type:
iterable
- Raises:
KeyError – the namespace identifier was not resolved
TypeError – the namespace identifier has an inappropriate type such as bool, or an iterable with more than one namespace
- lonelypages(total=None)[source]#
Yield Pages retrieved from Special:Lonelypages.
- Parameters:
total – number of pages to return
- longpages(total=None)[source]#
Yield Pages and lengths from Special:Longpages.
Yields a tuple of Page object, length(int).
- Parameters:
total – number of pages to return
- newpages(user=None, returndict=False, start=None, end=None, reverse=False, bot=False, redirect=False, excludeuser=None, patrolled=None, namespaces=None, total=None)[source]#
Yield new articles (as Page objects) from recent changes.
Starts with the newest article and fetches the number of articles specified in the first argument.
The objects yielded are dependent on parameter returndict. When true, it yields a tuple composed of a Page object and a dict of attributes. When false, it yields a tuple composed of the Page object, timestamp (str), length (int), an empty string, username or IP address (str), comment (str).
- Parameters:
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – only iterate pages in these namespaces
returndict (bool) –
reverse (bool) –
bot (bool) –
redirect (bool) –
- Raises:
KeyError – a namespace identifier was not resolved
TypeError – a namespace identifier has an inappropriate type such as NoneType or bool
- page_embeddedin(page, *, filter_redirects=None, namespaces=None, total=None, content=False)[source]#
Iterate all pages that embedded the given page as a template.
See also
- Parameters:
page – The Page to get inclusions for.
filter_redirects – If True, only return redirects that embed the given page. If False, only return non-redirect links. If None, return both (no filtering).
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – If present, only return links from the namespaces in this list.
content (bool) – if True, load the current content of each iterated page (default False)
- Return type:
Iterable[Page]
- Raises:
KeyError – a namespace identifier was not resolved
TypeError – a namespace identifier has an inappropriate type such as NoneType or bool
- page_extlinks(page, *, total=None)[source]#
Iterate all external links on page, yielding URL strings.
See also
- page_redirects(page, *, filter_fragments=None, namespaces=None, total=None, content=False)[source]#
Iterale all redirects to the given page.
See also
New in version 7.0.
- Parameters:
page (Page) – The Page to get redirects for.
filter_fragments (bool | None) – If True, only return redirects with fragments. If False, only return redirects without fragments. If None, return both (no filtering).
namespaces (int | str | Namespace | Iterable[int | str | Namespace] | None) – Only return redirects from the namespaces
total (int | None) – maximum number of redirects to retrieve in total
content (bool) – load the current content of each redirect
- Return type:
Iterable[Page]
- pagebacklinks(page, *, follow_redirects=False, filter_redirects=None, namespaces=None, total=None, content=False)[source]#
Iterate all pages that link to the given page.
See also
- Parameters:
page – The Page to get links to.
follow_redirects (bool) – Also return links to redirects pointing to the given page.
filter_redirects – If True, only return redirects to the given page. If False, only return non-redirect links. If None, return both (no filtering).
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – If present, only return links from the namespaces in this list.
total – Maximum number of pages to retrieve in total.
content (bool) – if True, load the current content of each iterated page (default False)
- Return type:
Iterable[Page]
- Raises:
KeyError – a namespace identifier was not resolved
TypeError – a namespace identifier has an inappropriate type such as NoneType or bool
- pagecategories(page, *, total=None, content=False)[source]#
Iterate categories to which page belongs.
See also
- Parameters:
content (bool) – if True, load the current content of each iterated page (default False); note that this means the contents of the category description page, not the pages contained in the category
- pageimages(page, *, total=None, content=False)[source]#
Iterate images used (not just linked) on the page.
See also
- Parameters:
content (bool) – if True, load the current content of each iterated page (default False); note that this means the content of the image description page, not the image itself
- pagelanglinks(page, *, total=None, include_obsolete=False, include_empty_titles=False)[source]#
Iterate all interlanguage links on page, yielding Link objects.
Changed in version 6.2::
include_empty_titles
parameter was added.See also
- Parameters:
include_obsolete (bool) – if true, yield even Link objects whose site is obsolete
include_empty_titles (bool) – if true, yield even Link objects whose title is empty but redirects to a site like [[en:]]
total (int | None) –
- pagelinks(page, *, namespaces=None, follow_redirects=False, total=None, content=False)[source]#
Iterate internal wikilinks contained (or transcluded) on page.
See also
- Parameters:
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – Only iterate pages in these namespaces (default: all)
follow_redirects (bool) – if True, yields the target of any redirects, rather than the redirect page
total (int | None) – iterate no more than this number of pages in total
content (bool) – if True, load the current content of each iterated page
- Raises:
KeyError – a namespace identifier was not resolved
TypeError – a namespace identifier has an inappropriate type such as NoneType or bool
- Return type:
Generator[Page, None, None]
- pagereferences(page, *, follow_redirects=False, filter_redirects=None, with_template_inclusion=True, only_template_inclusion=False, namespaces=None, total=None, content=False)[source]#
Convenience method combining pagebacklinks and page_embeddedin.
- Parameters:
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – If present, only return links from the namespaces in this list.
follow_redirects (bool) –
with_template_inclusion (bool) –
only_template_inclusion (bool) –
content (bool) –
- Return type:
Iterable[Page]
- Raises:
KeyError – a namespace identifier was not resolved
TypeError – a namespace identifier has an inappropriate type such as NoneType or bool
- pages_with_property(propname, *, total=None)[source]#
Yield Page objects from Special:PagesWithProp.
See also
- Parameters:
propname (str) – must be a valid property.
total (int | None) – number of pages to return
- Returns:
return a generator of Page objects
- Return type:
iterator
- pagetemplates(page, *, namespaces=None, total=None, content=False)[source]#
Iterate templates transcluded (not just linked) on the page.
See also
- Parameters:
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – Only iterate pages in these namespaces
content (bool) – if True, load the current content of each iterated page (default False)
- Raises:
KeyError – a namespace identifier was not resolved
TypeError – a namespace identifier has an inappropriate type such as NoneType or bool
- patrol(rcid=None, revid=None, revision=None)[source]#
Return a generator of patrolled pages.
See also
Pages to be patrolled are identified by rcid, revid or revision. At least one of the parameters is mandatory. See https://www.mediawiki.org/wiki/API:Patrol.
- Parameters:
rcid (iterable/iterator which returns a number or string which contains only digits; it also supports a string (as above) or int) – an int/string/iterable/iterator providing rcid of pages to be patrolled.
revid (iterable/iterator which returns a number or string which contains only digits; it also supports a string (as above) or int.) – an int/string/iterable/iterator providing revid of pages to be patrolled.
revision (iterable/iterator which returns a Revision object; it also supports a single Revision.) – an Revision/iterable/iterator providing Revision object of pages to be patrolled.
- Return type:
iterator of dict with ‘rcid’, ’ns’ and ‘title’ of the patrolled page.
- preloadpages(pagelist, *, groupsize=None, templates=False, langlinks=False, pageprops=False, categories=False, content=True, quiet=True)[source]#
Return a generator to a list of preloaded pages.
Pages are iterated in the same order than in the underlying pagelist. In case of duplicates in a groupsize batch, return the first entry.
Changed in version 7.6: content parameter was added.
Changed in version 7.7: categories parameter was added.
Changed in version 8.1: groupsize is maxlimit by default. quiet parameter was added. No longer show the “Retrieving pages from site” message by default.
- Parameters:
pagelist – an iterable that returns Page objects
groupsize (int | None) – how many Pages to query at a time. If None (default),
maxlimit
is used.templates (bool) – preload pages (typically templates) transcluded in the provided pages
langlinks (bool) – preload all language links from the provided pages to other languages
pageprops (bool) – preload various properties defined in page content
categories (bool) – preload page categories
content (bool) – preload page content
quiet (bool) – If True (default), do not show the “Retrieving pages” message
- protectedpages(namespace=0, type='edit', level=False, total=None)[source]#
Return protected pages depending on protection level and type.
For protection types which aren’t ‘create’ it uses
APISite.allpages
, while it uses for ‘create’ the ‘query+protectedtitles’ module.See also
- Parameters:
namespace (int or Namespace or str) – The searched namespace.
type (str) – The protection type to search for (default ‘edit’).
level (str | bool) – The protection level (like ‘autoconfirmed’). If False it shows all protection levels.
- Returns:
The pages which are protected.
- Return type:
Iterable[Page]
- querypage(special_page, total=True)[source]#
Yield Page objects retrieved from Special:{special_page}.
See also
Generic function for all special pages supported by the site MW API.
- Parameters:
special_page – Special page to query
total – number of pages to return
- Raises:
AssertionError – special_page is not supported in SpecialPages.
- randompages(total=None, namespaces=None, redirects=False, content=False)[source]#
Iterate a number of random pages.
Pages are listed in a fixed sequence, only the starting point is random.
- Parameters:
total – the maximum number of pages to iterate
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – only iterate pages in these namespaces.
redirects (bool | None) – if True, include only redirect pages in results, False does not include redirects and None (MW 1.26+) include both types. (default: False)
content (bool) – if True, load the current content of each iterated page (default False)
- Raises:
KeyError – a namespace identifier was not resolved
TypeError – a namespace identifier has an inappropriate type such as NoneType or bool
AssertError – unsupported redirects parameter
- recentchanges(*, start=None, end=None, reverse=False, namespaces=None, changetype=None, minor=None, bot=None, anon=None, redirect=None, patrolled=None, top_only=False, total=None, user=None, excludeuser=None, tag=None)[source]#
Iterate recent changes.
See also
- Parameters:
start (Timestamp) – Timestamp to start listing from
end (Timestamp) – Timestamp to end listing at
reverse (bool) – if True, start with oldest changes (default: newest)
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – only iterate pages in these namespaces
changetype (str | None) – only iterate changes of this type (“edit” for edits to existing pages, “new” for new pages, “log” for log entries)
minor (bool | None) – if True, only list minor edits; if False, only list non-minor edits; if None, list all
bot (bool | None) – if True, only list bot edits; if False, only list non-bot edits; if None, list all
anon (bool | None) – if True, only list anon edits; if False, only list non-anon edits; if None, list all
redirect (bool | None) – if True, only list edits to redirect pages; if False, only list edits to non-redirect pages; if None, list all
patrolled (bool | None) – if True, only list patrolled edits; if False, only list non-patrolled edits; if None, list all
top_only (bool) – if True, only list changes that are the latest revision (default False)
user (str | List[str] | None) – if not None, only list edits by this user or users
excludeuser (str | List[str] | None) – if not None, exclude edits by this user or users
tag (str | None) – a recent changes tag
total (int | None) –
- Raises:
KeyError – a namespace identifier was not resolved
TypeError – a namespace identifier has an inappropriate type such as NoneType or bool
- redirectpages(total=None)[source]#
Yield redirect pages from Special:ListRedirects.
- Parameters:
total – number of pages to return
- search(searchstring, *, namespaces=None, where=None, total=None, content=False)[source]#
Iterate Pages that contain the searchstring.
Note that this may include non-existing Pages if the wiki’s database table contains outdated entries.
Changed in version 7.0: Default of
where
parameter has been changed from ‘text’ to None. The behaviour depends on the installed search engine which is ‘text’ on CirrusSearch’. raises APIError instead of Error if searchstring is not set or what parameter is wrong.See also
- Parameters:
searchstring (str) – the text to search for
where (str | None) – Where to search; value must be “text”, “title”, “nearmatch” or None (many wikis do not support all search types)
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – search only in these namespaces (defaults to all)
content (bool) – if True, load the current content of each iterated page (default False)
total (int | None) –
- Raises:
KeyError – a namespace identifier was not resolved
TypeError – a namespace identifier has an inappropriate type such as NoneType or bool
APIError – The “gsrsearch” parameter must be set: searchstring parameter is not set
APIError – Unrecognized value for parameter “gsrwhat”: wrong where parameter is given
- shortpages(total=None)[source]#
Yield Pages and lengths from Special:Shortpages.
Yields a tuple of Page object, length(int).
- Parameters:
total – number of pages to return
- uncategorizedcategories(total=None)[source]#
Yield Categories from Special:Uncategorizedcategories.
- Parameters:
total – number of pages to return
- uncategorizedfiles(total=None)#
Yield FilePages from Special:Uncategorizedimages.
- Parameters:
total – number of pages to return
- uncategorizedimages(total=None)[source]#
Yield FilePages from Special:Uncategorizedimages.
- Parameters:
total – number of pages to return
- uncategorizedpages(total=None)[source]#
Yield Pages from Special:Uncategorizedpages.
- Parameters:
total – number of pages to return
- uncategorizedtemplates(total=None)[source]#
Yield Pages from Special:Uncategorizedtemplates.
- Parameters:
total – number of pages to return
- unusedcategories(total=None)[source]#
Yield Category objects from Special:Unusedcategories.
- Parameters:
total – number of pages to return
- unusedfiles(total=None)[source]#
Yield FilePage objects from Special:Unusedimages.
- Parameters:
total – number of pages to return
- unwatchedpages(total=None)[source]#
Yield Pages from Special:Unwatchedpages (requires Admin privileges).
- Parameters:
total – number of pages to return
- usercontribs(user=None, userprefix=None, start=None, end=None, reverse=False, namespaces=None, minor=None, total=None, top_only=False)[source]#
Iterate contributions by a particular user.
Iterated values are in the same format as recentchanges.
- Parameters:
user – Iterate contributions by this user (name or IP)
userprefix – Iterate contributions by all users whose names or IPs start with this substring
start – Iterate contributions starting at this Timestamp
end – Iterate contributions ending at this Timestamp
reverse (bool) – Iterate oldest contributions first (default: newest)
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – only iterate pages in these namespaces
minor – if True, iterate only minor edits; if False and not None, iterate only non-minor edits (default: iterate both)
total (int | None) – limit result to this number of pages
top_only (bool) – if True, iterate only edits which are the latest revision (default: False)
- Raises:
pywikibot.exceptions.Error – either user or userprefix must be non-empty
KeyError – a namespace identifier was not resolved
TypeError – a namespace identifier has an inappropriate type such as NoneType or bool
- users(usernames)[source]#
Iterate info about a list of users by name or IP.
See also
- Parameters:
usernames (list, or other iterable, of str) – a list of user names
- wantedcategories(total=None)[source]#
Yield Pages from Special:Wantedcategories.
- Parameters:
total – number of pages to return
- wantedfiles(total=None)[source]#
Yield Pages from Special:Wantedfiles.
- Parameters:
total – number of pages to return
- wantedpages(total=None)[source]#
Yield Pages from Special:Wantedpages.
- Parameters:
total – number of pages to return
- wantedtemplates(total=None)[source]#
Yield Pages from Special:Wantedtemplates.
- Parameters:
total – number of pages to return
- watched_pages(force=False, total=None, *, with_talkpage=True)[source]#
Return watchlist.
Note
watched_pages
is a restartable generator. Seetools.collections.GeneratorWrapper
for its usage.See also
New in version 8.1: the with_talkpage parameter.
- Parameters:
force (bool) – Reload watchlist
total (int | None) – if not None, limit the generator to yielding this many items in total
with_talkpage (bool) – if false, ignore talk pages and special pages
- Returns:
generator of pages in watchlist
- Return type:
Generator[Page, Any, None]
- watchlist_revs(start=None, end=None, reverse=False, namespaces=None, minor=None, bot=None, anon=None, total=None)[source]#
Iterate revisions to pages on the bot user’s watchlist.
Iterated values will be in same format as recentchanges.
See also
- Parameters:
start – Iterate revisions starting at this Timestamp
end – Iterate revisions ending at this Timestamp
reverse (bool) – Iterate oldest revisions first (default: newest)
namespaces (iterable of str or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers.) – only iterate pages in these namespaces
minor – if True, only list minor edits; if False (and not None), only list non-minor edits
bot – if True, only list bot edits; if False (and not None), only list non-bot edits
anon – if True, only list anon edits; if False (and not None), only list non-anon edits
- Raises:
KeyError – a namespace identifier was not resolved
TypeError – a namespace identifier has an inappropriate type such as NoneType or bool
DataSite
— API Interface for Wikibase#
Objects representing API interface to Wikibase site.
- class pywikibot.site._datasite.DataSite(*args, **kwargs)[source]#
Bases:
APISite
Wikibase data capable site.
- addClaim(entity, claim, bot=True, summary=None)[source]#
Add a claim.
- Parameters:
entity (pywikibot.page.WikibaseEntity) – Entity to modify
claim (pywikibot.page.Claim) – Claim to be added
bot (bool) – Whether to mark the edit as a bot edit
summary (str | None) – Edit summary
- Return type:
None
- add_form(lexeme, form, *, bot=True, baserevid=None)[source]#
Add a form.
- Parameters:
lexeme (LexemePage) – Lexeme to modify
form (LexemeForm) – Form to be added
bot (bool) –
- Keyword Arguments:
bot – Whether to mark the edit as a bot edit
baserevid – Base revision id override, used to detect conflicts.
- Return type:
dict
- changeClaimTarget(claim, snaktype='value', bot=True, summary=None)[source]#
Set the claim target to the value of the provided claim target.
- Parameters:
claim (Claim) – The source of the claim target value
snaktype (str) – An optional snaktype (‘value’, ‘novalue’ or ‘somevalue’). Default: ‘value’
bot (bool) – Whether to mark the edit as a bot edit
summary (str) – Edit summary
- property concept_base_uri#
Return the base uri for concepts/entities.
- Returns:
concept base uri
- Return type:
str
- editEntity(entity, data, bot=True, **kwargs)[source]#
Edit entity.
Note
This method is unable to create entities other than ‘item’ if dict with API parameters was passed to ‘entity’ parameter.
- Parameters:
entity (WikibaseEntity or dict) – Page to edit, or dict with API parameters to use for entity identification
data (dict) – data updates
bot (bool) – Whether to mark the edit as a bot edit
- Returns:
New entity data
- Return type:
dict
- editQualifier(claim, qualifier, new=False, bot=True, summary=None)[source]#
Create/Edit a qualifier.
Changed in version 7.0: deprecated
baserevid
parameter was removed
- editSource(claim, source, new=False, bot=True, summary=None)[source]#
Create/Edit a source.
Changed in version 7.0: deprecated
baserevid
parameter was removed
- edit_form_elements(form, data, *, bot=True, baserevid=None)[source]#
Edit lexeme form elements.
- Parameters:
form (LexemeForm) – Form
data (dict) – data updates
bot (bool) –
- Keyword Arguments:
bot – Whether to mark the edit as a bot edit
baserevid – Base revision id override, used to detect conflicts.
- Returns:
New form data
- Return type:
dict
- getPropertyType(prop)[source]#
Obtain the type of a property.
This is used specifically because we can cache the value for a much longer time (near infinite).
- get_entity_for_entity_id(entity_id)[source]#
Return a new instance for given entity id.
- Raises:
pywikibot.exceptions.NoWikibaseEntityError – there is no entity with the id
- Returns:
a WikibaseEntity subclass
- Return type:
- get_namespace_for_entity_type(entity_type)[source]#
Return namespace for given entity type.
- Returns:
corresponding namespace
- Return type:
- get_repo_for_entity_type(entity_type)[source]#
Get the data repository for the entity type.
When no foreign repository is defined for the entity type, the method returns this repository itself even if it does not support that entity type either.
New in version 8.0.
- Raises:
ValueError – when invalid entity type was provided
- Parameters:
entity_type (str) –
- Return type:
- loadcontent(identification, *props)[source]#
Fetch the current content of a Wikibase item.
This is called loadcontent since wbgetentities does not support fetching old revisions. Eventually this will get replaced by an actual loadrevisions.
- Parameters:
identification (dict) – Parameters used to identify the page(s)
props – the optional properties to fetch.
- mergeItems(from_item, to_item, ignore_conflicts=None, summary=None, bot=True)[source]#
Merge two items together.
- Parameters:
- Returns:
dict API output
- Return type:
dict
- mergeLexemes(from_lexeme, to_lexeme, summary=None, *, bot=True)[source]#
Merge two lexemes together.
- Parameters:
from_lexeme (LexemePage) – Lexeme to merge from
to_lexeme (LexemePage) – Lexeme to merge into
summary (str) – Edit summary
bot (bool) –
- Keyword Arguments:
bot – Whether to mark the edit as a bot edit
- Returns:
dict API output
- Return type:
dict
- parsevalue(datatype, values, options=None, language=None, validate=False)[source]#
Send data values to the wikibase parser for interpretation.
New in version 7.5.
See also
- Parameters:
datatype (str) – datatype of the values being parsed. Refer the API for a valid datatype.
values (List[str]) – list of values to be parsed
options (Dict[str, Any] | None) – any additional options for wikibase parser (for time, ‘precision’ should be specified)
language (str | None) – code of the language to parse the value in
validate (bool) – whether parser should provide data validation as well as parsing
- Returns:
list of parsed values
- Raises:
ValueError – parsing failed due to some invalid input values
- Return type:
List[Any]
- preload_entities(pagelist, groupsize=50)[source]#
Yield subclasses of WikibaseEntity’s with content prefilled.
Note
Pages will be iterated in a different order than in the underlying pagelist.
- Parameters:
pagelist – an iterable that yields either WikibaseEntity objects, or Page objects linked to an ItemPage.
groupsize (int) – how many pages to query at a time
- property property_namespace#
Return namespace for properties.
- Returns:
property namespace
- Return type:
- removeClaims(claims, bot=True, summary=None)[source]#
Remove claims.
Changed in version 7.0: deprecated
baserevid
parameter was removed- Parameters:
claims (List[Claim]) – Claims to be removed
bot (bool) – Whether to mark the edit as a bot edit
summary (str) – Edit summary
- removeSources(claim, sources, bot=True, summary=None)[source]#
Remove sources.
Changed in version 7.0: deprecated
baserevid
parameter was removed- Parameters:
claim (Claim) – A Claim object to remove the sources from
sources (list) – A list of Claim objects that are sources
bot (bool) – Whether to mark the edit as a bot edit
summary (str | None) – Edit summary
- remove_form(form, *, bot=True, baserevid=None)[source]#
Remove a form.
- Parameters:
form (LexemeForm) – Form to be removed
bot (bool) –
- Keyword Arguments:
bot – Whether to mark the edit as a bot edit
baserevid – Base revision id override, used to detect conflicts.
- Return type:
dict
- remove_qualifiers(claim, qualifiers, bot=True, summary=None)[source]#
Remove qualifiers.
Changed in version 7.0: deprecated
baserevid
parameter was removed
- save_claim(claim, summary=None, bot=True)[source]#
Save the whole claim to the wikibase site.
- Parameters:
claim (pywikibot.page.Claim) – The claim to save
bot (bool) – Whether to mark the edit as a bot edit
summary (str | None) – Edit summary
- search_entities(search, language, total=None, **kwargs)[source]#
Search for pages or properties that contain the given text.
- Parameters:
search (str) – Text to find.
language (str) – Language to search in.
total (int | None) – Maximum number of pages to retrieve in total, or None in case of no limit.
- Returns:
‘search’ list from API output.
- Return type:
Generator
- property sparql_endpoint#
Return the sparql endpoint url, if any has been set.
- Returns:
sparql endpoint url
- Return type:
str|None
- tabular_data_repository()[source]#
Return Site object for the tabular-datas repository e.g. commons.
- wbsetaliases(itemdef, aliases, **kwargs)[source]#
Set aliases for a single Wikibase entity.
See self._wbset_action() for parameters
- wbsetdescription(itemdef, description, **kwargs)[source]#
Set description for a single Wikibase entity.
See self._wbset_action()
Obsolete Sites
— Outdated Sites#
Objects representing obsolete MediaWiki sites.
- class pywikibot.site._obsoletesites.ClosedSite(code, fam=None, user=None)[source]#
Bases:
APISite
Site closed to read-only mode.
- Parameters:
code (str) –
fam (str | pywikibot.family.Family | None) –
user (str | None) –
Siteinfo
— Site Info Container#
Objects representing site info data contents.
- class pywikibot.site._siteinfo.Siteinfo(site)[source]#
Bases:
Container
A ‘dictionary’ like container for siteinfo.
This class queries the server to get the requested siteinfo property. Optionally it can cache this directly in the instance so that later requests don’t need to query the server.
All values of the siteinfo property ‘general’ are directly available.
Initialise it with an empty cache.
- BOOLEAN_PROPS = {'general': ['imagewhitelistenabled', 'langconversion', 'titleconversion', 'rtl', 'readonly', 'writeapi', 'variantarticlepath', 'misermode', 'uploadsenabled'], 'magicwords': ['case-sensitive'], 'namespaces': ['subpages', 'content', 'nonincludable']}#
- WARNING_REGEX = re.compile('Unrecognized values? for parameter ["\\\']siprop["\\\']: (.+?)\\.?')#
- get(key, get_default=True, cache=True, expiry=False)[source]#
Return a siteinfo property.
It will never throw an APIError if it only stated, that the siteinfo property doesn’t exist. Instead it will use the default value.
See also
_get_siteinfo
- Parameters:
key (str) – The name of the siteinfo property.
get_default (bool) – Whether to throw an KeyError if the key is invalid.
cache (bool) – Caches the result internally so that future accesses via this method won’t query the server.
expiry (datetime | float | bool) – If the cache is older than the expiry it ignores the cache and queries the server to get the newest value.
- Returns:
The gathered property
- Raises:
KeyError – If the key is not a valid siteinfo property and the get_default option is set to False.
- Return type:
Any
- get_requested_time(key)[source]#
Return when ‘key’ was successfully requested from the server.
If the property is actually in the siprop ‘general’ it returns the last request from the ‘general’ siprop.
- Parameters:
key (str) – The siprop value or a property of ‘general’.
- Returns:
The last time the siprop of ‘key’ was requested.
- Return type:
None (never), False (default),
datetime.datetime
(cached)
Namespace
— Namespace Object#
Objects representing Namespaces of MediaWiki site.
- class pywikibot.site._namespace.BuiltinNamespace(value)[source]#
Bases:
IntEnum
Builtin namespace enum.
- CATEGORY = 14#
- CATEGORY_TALK = 15#
- FILE = 6#
- FILE_TALK = 7#
- HELP = 12#
- HELP_TALK = 13#
- MAIN = 0#
- MEDIA = -2#
- MEDIAWIKI = 8#
- MEDIAWIKI_TALK = 9#
- PROJECT = 4#
- PROJECT_TALK = 5#
- SPECIAL = -1#
- TALK = 1#
- TEMPLATE = 10#
- TEMPLATE_TALK = 11#
- USER = 2#
- USER_TALK = 3#
- property canonical: str#
Canonical form of MediaWiki built-in namespace.
New in version 7.1.
- class pywikibot.site._namespace.Namespace(id, canonical_name=None, custom_name=None, aliases=None, **kwargs)[source]#
Bases:
Iterable
,ComparableMixin
Namespace site data object.
This is backwards compatible with the structure of entries in site._namespaces which were a list of:
[customised namespace, canonical namespace name?, namespace alias*]
If the canonical_name is not provided for a namespace between -2 and 15, the MediaWiki built-in names are used. Image and File are aliases of each other by default.
If only one of canonical_name and custom_name are available, both properties will have the same value.
- Parameters:
canonical_name (str | None) – Canonical name
custom_name (str | None) – Name defined in server LocalSettings.php
aliases (List[str] | None) – Aliases
- CATEGORY = 14#
- CATEGORY_TALK = 15#
- FILE = 6#
- FILE_TALK = 7#
- HELP = 12#
- HELP_TALK = 13#
- MAIN = 0#
- MEDIA = -2#
- MEDIAWIKI = 8#
- MEDIAWIKI_TALK = 9#
- PROJECT = 4#
- PROJECT_TALK = 5#
- SPECIAL = -1#
- TALK = 1#
- TEMPLATE = 10#
- TEMPLATE_TALK = 11#
- USER = 2#
- USER_TALK = 3#
- classmethod builtin_namespaces(case='first-letter')[source]#
Return a dict of the builtin namespaces.
- Parameters:
case (str) –
- canonical_namespaces = {-2: 'Media', -1: 'Special', 0: '', 1: 'Talk', 2: 'User', 3: 'User talk', 4: 'Project', 5: 'Project talk', 6: 'File', 7: 'File talk', 8: 'MediaWiki', 9: 'MediaWiki talk', 10: 'Template', 11: 'Template talk', 12: 'Help', 13: 'Help talk', 14: 'Category', 15: 'Category talk'}#
- class pywikibot.site._namespace.NamespacesDict(namespaces)[source]#
Bases:
Mapping
,SelfCallMixin
An immutable dictionary containing the Namespace instances.
It adds a deprecation message when called as the ‘namespaces’ property of APISite was callable.
Create new dict using the given namespaces.
- lookup_name(name)[source]#
Find the Namespace for a name also checking aliases.
- Parameters:
name (str) – Name of the namespace.
- Return type:
Namespace | None
- lookup_normalized_name(name)[source]#
Find the Namespace for a name also checking aliases.
The name has to be normalized and must be lower case.
- Parameters:
name (str) – Name of the namespace.
- Return type:
Namespace | None
- resolve(identifiers)[source]#
Resolve namespace identifiers to obtain Namespace objects.
Identifiers may be any value for which int() produces a valid namespace id, except bool, or any string which Namespace.lookup_name successfully finds. A numerical string is resolved as an integer.
- Parameters:
identifiers (iterable of str or Namespace key, or a single instance of those types) – namespace identifiers
- Returns:
list of Namespace objects in the same order as the identifiers
- Raises:
KeyError – a namespace identifier was not resolved
TypeError – a namespace identifier has an inappropriate type such as NoneType or bool
- Return type:
List[Namespace]
TokenWallet
— Token Wallet#
Objects representing api tokens.
- class pywikibot.site._tokenwallet.TokenWallet(site)[source]#
Bases:
Container
Container for tokens.
You should not use this container class directly; use
APISite.tokens
instead which gives access to the site’s TokenWallet instance.- Parameters:
site (APISite) –
- load_tokens(*args, **kwargs)[source]#
Clear cache to lazy load tokens when needed.
Deprecated since version 8.0: Use
clear()
instead.Changed in version 8.0: Clear the cache instead of loading tokens. All parameters are ignored.
- Parameters:
args (Any) –
kwargs (Any) –
- Return type:
None
- update_tokens(tokens)[source]#
Return a list of new tokens for a given list of tokens.
This method can be used if a token is outdated and has to be renewed but the token type is unknown and we only have the old token. It first gets the token names from all given tokens, clears the cache and returns fresh new tokens of the found types.
Usage:
>>> import pywikibot >>> site = pywikibot.Site() >>> tokens = [site.tokens['csrf']] >>> new_tokens = site.tokens.update_tokens(tokens)
r._params['token'] = r.site.tokens.update_tokens(r._params['token'])
New in version 8.0.
- Parameters:
tokens (List[str]) –
- Return type:
List[str]
Uploader
— Uploader Interface#
Objects representing API upload to MediaWiki site.
- class pywikibot.site._upload.Uploader(site, filepage, *, source_filename=None, source_url=None, comment=None, text=None, watch=False, chunk_size=0, asynchronous=False, ignore_warnings=False, report_success=None)[source]#
Bases:
object
Uploader class to upload a file to the wiki.
New in version 7.1.
- Parameters:
site (pywikibot.site.APISite) – The current site to work on
filepage (FilePage) – a FilePage object from which the wiki-name of the file will be obtained.
source_filename (str | None) – path to the file to be uploaded
source_url (str | None) – URL of the file to be uploaded
comment (str | None) – Edit summary; if this is not provided, then filepage.text will be used. An empty summary is not permitted. This may also serve as the initial page text (see below).
text (str | None) – Initial page text; if this is not set, then filepage.text will be used, or comment.
watch (bool) – If true, add filepage to the bot user’s watchlist
chunk_size (int) – The chunk size in bytes for chunked uploading (see API:Upload#Chunked_uploading). It will only upload in chunks, if the chunk size is positive but lower than the file size.
asynchronous (bool) – Make potentially large file operations asynchronous on the server side when possible.
ignore_warnings (bool or callable or iterable of str) –
It may be a static boolean, a callable returning a boolean or an iterable. The callable gets a list of UploadError instances and the iterable should contain the warning codes for which an equivalent callable would return True if all UploadError codes are in thet list. If the result is False it’ll not continue uploading the file and otherwise disable any warning and reattempt to upload the file.
Note
If report_success is True or None it’ll raise an UploadError exception if the static boolean is False.
report_success (bool | None) – If the upload was successful it’ll print a success message and if ignore_warnings is set to False it’ll raise an UploadError if a warning occurred. If it’s None (default) it’ll be True if ignore_warnings is a bool and False otherwise. If it’s True or None ignore_warnings must be a bool.
- submit(request, result, data_result, ignore_warnings, ignore_all_warnings, report_success, file_key)[source]#
Submit request and return whether upload was successful.
- Parameters:
data_result (str | None) –
- Return type:
bool
- upload()[source]#
Check for required parameters to upload and run the job.
- Returns:
Whether the upload was successful.
- Return type:
bool
- upload_warnings = {'bad-prefix': 'Target filename has a bad prefix {msg}.', 'badfilename': 'Target filename is invalid.', 'duplicate': 'Uploaded file is a duplicate of {msg}.', 'duplicate-archive': 'The file is a duplicate of a deleted file {msg}.', 'duplicate-version': 'The upload is an exact duplicate of older version(s) of this file.', 'empty-file': 'File {msg} is empty.', 'exists': 'File {msg} already exists.', 'exists-normalized': 'File exists with different extension as {msg!r}.', 'filetype-unwanted-type': 'File {msg} type is unwanted type.', 'no-change': 'The upload is an exact duplicate of the current version of this file.', 'page-exists': 'Target filename exists but with a different file {msg}.', 'was-deleted': 'The file {msg} was previously deleted.'}#