API:Redirects
Appearance
This page is part of the MediaWiki Action API documentation. |
MediaWiki version: | ≥ 1.24 |
GET request to return all redirects to the given page(s).
API documentation
[edit]
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).