API:Allredirects
Appearance
| 本页是MediaWiki Action API帮助文档的一部分。 |
| MediaWiki版本: | ≥ 1.23 |
使用GET请求以列出所有重定向。
此API所提供的全部篩選器都會影響重定向的目標,但不會影響重定向的來源。
该模块可作為生成器使用。
當用作生成器時,生成器所使用的標題是來自重定向的來源而不是重定向的目標,除非使用 garunique 參數。
API帮助文档
arfrom、arto、和arprefix輸入參數依目標重定向的無命名空間的標題進行篩選。
不得為這些參數新增命名空間,否則 API 將傳回 invalidtitle 錯誤。
這意味著這些參數實際上會過濾主命名空間中的標題,除非透過 arnamespace 參數選擇不同的命名空間,這將影響該命名空間中的標題。
示例
GET请求
取得包含重定向到主命名空間的最初三個唯一頁面。
回应
傳回的標題和命名空間都指向重定向目標,而不是重定向本身。
{
"batchcomplete": "",
"continue": {
"arcontinue": "!Women_Art_Revolution",
"continue": "-||"
},
"query": {
"allredirects": [
{
"ns": 0,
"title": "!Action Pact!"
},
{
"ns": 0,
"title": "!Arriba! La Pachanga"
},
{
"ns": 0,
"title": "!Hero"
}
]
}
}
示例代码
Python
#!/usr/bin/python3
"""
get_allredirects.py
MediaWiki API Demos
Demo of `Allredirects` module: Get the first three unique pages containing
redirects to the main namespace.
MIT License
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
"action": "query",
"format": "json",
"list": "allredirects",
"arunique": "1",
"arnamespace": "0",
"arlimit": "3"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
REDIRECTS = DATA["query"]["allredirects"]
for r in REDIRECTS:
print(r["title"])
PHP
<?php
/*
get_allredirects.php
MediaWiki API Demos
Demo of `Allredirects` module: Get the first three unique pages containing redirects to the main namespace.
MIT License
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
"action" => "query",
"format" => "json",
"list" => "allredirects",
"arunique" => "1",
"arnamespace" => "0",
"arlimit" => "3"
];
$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"]["allredirects"] as $k => $v ) {
echo( $v["title"] . "\n" );
}
JavaScript
/*
get_allredirects.js
MediaWiki API Demos
Demo of `Allredirects` module: Get the first three unique pages containing redirects to the main namespace.
MIT License
*/
var url = "https://en.wikipedia.org/w/api.php";
var params = {
action: "query",
format: "json",
list: "allredirects",
arunique: "1",
arnamespace: "0",
arlimit: "3"
};
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 redirects = response.query.allredirects;
for (var r in redirects) {
console.log(redirects[r].title);
}
})
.catch(function(error){console.log(error);});
MediaWiki JS
/*
get_allredirects.js
MediaWiki API Demos
Demo of `Allredirects` module: Get the first three unique
pages containing redirects to the main namespace.
MIT License
*/
var params = {
action: 'query',
format: 'json',
list: 'allredirects',
arunique: '1',
arnamespace: '0',
arlimit: '3'
},
api = new mw.Api();
api.get( params ).done( function ( data ) {
var redirects = data.query.allredirects,
r;
for ( r in redirects ) {
console.log( redirects[ r ].title );
}
} );
其他说明
- 命名空間及其編號可以在Manual:命名空间獲得。
参阅
- API:重定向 – 針對指定頁面列出所有的重定向。