pywikibot.data package

Module providing several layers of data access to the wiki.

Submodules

pywikibot.data.api module

Interface to Mediawiki’s api.php.

exception pywikibot.data.api.APIError(code, info, **kwargs)[source]

Bases: pywikibot.exceptions.Error

The wiki site returned an error message.

__init__(code, info, **kwargs)[source]

Save error dict returned by MW API.

__module__ = 'pywikibot.data.api'
__repr__()[source]

Return internal representation.

__str__()[source]

Return a string representation.

class pywikibot.data.api.APIGenerator(action, continue_name='continue', limit_name='limit', data_name='data', **kwargs)[source]

Bases: pywikibot.data.api._RequestWrapper

Iterator that handle API responses containing lists.

The iterator will iterate each item in the query response and use the continue request parameter to retrieve the next portion of items automatically. If the limit attribute is set, the iterator will stop after iterating that many values.

__init__(action, continue_name='continue', limit_name='limit', data_name='data', **kwargs)[source]

Initialize an APIGenerator object.

kwargs are used to create a Request object; see that object’s documentation for values.

Parameters
  • action (str) – API action name.

  • continue_name (str) – Name of the continue API parameter.

  • limit_name (str) – Name of the limit API parameter.

  • data_name (str) – Name of the data in API response.

__iter__()[source]

Submit request and iterate the response.

Continues response as needed until limit (if defined) is reached.

__module__ = 'pywikibot.data.api'
set_maximum_items(value)[source]

Set the maximum number of items to be retrieved from the wiki.

If not called, most queries will continue as long as there is more data to be retrieved from the API.

Parameters

value (int or str or None) – The value of maximum number of items to be retrieved in total to set. Ignores None value.

set_query_increment(value)[source]

Set the maximum number of items to be retrieved per API query.

If not called, the default is config.step.

Parameters

value (int) – The value of maximum number of items to be retrieved per API request to set.

exception pywikibot.data.api.APIMWException(mediawiki_exception_class_name, info, **kwargs)[source]

Bases: pywikibot.data.api.APIError

The API site returned an error about a MediaWiki internal exception.

__init__(mediawiki_exception_class_name, info, **kwargs)[source]

Save error dict returned by MW API.

__module__ = 'pywikibot.data.api'
class pywikibot.data.api.CTEBinaryBytesGenerator(*args, **kwargs)[source]

Bases: email.generator.BytesGenerator

Workaround for bug in python 3 email handling of CTE binary.

__init__(*args, **kwargs)[source]

Initializer.

__module__ = 'pywikibot.data.api'
class pywikibot.data.api.CTEBinaryMIMEMultipart(_subtype='mixed', boundary=None, _subparts=None, **_params)[source]

Bases: email.mime.multipart.MIMEMultipart

Workaround for bug in python 3 email handling of CTE binary.

__module__ = 'pywikibot.data.api'
as_bytes(unixfrom=False, policy=None)[source]

Return unmodified binary payload.

class pywikibot.data.api.CachedRequest(expiry, *args, **kwargs)[source]

Bases: pywikibot.data.api.Request

Cached request.

__abstractmethods__ = frozenset({})
__init__(expiry, *args, **kwargs)[source]

Initialize a CachedRequest object.

Parameters

expiry – either a number of days or a datetime.timedelta object

__module__ = 'pywikibot.data.api'
classmethod create_simple(site, **kwargs)[source]

Unsupported as it requires at least two parameters.

submit()[source]

Submit cached request.

class pywikibot.data.api.EnableSSLSiteWrapper(site)[source]

Bases: object

Wrapper to change the site protocol to https.

__eq__(other)[source]

Compare two objects.

__getattr__(attr)[source]

Access object’s site attributes.

__hash__ = None
__init__(site)[source]

Initializer.

__module__ = 'pywikibot.data.api'
__repr__()[source]

Return internal representation.

protocol()[source]

Return protocol.

class pywikibot.data.api.ListGenerator(listaction, **kwargs)[source]

Bases: pywikibot.data.api.QueryGenerator

Iterator for queries of type action=query&list=foo.

See the API documentation for types of lists that can be queried. Lists include both site-wide information (such as ‘allpages’) and page-specific information (such as ‘backlinks’).

This iterator yields a dict object for each member of the list returned by the API, with the format of the dict depending on the particular list command used. For those lists that contain page information, it may be easier to use the PageGenerator class instead, as that will convert the returned information into a Page object.

__init__(listaction, **kwargs)[source]

Initializer.

Required and optional parameters are as for Request, except that action=query is assumed and listaction is required.

Parameters

listaction (str) – the “list=” type from api.php

__module__ = 'pywikibot.data.api'
class pywikibot.data.api.LogEntryListGenerator(logtype=None, **kwargs)[source]

Bases: pywikibot.data.api.ListGenerator

Iterator for queries of list ‘logevents’.

Yields LogEntry objects instead of dicts.

__init__(logtype=None, **kwargs)[source]

Initializer.

__module__ = 'pywikibot.data.api'
result(pagedata)[source]

Instantiate LogEntry from data from api.

class pywikibot.data.api.LoginManager(password=None, site=None, user=None, sysop=NotImplemented, verbose=NotImplemented, username='[deprecated name of user]')[source]

Bases: pywikibot.login.LoginManager

Supply getCookie() method to use API interface.

__module__ = 'pywikibot.data.api'
getCookie()[source]

Login to the site.

Note, this doesn’t actually return or do anything with cookies. The threadedhttp module takes care of all the cookie stuff, this just has a legacy name for now and should be renamed in the future.

Returns

empty string if successful, throws exception on failure

get_login_token()[source]

Fetch login token from action=query&meta=tokens.

Requires MediaWiki >= 1.27.

Returns

login token

Return type

str

storecookiedata(data)[source]

Ignore data; cookies are set by threadedhttp module.

pywikibot.data.api.MIMEMultipart

alias of pywikibot.data.api.CTEBinaryMIMEMultipart

class pywikibot.data.api.OptionSet(site=None, module=None, param=None, dict=None)[source]

Bases: collections.abc.MutableMapping

A class to store a set of options which can be either enabled or not.

If it is instantiated with the associated site, module and parameter it will only allow valid names as options. If instantiated ‘lazy loaded’ it won’t checks if the names are valid until the site has been set (which isn’t required, but recommended). The site can only be set once if it’s not None and after setting it, any site (even None) will fail.

__abstractmethods__ = frozenset({})
__contains__(name)[source]

Return True if option has been set.

__delitem__(name)[source]

Remove the item by setting it to None.

__getitem__(name)[source]

Return whether the option is enabled.

Returns

If the name has been set it returns whether it is enabled. Otherwise it returns None. If the site has been set it raises a KeyError if the name is invalid. Otherwise it might return a value even though the name might be invalid.

Return type

bool/None

__init__(site=None, module=None, param=None, dict=None)[source]

Initializer.

If a site is given, the module and param must be given too.

Parameters
  • site (APISite) – The associated site

  • module (str) – The module name which is used by paraminfo. (Ignored when site is None)

  • param (str) – The parameter name inside the module. That parameter must have a ‘type’ entry. (Ignored when site is None)

  • dict (dict) – The initializing dict which is used for from_dict.

__iter__()[source]

Iterate over each enabled and disabled option.

__len__()[source]

Return the number of enabled and disabled options.

__module__ = 'pywikibot.data.api'
__setitem__(name, value)[source]

Set option to enabled, disabled or neither.

api_iter()[source]

Iterate over each option as they appear in the URL.

clear()[source]

Clear all enabled and disabled options.

from_dict(dict)[source]

Load options from the dict.

The options are not cleared before. If changes have been made previously, but only the dict values should be applied it needs to be cleared first.

Parameters

dict (dict (keys are strings, values are bool/None)) – A dictionary containing for each entry either the value False, True or None. The names must be valid depending on whether they enable or disable the option. All names with the value None can be in either of the list.

class pywikibot.data.api.PageGenerator(generator, g_content=False, **kwargs)[source]

Bases: pywikibot.data.api.QueryGenerator

Iterator for response to a request of type action=query&generator=foo.

This class can be used for any of the query types that are listed in the API documentation as being able to be used as a generator. Instances of this class iterate Page objects.

__init__(generator, g_content=False, **kwargs)[source]

Initializer.

Required and optional parameters are as for Request, except that action=query is assumed and generator is required.

Parameters
  • generator (str) – the “generator=” type from api.php

  • g_content – if True, retrieve the contents of the current version of each Page (default False)

__module__ = 'pywikibot.data.api'
result(pagedata)[source]

Convert page dict entry from api to Page object.

This can be overridden in subclasses to return a different type of object.

class pywikibot.data.api.ParamInfo(site, preloaded_modules=None, modules_only_mode=None)[source]

Bases: collections.abc.Container

API parameter information data object.

Provides cache aware fetching of parameter information.

It does not support the format modules.

__abstractmethods__ = frozenset({})
__contains__(key)[source]

Return whether the key is valid.

__getitem__(key)[source]

Return a paraminfo module for the module path, caching it.

Use the module path, such as ‘query+x’, to obtain the paraminfo for submodule ‘x’ in the query module.

If the key does not include a ‘+’ and is not present in the top level of the API, it will fallback to looking for the key ‘query+x’.

__init__(site, preloaded_modules=None, modules_only_mode=None)[source]

Initializer.

Parameters
  • preloaded_modules (set of string) – API modules to preload

  • modules_only_mode (bool or None to only use default, which True if the site is 1.25wmf4+) – use the ‘modules’ only syntax for API request

__len__()[source]

Return number of cached modules.

__module__ = 'pywikibot.data.api'
property action_modules

Set of all action modules.

attributes(attribute, modules=None)[source]

Mapping of modules with an attribute to the attribute value.

It will include all modules which have that attribute set, also if that attribute is empty or set to False.

Parameters
  • attribute (basestring) – attribute name

  • modules (set or None) – modules to include. If None (default), it’ll load all modules including all submodules using the paths.

Return type

dict using modules as keys

fetch(modules)[source]

Fetch paraminfo for multiple modules.

No exception is raised when paraminfo for a module does not exist. Use __getitem__ to cause an exception if a module does not exist.

Parameters

modules (iterable or str) – API modules to load

Return type

NoneType

init_modules = frozenset({'main', 'paraminfo'})
property module_paths

Set of all modules using their paths.

normalize_modules(modules)[source]

Convert the modules into module paths.

Add query+ to any query module name not also in action modules.

Returns

The modules converted into a module paths

Return type

set

classmethod normalize_paraminfo(data)[source]

Convert both old and new API JSON into a new-ish data structure.

For duplicate paths, the value will be False.

parameter(module, param_name)[source]

Get details about one modules parameter.

Returns None if the parameter does not exist.

Parameters
  • module (str) – API module name

  • param_name (str) – parameter name in the module

Returns

metadata that describes how the parameter may be used

Return type

dict or None

paraminfo_keys = frozenset({'formatmodules', 'mainmodule', 'modules', 'pagesetmodule', 'querymodules'})
property prefix_map

Mapping of module to its prefix for all modules with a prefix.

This loads paraminfo for all modules.

property query_modules

Set of all query module names without query+ path prefix.

root_modules = frozenset({'main', 'pageset'})
submodules(name, path=False)[source]

Set of all submodules.

Parameters
  • name (str) – The name of the parent module.

  • path (bool) – Whether the path and not the name is returned.

Returns

The names or paths of the submodules.

Return type

set

class pywikibot.data.api.PropertyGenerator(prop, **kwargs)[source]

Bases: pywikibot.data.api.QueryGenerator

Iterator for queries of type action=query&prop=foo.

See the API documentation for types of page properties that can be queried.

This iterator yields one or more dict object(s) corresponding to each “page” item(s) from the API response; the calling module has to decide what to do with the contents of the dict. There will be one dict for each page queried via a titles= or ids= parameter (which must be supplied when instantiating this class).

__init__(prop, **kwargs)[source]

Initializer.

Required and optional parameters are as for Request, except that action=query is assumed and prop is required.

Parameters

prop (str) – the “prop=” type from api.php

__iter__()[source]

Yield results.

__module__ = 'pywikibot.data.api'
property props

The requested property names.

class pywikibot.data.api.QueryGenerator(**kwargs)[source]

Bases: pywikibot.data.api._RequestWrapper

Base class for iterators that handle responses to API action=query.

By default, the iterator will iterate each item in the query response, and use the (query-)continue element, if present, to continue iterating as long as the wiki returns additional values. However, if the iterator’s limit attribute is set to a positive int, the iterator will stop after iterating that many values. If limit is negative, the limit parameter will not be passed to the API at all.

Most common query types are more efficiently handled by subclasses, but this class can be used directly for custom queries and miscellaneous types (such as “meta=…”) that don’t return the usual list of pages or links. See the API documentation for specific query options.

__init__(**kwargs)[source]

Initialize a QueryGenerator object.

kwargs are used to create a Request object; see that object’s documentation for values. ‘action’=’query’ is assumed.

__iter__()[source]

Submit request and iterate the response based on self.resultkey.

Continues response as needed until limit (if any) is reached.

__module__ = 'pywikibot.data.api'
result(data)[source]

Process result data as needed for particular subclass.

set_maximum_items(value)[source]

Set the maximum number of items to be retrieved from the wiki.

If not called, most queries will continue as long as there is more data to be retrieved from the API.

If set to -1 (or any negative value), the “limit” parameter will be omitted from the request. For some request types (such as prop=revisions), this is necessary to signal that only current revision is to be returned.

Parameters

value (int or str or None) – The value of maximum number of items to be retrieved in total to set. Ignores None value.

set_namespace(namespaces)[source]

Set a namespace filter on this query.

Parameters

namespaces (iterable of basestring or Namespace key, or a single instance of those types. May be a '|' separated list of namespace identifiers. An empty iterator clears any namespace restriction.) – namespace identifiers to limit query results

Raises

KeyError – a namespace identifier was not resolved

# TODO: T196619 # :raises TypeError: module does not support a namespace parameter # or a namespace identifier has an inappropriate # type such as NoneType or bool, or more than one namespace # if the API module does not support multiple namespaces

set_query_increment(value)[source]

Set the maximum number of items to be retrieved per API query.

If not called, the default is to ask for “max” items and let the API decide how many to send.

support_namespace()[source]

Check if namespace is a supported parameter on this query.

Note: this function will be removed when self.set_namespace() will

throw TypeError() instead of just giving a warning. See T196619.

Returns

True if yes, False otherwise

Return type

bool

class pywikibot.data.api.Request(site=None, mime=None, throttle=True, mime_params=None, max_retries=None, retry_wait=None, use_get=None, parameters=<object object>, **kwargs)[source]

Bases: collections.abc.MutableMapping

A request to a Site’s api.php interface.

Attributes of this object (except for the special parameters listed below) get passed as commands to api.php, and can be get or set using the dict interface. All attributes must be strings (or unicode). Use an empty string for parameters that don’t require a value. For example, Request(action=”query”, titles=”Foo bar”, prop=”info”, redirects=””) corresponds to the API request “api.php?action=query&titles=Foo%20bar&prop=info&redirects”

This is the lowest-level interface to the API, and can be used for any request that a particular site’s API supports. See the API documentation (https://www.mediawiki.org/wiki/API) and site-specific settings for details on what parameters are accepted for each request type.

Uploading files is a special case: to upload, the parameter “mime” must be true, and the parameter “file” must be set equal to a valid filename on the local computer, _not_ to the content of the file.

Returns a dict containing the JSON data returned by the wiki. Normally, one of the dict keys will be equal to the value of the ‘action’ parameter. Errors are caught and raise an APIError exception.

Example:

>>> r = Request(parameters={'action': 'query', 'meta': 'userinfo'})
>>> # This is equivalent to
>>> # https://{path}/api.php?action=query&meta=userinfo&format=json
>>> # change a parameter
>>> r['meta'] = "userinfo|siteinfo"
>>> # add a new parameter
>>> r['siprop'] = "namespaces"
>>> # note that "uiprop" param gets added automatically
>>> str(r.action)
'query'
>>> sorted(str(key) for key in r._params.keys())
['action', 'meta', 'siprop']
>>> [str(key) for key in r._params['action']]
['query']
>>> [str(key) for key in r._params['meta']]
['userinfo', 'siteinfo']
>>> [str(key) for key in r._params['siprop']]
['namespaces']
>>> data = r.submit()
>>> isinstance(data, dict)
True
>>> set(['query', 'batchcomplete', 'warnings']).issuperset(data.keys())
True
>>> 'query' in data
True
>>> sorted(str(key) for key in data[u'query'].keys())
['namespaces', 'userinfo']
__abstractmethods__ = frozenset({})
__contains__(key)[source]

Implement dict interface.

__delitem__(key)[source]

Implement dict interface.

__getitem__(key)[source]

Implement dict interface.

__init__(site=None, mime=None, throttle=True, mime_params=None, max_retries=None, retry_wait=None, use_get=None, parameters=<object object>, **kwargs)[source]

Create a new Request instance with the given parameters.

The parameters for the request can be defined via either the ‘parameters’ parameter or the keyword arguments. The keyword arguments were the previous implementation but could cause problems when there are arguments to the API named the same as normal arguments to this class. So the second parameter ‘parameters’ was added which just contains all parameters. When a Request instance is created it must use either one of them and not both at the same time. To have backwards compatibility it adds a parameter named ‘parameters’ to kwargs when both parameters are set as that indicates an old call and ‘parameters’ was originally supplied as a keyword parameter.

If undefined keyword arguments were given AND the ‘parameters’ parameter was supplied as a positional parameter it still assumes ‘parameters’ were part of the keyword arguments.

If a class is using Request and is directly forwarding the parameters, Request.clean_kwargs can be used to automatically convert the old kwargs mode into the new parameter mode. This normalizes the arguments so that when the API parameters are modified the changes can always be applied to the ‘parameters’ parameter.

Parameters
  • parameters (dict) – The parameters used for the request to the API.

  • site – The Site to which the request will be submitted. If not supplied, uses the user’s configured default Site.

  • mime (bool or dict) – If true, send in “multipart/form-data” format (default False). Parameters which should only be transferred via mime mode can be defined via that parameter too (an empty dict acts like ‘True’ not like ‘False’!).

  • mime_params – DEPRECATED! A dictionary of parameter which should only be transferred via mime mode. If not None sets mime to True.

  • max_retries – (optional) Maximum number of times to retry after errors, defaults to config.max_retries.

  • retry_wait – (optional) Minimum time in seconds to wait after an error, defaults to config.retry_wait seconds (doubles each retry until config.retry_max seconds is reached).

  • use_get – (optional) Use HTTP GET request if possible. If False it uses a POST request. If None, it’ll try to determine via action=paraminfo if the action requires a POST.

  • kwargs – The parameters used for the request to the API.

__iter__()[source]

Implement dict interface.

__len__()[source]

Implement dict interface.

__module__ = 'pywikibot.data.api'
__repr__()[source]

Return internal representation.

__setitem__(key, value)[source]

Set MediaWiki API request parameter.

Parameters
  • key (basestring) – param key

  • value (str in site encoding (string types may be a |-separated list) iterable, where items are converted to unicode with special handling for datetime.datetime to convert it to a string using the ISO 8601 format accepted by the MediaWiki API.) – param value(s)

__str__()[source]

Return a string representation.

classmethod clean_kwargs(kwargs)[source]

Convert keyword arguments into new parameters mode.

If there are no other arguments in kwargs apart from the used arguments by the class’ initializer it’ll just return kwargs and otherwise remove those which aren’t in the initializer and put them in a dict which is added as a ‘parameters’ keyword. It will always create a shallow copy.

Parameters

kwargs (dict) – The original keyword arguments which is not modified.

Returns

The normalized keyword arguments.

Return type

dict

classmethod create_simple(site, **kwargs)[source]

Create a new instance using all args except site for the API.

http_params()[source]

Return the parameters formatted for inclusion in an HTTP request.

DEPRECATED. See _encoded_items for explanation of encoding used.

items()[source]

Return a list of tuples containing the parameters in any order.

iteritems()[source]

Implement dict interface.

keys()[source]

Implement dict interface.

property mime

Return whether mime parameters are defined.

submit()[source]

Submit a query and parse the response.

Returns

a dict containing data retrieved from api.php

Return type

dict

wait()[source]

Determine how long to wait after a failed request.

exception pywikibot.data.api.UploadWarning(code, message, file_key=None, offset=0)[source]

Bases: pywikibot.data.api.APIError

Upload failed with a warning message (passed as the argument).

__init__(code, message, file_key=None, offset=0)[source]

Create a new UploadWarning instance.

Parameters
  • filekey (str or None) – The filekey of the uploaded file to reuse it later. If no key is known or it is an incomplete file it may be None.

  • offset (int or bool) – The starting offset for a chunked upload. Is False when there is no offset.

__module__ = 'pywikibot.data.api'
property message

Return warning message.

pywikibot.data.api.encode_url(query)[source]

Encode parameters to pass with a url.

Reorder parameters so that token parameters go last and call wraps urlencode. Return an HTTP URL query fragment which complies with https://www.mediawiki.org/wiki/API:Edit#Parameters (See the ‘token’ bullet.)

Parameters

query (mapping object or a sequence of two-element tuples) – keys and values to be uncoded for passing with a url

Returns

encoded parameters with token parameters at the end

Return type

str

pywikibot.data.api.update_page(page, pagedict, props=[])[source]

Update attributes of Page object page, based on query data in pagedict.

Parameters
  • page (Page) – object to be updated

  • pagedict (dict) – the contents of a “page” element of a query response

  • props (iterable of string) – the property names which resulted in pagedict. If a missing value in pagedict can indicate both ‘false’ and ‘not present’ the property which would make the value present must be in the props parameter.

Raises

pywikibot.data.mysql module

Miscellaneous helper functions for mysql queries.

pywikibot.data.mysql.mysql_query(query, params=None, dbname=None, verbose=None, encoding=NotImplemented)[source]

Yield rows from a MySQL query.

An example query that yields all ns0 pages might look like:

SELECT
 page_namespace,
 page_title,
FROM page
WHERE page_namespace = 0;

From MediaWiki 1.5, all projects use Unicode (UTF-8) character encoding. Cursor charset is utf8.

Parameters
  • query (str (unicode in py2)) – MySQL query to execute

  • params (tuple, list or dict of str (unicode in py2)) – input parameters for the query, if needed if list or tuple, %s shall be used as placeholder in the query string. if a dict, %(key)s shall be used as placeholder in the query string.

  • dbname (str) – db name

  • verbose (None or bool) – if True, print query to be executed; if None, config.verbose_output will be used.

Returns

generator which yield tuples

pywikibot.data.sparql module

SPARQL Query interface.

class pywikibot.data.sparql.Bnode(data, **kwargs)[source]

Bases: pywikibot.data.sparql.SparqlNode

Representation of blank node.

__init__(data, **kwargs)[source]

Create Bnode.

__module__ = 'pywikibot.data.sparql'
__repr__()[source]

Return repr(self).

class pywikibot.data.sparql.Literal(data, **kwargs)[source]

Bases: pywikibot.data.sparql.SparqlNode

Representation of RDF literal result type.

__init__(data, **kwargs)[source]

Create Literal object.

__module__ = 'pywikibot.data.sparql'
__repr__()[source]

Return repr(self).

class pywikibot.data.sparql.SparqlNode(value)[source]

Bases: pywikibot.tools.UnicodeMixin

Base class for SPARQL nodes.

__init__(value)[source]

Create a SparqlNode.

__module__ = 'pywikibot.data.sparql'
__unicode__()[source]
class pywikibot.data.sparql.SparqlQuery(endpoint=None, entity_url=None, repo=None, max_retries=None, retry_wait=None)[source]

Bases: object

SPARQL Query class.

This class allows to run SPARQL queries against any SPARQL endpoint.

__init__(endpoint=None, entity_url=None, repo=None, max_retries=None, retry_wait=None)[source]

Create endpoint.

Parameters
  • endpoint (str) – SPARQL endpoint URL

  • entity_url (str) – URL prefix for any entities returned in a query.

  • repo (pywikibot.site.DataSite) – The Wikibase site which we want to run queries on. If provided this overrides any value in endpoint and entity_url. Defaults to Wikidata.

  • max_retries (int) – (optional) Maximum number of times to retry after errors, defaults to config.max_retries.

  • retry_wait (float) – (optional) Minimum time in seconds to wait after an error, defaults to config.retry_wait seconds (doubles each retry until config.retry_max is reached).

__module__ = 'pywikibot.data.sparql'
ask(query, headers={'Accept': 'application/sparql-results+json', 'cache-control': 'no-cache'})[source]

Run SPARQL ASK query and return boolean result.

Parameters

query (str) – Query text

Return type

bool

get_items(query, item_name='item', result_type=<class 'set'>)[source]

Retrieve items which satisfy given query.

Items are returned as Wikibase IDs.

Parameters
  • query – Query string. Must contain ?{item_name} as one of the projected values.

  • item_name – Name of the value to extract

  • result_type (iterable) – type of the iterable in which SPARQL results are stored (default set)

Returns

item ids, e.g. Q1234

Return type

same as result_type

get_last_response()[source]

Return last received response.

Returns

Response object from last request or None

query(query, headers={'Accept': 'application/sparql-results+json', 'cache-control': 'no-cache'})[source]

Run SPARQL query and return parsed JSON result.

Parameters

query (str) – Query text

select(query, full_data=False, headers={'Accept': 'application/sparql-results+json', 'cache-control': 'no-cache'})[source]

Run SPARQL query and return the result.

The response is assumed to be in format defined by: https://www.w3.org/TR/2013/REC-sparql11-results-json-20130321/

Parameters
  • query (str) – Query text

  • full_data (bool) – Whether return full data objects or only values

Returns

List of query results or None if query failed

wait()[source]

Determine how long to wait after a failed request.

class pywikibot.data.sparql.URI(data, entity_url, **kwargs)[source]

Bases: pywikibot.data.sparql.SparqlNode

Representation of URI result type.

__init__(data, entity_url, **kwargs)[source]

Create URI object.

__module__ = 'pywikibot.data.sparql'
__repr__()[source]

Return repr(self).

getID()[source]

Get ID of Wikibase object identified by the URI.

Returns

ID of Wikibase object, e.g. Q1234

pywikibot.data.wikistats module

Objects representing WikiStats API.

class pywikibot.data.wikistats.WikiStats(url='https://wikistats.wmflabs.org/')[source]

Bases: object

Light wrapper around WikiStats data, caching responses and data.

The methods accept a Pywikibot family name as the WikiStats table name, mapping the names before calling the WikiStats API.

ALL_KEYS = {'editthis', 'gamepedias', 'gentoo', 'lxde', 'mediawikis', 'metapedias', 'neoseeker', 'opensuse', 'orain', 'pardus', 'referata', 'rodovid', 'scoutwiki', 'shoutwiki', 'sourceforge', 'uncyclomedia', 'w3cwikis', 'wikia', 'wikibooks', 'wikifur', 'wikinews', 'wikipedia', 'wikipedias', 'wikiquote', 'wikiquotes', 'wikisite', 'wikisource', 'wikisources', 'wikitravel', 'wikiversity', 'wikivoyage', 'wikkii', 'wiktionaries', 'wiktionary', 'wmspecials'}
ALL_TABLES = {'editthis', 'gamepedias', 'gentoo', 'lxde', 'mediawikis', 'metapedias', 'neoseeker', 'opensuse', 'orain', 'pardus', 'referata', 'rodovid', 'scoutwiki', 'shoutwiki', 'sourceforge', 'uncyclomedia', 'w3cwikis', 'wikia', 'wikibooks', 'wikifur', 'wikinews', 'wikipedias', 'wikiquotes', 'wikisite', 'wikisources', 'wikitravel', 'wikiversity', 'wikivoyage', 'wikkii', 'wiktionaries', 'wmspecials'}
FAMILY_MAPPING = {'wikipedia': 'wikipedias', 'wikiquote': 'wikiquotes', 'wikisource': 'wikisources', 'wiktionary': 'wiktionaries'}
MISC_SITES_TABLE = 'mediawikis'
OTHER_MULTILANG_TABLES = {'gentoo', 'lxde', 'metapedias', 'opensuse', 'pardus', 'rodovid', 'scoutwiki', 'uncyclomedia', 'wikifur', 'wikitravel'}
OTHER_TABLES = {'editthis', 'gamepedias', 'neoseeker', 'orain', 'referata', 'shoutwiki', 'sourceforge', 'w3cwikis', 'wikia', 'wikisite', 'wikkii', 'wmspecials'}
WMF_MULTILANG_TABLES = {'wikibooks', 'wikinews', 'wikipedias', 'wikiquotes', 'wikisources', 'wikiversity', 'wikivoyage', 'wiktionaries'}
__init__(url='https://wikistats.wmflabs.org/')[source]

Initializer.

__module__ = 'pywikibot.data.wikistats'
csv(table)[source]

Fetch and parse CSV for a table.

Parameters

table (basestring) – table of data to fetch

Return type

list

fetch(table, format='xml')[source]

Fetch data from WikiStats.

Parameters
  • table (basestring) – table of data to fetch

  • format ('xml' or 'csv'.) – Format of data to use

Return type

bytes

get(table, format=None)[source]

Get a list of a table of data using format.

Parameters
  • table (basestring) – table of data to fetch

  • format ('xml' or 'csv', or None to autoselect.) – Format of data to use

Return type

list

get_dict(table, format=None)[source]

Get dictionary of a table of data using format.

Parameters
  • table (basestring) – table of data to fetch

  • format ('xml' or 'csv', or None to autoselect.) – Format of data to use

Return type

dict

languages_by_size(table)[source]

Return ordered list of languages by size from WikiStats.

raw_cached(table, format)[source]

Cache raw data.

Parameters
  • table (basestring) – table of data to fetch

  • format ('xml' or 'csv'.) – Format of data to use

Return type

bytes

sorted(table, key)[source]

Reverse numerical sort of data.

Parameters
  • table – name of table of data

  • key – numerical key, such as id, total, good

xml(table)[source]

Fetch and parse XML for a table.

Parameters

table (basestring) – table of data to fetch

Return type

list