MediaWiki  master
SpecialListusers.php
Go to the documentation of this file.
00001 <?php
00035 class UsersPager extends AlphabeticPager {
00036 
00043         function __construct( IContextSource $context = null, $par = null, $including = null ) {
00044                 if ( $context ) {
00045                         $this->setContext( $context );
00046                 }
00047 
00048                 $request = $this->getRequest();
00049                 $par = ( $par !== null ) ? $par : '';
00050                 $parms = explode( '/', $par );
00051                 $symsForAll = array( '*', 'user' );
00052                 if ( $parms[0] != '' && ( in_array( $par, User::getAllGroups() ) || in_array( $par, $symsForAll ) ) ) {
00053                         $this->requestedGroup = $par;
00054                         $un = $request->getText( 'username' );
00055                 } elseif ( count( $parms ) == 2 ) {
00056                         $this->requestedGroup = $parms[0];
00057                         $un = $parms[1];
00058                 } else {
00059                         $this->requestedGroup = $request->getVal( 'group' );
00060                         $un = ( $par != '' ) ? $par : $request->getText( 'username' );
00061                 }
00062                 if ( in_array( $this->requestedGroup, $symsForAll ) ) {
00063                         $this->requestedGroup = '';
00064                 }
00065                 $this->editsOnly = $request->getBool( 'editsOnly' );
00066                 $this->creationSort = $request->getBool( 'creationSort' );
00067                 $this->including = $including;
00068 
00069                 $this->requestedUser = '';
00070                 if ( $un != '' ) {
00071                         $username = Title::makeTitleSafe( NS_USER, $un );
00072                         if( ! is_null( $username ) ) {
00073                                 $this->requestedUser = $username->getText();
00074                         }
00075                 }
00076                 parent::__construct();
00077         }
00078 
00082         function getIndexField() {
00083                 return $this->creationSort ? 'user_id' : 'user_name';
00084         }
00085 
00089         function getQueryInfo() {
00090                 $dbr = wfGetDB( DB_SLAVE );
00091                 $conds = array();
00092                 // Don't show hidden names
00093                 if( !$this->getUser()->isAllowed( 'hideuser' ) ) {
00094                         $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
00095                 }
00096 
00097                 $options = array();
00098 
00099                 if( $this->requestedGroup != '' ) {
00100                         $conds['ug_group'] = $this->requestedGroup;
00101                 } else {
00102                         //$options['USE INDEX'] = $this->creationSort ? 'PRIMARY' : 'user_name';
00103                 }
00104                 if( $this->requestedUser != '' ) {
00105                         # Sorted either by account creation or name
00106                         if( $this->creationSort ) {
00107                                 $conds[] = 'user_id >= ' . intval( User::idFromName( $this->requestedUser ) );
00108                         } else {
00109                                 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
00110                         }
00111                 }
00112                 if( $this->editsOnly ) {
00113                         $conds[] = 'user_editcount > 0';
00114                 }
00115 
00116                 $options['GROUP BY'] = $this->creationSort ? 'user_id' : 'user_name';
00117 
00118                 $query = array(
00119                         'tables' => array( 'user', 'user_groups', 'ipblocks'),
00120                         'fields' => array(
00121                                 'user_name' => $this->creationSort ? 'MAX(user_name)' : 'user_name',
00122                                 'user_id' => $this->creationSort ? 'user_id' : 'MAX(user_id)',
00123                                 'edits' => 'MAX(user_editcount)',
00124                                 'numgroups' => 'COUNT(ug_group)',
00125                                 'singlegroup' => 'MAX(ug_group)', // the usergroup if there is only one
00126                                 'creation' => 'MIN(user_registration)',
00127                                 'ipb_deleted' => 'MAX(ipb_deleted)' // block/hide status
00128                         ),
00129                         'options' => $options,
00130                         'join_conds' => array(
00131                                 'user_groups' => array( 'LEFT JOIN', 'user_id=ug_user' ),
00132                                 'ipblocks' => array( 'LEFT JOIN', array(
00133                                         'user_id=ipb_user',
00134                                         'ipb_auto' => 0
00135                                 )),
00136                         ),
00137                         'conds' => $conds
00138                 );
00139 
00140                 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
00141                 return $query;
00142         }
00143 
00148         function formatRow( $row ) {
00149                 if ( $row->user_id == 0 ) { #Bug 16487
00150                         return '';
00151                 }
00152 
00153                 $userName = $row->user_name;
00154 
00155                 $ulinks = Linker::userLink( $row->user_id, $userName );
00156                 $ulinks .= Linker::userToolLinksRedContribs( $row->user_id, $userName, intval( $row->edits ) );
00157 
00158                 $lang = $this->getLanguage();
00159 
00160                 $groups = '';
00161                 $groups_list = self::getGroups( $row->user_id );
00162                 if( !$this->including && count( $groups_list ) > 0 ) {
00163                         $list = array();
00164                         foreach( $groups_list as $group )
00165                                 $list[] = self::buildGroupLink( $group, $userName );
00166                         $groups = $lang->commaList( $list );
00167                 }
00168 
00169                 $item = $lang->specialList( $ulinks, $groups );
00170                 if( $row->ipb_deleted ) {
00171                         $item = "<span class=\"deleted\">$item</span>";
00172                 }
00173 
00174                 $edits = '';
00175                 global $wgEdititis;
00176                 if ( !$this->including && $wgEdititis ) {
00177                         $edits = ' [' . $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped() . ']';
00178                 }
00179 
00180                 $created = '';
00181                 # Some rows may be NULL
00182                 if( !$this->including && $row->creation ) {
00183                         $user = $this->getUser();
00184                         $d = $lang->userDate( $row->creation, $user );
00185                         $t = $lang->userTime( $row->creation, $user );
00186                         $created = $this->msg( 'usercreated', $d, $t, $row->user_name )->escaped();
00187                         $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
00188                 }
00189                 $blocked = !is_null( $row->ipb_deleted ) ? ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() : '';
00190 
00191                 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
00192                 return Html::rawElement( 'li', array(), "{$item}{$edits}{$created}{$blocked}" );
00193         }
00194 
00195         function doBatchLookups() {
00196                 $batch = new LinkBatch();
00197                 # Give some pointers to make user links
00198                 foreach ( $this->mResult as $row ) {
00199                         $batch->add( NS_USER, $row->user_name );
00200                         $batch->add( NS_USER_TALK, $row->user_name );
00201                 }
00202                 $batch->execute();
00203                 $this->mResult->rewind();
00204         }
00205 
00209         function getPageHeader( ) {
00210                 global $wgScript;
00211 
00212                 list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
00213 
00214                 # Form tag
00215                 $out  = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listusers-form' ) ) .
00216                         Xml::fieldset( $this->msg( 'listusers' )->text() ) .
00217                         Html::hidden( 'title', $self );
00218 
00219                 # Username field
00220                 $out .= Xml::label( $this->msg( 'listusersfrom' )->text(), 'offset' ) . ' ' .
00221                         Xml::input( 'username', 20, $this->requestedUser, array( 'id' => 'offset' ) ) . ' ';
00222 
00223                 # Group drop-down list
00224                 $out .= Xml::label( $this->msg( 'group' )->text(), 'group' ) . ' ' .
00225                         Xml::openElement('select',  array( 'name' => 'group', 'id' => 'group' ) ) .
00226                         Xml::option( $this->msg( 'group-all' )->text(), '' );
00227                 foreach( $this->getAllGroups() as $group => $groupText )
00228                         $out .= Xml::option( $groupText, $group, $group == $this->requestedGroup );
00229                 $out .= Xml::closeElement( 'select' ) . '<br />';
00230                 $out .= Xml::checkLabel( $this->msg( 'listusers-editsonly' )->text(), 'editsOnly', 'editsOnly', $this->editsOnly );
00231                 $out .= '&#160;';
00232                 $out .= Xml::checkLabel( $this->msg( 'listusers-creationsort' )->text(), 'creationSort', 'creationSort', $this->creationSort );
00233                 $out .= '<br />';
00234 
00235                 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
00236 
00237                 # Submit button and form bottom
00238                 $out .= Html::hidden( 'limit', $this->mLimit );
00239                 $out .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() );
00240                 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
00241                 $out .= Xml::closeElement( 'fieldset' ) .
00242                         Xml::closeElement( 'form' );
00243 
00244                 return $out;
00245         }
00246 
00251         function getAllGroups() {
00252                 $result = array();
00253                 foreach( User::getAllGroups() as $group ) {
00254                         $result[$group] = User::getGroupName( $group );
00255                 }
00256                 asort( $result );
00257                 return $result;
00258         }
00259 
00264         function getDefaultQuery() {
00265                 $query = parent::getDefaultQuery();
00266                 if( $this->requestedGroup != '' ) {
00267                         $query['group'] = $this->requestedGroup;
00268                 }
00269                 if( $this->requestedUser != '' ) {
00270                         $query['username'] = $this->requestedUser;
00271                 }
00272                 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
00273                 return $query;
00274         }
00275 
00282         protected static function getGroups( $uid ) {
00283                 $user = User::newFromId( $uid );
00284                 $groups = array_diff( $user->getEffectiveGroups(), User::getImplicitGroups() );
00285                 return $groups;
00286         }
00287 
00295         protected static function buildGroupLink( $group, $username ) {
00296                 return User::makeGroupLinkHtml( $group, htmlspecialchars( User::getGroupMember( $group, $username ) ) );
00297         }
00298 }
00299 
00303 class SpecialListUsers extends IncludableSpecialPage {
00304 
00308         public function __construct() {
00309                 parent::__construct( 'Listusers' );
00310         }
00311 
00317         public function execute( $par ) {
00318                 $this->setHeaders();
00319                 $this->outputHeader();
00320 
00321                 $up = new UsersPager( $this->getContext(), $par, $this->including() );
00322 
00323                 # getBody() first to check, if empty
00324                 $usersbody = $up->getBody();
00325 
00326                 $s = '';
00327                 if ( !$this->including() ) {
00328                         $s = $up->getPageHeader();
00329                 }
00330 
00331                 if( $usersbody ) {
00332                         $s .= $up->getNavigationBar();
00333                         $s .= Html::rawElement( 'ul', array(), $usersbody );
00334                         $s .= $up->getNavigationBar();
00335                 } else {
00336                         $s .= $this->msg( 'listusers-noresult' )->parseAsBlock();
00337                 }
00338 
00339                 $this->getOutput()->addHTML( $s );
00340         }
00341 }