Merge "mw.Feedback: If the message is posted remotely, link the title correctly"
[lhc/web/wiklou.git] / includes / api / ApiUnblock.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 unblocking of users. Requires API write mode
25 * to be enabled.
26 *
27 * @ingroup API
28 */
29 class ApiUnblock extends ApiBase {
30
31 /**
32 * Unblocks the specified user or provides the reason the unblock failed.
33 */
34 public function execute() {
35 $user = $this->getUser();
36 $params = $this->extractRequestParams();
37
38 $this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' );
39
40 if ( !$user->isAllowed( 'block' ) ) {
41 $this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' );
42 }
43 # T17810: blocked admins should have limited access here
44 if ( $user->isBlocked() ) {
45 $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
46 if ( $status !== true ) {
47 $this->dieWithError(
48 $status,
49 null,
50 [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $user->getBlock() ) ]
51 );
52 }
53 }
54
55 // Check if user can add tags
56 if ( !is_null( $params['tags'] ) ) {
57 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
58 if ( !$ableToTag->isOK() ) {
59 $this->dieStatus( $ableToTag );
60 }
61 }
62
63 if ( $params['userid'] !== null ) {
64 $username = User::whoIs( $params['userid'] );
65
66 if ( $username === false ) {
67 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
68 } else {
69 $params['user'] = $username;
70 }
71 }
72
73 $data = [
74 'Target' => is_null( $params['id'] ) ? $params['user'] : "#{$params['id']}",
75 'Reason' => $params['reason'],
76 'Tags' => $params['tags']
77 ];
78 $block = Block::newFromTarget( $data['Target'] );
79 $retval = SpecialUnblock::processUnblock( $data, $this->getContext() );
80 if ( $retval !== true ) {
81 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
82 }
83
84 $res['id'] = $block->getId();
85 $target = $block->getType() == Block::TYPE_AUTO ? '' : $block->getTarget();
86 $res['user'] = $target instanceof User ? $target->getName() : $target;
87 $res['userid'] = $target instanceof User ? $target->getId() : 0;
88 $res['reason'] = $params['reason'];
89 $this->getResult()->addValue( null, $this->getModuleName(), $res );
90 }
91
92 public function mustBePosted() {
93 return true;
94 }
95
96 public function isWriteMode() {
97 return true;
98 }
99
100 public function getAllowedParams() {
101 return [
102 'id' => [
103 ApiBase::PARAM_TYPE => 'integer',
104 ],
105 'user' => null,
106 'userid' => [
107 ApiBase::PARAM_TYPE => 'integer'
108 ],
109 'reason' => '',
110 'tags' => [
111 ApiBase::PARAM_TYPE => 'tags',
112 ApiBase::PARAM_ISMULTI => true,
113 ],
114 ];
115 }
116
117 public function needsToken() {
118 return 'csrf';
119 }
120
121 protected function getExamplesMessages() {
122 return [
123 'action=unblock&id=105'
124 => 'apihelp-unblock-example-id',
125 'action=unblock&user=Bob&reason=Sorry%20Bob'
126 => 'apihelp-unblock-example-user',
127 ];
128 }
129
130 public function getHelpUrls() {
131 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';
132 }
133 }