Feature karma: removing a useless feature that I recently committed.
[lhc/web/wiklou.git] / includes / SpecialListusers.php
index 559ef6a..b2a35dd 100644 (file)
@@ -3,17 +3,17 @@
 # Domas Mituzas, Ashar Voultoiz, Jens Frank, Zhengzhu.
 #
 # http://www.mediawiki.org/
-# 
+#
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or 
+# the Free Software Foundation; either version 2 of the License, or
 # (at your option) any later version.
-# 
+#
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 # GNU General Public License for more details.
-# 
+#
 # You should have received a copy of the GNU General Public License along
 # with this program; if not, write to the Free Software Foundation, Inc.,
 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
@@ -40,14 +40,36 @@ require_once('QueryPage.php');
 class ListUsersPage extends QueryPage {
        var $requestedGroup = '';
        var $requestedUser = '';
-       var $previousResult = false;
-       var $concatGroups = '';
-       
+
        function getName() {
                return 'Listusers';
        }
        function isSyndicated() { return false; }
 
+       /**
+        * Not expensive, this class won't work properly with the caching system anyway
+        */
+       function isExpensive() {
+               return false;
+       }
+
+       /**
+        * Fetch user page links and cache their existence
+        */
+       function preprocessResults( &$db, &$res ) {
+               $batch = new LinkBatch;
+               while ( $row = $db->fetchObject( $res ) ) {
+                       $batch->addObj( Title::makeTitleSafe( $row->namespace, $row->title ) );
+               }
+               $batch->execute();
+
+               // Back to start for display
+               if( $db->numRows( $res ) > 0 ) {
+                       // If there are no rows we get an error seeking.
+                       $db->dataSeek( $res, 0 );
+               }
+       }
+
        /**
         * Show a drop down list to select a group as well as a user name
         * search box.
@@ -55,7 +77,7 @@ class ListUsersPage extends QueryPage {
         */
        function getPageHeader( ) {
                global $wgScript;
-               
+
                // Various variables used for the form
                $action = htmlspecialchars( $wgScript );
                $title = Title::makeTitle( NS_SPECIAL, 'Listusers' );
@@ -64,123 +86,138 @@ class ListUsersPage extends QueryPage {
                // form header
                $out = '<form method="get" action="'.$action.'">' .
                                '<input type="hidden" name="title" value="'.$special.'" />' .
-                               'Group: <select name="group">';
-
-               // get all group names and id
-               $dbr = & wfGetDB( DB_SLAVE );
-               $group = $dbr->tableName( 'group' );
-               $sql = "SELECT group_id, group_name FROM $group;";
-               $result = $dbr->query($sql);
-               
+                               wfMsgHtml( 'groups-editgroup-name' ) . '<select name="group">';
+
+               // get group names
+               $groups = User::getAllGroups();
+
                // we want a default empty group
                $out.= '<option value=""></option>';
-               
+
                // build the dropdown list menu using datas from the database
-               while($agroup = $dbr->fetchObject( $result )) {
-                       $selected = ($agroup->group_id == $this->requestedGroup) ? ' selected ' : '' ;
-                       $out.= '<option value="'.$agroup->group_id.'" '.$selected.'>'.$agroup->group_name.'</option>';
+               foreach ( $groups as $group ) {
+                       $selected = ($group == $this->requestedGroup);
+                       $out .= wfElement( 'option',
+                               array_merge(
+                                       array( 'value' => $group ),
+                                       $selected ? array( 'selected' => 'selected' ) : array() ),
+                               User::getGroupName( $group ) );
                }
                $out .= '</select> ';
 
-               $out .= 'User: <input type="text" name="username" /> ';
+               $out .= wfMsgHtml( 'specialloguserlabel' ) . '<input type="text" name="username" /> ';
 
                // OK button, end of form.
-               $out .= '<input type="submit" /></form>';
+               $out .= '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . '" /></form>';
                // congratulations the form is now build
-               return $out;    
+               return $out;
        }
-       
+
        function getSQL() {
                $dbr =& wfGetDB( DB_SLAVE );
-       /* system showing possible actions for users
-               $user = $dbr->tableName( 'user' );
-               $user_rights = $dbr->tableName( 'user_rights' );
-               $userspace = Namespace::getUser();
-               return "SELECT ur_rights as type, $userspace as namespace, user_name as title, " .
-                       "user_name as value FROM $user LEFT JOIN $user_rights ON user_id = ur_user";
-       */
-       /** Show groups instead */
                $user = $dbr->tableName( 'user' );
-               $group = $dbr->tableName( 'group' );
                $user_groups = $dbr->tableName( 'user_groups' );
-               
+
+               // We need to get an 'atomic' list of users, so that we
+               // don't break the list half-way through a user's group set
+               // and so that lists by group will show all group memberships.
+               //
+               // On MySQL 4.1 we could use GROUP_CONCAT to grab group
+               // assignments together with users pretty easily. On other
+               // versions, it's not so easy to do it consistently.
+               // For now we'll just grab the number of memberships, so
+               // we can then do targetted checks on those who are in
+               // non-default groups as we go down the list.
+
                $userspace = NS_USER;
-               $sql = "SELECT group_name as type, $userspace AS namespace, user_name AS title, user_name as value " .
+               $sql = "SELECT 'Listusers' as type, $userspace AS namespace, user_name AS title, " .
+                       "user_name as value, user_id, COUNT(ug_group) as numgroups " .
                        "FROM $user ".
-                       "LEFT JOIN $user_groups ON user_id =ug_user " .
-                       "LEFT JOIN $group ON ug_group = group_id ";
+                       "LEFT JOIN $user_groups ON user_id=ug_user " .
+                       $this->userQueryWhere( $dbr ) .
+                       " GROUP BY user_name";
 
-               if($this->requestedGroup != '') {
-                       $sql .=  "WHERE group_id= '" . IntVal( $this->requestedGroup ) . "' ";
-                       if($this->requestedUser != '') {
-                               $sql .= "AND user_name = " . $dbr->addQuotes( $this->requestedUser ) . ' ';
-                       }
-               } else {
-                       if($this->requestedUser !='') {
-                               $sql .= "WHERE user_name = " . $dbr->addQuotes( $this->requestedUser ) . ' ';
-                       }       
-               }                               
-               
                return $sql;
        }
-       
-       /**
-        * When calling formatResult we output the previous result instead of the
-        * current one. We need an additional step to flush out the last result.
-        */
-       function tryLastResult( ) {
-               return true;
+
+       function userQueryWhere( &$dbr ) {
+               $conds = $this->userQueryConditions();
+               return empty( $conds )
+                       ? ""
+                       : "WHERE " . $dbr->makeList( $conds, LIST_AND );
        }
-       
-       function sortDescending() {
-               return false;
+
+       function userQueryConditions() {
+               $conds = array();
+               if( $this->requestedGroup != '' ) {
+                       $conds['ug_group'] = $this->requestedGroup;
+               }
+               if( $this->requestedUser != '' ) {
+                       $conds['user_name'] = $this->requestedUser;
+               }
+               return $conds;
        }
 
-       function appendGroups($group) {
-               $this->concatGroups     .= $group.' ';  
+       function linkParameters() {
+               $conds = array();
+               if( $this->requestedGroup != '' ) {
+                       $conds['group'] = $this->requestedGroup;
+               }
+               if( $this->requestedUser != '' ) {
+                       $conds['username'] = $this->requestedUser;
+               }
+               return $conds;
        }
 
-       function clearGroups() {
-               $this->concatGroups = '';       
+       function sortDescending() {
+               return false;
        }
-/*
-       var $previousResult = false;
-       var $concatGroups = '';
-*/
+
        function formatResult( $skin, $result ) {
-               global $wgContLang;
-               $name = false;
-
-               if($this->previousResult->title != $result->title && $this->previousResult != false) {
-                       // Different username, give back name(group1,group2)
-                       $name = $skin->makeLink( $wgContLang->getNsText($this->previousResult->namespace) . ':' . $this->previousResult->title, $this->previousResult->title );
-                       $name .= $this->concatGroups ? '('.substr($this->concatGroups,0,-1).')' : '';
-                       $this->clearGroups();
-               }
+               $userPage = Title::makeTitle( $result->namespace, $result->title );
+               $name = $skin->makeLinkObj( $userPage, htmlspecialchars( $userPage->getText() ) );
+
+               if( !isset( $result->numgroups ) || $result->numgroups > 0 ) {
+                       $dbr =& wfGetDB( DB_SLAVE );
+                       $result = $dbr->select( 'user_groups',
+                               array( 'ug_group' ),
+                               array( 'ug_user' => $result->user_id ),
+                               'ListUsersPage::formatResult' );
+                       $groups = array();
+                       while( $row = $dbr->fetchObject( $result ) ) {
+                               $groups[] = User::getGroupName( $row->ug_group );
+                       }
+                       $dbr->freeResult( $result );
 
-               if($result->type != '') {
-               $this->appendGroups( $skin->makeLink( wfMsgForContent( 'administrators' ), $result->type ) );
-               }                       
+                       if( count( $groups ) > 0 ) {
+                               $name .= ' (' .
+                                       $skin->makeLink( wfMsgForContent( 'administrators' ),
+                                               htmlspecialchars( implode( ', ', $groups ) ) ) .
+                                       ')';
+                       }
+               }
 
-               $this->previousResult = $result;
                return $name;
        }
 }
 
 /**
  * constructor
+ * $par string (optional) A group to list users from
  */
-function wfSpecialListusers() {
+function wfSpecialListusers( $par = null ) {
        global $wgRequest;
 
        list( $limit, $offset ) = wfCheckLimits();
 
+
        $slu = new ListUsersPage();
-       
+
        /**
         * Get some parameters
         */
-       $slu->requestedGroup = $wgRequest->getVal('group');
+       $groupTarget = isset($par) ? $par : $wgRequest->getVal( 'group' );
+       $slu->requestedGroup = $groupTarget;
        $slu->requestedUser = $wgRequest->getVal('username');
 
        return $slu->doQuery( $offset, $limit );