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