MediaWiki  REL1_24
ApiBlock.php
Go to the documentation of this file.
00001 <?php
00033 class ApiBlock extends ApiBase {
00034 
00041     public function execute() {
00042         $user = $this->getUser();
00043         $params = $this->extractRequestParams();
00044 
00045         if ( !$user->isAllowed( 'block' ) ) {
00046             $this->dieUsageMsg( 'cantblock' );
00047         }
00048 
00049         # bug 15810: blocked admins should have limited access here
00050         if ( $user->isBlocked() ) {
00051             $status = SpecialBlock::checkUnblockSelf( $params['user'], $user );
00052             if ( $status !== true ) {
00053                 $this->dieUsageMsg( array( $status ) );
00054             }
00055         }
00056 
00057         $target = User::newFromName( $params['user'] );
00058         // Bug 38633 - if the target is a user (not an IP address), but it
00059         // doesn't exist or is unusable, error.
00060         if ( $target instanceof User &&
00061             ( $target->isAnon() /* doesn't exist */ || !User::isUsableName( $target->getName() ) )
00062         ) {
00063             $this->dieUsageMsg( array( 'nosuchuser', $params['user'] ) );
00064         }
00065 
00066         if ( $params['hidename'] && !$user->isAllowed( 'hideuser' ) ) {
00067             $this->dieUsageMsg( 'canthide' );
00068         }
00069         if ( $params['noemail'] && !SpecialBlock::canBlockEmail( $user ) ) {
00070             $this->dieUsageMsg( 'cantblock-email' );
00071         }
00072 
00073         $data = array(
00074             'PreviousTarget' => $params['user'],
00075             'Target' => $params['user'],
00076             'Reason' => array(
00077                 $params['reason'],
00078                 'other',
00079                 $params['reason']
00080             ),
00081             'Expiry' => $params['expiry'] == 'never' ? 'infinite' : $params['expiry'],
00082             'HardBlock' => !$params['anononly'],
00083             'CreateAccount' => $params['nocreate'],
00084             'AutoBlock' => $params['autoblock'],
00085             'DisableEmail' => $params['noemail'],
00086             'HideUser' => $params['hidename'],
00087             'DisableUTEdit' => !$params['allowusertalk'],
00088             'Reblock' => $params['reblock'],
00089             'Watch' => $params['watchuser'],
00090             'Confirm' => true,
00091         );
00092 
00093         $retval = SpecialBlock::processForm( $data, $this->getContext() );
00094         if ( $retval !== true ) {
00095             // We don't care about multiple errors, just report one of them
00096             $this->dieUsageMsg( $retval );
00097         }
00098 
00099         list( $target, /*...*/ ) = SpecialBlock::getTargetAndType( $params['user'] );
00100         $res['user'] = $params['user'];
00101         $res['userID'] = $target instanceof User ? $target->getId() : 0;
00102 
00103         $block = Block::newFromTarget( $target );
00104         if ( $block instanceof Block ) {
00105             $res['expiry'] = $block->mExpiry == $this->getDB()->getInfinity()
00106                 ? 'infinite'
00107                 : wfTimestamp( TS_ISO_8601, $block->mExpiry );
00108             $res['id'] = $block->getId();
00109         } else {
00110             # should be unreachable
00111             $res['expiry'] = '';
00112             $res['id'] = '';
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             'expiry' => 'never',
00156             'reason' => '',
00157             'anononly' => false,
00158             'nocreate' => false,
00159             'autoblock' => false,
00160             'noemail' => false,
00161             'hidename' => false,
00162             'allowusertalk' => false,
00163             'reblock' => false,
00164             'watchuser' => false,
00165         );
00166     }
00167 
00168     public function getParamDescription() {
00169         return array(
00170             'user' => 'Username, IP address or IP range you want to block',
00171             'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. ' .
00172                 'If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',
00173             'reason' => 'Reason for block',
00174             'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',
00175             'nocreate' => 'Prevent account creation',
00176             'autoblock' => 'Automatically block the last used IP address, and ' .
00177                 'any subsequent IP addresses they try to login from',
00178             'noemail'
00179                 => 'Prevent user from sending email through the wiki. (Requires the "blockemail" right.)',
00180             'hidename' => 'Hide the username from the block log. (Requires the "hideuser" right.)',
00181             'allowusertalk'
00182                 => '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 needsToken() {
00193         return 'csrf';
00194     }
00195 
00196     public function getExamples() {
00197         return array(
00198             'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike&token=123ABC',
00199             'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate=&autoblock=&noemail=&token=123ABC'
00200         );
00201     }
00202 
00203     public function getHelpUrls() {
00204         return 'https://www.mediawiki.org/wiki/API:Block';
00205     }
00206 }