Merge "Silence transaction profiler master queries notices for rollback"
[lhc/web/wiklou.git] / includes / api / ApiBlock.php
1 <?php
2 /**
3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 /**
24 * API module that facilitates the blocking of users. Requires API write mode
25 * to be enabled.
26 *
27 * @ingroup API
28 */
29 class ApiBlock extends ApiBase {
30
31 /**
32 * Blocks the user specified in the parameters for the given expiry, with the
33 * given reason, and with all other settings provided in the params. If the block
34 * succeeds, produces a result containing the details of the block and notice
35 * of success. If it fails, the result will specify the nature of the error.
36 */
37 public function execute() {
38 $this->checkUserRightsAny( 'block' );
39
40 $user = $this->getUser();
41 $params = $this->extractRequestParams();
42
43 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
44
45 # T17810: blocked admins should have limited access here
46 if ( $user->isBlocked() ) {
47 $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
48 if ( $status !== true ) {
49 $this->dieWithError(
50 $status,
51 null,
52 [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ]
53 );
54 }
55 }
56
57 $editingRestriction = 'sitewide';
58 $pageRestrictions = '';
59 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
60 if ( $params['pagerestrictions'] ) {
61 $count = count( $params['pagerestrictions'] );
62 if ( $count > 10 ) {
63 $this->dieWithError(
64 $this->msg(
65 'apierror-integeroutofrange-abovebotmax',
66 'pagerestrictions',
67 10,
68 $count
69 )
70 );
71 }
72 }
73
74 if ( $params['partial'] ) {
75 $editingRestriction = 'partial';
76 }
77
78 $pageRestrictions = implode( "\n", $params['pagerestrictions'] );
79 }
80
81 if ( $params['userid'] !== null ) {
82 $username = User::whoIs( $params['userid'] );
83
84 if ( $username === false ) {
85 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
86 } else {
87 $params['user'] = $username;
88 }
89 } else {
90 list( $target, $type ) = SpecialBlock::getTargetAndType( $params['user'] );
91
92 // T40633 - if the target is a user (not an IP address), but it
93 // doesn't exist or is unusable, error.
94 if ( $type === Block::TYPE_USER &&
95 ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $params['user'] ) )
96 ) {
97 $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
98 }
99 }
100
101 if ( $params['tags'] ) {
102 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
103 if ( !$ableToTag->isOK() ) {
104 $this->dieStatus( $ableToTag );
105 }
106 }
107
108 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
109 $this->dieWithError( 'apierror-canthide' );
110 }
111 if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
112 $this->dieWithError( 'apierror-cantblock-email' );
113 }
114
115 $data = [
116 'PreviousTarget' => $params['user'],
117 'Target' => $params['user'],
118 'Reason' => [
119 $params['reason'],
120 'other',
121 $params['reason']
122 ],
123 'Expiry' => $params['expiry'],
124 'HardBlock' => !$params['anononly'],
125 'CreateAccount' => $params['nocreate'],
126 'AutoBlock' => $params['autoblock'],
127 'DisableEmail' => $params['noemail'],
128 'HideUser' => $params['hidename'],
129 'DisableUTEdit' => !$params['allowusertalk'],
130 'Reblock' => $params['reblock'],
131 'Watch' => $params['watchuser'],
132 'Confirm' => true,
133 'Tags' => $params['tags'],
134 'EditingRestriction' => $editingRestriction,
135 'PageRestrictions' => $pageRestrictions,
136 ];
137
138 $retval = SpecialBlock::processForm( $data, $this->getContext() );
139 if ( $retval !== true ) {
140 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
141 }
142
143 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
144 $res['user'] = $params['user'];
145 $res['userID'] = $target instanceof User ? $target->getId() : 0;
146
147 $block = Block::newFromTarget( $target, null, true );
148 if ( $block instanceof Block ) {
149 $res['expiry'] = ApiResult::formatExpiry( $block->mExpiry, 'infinite' );
150 $res['id'] = $block->getId();
151 } else {
152 # should be unreachable
153 $res['expiry'] = ''; // @codeCoverageIgnore
154 $res['id'] = ''; // @codeCoverageIgnore
155 }
156
157 $res['reason'] = $params['reason'];
158 $res['anononly'] = $params['anononly'];
159 $res['nocreate'] = $params['nocreate'];
160 $res['autoblock'] = $params['autoblock'];
161 $res['noemail'] = $params['noemail'];
162 $res['hidename'] = $params['hidename'];
163 $res['allowusertalk'] = $params['allowusertalk'];
164 $res['watchuser'] = $params['watchuser'];
165
166 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
167 $res['partial'] = $params['partial'];
168 $res['pagerestrictions'] = $params['pagerestrictions'];
169 }
170
171 $this->getResult()->addValue( null, $this->getModuleName(), $res );
172 }
173
174 public function mustBePosted() {
175 return true;
176 }
177
178 public function isWriteMode() {
179 return true;
180 }
181
182 public function getAllowedParams() {
183 $params = [
184 'user' => [
185 ApiBase::PARAM_TYPE => 'user',
186 ],
187 'userid' => [
188 ApiBase::PARAM_TYPE => 'integer',
189 ],
190 'expiry' => 'never',
191 'reason' => '',
192 'anononly' => false,
193 'nocreate' => false,
194 'autoblock' => false,
195 'noemail' => false,
196 'hidename' => false,
197 'allowusertalk' => false,
198 'reblock' => false,
199 'watchuser' => false,
200 'tags' => [
201 ApiBase::PARAM_TYPE => 'tags',
202 ApiBase::PARAM_ISMULTI => true,
203 ],
204 ];
205
206 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
207 $params['partial'] = false;
208 $params['pagerestrictions'] = [
209 ApiBase::PARAM_ISMULTI => true,
210 ];
211 }
212
213 return $params;
214 }
215
216 public function needsToken() {
217 return 'csrf';
218 }
219
220 protected function getExamplesMessages() {
221 // phpcs:disable Generic.Files.LineLength
222 return [
223 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
224 => 'apihelp-block-example-ip-simple',
225 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
226 => 'apihelp-block-example-user-complex',
227 ];
228 // phpcs:enable
229 }
230
231 public function getHelpUrls() {
232 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
233 }
234 }