Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[lhc/web/wiklou.git] / includes / specials / pagers / ActiveUsersPager.php
1 <?php
2 /**
3 * Copyright © 2008 Aaron Schulz
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Pager
22 */
23
24 /**
25 * This class is used to get a list of active users. The ones with specials
26 * rights (sysop, bureaucrat, developer) will have them displayed
27 * next to their names.
28 *
29 * @ingroup Pager
30 */
31 class ActiveUsersPager extends UsersPager {
32
33 /**
34 * @var FormOptions
35 */
36 protected $opts;
37
38 /**
39 * @var string[]
40 */
41 protected $groups;
42
43 /**
44 * @var array
45 */
46 private $blockStatusByUid;
47
48 /**
49 * @param IContextSource $context
50 * @param FormOptions $opts
51 */
52 function __construct( IContextSource $context = null, FormOptions $opts ) {
53 parent::__construct( $context );
54
55 $this->RCMaxAge = $this->getConfig()->get( 'ActiveUserDays' );
56 $this->requestedUser = '';
57
58 $un = $opts->getValue( 'username' );
59 if ( $un != '' ) {
60 $username = Title::makeTitleSafe( NS_USER, $un );
61 if ( !is_null( $username ) ) {
62 $this->requestedUser = $username->getText();
63 }
64 }
65
66 $this->groups = $opts->getValue( 'groups' );
67 $this->excludegroups = $opts->getValue( 'excludegroups' );
68 // Backwards-compatibility with old URLs
69 if ( $opts->getValue( 'hidebots' ) ) {
70 $this->excludegroups[] = 'bot';
71 }
72 if ( $opts->getValue( 'hidesysops' ) ) {
73 $this->excludegroups[] = 'sysop';
74 }
75 }
76
77 function getIndexField() {
78 return 'qcc_title';
79 }
80
81 function getQueryInfo() {
82 $dbr = $this->getDatabase();
83
84 $activeUserSeconds = $this->getConfig()->get( 'ActiveUserDays' ) * 86400;
85 $timestamp = $dbr->timestamp( wfTimestamp( TS_UNIX ) - $activeUserSeconds );
86 $tables = [ 'querycachetwo', 'user', 'recentchanges' ];
87 $conds = [
88 'qcc_type' => 'activeusers',
89 'qcc_namespace' => NS_USER,
90 'user_name = qcc_title',
91 'rc_user_text = qcc_title',
92 'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL ), // Don't count wikidata.
93 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE ), // Don't count categorization changes.
94 'rc_log_type IS NULL OR rc_log_type != ' . $dbr->addQuotes( 'newusers' ),
95 'rc_timestamp >= ' . $dbr->addQuotes( $timestamp ),
96 ];
97 if ( $this->requestedUser != '' ) {
98 $conds[] = 'qcc_title >= ' . $dbr->addQuotes( $this->requestedUser );
99 }
100 if ( $this->groups !== [] ) {
101 $tables[] = 'user_groups';
102 $conds[] = 'ug_user = user_id';
103 $conds['ug_group'] = $this->groups;
104 if ( !$this->getConfig()->get( 'DisableUserGroupExpiry' ) ) {
105 $conds[] = 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() );
106 }
107 }
108 if ( $this->excludegroups !== [] ) {
109 foreach ( $this->excludegroups as $group ) {
110 $conds[] = 'NOT EXISTS (' . $dbr->selectSQLText(
111 'user_groups', '1', [
112 'ug_user = user_id',
113 'ug_group' => $group,
114 $this->getConfig()->get( 'DisableUserGroupExpiry' ) ?
115 '1' :
116 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
117 ]
118 ) . ')';
119 }
120 }
121 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
122 $conds[] = 'NOT EXISTS (' . $dbr->selectSQLText(
123 'ipblocks', '1', [ 'ipb_user=user_id', 'ipb_deleted' => 1 ]
124 ) . ')';
125 }
126
127 if ( $dbr->implicitGroupby() ) {
128 $options = [ 'GROUP BY' => [ 'qcc_title' ] ];
129 } else {
130 $options = [ 'GROUP BY' => [ 'user_name', 'user_id', 'qcc_title' ] ];
131 }
132
133 return [
134 'tables' => $tables,
135 'fields' => [ 'user_name', 'user_id', 'recentedits' => 'COUNT(*)', 'qcc_title' ],
136 'options' => $options,
137 'conds' => $conds
138 ];
139 }
140
141 function doBatchLookups() {
142 parent::doBatchLookups();
143
144 $uids = [];
145 foreach ( $this->mResult as $row ) {
146 $uids[] = $row->user_id;
147 }
148 // Fetch the block status of the user for showing "(blocked)" text and for
149 // striking out names of suppressed users when privileged user views the list.
150 // Although the first query already hits the block table for un-privileged, this
151 // is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
152 $dbr = $this->getDatabase();
153 $res = $dbr->select( 'ipblocks',
154 [ 'ipb_user', 'MAX(ipb_deleted) AS block_status' ],
155 [ 'ipb_user' => $uids ],
156 __METHOD__,
157 [ 'GROUP BY' => [ 'ipb_user' ] ]
158 );
159 $this->blockStatusByUid = [];
160 foreach ( $res as $row ) {
161 $this->blockStatusByUid[$row->ipb_user] = $row->block_status; // 0 or 1
162 }
163 $this->mResult->seek( 0 );
164 }
165
166 function formatRow( $row ) {
167 $userName = $row->user_name;
168
169 $ulinks = Linker::userLink( $row->user_id, $userName );
170 $ulinks .= Linker::userToolLinks( $row->user_id, $userName );
171
172 $lang = $this->getLanguage();
173
174 $list = [];
175 $user = User::newFromId( $row->user_id );
176
177 $ugms = self::getGroupMemberships( intval( $row->user_id ), $this->userGroupCache );
178 foreach ( $ugms as $ugm ) {
179 $list[] = $this->buildGroupLink( $ugm, $userName );
180 }
181
182 $groups = $lang->commaList( $list );
183
184 $item = $lang->specialList( $ulinks, $groups );
185
186 $isBlocked = isset( $this->blockStatusByUid[$row->user_id] );
187 if ( $isBlocked && $this->blockStatusByUid[$row->user_id] == 1 ) {
188 $item = "<span class=\"deleted\">$item</span>";
189 }
190 $count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits )
191 ->params( $userName )->numParams( $this->RCMaxAge )->escaped();
192 $blocked = $isBlocked ? ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() : '';
193
194 return Html::rawElement( 'li', [], "{$item} [{$count}]{$blocked}" );
195 }
196
197 }