MediaWiki  REL1_19
ApiQueryAllUsers.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryAllUsers extends ApiQueryBase {
00033         public function __construct( $query, $moduleName ) {
00034                 parent::__construct( $query, $moduleName, 'au' );
00035         }
00036 
00037         public function execute() {
00038                 $db = $this->getDB();
00039                 $params = $this->extractRequestParams();
00040 
00041                 $prop = $params['prop'];
00042                 if ( !is_null( $prop ) ) {
00043                         $prop = array_flip( $prop );
00044                         $fld_blockinfo = isset( $prop['blockinfo'] );
00045                         $fld_editcount = isset( $prop['editcount'] );
00046                         $fld_groups = isset( $prop['groups'] );
00047                         $fld_rights = isset( $prop['rights'] );
00048                         $fld_registration = isset( $prop['registration'] );
00049                         $fld_implicitgroups = isset( $prop['implicitgroups'] );
00050                 } else {
00051                         $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = $fld_rights = $fld_implicitgroups = false;
00052                 }
00053 
00054                 $limit = $params['limit'];
00055 
00056                 $this->addTables( 'user' );
00057                 $useIndex = true;
00058 
00059                 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
00060                 $from = is_null( $params['from'] ) ? null : $this->keyToTitle( $params['from'] );
00061                 $to = is_null( $params['to'] ) ? null : $this->keyToTitle( $params['to'] );
00062 
00063                 # MySQL doesn't seem to use 'equality propagation' here, so like the
00064                 # ActiveUsers special page, we have to use rc_user_text for some cases.
00065                 $userFieldToSort = $params['activeusers'] ? 'rc_user_text' : 'user_name';
00066 
00067                 $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
00068 
00069                 if ( !is_null( $params['prefix'] ) ) {
00070                         $this->addWhere( $userFieldToSort .
00071                                 $db->buildLike( $this->keyToTitle( $params['prefix'] ), $db->anyString() ) );
00072                 }
00073 
00074                 if ( !is_null( $params['rights'] ) ) {
00075                         $groups = array();
00076                         foreach( $params['rights'] as $r ) {
00077                                 $groups = array_merge( $groups, User::getGroupsWithPermission( $r ) );
00078                         }
00079 
00080                         $groups = array_unique( $groups );
00081 
00082                         if ( is_null( $params['group'] ) ) {
00083                                 $params['group'] = $groups;
00084                         } else {
00085                                 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
00086                         }
00087                 }
00088 
00089                 if ( !is_null( $params['group'] ) && !is_null( $params['excludegroup'] ) ) {
00090                         $this->dieUsage( 'group and excludegroup cannot be used together', 'group-excludegroup' );
00091                 }
00092 
00093                 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
00094                         $useIndex = false;
00095                         // Filter only users that belong to a given group
00096                         $this->addTables( 'user_groups', 'ug1' );
00097                         $this->addJoinConds( array( 'ug1' => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
00098                                         'ug1.ug_group' => $params['group'] ) ) ) );
00099                 }
00100 
00101                 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
00102                         $useIndex = false;
00103                         // Filter only users don't belong to a given group
00104                         $this->addTables( 'user_groups', 'ug1' );
00105 
00106                         if ( count( $params['excludegroup'] ) == 1 ) {
00107                                 $exclude = array( 'ug1.ug_group' => $params['excludegroup'][0] );
00108                         } else {
00109                                 $exclude = array( $db->makeList( array( 'ug1.ug_group' => $params['excludegroup'] ), LIST_OR ) );
00110                         }
00111                         $this->addJoinConds( array( 'ug1' => array( 'LEFT OUTER JOIN',
00112                                 array_merge( array( 'ug1.ug_user=user_id' ), $exclude )
00113                                 )
00114                         ) );
00115                         $this->addWhere( 'ug1.ug_user IS NULL' );
00116                 }
00117 
00118                 if ( $params['witheditsonly'] ) {
00119                         $this->addWhere( 'user_editcount > 0' );
00120                 }
00121 
00122                 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
00123 
00124                 if ( $fld_groups || $fld_rights ) {
00125                         // Show the groups the given users belong to
00126                         // request more than needed to avoid not getting all rows that belong to one user
00127                         $groupCount = count( User::getAllGroups() );
00128                         $sqlLimit = $limit + $groupCount + 1;
00129 
00130                         $this->addTables( 'user_groups', 'ug2' );
00131                         $this->addJoinConds( array( 'ug2' => array( 'LEFT JOIN', 'ug2.ug_user=user_id' ) ) );
00132                         $this->addFields( 'ug2.ug_group ug_group2' );
00133                 } else {
00134                         $sqlLimit = $limit + 1;
00135                 }
00136 
00137                 if ( $params['activeusers'] ) {
00138                         global $wgActiveUserDays;
00139                         $this->addTables( 'recentchanges' );
00140 
00141                         $this->addJoinConds( array( 'recentchanges' => array(
00142                                 'INNER JOIN', 'rc_user_text=user_name'
00143                         ) ) );
00144 
00145                         $this->addFields( 'COUNT(*) AS recentedits' );
00146 
00147                         $this->addWhere( "rc_log_type IS NULL OR rc_log_type != 'newusers'" );
00148                         $timestamp = $db->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays*24*3600 );
00149                         $this->addWhere( "rc_timestamp >= {$db->addQuotes( $timestamp )}" );
00150 
00151                         $this->addOption( 'GROUP BY', $userFieldToSort );
00152                 }
00153 
00154                 $this->addOption( 'LIMIT', $sqlLimit );
00155 
00156                 $this->addFields( array(
00157                         'user_name',
00158                         'user_id'
00159                 ) );
00160                 $this->addFieldsIf( 'user_editcount', $fld_editcount );
00161                 $this->addFieldsIf( 'user_registration', $fld_registration );
00162 
00163                 if ( $useIndex ) {
00164                         $this->addOption( 'USE INDEX', array( 'user' => 'user_name' ) );
00165                 }
00166 
00167                 $res = $this->select( __METHOD__ );
00168 
00169                 $count = 0;
00170                 $lastUserData = false;
00171                 $lastUser = false;
00172                 $result = $this->getResult();
00173 
00174                 //
00175                 // This loop keeps track of the last entry.
00176                 // For each new row, if the new row is for different user then the last, the last entry is added to results.
00177                 // Otherwise, the group of the new row is appended to the last entry.
00178                 // The setContinue... is more complex because of this, and takes into account the higher sql limit
00179                 // to make sure all rows that belong to the same user are received.
00180 
00181                 foreach ( $res as $row ) {
00182                         $count++;
00183 
00184                         if ( $lastUser !== $row->user_name ) {
00185                                 // Save the last pass's user data
00186                                 if ( is_array( $lastUserData ) ) {
00187                                         $fit = $result->addValue( array( 'query', $this->getModuleName() ),
00188                                                         null, $lastUserData );
00189 
00190                                         $lastUserData = null;
00191 
00192                                         if ( !$fit ) {
00193                                                 $this->setContinueEnumParameter( 'from',
00194                                                                 $this->keyToTitle( $lastUserData['name'] ) );
00195                                                 break;
00196                                         }
00197                                 }
00198 
00199                                 if ( $count > $limit ) {
00200                                         // We've reached the one extra which shows that there are additional pages to be had. Stop here...
00201                                         $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->user_name ) );
00202                                         break;
00203                                 }
00204 
00205                                 // Record new user's data
00206                                 $lastUser = $row->user_name;
00207                                 $lastUserData = array(
00208                                         'userid' => $row->user_id,
00209                                         'name' => $lastUser,
00210                                 );
00211                                 if ( $fld_blockinfo && !is_null( $row->ipb_by_text ) ) {
00212                                         $lastUserData['blockedby'] = $row->ipb_by_text;
00213                                         $lastUserData['blockreason'] = $row->ipb_reason;
00214                                         $lastUserData['blockexpiry'] = $row->ipb_expiry;
00215                                 }
00216                                 if ( $row->ipb_deleted ) {
00217                                         $lastUserData['hidden'] = '';
00218                                 }
00219                                 if ( $fld_editcount ) {
00220                                         $lastUserData['editcount'] = intval( $row->user_editcount );
00221                                 }
00222                                 if ( $params['activeusers'] ) {
00223                                         $lastUserData['recenteditcount'] = intval( $row->recentedits );
00224                                 }
00225                                 if ( $fld_registration ) {
00226                                         $lastUserData['registration'] = $row->user_registration ?
00227                                                 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
00228                                 }
00229                         }
00230 
00231                         if ( $sqlLimit == $count ) {
00232                                 // BUG!  database contains group name that User::getAllGroups() does not return
00233                                 // TODO: should handle this more gracefully
00234                                 ApiBase::dieDebug( __METHOD__,
00235                                         'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
00236                         }
00237 
00238                         $lastUserObj = User::newFromName( $lastUser );
00239 
00240                         // Add user's group info
00241                         if ( $fld_groups ) {
00242                                 if ( !isset( $lastUserData['groups'] ) && $lastUserObj ) {
00243                                         $lastUserData['groups'] = ApiQueryUsers::getAutoGroups( $lastUserObj );
00244                                 }
00245 
00246                                 if ( !is_null( $row->ug_group2 ) ) {
00247                                         $lastUserData['groups'][] = $row->ug_group2;
00248                                 }
00249                                 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
00250                         }
00251 
00252                         if ( $fld_implicitgroups && !isset( $lastUserData['implicitgroups'] ) && $lastUserObj ) {
00253                                 $lastUserData['implicitgroups'] = ApiQueryUsers::getAutoGroups( $lastUserObj );
00254                                 $result->setIndexedTagName( $lastUserData['implicitgroups'], 'g' );
00255                         }
00256                         if ( $fld_rights ) {
00257                                 if ( !isset( $lastUserData['rights'] ) && $lastUserObj ) {
00258                                         $lastUserData['rights'] =  User::getGroupPermissions( $lastUserObj->getAutomaticGroups() );
00259                                 }
00260                                 if ( !is_null( $row->ug_group2 ) ) {
00261                                         $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
00262                                                 User::getGroupPermissions( array( $row->ug_group2 ) ) ) );
00263                                 }
00264                                 $result->setIndexedTagName( $lastUserData['rights'], 'r' );
00265                         }
00266                 }
00267 
00268                 if ( is_array( $lastUserData ) ) {
00269                         $fit = $result->addValue( array( 'query', $this->getModuleName() ),
00270                                 null, $lastUserData );
00271                         if ( !$fit ) {
00272                                 $this->setContinueEnumParameter( 'from',
00273                                         $this->keyToTitle( $lastUserData['name'] ) );
00274                         }
00275                 }
00276 
00277                 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
00278         }
00279 
00280         public function getCacheMode( $params ) {
00281                 return 'anon-public-user-private';
00282         }
00283 
00284         public function getAllowedParams() {
00285                 $userGroups = User::getAllGroups();
00286                 return array(
00287                         'from' => null,
00288                         'to' => null,
00289                         'prefix' => null,
00290                         'dir' => array(
00291                                 ApiBase::PARAM_DFLT => 'ascending',
00292                                 ApiBase::PARAM_TYPE => array(
00293                                         'ascending',
00294                                         'descending'
00295                                 ),
00296                         ),
00297                         'group' => array(
00298                                 ApiBase::PARAM_TYPE => $userGroups,
00299                                 ApiBase::PARAM_ISMULTI => true,
00300                         ),
00301                         'excludegroup' => array(
00302                                 ApiBase::PARAM_TYPE => $userGroups,
00303                                 ApiBase::PARAM_ISMULTI => true,
00304                         ),
00305                         'rights' => array(
00306                                 ApiBase::PARAM_TYPE => User::getAllRights(),
00307                                 ApiBase::PARAM_ISMULTI => true,
00308                         ),
00309                         'prop' => array(
00310                                 ApiBase::PARAM_ISMULTI => true,
00311                                 ApiBase::PARAM_TYPE => array(
00312                                         'blockinfo',
00313                                         'groups',
00314                                         'implicitgroups',
00315                                         'rights',
00316                                         'editcount',
00317                                         'registration'
00318                                 )
00319                         ),
00320                         'limit' => array(
00321                                 ApiBase::PARAM_DFLT => 10,
00322                                 ApiBase::PARAM_TYPE => 'limit',
00323                                 ApiBase::PARAM_MIN => 1,
00324                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00325                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00326                         ),
00327                         'witheditsonly' => false,
00328                         'activeusers' => false,
00329                 );
00330         }
00331 
00332         public function getParamDescription() {
00333                 global $wgActiveUserDays;
00334                 return array(
00335                         'from' => 'The user name to start enumerating from',
00336                         'to' => 'The user name to stop enumerating at',
00337                         'prefix' => 'Search for all users that begin with this value',
00338                         'dir' => 'Direction to sort in',
00339                         'group' => 'Limit users to given group name(s)',
00340                         'excludegroup' => 'Exclude users in given group name(s)',
00341                         'rights' => 'Limit users to given right(s)',
00342                         'prop' => array(
00343                                 'What pieces of information to include.',
00344                                 ' blockinfo      - Adds the information about a current block on the user',
00345                                 ' groups         - Lists groups that the user is in. This uses more server resources and may return fewer results than the limit',
00346                                 ' implicitgroups - Lists all the groups the user is automatically in',
00347                                 ' rights         - Lists rights that the user has',
00348                                 ' editcount      - Adds the edit count of the user',
00349                                 ' registration   - Adds the timestamp of when the user registered if available (may be blank)',
00350                                 ),
00351                         'limit' => 'How many total user names to return',
00352                         'witheditsonly' => 'Only list users who have made edits',
00353                         'activeusers' => "Only list users active in the last {$wgActiveUserDays} days(s)"
00354                 );
00355         }
00356 
00357         public function getDescription() {
00358                 return 'Enumerate all registered users';
00359         }
00360 
00361         public function getPossibleErrors() {
00362                 return array_merge( parent::getPossibleErrors(), array(
00363                         array( 'code' => 'group-excludegroup', 'info' => 'group and excludegroup cannot be used together' ),
00364                 ) );
00365         }
00366 
00367         public function getExamples() {
00368                 return array(
00369                         'api.php?action=query&list=allusers&aufrom=Y',
00370                 );
00371         }
00372 
00373         public function getHelpUrls() {
00374                 return 'https://www.mediawiki.org/wiki/API:Allusers';
00375         }
00376 
00377         public function getVersion() {
00378                 return __CLASS__ . ': $Id$';
00379         }
00380 }