Update to r32375 / bug 11874 -- !important may have whitespace between ! and important
[lhc/web/wiklou.git] / includes / SpecialListusers.php
1 <?php
2
3 # Copyright (C) 2004 Brion Vibber, lcrocker, Tim Starling,
4 # Domas Mituzas, Ashar Voultoiz, Jens Frank, Zhengzhu.
5 #
6 # © 2006 Rob Church <robchur@gmail.com>
7 #
8 # http://www.mediawiki.org/
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along
21 # with this program; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 # http://www.gnu.org/copyleft/gpl.html
24 /**
25 *
26 * @addtogroup SpecialPage
27 */
28
29 /**
30 * This class is used to get a list of user. The ones with specials
31 * rights (sysop, bureaucrat, developer) will have them displayed
32 * next to their names.
33 *
34 * @addtogroup SpecialPage
35 */
36
37 class UsersPager extends AlphabeticPager {
38
39 function __construct($group=null) {
40 global $wgRequest;
41 $this->requestedGroup = $group != "" ? $group : $wgRequest->getVal( 'group' );
42 $un = $wgRequest->getText( 'username' );
43 $this->requestedUser = '';
44 if ( $un != '' ) {
45 $username = Title::makeTitleSafe( NS_USER, $un );
46 if( ! is_null( $username ) ) {
47 $this->requestedUser = $username->getText();
48 }
49 }
50 parent::__construct();
51 }
52
53
54 function getIndexField() {
55 return 'user_name';
56 }
57
58 function getQueryInfo() {
59 $conds=array();
60 // don't show hidden names
61 $conds[]='ipb_deleted IS NULL OR ipb_deleted = 0';
62 if ($this->requestedGroup != "") {
63 $conds['ug_group'] = $this->requestedGroup;
64 }
65 if ($this->requestedUser != "") {
66 $conds[] = 'user_name >= ' . wfGetDB()->addQuotes( $this->requestedUser );
67 }
68
69 list ($user,$user_groups,$ipblocks) = wfGetDB()->tableNamesN('user','user_groups','ipblocks');
70
71 $query = array(
72 'tables' => " $user LEFT JOIN $user_groups ON user_id=ug_user LEFT JOIN $ipblocks ON user_id=ipb_user AND ipb_auto=0 ",
73 'fields' => array('user_name',
74 'MAX(user_id) AS user_id',
75 'COUNT(ug_group) AS numgroups',
76 'MAX(ug_group) AS singlegroup'),
77 'options' => array('GROUP BY' => 'user_name'),
78 'conds' => $conds
79 );
80
81 wfRunHooks( 'SpecialListusersQueryInfo', array( $this, &$query ) );
82 return $query;
83 }
84
85 function formatRow( $row ) {
86 $userPage = Title::makeTitle( NS_USER, $row->user_name );
87 $name = $this->getSkin()->makeLinkObj( $userPage, htmlspecialchars( $userPage->getText() ) );
88
89 if( $row->numgroups > 1 || ( $this->requestedGroup && $row->numgroups == 1 ) ) {
90 $list = array();
91 foreach( self::getGroups( $row->user_id ) as $group )
92 $list[] = self::buildGroupLink( $group );
93 $groups = implode( ', ', $list );
94 } elseif( $row->numgroups == 1 ) {
95 $groups = self::buildGroupLink( $row->singlegroup );
96 } else {
97 $groups = '';
98 }
99
100 $item = wfSpecialList( $name, $groups );
101 wfRunHooks( 'SpecialListusersFormatRow', array( &$item, $row ) );
102 return "<li>{$item}</li>";
103 }
104
105 function getBody() {
106 if (!$this->mQueryDone) {
107 $this->doQuery();
108 }
109 $batch = new LinkBatch;
110
111 $this->mResult->rewind();
112
113 while ( $row = $this->mResult->fetchObject() ) {
114 $batch->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
115 }
116 $batch->execute();
117 $this->mResult->rewind();
118 return parent::getBody();
119 }
120
121 function getPageHeader( ) {
122 global $wgScript, $wgRequest;
123 $self = $this->getTitle();
124
125 # Form tag
126 $out = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) ) .
127 '<fieldset>' .
128 Xml::element( 'legend', array(), wfMsg( 'listusers' ) );
129 $out .= Xml::hidden( 'title', $self->getPrefixedDbKey() );
130
131 # Username field
132 $out .= Xml::label( wfMsg( 'listusersfrom' ), 'offset' ) . ' ' .
133 Xml::input( 'username', 20, $this->requestedUser, array( 'id' => 'offset' ) ) . ' ';
134
135 # Group drop-down list
136 $out .= Xml::label( wfMsg( 'group' ), 'group' ) . ' ' .
137 Xml::openElement('select', array( 'name' => 'group', 'id' => 'group' ) ) .
138 Xml::option( wfMsg( 'group-all' ), '' );
139 foreach( User::getAllGroups() as $group )
140 $out .= Xml::option( User::getGroupName( $group ), $group, $group == $this->requestedGroup );
141 $out .= Xml::closeElement( 'select' ) . ' ';
142
143 wfRunHooks( 'SpecialListusersHeaderForm', array( $this, &$out ) );
144
145 # Submit button and form bottom
146 if( $this->mLimit )
147 $out .= Xml::hidden( 'limit', $this->mLimit );
148 $out .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
149 wfRunHooks( 'SpecialListusersHeader', array( $this, &$out ) );
150 $out .= '</fieldset>' .
151 Xml::closeElement( 'form' );
152
153 return $out;
154 }
155
156 /**
157 * Preserve group and username offset parameters when paging
158 * @return array
159 */
160 function getDefaultQuery() {
161 $query = parent::getDefaultQuery();
162 if( $this->requestedGroup != '' )
163 $query['group'] = $this->requestedGroup;
164 if( $this->requestedUser != '' )
165 $query['username'] = $this->requestedUser;
166 wfRunHooks( 'SpecialListusersDefaultQuery', array( $this, &$query ) );
167 return $query;
168 }
169
170 /**
171 * Get a list of groups the specified user belongs to
172 *
173 * @param int $uid
174 * @return array
175 */
176 private static function getGroups( $uid ) {
177 $dbr = wfGetDB( DB_SLAVE );
178 $groups = array();
179 $res = $dbr->select( 'user_groups', 'ug_group', array( 'ug_user' => $uid ), __METHOD__ );
180 if( $res && $dbr->numRows( $res ) > 0 ) {
181 while( $row = $dbr->fetchObject( $res ) )
182 $groups[] = $row->ug_group;
183 $dbr->freeResult( $res );
184 }
185 return $groups;
186 }
187
188 /**
189 * Format a link to a group description page
190 *
191 * @param string $group
192 * @return string
193 */
194 private static function buildGroupLink( $group ) {
195 static $cache = array();
196 if( !isset( $cache[$group] ) )
197 $cache[$group] = User::makeGroupLinkHtml( $group, User::getGroupMember( $group ) );
198 return $cache[$group];
199 }
200 }
201
202 /**
203 * constructor
204 * $par string (optional) A group to list users from
205 */
206 function wfSpecialListusers( $par = null ) {
207 global $wgRequest, $wgOut;
208
209 $up = new UsersPager($par);
210
211 # getBody() first to check, if empty
212 $usersbody = $up->getBody();
213 $s = $up->getPageHeader();
214 if( $usersbody ) {
215 $s .= $up->getNavigationBar();
216 $s .= '<ul>' . $usersbody . '</ul>';
217 $s .= $up->getNavigationBar() ;
218 } else {
219 $s .= '<p>' . wfMsgHTML('listusers-noresult') . '</p>';
220 };
221
222 $wgOut->addHTML( $s );
223 }