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