MediaWiki  REL1_22
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 
00053         if ( $parms[0] != '' &&
00054             ( in_array( $par, User::getAllGroups() ) || in_array( $par, $symsForAll ) )
00055         ) {
00056             $this->requestedGroup = $par;
00057             $un = $request->getText( 'username' );
00058         } elseif ( count( $parms ) == 2 ) {
00059             $this->requestedGroup = $parms[0];
00060             $un = $parms[1];
00061         } else {
00062             $this->requestedGroup = $request->getVal( 'group' );
00063             $un = ( $par != '' ) ? $par : $request->getText( 'username' );
00064         }
00065 
00066         if ( in_array( $this->requestedGroup, $symsForAll ) ) {
00067             $this->requestedGroup = '';
00068         }
00069         $this->editsOnly = $request->getBool( 'editsOnly' );
00070         $this->creationSort = $request->getBool( 'creationSort' );
00071         $this->including = $including;
00072         $this->mDefaultDirection = $request->getBool( 'desc' );
00073 
00074         $this->requestedUser = '';
00075 
00076         if ( $un != '' ) {
00077             $username = Title::makeTitleSafe( NS_USER, $un );
00078 
00079             if ( !is_null( $username ) ) {
00080                 $this->requestedUser = $username->getText();
00081             }
00082         }
00083 
00084         parent::__construct();
00085     }
00086 
00090     function getIndexField() {
00091         return $this->creationSort ? 'user_id' : 'user_name';
00092     }
00093 
00097     function getQueryInfo() {
00098         $dbr = wfGetDB( DB_SLAVE );
00099         $conds = array();
00100 
00101         // Don't show hidden names
00102         if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
00103             $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
00104         }
00105 
00106         $options = array();
00107 
00108         if ( $this->requestedGroup != '' ) {
00109             $conds['ug_group'] = $this->requestedGroup;
00110         }
00111 
00112         if ( $this->requestedUser != '' ) {
00113             # Sorted either by account creation or name
00114             if ( $this->creationSort ) {
00115                 $conds[] = 'user_id >= ' . intval( User::idFromName( $this->requestedUser ) );
00116             } else {
00117                 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
00118             }
00119         }
00120 
00121         if ( $this->editsOnly ) {
00122             $conds[] = 'user_editcount > 0';
00123         }
00124 
00125         $options['GROUP BY'] = $this->creationSort ? 'user_id' : 'user_name';
00126 
00127         $query = array(
00128             'tables' => array( 'user', 'user_groups', 'ipblocks' ),
00129             'fields' => array(
00130                 'user_name' => $this->creationSort ? 'MAX(user_name)' : 'user_name',
00131                 'user_id' => $this->creationSort ? 'user_id' : 'MAX(user_id)',
00132                 'edits' => 'MAX(user_editcount)',
00133                 'numgroups' => 'COUNT(ug_group)',
00134                 'singlegroup' => 'MAX(ug_group)', // the usergroup if there is only one
00135                 'creation' => 'MIN(user_registration)',
00136                 'ipb_deleted' => 'MAX(ipb_deleted)' // block/hide status
00137             ),
00138             'options' => $options,
00139             'join_conds' => array(
00140                 'user_groups' => array( 'LEFT JOIN', 'user_id=ug_user' ),
00141                 'ipblocks' => array(
00142                     'LEFT JOIN', array(
00143                         'user_id=ipb_user',
00144                         'ipb_auto' => 0
00145                     )
00146                 ),
00147             ),
00148             'conds' => $conds
00149         );
00150 
00151         wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
00152 
00153         return $query;
00154     }
00155 
00160     function formatRow( $row ) {
00161         if ( $row->user_id == 0 ) { #Bug 16487
00162             return '';
00163         }
00164 
00165         $userName = $row->user_name;
00166 
00167         $ulinks = Linker::userLink( $row->user_id, $userName );
00168         $ulinks .= Linker::userToolLinksRedContribs(
00169             $row->user_id,
00170             $userName,
00171             (int)$row->edits
00172         );
00173 
00174         $lang = $this->getLanguage();
00175 
00176         $groups = '';
00177         $groups_list = self::getGroups( $row->user_id );
00178 
00179         if ( !$this->including && count( $groups_list ) > 0 ) {
00180             $list = array();
00181             foreach ( $groups_list as $group ) {
00182                 $list[] = self::buildGroupLink( $group, $userName );
00183             }
00184             $groups = $lang->commaList( $list );
00185         }
00186 
00187         $item = $lang->specialList( $ulinks, $groups );
00188 
00189         if ( $row->ipb_deleted ) {
00190             $item = "<span class=\"deleted\">$item</span>";
00191         }
00192 
00193         $edits = '';
00194         global $wgEdititis;
00195         if ( !$this->including && $wgEdititis ) {
00196             // @fixme i18n issue: Hardcoded square brackets.
00197             $edits = ' [' .
00198                 $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped() .
00199                 ']';
00200         }
00201 
00202         $created = '';
00203         # Some rows may be null
00204         if ( !$this->including && $row->creation ) {
00205             $user = $this->getUser();
00206             $d = $lang->userDate( $row->creation, $user );
00207             $t = $lang->userTime( $row->creation, $user );
00208             $created = $this->msg( 'usercreated', $d, $t, $row->user_name )->escaped();
00209             $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
00210         }
00211         $blocked = !is_null( $row->ipb_deleted ) ?
00212             ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
00213             '';
00214 
00215         wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
00216 
00217         return Html::rawElement( 'li', array(), "{$item}{$edits}{$created}{$blocked}" );
00218     }
00219 
00220     function doBatchLookups() {
00221         $batch = new LinkBatch();
00222         # Give some pointers to make user links
00223         foreach ( $this->mResult as $row ) {
00224             $batch->add( NS_USER, $row->user_name );
00225             $batch->add( NS_USER_TALK, $row->user_name );
00226         }
00227         $batch->execute();
00228         $this->mResult->rewind();
00229     }
00230 
00234     function getPageHeader() {
00235         global $wgScript;
00236 
00237         list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
00238 
00239         # Form tag
00240         $out = Xml::openElement(
00241             'form',
00242             array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listusers-form' )
00243         ) .
00244             Xml::fieldset( $this->msg( 'listusers' )->text() ) .
00245             Html::hidden( 'title', $self );
00246 
00247         # Username field
00248         $out .= Xml::label( $this->msg( 'listusersfrom' )->text(), 'offset' ) . ' ' .
00249             Html::input(
00250                 'username',
00251                 $this->requestedUser,
00252                 'text',
00253                 array(
00254                     'id' => 'offset',
00255                     'size' => 20,
00256                     'autofocus' => $this->requestedUser === ''
00257                 )
00258             ) . ' ';
00259 
00260         # Group drop-down list
00261         $out .= Xml::label( $this->msg( 'group' )->text(), 'group' ) . ' ' .
00262             Xml::openElement( 'select', array( 'name' => 'group', 'id' => 'group' ) ) .
00263             Xml::option( $this->msg( 'group-all' )->text(), '' );
00264         foreach ( $this->getAllGroups() as $group => $groupText ) {
00265             $out .= Xml::option( $groupText, $group, $group == $this->requestedGroup );
00266         }
00267         $out .= Xml::closeElement( 'select' ) . '<br />';
00268         $out .= Xml::checkLabel(
00269             $this->msg( 'listusers-editsonly' )->text(),
00270             'editsOnly',
00271             'editsOnly',
00272             $this->editsOnly
00273         );
00274         $out .= '&#160;';
00275         $out .= Xml::checkLabel(
00276             $this->msg( 'listusers-creationsort' )->text(),
00277             'creationSort',
00278             'creationSort',
00279             $this->creationSort
00280         );
00281         $out .= '&#160;';
00282         $out .= Xml::checkLabel(
00283             $this->msg( 'listusers-desc' )->text(),
00284             'desc',
00285             'desc',
00286             $this->mDefaultDirection
00287         );
00288         $out .= '<br />';
00289 
00290         wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
00291 
00292         # Submit button and form bottom
00293         $out .= Html::hidden( 'limit', $this->mLimit );
00294         $out .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() );
00295         wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
00296         $out .= Xml::closeElement( 'fieldset' ) .
00297             Xml::closeElement( 'form' );
00298 
00299         return $out;
00300     }
00301 
00306     function getAllGroups() {
00307         $result = array();
00308         foreach ( User::getAllGroups() as $group ) {
00309             $result[$group] = User::getGroupName( $group );
00310         }
00311         asort( $result );
00312 
00313         return $result;
00314     }
00315 
00320     function getDefaultQuery() {
00321         $query = parent::getDefaultQuery();
00322         if ( $this->requestedGroup != '' ) {
00323             $query['group'] = $this->requestedGroup;
00324         }
00325         if ( $this->requestedUser != '' ) {
00326             $query['username'] = $this->requestedUser;
00327         }
00328         wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
00329 
00330         return $query;
00331     }
00332 
00339     protected static function getGroups( $uid ) {
00340         $user = User::newFromId( $uid );
00341         $groups = array_diff( $user->getEffectiveGroups(), User::getImplicitGroups() );
00342 
00343         return $groups;
00344     }
00345 
00353     protected static function buildGroupLink( $group, $username ) {
00354         return User::makeGroupLinkHtml(
00355             $group,
00356             htmlspecialchars( User::getGroupMember( $group, $username ) )
00357         );
00358     }
00359 }
00360 
00364 class SpecialListUsers extends IncludableSpecialPage {
00368     public function __construct() {
00369         parent::__construct( 'Listusers' );
00370     }
00371 
00377     public function execute( $par ) {
00378         $this->setHeaders();
00379         $this->outputHeader();
00380 
00381         $up = new UsersPager( $this->getContext(), $par, $this->including() );
00382 
00383         # getBody() first to check, if empty
00384         $usersbody = $up->getBody();
00385 
00386         $s = '';
00387         if ( !$this->including() ) {
00388             $s = $up->getPageHeader();
00389         }
00390 
00391         if ( $usersbody ) {
00392             $s .= $up->getNavigationBar();
00393             $s .= Html::rawElement( 'ul', array(), $usersbody );
00394             $s .= $up->getNavigationBar();
00395         } else {
00396             $s .= $this->msg( 'listusers-noresult' )->parseAsBlock();
00397         }
00398 
00399         $this->getOutput()->addHTML( $s );
00400     }
00401 
00402     protected function getGroupName() {
00403         return 'users';
00404     }
00405 }