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
list=backlinks (bl)
Find all pages that link to the given page. Specific parameters: Other general parameters are available.
Examples:
|
Examples
editExample 1: Show links to Main page.
editGET Request
editFind 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
editbacklinks.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)
Example 2: Links through redirects.
editMediaWiki 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
editGet a list of pages linking to Albert Einstein (note: the initial query includes a "blcontinue" parameter for purposes of illustration)
Response
editResponse |
---|
{
"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
editlinks_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
editWhen 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
editCode | 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
edithttps://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