MediaWiki  REL1_20
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();
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                                 $params['reason'],
00080                                 'other',
00081                                 $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 == $this->getDB()->getInfinity()
00108                                 ? 'infinite'
00109                                 : wfTimestamp( TS_ISO_8601, $block->mExpiry );
00110                         $res['id'] = $block->getId();
00111                 } else {
00112                         # should be unreachable
00113                         $res['expiry'] = '';
00114                         $res['id'] = '';
00115                 }
00116 
00117                 $res['reason'] = $params['reason'];
00118                 if ( $params['anononly'] ) {
00119                         $res['anononly'] = '';
00120                 }
00121                 if ( $params['nocreate'] ) {
00122                         $res['nocreate'] = '';
00123                 }
00124                 if ( $params['autoblock'] ) {
00125                         $res['autoblock'] = '';
00126                 }
00127                 if ( $params['noemail'] ) {
00128                         $res['noemail'] = '';
00129                 }
00130                 if ( $params['hidename'] ) {
00131                         $res['hidename'] = '';
00132                 }
00133                 if ( $params['allowusertalk'] ) {
00134                         $res['allowusertalk'] = '';
00135                 }
00136                 if ( $params['watchuser'] ) {
00137                         $res['watchuser'] = '';
00138                 }
00139 
00140                 $this->getResult()->addValue( null, $this->getModuleName(), $res );
00141         }
00142 
00143         public function mustBePosted() {
00144                 return true;
00145         }
00146 
00147         public function isWriteMode() {
00148                 return true;
00149         }
00150 
00151         public function getAllowedParams() {
00152                 return array(
00153                         'user' => array(
00154                                 ApiBase::PARAM_TYPE => 'string',
00155                                 ApiBase::PARAM_REQUIRED => true
00156                         ),
00157                         'token' => null,
00158                         'gettoken' => array(
00159                                 ApiBase::PARAM_DFLT => false,
00160                                 ApiBase::PARAM_DEPRECATED => true,
00161                         ),
00162                         'expiry' => 'never',
00163                         'reason' => '',
00164                         'anononly' => false,
00165                         'nocreate' => false,
00166                         'autoblock' => false,
00167                         'noemail' => false,
00168                         'hidename' => false,
00169                         'allowusertalk' => false,
00170                         'reblock' => false,
00171                         'watchuser' => false,
00172                 );
00173         }
00174 
00175         public function getParamDescription() {
00176                 return array(
00177                         'user' => 'Username, IP address or IP range you want to block',
00178                         'token' => 'A block token previously obtained through prop=info',
00179                         'gettoken' => 'If set, a block token will be returned, and no other action will be taken',
00180                         'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
00181                         'reason' => 'Reason for block',
00182                         'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
00183                         'nocreate' => 'Prevent account creation',
00184                         'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',
00185                         'noemail' => 'Prevent user from sending e-mail through the wiki. (Requires the "blockemail" right.)',
00186                         'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
00187                         'allowusertalk' => 'Allow the user to edit their own talk page (depends on $wgBlockAllowsUTEdit)',
00188                         'reblock' => 'If the user is already blocked, overwrite the existing block',
00189                         'watchuser' => 'Watch the user/IP\'s user and talk pages',
00190                 );
00191         }
00192 
00193         public function getResultProperties() {
00194                 return array(
00195                         '' => array(
00196                                 'blocktoken' => array(
00197                                         ApiBase::PROP_TYPE => 'string',
00198                                         ApiBase::PROP_NULLABLE => true
00199                                 ),
00200                                 'user' => array(
00201                                         ApiBase::PROP_TYPE => 'string',
00202                                         ApiBase::PROP_NULLABLE => true
00203                                 ),
00204                                 'userID' => array(
00205                                         ApiBase::PROP_TYPE => 'integer',
00206                                         ApiBase::PROP_NULLABLE => true
00207                                 ),
00208                                 'expiry' => array(
00209                                         ApiBase::PROP_TYPE => 'string',
00210                                         ApiBase::PROP_NULLABLE => true
00211                                 ),
00212                                 'id' => array(
00213                                         ApiBase::PROP_TYPE => 'integer',
00214                                         ApiBase::PROP_NULLABLE => true
00215                                 ),
00216                                 'reason' => array(
00217                                         ApiBase::PROP_TYPE => 'string',
00218                                         ApiBase::PROP_NULLABLE => true
00219                                 ),
00220                                 'anononly' => 'boolean',
00221                                 'nocreate' => 'boolean',
00222                                 'autoblock' => 'boolean',
00223                                 'noemail' => 'boolean',
00224                                 'hidename' => 'boolean',
00225                                 'allowusertalk' => 'boolean',
00226                                 'watchuser' => 'boolean'
00227                         )
00228                 );
00229         }
00230 
00231         public function getDescription() {
00232                 return 'Block a user';
00233         }
00234 
00235         public function getPossibleErrors() {
00236                 return array_merge( parent::getPossibleErrors(), array(
00237                         array( 'cantblock' ),
00238                         array( 'canthide' ),
00239                         array( 'cantblock-email' ),
00240                         array( 'ipbblocked' ),
00241                         array( 'ipbnounblockself' ),
00242                 ) );
00243         }
00244 
00245         public function needsToken() {
00246                 return true;
00247         }
00248 
00249         public function getTokenSalt() {
00250                 return '';
00251         }
00252 
00253         public function getExamples() {
00254                 return array(
00255                         'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',
00256                         'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail='
00257                 );
00258         }
00259 
00260         public function getHelpUrls() {
00261                 return 'https://www.mediawiki.org/wiki/API:Block';
00262         }
00263 
00264         public function getVersion() {
00265                 return __CLASS__ . ': $Id$';
00266         }
00267 }