MediaWiki  REL1_19
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                 if ( isset( $params['ids'] ) ) {
00081                         $this->addWhereFld( 'ipb_id', $params['ids'] );
00082                 }
00083                 if ( isset( $params['users'] ) ) {
00084                         foreach ( (array)$params['users'] as $u ) {
00085                                 $this->prepareUsername( $u );
00086                         }
00087                         $this->addWhereFld( 'ipb_address', $this->usernames );
00088                         $this->addWhereFld( 'ipb_auto', 0 );
00089                 }
00090                 $db = $this->getDB();
00091                 if ( isset( $params['ip'] ) ) {
00092                         list( $ip, $range ) = IP::parseCIDR( $params['ip'] );
00093                         if ( $ip && $range ) {
00094                                 // We got a CIDR range
00095                                 if ( $range < 16 )
00096                                         $this->dieUsage( 'CIDR ranges broader than /16 are not accepted', 'cidrtoobroad' );
00097                                 $lower = wfBaseConvert( $ip, 10, 16, 8, false );
00098                                 $upper = wfBaseConvert( $ip + pow( 2, 32 - $range ) - 1, 10, 16, 8, false );
00099                         } else {
00100                                 $lower = $upper = IP::toHex( $params['ip'] );
00101                         }
00102                         $prefix = substr( $lower, 0, 4 );
00103 
00104                         $this->addWhere( array(
00105                                 'ipb_range_start' . $db->buildLike( $prefix, $db->anyString() ),
00106                                 "ipb_range_start <= '$lower'",
00107                                 "ipb_range_end >= '$upper'",
00108                                 'ipb_auto' => 0
00109                         ) );
00110                 }
00111 
00112                 if ( !is_null( $params['show'] ) ) {
00113                         $show = array_flip( $params['show'] );
00114 
00115                         /* Check for conflicting parameters. */
00116                         if ( ( isset ( $show['account'] ) && isset ( $show['!account'] ) )
00117                                         || ( isset ( $show['ip'] ) && isset ( $show['!ip'] ) )
00118                                         || ( isset ( $show['range'] ) && isset ( $show['!range'] ) )
00119                                         || ( isset ( $show['temp'] ) && isset ( $show['!temp'] ) )
00120                         ) {
00121                                 $this->dieUsageMsg( 'show' );
00122                         }
00123 
00124                         $this->addWhereIf( 'ipb_user = 0', isset( $show['!account'] ) );
00125                         $this->addWhereIf( 'ipb_user != 0', isset( $show['account'] ) );
00126                         $this->addWhereIf( 'ipb_user != 0 OR ipb_range_end > ipb_range_start', isset( $show['!ip'] ) );
00127                         $this->addWhereIf( 'ipb_user = 0 AND ipb_range_end = ipb_range_start', isset( $show['ip'] ) );
00128                         $this->addWhereIf( 'ipb_expiry =  '.$db->addQuotes($db->getInfinity()), isset( $show['!temp'] ) );
00129                         $this->addWhereIf( 'ipb_expiry != '.$db->addQuotes($db->getInfinity()), isset( $show['temp'] ) );
00130                         $this->addWhereIf( "ipb_range_end = ipb_range_start", isset( $show['!range'] ) );
00131                         $this->addWhereIf( "ipb_range_end > ipb_range_start", isset( $show['range'] ) );
00132                 }
00133 
00134                 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
00135                         $this->addWhereFld( 'ipb_deleted', 0 );
00136                 }
00137 
00138                 // Purge expired entries on one in every 10 queries
00139                 if ( !mt_rand( 0, 10 ) ) {
00140                         Block::purgeExpired();
00141                 }
00142 
00143                 $res = $this->select( __METHOD__ );
00144 
00145                 $count = 0;
00146                 foreach ( $res as $row ) {
00147                         if ( ++$count > $params['limit'] ) {
00148                                 // We've had enough
00149                                 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
00150                                 break;
00151                         }
00152                         $block = array();
00153                         if ( $fld_id ) {
00154                                 $block['id'] = $row->ipb_id;
00155                         }
00156                         if ( $fld_user && !$row->ipb_auto ) {
00157                                 $block['user'] = $row->ipb_address;
00158                         }
00159                         if ( $fld_userid && !$row->ipb_auto ) {
00160                                 $block['userid'] = $row->ipb_user;
00161                         }
00162                         if ( $fld_by ) {
00163                                 $block['by'] = $row->ipb_by_text;
00164                         }
00165                         if ( $fld_byid ) {
00166                                 $block['byid'] = $row->ipb_by;
00167                         }
00168                         if ( $fld_timestamp ) {
00169                                 $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp );
00170                         }
00171                         if ( $fld_expiry ) {
00172                                 $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry, TS_ISO_8601 );
00173                         }
00174                         if ( $fld_reason ) {
00175                                 $block['reason'] = $row->ipb_reason;
00176                         }
00177                         if ( $fld_range && !$row->ipb_auto ) {
00178                                 $block['rangestart'] = IP::hexToQuad( $row->ipb_range_start );
00179                                 $block['rangeend'] = IP::hexToQuad( $row->ipb_range_end );
00180                         }
00181                         if ( $fld_flags ) {
00182                                 // For clarity, these flags use the same names as their action=block counterparts
00183                                 if ( $row->ipb_auto ) {
00184                                         $block['automatic'] = '';
00185                                 }
00186                                 if ( $row->ipb_anon_only ) {
00187                                         $block['anononly'] = '';
00188                                 }
00189                                 if ( $row->ipb_create_account ) {
00190                                         $block['nocreate'] = '';
00191                                 }
00192                                 if ( $row->ipb_enable_autoblock ) {
00193                                         $block['autoblock'] = '';
00194                                 }
00195                                 if ( $row->ipb_block_email ) {
00196                                         $block['noemail'] = '';
00197                                 }
00198                                 if ( $row->ipb_deleted ) {
00199                                         $block['hidden'] = '';
00200                                 }
00201                                 if ( $row->ipb_allow_usertalk ) {
00202                                         $block['allowusertalk'] = '';
00203                                 }
00204                         }
00205                         $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $block );
00206                         if ( !$fit ) {
00207                                 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ) );
00208                                 break;
00209                         }
00210                 }
00211                 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'block' );
00212         }
00213 
00214         protected function prepareUsername( $user ) {
00215                 if ( !$user ) {
00216                         $this->dieUsage( 'User parameter may not be empty', 'param_user' );
00217                 }
00218                 $name = User::isIP( $user )
00219                         ? $user
00220                         : User::getCanonicalName( $user, 'valid' );
00221                 if ( $name === false ) {
00222                         $this->dieUsage( "User name {$user} is not valid", 'param_user' );
00223                 }
00224                 $this->usernames[] = $name;
00225         }
00226 
00227         public function getAllowedParams() {
00228                 return array(
00229                         'start' => array(
00230                                 ApiBase::PARAM_TYPE => 'timestamp'
00231                         ),
00232                         'end' => array(
00233                                 ApiBase::PARAM_TYPE => 'timestamp',
00234                         ),
00235                         'dir' => array(
00236                                 ApiBase::PARAM_TYPE => array(
00237                                         'newer',
00238                                         'older'
00239                                 ),
00240                                 ApiBase::PARAM_DFLT => 'older'
00241                         ),
00242                         'ids' => array(
00243                                 ApiBase::PARAM_TYPE => 'integer',
00244                                 ApiBase::PARAM_ISMULTI => true
00245                         ),
00246                         'users' => array(
00247                                 ApiBase::PARAM_ISMULTI => true
00248                         ),
00249                         'ip' => null,
00250                         'limit' => array(
00251                                 ApiBase::PARAM_DFLT => 10,
00252                                 ApiBase::PARAM_TYPE => 'limit',
00253                                 ApiBase::PARAM_MIN => 1,
00254                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00255                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00256                         ),
00257                         'prop' => array(
00258                                 ApiBase::PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',
00259                                 ApiBase::PARAM_TYPE => array(
00260                                         'id',
00261                                         'user',
00262                                         'userid',
00263                                         'by',
00264                                         'byid',
00265                                         'timestamp',
00266                                         'expiry',
00267                                         'reason',
00268                                         'range',
00269                                         'flags'
00270                                 ),
00271                                 ApiBase::PARAM_ISMULTI => true
00272                         ),
00273                         'show' => array(
00274                                 ApiBase::PARAM_TYPE => array(
00275                                         'account',
00276                                         '!account',
00277                                         'temp',
00278                                         '!temp',
00279                                         'ip',
00280                                         '!ip',
00281                                         'range',
00282                                         '!range',
00283                                 ),
00284                                 ApiBase::PARAM_ISMULTI => true
00285                         ),
00286                 );
00287         }
00288 
00289         public function getParamDescription() {
00290                 $p = $this->getModulePrefix();
00291                 return array(
00292                         'start' => 'The timestamp to start enumerating from',
00293                         'end' => 'The timestamp to stop enumerating at',
00294                         'dir' => $this->getDirectionDescription( $p ),
00295                         'ids' => 'Pipe-separated list of block IDs to list (optional)',
00296                         'users' => 'Pipe-separated list of users to search for (optional)',
00297                         'ip' => array(  'Get all blocks applying to this IP or CIDR range, including range blocks.',
00298                                         'Cannot be used together with bkusers. CIDR ranges broader than /16 are not accepted' ),
00299                         'limit' => 'The maximum amount of blocks to list',
00300                         'prop' => array(
00301                                 'Which properties to get',
00302                                 ' id         - Adds the ID of the block',
00303                                 ' user       - Adds the username of the blocked user',
00304                                 ' userid     - Adds the user ID of the blocked user',
00305                                 ' by         - Adds the username of the blocking user',
00306                                 ' byid       - Adds the user ID of the blocking user',
00307                                 ' timestamp  - Adds the timestamp of when the block was given',
00308                                 ' expiry     - Adds the timestamp of when the block expires',
00309                                 ' reason     - Adds the reason given for the block',
00310                                 ' range      - Adds the range of IPs affected by the block',
00311                                 ' flags      - Tags the ban with (autoblock, anononly, etc)',
00312                         ),
00313                         'show' => array(
00314                                 'Show only items that meet this criteria.',
00315                                 "For example, to see only indefinite blocks on IPs, set {$p}show=ip|!temp"
00316                         ),
00317                 );
00318         }
00319 
00320         public function getDescription() {
00321                 return 'List all blocked users and IP addresses';
00322         }
00323 
00324         public function getPossibleErrors() {
00325                 return array_merge( parent::getPossibleErrors(), array(
00326                         $this->getRequireOnlyOneParameterErrorMessages( array( 'users', 'ip' ) ),
00327                         array( 'code' => 'cidrtoobroad', 'info' => 'CIDR ranges broader than /16 are not accepted' ),
00328                         array( 'code' => 'param_user', 'info' => 'User parameter may not be empty' ),
00329                         array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
00330                         array( 'show' ),
00331                 ) );
00332         }
00333 
00334         public function getExamples() {
00335                 return array(
00336                         'api.php?action=query&list=blocks',
00337                         'api.php?action=query&list=blocks&bkusers=Alice|Bob'
00338                 );
00339         }
00340 
00341         public function getHelpUrls() {
00342                 return 'https://www.mediawiki.org/wiki/API:Blocks';
00343         }
00344 
00345         public function getVersion() {
00346                 return __CLASS__ . ': $Id$';
00347         }
00348 }