Merge "Drop index oi_name_archive_name on table oldimage"
[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 if ( !$this->getConfig()->get( 'DisableUserGroupExpiry' ) ) {
116 $conds[] = 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() );
117 }
118 }
119
120 if ( $this->requestedUser != '' ) {
121 # Sorted either by account creation or name
122 if ( $this->creationSort ) {
123 $conds[] = 'user_id >= ' . intval( User::idFromName( $this->requestedUser ) );
124 } else {
125 $conds[] = 'user_name >= ' . $dbr->addQuotes( $this->requestedUser );
126 }
127 }
128
129 if ( $this->editsOnly ) {
130 $conds[] = 'user_editcount > 0';
131 }
132
133 $options['GROUP BY'] = $this->creationSort ? 'user_id' : 'user_name';
134
135 $query = [
136 'tables' => [ 'user', 'user_groups', 'ipblocks' ],
137 'fields' => [
138 'user_name' => $this->creationSort ? 'MAX(user_name)' : 'user_name',
139 'user_id' => $this->creationSort ? 'user_id' : 'MAX(user_id)',
140 'edits' => 'MAX(user_editcount)',
141 'creation' => 'MIN(user_registration)',
142 'ipb_deleted' => 'MAX(ipb_deleted)' // block/hide status
143 ],
144 'options' => $options,
145 'join_conds' => [
146 'user_groups' => [ 'LEFT JOIN', 'user_id=ug_user' ],
147 'ipblocks' => [
148 'LEFT JOIN', [
149 'user_id=ipb_user',
150 'ipb_auto' => 0
151 ]
152 ],
153 ],
154 'conds' => $conds
155 ];
156
157 Hooks::run( 'SpecialListusersQueryInfo', [ $this, &$query ] );
158
159 return $query;
160 }
161
162 /**
163 * @param stdClass $row
164 * @return string
165 */
166 function formatRow( $row ) {
167 if ( $row->user_id == 0 ) { # T18487
168 return '';
169 }
170
171 $userName = $row->user_name;
172
173 $ulinks = Linker::userLink( $row->user_id, $userName );
174 $ulinks .= Linker::userToolLinksRedContribs(
175 $row->user_id,
176 $userName,
177 (int)$row->edits
178 );
179
180 $lang = $this->getLanguage();
181
182 $groups = '';
183 $ugms = self::getGroupMemberships( intval( $row->user_id ), $this->userGroupCache );
184
185 if ( !$this->including && count( $ugms ) > 0 ) {
186 $list = [];
187 foreach ( $ugms as $ugm ) {
188 $list[] = $this->buildGroupLink( $ugm, $userName );
189 }
190 $groups = $lang->commaList( $list );
191 }
192
193 $item = $lang->specialList( $ulinks, $groups );
194
195 if ( $row->ipb_deleted ) {
196 $item = "<span class=\"deleted\">$item</span>";
197 }
198
199 $edits = '';
200 if ( !$this->including && $this->getConfig()->get( 'Edititis' ) ) {
201 $count = $this->msg( 'usereditcount' )->numParams( $row->edits )->escaped();
202 $edits = $this->msg( 'word-separator' )->escaped() . $this->msg( 'brackets', $count )->escaped();
203 }
204
205 $created = '';
206 # Some rows may be null
207 if ( !$this->including && $row->creation ) {
208 $user = $this->getUser();
209 $d = $lang->userDate( $row->creation, $user );
210 $t = $lang->userTime( $row->creation, $user );
211 $created = $this->msg( 'usercreated', $d, $t, $row->user_name )->escaped();
212 $created = ' ' . $this->msg( 'parentheses' )->rawParams( $created )->escaped();
213 }
214 $blocked = !is_null( $row->ipb_deleted ) ?
215 ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() :
216 '';
217
218 Hooks::run( 'SpecialListusersFormatRow', [ &$item, $row ] );
219
220 return Html::rawElement( 'li', [], "{$item}{$edits}{$created}{$blocked}" );
221 }
222
223 function doBatchLookups() {
224 $batch = new LinkBatch();
225 $userIds = [];
226 # Give some pointers to make user links
227 foreach ( $this->mResult as $row ) {
228 $batch->add( NS_USER, $row->user_name );
229 $batch->add( NS_USER_TALK, $row->user_name );
230 $userIds[] = $row->user_id;
231 }
232
233 // Lookup groups for all the users
234 $dbr = wfGetDB( DB_REPLICA );
235 $groupRes = $dbr->select(
236 'user_groups',
237 UserGroupMembership::selectFields(),
238 [ 'ug_user' => $userIds ],
239 __METHOD__
240 );
241 $cache = [];
242 $groups = [];
243 foreach ( $groupRes as $row ) {
244 $ugm = UserGroupMembership::newFromRow( $row );
245 if ( !$ugm->isExpired() ) {
246 $cache[$row->ug_user][$row->ug_group] = $ugm;
247 $groups[$row->ug_group] = true;
248 }
249 }
250
251 // Give extensions a chance to add things like global user group data
252 // into the cache array to ensure proper output later on
253 Hooks::run( 'UsersPagerDoBatchLookups', [ $dbr, $userIds, &$cache, &$groups ] );
254
255 $this->userGroupCache = $cache;
256
257 // Add page of groups to link batch
258 foreach ( $groups as $group => $unused ) {
259 $groupPage = UserGroupMembership::getGroupPage( $group );
260 if ( $groupPage ) {
261 $batch->addObj( $groupPage );
262 }
263 }
264
265 $batch->execute();
266 $this->mResult->rewind();
267 }
268
269 /**
270 * @return string
271 */
272 function getPageHeader() {
273 list( $self ) = explode( '/', $this->getTitle()->getPrefixedDBkey() );
274
275 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
276
277 # Form tag
278 $out = Xml::openElement(
279 'form',
280 [ 'method' => 'get', 'action' => wfScript(), 'id' => 'mw-listusers-form' ]
281 ) .
282 Xml::fieldset( $this->msg( 'listusers' )->text() ) .
283 Html::hidden( 'title', $self );
284
285 # Username field (with autocompletion support)
286 $out .= Xml::label( $this->msg( 'listusersfrom' )->text(), 'offset' ) . ' ' .
287 Html::input(
288 'username',
289 $this->requestedUser,
290 'text',
291 [
292 'class' => 'mw-autocomplete-user',
293 'id' => 'offset',
294 'size' => 20,
295 'autofocus' => $this->requestedUser === ''
296 ]
297 ) . ' ';
298
299 # Group drop-down list
300 $sel = new XmlSelect( 'group', 'group', $this->requestedGroup );
301 $sel->addOption( $this->msg( 'group-all' )->text(), '' );
302 foreach ( $this->getAllGroups() as $group => $groupText ) {
303 $sel->addOption( $groupText, $group );
304 }
305
306 $out .= Xml::label( $this->msg( 'group' )->text(), 'group' ) . ' ';
307 $out .= $sel->getHTML() . '<br />';
308 $out .= Xml::checkLabel(
309 $this->msg( 'listusers-editsonly' )->text(),
310 'editsOnly',
311 'editsOnly',
312 $this->editsOnly
313 );
314 $out .= '&#160;';
315 $out .= Xml::checkLabel(
316 $this->msg( 'listusers-creationsort' )->text(),
317 'creationSort',
318 'creationSort',
319 $this->creationSort
320 );
321 $out .= '&#160;';
322 $out .= Xml::checkLabel(
323 $this->msg( 'listusers-desc' )->text(),
324 'desc',
325 'desc',
326 $this->mDefaultDirection
327 );
328 $out .= '<br />';
329
330 Hooks::run( 'SpecialListusersHeaderForm', [ $this, &$out ] );
331
332 # Submit button and form bottom
333 $out .= Html::hidden( 'limit', $this->mLimit );
334 $out .= Xml::submitButton( $this->msg( 'listusers-submit' )->text() );
335 Hooks::run( 'SpecialListusersHeader', [ $this, &$out ] );
336 $out .= Xml::closeElement( 'fieldset' ) .
337 Xml::closeElement( 'form' );
338
339 return $out;
340 }
341
342 /**
343 * Get a list of all explicit groups
344 * @return array
345 */
346 function getAllGroups() {
347 $result = [];
348 foreach ( User::getAllGroups() as $group ) {
349 $result[$group] = UserGroupMembership::getGroupName( $group );
350 }
351 asort( $result );
352
353 return $result;
354 }
355
356 /**
357 * Preserve group and username offset parameters when paging
358 * @return array
359 */
360 function getDefaultQuery() {
361 $query = parent::getDefaultQuery();
362 if ( $this->requestedGroup != '' ) {
363 $query['group'] = $this->requestedGroup;
364 }
365 if ( $this->requestedUser != '' ) {
366 $query['username'] = $this->requestedUser;
367 }
368 Hooks::run( 'SpecialListusersDefaultQuery', [ $this, &$query ] );
369
370 return $query;
371 }
372
373 /**
374 * Get an associative array containing groups the specified user belongs to,
375 * and the relevant UserGroupMembership objects
376 *
377 * @param int $uid User id
378 * @param array|null $cache
379 * @return array (group name => UserGroupMembership object)
380 */
381 protected static function getGroupMemberships( $uid, $cache = null ) {
382 if ( $cache === null ) {
383 $user = User::newFromId( $uid );
384 return $user->getGroupMemberships();
385 } else {
386 return isset( $cache[$uid] ) ? $cache[$uid] : [];
387 }
388 }
389
390 /**
391 * Format a link to a group description page
392 *
393 * @param string|UserGroupMembership $group Group name or UserGroupMembership object
394 * @param string $username Username
395 * @return string
396 */
397 protected function buildGroupLink( $group, $username ) {
398 return UserGroupMembership::getLink( $group, $this->getContext(), 'html', $username );
399 }
400 }