5 * Created on Sep 4, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * API module that facilitates the blocking of users. Requires API write mode
33 class ApiBlock
extends ApiBase
{
36 * Blocks the user specified in the parameters for the given expiry, with the
37 * given reason, and with all other settings provided in the params. If the block
38 * succeeds, produces a result containing the details of the block and notice
39 * of success. If it fails, the result will specify the nature of the error.
41 public function execute() {
44 $user = $this->getUser();
45 $params = $this->extractRequestParams();
47 if ( !$user->isAllowed( 'block' ) ) {
48 $this->dieUsageMsg( 'cantblock' );
51 # bug 15810: blocked admins should have limited access here
52 if ( $user->isBlocked() ) {
53 $status = SpecialBlock
::checkUnblockSelf( $params['user'], $user );
54 if ( $status !== true ) {
55 $this->dieUsageMsg( array( $status ) );
59 $target = User
::newFromName( $params['user'] );
60 // Bug 38633 - if the target is a user (not an IP address), but it
61 // doesn't exist or is unusable, error.
62 if ( $target instanceof User
&&
63 ( $target->isAnon() /* doesn't exist */ ||
!User
::isUsableName( $target->getName() ) )
65 $this->dieUsageMsg( array( 'nosuchuser', $params['user'] ) );
68 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
69 $this->dieUsageMsg( 'canthide' );
71 if ( $params['noemail'] && !SpecialBlock
::canBlockEmail( $user ) ) {
72 $this->dieUsageMsg( 'cantblock-email' );
76 'PreviousTarget' => $params['user'],
77 'Target' => $params['user'],
83 'Expiry' => $params['expiry'],
84 'HardBlock' => !$params['anononly'],
85 'CreateAccount' => $params['nocreate'],
86 'AutoBlock' => $params['autoblock'],
87 'DisableEmail' => $params['noemail'],
88 'HideUser' => $params['hidename'],
89 'DisableUTEdit' => !$params['allowusertalk'],
90 'Reblock' => $params['reblock'],
91 'Watch' => $params['watchuser'],
95 $retval = SpecialBlock
::processForm( $data, $this->getContext() );
96 if ( $retval !== true ) {
97 // We don't care about multiple errors, just report one of them
98 $this->dieUsageMsg( $retval );
101 list( $target, /*...*/ ) = SpecialBlock
::getTargetAndType( $params['user'] );
102 $res['user'] = $params['user'];
103 $res['userID'] = $target instanceof User ?
$target->getId() : 0;
105 $block = Block
::newFromTarget( $target, null, true );
106 if ( $block instanceof Block
) {
107 $res['expiry'] = $wgContLang->formatExpiry( $block->mExpiry
, TS_ISO_8601
, 'infinite' );
108 $res['id'] = $block->getId();
110 # should be unreachable
115 $res['reason'] = $params['reason'];
116 $res['anononly'] = $params['anononly'];
117 $res['nocreate'] = $params['nocreate'];
118 $res['autoblock'] = $params['autoblock'];
119 $res['noemail'] = $params['noemail'];
120 $res['hidename'] = $params['hidename'];
121 $res['allowusertalk'] = $params['allowusertalk'];
122 $res['watchuser'] = $params['watchuser'];
124 $this->getResult()->addValue( null, $this->getModuleName(), $res );
127 public function mustBePosted() {
131 public function isWriteMode() {
135 public function getAllowedParams() {
138 ApiBase
::PARAM_TYPE
=> 'string',
139 ApiBase
::PARAM_REQUIRED
=> true
145 'autoblock' => false,
148 'allowusertalk' => false,
150 'watchuser' => false,
154 public function needsToken() {
158 protected function getExamplesMessages() {
160 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
161 => 'apihelp-block-example-ip-simple',
162 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
163 => 'apihelp-block-example-user-complex',
167 public function getHelpUrls() {
168 return 'https://www.mediawiki.org/wiki/API:Block';