Follow-up r90742: the parser should call getFunctionLang()
[lhc/web/wiklou.git] / includes / api / ApiQueryAllUsers.php
index 5570e7d..336c494 100644 (file)
@@ -56,15 +56,15 @@ class ApiQueryAllUsers extends ApiQueryBase {
                }
 
                $limit = $params['limit'];
+
                $this->addTables( 'user' );
                $useIndex = true;
 
-               if ( !is_null( $params['from'] ) ) {
-                       $this->addWhere( 'user_name >= ' . $db->addQuotes( $this->keyToTitle( $params['from'] ) ) );
-               }
-               if ( !is_null( $params['to'] ) ) {
-                       $this->addWhere( 'user_name <= ' . $db->addQuotes( $this->keyToTitle( $params['to'] ) ) );
-               }
+               $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
+               $from = is_null( $params['from'] ) ? null : $this->keyToTitle( $params['from'] );
+               $to = is_null( $params['to'] ) ? null : $this->keyToTitle( $params['to'] );
+
+               $this->addWhereRange( 'user_name', $dir, $from, $to );
 
                if ( !is_null( $params['prefix'] ) ) {
                        $this->addWhere( 'user_name' . $db->buildLike( $this->keyToTitle( $params['prefix'] ), $db->anyString() ) );
@@ -85,19 +85,41 @@ class ApiQueryAllUsers extends ApiQueryBase {
                        }
                }
 
-               if ( !is_null( $params['group'] ) ) {
+               if ( !is_null( $params['group'] ) && !is_null( $params['excludegroup'] ) ) {
+                       $this->dieUsage( 'group and excludegroup cannot be used together', 'group-excludegroup' );
+               }
+
+               if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
                        $useIndex = false;
                        // Filter only users that belong to a given group
                        $this->addTables( 'user_groups', 'ug1' );
-                       $ug1 = $this->getAliasedName( 'user_groups', 'ug1' );
-                       $this->addJoinConds( array( $ug1 => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
+                       $this->addJoinConds( array( 'ug1' => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
                                        'ug1.ug_group' => $params['group'] ) ) ) );
                }
 
+               if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
+                       $useIndex = false;
+                       // Filter only users don't belong to a given group
+                       $this->addTables( 'user_groups', 'ug1' );
+
+                       if ( count( $params['excludegroup'] ) == 1 ) {
+                               $exclude = array( 'ug1.ug_group' => $params['excludegroup'][0] );
+                       } else {
+                               $exclude = array( $db->makeList( array( 'ug1.ug_group' => $params['excludegroup'] ), LIST_OR ) );
+                       }
+                       $this->addJoinConds( array( 'ug1' => array( 'LEFT OUTER JOIN',
+                               array_merge( array( 'ug1.ug_user=user_id' ), $exclude )
+                               )
+                       ) );
+                       $this->addWhere( 'ug1.ug_user IS NULL' );
+               }
+
                if ( $params['witheditsonly'] ) {
                        $this->addWhere( 'user_editcount > 0' );
                }
 
+               $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
+
                if ( $fld_groups || $fld_rights ) {
                        // Show the groups the given users belong to
                        // request more than needed to avoid not getting all rows that belong to one user
@@ -105,14 +127,28 @@ class ApiQueryAllUsers extends ApiQueryBase {
                        $sqlLimit = $limit + $groupCount + 1;
 
                        $this->addTables( 'user_groups', 'ug2' );
-                       $tname = $this->getAliasedName( 'user_groups', 'ug2' );
-                       $this->addJoinConds( array( $tname => array( 'LEFT JOIN', 'ug2.ug_user=user_id' ) ) );
+                       $this->addJoinConds( array( 'ug2' => array( 'LEFT JOIN', 'ug2.ug_user=user_id' ) ) );
                        $this->addFields( 'ug2.ug_group ug_group2' );
                } else {
                        $sqlLimit = $limit + 1;
                }
 
-               $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
+               if ( $params['activeusers'] ) {
+                       global $wgActiveUserDays;
+                       $this->addTables( 'recentchanges' );
+
+                       $this->addJoinConds( array( 'recentchanges' => array(
+                               'INNER JOIN', 'rc_user_text=user_name'
+                       ) ) );
+
+                       $this->addFields( 'COUNT(*) AS recentedits' );
+
+                       $this->addWhere( "rc_log_type IS NULL OR rc_log_type != 'newusers'" );
+                       $timestamp = $db->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays*24*3600 );
+                       $this->addWhere( "rc_timestamp >= {$db->addQuotes( $timestamp )}" );
+
+                       $this->addOption( 'GROUP BY', 'user_name' );
+               }
 
                $this->addOption( 'LIMIT', $sqlLimit );
 
@@ -123,7 +159,6 @@ class ApiQueryAllUsers extends ApiQueryBase {
                $this->addFieldsIf( 'user_editcount', $fld_editcount );
                $this->addFieldsIf( 'user_registration', $fld_registration );
 
-               $this->addOption( 'ORDER BY', 'user_name' );
                if ( $useIndex ) {
                        $this->addOption( 'USE INDEX', array( 'user' => 'user_name' ) );
                }
@@ -177,14 +212,19 @@ class ApiQueryAllUsers extends ApiQueryBase {
                                        $lastUserData['blockreason'] = $row->ipb_reason;
                                        $lastUserData['blockexpiry'] = $row->ipb_expiry;
                                }
+                               if ( $row->ipb_deleted ) {
+                                       $lastUserData['hidden'] = '';
+                               }
                                if ( $fld_editcount ) {
                                        $lastUserData['editcount'] = intval( $row->user_editcount );
                                }
+                               if ( $params['activeusers'] ) {
+                                       $lastUserData['recenteditcount'] = intval( $row->recentedits );
+                               }
                                if ( $fld_registration ) {
                                        $lastUserData['registration'] = $row->user_registration ?
                                                wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
                                }
-
                        }
 
                        if ( $sqlLimit == $count ) {
@@ -195,22 +235,25 @@ class ApiQueryAllUsers extends ApiQueryBase {
                        }
 
                        // Add user's group info
-                       if ( $fld_groups && !is_null( $row->ug_group2 ) ) {
+                       if ( $fld_groups ) {
                                if ( !isset( $lastUserData['groups'] ) ) {
                                        $lastUserData['groups'] = ApiQueryUsers::getAutoGroups( User::newFromName( $lastUser ) );
                                }
 
-                               $lastUserData['groups'][] = $row->ug_group2;
+                               if ( !is_null( $row->ug_group2 ) ) {
+                                       $lastUserData['groups'][] = $row->ug_group2;
+                               }
                                $result->setIndexedTagName( $lastUserData['groups'], 'g' );
                        }
 
-                       if ( $fld_rights && !is_null( $row->ug_group2 ) ) {
+                       if ( $fld_rights ) {
                                if ( !isset( $lastUserData['rights'] ) ) {
-                                       $lastUserData['rights'] = User::getGroupPermissions( User::getImplicitGroups() );
+                                       $lastUserData['rights'] =  User::getGroupPermissions( User::getImplicitGroups() );
+                               }
+                               if ( !is_null( $row->ug_group2 ) ) {
+                                       $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
+                                                                               User::getGroupPermissions( array( $row->ug_group2 ) ) ) );
                                }
-
-                               $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
-                                       User::getGroupPermissions( array( $row->ug_group2 ) ) ) );
                                $result->setIndexedTagName( $lastUserData['rights'], 'r' );
                        }
                }
@@ -228,16 +271,28 @@ class ApiQueryAllUsers extends ApiQueryBase {
        }
 
        public function getCacheMode( $params ) {
-               return 'public';
+               return 'anon-public-user-private';
        }
 
        public function getAllowedParams() {
+               $userGroups = User::getAllGroups();
                return array(
                        'from' => null,
                        'to' => null,
                        'prefix' => null,
+                       'dir' => array(
+                               ApiBase::PARAM_DFLT => 'ascending',
+                               ApiBase::PARAM_TYPE => array(
+                                       'ascending',
+                                       'descending'
+                               ),
+                       ),
                        'group' => array(
-                               ApiBase::PARAM_TYPE => User::getAllGroups(),
+                               ApiBase::PARAM_TYPE => $userGroups,
+                               ApiBase::PARAM_ISMULTI => true,
+                       ),
+                       'excludegroup' => array(
+                               ApiBase::PARAM_TYPE => $userGroups,
                                ApiBase::PARAM_ISMULTI => true,
                        ),
                        'rights' => array(
@@ -262,15 +317,19 @@ class ApiQueryAllUsers extends ApiQueryBase {
                                ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
                        ),
                        'witheditsonly' => false,
+                       'activeusers' => false,
                );
        }
 
        public function getParamDescription() {
+               global $wgActiveUserDays;
                return array(
                        'from' => 'The user name to start enumerating from',
                        'to' => 'The user name to stop enumerating at',
                        'prefix' => 'Search for all users that begin with this value',
+                       'dir' => 'Direction to sort in',
                        'group' => 'Limit users to given group name(s)',
+                       'excludegroup' => 'Exclude users in given group name(s)',
                        'rights' => 'Limit users to given right(s)',
                        'prop' => array(
                                'What pieces of information to include.',
@@ -278,10 +337,11 @@ class ApiQueryAllUsers extends ApiQueryBase {
                                ' groups        - Lists groups that the user is in. This uses more server resources and may return fewer results than the limit',
                                ' rights        - Lists rights that the user has',
                                ' editcount     - Adds the edit count of the user',
-                               ' registration  - Adds the timestamp of when the user registered',
+                               ' registration  - Adds the timestamp of when the user registered if available (may be blank)',
                                ),
                        'limit' => 'How many total user names to return',
                        'witheditsonly' => 'Only list users who have made edits',
+                       'activeusers' => "Only list users active in the last {$wgActiveUserDays} days(s)"
                );
        }
 
@@ -289,6 +349,12 @@ class ApiQueryAllUsers extends ApiQueryBase {
                return 'Enumerate all registered users';
        }
 
+       public function getPossibleErrors() {
+               return array_merge( parent::getPossibleErrors(), array(
+                       array( 'code' => 'group-excludegroup', 'info' => 'group and excludegroup cannot be used together' ),
+               ) );
+       }
+
        protected function getExamples() {
                return array(
                        'api.php?action=query&list=allusers&aufrom=Y',