00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if ( !defined( 'MEDIAWIKI' ) ) {
00027
00028 require_once ( 'ApiQueryBase.php' );
00029 }
00030
00036 class ApiQueryAllUsers extends ApiQueryBase {
00037
00038 public function __construct( $query, $moduleName ) {
00039 parent :: __construct( $query, $moduleName, 'au' );
00040 }
00041
00042 public function execute() {
00043 $db = $this->getDB();
00044 $params = $this->extractRequestParams();
00045
00046 $prop = $params['prop'];
00047 if ( !is_null( $prop ) ) {
00048 $prop = array_flip( $prop );
00049 $fld_blockinfo = isset( $prop['blockinfo'] );
00050 $fld_editcount = isset( $prop['editcount'] );
00051 $fld_groups = isset( $prop['groups'] );
00052 $fld_registration = isset( $prop['registration'] );
00053 } else {
00054 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = false;
00055 }
00056
00057 $limit = $params['limit'];
00058 $this->addTables( 'user', 'u1' );
00059 $useIndex = true;
00060
00061 if ( !is_null( $params['from'] ) )
00062 $this->addWhere( 'u1.user_name >= ' . $db->addQuotes( $this->keyToTitle( $params['from'] ) ) );
00063
00064 if ( !is_null( $params['prefix'] ) )
00065 $this->addWhere( 'u1.user_name' . $db->buildLike( $this->keyToTitle( $params['prefix'] ), $db->anyString() ) );
00066
00067 if ( !is_null( $params['group'] ) ) {
00068 $useIndex = false;
00069
00070 $this->addTables( 'user_groups', 'ug1' );
00071 $ug1 = $this->getAliasedName( 'user_groups', 'ug1' );
00072 $this->addJoinConds( array( $ug1 => array( 'INNER JOIN', array( 'ug1.ug_user=u1.user_id',
00073 'ug1.ug_group' => $params['group'] ) ) ) );
00074 }
00075
00076 if ( $params['witheditsonly'] )
00077 $this->addWhere( 'u1.user_editcount > 0' );
00078
00079 if ( $fld_groups ) {
00080
00081
00082 $groupCount = count( User::getAllGroups() );
00083 $sqlLimit = $limit + $groupCount + 1;
00084
00085 $this->addTables( 'user_groups', 'ug2' );
00086 $tname = $this->getAliasedName( 'user_groups', 'ug2' );
00087 $this->addJoinConds( array( $tname => array( 'LEFT JOIN', 'ug2.ug_user=u1.user_id' ) ) );
00088 $this->addFields( 'ug2.ug_group ug_group2' );
00089 } else {
00090 $sqlLimit = $limit + 1;
00091 }
00092 if ( $fld_blockinfo ) {
00093 $this->addTables( 'ipblocks' );
00094 $this->addTables( 'user', 'u2' );
00095 $u2 = $this->getAliasedName( 'user', 'u2' );
00096 $this->addJoinConds( array(
00097 'ipblocks' => array( 'LEFT JOIN', 'ipb_user=u1.user_id' ),
00098 $u2 => array( 'LEFT JOIN', 'ipb_by=u2.user_id' ) ) );
00099 $this->addFields( array( 'ipb_reason', 'u2.user_name AS blocker_name' ) );
00100 }
00101
00102 $this->addOption( 'LIMIT', $sqlLimit );
00103
00104 $this->addFields( 'u1.user_name' );
00105 $this->addFieldsIf( 'u1.user_editcount', $fld_editcount );
00106 $this->addFieldsIf( 'u1.user_registration', $fld_registration );
00107
00108 $this->addOption( 'ORDER BY', 'u1.user_name' );
00109 if ( $useIndex ) {
00110 $u1 = $this->getAliasedName( 'user', 'u1' );
00111 $this->addOption( 'USE INDEX', array( $u1 => 'user_name' ) );
00112 }
00113
00114 $res = $this->select( __METHOD__ );
00115
00116 $data = array ();
00117 $count = 0;
00118 $lastUserData = false;
00119 $lastUser = false;
00120 $result = $this->getResult();
00121
00122
00123
00124
00125
00126
00127
00128
00129 while ( true ) {
00130
00131 $row = $db->fetchObject( $res );
00132 $count++;
00133
00134 if ( !$row || $lastUser !== $row->user_name ) {
00135
00136 if ( is_array( $lastUserData ) )
00137 {
00138 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
00139 null, $lastUserData );
00140 if ( !$fit )
00141 {
00142 $this->setContinueEnumParameter( 'from',
00143 $this->keyToTitle( $lastUserData['name'] ) );
00144 break;
00145 }
00146 }
00147
00148
00149 if ( !$row )
00150 break;
00151
00152 if ( $count > $limit ) {
00153
00154 $this->setContinueEnumParameter( 'from', $this->keyToTitle( $row->user_name ) );
00155 break;
00156 }
00157
00158
00159 $lastUser = $row->user_name;
00160 $lastUserData = array( 'name' => $lastUser );
00161 if ( $fld_blockinfo ) {
00162 $lastUserData['blockedby'] = $row->blocker_name;
00163 $lastUserData['blockreason'] = $row->ipb_reason;
00164 }
00165 if ( $fld_editcount )
00166 $lastUserData['editcount'] = intval( $row->user_editcount );
00167 if ( $fld_registration )
00168 $lastUserData['registration'] = $row->user_registration ?
00169 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
00170
00171 }
00172
00173 if ( $sqlLimit == $count ) {
00174
00175
00176 ApiBase :: dieDebug( __METHOD__,
00177 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
00178 }
00179
00180
00181 if ( $fld_groups && !is_null( $row->ug_group2 ) ) {
00182 $lastUserData['groups'][] = $row->ug_group2;
00183 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
00184 }
00185 }
00186
00187 if ( is_array( $lastUserData ) ) {
00188 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
00189 null, $lastUserData );
00190 if ( !$fit ) {
00191 $this->setContinueEnumParameter( 'from',
00192 $this->keyToTitle( $lastUserData['name'] ) );
00193 }
00194 }
00195
00196 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
00197 }
00198
00199 public function getCacheMode( $params ) {
00200 return 'public';
00201 }
00202
00203 public function getAllowedParams() {
00204 return array (
00205 'from' => null,
00206 'prefix' => null,
00207 'group' => array(
00208 ApiBase :: PARAM_TYPE => User::getAllGroups()
00209 ),
00210 'prop' => array (
00211 ApiBase :: PARAM_ISMULTI => true,
00212 ApiBase :: PARAM_TYPE => array (
00213 'blockinfo',
00214 'groups',
00215 'editcount',
00216 'registration'
00217 )
00218 ),
00219 'limit' => array (
00220 ApiBase :: PARAM_DFLT => 10,
00221 ApiBase :: PARAM_TYPE => 'limit',
00222 ApiBase :: PARAM_MIN => 1,
00223 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
00224 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
00225 ),
00226 'witheditsonly' => false,
00227 );
00228 }
00229
00230 public function getParamDescription() {
00231 return array (
00232 'from' => 'The user name to start enumerating from.',
00233 'prefix' => 'Search for all page titles that begin with this value.',
00234 'group' => 'Limit users to a given group name',
00235 'prop' => array(
00236 'What pieces of information to include.',
00237 '`groups` property uses more server resources and may return fewer results than the limit.' ),
00238 'limit' => 'How many total user names to return.',
00239 'witheditsonly' => 'Only list users who have made edits',
00240 );
00241 }
00242
00243 public function getDescription() {
00244 return 'Enumerate all registered users';
00245 }
00246
00247 protected function getExamples() {
00248 return array (
00249 'api.php?action=query&list=allusers&aufrom=Y',
00250 );
00251 }
00252
00253 public function getVersion() {
00254 return __CLASS__ . ': $Id: ApiQueryAllUsers.php 79562 2011-01-04 06:15:54Z tstarling $';
00255 }
00256 }