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