3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * API module that facilitates the blocking of users. Requires API write mode
29 class ApiBlock
extends ApiBase
{
31 use ApiBlockInfoTrait
;
34 * Blocks the user specified in the parameters for the given expiry, with the
35 * given reason, and with all other settings provided in the params. If the block
36 * succeeds, produces a result containing the details of the block and notice
37 * of success. If it fails, the result will specify the nature of the error.
39 public function execute() {
40 $this->checkUserRightsAny( 'block' );
42 $user = $this->getUser();
43 $params = $this->extractRequestParams();
45 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
47 # T17810: blocked admins should have limited access here
48 $block = $user->getBlock();
50 $status = SpecialBlock
::checkUnblockSelf( $params['user'], $user );
51 if ( $status !== true ) {
55 [ 'blockinfo' => $this->getBlockInfo( $block ) ]
60 $editingRestriction = 'sitewide';
61 $pageRestrictions = '';
62 $namespaceRestrictions = '';
63 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
64 if ( $params['partial'] ) {
65 $editingRestriction = 'partial';
68 $pageRestrictions = implode( "\n", (array)$params['pagerestrictions'] );
69 $namespaceRestrictions = implode( "\n", (array)$params['namespacerestrictions'] );
72 if ( $params['userid'] !== null ) {
73 $username = User
::whoIs( $params['userid'] );
75 if ( $username === false ) {
76 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
78 $params['user'] = $username;
81 list( $target, $type ) = SpecialBlock
::getTargetAndType( $params['user'] );
83 // T40633 - if the target is a user (not an IP address), but it
84 // doesn't exist or is unusable, error.
85 if ( $type === Block
::TYPE_USER
&&
86 ( $target->isAnon() /* doesn't exist */ ||
!User
::isUsableName( $params['user'] ) )
88 $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
92 if ( $params['tags'] ) {
93 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
94 if ( !$ableToTag->isOK() ) {
95 $this->dieStatus( $ableToTag );
99 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
100 $this->dieWithError( 'apierror-canthide' );
102 if ( $params['noemail'] && !SpecialBlock
::canBlockEmail( $user ) ) {
103 $this->dieWithError( 'apierror-cantblock-email' );
107 'PreviousTarget' => $params['user'],
108 'Target' => $params['user'],
114 'Expiry' => $params['expiry'],
115 'HardBlock' => !$params['anononly'],
116 'CreateAccount' => $params['nocreate'],
117 'AutoBlock' => $params['autoblock'],
118 'DisableEmail' => $params['noemail'],
119 'HideUser' => $params['hidename'],
120 'DisableUTEdit' => !$params['allowusertalk'],
121 'Reblock' => $params['reblock'],
122 'Watch' => $params['watchuser'],
124 'Tags' => $params['tags'],
125 'EditingRestriction' => $editingRestriction,
126 'PageRestrictions' => $pageRestrictions,
127 'NamespaceRestrictions' => $namespaceRestrictions,
130 $retval = SpecialBlock
::processForm( $data, $this->getContext() );
131 if ( $retval !== true ) {
132 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
135 list( $target, /*...*/ ) = SpecialBlock
::getTargetAndType( $params['user'] );
136 $res['user'] = $params['user'];
137 $res['userID'] = $target instanceof User ?
$target->getId() : 0;
139 $block = Block
::newFromTarget( $target, null, true );
140 if ( $block instanceof Block
) {
141 $res['expiry'] = ApiResult
::formatExpiry( $block->getExpiry(), 'infinite' );
142 $res['id'] = $block->getId();
144 # should be unreachable
145 $res['expiry'] = ''; // @codeCoverageIgnore
146 $res['id'] = ''; // @codeCoverageIgnore
149 $res['reason'] = $params['reason'];
150 $res['anononly'] = $params['anononly'];
151 $res['nocreate'] = $params['nocreate'];
152 $res['autoblock'] = $params['autoblock'];
153 $res['noemail'] = $params['noemail'];
154 $res['hidename'] = $params['hidename'];
155 $res['allowusertalk'] = $params['allowusertalk'];
156 $res['watchuser'] = $params['watchuser'];
158 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
159 $res['partial'] = $params['partial'];
160 $res['pagerestrictions'] = $params['pagerestrictions'];
161 $res['namespacerestrictions'] = $params['namespacerestrictions'];
164 $this->getResult()->addValue( null, $this->getModuleName(), $res );
167 public function mustBePosted() {
171 public function isWriteMode() {
175 public function getAllowedParams() {
178 ApiBase
::PARAM_TYPE
=> 'user',
181 ApiBase
::PARAM_TYPE
=> 'integer',
187 'autoblock' => false,
190 'allowusertalk' => false,
192 'watchuser' => false,
194 ApiBase
::PARAM_TYPE
=> 'tags',
195 ApiBase
::PARAM_ISMULTI
=> true,
199 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
200 $params['partial'] = false;
201 $params['pagerestrictions'] = [
202 ApiBase
::PARAM_ISMULTI
=> true,
203 ApiBase
::PARAM_ISMULTI_LIMIT1
=> 10,
204 ApiBase
::PARAM_ISMULTI_LIMIT2
=> 10,
206 $params['namespacerestrictions'] = [
207 ApiBase
::PARAM_ISMULTI
=> true,
208 ApiBase
::PARAM_TYPE
=> 'namespace',
215 public function needsToken() {
219 protected function getExamplesMessages() {
220 // phpcs:disable Generic.Files.LineLength
222 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
223 => 'apihelp-block-example-ip-simple',
224 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
225 => 'apihelp-block-example-user-complex',
230 public function getHelpUrls() {
231 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';