API:巡查
本页是MediaWiki Action API帮助文档的一部份。 |
MediaWiki版本: | ≥ 1.14 |
POST request to patrol a page or a revision.
API帮助文档
示例
巡视请求都是一个多步骤的过程:
- 使用API:登录 中描述的方法之一登录。
- 获取巡查令牌 。 所有页面的令牌是相同的,但是每次登录时令牌都会改变。
- Send a POST request, with the patrol token, to patrol a request.
下面的示例代码详细介绍了最后一步。
POST请求
巡查
rcid
437659的最近更改。
回应
{
"patrol": {
"rcid": 437659,
"ns": 0,
"title": "Sandbox"
}
}
示例代码
Python
#!/usr/bin/python3
"""
patrol.py
MediaWiki API Demos
Demo of `Patrol` module: Patrol a recent change
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 log in. For this login
# method, obtain bot credentials by first visiting
# https://www.test.wikipedia.org/wiki/Manual:Bot_passwords
# See https://www.mediawiki.org/wiki/API:Login for more
# information on log in methods.
PARAMS_2 = {
"action": "login",
"lgname": "username",
"lgpassword": "password",
"format": "json",
"lgtoken": LOGIN_TOKEN
}
R = S.post(URL, data=PARAMS_2)
DATA = R.json()
# Step 3: While logged in, retrieve a patrol token
PARAMS_3 = {
"action": "query",
"meta": "tokens",
"type":"patrol",
"format": "json"
}
R = S.get(url=URL, params=PARAMS_3)
DATA = R.json()
PATROL_TOKEN = DATA["query"]["tokens"]["patroltoken"]
# Step 4: Send a POST request to patrol a recent change
PARAMS_4 = {
"action": "patrol",
"format": "json",
"rcid":"437659",
"token": PATROL_TOKEN
}
R = S.post(url=URL, data=PARAMS_4)
DATA = R.json()
print(DATA)
PHP
<?php
/*
patrol.php
MediaWiki API Demos
Demo of `Patrol` module: Patrol a recent change
MIT license
*/
$endPoint = "https://test.wikipedia.org/w/api.php";
$login_Token = getLoginToken(); // Step 1
loginRequest( $login_Token ); // Step 2
$patrol_Token = getPatrolToken(); // Step 3
patrol( $patrol_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 Patrol token
function getPatrolToken() {
global $endPoint;
$params3 = [
"action" => "query",
"meta" => "tokens",
"type" => "patrol",
"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"]["patroltoken"];
}
// Step 4: POST request to patrol a recent change
function patrol( $patroltoken ) {
global $endPoint;
$params4 = [
"action" => "patrol",
"rcid" => "91",
"token" => $patroltoken,
"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
/*
patrol.js
MediaWiki API Demos
Demo of `Patrol` module: Patrol a recent change
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;
}
getPatrolToken();
});
}
// Step 3: GET request to fetch Patrol token
function getPatrolToken() {
var params_2 = {
action: "query",
meta: "tokens",
type: "patrol",
format: "json"
};
request.get({ url: url, qs: params_2 }, function(error, res, body) {
if (error) {
return;
}
var data = JSON.parse(body);
patrol(data.query.tokens.patroltoken);
});
}
// Step 4: POST request to patrol a recent change
function patrol(patrol_token) {
var params_3 = {
action: "patrol",
rcid: "104",
token: patrol_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
/*
patrol.js
MediaWiki API Demos
Demo of `Patrol` module: Patrol a recent change
MIT License
*/
var params = {
action: 'patrol',
revid: '77',
format: 'json'
},
api = new mw.Api();
api.postWithToken( 'patrol', params ).done( function ( data ) {
console.log( data );
} );
可能的错误
In addition to the standard error messages :
代码 | 信息 |
---|---|
patroldisabled | 在该维基上巡查未启用 |
noautopatrol | 您无法将您自己的更改标记为已巡查。 只有拥有
autopatrol 权限的用户才能这样做 |
notpatrollable | 修订版本rid不能巡查,因为它太旧了。 |
nosuchrcid | 没有ID为rcid的最近更改。 |
nosuchrevid | 没有ID为revid的修订版本。 |
参数历史
- v1.27: 启用
tags
- v1.22: 启用
revid
附加提醒
- 对于早于1.17的MediaWiki版本,巡查令牌与编辑令牌相同。
- 使用此模块要求
autopatrol
权限。 revid
与rcid
可通过API:Recentchanges 获取。