Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[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 use MediaWiki\Block\DatabaseBlock;
24
25 /**
26 * API module that facilitates the blocking of users. Requires API write mode
27 * to be enabled.
28 *
29 * @ingroup API
30 */
31 class ApiBlock extends ApiBase {
32
33 use ApiBlockInfoTrait;
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 $this->checkUserRightsAny( 'block' );
43
44 $user = $this->getUser();
45 $params = $this->extractRequestParams();
46
47 $this->requireOnlyOneParameter( $params, 'user', 'userid' );
48
49 # T17810: blocked admins should have limited access here
50 $block = $user->getBlock();
51 if ( $block ) {
52 $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
53 if ( $status !== true ) {
54 $this->dieWithError(
55 $status,
56 null,
57 [ 'blockinfo' => $this->getBlockDetails( $block ) ]
58 );
59 }
60 }
61
62 $editingRestriction = 'sitewide';
63 $pageRestrictions = '';
64 $namespaceRestrictions = '';
65 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
66 if ( $params['partial'] ) {
67 $editingRestriction = 'partial';
68 }
69
70 $pageRestrictions = implode( "\n", (array)$params['pagerestrictions'] );
71 $namespaceRestrictions = implode( "\n", (array)$params['namespacerestrictions'] );
72 }
73
74 if ( $params['userid'] !== null ) {
75 $username = User::whoIs( $params['userid'] );
76
77 if ( $username === false ) {
78 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
79 } else {
80 $params['user'] = $username;
81 }
82 } else {
83 list( $target, $type ) = SpecialBlock::getTargetAndType( $params['user'] );
84
85 // T40633 - if the target is a user (not an IP address), but it
86 // doesn't exist or is unusable, error.
87 if ( $type === DatabaseBlock::TYPE_USER &&
88 ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $params['user'] ) )
89 ) {
90 $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
91 }
92 }
93
94 if ( $params['tags'] ) {
95 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
96 if ( !$ableToTag->isOK() ) {
97 $this->dieStatus( $ableToTag );
98 }
99 }
100
101 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
102 $this->dieWithError( 'apierror-canthide' );
103 }
104 if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
105 $this->dieWithError( 'apierror-cantblock-email' );
106 }
107
108 $data = [
109 'PreviousTarget' => $params['user'],
110 'Target' => $params['user'],
111 'Reason' => [
112 $params['reason'],
113 'other',
114 $params['reason']
115 ],
116 'Expiry' => $params['expiry'],
117 'HardBlock' => !$params['anononly'],
118 'CreateAccount' => $params['nocreate'],
119 'AutoBlock' => $params['autoblock'],
120 'DisableEmail' => $params['noemail'],
121 'HideUser' => $params['hidename'],
122 'DisableUTEdit' => !$params['allowusertalk'],
123 'Reblock' => $params['reblock'],
124 'Watch' => $params['watchuser'],
125 'Confirm' => true,
126 'Tags' => $params['tags'],
127 'EditingRestriction' => $editingRestriction,
128 'PageRestrictions' => $pageRestrictions,
129 'NamespaceRestrictions' => $namespaceRestrictions,
130 ];
131
132 $status = SpecialBlock::validateTarget( $params['user'], $user );
133 if ( !$status->isOK() ) {
134 $this->dieStatus( $status );
135 }
136
137 $retval = SpecialBlock::processForm( $data, $this->getContext() );
138 if ( $retval !== true ) {
139 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
140 }
141
142 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
143 $res['user'] = $params['user'];
144 $res['userID'] = $target instanceof User ? $target->getId() : 0;
145
146 $block = DatabaseBlock::newFromTarget( $target, null, true );
147 if ( $block instanceof DatabaseBlock ) {
148 $res['expiry'] = ApiResult::formatExpiry( $block->getExpiry(), 'infinite' );
149 $res['id'] = $block->getId();
150 } else {
151 # should be unreachable
152 $res['expiry'] = ''; // @codeCoverageIgnore
153 $res['id'] = ''; // @codeCoverageIgnore
154 }
155
156 $res['reason'] = $params['reason'];
157 $res['anononly'] = $params['anononly'];
158 $res['nocreate'] = $params['nocreate'];
159 $res['autoblock'] = $params['autoblock'];
160 $res['noemail'] = $params['noemail'];
161 $res['hidename'] = $params['hidename'];
162 $res['allowusertalk'] = $params['allowusertalk'];
163 $res['watchuser'] = $params['watchuser'];
164
165 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
166 $res['partial'] = $params['partial'];
167 $res['pagerestrictions'] = $params['pagerestrictions'];
168 $res['namespacerestrictions'] = $params['namespacerestrictions'];
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 ApiBase::PARAM_ISMULTI_LIMIT1 => 10,
211 ApiBase::PARAM_ISMULTI_LIMIT2 => 10,
212 ];
213 $params['namespacerestrictions'] = [
214 ApiBase::PARAM_ISMULTI => true,
215 ApiBase::PARAM_TYPE => 'namespace',
216 ];
217 }
218
219 return $params;
220 }
221
222 public function needsToken() {
223 return 'csrf';
224 }
225
226 protected function getExamplesMessages() {
227 // phpcs:disable Generic.Files.LineLength
228 return [
229 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
230 => 'apihelp-block-example-ip-simple',
231 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
232 => 'apihelp-block-example-user-complex',
233 ];
234 // phpcs:enable
235 }
236
237 public function getHelpUrls() {
238 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
239 }
240 }