Merge "resourceloader: Remove comment about XmlJsCode wrapper"
[lhc/web/wiklou.git] / includes / specials / pagers / ActiveUsersPager.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Pager
20 */
21
22 /**
23 * This class is used to get a list of active users. The ones with specials
24 * rights (sysop, bureaucrat, developer) will have them displayed
25 * next to their names.
26 *
27 * @ingroup Pager
28 */
29 class ActiveUsersPager extends UsersPager {
30
31 /**
32 * @var FormOptions
33 */
34 protected $opts;
35
36 /**
37 * @var string[]
38 */
39 protected $groups;
40
41 /**
42 * @var array
43 */
44 private $blockStatusByUid;
45
46 /** @var int */
47 private $RCMaxAge;
48
49 /** @var string[] */
50 private $excludegroups;
51
52 /**
53 * @param IContextSource|null $context
54 * @param FormOptions $opts
55 */
56 public function __construct( IContextSource $context = null, FormOptions $opts ) {
57 parent::__construct( $context );
58
59 $this->RCMaxAge = $this->getConfig()->get( 'ActiveUserDays' );
60 $this->requestedUser = '';
61
62 $un = $opts->getValue( 'username' );
63 if ( $un != '' ) {
64 $username = Title::makeTitleSafe( NS_USER, $un );
65 if ( !is_null( $username ) ) {
66 $this->requestedUser = $username->getText();
67 }
68 }
69
70 $this->groups = $opts->getValue( 'groups' );
71 $this->excludegroups = $opts->getValue( 'excludegroups' );
72 // Backwards-compatibility with old URLs
73 if ( $opts->getValue( 'hidebots' ) ) {
74 $this->excludegroups[] = 'bot';
75 }
76 if ( $opts->getValue( 'hidesysops' ) ) {
77 $this->excludegroups[] = 'sysop';
78 }
79 }
80
81 function getIndexField() {
82 return 'qcc_title';
83 }
84
85 function getQueryInfo( $data = null ) {
86 $dbr = $this->getDatabase();
87
88 $activeUserSeconds = $this->getConfig()->get( 'ActiveUserDays' ) * 86400;
89 $timestamp = $dbr->timestamp( wfTimestamp( TS_UNIX ) - $activeUserSeconds );
90 $fname = __METHOD__ . ' (' . $this->getSqlComment() . ')';
91
92 // Inner subselect to pull the active users out of querycachetwo
93 $tables = [ 'querycachetwo', 'user', 'actor' ];
94 $fields = [ 'qcc_title', 'user_id', 'actor_id' ];
95 $jconds = [
96 'user' => [ 'JOIN', 'user_name = qcc_title' ],
97 'actor' => [ 'JOIN', 'actor_user = user_id' ],
98 ];
99 $conds = [
100 'qcc_type' => 'activeusers',
101 'qcc_namespace' => NS_USER,
102 ];
103 $options = [];
104 if ( $data !== null ) {
105 $options['ORDER BY'] = 'qcc_title ' . $data['order'];
106 $options['LIMIT'] = $data['limit'];
107 $conds = array_merge( $conds, $data['conds'] );
108 }
109 if ( $this->requestedUser != '' ) {
110 $conds[] = 'qcc_title >= ' . $dbr->addQuotes( $this->requestedUser );
111 }
112 if ( $this->groups !== [] ) {
113 $tables['ug1'] = 'user_groups';
114 $jconds['ug1'] = [ 'JOIN', 'ug1.ug_user = user_id' ];
115 $conds['ug1.ug_group'] = $this->groups;
116 $conds[] = 'ug1.ug_expiry IS NULL OR ug1.ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() );
117 }
118 if ( $this->excludegroups !== [] ) {
119 $tables['ug2'] = 'user_groups';
120 $jconds['ug2'] = [ 'LEFT JOIN', [
121 'ug2.ug_user = user_id',
122 'ug2.ug_group' => $this->excludegroups,
123 'ug2.ug_expiry IS NULL OR ug2.ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
124 ] ];
125 $conds['ug2.ug_user'] = null;
126 }
127 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
128 $conds[] = 'NOT EXISTS (' . $dbr->selectSQLText(
129 'ipblocks', '1', [ 'ipb_user=user_id', 'ipb_deleted' => 1 ]
130 ) . ')';
131 }
132 $subquery = $dbr->buildSelectSubquery( $tables, $fields, $conds, $fname, $options, $jconds );
133
134 // Outer query to select the recent edit counts for the selected active users
135 $tables = [ 'qcc_users' => $subquery, 'recentchanges' ];
136 $jconds = [ 'recentchanges' => [ 'LEFT JOIN', [
137 'rc_actor = actor_id',
138 'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL ), // Don't count wikidata.
139 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE ), // Don't count categorization changes.
140 'rc_log_type IS NULL OR rc_log_type != ' . $dbr->addQuotes( 'newusers' ),
141 'rc_timestamp >= ' . $dbr->addQuotes( $timestamp ),
142 ] ] ];
143 $conds = [];
144
145 return [
146 'tables' => $tables,
147 'fields' => [
148 'qcc_title',
149 'user_name' => 'qcc_title',
150 'user_id' => 'user_id',
151 'recentedits' => 'COUNT(rc_id)'
152 ],
153 'options' => [ 'GROUP BY' => [ 'qcc_title', 'user_id' ] ],
154 'conds' => $conds,
155 'join_conds' => $jconds,
156 ];
157 }
158
159 protected function buildQueryInfo( $offset, $limit, $order ) {
160 $fname = __METHOD__ . ' (' . $this->getSqlComment() . ')';
161
162 $sortColumns = array_merge( [ $this->mIndexField ], $this->mExtraSortFields );
163 if ( $order === self::QUERY_ASCENDING ) {
164 $dir = 'ASC';
165 $orderBy = $sortColumns;
166 $operator = $this->mIncludeOffset ? '>=' : '>';
167 } else {
168 $dir = 'DESC';
169 $orderBy = [];
170 foreach ( $sortColumns as $col ) {
171 $orderBy[] = $col . ' DESC';
172 }
173 $operator = $this->mIncludeOffset ? '<=' : '<';
174 }
175 $info = $this->getQueryInfo( [
176 'limit' => intval( $limit ),
177 'order' => $dir,
178 'conds' =>
179 $offset != '' ? [ $this->mIndexField . $operator . $this->mDb->addQuotes( $offset ) ] : [],
180 ] );
181
182 $tables = $info['tables'];
183 $fields = $info['fields'];
184 $conds = $info['conds'];
185 $options = $info['options'];
186 $join_conds = $info['join_conds'];
187 $options['ORDER BY'] = $orderBy;
188 return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
189 }
190
191 protected function doBatchLookups() {
192 parent::doBatchLookups();
193
194 $uids = [];
195 foreach ( $this->mResult as $row ) {
196 $uids[] = $row->user_id;
197 }
198 // Fetch the block status of the user for showing "(blocked)" text and for
199 // striking out names of suppressed users when privileged user views the list.
200 // Although the first query already hits the block table for un-privileged, this
201 // is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
202 $dbr = $this->getDatabase();
203 $res = $dbr->select( 'ipblocks',
204 [ 'ipb_user', 'MAX(ipb_deleted) AS deleted, MAX(ipb_sitewide) AS sitewide' ],
205 [ 'ipb_user' => $uids ],
206 __METHOD__,
207 [ 'GROUP BY' => [ 'ipb_user' ] ]
208 );
209 $this->blockStatusByUid = [];
210 foreach ( $res as $row ) {
211 $this->blockStatusByUid[$row->ipb_user] = [
212 'deleted' => $row->deleted,
213 'sitewide' => $row->sitewide,
214 ];
215 }
216 $this->mResult->seek( 0 );
217 }
218
219 function formatRow( $row ) {
220 $userName = $row->user_name;
221
222 $ulinks = Linker::userLink( $row->user_id, $userName );
223 $ulinks .= Linker::userToolLinks(
224 $row->user_id,
225 $userName,
226 // Should the contributions link be red if the user has no edits (using default)
227 false,
228 // Customisation flags (using default 0)
229 0,
230 // User edit count (using default)
231 null,
232 // do not wrap the message in parentheses (CSS will provide these)
233 false
234 );
235
236 $lang = $this->getLanguage();
237
238 $list = [];
239
240 $ugms = self::getGroupMemberships( intval( $row->user_id ), $this->userGroupCache );
241 foreach ( $ugms as $ugm ) {
242 $list[] = $this->buildGroupLink( $ugm, $userName );
243 }
244
245 $groups = $lang->commaList( $list );
246
247 $item = $lang->specialList( $ulinks, $groups );
248
249 // If there is a block, 'deleted' and 'sitewide' are both set on
250 // $this->blockStatusByUid[$row->user_id].
251 $blocked = '';
252 $isBlocked = isset( $this->blockStatusByUid[$row->user_id] );
253 if ( $isBlocked ) {
254 if ( $this->blockStatusByUid[$row->user_id]['deleted'] == 1 ) {
255 $item = "<span class=\"deleted\">$item</span>";
256 }
257 if ( $this->blockStatusByUid[$row->user_id]['sitewide'] == 1 ) {
258 $blocked = ' ' . $this->msg( 'listusers-blocked', $userName )->escaped();
259 }
260 }
261 $count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits )
262 ->params( $userName )->numParams( $this->RCMaxAge )->escaped();
263
264 return Html::rawElement( 'li', [], "{$item} [{$count}]{$blocked}" );
265 }
266
267 }