MediaWiki  REL1_19
ApiBlock.php
Go to the documentation of this file.
00001 <?php
00033 class ApiBlock extends ApiBase {
00034 
00035         public function __construct( $main, $action ) {
00036                 parent::__construct( $main, $action );
00037         }
00038 
00045         public function execute() {
00046                 $user = $this->getUser();
00047                 $params = $this->extractRequestParams();
00048 
00049                 if ( $params['gettoken'] ) {
00050                         // If we're in JSON callback mode, no tokens can be obtained
00051                         if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
00052                                 $this->dieUsage( 'Cannot get token when using a callback', 'aborted' );
00053                         }
00054                         $res['blocktoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
00055                         $this->getResult()->addValue( null, $this->getModuleName(), $res );
00056                         return;
00057                 }
00058 
00059                 if ( !$user->isAllowed( 'block' ) ) {
00060                         $this->dieUsageMsg( 'cantblock' );
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( array( $status ) );
00067                         }
00068                 }
00069                 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
00070                         $this->dieUsageMsg( 'canthide' );
00071                 }
00072                 if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
00073                         $this->dieUsageMsg( 'cantblock-email' );
00074                 }
00075 
00076                 $data = array(
00077                         'Target' => $params['user'],
00078                         'Reason' => array(
00079                                 is_null( $params['reason'] ) ? '' : $params['reason'],
00080                                 'other',
00081                                 is_null( $params['reason'] ) ? '' : $params['reason']
00082                         ),
00083                         'Expiry' => $params['expiry'] == 'never' ? 'infinite' : $params['expiry'],
00084                         'HardBlock' => !$params['anononly'],
00085                         'CreateAccount' => $params['nocreate'],
00086                         'AutoBlock' => $params['autoblock'],
00087                         'DisableEmail' => $params['noemail'],
00088                         'HideUser' => $params['hidename'],
00089                         'DisableUTEdit' => !$params['allowusertalk'],
00090                         'AlreadyBlocked' => $params['reblock'],
00091                         'Watch' => $params['watchuser'],
00092                         'Confirm' => true,
00093                 );
00094 
00095                 $retval = SpecialBlock::processForm( $data, $this->getContext() );
00096                 if ( $retval !== true ) {
00097                         // We don't care about multiple errors, just report one of them
00098                         $this->dieUsageMsg( $retval );
00099                 }
00100 
00101                 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
00102                 $res['user'] = $params['user'];
00103                 $res['userID'] = $target instanceof User ? $target->getId() : 0;
00104 
00105                 $block = Block::newFromTarget( $target );
00106                 if( $block instanceof Block ){
00107                         $res['expiry'] = $block->mExpiry == wfGetDB( DB_SLAVE )->getInfinity()
00108                                 ? 'infinite'
00109                                 : wfTimestamp( TS_ISO_8601, $block->mExpiry );
00110                 } else {
00111                         # should be unreachable
00112                         $res['expiry'] = '';
00113                 }
00114 
00115                 $res['reason'] = $params['reason'];
00116                 if ( $params['anononly'] ) {
00117                         $res['anononly'] = '';
00118                 }
00119                 if ( $params['nocreate'] ) {
00120                         $res['nocreate'] = '';
00121                 }
00122                 if ( $params['autoblock'] ) {
00123                         $res['autoblock'] = '';
00124                 }
00125                 if ( $params['noemail'] ) {
00126                         $res['noemail'] = '';
00127                 }
00128                 if ( $params['hidename'] ) {
00129                         $res['hidename'] = '';
00130                 }
00131                 if ( $params['allowusertalk'] ) {
00132                         $res['allowusertalk'] = '';
00133                 }
00134                 if ( $params['watchuser'] ) {
00135                         $res['watchuser'] = '';
00136                 }
00137 
00138                 $this->getResult()->addValue( null, $this->getModuleName(), $res );
00139         }
00140 
00141         public function mustBePosted() {
00142                 return true;
00143         }
00144 
00145         public function isWriteMode() {
00146                 return true;
00147         }
00148 
00149         public function getAllowedParams() {
00150                 return array(
00151                         'user' => array(
00152                                 ApiBase::PARAM_TYPE => 'string',
00153                                 ApiBase::PARAM_REQUIRED => true
00154                         ),
00155                         'token' => null,
00156                         'gettoken' => false,
00157                         'expiry' => 'never',
00158                         'reason' => null,
00159                         'anononly' => false,
00160                         'nocreate' => false,
00161                         'autoblock' => false,
00162                         'noemail' => false,
00163                         'hidename' => false,
00164                         'allowusertalk' => false,
00165                         'reblock' => false,
00166                         'watchuser' => false,
00167                 );
00168         }
00169 
00170         public function getParamDescription() {
00171                 return array(
00172                         'user' => 'Username, IP address or IP range you want to block',
00173                         'token' => 'A block token previously obtained through the gettoken parameter or prop=info',
00174                         'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
00175                         'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
00176                         'reason' => 'Reason for block (optional)',
00177                         'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
00178                         'nocreate' => 'Prevent account creation',
00179                         'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
00180                         'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
00181                         'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
00182                         'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
00183                         'reblock' => 'If the user is already blocked, overwrite the existing block',
00184                         'watchuser' => 'Watch the user/IP\'s user and talk pages',
00185                 );
00186         }
00187 
00188         public function getDescription() {
00189                 return 'Block a user';
00190         }
00191 
00192         public function getPossibleErrors() {
00193                 return array_merge( parent::getPossibleErrors(), array(
00194                         array( 'cantblock' ),
00195                         array( 'canthide' ),
00196                         array( 'cantblock-email' ),
00197                         array( 'ipbblocked' ),
00198                         array( 'ipbnounblockself' ),
00199                 ) );
00200         }
00201 
00202         public function needsToken() {
00203                 return true;
00204         }
00205 
00206         public function getTokenSalt() {
00207                 return '';
00208         }
00209 
00210         public function getExamples() {
00211                 return array(
00212                         'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
00213                         'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail='
00214                 );
00215         }
00216 
00217         public function getHelpUrls() {
00218                 return 'https://www.mediawiki.org/wiki/API:Block';
00219         }
00220 
00221         public function getVersion() {
00222                 return __CLASS__ . ': $Id$';
00223         }
00224 }