MediaWiki version:
1.9

GET request to list pages which link to a certain page.

API documentation


Special:ApiHelp/query+backlinks

Example

GET request

List backlinks to the philosophy page.

Response

{
    "batchcomplete": "",
    "continue": {
        "blcontinue": "1|987",
        "continue": "-||"
    },
    "query": {
        "backlinks": [
            {
                "pageid": 12,
                "ns": 0,
                "title": "Anarchism"
            },
            {
                "pageid": 128,
                "ns": 1,
                "title": "Talk:Atlas Shrugged"
            },
            {
                "pageid": 336,
                "ns": 0,
                "title": "Altruism"
            },
            ...
        ]
    }
}

Sample code

Python

#!/usr/bin/python3

"""
    get_backlinks.py

    MediaWiki API Demos
    Demo of `Backlinks` module: Get request to list pages which link to a certain page.

    MIT License
"""

import requests

S = requests.Session()

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

PARAMS = {
    "action": "query",
    "format": "json",
    "list": "backlinks",
    "bltitle": "philosophy"
}

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

BACKLINKS = DATA["query"]["backlinks"]

for b in BACKLINKS:
    print(b["title"])

PHP

<?php
/*
    get_backlinks.php

    MediaWiki API Demos
    Demo of `Backlinks` module: Get request to list pages which link to a certain page.

    MIT License
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "format" => "json",
    "list" => "backlinks",
    "bltitle" => "philosophy"
];

$url = $endPoint . "?" . http_build_query( $params );

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );

$result = json_decode( $output, true );

foreach( $result["query"]["backlinks"] as $k => $v ) {
    echo( $v["title"] . "\n" );
}

JavaScript

/*
    get_backlinks.js

    MediaWiki API Demos
    Demo of `Backlinks` module: Get request to list pages which link to a certain page.

    MIT License
*/

var url = "https://en.wikipedia.org/w/api.php"; 

var params = {
    action: "query",
    format: "json",
    list: "backlinks",
    bltitle: "philosophy"
};

url = url + "?origin=*";
Object.keys(params).forEach(function(key){url += "&" + key + "=" + params[key];});

fetch(url)
    .then(function(response){return response.json();})
    .then(function(response) {
        var backlinks = response.query.backlinks;
        for (var b in backlinks) {
            console.log(backlinks[b].title);
        }
    })
    .catch(function(error){console.log(error);});

MediaWiki JS

/*
	get_backlinks.js

	MediaWiki API Demos
	Demo of `Backlinks` module: Get request to list pages which link to a certain page.

	MIT License
*/

var params = {
		action: 'query',
		format: 'json',
		list: 'backlinks',
		bltitle: 'philosophy'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	var backlinks = data.query.backlinks,
		b;
	for ( b in backlinks ) {
		console.log( backlinks[ b ].title );
	}
} );

Redirects

In the example above, only direct links to the philosophy page are returned. When blredirect is set, the response will include any pages which backlink to redirects for the value in bltitle.

These redirected backlinks are treated as separate groups within the response hierarchy, one level down from the redirect itself.

The limit set in bllimit applies separately to each level of the response, so bllimit=25 would return up to 25 direct backlinks, and up to 25 backlinks within each individual redirect.

In addition, using blcontinue when a redirect was in the response will return more second-level backlinks, before finally moving on to more direct backlinks, once all the backlinks for a redirect have been returned in full.

Possible errors

Code Info
blbadcontinueInvalid continue param. You should pass the original value returned by the previous query.

See also

  • API:Linkshere - finds all pages that link to a given page. Note that, unlike API:Backlinks, which is a list module, API:Linkshere is a prop module. See the respective pages on API:Properties and API:Lists for how these two kinds of modules differ.
  • API:Transcludedin - a prop module that finds all pages that transclude (i.e. embed information from) the given pages.
  • API:Embeddedin - a list module which lists backlinks via transclusion, similar to Special:Whatlinkshere.
  • API:Imageusage - lists pages that use the given image(s).
  • API:Fileusage - lists pages that use the given file(s).
This article is issued from Mediawiki. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.