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 /**
47 * @param IContextSource|null $context
48 * @param FormOptions $opts
49 */
50 public function __construct( IContextSource $context = null, FormOptions $opts ) {
51 parent::__construct( $context );
52
53 $this->RCMaxAge = $this->getConfig()->get( 'ActiveUserDays' );
54 $this->requestedUser = '';
55
56 $un = $opts->getValue( 'username' );
57 if ( $un != '' ) {
58 $username = Title::makeTitleSafe( NS_USER, $un );
59 if ( !is_null( $username ) ) {
60 $this->requestedUser = $username->getText();
61 }
62 }
63
64 $this->groups = $opts->getValue( 'groups' );
65 $this->excludegroups = $opts->getValue( 'excludegroups' );
66 // Backwards-compatibility with old URLs
67 if ( $opts->getValue( 'hidebots' ) ) {
68 $this->excludegroups[] = 'bot';
69 }
70 if ( $opts->getValue( 'hidesysops' ) ) {
71 $this->excludegroups[] = 'sysop';
72 }
73 }
74
75 function getIndexField() {
76 return 'qcc_title';
77 }
78
79 function getQueryInfo( $data = null ) {
80 $dbr = $this->getDatabase();
81
82 $activeUserSeconds = $this->getConfig()->get( 'ActiveUserDays' ) * 86400;
83 $timestamp = $dbr->timestamp( wfTimestamp( TS_UNIX ) - $activeUserSeconds );
84 $fname = __METHOD__ . ' (' . $this->getSqlComment() . ')';
85
86 // Inner subselect to pull the active users out of querycachetwo
87 $tables = [ 'querycachetwo', 'user', 'actor' ];
88 $fields = [ 'qcc_title', 'user_id', 'actor_id' ];
89 $jconds = [
90 'user' => [ 'JOIN', 'user_name = qcc_title' ],
91 'actor' => [ 'JOIN', 'actor_user = user_id' ],
92 ];
93 $conds = [
94 'qcc_type' => 'activeusers',
95 'qcc_namespace' => NS_USER,
96 ];
97 $options = [];
98 if ( $data !== null ) {
99 $options['ORDER BY'] = 'qcc_title ' . $data['order'];
100 $options['LIMIT'] = $data['limit'];
101 $conds = array_merge( $conds, $data['conds'] );
102 }
103 if ( $this->requestedUser != '' ) {
104 $conds[] = 'qcc_title >= ' . $dbr->addQuotes( $this->requestedUser );
105 }
106 if ( $this->groups !== [] ) {
107 $tables['ug1'] = 'user_groups';
108 $jconds['ug1'] = [ 'JOIN', 'ug1.ug_user = user_id' ];
109 $conds['ug1.ug_group'] = $this->groups;
110 $conds[] = 'ug1.ug_expiry IS NULL OR ug1.ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() );
111 }
112 if ( $this->excludegroups !== [] ) {
113 $tables['ug2'] = 'user_groups';
114 $jconds['ug2'] = [ 'LEFT JOIN', [
115 'ug2.ug_user = user_id',
116 'ug2.ug_group' => $this->excludegroups,
117 'ug2.ug_expiry IS NULL OR ug2.ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
118 ] ];
119 $conds['ug2.ug_user'] = null;
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 $subquery = $dbr->buildSelectSubquery( $tables, $fields, $conds, $fname, $options, $jconds );
127
128 // Outer query to select the recent edit counts for the selected active users
129 $tables = [ 'qcc_users' => $subquery, 'recentchanges' ];
130 $jconds = [ 'recentchanges' => [ 'LEFT JOIN', [
131 'rc_actor = actor_id',
132 'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL ), // Don't count wikidata.
133 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE ), // Don't count categorization changes.
134 'rc_log_type IS NULL OR rc_log_type != ' . $dbr->addQuotes( 'newusers' ),
135 'rc_timestamp >= ' . $dbr->addQuotes( $timestamp ),
136 ] ] ];
137 $conds = [];
138
139 return [
140 'tables' => $tables,
141 'fields' => [
142 'qcc_title',
143 'user_name' => 'qcc_title',
144 'user_id' => 'user_id',
145 'recentedits' => 'COUNT(rc_id)'
146 ],
147 'options' => [ 'GROUP BY' => [ 'qcc_title', 'user_id' ] ],
148 'conds' => $conds,
149 'join_conds' => $jconds,
150 ];
151 }
152
153 protected function buildQueryInfo( $offset, $limit, $order ) {
154 $fname = __METHOD__ . ' (' . $this->getSqlComment() . ')';
155
156 $sortColumns = array_merge( [ $this->mIndexField ], $this->mExtraSortFields );
157 if ( $order === self::QUERY_ASCENDING ) {
158 $dir = 'ASC';
159 $orderBy = $sortColumns;
160 $operator = $this->mIncludeOffset ? '>=' : '>';
161 } else {
162 $dir = 'DESC';
163 $orderBy = [];
164 foreach ( $sortColumns as $col ) {
165 $orderBy[] = $col . ' DESC';
166 }
167 $operator = $this->mIncludeOffset ? '<=' : '<';
168 }
169 $info = $this->getQueryInfo( [
170 'limit' => intval( $limit ),
171 'order' => $dir,
172 'conds' =>
173 $offset != '' ? [ $this->mIndexField . $operator . $this->mDb->addQuotes( $offset ) ] : [],
174 ] );
175
176 $tables = $info['tables'];
177 $fields = $info['fields'];
178 $conds = $info['conds'];
179 $options = $info['options'];
180 $join_conds = $info['join_conds'];
181 $options['ORDER BY'] = $orderBy;
182 return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
183 }
184
185 protected function doBatchLookups() {
186 parent::doBatchLookups();
187
188 $uids = [];
189 foreach ( $this->mResult as $row ) {
190 $uids[] = $row->user_id;
191 }
192 // Fetch the block status of the user for showing "(blocked)" text and for
193 // striking out names of suppressed users when privileged user views the list.
194 // Although the first query already hits the block table for un-privileged, this
195 // is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
196 $dbr = $this->getDatabase();
197 $res = $dbr->select( 'ipblocks',
198 [ 'ipb_user', 'MAX(ipb_deleted) AS deleted, MAX(ipb_sitewide) AS sitewide' ],
199 [ 'ipb_user' => $uids ],
200 __METHOD__,
201 [ 'GROUP BY' => [ 'ipb_user' ] ]
202 );
203 $this->blockStatusByUid = [];
204 foreach ( $res as $row ) {
205 $this->blockStatusByUid[$row->ipb_user] = [
206 'deleted' => $row->deleted,
207 'sitewide' => $row->sitewide,
208 ];
209 }
210 $this->mResult->seek( 0 );
211 }
212
213 function formatRow( $row ) {
214 $userName = $row->user_name;
215
216 $ulinks = Linker::userLink( $row->user_id, $userName );
217 $ulinks .= Linker::userToolLinks(
218 $row->user_id,
219 $userName,
220 // Should the contributions link be red if the user has no edits (using default)
221 false,
222 // Customisation flags (using default 0)
223 0,
224 // User edit count (using default)
225 null,
226 // do not wrap the message in parentheses (CSS will provide these)
227 false
228 );
229
230 $lang = $this->getLanguage();
231
232 $list = [];
233
234 $ugms = self::getGroupMemberships( intval( $row->user_id ), $this->userGroupCache );
235 foreach ( $ugms as $ugm ) {
236 $list[] = $this->buildGroupLink( $ugm, $userName );
237 }
238
239 $groups = $lang->commaList( $list );
240
241 $item = $lang->specialList( $ulinks, $groups );
242
243 // If there is a block, 'deleted' and 'sitewide' are both set on
244 // $this->blockStatusByUid[$row->user_id].
245 $blocked = '';
246 $isBlocked = isset( $this->blockStatusByUid[$row->user_id] );
247 if ( $isBlocked ) {
248 if ( $this->blockStatusByUid[$row->user_id]['deleted'] == 1 ) {
249 $item = "<span class=\"deleted\">$item</span>";
250 }
251 if ( $this->blockStatusByUid[$row->user_id]['sitewide'] == 1 ) {
252 $blocked = ' ' . $this->msg( 'listusers-blocked', $userName )->escaped();
253 }
254 }
255 $count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits )
256 ->params( $userName )->numParams( $this->RCMaxAge )->escaped();
257
258 return Html::rawElement( 'li', [], "{$item} [{$count}]{$blocked}" );
259 }
260
261 }