MediaWiki
REL1_19
|
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 // If we're in JSON callback mode, no tokens can be obtained 00048 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) { 00049 $this->dieUsage( 'Cannot get token when using a callback', 'aborted' ); 00050 } 00051 $res['unblocktoken'] = $user->getEditToken( '', $this->getMain()->getRequest() ); 00052 $this->getResult()->addValue( null, $this->getModuleName(), $res ); 00053 return; 00054 } 00055 00056 if ( is_null( $params['id'] ) && is_null( $params['user'] ) ) { 00057 $this->dieUsageMsg( 'unblock-notarget' ); 00058 } 00059 if ( !is_null( $params['id'] ) && !is_null( $params['user'] ) ) { 00060 $this->dieUsageMsg( 'unblock-idanduser' ); 00061 } 00062 00063 if ( !$user->isAllowed( 'block' ) ) { 00064 $this->dieUsageMsg( 'cantunblock' ); 00065 } 00066 # bug 15810: blocked admins should have limited access here 00067 if ( $user->isBlocked() ) { 00068 $status = SpecialBlock::checkUnblockSelf( $params['user'], $user ); 00069 if ( $status !== true ) { 00070 $this->dieUsageMsg( $status ); 00071 } 00072 } 00073 00074 $data = array( 00075 'Target' => is_null( $params['id'] ) ? $params['user'] : "#{$params['id']}", 00076 'Reason' => is_null( $params['reason'] ) ? '' : $params['reason'] 00077 ); 00078 $block = Block::newFromTarget( $data['Target'] ); 00079 $retval = SpecialUnblock::processUnblock( $data, $this->getContext() ); 00080 if ( $retval !== true ) { 00081 $this->dieUsageMsg( $retval[0] ); 00082 } 00083 00084 $res['id'] = $block->getId(); 00085 $target = $block->getType() == Block::TYPE_AUTO ? '' : $block->getTarget(); 00086 $res['user'] = $target instanceof User ? $target->getName() : $target; 00087 $res['reason'] = $params['reason']; 00088 $this->getResult()->addValue( null, $this->getModuleName(), $res ); 00089 } 00090 00091 public function mustBePosted() { 00092 return true; 00093 } 00094 00095 public function isWriteMode() { 00096 return true; 00097 } 00098 00099 public function getAllowedParams() { 00100 return array( 00101 'id' => array( 00102 ApiBase::PARAM_TYPE => 'integer', 00103 ), 00104 'user' => null, 00105 'token' => null, 00106 'gettoken' => false, 00107 'reason' => null, 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 the gettoken parameter or {$p}prop=info", 00117 'gettoken' => 'If set, an unblock token will be returned, and no other action will be taken', 00118 'reason' => 'Reason for unblock (optional)', 00119 ); 00120 } 00121 00122 public function getDescription() { 00123 return 'Unblock a user'; 00124 } 00125 00126 public function getPossibleErrors() { 00127 return array_merge( parent::getPossibleErrors(), array( 00128 array( 'unblock-notarget' ), 00129 array( 'unblock-idanduser' ), 00130 array( 'cantunblock' ), 00131 array( 'ipbblocked' ), 00132 array( 'ipbnounblockself' ), 00133 ) ); 00134 } 00135 00136 public function needsToken() { 00137 return true; 00138 } 00139 00140 public function getTokenSalt() { 00141 return ''; 00142 } 00143 00144 public function getExamples() { 00145 return array( 00146 'api.php?action=unblock&id=105', 00147 'api.php?action=unblock&user=Bob&reason=Sorry%20Bob' 00148 ); 00149 } 00150 00151 public function getHelpUrls() { 00152 return 'https://www.mediawiki.org/wiki/API:Block'; 00153 } 00154 00155 public function getVersion() { 00156 return __CLASS__ . ': $Id$'; 00157 } 00158 }