MediaWiki  master
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                         $res['blocktoken'] = $user->getEditToken();
00051                         $this->getResult()->addValue( null, $this->getModuleName(), $res );
00052                         return;
00053                 }
00054 
00055                 if ( !$user->isAllowed( 'block' ) ) {
00056                         $this->dieUsageMsg( 'cantblock' );
00057                 }
00058                 # bug 15810: blocked admins should have limited access here
00059                 if ( $user->isBlocked() ) {
00060                         $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
00061                         if ( $status !== true ) {
00062                                 $this->dieUsageMsg( array( $status ) );
00063                         }
00064                 }
00065                 if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
00066                         $this->dieUsageMsg( 'canthide' );
00067                 }
00068                 if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
00069                         $this->dieUsageMsg( 'cantblock-email' );
00070                 }
00071 
00072                 $data = array(
00073                         'Target' => $params['user'],
00074                         'Reason' => array(
00075                                 $params['reason'],
00076                                 'other',
00077                                 $params['reason']
00078                         ),
00079                         'Expiry' => $params['expiry'] == 'never' ? 'infinite' : $params['expiry'],
00080                         'HardBlock' => !$params['anononly'],
00081                         'CreateAccount' => $params['nocreate'],
00082                         'AutoBlock' => $params['autoblock'],
00083                         'DisableEmail' => $params['noemail'],
00084                         'HideUser' => $params['hidename'],
00085                         'DisableUTEdit' => !$params['allowusertalk'],
00086                         'AlreadyBlocked' => $params['reblock'],
00087                         'Watch' => $params['watchuser'],
00088                         'Confirm' => true,
00089                 );
00090 
00091                 $retval = SpecialBlock::processForm( $data, $this->getContext() );
00092                 if ( $retval !== true ) {
00093                         // We don't care about multiple errors, just report one of them
00094                         $this->dieUsageMsg( $retval );
00095                 }
00096 
00097                 list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
00098                 $res['user'] = $params['user'];
00099                 $res['userID'] = $target instanceof User ? $target->getId() : 0;
00100 
00101                 $block = Block::newFromTarget( $target );
00102                 if( $block instanceof Block ){
00103                         $res['expiry'] = $block->mExpiry == $this->getDB()->getInfinity()
00104                                 ? 'infinite'
00105                                 : wfTimestamp( TS_ISO_8601, $block->mExpiry );
00106                         $res['id'] = $block->getId();
00107                 } else {
00108                         # should be unreachable
00109                         $res['expiry'] = '';
00110                         $res['id'] = '';
00111                 }
00112 
00113                 $res['reason'] = $params['reason'];
00114                 if ( $params['anononly'] ) {
00115                         $res['anononly'] = '';
00116                 }
00117                 if ( $params['nocreate'] ) {
00118                         $res['nocreate'] = '';
00119                 }
00120                 if ( $params['autoblock'] ) {
00121                         $res['autoblock'] = '';
00122                 }
00123                 if ( $params['noemail'] ) {
00124                         $res['noemail'] = '';
00125                 }
00126                 if ( $params['hidename'] ) {
00127                         $res['hidename'] = '';
00128                 }
00129                 if ( $params['allowusertalk'] ) {
00130                         $res['allowusertalk'] = '';
00131                 }
00132                 if ( $params['watchuser'] ) {
00133                         $res['watchuser'] = '';
00134                 }
00135 
00136                 $this->getResult()->addValue( null, $this->getModuleName(), $res );
00137         }
00138 
00139         public function mustBePosted() {
00140                 return true;
00141         }
00142 
00143         public function isWriteMode() {
00144                 return true;
00145         }
00146 
00147         public function getAllowedParams() {
00148                 return array(
00149                         'user' => array(
00150                                 ApiBase::PARAM_TYPE => 'string',
00151                                 ApiBase::PARAM_REQUIRED => true
00152                         ),
00153                         'token' => null,
00154                         'gettoken' => array(
00155                                 ApiBase::PARAM_DFLT => false,
00156                                 ApiBase::PARAM_DEPRECATED => true,
00157                         ),
00158                         'expiry' => 'never',
00159                         'reason' => '',
00160                         'anononly' => false,
00161                         'nocreate' => false,
00162                         'autoblock' => false,
00163                         'noemail' => false,
00164                         'hidename' => false,
00165                         'allowusertalk' => false,
00166                         'reblock' => false,
00167                         'watchuser' => false,
00168                 );
00169         }
00170 
00171         public function getParamDescription() {
00172                 return array(
00173                         'user' => 'Username, IP address or IP range you want to block',
00174                         'token' => 'A block token previously obtained through prop=info',
00175                         'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
00176                         'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
00177                         'reason' => 'Reason for block',
00178                         'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
00179                         'nocreate' => 'Prevent account creation',
00180                         'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
00181                         'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
00182                         'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
00183                         'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
00184                         'reblock' => 'If the user is already blocked, overwrite the existing block',
00185                         'watchuser' => 'Watch the user/IP\'s user and talk pages',
00186                 );
00187         }
00188 
00189         public function getResultProperties() {
00190                 return array(
00191                         '' => array(
00192                                 'blocktoken' => array(
00193                                         ApiBase::PROP_TYPE => 'string',
00194                                         ApiBase::PROP_NULLABLE => true
00195                                 ),
00196                                 'user' => array(
00197                                         ApiBase::PROP_TYPE => 'string',
00198                                         ApiBase::PROP_NULLABLE => true
00199                                 ),
00200                                 'userID' => array(
00201                                         ApiBase::PROP_TYPE => 'integer',
00202                                         ApiBase::PROP_NULLABLE => true
00203                                 ),
00204                                 'expiry' => array(
00205                                         ApiBase::PROP_TYPE => 'string',
00206                                         ApiBase::PROP_NULLABLE => true
00207                                 ),
00208                                 'id' => array(
00209                                         ApiBase::PROP_TYPE => 'integer',
00210                                         ApiBase::PROP_NULLABLE => true
00211                                 ),
00212                                 'reason' => array(
00213                                         ApiBase::PROP_TYPE => 'string',
00214                                         ApiBase::PROP_NULLABLE => true
00215                                 ),
00216                                 'anononly' => 'boolean',
00217                                 'nocreate' => 'boolean',
00218                                 'autoblock' => 'boolean',
00219                                 'noemail' => 'boolean',
00220                                 'hidename' => 'boolean',
00221                                 'allowusertalk' => 'boolean',
00222                                 'watchuser' => 'boolean'
00223                         )
00224                 );
00225         }
00226 
00227         public function getDescription() {
00228                 return 'Block a user';
00229         }
00230 
00231         public function getPossibleErrors() {
00232                 return array_merge( parent::getPossibleErrors(), array(
00233                         array( 'cantblock' ),
00234                         array( 'canthide' ),
00235                         array( 'cantblock-email' ),
00236                         array( 'ipbblocked' ),
00237                         array( 'ipbnounblockself' ),
00238                 ) );
00239         }
00240 
00241         public function needsToken() {
00242                 return true;
00243         }
00244 
00245         public function getTokenSalt() {
00246                 return '';
00247         }
00248 
00249         public function getExamples() {
00250                 return array(
00251                         'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
00252                         'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail='
00253                 );
00254         }
00255 
00256         public function getHelpUrls() {
00257                 return 'https://www.mediawiki.org/wiki/API:Block';
00258         }
00259 
00260         public function getVersion() {
00261                 return __CLASS__ . ': $Id$';
00262         }
00263 }