API:Redirects

From mediawiki.org
MediaWiki version:
1.24

GET request to return all redirects to the given page(s).

API documentation[edit]


prop=redirects (rd)

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

Returns all redirects to the given pages.

Specific parameters:
Other general parameters are available.
rdprop

Which properties to get:

pageid
Page ID of each redirect.
title
Title of each redirect.
fragment
Fragment of each redirect, if any.
Values (separate with | or alternative): fragment, pageid, title
Default: pageid|title
rdnamespace

Only include pages in these namespaces.

Note: Due to miser mode, using this may result in fewer than rdlimit results returned before continuing; in extreme cases, zero results may be returned.

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 *.
rdshow

Show only items that meet these criteria:

fragment
Only show redirects with a fragment.
!fragment
Only show redirects without a fragment.
Values (separate with | or alternative): !fragment, fragment
rdlimit

How many redirects to return.

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

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

Example[edit]

GET request[edit]


Response[edit]

{
    "batchcomplete": "",
    "query": {
        "pages": {
            "571857": {
                "pageid": 571857,
                "ns": 0,
                "title": "Jacques Kallis",
                "redirects": [
                    {
                        "pageid": 4388536,
                        "ns": 0,
                        "title": "JH Kallis"
                    },
                    {
                        "pageid": 10977683,
                        "ns": 0,
                        "title": "Kallis"
                    },
                    {
                        "pageid": 16779878,
                        "ns": 0,
                        "title": "Jack kallis"
                    }
                    ...
                ]
            }
        }
    }
}

Sample code[edit]

Python[edit]

#!/usr/bin/python3

"""
    get_redirects.py

    MediaWiki API Demos
    Demo of `Redirects` module: Get all redirects to the given page(s)

    MIT License
"""

import requests

S = requests.Session()

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

PARAMS = {
    "action": "query",
    "format": "json",
    "titles": "Jacques Kallis",
    "prop": "redirects"
}

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

PAGES = DATA["query"]["pages"]

for k, v in PAGES.items():
    for re in v["redirects"]:
        print(re["title"] + " redirect to " + v["title"])

PHP[edit]

<?php
/*
    get_redirects.php

    MediaWiki API Demos
    Demo of `Redirects` module: Get all redirects to the given page(s)

    MIT License
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "titles" => "Jacques Kallis",
    "prop" => "redirects",
    "format" => "json"
];

$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"]["pages"] as $page ) {
    foreach( $page["redirects"] as $k => $v ) {
        echo( $v["title"] . " redirect to " . $page["title"] . "\n");
    }
}

JavaScript[edit]

/*
    get_redirects.js

    MediaWiki API Demos
    Demo of `Redirects` module: Get all redirects to the given page(s)

    MIT License
*/

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

var params = {
    action: "query",
    titles: "Jacques Kallis",
    prop: "redirects",
    format: "json"
};

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 pages = response.query.pages;
        for (var p in pages) {
            for (var re of pages[p].redirects) {
                console.log(re.title + " redirect to " + pages[p].title );
            }
        }
    })
    .catch(function(error){console.log(error);});

MediaWiki JS[edit]

/*
	get_redirects.js

	MediaWiki API Demos
	Demo of `Redirects` module: Get all redirects to the given page(s)

	MIT License
*/

var params = {
		action: 'query',
		titles: 'Jacques Kallis',
		prop: 'redirects',
		format: 'json'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	var pages = data.query.pages,
		p;

	function result( p ) {
		pages[ p ].redirects.forEach( function ( re ) {
			console.log( re.title + ' redirect to ' + pages[ p ].title );
		} );
	}
	for ( p in pages ) {
		result( p );
	}
} );

Possible errors[edit]

Code Info
show Incorrect parameter - mutually exclusive values may not be supplied.

Additional notes[edit]

  • This module can be used as a generator .
  • Double redirects are not returned (e.g., a redirect page that links to another redirect page).

See also[edit]