MediaWiki
master
|
00001 <?php 00033 class ApiUnblock extends ApiBase { 00034 00035 public function __construct( $main, $action ) { 00036 parent::__construct( $main, $action ); 00037 } 00038 00042 public function execute() { 00043 $user = $this->getUser(); 00044 $params = $this->extractRequestParams(); 00045 00046 if ( $params['gettoken'] ) { 00047 $res['unblocktoken'] = $user->getEditToken(); 00048 $this->getResult()->addValue( null, $this->getModuleName(), $res ); 00049 return; 00050 } 00051 00052 if ( is_null( $params['id'] ) && is_null( $params['user'] ) ) { 00053 $this->dieUsageMsg( 'unblock-notarget' ); 00054 } 00055 if ( !is_null( $params['id'] ) && !is_null( $params['user'] ) ) { 00056 $this->dieUsageMsg( 'unblock-idanduser' ); 00057 } 00058 00059 if ( !$user->isAllowed( 'block' ) ) { 00060 $this->dieUsageMsg( 'cantunblock' ); 00061 } 00062 # bug 15810: blocked admins should have limited access here 00063 if ( $user->isBlocked() ) { 00064 $status = SpecialBlock::checkUnblockSelf( $params['user'], $user ); 00065 if ( $status !== true ) { 00066 $this->dieUsageMsg( $status ); 00067 } 00068 } 00069 00070 $data = array( 00071 'Target' => is_null( $params['id'] ) ? $params['user'] : "#{$params['id']}", 00072 'Reason' => $params['reason'] 00073 ); 00074 $block = Block::newFromTarget( $data['Target'] ); 00075 $retval = SpecialUnblock::processUnblock( $data, $this->getContext() ); 00076 if ( $retval !== true ) { 00077 $this->dieUsageMsg( $retval[0] ); 00078 } 00079 00080 $res['id'] = $block->getId(); 00081 $target = $block->getType() == Block::TYPE_AUTO ? '' : $block->getTarget(); 00082 $res['user'] = $target; 00083 $res['userid'] = $target instanceof User ? $target->getId() : 0; 00084 $res['reason'] = $params['reason']; 00085 $this->getResult()->addValue( null, $this->getModuleName(), $res ); 00086 } 00087 00088 public function mustBePosted() { 00089 return true; 00090 } 00091 00092 public function isWriteMode() { 00093 return true; 00094 } 00095 00096 public function getAllowedParams() { 00097 return array( 00098 'id' => array( 00099 ApiBase::PARAM_TYPE => 'integer', 00100 ), 00101 'user' => null, 00102 'token' => null, 00103 'gettoken' => array( 00104 ApiBase::PARAM_DFLT => false, 00105 ApiBase::PARAM_DEPRECATED => true, 00106 ), 00107 'reason' => '', 00108 ); 00109 } 00110 00111 public function getParamDescription() { 00112 $p = $this->getModulePrefix(); 00113 return array( 00114 'id' => "ID of the block you want to unblock (obtained through list=blocks). Cannot be used together with {$p}user", 00115 'user' => "Username, IP address or IP range you want to unblock. Cannot be used together with {$p}id", 00116 'token' => "An unblock token previously obtained through prop=info", 00117 'gettoken' => 'If set, an unblock token will be returned, and no other action will be taken', 00118 'reason' => 'Reason for unblock', 00119 ); 00120 } 00121 00122 public function getResultProperties() { 00123 return array( 00124 '' => array( 00125 'unblocktoken' => array( 00126 ApiBase::PROP_TYPE => 'string', 00127 ApiBase::PROP_NULLABLE => true 00128 ), 00129 'id' => array( 00130 ApiBase::PROP_TYPE => 'integer', 00131 ApiBase::PROP_NULLABLE => true 00132 ), 00133 'user' => array( 00134 ApiBase::PROP_TYPE => 'string', 00135 ApiBase::PROP_NULLABLE => true 00136 ), 00137 'userid' => array( 00138 ApiBase::PROP_TYPE => 'integer', 00139 ApiBase::PROP_NULLABLE => true 00140 ), 00141 'reason' => array( 00142 ApiBase::PROP_TYPE => 'string', 00143 ApiBase::PROP_NULLABLE => true 00144 ) 00145 ) 00146 ); 00147 } 00148 00149 public function getDescription() { 00150 return 'Unblock a user'; 00151 } 00152 00153 public function getPossibleErrors() { 00154 return array_merge( parent::getPossibleErrors(), array( 00155 array( 'unblock-notarget' ), 00156 array( 'unblock-idanduser' ), 00157 array( 'cantunblock' ), 00158 array( 'ipbblocked' ), 00159 array( 'ipbnounblockself' ), 00160 ) ); 00161 } 00162 00163 public function needsToken() { 00164 return true; 00165 } 00166 00167 public function getTokenSalt() { 00168 return ''; 00169 } 00170 00171 public function getExamples() { 00172 return array( 00173 'api.php?action=unblock&id=105', 00174 'api.php?action=unblock&user=Bob&reason=Sorry%20Bob' 00175 ); 00176 } 00177 00178 public function getHelpUrls() { 00179 return 'https://www.mediawiki.org/wiki/API:Block'; 00180 } 00181 00182 public function getVersion() { 00183 return __CLASS__ . ': $Id$'; 00184 } 00185 }