| This page is part of theMediaWiki Action API documentation. |
| MediaWiki Action API |
|---|
| Basics |
| Authentication |
| Accounts and Users |
| Page Operations |
|
| Search |
| Developer Utilities |
| Tutorials |
| v · d · e |
| MediaWiki version: | ≥ 1.16 |
POST request to add or remove users from a group, thereby granting or removing certain user rights.
| The following documentation is the output ofSpecial: |
Change a user's group membership.
User.
Specifyuser=#ID instead.
Add the user to these groups, or if they are already a member, update the expiry of their membership in that group.
Expiry timestamps. May be relative (e.g.5 months or2 weeks) or absolute (e.g.2014-09-18T12:34:56Z). If only one timestamp is set, it will be used for all groups passed to theadd parameter. Useinfinite,indefinite,infinity, ornever for a never-expiring user group.
Remove the user from these groups.
Reason for the change.
A "userrights" token retrieved fromaction=query&meta=tokens
For compatibility, the token used in the web UI is also accepted.
Change tags to apply to the entry in the user rights log.
Watch the user's user and talk pages.
Watchlist expiry timestamp. Omit this parameter entirely to leave the current expiry unchanged.
To use this API, you first need to log in to verify your own user group membership.Only certain groups are granted the ability to alter user rights via this API.SeeAPI:Login for more details on logging in.
The query above applies to MediaWiki v1.24+; in older versions, theuserrights token would depend on the name of the user whose rights were being changed.The query would be made like so:
For compatibility reasons, the API will also accept the token used in the web UI.
Whichever method you choose, once you have your token, you can use it to make youruserrights request, as seen below.
{"userrights":{"user":"Bob","userid":2793024,"removed":["bureaucrat"],"added":["sysop"]}}
#!/usr/bin/python3""" userrights.py MediaWiki API Demos Demo of `Userrights` module: Add and remove user rights by changing the user's group membership. MIT license"""importrequestsS=requests.Session()URL="https://test.wikipedia.org/w/api.php"# Step 1: Retrieve a login tokenPARAMS_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.# See https://www.mediawiki.org/wiki/Manual:Bot_passwords for a special note on logging in using a simplified interface when accessing wikis via an application, rather than the GUIPARAMS_2={"action":"login","lgname":"username","lgpassword":"password","lgtoken":LOGIN_TOKEN,"format":"json"}R=S.post(URL,data=PARAMS_2)# Step 3: Obtain a Userrights tokenPARAMS_3={"action":"query","format":"json","meta":"tokens","type":"userrights"}R=S.get(url=URL,params=PARAMS_3)DATA=R.json()USERRIGHTS_TOKEN=DATA["query"]["tokens"]["userrightstoken"]# Step 4: Request to add or remove a user from a groupPARAMS_4={"action":"userrights","format":"json","user":"Bob","add":"sysop","remove":"bureaucrat","reason":"OOPS! added Bob to the wrong group","token":USERRIGHTS_TOKEN}R=S.post(URL,data=PARAMS_4)DATA=R.json()print(DATA)
<?php/* userrights.php MediaWiki API Demos Demo of `Userrights` module: Add and remove user rights by changing the user's group membership. MIT license*/$endPoint="http://dev.wiki.local.wmftest.net:8080/w/api.php";$login_Token=getLoginToken();// Step 1loginRequest($login_Token);// Step 2$userrights_Token=getUserRightsToken();// Step 3change_userrights($userrights_Token);// Step 4// Step 1: GET request to fetch login tokenfunctiongetLoginToken(){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 & lgpasswordfunctionloginRequest($logintoken){global$endPoint;$params2=["action"=>"clientlogin","username"=>"username","password"=>"password",'loginreturnurl'=>'http://127.0.0.1:5000/',"logintoken"=>$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 userrights tokenfunctiongetUserRightsToken(){global$endPoint;$params3=["action"=>"query","meta"=>"tokens","type"=>"userrights","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"]["userrightstoken"];}// Step 4: POST request to add or remove a user from a groupfunctionchange_userrights($userrightstoken){global$endPoint;$params4=["action"=>"userrights","user"=>"ABCDEF","add"=>"bot","expiry"=>"infinite","reason"=>"API Testing","token"=>$userrightstoken,"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);}
/* userrights.js MediaWiki API Demos Demo of `Userrights` module: Add and remove user rights by changing the user's group membership. MIT license*/varrequest=require('request').defaults({jar:true}),url="http://dev.wiki.local.wmftest.net:8080/w/api.php";// Step 1: GET request to fetch login tokenfunctiongetLoginToken(){varparams_0={action:"query",meta:"tokens",type:"login",format:"json"};request.get({url:url,qs:params_0},function(error,res,body){if(error){return;}vardata=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 & lgpasswordfunctionloginRequest(login_token){varparams_1={action:"clientlogin",username:"username",password:"password",loginreturnurl:"http://127.0.0.1:5000/",logintoken:login_token,format:"json"};request.post({url:url,form:params_1},function(error,res,body){if(error){return;}getUserRightsToken();});}// Step 3: GET request to fetch UserRights tokenfunctiongetUserRightsToken(){varparams_2={action:"query",meta:"tokens",type:"userrights",format:"json"};request.get({url:url,qs:params_2},function(error,res,body){if(error){return;}vardata=JSON.parse(body);userrights(data.query.tokens.userrightstoken);});}// Step 4: POST request to add or remove a user from a groupfunctionuserrights(userrights_token){varparams_3={action:"userrights",user:"ABCDEFG",add:"bot",expiry:"infinite",reason:"API Testing",token:userrights_token,format:"json"};request.post({url:url,form:params_3},function(error,res,body){if(error){return;}console.log(body);});}// Start From Step 1getLoginToken();
/* userrights.js MediaWiki API Demos Demo of `Userrights` module: Add and remove user rights by changing the user's group membership. MIT license*/varparams={action:'userrights',user:'ABCD',add:'sysop',reason:'Added ABCD to the sysop group',format:'json'},api=newmw.Api();api.postWithToken('userrights',params).done(function(data){console.log(data);});
| Code | Info |
|---|---|
| nouser | Theuser parameter must be set. |
| nosuchuser | User "user" doesn't exist This may happen when trying to change an anonymous user's rights. |
| notoken | Thetoken parameter must be set. |
| badtoken | Invalid CSRF token. |
| readonly | The wiki is currently in read-only mode. |
expiryuseridadd andremove fields in the response will simply contain empty arrays.