wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / api / ApiUserrights.php
blobe32b61236988f891f43ccfa774a14a3820c11b21
1 <?php
3 /**
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
25 * @file
28 /**
29 * @ingroup API
31 class ApiUserrights extends ApiBase {
33 private $mUser = null;
35 /**
36 * Get a UserrightsPage object, or subclass.
37 * @return UserrightsPage
39 protected function getUserRightsPage() {
40 return new UserrightsPage;
43 /**
44 * Get all available groups.
45 * @return array
47 protected function getAllGroups() {
48 return User::getAllGroups();
51 public function execute() {
52 $pUser = $this->getUser();
54 // Deny if the user is blocked and doesn't have the full 'userrights' permission.
55 // This matches what Special:UserRights does for the web UI.
56 if ( $pUser->isBlocked() && !$pUser->isAllowed( 'userrights' ) ) {
57 $this->dieBlocked( $pUser->getBlock() );
60 $params = $this->extractRequestParams();
62 $user = $this->getUrUser( $params );
64 $form = $this->getUserRightsPage();
65 $form->setContext( $this->getContext() );
66 $r['user'] = $user->getName();
67 $r['userid'] = $user->getId();
68 list( $r['added'], $r['removed'] ) = $form->doSaveUserGroups(
69 $user, (array)$params['add'],
70 (array)$params['remove'], $params['reason']
73 $result = $this->getResult();
74 ApiResult::setIndexedTagName( $r['added'], 'group' );
75 ApiResult::setIndexedTagName( $r['removed'], 'group' );
76 $result->addValue( null, $this->getModuleName(), $r );
79 /**
80 * @param array $params
81 * @return User
83 private function getUrUser( array $params ) {
84 if ( $this->mUser !== null ) {
85 return $this->mUser;
88 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
90 $user = isset( $params['user'] ) ? $params['user'] : '#' . $params['userid'];
92 $form = $this->getUserRightsPage();
93 $form->setContext( $this->getContext() );
94 $status = $form->fetchUser( $user );
95 if ( !$status->isOK() ) {
96 $this->dieStatus( $status );
99 $this->mUser = $status->value;
101 return $status->value;
104 public function mustBePosted() {
105 return true;
108 public function isWriteMode() {
109 return true;
112 public function getAllowedParams() {
113 return array(
114 'user' => array(
115 ApiBase::PARAM_TYPE => 'string',
117 'userid' => array(
118 ApiBase::PARAM_TYPE => 'integer',
120 'add' => array(
121 ApiBase::PARAM_TYPE => $this->getAllGroups(),
122 ApiBase::PARAM_ISMULTI => true
124 'remove' => array(
125 ApiBase::PARAM_TYPE => $this->getAllGroups(),
126 ApiBase::PARAM_ISMULTI => true
128 'reason' => array(
129 ApiBase::PARAM_DFLT => ''
131 'token' => array(
132 // Standard definition automatically inserted
133 ApiBase::PARAM_HELP_MSG_APPEND => array( 'api-help-param-token-webui' ),
138 public function needsToken() {
139 return 'userrights';
142 protected function getWebUITokenSalt( array $params ) {
143 return $this->getUrUser( $params )->getName();
146 protected function getExamplesMessages() {
147 return array(
148 'action=userrights&user=FooBot&add=bot&remove=sysop|bureaucrat&token=123ABC'
149 => 'apihelp-userrights-example-user',
150 'action=userrights&userid=123&add=bot&remove=sysop|bureaucrat&token=123ABC'
151 => 'apihelp-userrights-example-userid',
155 public function getHelpUrls() {
156 return 'https://www.mediawiki.org/wiki/API:User_group_membership';