Set API limits for page restrictions to 10
[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['partial'] ) {
61 $editingRestriction = 'partial';
62 }
63
64 $pageRestrictions = implode( "\n", $params['pagerestrictions'] );
65 }
66
67 if ( $params['userid'] !== null ) {
68 $username = User::whoIs( $params['userid'] );
69
70 if ( $username === false ) {
71 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
72 } else {
73 $params['user'] = $username;
74 }
75 } else {
76 list( $target, $type ) = SpecialBlock::getTargetAndType( $params['user'] );
77
78 // T40633 - if the target is a user (not an IP address), but it
79 // doesn't exist or is unusable, error.
80 if ( $type === Block::TYPE_USER &&
81 ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $params['user'] ) )
82 ) {
83 $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
84 }
85 }
86
87 if ( $params['tags'] ) {
88 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
89 if ( !$ableToTag->isOK() ) {
90 $this->dieStatus( $ableToTag );
91 }
92 }
93
94 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
95 $this->dieWithError( 'apierror-canthide' );
96 }
97 if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
98 $this->dieWithError( 'apierror-cantblock-email' );
99 }
100
101 $data = [
102 'PreviousTarget' => $params['user'],
103 'Target' => $params['user'],
104 'Reason' => [
105 $params['reason'],
106 'other',
107 $params['reason']
108 ],
109 'Expiry' => $params['expiry'],
110 'HardBlock' => !$params['anononly'],
111 'CreateAccount' => $params['nocreate'],
112 'AutoBlock' => $params['autoblock'],
113 'DisableEmail' => $params['noemail'],
114 'HideUser' => $params['hidename'],
115 'DisableUTEdit' => !$params['allowusertalk'],
116 'Reblock' => $params['reblock'],
117 'Watch' => $params['watchuser'],
118 'Confirm' => true,
119 'Tags' => $params['tags'],
120 'EditingRestriction' => $editingRestriction,
121 'PageRestrictions' => $pageRestrictions,
122 ];
123
124 $retval = SpecialBlock::processForm( $data, $this->getContext() );
125 if ( $retval !== true ) {
126 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
127 }
128
129 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
130 $res['user'] = $params['user'];
131 $res['userID'] = $target instanceof User ? $target->getId() : 0;
132
133 $block = Block::newFromTarget( $target, null, true );
134 if ( $block instanceof Block ) {
135 $res['expiry'] = ApiResult::formatExpiry( $block->mExpiry, 'infinite' );
136 $res['id'] = $block->getId();
137 } else {
138 # should be unreachable
139 $res['expiry'] = ''; // @codeCoverageIgnore
140 $res['id'] = ''; // @codeCoverageIgnore
141 }
142
143 $res['reason'] = $params['reason'];
144 $res['anononly'] = $params['anononly'];
145 $res['nocreate'] = $params['nocreate'];
146 $res['autoblock'] = $params['autoblock'];
147 $res['noemail'] = $params['noemail'];
148 $res['hidename'] = $params['hidename'];
149 $res['allowusertalk'] = $params['allowusertalk'];
150 $res['watchuser'] = $params['watchuser'];
151
152 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
153 $res['partial'] = $params['partial'];
154 $res['pagerestrictions'] = $params['pagerestrictions'];
155 }
156
157 $this->getResult()->addValue( null, $this->getModuleName(), $res );
158 }
159
160 public function mustBePosted() {
161 return true;
162 }
163
164 public function isWriteMode() {
165 return true;
166 }
167
168 public function getAllowedParams() {
169 $params = [
170 'user' => [
171 ApiBase::PARAM_TYPE => 'user',
172 ],
173 'userid' => [
174 ApiBase::PARAM_TYPE => 'integer',
175 ],
176 'expiry' => 'never',
177 'reason' => '',
178 'anononly' => false,
179 'nocreate' => false,
180 'autoblock' => false,
181 'noemail' => false,
182 'hidename' => false,
183 'allowusertalk' => false,
184 'reblock' => false,
185 'watchuser' => false,
186 'tags' => [
187 ApiBase::PARAM_TYPE => 'tags',
188 ApiBase::PARAM_ISMULTI => true,
189 ],
190 ];
191
192 if ( $this->getConfig()->get( 'EnablePartialBlocks' ) ) {
193 $params['partial'] = false;
194 $params['pagerestrictions'] = [
195 ApiBase::PARAM_ISMULTI => true,
196 ApiBase::PARAM_ISMULTI_LIMIT1 => 10,
197 ApiBase::PARAM_ISMULTI_LIMIT2 => 10,
198 ];
199 }
200
201 return $params;
202 }
203
204 public function needsToken() {
205 return 'csrf';
206 }
207
208 protected function getExamplesMessages() {
209 // phpcs:disable Generic.Files.LineLength
210 return [
211 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
212 => 'apihelp-block-example-ip-simple',
213 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
214 => 'apihelp-block-example-user-complex',
215 ];
216 // phpcs:enable
217 }
218
219 public function getHelpUrls() {
220 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
221 }
222 }