API:修订版本
本页是MediaWiki Action API帮助文档的一部份。 |
MediaWiki版本: | ≥ 1.8 |
使用「GET請求」以获取页面的修订信息。
API帮助文档
範例1:從多個頁面取得版本資料
GET请求
api.php? action=query& prop=revisions& titles=API|Main%20Page& rvprop=timestamp|user|comment& rvslots=main& formatversion=2 [在Api沙盒中尝试]
上面的請求是用來取得頁面標題為API 和首頁 的修訂版本資料
回應
回應 |
---|
{
"batchcomplete": true,
"query": {
"pages": [
{
"pageid": 1423,
"ns": 0,
"title": "Main Page",
"revisions": [
{
"user": "Bdk",
"timestamp": "2005-09-16T01:14:43Z",
"comment": "Reverted edit of 82.36.210.14, changed back to last version by Brion VIBBER"
}
]
},
{
"pageid": 55332,
"ns": 0,
"title": "API",
"revisions": [
{
"user": "Mainframe98",
"timestamp": "2017-08-19T18:23:42Z",
"comment": "Reverted edits by [[Special:Contributions/Sankaran kumar|Sankaran kumar]] ([[User talk:Sankaran kumar|talk]]) to last revision by [[User:Shirayuki|Shirayuki]]"
}
]
}
]
}
}
|
示例代码
Python
#!/usr/bin/python3
"""
get_pages_revisions.py
MediaWiki API Demos
Demo of `Revisions` module: Get revision data with content for pages
with titles [[API]] and [[Main Page]]
MIT License
"""
import requests
S = requests.Session()
URL = "https://www.mediawiki.org/w/api.php"
PARAMS = {
"action": "query",
"prop": "revisions",
"titles": "API|Main Page",
"rvprop": "timestamp|user|comment|content",
"rvslots": "main",
"formatversion": "2",
"format": "json"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
PAGES = DATA["query"]["pages"]
for page in PAGES:
print(page["revisions"])
PHP
<?php
/*
get_pages_revisions.php
MediaWiki API Demos
Demo of `Revisions` module: Get revision data with content for pages with titles [[API]] and [[Main Page]]
MIT License
*/
$endPoint = "https://www.mediawiki.org/w/api.php";
$params = [
"action" => "query",
"prop" => "revisions",
"titles" => "API|Main Page",
"rvprop" => "timestamp|user|comment|content",
"rvslots" => "main",
"formatversion" => "2",
"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 $k => $v ) {
var_dump( $v["revisions"] );
}
JavaScript
/*
get_pages_revisions.js
MediaWiki API Demos
Demo of `Revisions` module: Get revision data with content for pages with titles [[API]] and [[Main Page]]
MIT License
*/
var url = "https://www.mediawiki.org/w/api.php";
var params = {
action: "query",
prop: "revisions",
titles: "API|Main Page",
rvprop: "timestamp|user|comment|content",
rvslots: "main",
formatversion: "2",
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) {
console.log(pages[p].revisions);
}
})
.catch(function(error){console.log(error);});
MediaWiki JS
/*
get_pages_revisions.js
MediaWiki API Demos
Demo of `Revisions` module: Get revision data with content for pages with
titles [[API]] and [[Main Page]]
MIT License
*/
var params = {
action: 'query',
prop: 'revisions',
titles: 'API|Main Page',
rvprop: 'timestamp|user|comment|content',
rvslots: 'main',
formatversion: '2',
format: 'json'
},
api = new mw.Api();
api.get( params ).done( function ( data ) {
var pages = data.query.pages,
p;
for ( p in pages ) {
console.log( pages[ p ].revisions );
}
} );
範例2:取得頁面最後五個修訂版本並以日期和用戶篩選
GET请求
api.php? action=query& prop=revisions& titles=API:Geosearch& rvlimit=5& rvslots=main& rvprop=timestamp|user|comment& rvdir=newer& rvstart=2018-07-01T00:00:00Z& rvexcludeuser=SSethi_(WMF) [在Api沙盒中尝试]
Request above is to obtain data for the last five revisions of the page API:Geosearch made after the 1st of July 2018, i.e. 2018-07-01 excluding changes made by the user SSethi (WMF)
回應
回應 |
---|
{
"batchcomplete": "",
"query": {
"pages": {
"812323": {
"pageid": 812323,
"ns": 104,
"title": "API:Geosearch",
"revisions": [
{
"user": "Shirayuki",
"timestamp": "2018-11-04T05:25:34Z",
"comment": "translation tweaks"
},
{
"user": "Shirayuki",
"timestamp": "2018-11-25T06:06:50Z",
"comment": "translation tweaks"
}
]
}
}
}
}
|
示例代码
get_filtered_page_revisions.py |
---|
Python#!/usr/bin/python3
"""
get_filtered_page_revisions.py
MediaWiki API Demos
Demo of `Revisions` module: Get data including content of last 5 revisions
of the title [[API:Geosearch]] made after the 1st of July 2018 i.e 2018-07-01
excluding changes made by the user SSethi (WMF)
MIT License
"""
import requests
S = requests.Session()
URL = "https://www.mediawiki.org/w/api.php"
PARAMS = {
"action": "query",
"prop": "revisions",
"titles": "API:Geosearch",
"rvlimit": "5",
"rvprop": "timestamp|user|comment|content",
"rvdir": "newer",
"rvstart": "2018-07-01T00:00:00Z",
"rvexcludeuser": "SSethi (WMF)",
"rvslots": "main",
"formatversion": "2",
"format": "json"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
PAGES = DATA["query"]["pages"]
for page in PAGES:
print(page["revisions"])
PHP<?php
/*
get_filtered_page_revisions.php
MediaWiki API Demos
Demo of `Revisions` module: Get data including content of last 5 revisions of the title [[API:Geosearch]] made after July 1st 2018 excluding changes made by the user SSethi (WMF)
MIT License
*/
$endPoint = "https://www.mediawiki.org/w/api.php";
$params = [
"action" => "query",
"prop" => "revisions",
"titles" => "API:Geosearch",
"rvlimit" => "5",
"rvprop" => "timestamp|user|comment|content",
"rvdir" => "newer",
"rvstart" => "2018-07-01T00:00:00Z",
"rvexcludeuser" => "SSethi (WMF)",
"rvslots" => "main",
"formatversion" => "2",
"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 $k => $v ) {
var_dump( $v["revisions"] );
}
JavaScript/*
get_filtered_page_revisions.js
MediaWiki API Demos
Demo of `Revisions` module: Get data including content of last 5 revisions of the title [[API:Geosearch]] made after July 1st 2018 excluding changes made by the user SSethi (WMF)
MIT License
*/
var url = "https://www.mediawiki.org/w/api.php";
var params = {
action: "query",
prop: "revisions",
titles: "API:Geosearch",
rvlimit: "5",
rvprop: "timestamp|user|comment|content",
rvdir: "newer",
rvstart: "2018-07-01T00:00:00Z",
rvexcludeuser: "SSethi (WMF)",
rvslots: "main",
formatversion: "2",
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) {
console.log(pages[p].revisions);
}
})
.catch(function(error){console.log(error);});
MediaWiki JS/*
get_filtered_page_revisions.js
MediaWiki API Demos
Demo of `Revisions` module: Get data including content of last 5 revisions of the
title [[API:Geosearch]] made after July 1st 2018 excluding changes made by the user SSethi (WMF)
MIT License
*/
var params = {
action: 'query',
prop: 'revisions',
titles: 'API:Geosearch',
rvlimit: '5',
rvprop: 'timestamp|user|comment|content',
rvdir: 'newer',
rvstart: '2018-07-01T00:00:00Z',
rvexcludeuser: 'SSethi (WMF)',
rvslots: 'main',
formatversion: '2',
format: 'json'
},
api = new mw.Api();
api.get( params ).done( function ( data ) {
var pages = data.query.pages,
p;
for ( p in pages ) {
console.log( pages[ p ].revisions );
}
} );
|
Example 3: Get last revision of a page, following any redirects
GET请求
Request above is to obtain revision data of the page AntiSpoof , following any redirects. Since AntiSpoof redirects to 扩展:AntiSpoof , it will actually return revision data for 扩展:AntiSpoof .
回應
回應 |
---|
{
"batchcomplete": true,
"query": {
"redirects": [
{
"from": "AntiSpoof",
"to": "Extension:AntiSpoof"
}
],
"pages": [
{
"pageid": 8993,
"ns": 102,
"title": "Extension:AntiSpoof",
"revisions": [
{
"revid": 3419761,
"parentid": 3053177,
"minor": true,
"user": "Shirayuki",
"timestamp": "2019-09-22T05:14:46Z",
"comment": ""
}
]
}
]
}
}
|
可能的错误
代码 | 信息 |
---|---|
rvdiffto | rvdiffto must be set to "prev", "next", "cur" or a non-negative number. |
rvnosuchrevid | 没有ID为ID的修订版本。 |
rvnosuchsection | There is no section section in rID |
rvrevids | revids参数不能与列表选项(rvlimit、rvstartid、rvendid、rvdir=newer、rvuser、rvexcludeuser、rvstart和rvend)一起使用 |
rvmultpages | titles、pageids或发生器用于提供多个页面,但rvlimit、rvstartid、rvendid、rvdir=newer、rvuser、rvexcludeuser、rvstart和rvend参数只能在一个页面上使用。 |
rvaccessdenied | The current user is not allowed to read title |
rvbadparams | start and startid cannot be used together |
rvbadparams | end and endid cannot be used together |
rvbadparams | user and excludeuser cannot be used together |
invalidparammix | titles、pageids或发生器用于提供多个页面,但rvlimit、rvstartid、rvendid、rvdir=newer、rvuser、rvexcludeuser、rvstart和rvend参数只能在一个页面上使用。 |
accessdenied | 您不被允许查看title。 |
badid_startid | 未找到参数startid的修订版本。 |
badid_endid | 未找到参数endid的修订版本。 |
附加提醒
- Pages are specified either by
pageids
ortitles
parameter.
Individual revisions are specified by revids
parameter. 參見API:查詢 。
- Slots can be specified with the
rvslots
parameter.
When the parameter is not present, the API will only return information about the main slot.
- When using parameters marked as (enum),
titles=
must have only one title listed.
- This module couldn't be used as a generator prior to version 1.25.
- Diff functionality of this module was deprecated in version 1.30 and got moved to API:Compare .
- The code for this query is located on the git repository at ApiQueryRevisions.php.
参数历史
- v1.32: 啟用
rvslots
,roles
棄用rvcontentformat
- v1.30: 棄用
parsetree
,rvprop
,rvexpandtemplates
,rvparse
,rvdiffto
,rvdifftotext
,rvdifftotextpst
- v1.27: 啟用
rvdifftotextpst
- v1.26: 啟用
parsetree
棄用rvgeneratexml
- v1.24: 棄用
rvtoken
- v1.21: 啟用
contentmodel
,rvcontentformat
- v1.19: 啟用
sha1
,rvparse
- v1.17: 啟用
userid
,rvparse
- v1.16: 啟用
parsedcomment
,tags
,rvdifftotext
,rvtag
- v1.15: 啟用
rvdiffto
,rvcontinue
- v1.14: 啟用
rvgeneratexml
- v1.13: 啟用
rvsection
- v1.12: 啟用
rvexpandtemplates
,rvtoken
- v1.11: 啟用
ids
,flags
,size
,rvuser
,rvexcludeuser