MediaWiki  REL1_20
ApiUnblock.php
Go to the documentation of this file.
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();
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' => $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['userid'] = $target instanceof User ? $target->getId() : 0;
00088                 $res['reason'] = $params['reason'];
00089                 $this->getResult()->addValue( null, $this->getModuleName(), $res );
00090         }
00091 
00092         public function mustBePosted() {
00093                 return true;
00094         }
00095 
00096         public function isWriteMode() {
00097                 return true;
00098         }
00099 
00100         public function getAllowedParams() {
00101                 return array(
00102                         'id' => array(
00103                                 ApiBase::PARAM_TYPE => 'integer',
00104                         ),
00105                         'user' => null,
00106                         'token' => null,
00107                         'gettoken' => array(
00108                                 ApiBase::PARAM_DFLT => false,
00109                                 ApiBase::PARAM_DEPRECATED => true,
00110                         ),
00111                         'reason' => '',
00112                 );
00113         }
00114 
00115         public function getParamDescription() {
00116                 $p = $this->getModulePrefix();
00117                 return array(
00118                         'id' => "ID of the block you want to unblock (obtained through list=blocks). Cannot be used together with {$p}user",
00119                         'user' => "Username, IP address or IP range you want to unblock. Cannot be used together with {$p}id",
00120                         'token' => "An unblock token previously obtained through prop=info",
00121                         'gettoken' => 'If set, an unblock token will be returned, and no other action will be taken',
00122                         'reason' => 'Reason for unblock',
00123                 );
00124         }
00125 
00126         public function getResultProperties() {
00127                 return array(
00128                         '' => array(
00129                                 'unblocktoken' => array(
00130                                         ApiBase::PROP_TYPE => 'string',
00131                                         ApiBase::PROP_NULLABLE => true
00132                                 ),
00133                                 'id' => array(
00134                                         ApiBase::PROP_TYPE => 'integer',
00135                                         ApiBase::PROP_NULLABLE => true
00136                                 ),
00137                                 'user' => array(
00138                                         ApiBase::PROP_TYPE => 'string',
00139                                         ApiBase::PROP_NULLABLE => true
00140                                 ),
00141                                 'userid' => array(
00142                                         ApiBase::PROP_TYPE => 'integer',
00143                                         ApiBase::PROP_NULLABLE => true
00144                                 ),
00145                                 'reason' => array(
00146                                         ApiBase::PROP_TYPE => 'string',
00147                                         ApiBase::PROP_NULLABLE => true
00148                                 )
00149                         )
00150                 );
00151         }
00152 
00153         public function getDescription() {
00154                 return 'Unblock a user';
00155         }
00156 
00157         public function getPossibleErrors() {
00158                 return array_merge( parent::getPossibleErrors(), array(
00159                         array( 'unblock-notarget' ),
00160                         array( 'unblock-idanduser' ),
00161                         array( 'cantunblock' ),
00162                         array( 'ipbblocked' ),
00163                         array( 'ipbnounblockself' ),
00164                 ) );
00165         }
00166 
00167         public function needsToken() {
00168                 return true;
00169         }
00170 
00171         public function getTokenSalt() {
00172                 return '';
00173         }
00174 
00175         public function getExamples() {
00176                 return array(
00177                         'api.php?action=unblock&id=105',
00178                         'api.php?action=unblock&user=Bob&reason=Sorry%20Bob'
00179                 );
00180         }
00181 
00182         public function getHelpUrls() {
00183                 return 'https://www.mediawiki.org/wiki/API:Block';
00184         }
00185 
00186         public function getVersion() {
00187                 return __CLASS__ . ': $Id$';
00188         }
00189 }