API:Rollback/zh
Appearance
本页是MediaWiki Action API帮助文档的一部份。 |
MediaWiki版本: | ≥ 1.12 |
POST request to revert the last series of edits made by one user to a given page.
此功能与历史界面中提供的rollback链接效果是一样的。
API帮助文档
示例
发出任何POST请求都是一个多步骤的过程:
- 使用API:登录 中描述的方法之一登录。
- 获取CSRF令牌 。
- Send a POST request, with the CSRF token, to take action on a page and in this process gain
editpage
rights to the given page: - GET a rollback token:
- If your wiki is using MediaWiki 1.24+, follow these instructions:
- Rollback tokens aren't universal: they are not only different for every login, but also depend on the title of the page and the name of the user whose edits are to be rolled back. 对于MediaWiki 1.23及之前的版本,可以通过如下的步骤:
POST请求
Send a POST request, with a rollback token to revert a user's edits to the Sandbox page.
在本例中,所有的参数通过GET请求传递只是为了便于理解。实际上, action=rollback 需要以POST方式请求,以GET方式请求将报错。
api.php? action=rollback& format=json& title=Sandbox& user=41.190.3.231& token=d36a517a732991d5aaafae8bd40eee4a5c7bf75a+\\ [在Api沙盒中尝试]
回应
The Response section below is for the final POST request, to revert the edits to the Sandbox page.
{
"rollback": {
"title": "Sandbox",
"pageid": 94542,
"summary": "Reverted edits by [[Special:Contributions/41.190.3.231|41.190.3.231]] ([[User talk:41.190.3.231|talk]]) to last revision by [[User:Didicodes|Didicodes]]",
"revid": 381084,
"old_revid": 381083,
"last_revid": 381070
}
}
回应中的修订ID为:
revid
- 回退创建的新修订版的ID。old_revid
- 正在回退的最新修订版的ID。回退之前,这是当前版本。last_revid
- 回退恢复的修订版本的ID。这是已回退的最新修订版本。
示例代码
Python
#!/usr/bin/python3
"""
rollback.py
MediaWiki API Demos
Demo of `rollback` module: Sending post request to rollback the
last edits made to a given page.
MIT license
"""
import requests
S = requests.Session()
URL = "https://test.wikipedia.org/w/api.php"
# Step 1: Retrieve a login token
PARAMS_1 = {
"action": "query",
"meta": "tokens",
"type": "login",
"format": "json"
}
R = S.get(url=URL, params=PARAMS_1)
DATA = R.json()
LOGIN_TOKEN = DATA['query']['tokens']['logintoken']
# Step 2: Send a post request to login. Use of main account for login is not
# supported. Obtain credentials via Special:BotPasswords
# (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
PARAMS_2 = {
"action": "login",
"lgname": "bot_user_name",
"lgpassword": "bot_password",
"lgtoken": LOGIN_TOKEN,
"format": "json"
}
R = S.post(URL, data=PARAMS_2)
# Step 3: While logged in, retrieve a CSRF token
PARAMS_3 = {
"action": "query",
"meta": "tokens",
"format": "json"
}
R = S.get(url=URL, params=PARAMS_3)
DATA = R.json()
CSRF_TOKEN = DATA["query"]["tokens"]["csrftoken"]
# Step 4: POST request to edit a page
PARAMS_4 = {
"action": "edit",
"title": "Project:Sandbox",
"token": CSRF_TOKEN,
"format": "json",
"appendtext": "Hello"
}
R = S.post(URL, data=PARAMS_4)
# Step 5: Retrieve a rollback token
PARAMS_5 = {
"action": "query",
"meta": "tokens",
"type": "rollback",
"format": "json"
}
R = S.get(url=URL, params=PARAMS_5)
DATA = R.json()
ROLLBACK_TOKEN = DATA['query']['tokens']['rollbacktoken']
# Step 5: POST request to rollback a page
PARAMS_6 = {
"action": "rollback",
"format": "json",
"title": "Project:Sandbox",
"user": "bot_user_name",
"token": ROLLBACK_TOKEN,
}
R = S.post(URL, data=PARAMS_6)
DATA = R.json()
print(DATA)
PHP
<?php
/*
rollback.php
MediaWiki API Demos
Demo of `rollback` module: Sending post request to rollback the
edits of a given page.
MIT license
*/
$endPoint = "http://dev.wiki.local.wmftest.net:8080/w/api.php";
$login_Token = getLoginToken(); // Step 1
loginRequest( $login_Token ); // Step 2
$rollback_Token = getRollbackToken(); // Step 3
rollback( $rollback_Token ); // Step 4
// Step 1: GET request to fetch login token
function getLoginToken() {
global $endPoint;
$params1 = [
"action" => "query",
"meta" => "tokens",
"type" => "login",
"format" => "json"
];
$url = $endPoint . "?" . http_build_query( $params1 );
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
$output = curl_exec( $ch );
curl_close( $ch );
$result = json_decode( $output, true );
return $result["query"]["tokens"]["logintoken"];
}
// Step 2: POST request to log in. Use of main account for login is not
// supported. Obtain credentials via Special:BotPasswords
// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
function loginRequest( $logintoken ) {
global $endPoint;
$params2 = [
"action" => "login",
"lgname" => "bot_user_name",
"lgpassword" => "bot_password",
"lgtoken" => $logintoken,
"format" => "json"
];
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $endPoint );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params2 ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
$output = curl_exec( $ch );
curl_close( $ch );
}
// Step 3: GET request to fetch rollback token
function getRollbackToken() {
global $endPoint;
$params3 = [
"action" => "query",
"meta" => "tokens",
"type" => "rollback",
"format" => "json"
];
$url = $endPoint . "?" . http_build_query( $params3 );
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
$output = curl_exec( $ch );
curl_close( $ch );
$result = json_decode( $output, true );
return $result["query"]["tokens"]["rollbacktoken"];
}
// Step 4: POST request to rollback a page
function rollback( $rollback_Token ) {
global $endPoint;
$params4 = [
"action" => "rollback",
"title" => "Project:Sandbox",
"user" => "10.0.2.2",
"token" => $rollback_Token,
"format" => "json"
];
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $endPoint );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query( $params4 ) );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
$output = curl_exec( $ch );
curl_close( $ch );
echo ( $output );
}
JavaScript
/*
rollback.js
MediaWiki API Demos
Demo of `rollback` module: Sending post request to rollback the
edits of a given page.
MIT license
*/
var request = require('request').defaults({jar: true}),
url = "http://dev.wiki.local.wmftest.net:8080/w/api.php";
// Step 1: GET request to fetch login token
function getLoginToken() {
var params_0 = {
action: "query",
meta: "tokens",
type: "login",
format: "json"
};
request.get({ url: url, qs: params_0 }, function (error, res, body) {
if (error) {
return;
}
var data = JSON.parse(body);
loginRequest(data.query.tokens.logintoken);
});
}
// Step 2: POST request to log in.
// Use of main account for login is not
// supported. Obtain credentials via Special:BotPasswords
// (https://www.mediawiki.org/wiki/Special:BotPasswords) for lgname & lgpassword
function loginRequest(login_token) {
var params_1 = {
action: "login",
lgname: "bot_username",
lgpassword: "bot_password",
lgtoken: login_token,
format: "json"
};
request.post({ url: url, form: params_1 }, function (error, res, body) {
if (error) {
return;
}
getRollbackToken();
});
}
// Step 3: GET request to fetch Rollback token
function getRollbackToken() {
var params_2 = {
action: "query",
meta: "tokens",
type: "rollback",
format: "json"
};
request.get({ url: url, qs: params_2 }, function(error, res, body) {
if (error) {
return;
}
var data = JSON.parse(body);
rollback(data.query.tokens.rollbacktoken);
});
}
// Step 4: POST request to rollback a page
function rollback(rollback_token) {
var params_3 = {
action: "rollback",
title: "Project:Sandbox",
user: "10.0.2.2",
token: rollback_token,
format: "json"
};
request.post({ url: url, form: params_3 }, function (error, res, body) {
if (error) {
return;
}
console.log(body);
});
}
// Start From Step 1
getLoginToken();
MediaWiki JS
/*
rollback.js
MediaWiki API Demos
Demo of `rollback` module: Sending post request to rollback the
last edits made to a given page.
MIT License
*/
var params = {
action: 'rollback',
title: 'Sandbox',
user: '10.0.2.2',
format: 'json'
},
api = new mw.Api();
api.postWithToken( 'rollback', params ).done( function ( data ) {
console.log( data );
} );
权限
Rollback requires both the edit
and the rollback
right on the target page; currently, this means that OAuth consumers, bot passwords etc. require both the editpage
and the rollback
grant – rollback
alone is not enough!
可能的错误
In addition to the usual stuff:
代码 | 信息 |
---|---|
alreadyrolled | 您尝试回退的页面已近被回退 |
missingparams | One of the parameters title and pageid is required.
|
mustpostparams | The rollback module requires a POST request. |
notitle | title参数必须被设置。 |
notoken | token参数必须被设置。 |
nouser | user参数必须被设置。 |
onlyauthor | 您尝试回退的页面只有一个编辑者 |
permissiondenied | The action you have requested is limited to users in one of the groups: Administrators, Rollbackers |
参数历史
- v1.24: 启用
pageid
- v1.27: 启用
tags
- v1.17: 启用
watchlist
补充资料
- 可选的是,回退与与被回退的编辑均可被标记为机器人操作,可在Special:RecentChanges中默认不显示。
參見
- API:编辑 - 编辑页面
- Help:编辑 - 包含对编辑页面有用的链接
- API:令牌 - Has more details on using tokens to log in or make POST requests.
- API:令牌(操作) - A deprecated API, distinct from API:令牌 , for requesting tokens in earlier versions of MediaWiki.
- API:Filerevert - Rolls back files to an earlier state.
- API:修订版本删除 - Deletes and restores revisions to a page.
- API:Compare - Allows you to diff between edits on a page.