User:Anadolocan/Sandbox/API:backlinks

MediaWiki version:
1.9

GET Request to list pages that link to a given page, similar to Special:Whatlinkshere.

API documentation

edit
(main | query | backlinks)
  • This module requires read rights.
  • This module can be used as a generator.
  • Source: MediaWiki
  • License: GPL-2.0-or-later

Find all pages that link to the given page.

Specific parameters:
Other general parameters are available.
bltitle

Title to search. Cannot be used together with blpageid.

blpageid

Page ID to search. Cannot be used together with bltitle.

Type: integer
blcontinue

When more results are available, use this to continue. More detailed information on how to continue queries can be found on mediawiki.org.

blnamespace

The namespace to enumerate.

Values (separate with | or alternative): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 90, 91, 92, 93, 100, 101, 102, 103, 104, 105, 106, 107, 486, 487, 710, 711, 828, 829, 1198, 1199, 2600, 5500, 5501
To specify all values, use *.
bldir

The direction in which to list.

One of the following values: ascending, descending
Default: ascending
blfilterredir

How to filter for redirects. If set to nonredirects when blredirect is enabled, this is only applied to the second level.

One of the following values: all, nonredirects, redirects
Default: all
bllimit

How many total pages to return. If blredirect is enabled, the limit applies to each level separately (which means up to 2 * bllimit results may be returned).

Type: integer or max
The value must be between 1 and 500.
Default: 10
blredirect

If linking page is a redirect, find all pages that link to that redirect as well. Maximum limit is halved.

Type: boolean (details)


Examples

edit
edit

GET Request

edit

Find all redirect pages that link or redirect to the en:Main Page

Response

edit
{
    "batchcomplete": "",
    "continue": {
        "blcontinue": "4|2124921",
        "continue": "-||"
    },
    "query": {
        "backlinks": [
            {
                "pageid": 217224,
                "ns": 0,
                "title": "Mainpage",
                "redirect": ""
            },
            {
                "pageid": 217225,
                "ns": 0,
                "title": "Main page",
                "redirect": ""
            },
            ...
        ]
    }
}

Sample code

edit

backlinks.py

#!/usr/bin/python3

"""
	backlinks.py

	MediaWiki Action API Code Samples
	Demo of `Backlinks` module: Find all redirect pages that link or redirect to a page
	MIT license
"""

import requests

S = requests.Session()

URL = "https://en.wikipedia.org/w/api.php"

TITLE = "Main Page"

PARAMS = {
	'action': "query",
	'list': "backlinks",
	'bltitle': TITLE,
	'bllimit': 5,
	'blfilterredir': "redirects",
	'format': "json"
}

R = S.get(url=URL, params=PARAMS)
DATA = R.json()

print(DATA)
edit
MediaWiki version:
1.12

When the blredirect parameter is set, this module behaves slightly differently. bllimit applies to both levels separately: if e.g. bllimit=10, at most 10 first-level pages (pages that link to bltitle) and 10 second-level pages (pages that link to bltitle through a redirect) will be listed. Continuing queries also works differently, as displayed in the following example.

GET Request

edit

Get a list of pages linking to Albert Einstein (note: the initial query includes a "blcontinue" parameter for purposes of illustration)

Response

edit
Response
{
    "batchcomplete":"",
    "continue":{
        "blcontinue":"0|8756|0|Einstein|0|12511",
        "continue":"-||"
    },
    "query":{
        "backlinks":[
            {
                "pageid":8756,
                "ns":0,
                "title":"Daniel Dennett"
            },
            {
                "pageid":9247,
                "ns":0,
                "title":"Epistemology"
            },
            {
                "pageid":9251,
                "ns":0,
                "title":"Engineering"
            },
            {
                "pageid":9292,
                "ns":0,
                "title":"Einstein",
                "redirect":"",
                "redirlinks":[
                    {
                        "pageid":736,
                        "ns":0,
                        "title":"Albert Einstein"
                    },
                    {
                        "pageid":2792,
                        "ns":0,
                        "title":"Anthropic principle"
                    },
                    ...
                ]
            },
            ...
        ]
    }
}

Sample code

edit

links_through_redirect.py

Sample code
#!/usr/bin/python3

"""
	links_through_redirect.py

	MediaWiki Action API Code Samples
	Demo of `Backlinks` module: Get a list of pages linking to Albert Einstein
	MIT license
"""

import requests

S = requests.Session()

URL = "https://en.wikipedia.org/w/api.php"

TITLE = "Albert_Einstein"

PARAMS = {
	'action': "query",
	'list': "backlinks",
	'bltitle': TITLE,
	'blredirect': "",
	'blcontinue': "0|8756",
	'format': "json"
}

R = S.get(url=URL, params=PARAMS)
DATA = R.json()

print(DATA)

Continuing the previous request

edit

When continuing this request, we see that there are more pages linking to Einstein, and that some of the other first-level pages are listed again. If we continue this query again, the same first-level pages will be listed in the next response, as well. To progress beyond Energy, we have to query-continue again and again until we've had all links to Einstein (or increase bllimit, of course).

In order to query-continue, we need to get the new intervals from the previous query response (i.e. "blcontinue":"0|8756|0|Einstein|0|12511" translates into a new GET request having the blcontinue field set to 8756|12511).

Possible errors

edit
Code Info
blbadcontinue Invalid continue param. You should pass the original value returned by the previous query.

Additional notes

edit
  • Links through transclusions

If you need to list pages as they appear in "What links here" on the MediaWiki sites with pages transcluding the desired page, you might want to try API:Embeddedin list.

  • Links through image usage

If you need to list pages as they appear in "What links here" on the MediaWiki sites with pages displaying the desired file, you might want to try API:Imageusage list. This also works on non-existent files.

See also

edit

https://www.mediawiki.org/wiki/Help:What_links_here

https://www.mediawiki.org/wiki/Special:MyLanguage/Special:MyLanguage/API:Query_-_Lists

https://www.mediawiki.org/wiki/Manual:SpecialWhatlinkshere.php