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