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");
33 class ApiChangeRights
extends ApiBase
{
35 public function __construct($main, $action) {
36 parent
:: __construct($main, $action);
39 public function execute() {
40 global $wgUser, $wgRequest;
41 $this->getMain()->requestWriteMode();
44 $this->dieUsage('The wiki is in read-only mode', 'readonly');
45 $params = $this->extractRequestParams();
47 $ur = new UserrightsPage($wgRequest);
48 $allowed = $ur->changeableGroups();
51 $u = $ur->fetchUser_real($params['user']);
55 case UserrightsPage
::FETCHUSER_NO_INTERWIKI
:
56 $this->dieUsage("You don't have permission to change users' rights on other wikis", 'nointerwiki');
57 case UserrightsPage
::FETCHUSER_NO_DATABASE
:
58 $this->dieUsage("Database ``{$u[1]}'' does not exist or is not local", 'nosuchdatabase');
59 case UserrightsPage
::FETCHUSER_NO_USER
:
60 $this->dieUsage("You specified an empty username, or none at all", 'emptyuser');
61 case UserrightsPage
::FETCHUSER_NOSUCH_USERID
:
62 $this->dieUsage("There is no user with ID ``{$u[1]}''", 'nosuchuserid');
63 case UserrightsPage
::FETCHUSER_NOSUCH_USERNAME
:
64 $this->dieUsage("There is no user with username ``{$u[1]}''", 'nosuchusername');
66 $this->dieDebug(__METHOD__
, "UserrightsPage::fetchUser_real() returned an unknown error ({$u[0]})");
69 $curgroups = $u->getGroups();
70 if($params['listgroups'])
72 $res['user'] = $u->getName();
73 $res['allowedgroups'] = $allowed;
74 $res['ingroups'] = $curgroups;
75 $this->getResult()->setIndexedTagName($res['ingroups'], 'group');
76 $this->getResult()->setIndexedTagName($res['allowedgroups']['add'], 'group');
77 $this->getResult()->setIndexedTagName($res['allowedgroups']['remove'], 'group');
80 if($params['gettoken'])
82 $res['changerightstoken'] = $wgUser->editToken($u->getName());
83 $this->getResult()->addValue(null, $this->getModuleName(), $res);
87 if(empty($params['addto']) && empty($params['rmfrom']))
88 $this->dieUsage('At least one of the addto and rmfrom parameters must be set', 'noaddrm');
89 if(is_null($params['token']))
90 $this->dieUsage('The token parameter must be set', 'notoken');
91 if(!$wgUser->matchEditToken($params['token'], $u->getName()))
92 $this->dieUsage('Invalid token', 'badtoken');
94 $dbw = wfGetDb(DB_MASTER
);
96 $ur->saveUserGroups($u, $params['rmfrom'], $params['addto'], $params['reason']);
98 $res['user'] = $u->getName();
99 $res['addedto'] = (array)$params['addto'];
100 $res['removedfrom'] = (array)$params['rmfrom'];
101 $res['reason'] = $params['reason'];
103 $this->getResult()->setIndexedTagName($res['addedto'], 'group');
104 $this->getResult()->setIndexedTagName($res['removedfrom'], 'group');
105 $this->getResult()->addValue(null, $this->getModuleName(), $res);
108 protected function getAllowedParams() {
113 'listgroups' => false,
115 ApiBase
:: PARAM_ISMULTI
=> true,
118 ApiBase
:: PARAM_ISMULTI
=> true,
124 protected function getParamDescription() {
126 'user' => 'The user you want to add to or remove from groups.',
127 'token' => 'A changerights token previously obtained through the gettoken parameter.',
128 'gettoken' => 'Output a token. Note that the user parameter still has to be set.',
129 'listgroups' => 'List the groups the user is in, and the ones you can add them to and remove them from.',
130 'addto' => 'Pipe-separated list of groups to add this user to',
131 'rmfrom' => 'Pipe-separated list of groups to remove this user from',
132 'reason' => 'Reason for change (optional)'
136 protected function getDescription() {
138 'Add or remove a user from certain groups.'
142 protected function getExamples() {
144 'api.php?action=changerights&user=Bob&gettoken&listgroups',
145 'api.php?action=changerights&user=Bob&token=123ABC&addto=sysop&reason=Promoting%20per%20RFA'
149 public function getVersion() {
150 return __CLASS__
. ': $Id: ApiChangeRights.php 28216 2007-12-06 18:33:18Z vasilievvv $';