Add parameter to API modules to apply change tags to log entries
[lhc/web/wiklou.git] / includes / api / ApiBlock.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 4, 2007
6 *
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * API module that facilitates the blocking of users. Requires API write mode
29 * to be enabled.
30 *
31 * @ingroup API
32 */
33 class ApiBlock extends ApiBase {
34
35 /**
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.
40 */
41 public function execute() {
42 global $wgContLang;
43
44 $this->checkUserRightsAny( 'block' );
45
46 $user = $this->getUser();
47 $params = $this->extractRequestParams();
48
49 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
50
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->dieWithError(
56 $status,
57 null,
58 [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ]
59 );
60 }
61 }
62
63 if ( $params['userid'] !== null ) {
64 $username = User::whoIs( $params['userid'] );
65
66 if ( $username === false ) {
67 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
68 } else {
69 $params['user'] = $username;
70 }
71 } else {
72 $target = User::newFromName( $params['user'] );
73
74 // Bug 38633 - if the target is a user (not an IP address), but it
75 // doesn't exist or is unusable, error.
76 if ( $target instanceof User &&
77 ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $target->getName() ) )
78 ) {
79 $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
80 }
81 }
82
83 if ( $params['tags'] ) {
84 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
85 if ( !$ableToTag->isOK() ) {
86 $this->dieStatus( $ableToTag );
87 }
88 }
89
90 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
91 $this->dieWithError( 'apierror-canthide' );
92 }
93 if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
94 $this->dieWithError( 'apierror-cantblock-email' );
95 }
96
97 $data = [
98 'PreviousTarget' => $params['user'],
99 'Target' => $params['user'],
100 'Reason' => [
101 $params['reason'],
102 'other',
103 $params['reason']
104 ],
105 'Expiry' => $params['expiry'],
106 'HardBlock' => !$params['anononly'],
107 'CreateAccount' => $params['nocreate'],
108 'AutoBlock' => $params['autoblock'],
109 'DisableEmail' => $params['noemail'],
110 'HideUser' => $params['hidename'],
111 'DisableUTEdit' => !$params['allowusertalk'],
112 'Reblock' => $params['reblock'],
113 'Watch' => $params['watchuser'],
114 'Confirm' => true,
115 'Tags' => $params['tags'],
116 ];
117
118 $retval = SpecialBlock::processForm( $data, $this->getContext() );
119 if ( $retval !== true ) {
120 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
121 }
122
123 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
124 $res['user'] = $params['user'];
125 $res['userID'] = $target instanceof User ? $target->getId() : 0;
126
127 $block = Block::newFromTarget( $target, null, true );
128 if ( $block instanceof Block ) {
129 $res['expiry'] = $wgContLang->formatExpiry( $block->mExpiry, TS_ISO_8601, 'infinite' );
130 $res['id'] = $block->getId();
131 } else {
132 # should be unreachable
133 $res['expiry'] = '';
134 $res['id'] = '';
135 }
136
137 $res['reason'] = $params['reason'];
138 $res['anononly'] = $params['anononly'];
139 $res['nocreate'] = $params['nocreate'];
140 $res['autoblock'] = $params['autoblock'];
141 $res['noemail'] = $params['noemail'];
142 $res['hidename'] = $params['hidename'];
143 $res['allowusertalk'] = $params['allowusertalk'];
144 $res['watchuser'] = $params['watchuser'];
145
146 $this->getResult()->addValue( null, $this->getModuleName(), $res );
147 }
148
149 public function mustBePosted() {
150 return true;
151 }
152
153 public function isWriteMode() {
154 return true;
155 }
156
157 public function getAllowedParams() {
158 return [
159 'user' => [
160 ApiBase::PARAM_TYPE => 'user',
161 ],
162 'userid' => [
163 ApiBase::PARAM_TYPE => 'integer',
164 ],
165 'expiry' => 'never',
166 'reason' => '',
167 'anononly' => false,
168 'nocreate' => false,
169 'autoblock' => false,
170 'noemail' => false,
171 'hidename' => false,
172 'allowusertalk' => false,
173 'reblock' => false,
174 'watchuser' => false,
175 'tags' => [
176 ApiBase::PARAM_TYPE => 'tags',
177 ApiBase::PARAM_ISMULTI => true,
178 ],
179 ];
180 }
181
182 public function needsToken() {
183 return 'csrf';
184 }
185
186 protected function getExamplesMessages() {
187 // @codingStandardsIgnoreStart Generic.Files.LineLength
188 return [
189 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
190 => 'apihelp-block-example-ip-simple',
191 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
192 => 'apihelp-block-example-user-complex',
193 ];
194 // @codingStandardsIgnoreEnd
195 }
196
197 public function getHelpUrls() {
198 return 'https://www.mediawiki.org/wiki/API:Block';
199 }
200 }