ws.client.redirects module

class ws.client.redirects.Redirects(api)

Bases: object

General interface for working with MediaWiki’s redirects.

MediaWiki provides multiple different queries for handling redirects, but they are all kind of awkward:

  1. The first way using the redirects parameter assumes that you already know the source titles or pageids of the redirects you want to resolve. The relevant titles or pageids parameters have considerably lower limits compared to lists or generator modules.

  2. Second way uses prop=redirects, which gives pages redirecting to pages generated by generator=allpages. There are much higher limits, but all pages that are neither redirects or redirect targets are returned as well.

  3. It is also possible to use generator=allpages with the gapfilterredir=redirects parameter to generate only redirect pages, but it is not possible to get the redirect targets at the same time.

  4. There is also list=allredirects module, but its purpose is a great mystery to me.

This class therefore uses the second method to query redirects for the whole wiki and caches the post-processed results. There are some limitations:

  • Interwiki redirects are not included in the mapping.

fetch(source_namespaces='all', target_namespaces='all')

Build a mapping of redirects in given namespaces.

Note that the mapping can contain double redirects, which could cause some algorithms to break. Always use Redirects.resolve() to resolve redirects.

Parameters
  • source_namespaces (list) –

    the namespace ID of the source title must be in this list in order to be included in the mapping (default is [0], the magic word "all" will select all available namespaces)

    Note

    Due to a MediaWiki bug, this parameter does not have any effect. Should be fixed in 1.27: https://phabricator.wikimedia.org/T113453

  • target_namespaces (list) – the namespace ID of the target title must be in this list in order to be included in the mapping (default is "all", which will select all available namespaces)

Returns

a dictionary where the keys are source titles and values are the redirect targets, including the link fragments (e.g. "Page title#Section title").

property map

A lazily evaluated mapping for all namespaces on the wiki.

resolve(source)

Looks into the map property and checks if given title is a redirect page. Double redirects are resolved repeatedly, if an infinite loop is detected, an error is logged and the page is treated as if it was not a redirect.

Parameters

source (str) – the title to be resolved

Returns

A string of the last non-redirect target page if source is a redirect page, otherwise None.