Merge "Revert "Replace Linker::link() usage with LinkRenderer""
[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 # bug 15810: blocked admins should have limited access here
50 if ( $user->isBlocked() ) {
51 $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
52 if ( $status !== true ) {
53 $this->dieWithError(
54 $status,
55 null,
56 [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ]
57 );
58 }
59 }
60
61 $target = User::newFromName( $params['user'] );
62 // Bug 38633 - if the target is a user (not an IP address), but it
63 // doesn't exist or is unusable, error.
64 if ( $target instanceof User &&
65 ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $target->getName() ) )
66 ) {
67 $this->dieWithError( [ 'nosuchusershort', $params['user'] ], 'nosuchuser' );
68 }
69
70 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
71 $this->dieWithError( 'apierror-canthide' );
72 }
73 if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
74 $this->dieWithError( 'apierror-cantblock-email' );
75 }
76
77 $data = [
78 'PreviousTarget' => $params['user'],
79 'Target' => $params['user'],
80 'Reason' => [
81 $params['reason'],
82 'other',
83 $params['reason']
84 ],
85 'Expiry' => $params['expiry'],
86 'HardBlock' => !$params['anononly'],
87 'CreateAccount' => $params['nocreate'],
88 'AutoBlock' => $params['autoblock'],
89 'DisableEmail' => $params['noemail'],
90 'HideUser' => $params['hidename'],
91 'DisableUTEdit' => !$params['allowusertalk'],
92 'Reblock' => $params['reblock'],
93 'Watch' => $params['watchuser'],
94 'Confirm' => true,
95 ];
96
97 $retval = SpecialBlock::processForm( $data, $this->getContext() );
98 if ( $retval !== true ) {
99 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
100 }
101
102 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
103 $res['user'] = $params['user'];
104 $res['userID'] = $target instanceof User ? $target->getId() : 0;
105
106 $block = Block::newFromTarget( $target, null, true );
107 if ( $block instanceof Block ) {
108 $res['expiry'] = $wgContLang->formatExpiry( $block->mExpiry, TS_ISO_8601, 'infinite' );
109 $res['id'] = $block->getId();
110 } else {
111 # should be unreachable
112 $res['expiry'] = '';
113 $res['id'] = '';
114 }
115
116 $res['reason'] = $params['reason'];
117 $res['anononly'] = $params['anononly'];
118 $res['nocreate'] = $params['nocreate'];
119 $res['autoblock'] = $params['autoblock'];
120 $res['noemail'] = $params['noemail'];
121 $res['hidename'] = $params['hidename'];
122 $res['allowusertalk'] = $params['allowusertalk'];
123 $res['watchuser'] = $params['watchuser'];
124
125 $this->getResult()->addValue( null, $this->getModuleName(), $res );
126 }
127
128 public function mustBePosted() {
129 return true;
130 }
131
132 public function isWriteMode() {
133 return true;
134 }
135
136 public function getAllowedParams() {
137 return [
138 'user' => [
139 ApiBase::PARAM_TYPE => 'user',
140 ApiBase::PARAM_REQUIRED => true
141 ],
142 'expiry' => 'never',
143 'reason' => '',
144 'anononly' => false,
145 'nocreate' => false,
146 'autoblock' => false,
147 'noemail' => false,
148 'hidename' => false,
149 'allowusertalk' => false,
150 'reblock' => false,
151 'watchuser' => false,
152 ];
153 }
154
155 public function needsToken() {
156 return 'csrf';
157 }
158
159 protected function getExamplesMessages() {
160 // @codingStandardsIgnoreStart Generic.Files.LineLength
161 return [
162 'action=block&user=192.0.2.5&expiry=3%20days&reason=First%20strike&token=123ABC'
163 => 'apihelp-block-example-ip-simple',
164 'action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
165 => 'apihelp-block-example-user-complex',
166 ];
167 // @codingStandardsIgnoreEnd
168 }
169
170 public function getHelpUrls() {
171 return 'https://www.mediawiki.org/wiki/API:Block';
172 }
173 }