4 * Created on Sep 11, 2007
5 * API for MediaWiki 1.8+
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
25 if (!defined('MEDIAWIKI')) {
26 // Eclipse helper - will be ignored in production
27 require_once ("ApiBase.php");
31 * API module that facilitates the changing of user rights. The API eqivalent of
32 * Special:Userrights. Requires API write mode to be enabled.
36 class ApiChangeRights
extends ApiBase
{
38 public function __construct($main, $action) {
39 parent
:: __construct($main, $action);
42 public function execute() {
43 global $wgUser, $wgRequest;
44 $this->getMain()->requestWriteMode();
47 $this->dieUsage('The wiki is in read-only mode', 'readonly');
48 $params = $this->extractRequestParams();
50 $ur = new UserrightsPage($wgRequest);
51 $allowed = $ur->changeableGroups();
54 $u = $ur->fetchUser_real($params['user']);
58 case UserrightsPage
::FETCHUSER_NO_INTERWIKI
:
59 $this->dieUsage("You don't have permission to change users' rights on other wikis", 'nointerwiki');
60 case UserrightsPage
::FETCHUSER_NO_DATABASE
:
61 $this->dieUsage("Database ``{$u[1]}'' does not exist or is not local", 'nosuchdatabase');
62 case UserrightsPage
::FETCHUSER_NO_USER
:
63 $this->dieUsage("You specified an empty username, or none at all", 'emptyuser');
64 case UserrightsPage
::FETCHUSER_NOSUCH_USERID
:
65 $this->dieUsage("There is no user with ID ``{$u[1]}''", 'nosuchuserid');
66 case UserrightsPage
::FETCHUSER_NOSUCH_USERNAME
:
67 $this->dieUsage("There is no user with username ``{$u[1]}''", 'nosuchusername');
69 $this->dieDebug(__METHOD__
, "UserrightsPage::fetchUser_real() returned an unknown error ({$u[0]})");
72 $curgroups = $u->getGroups();
73 if($params['listgroups'])
75 $res['user'] = $u->getName();
76 $res['allowedgroups'] = $allowed;
77 $res['ingroups'] = $curgroups;
78 $this->getResult()->setIndexedTagName($res['ingroups'], 'group');
79 $this->getResult()->setIndexedTagName($res['allowedgroups']['add'], 'group');
80 $this->getResult()->setIndexedTagName($res['allowedgroups']['remove'], 'group');
83 if($params['gettoken'])
85 $res['changerightstoken'] = $wgUser->editToken($u->getName());
86 $this->getResult()->addValue(null, $this->getModuleName(), $res);
90 if(empty($params['addto']) && empty($params['rmfrom']))
91 $this->dieUsage('At least one of the addto and rmfrom parameters must be set', 'noaddrm');
92 if(is_null($params['token']))
93 $this->dieUsage('The token parameter must be set', 'notoken');
94 if(!$wgUser->matchEditToken($params['token'], $u->getName()))
95 $this->dieUsage('Invalid token', 'badtoken');
97 $dbw = wfGetDb(DB_MASTER
);
99 $ur->saveUserGroups($u, $params['rmfrom'], $params['addto'], $params['reason']);
101 $res['user'] = $u->getName();
102 $res['addedto'] = (array)$params['addto'];
103 $res['removedfrom'] = (array)$params['rmfrom'];
104 $res['reason'] = $params['reason'];
106 $this->getResult()->setIndexedTagName($res['addedto'], 'group');
107 $this->getResult()->setIndexedTagName($res['removedfrom'], 'group');
108 $this->getResult()->addValue(null, $this->getModuleName(), $res);
111 public function getAllowedParams() {
116 'listgroups' => false,
118 ApiBase
:: PARAM_ISMULTI
=> true,
121 ApiBase
:: PARAM_ISMULTI
=> true,
127 public function getParamDescription() {
129 'user' => 'The user you want to add to or remove from groups.',
130 'token' => 'A changerights token previously obtained through the gettoken parameter.',
131 'gettoken' => 'Output a token. Note that the user parameter still has to be set.',
132 'listgroups' => 'List the groups the user is in, and the ones you can add them to and remove them from.',
133 'addto' => 'Pipe-separated list of groups to add this user to',
134 'rmfrom' => 'Pipe-separated list of groups to remove this user from',
135 'reason' => 'Reason for change (optional)'
139 public function getDescription() {
141 'Add or remove a user from certain groups.'
145 protected function getExamples() {
147 'api.php?action=changerights&user=Bob&gettoken&listgroups',
148 'api.php?action=changerights&user=Bob&token=123ABC&addto=sysop&reason=Promoting%20per%20RFA'
152 public function getVersion() {
153 return __CLASS__
. ': $Id: ApiChangeRights.php 28216 2007-12-06 18:33:18Z vasilievvv $';