MediaWiki  REL1_22
ApiQueryBlocks.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryBlocks extends ApiQueryBase {
00033 
00037     protected $usernames;
00038 
00039     public function __construct( $query, $moduleName ) {
00040         parent::__construct( $query, $moduleName, 'bk' );
00041     }
00042 
00043     public function execute() {
00044         global $wgContLang;
00045 
00046         $params = $this->extractRequestParams();
00047         $this->requireMaxOneParameter( $params, 'users', 'ip' );
00048 
00049         $prop = array_flip( $params['prop'] );
00050         $fld_id = isset( $prop['id'] );
00051         $fld_user = isset( $prop['user'] );
00052         $fld_userid = isset( $prop['userid'] );
00053         $fld_by = isset( $prop['by'] );
00054         $fld_byid = isset( $prop['byid'] );
00055         $fld_timestamp = isset( $prop['timestamp'] );
00056         $fld_expiry = isset( $prop['expiry'] );
00057         $fld_reason = isset( $prop['reason'] );
00058         $fld_range = isset( $prop['range'] );
00059         $fld_flags = isset( $prop['flags'] );
00060 
00061         $result = $this->getResult();
00062 
00063         $this->addTables( 'ipblocks' );
00064         $this->addFields( 'ipb_auto' );
00065 
00066         $this->addFieldsIf( 'ipb_id', $fld_id );
00067         $this->addFieldsIf( array( 'ipb_address', 'ipb_user' ), $fld_user || $fld_userid );
00068         $this->addFieldsIf( 'ipb_by_text', $fld_by );
00069         $this->addFieldsIf( 'ipb_by', $fld_byid );
00070         $this->addFieldsIf( 'ipb_timestamp', $fld_timestamp );
00071         $this->addFieldsIf( 'ipb_expiry', $fld_expiry );
00072         $this->addFieldsIf( 'ipb_reason', $fld_reason );
00073         $this->addFieldsIf( array( 'ipb_range_start', 'ipb_range_end' ), $fld_range );
00074         $this->addFieldsIf( array( 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock',
00075                                     'ipb_block_email', 'ipb_deleted', 'ipb_allow_usertalk' ),
00076                             $fld_flags );
00077 
00078         $this->addOption( 'LIMIT', $params['limit'] + 1 );
00079         $this->addTimestampWhereRange( 'ipb_timestamp', $params['dir'], $params['start'], $params['end'] );
00080 
00081         $db = $this->getDB();
00082 
00083         if ( isset( $params['ids'] ) ) {
00084             $this->addWhereFld( 'ipb_id', $params['ids'] );
00085         }
00086         if ( isset( $params['users'] ) ) {
00087             foreach ( (array)$params['users'] as $u ) {
00088                 $this->prepareUsername( $u );
00089             }
00090             $this->addWhereFld( 'ipb_address', $this->usernames );
00091             $this->addWhereFld( 'ipb_auto', 0 );
00092         }
00093         if ( isset( $params['ip'] ) ) {
00094             global $wgBlockCIDRLimit;
00095             if ( IP::isIPv4( $params['ip'] ) ) {
00096                 $type = 'IPv4';
00097                 $cidrLimit = $wgBlockCIDRLimit['IPv4'];
00098                 $prefixLen = 0;
00099             } elseif ( IP::isIPv6( $params['ip'] ) ) {
00100                 $type = 'IPv6';
00101                 $cidrLimit = $wgBlockCIDRLimit['IPv6'];
00102                 $prefixLen = 3; // IP::toHex output is prefixed with "v6-"
00103             } else {
00104                 $this->dieUsage( 'IP parameter is not valid', 'param_ip' );
00105             }
00106 
00107             # Check range validity, if it's a CIDR
00108             list( $ip, $range ) = IP::parseCIDR( $params['ip'] );
00109             if ( $ip !== false && $range !== false && $range < $cidrLimit ) {
00110                 $this->dieUsage( "$type CIDR ranges broader than /$cidrLimit are not accepted", 'cidrtoobroad' );
00111             }
00112 
00113             # Let IP::parseRange handle calculating $upper, instead of duplicating the logic here.
00114             list( $lower, $upper ) = IP::parseRange( $params['ip'] );
00115 
00116             # Extract the common prefix to any rangeblock affecting this IP/CIDR
00117             $prefix = substr( $lower, 0, $prefixLen + floor( $cidrLimit / 4 ) );
00118 
00119             # Fairly hard to make a malicious SQL statement out of hex characters,
00120             # but it is good practice to add quotes
00121             $lower = $db->addQuotes( $lower );
00122             $upper = $db->addQuotes( $upper );
00123 
00124             $this->addWhere( array(
00125                 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
00126                 'ipb_range_start <= ' . $lower,
00127                 'ipb_range_end >= ' . $upper,
00128                 'ipb_auto' => 0
00129             ) );
00130         }
00131 
00132         if ( !is_null( $params['show'] ) ) {
00133             $show = array_flip( $params['show'] );
00134 
00135             /* Check for conflicting parameters. */
00136             if ( ( isset( $show['account'] ) && isset( $show['!account'] ) )
00137                     || ( isset( $show['ip'] ) && isset( $show['!ip'] ) )
00138                     || ( isset( $show['range'] ) && isset( $show['!range'] ) )
00139                     || ( isset( $show['temp'] ) && isset( $show['!temp'] ) )
00140             ) {
00141                 $this->dieUsageMsg( 'show' );
00142             }
00143 
00144             $this->addWhereIf( 'ipb_user = 0', isset( $show['!account'] ) );
00145             $this->addWhereIf( 'ipb_user != 0', isset( $show['account'] ) );
00146             $this->addWhereIf( 'ipb_user != 0 OR ipb_range_end > ipb_range_start', isset( $show['!ip'] ) );
00147             $this->addWhereIf( 'ipb_user = 0 AND ipb_range_end = ipb_range_start', isset( $show['ip'] ) );
00148             $this->addWhereIf( 'ipb_expiry = ' . $db->addQuotes( $db->getInfinity() ), isset( $show['!temp'] ) );
00149             $this->addWhereIf( 'ipb_expiry != ' . $db->addQuotes( $db->getInfinity() ), isset( $show['temp'] ) );
00150             $this->addWhereIf( 'ipb_range_end = ipb_range_start', isset( $show['!range'] ) );
00151             $this->addWhereIf( 'ipb_range_end > ipb_range_start', isset( $show['range'] ) );
00152         }
00153 
00154         if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
00155             $this->addWhereFld( 'ipb_deleted', 0 );
00156         }
00157 
00158         // Purge expired entries on one in every 10 queries
00159         if ( !mt_rand( 0, 10 ) ) {
00160             Block::purgeExpired();
00161         }
00162 
00163         $res = $this->select( __METHOD__ );
00164 
00165         $count = 0;
00166         foreach ( $res as $row ) {
00167             if ( ++$count > $params['limit'] ) {
00168                 // We've had enough
00169                 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
00170                 break;
00171             }
00172             $block = array();
00173             if ( $fld_id ) {
00174                 $block['id'] = $row->ipb_id;
00175             }
00176             if ( $fld_user && !$row->ipb_auto ) {
00177                 $block['user'] = $row->ipb_address;
00178             }
00179             if ( $fld_userid && !$row->ipb_auto ) {
00180                 $block['userid'] = $row->ipb_user;
00181             }
00182             if ( $fld_by ) {
00183                 $block['by'] = $row->ipb_by_text;
00184             }
00185             if ( $fld_byid ) {
00186                 $block['byid'] = $row->ipb_by;
00187             }
00188             if ( $fld_timestamp ) {
00189                 $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
00190             }
00191             if ( $fld_expiry ) {
00192                 $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry, TS_ISO_8601 );
00193             }
00194             if ( $fld_reason ) {
00195                 $block['reason'] = $row->ipb_reason;
00196             }
00197             if ( $fld_range && !$row->ipb_auto ) {
00198                 $block['rangestart'] = IP::formatHex( $row->ipb_range_start );
00199                 $block['rangeend'] = IP::formatHex( $row->ipb_range_end );
00200             }
00201             if ( $fld_flags ) {
00202                 // For clarity, these flags use the same names as their action=block counterparts
00203                 if ( $row->ipb_auto ) {
00204                     $block['automatic'] = '';
00205                 }
00206                 if ( $row->ipb_anon_only ) {
00207                     $block['anononly'] = '';
00208                 }
00209                 if ( $row->ipb_create_account ) {
00210                     $block['nocreate'] = '';
00211                 }
00212                 if ( $row->ipb_enable_autoblock ) {
00213                     $block['autoblock'] = '';
00214                 }
00215                 if ( $row->ipb_block_email ) {
00216                     $block['noemail'] = '';
00217                 }
00218                 if ( $row->ipb_deleted ) {
00219                     $block['hidden'] = '';
00220                 }
00221                 if ( $row->ipb_allow_usertalk ) {
00222                     $block['allowusertalk'] = '';
00223                 }
00224             }
00225             $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
00226             if ( !$fit ) {
00227                 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
00228                 break;
00229             }
00230         }
00231         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
00232     }
00233 
00234     protected function prepareUsername( $user ) {
00235         if ( !$user ) {
00236             $this->dieUsage( 'User parameter may not be empty', 'param_user' );
00237         }
00238         $name = User::isIP( $user )
00239             ? $user
00240             : User::getCanonicalName( $user, 'valid' );
00241         if ( $name === false ) {
00242             $this->dieUsage( "User name {$user} is not valid", 'param_user' );
00243         }
00244         $this->usernames[] = $name;
00245     }
00246 
00247     public function getAllowedParams() {
00248         return array(
00249             'start' => array(
00250                 ApiBase::PARAM_TYPE => 'timestamp'
00251             ),
00252             'end' => array(
00253                 ApiBase::PARAM_TYPE => 'timestamp',
00254             ),
00255             'dir' => array(
00256                 ApiBase::PARAM_TYPE => array(
00257                     'newer',
00258                     'older'
00259                 ),
00260                 ApiBase::PARAM_DFLT => 'older'
00261             ),
00262             'ids' => array(
00263                 ApiBase::PARAM_TYPE => 'integer',
00264                 ApiBase::PARAM_ISMULTI => true
00265             ),
00266             'users' => array(
00267                 ApiBase::PARAM_ISMULTI => true
00268             ),
00269             'ip' => null,
00270             'limit' => array(
00271                 ApiBase::PARAM_DFLT => 10,
00272                 ApiBase::PARAM_TYPE => 'limit',
00273                 ApiBase::PARAM_MIN => 1,
00274                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00275                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00276             ),
00277             'prop' => array(
00278                 ApiBase::PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
00279                 ApiBase::PARAM_TYPE => array(
00280                     'id',
00281                     'user',
00282                     'userid',
00283                     'by',
00284                     'byid',
00285                     'timestamp',
00286                     'expiry',
00287                     'reason',
00288                     'range',
00289                     'flags'
00290                 ),
00291                 ApiBase::PARAM_ISMULTI => true
00292             ),
00293             'show' => array(
00294                 ApiBase::PARAM_TYPE => array(
00295                     'account',
00296                     '!account',
00297                     'temp',
00298                     '!temp',
00299                     'ip',
00300                     '!ip',
00301                     'range',
00302                     '!range',
00303                 ),
00304                 ApiBase::PARAM_ISMULTI => true
00305             ),
00306         );
00307     }
00308 
00309     public function getParamDescription() {
00310         global $wgBlockCIDRLimit;
00311         $p = $this->getModulePrefix();
00312         return array(
00313             'start' => 'The timestamp to start enumerating from',
00314             'end' => 'The timestamp to stop enumerating at',
00315             'dir' => $this->getDirectionDescription( $p ),
00316             'ids' => 'List of block IDs to list (optional)',
00317             'users' => 'List of users to search for (optional)',
00318             'ip' => array(
00319                 'Get all blocks applying to this IP or CIDR range, including range blocks.',
00320                 "Cannot be used together with bkusers. CIDR ranges broader than " .
00321                     "IPv4/{$wgBlockCIDRLimit['IPv4']} or IPv6/{$wgBlockCIDRLimit['IPv6']} " .
00322                     "are not accepted"
00323             ),
00324             'limit' => 'The maximum amount of blocks to list',
00325             'prop' => array(
00326                 'Which properties to get',
00327                 ' id         - Adds the ID of the block',
00328                 ' user       - Adds the username of the blocked user',
00329                 ' userid     - Adds the user ID of the blocked user',
00330                 ' by         - Adds the username of the blocking user',
00331                 ' byid       - Adds the user ID of the blocking user',
00332                 ' timestamp  - Adds the timestamp of when the block was given',
00333                 ' expiry     - Adds the timestamp of when the block expires',
00334                 ' reason     - Adds the reason given for the block',
00335                 ' range      - Adds the range of IPs affected by the block',
00336                 ' flags      - Tags the ban with (autoblock, anononly, etc)',
00337             ),
00338             'show' => array(
00339                 'Show only items that meet this criteria.',
00340                 "For example, to see only indefinite blocks on IPs, set {$p}show=ip|!temp"
00341             ),
00342         );
00343     }
00344 
00345     public function getResultProperties() {
00346         return array(
00347             'id' => array(
00348                 'id' => 'integer'
00349             ),
00350             'user' => array(
00351                 'user' => array(
00352                     ApiBase::PROP_TYPE => 'string',
00353                     ApiBase::PROP_NULLABLE => true
00354                 )
00355             ),
00356             'userid' => array(
00357                 'userid' => array(
00358                     ApiBase::PROP_TYPE => 'integer',
00359                     ApiBase::PROP_NULLABLE => true
00360                 )
00361             ),
00362             'by' => array(
00363                 'by' => 'string'
00364             ),
00365             'byid' => array(
00366                 'byid' => 'integer'
00367             ),
00368             'timestamp' => array(
00369                 'timestamp' => 'timestamp'
00370             ),
00371             'expiry' => array(
00372                 'expiry' => 'timestamp'
00373             ),
00374             'reason' => array(
00375                 'reason' => 'string'
00376             ),
00377             'range' => array(
00378                 'rangestart' => array(
00379                     ApiBase::PROP_TYPE => 'string',
00380                     ApiBase::PROP_NULLABLE => true
00381                 ),
00382                 'rangeend' => array(
00383                     ApiBase::PROP_TYPE => 'string',
00384                     ApiBase::PROP_NULLABLE => true
00385                 )
00386             ),
00387             'flags' => array(
00388                 'automatic' => 'boolean',
00389                 'anononly' => 'boolean',
00390                 'nocreate' => 'boolean',
00391                 'autoblock' => 'boolean',
00392                 'noemail' => 'boolean',
00393                 'hidden' => 'boolean',
00394                 'allowusertalk' => 'boolean'
00395             )
00396         );
00397     }
00398 
00399     public function getDescription() {
00400         return 'List all blocked users and IP addresses';
00401     }
00402 
00403     public function getPossibleErrors() {
00404         global $wgBlockCIDRLimit;
00405         return array_merge( parent::getPossibleErrors(),
00406             $this->getRequireOnlyOneParameterErrorMessages( array( 'users', 'ip' ) ),
00407             array(
00408                 array(
00409                     'code' => 'cidrtoobroad',
00410                     'info' => "IPv4 CIDR ranges broader than /{$wgBlockCIDRLimit['IPv4']} are not accepted"
00411                 ),
00412                 array(
00413                     'code' => 'cidrtoobroad',
00414                     'info' => "IPv6 CIDR ranges broader than /{$wgBlockCIDRLimit['IPv6']} are not accepted"
00415                 ),
00416                 array( 'code' => 'param_ip', 'info' => 'IP parameter is not valid' ),
00417                 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
00418                 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
00419                 array( 'show' ),
00420             )
00421         );
00422     }
00423 
00424     public function getExamples() {
00425         return array(
00426             'api.php?action=query&list=blocks',
00427             'api.php?action=query&list=blocks&bkusers=Alice|Bob'
00428         );
00429     }
00430 
00431     public function getHelpUrls() {
00432         return 'https://www.mediawiki.org/wiki/API:Blocks';
00433     }
00434 }