Merge "Revised styling of sister-search sidebar."
[lhc/web/wiklou.git] / includes / specials / pagers / UsersPager.php
1 <?php
2 /**
3 * Copyright © 2004 Brion Vibber, lcrocker, Tim Starling,
4 * Domas Mituzas, Antoine Musso, Jens Frank, Zhengzhu,
5 * 2006 Rob Church <robchur@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Pager
24 */
25
26 /**
27 * This class is used to get a list of user. The ones with specials
28 * rights (sysop, bureaucrat, developer) will have them displayed
29 * next to their names.
30 *
31 * @ingroup Pager
32 */
33 class UsersPager extends AlphabeticPager {
34
35 /**
36 * @var array A array with user ids as key and a array of groups as value
37 */
38 protected $userGroupCache;
39
40 /**
41 * @param IContextSource $context
42 * @param array $par (Default null)
43 * @param bool $including Whether this page is being transcluded in
44 * another page
45 */
46 function __construct( IContextSource $context = null, $par = null, $including = null ) {
47 if ( $context ) {
48 $this->setContext( $context );
49 }
50
51 $request = $this->getRequest();
52 $par = ( $par !== null ) ? $par : '';
53 $parms = explode( '/', $par );
54 $symsForAll = [ '*', 'user' ];
55
56 if ( $parms[0] != '' &&
57 ( in_array( $par, User::getAllGroups() ) || in_array( $par, $symsForAll ) )
58 ) {
59 $this->requestedGroup = $par;
60 $un = $request->getText( 'username' );
61 } elseif ( count( $parms ) == 2 ) {
62 $this->requestedGroup = $parms[0];
63 $un = $parms[1];
64 } else {
65 $this->requestedGroup = $request->getVal( 'group' );
66 $un = ( $par != '' ) ? $par : $request->getText( 'username' );
67 }
68
69 if ( in_array( $this->requestedGroup, $symsForAll ) ) {
70 $this->requestedGroup = '';
71 }
72 $this->editsOnly = $request->getBool( 'editsOnly' );
73 $this->creationSort = $request->getBool( 'creationSort' );
74 $this->including = $including;
75 $this->mDefaultDirection = $request->getBool( 'desc' )
76 ? IndexPager::DIR_DESCENDING
77 : IndexPager::DIR_ASCENDING;
78
79 $this->requestedUser = '';
80
81 if ( $un != '' ) {
82 $username = Title::makeTitleSafe( NS_USER, $un );
83
84 if ( !is_null( $username ) ) {
85 $this->requestedUser = $username->getText();
86 }
87 }
88
89 parent::__construct();
90 }
91
92 /**
93 * @return string
94 */
95 function getIndexField() {
96 return $this->creationSort ? 'user_id' : 'user_name';
97 }
98
99 /**
100 * @return array
101 */
102 function getQueryInfo() {
103 $dbr = wfGetDB( DB_REPLICA );
104 $conds = [];
105
106 // Don't show hidden names
107 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
108 $conds[] = 'ipb_deleted IS NULL OR ipb_deleted = 0';
109 }
110
111 $options = [];
112
113 if ( $this->requestedGroup != '' ) {
114 $conds['ug_group'] = $this->requestedGroup;
115 $conds[] = 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() );
116 }
117
118 if ( $this->requestedUser != '' ) {
119 # Sorted either by account creation or name
120 if ( $this->creationSort ) {
121 $conds[] = 'user_id >= ' . intval( User::idFromName( $this->requestedUser ) );
122 } else {
123 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
124 }
125 }
126
127 if ( $this->editsOnly ) {
128 $conds[] = 'user_editcount > 0';
129 }
130
131 $options['GROUP BY'] = $this->creationSort ? 'user_id' : 'user_name';
132
133 $query = [
134 'tables' => [ 'user', 'user_groups', 'ipblocks' ],
135 'fields' => [
136 'user_name' => $this->creationSort ? 'MAX(user_name)' : 'user_name',
137 'user_id' => $this->creationSort ? 'user_id' : 'MAX(user_id)',
138 'edits' => 'MAX(user_editcount)',
139 'creation' => 'MIN(user_registration)',
140 'ipb_deleted' => 'MAX(ipb_deleted)' // block/hide status
141 ],
142 'options' => $options,
143 'join_conds' => [
144 'user_groups' => [ 'LEFT JOIN', 'user_id=ug_user' ],
145 'ipblocks' => [
146 'LEFT JOIN', [
147 'user_id=ipb_user',
148 'ipb_auto' => 0
149 ]
150 ],
151 ],
152 'conds' => $conds
153 ];
154
155 Hooks::run( 'SpecialListusersQueryInfo', [ $this, &$query ] );
156
157 return $query;
158 }
159
160 /**
161 * @param stdClass $row
162 * @return string
163 */
164 function formatRow( $row ) {
165 if ( $row->user_id == 0 ) { # T18487
166 return '';
167 }
168
169 $userName = $row->user_name;
170
171 $ulinks = Linker::userLink( $row->user_id, $userName );
172 $ulinks .= Linker::userToolLinksRedContribs(
173 $row->user_id,
174 $userName,
175 (int)$row->edits
176 );
177
178 $lang = $this->getLanguage();
179
180 $groups = '';
181 $ugms = self::getGroupMemberships( intval( $row->user_id ), $this->userGroupCache );
182
183 if ( !$this->including && count( $ugms ) > 0 ) {
184 $list = [];
185 foreach ( $ugms as $ugm ) {
186 $list[] = $this->buildGroupLink( $ugm, $userName );
187 }
188 $groups = $lang->commaList( $list );
189 }
190
191 $item = $lang->specialList( $ulinks, $groups );
192
193 if ( $row->ipb_deleted ) {
194 $item = "<span class=\"deleted\">$item</span>";
195 }
196
197 $edits = '';
198 if ( !$this->including && $this->getConfig()->get( 'Edititis' ) ) {
199 $count = $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped();
200 $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
201 }
202
203 $created = '';
204 # Some rows may be null
205 if ( !$this->including && $row->creation ) {
206 $user = $this->getUser();
207 $d = $lang->userDate( $row->creation, $user );
208 $t = $lang->userTime( $row->creation, $user );
209 $created = $this->msg( 'usercreated', $d, $t, $row->user_name )->escaped();
210 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
211 }
212 $blocked = !is_null( $row->ipb_deleted ) ?
213 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
214 '';
215
216 Hooks::run( 'SpecialListusersFormatRow', [ &$item, $row ] );
217
218 return Html::rawElement( 'li', [], "{$item}{$edits}{$created}{$blocked}" );
219 }
220
221 function doBatchLookups() {
222 $batch = new LinkBatch();
223 $userIds = [];
224 # Give some pointers to make user links
225 foreach ( $this->mResult as $row ) {
226 $batch->add( NS_USER, $row->user_name );
227 $batch->add( NS_USER_TALK, $row->user_name );
228 $userIds[] = $row->user_id;
229 }
230
231 // Lookup groups for all the users
232 $dbr = wfGetDB( DB_REPLICA );
233 $groupRes = $dbr->select(
234 'user_groups',
235 UserGroupMembership::selectFields(),
236 [ 'ug_user' => $userIds ],
237 __METHOD__
238 );
239 $cache = [];
240 $groups = [];
241 foreach ( $groupRes as $row ) {
242 $ugm = UserGroupMembership::newFromRow( $row );
243 if ( !$ugm->isExpired() ) {
244 $cache[$row->ug_user][$row->ug_group] = $ugm;
245 $groups[$row->ug_group] = true;
246 }
247 }
248
249 // Give extensions a chance to add things like global user group data
250 // into the cache array to ensure proper output later on
251 Hooks::run( 'UsersPagerDoBatchLookups', [ $dbr, $userIds, &$cache, &$groups ] );
252
253 $this->userGroupCache = $cache;
254
255 // Add page of groups to link batch
256 foreach ( $groups as $group => $unused ) {
257 $groupPage = UserGroupMembership::getGroupPage( $group );
258 if ( $groupPage ) {
259 $batch->addObj( $groupPage );
260 }
261 }
262
263 $batch->execute();
264 $this->mResult->rewind();
265 }
266
267 /**
268 * @return string
269 */
270 function getPageHeader() {
271 list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
272
273 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
274
275 # Form tag
276 $out = Xml::openElement(
277 'form',
278 [ 'method' => 'get', 'action' => wfScript(), 'id' => 'mw-listusers-form' ]
279 ) .
280 Xml::fieldset( $this->msg( 'listusers' )->text() ) .
281 Html::hidden( 'title', $self );
282
283 # Username field (with autocompletion support)
284 $out .= Xml::label( $this->msg( 'listusersfrom' )->text(), 'offset' ) . ' ' .
285 Html::input(
286 'username',
287 $this->requestedUser,
288 'text',
289 [
290 'class' => 'mw-autocomplete-user',
291 'id' => 'offset',
292 'size' => 20,
293 'autofocus' => $this->requestedUser === ''
294 ]
295 ) . ' ';
296
297 # Group drop-down list
298 $sel = new XmlSelect( 'group', 'group', $this->requestedGroup );
299 $sel->addOption( $this->msg( 'group-all' )->text(), '' );
300 foreach ( $this->getAllGroups() as $group => $groupText ) {
301 $sel->addOption( $groupText, $group );
302 }
303
304 $out .= Xml::label( $this->msg( 'group' )->text(), 'group' ) . ' ';
305 $out .= $sel->getHTML() . '<br />';
306 $out .= Xml::checkLabel(
307 $this->msg( 'listusers-editsonly' )->text(),
308 'editsOnly',
309 'editsOnly',
310 $this->editsOnly
311 );
312 $out .= '&#160;';
313 $out .= Xml::checkLabel(
314 $this->msg( 'listusers-creationsort' )->text(),
315 'creationSort',
316 'creationSort',
317 $this->creationSort
318 );
319 $out .= '&#160;';
320 $out .= Xml::checkLabel(
321 $this->msg( 'listusers-desc' )->text(),
322 'desc',
323 'desc',
324 $this->mDefaultDirection
325 );
326 $out .= '<br />';
327
328 Hooks::run( 'SpecialListusersHeaderForm', [ $this, &$out ] );
329
330 # Submit button and form bottom
331 $out .= Html::hidden( 'limit', $this->mLimit );
332 $out .= Xml::submitButton( $this->msg( 'listusers-submit' )->text() );
333 Hooks::run( 'SpecialListusersHeader', [ $this, &$out ] );
334 $out .= Xml::closeElement( 'fieldset' ) .
335 Xml::closeElement( 'form' );
336
337 return $out;
338 }
339
340 /**
341 * Get a list of all explicit groups
342 * @return array
343 */
344 function getAllGroups() {
345 $result = [];
346 foreach ( User::getAllGroups() as $group ) {
347 $result[$group] = UserGroupMembership::getGroupName( $group );
348 }
349 asort( $result );
350
351 return $result;
352 }
353
354 /**
355 * Preserve group and username offset parameters when paging
356 * @return array
357 */
358 function getDefaultQuery() {
359 $query = parent::getDefaultQuery();
360 if ( $this->requestedGroup != '' ) {
361 $query['group'] = $this->requestedGroup;
362 }
363 if ( $this->requestedUser != '' ) {
364 $query['username'] = $this->requestedUser;
365 }
366 Hooks::run( 'SpecialListusersDefaultQuery', [ $this, &$query ] );
367
368 return $query;
369 }
370
371 /**
372 * Get an associative array containing groups the specified user belongs to,
373 * and the relevant UserGroupMembership objects
374 *
375 * @param int $uid User id
376 * @param array|null $cache
377 * @return array (group name => UserGroupMembership object)
378 */
379 protected static function getGroupMemberships( $uid, $cache = null ) {
380 if ( $cache === null ) {
381 $user = User::newFromId( $uid );
382 return $user->getGroupMemberships();
383 } else {
384 return isset( $cache[$uid] ) ? $cache[$uid] : [];
385 }
386 }
387
388 /**
389 * Format a link to a group description page
390 *
391 * @param string|UserGroupMembership $group Group name or UserGroupMembership object
392 * @param string $username Username
393 * @return string
394 */
395 protected function buildGroupLink( $group, $username ) {
396 return UserGroupMembership::getLink( $group, $this->getContext(), 'html', $username );
397 }
398 }