6 * Created on Mar 24, 2009
8 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
31 class ApiUserrights
extends ApiBase
{
33 private $mUser = null;
36 * Get a UserrightsPage object, or subclass.
37 * @return UserrightsPage
39 protected function getUserRightsPage() {
40 return new UserrightsPage
;
44 * Get all available groups.
47 protected function getAllGroups() {
48 return User
::getAllGroups();
51 public function execute() {
52 $params = $this->extractRequestParams();
54 $user = $this->getUrUser( $params );
56 $form = $this->getUserRightsPage();
57 $form->setContext( $this->getContext() );
58 $r['user'] = $user->getName();
59 $r['userid'] = $user->getId();
60 list( $r['added'], $r['removed'] ) = $form->doSaveUserGroups(
61 $user, (array)$params['add'],
62 (array)$params['remove'], $params['reason']
65 $result = $this->getResult();
66 $result->setIndexedTagName( $r['added'], 'group' );
67 $result->setIndexedTagName( $r['removed'], 'group' );
68 $result->addValue( null, $this->getModuleName(), $r );
72 * @param array $params
75 private function getUrUser( array $params ) {
76 if ( $this->mUser
!== null ) {
80 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
82 $user = isset( $params['user'] ) ?
$params['user'] : '#' . $params['userid'];
84 $form = $this->getUserRightsPage();
85 $form->setContext( $this->getContext() );
86 $status = $form->fetchUser( $user );
87 if ( !$status->isOK() ) {
88 $this->dieStatus( $status );
91 $this->mUser
= $status->value
;
93 return $status->value
;
96 public function mustBePosted() {
100 public function isWriteMode() {
104 public function getAllowedParams() {
107 ApiBase
::PARAM_TYPE
=> 'string',
110 ApiBase
::PARAM_TYPE
=> 'integer',
113 ApiBase
::PARAM_TYPE
=> $this->getAllGroups(),
114 ApiBase
::PARAM_ISMULTI
=> true
117 ApiBase
::PARAM_TYPE
=> $this->getAllGroups(),
118 ApiBase
::PARAM_ISMULTI
=> true
121 ApiBase
::PARAM_DFLT
=> ''
124 // Standard definition automatically inserted
125 ApiBase
::PARAM_HELP_MSG_APPEND
=> array( 'api-help-param-token-webui' ),
130 public function needsToken() {
134 protected function getWebUITokenSalt( array $params ) {
135 return $this->getUrUser( $params )->getName();
138 protected function getExamplesMessages() {
140 'action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
141 => 'apihelp-userrights-example-user',
142 'action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
143 => 'apihelp-userrights-example-userid',
147 public function getHelpUrls() {
148 return 'https://www.mediawiki.org/wiki/API:User_group_membership';