3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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.
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.
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
23 use MediaWiki\Block\DatabaseBlock
;
26 * API module that facilitates the unblocking of users. Requires API write mode
31 class ApiUnblock
extends ApiBase
{
33 use ApiBlockInfoTrait
;
36 * Unblocks the specified user or provides the reason the unblock failed.
38 public function execute() {
39 $user = $this->getUser();
40 $params = $this->extractRequestParams();
42 $this->requireOnlyOneParameter( $params, 'id', 'user', 'userid' );
44 if ( !$user->isAllowed( 'block' ) ) {
45 $this->dieWithError( 'apierror-permissiondenied-unblock', 'permissiondenied' );
47 # T17810: blocked admins should have limited access here
48 $block = $user->getBlock();
50 $status = SpecialBlock
::checkUnblockSelf( $params['user'], $user );
51 if ( $status !== true ) {
55 [ 'blockinfo' => $this->getBlockDetails( $block ) ]
60 // Check if user can add tags
61 if ( !is_null( $params['tags'] ) ) {
62 $ableToTag = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
63 if ( !$ableToTag->isOK() ) {
64 $this->dieStatus( $ableToTag );
68 if ( $params['userid'] !== null ) {
69 $username = User
::whoIs( $params['userid'] );
71 if ( $username === false ) {
72 $this->dieWithError( [ 'apierror-nosuchuserid', $params['userid'] ], 'nosuchuserid' );
74 $params['user'] = $username;
79 'Target' => is_null( $params['id'] ) ?
$params['user'] : "#{$params['id']}",
80 'Reason' => $params['reason'],
81 'Tags' => $params['tags']
83 $block = DatabaseBlock
::newFromTarget( $data['Target'] );
84 $retval = SpecialUnblock
::processUnblock( $data, $this->getContext() );
85 if ( $retval !== true ) {
86 $this->dieStatus( $this->errorArrayToStatus( $retval ) );
89 $res['id'] = $block->getId();
90 $target = $block->getType() == DatabaseBlock
::TYPE_AUTO ?
'' : $block->getTarget();
91 $res['user'] = $target instanceof User ?
$target->getName() : $target;
92 $res['userid'] = $target instanceof User ?
$target->getId() : 0;
93 $res['reason'] = $params['reason'];
94 $this->getResult()->addValue( null, $this->getModuleName(), $res );
97 public function mustBePosted() {
101 public function isWriteMode() {
105 public function getAllowedParams() {
108 ApiBase
::PARAM_TYPE
=> 'integer',
112 ApiBase
::PARAM_TYPE
=> 'integer'
116 ApiBase
::PARAM_TYPE
=> 'tags',
117 ApiBase
::PARAM_ISMULTI
=> true,
122 public function needsToken() {
126 protected function getExamplesMessages() {
128 'action=unblock&id=105'
129 => 'apihelp-unblock-example-id',
130 'action=unblock&user=Bob&reason=Sorry%20Bob'
131 => 'apihelp-unblock-example-user',
135 public function getHelpUrls() {
136 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Block';