Merge "Use RemexHtml as the tidy implementation for parser tests"
[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 $context
48 * @param FormOptions $opts
49 */
50 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() {
80 $dbr = $this->getDatabase();
81
82 $rcQuery = ActorMigration::newMigration()->getJoin( 'rc_user' );
83
84 $activeUserSeconds = $this->getConfig()->get( 'ActiveUserDays' ) * 86400;
85 $timestamp = $dbr->timestamp( wfTimestamp( TS_UNIX ) - $activeUserSeconds );
86 $tables = [ 'querycachetwo', 'user', 'recentchanges' ] + $rcQuery['tables'];
87 $jconds = $rcQuery['joins'];
88 $conds = [
89 'qcc_type' => 'activeusers',
90 'qcc_namespace' => NS_USER,
91 'user_name = qcc_title',
92 $rcQuery['fields']['rc_user_text'] . ' = qcc_title',
93 'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL ), // Don't count wikidata.
94 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE ), // Don't count categorization changes.
95 'rc_log_type IS NULL OR rc_log_type != ' . $dbr->addQuotes( 'newusers' ),
96 'rc_timestamp >= ' . $dbr->addQuotes( $timestamp ),
97 ];
98 if ( $this->requestedUser != '' ) {
99 $conds[] = 'qcc_title >= ' . $dbr->addQuotes( $this->requestedUser );
100 }
101 if ( $this->groups !== [] ) {
102 $tables[] = 'user_groups';
103 $conds[] = 'ug_user = user_id';
104 $conds['ug_group'] = $this->groups;
105 $conds[] = 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() );
106 }
107 if ( $this->excludegroups !== [] ) {
108 foreach ( $this->excludegroups as $group ) {
109 $conds[] = 'NOT EXISTS (' . $dbr->selectSQLText(
110 'user_groups', '1', [
111 'ug_user = user_id',
112 'ug_group' => $group,
113 'ug_expiry IS NULL OR ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() )
114 ]
115 ) . ')';
116 }
117 }
118 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
119 $conds[] = 'NOT EXISTS (' . $dbr->selectSQLText(
120 'ipblocks', '1', [ 'ipb_user=user_id', 'ipb_deleted' => 1 ]
121 ) . ')';
122 }
123
124 return [
125 'tables' => $tables,
126 'fields' => [
127 'qcc_title',
128 'user_name' => 'qcc_title',
129 'user_id' => 'MAX(user_id)',
130 'recentedits' => 'COUNT(*)'
131 ],
132 'options' => [ 'GROUP BY' => [ 'qcc_title' ] ],
133 'conds' => $conds,
134 'join_conds' => $jconds,
135 ];
136 }
137
138 function doBatchLookups() {
139 parent::doBatchLookups();
140
141 $uids = [];
142 foreach ( $this->mResult as $row ) {
143 $uids[] = $row->user_id;
144 }
145 // Fetch the block status of the user for showing "(blocked)" text and for
146 // striking out names of suppressed users when privileged user views the list.
147 // Although the first query already hits the block table for un-privileged, this
148 // is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
149 $dbr = $this->getDatabase();
150 $res = $dbr->select( 'ipblocks',
151 [ 'ipb_user', 'MAX(ipb_deleted) AS block_status' ],
152 [ 'ipb_user' => $uids ],
153 __METHOD__,
154 [ 'GROUP BY' => [ 'ipb_user' ] ]
155 );
156 $this->blockStatusByUid = [];
157 foreach ( $res as $row ) {
158 $this->blockStatusByUid[$row->ipb_user] = $row->block_status; // 0 or 1
159 }
160 $this->mResult->seek( 0 );
161 }
162
163 function formatRow( $row ) {
164 $userName = $row->user_name;
165
166 $ulinks = Linker::userLink( $row->user_id, $userName );
167 $ulinks .= Linker::userToolLinks( $row->user_id, $userName );
168
169 $lang = $this->getLanguage();
170
171 $list = [];
172 $user = User::newFromId( $row->user_id );
173
174 $ugms = self::getGroupMemberships( intval( $row->user_id ), $this->userGroupCache );
175 foreach ( $ugms as $ugm ) {
176 $list[] = $this->buildGroupLink( $ugm, $userName );
177 }
178
179 $groups = $lang->commaList( $list );
180
181 $item = $lang->specialList( $ulinks, $groups );
182
183 $isBlocked = isset( $this->blockStatusByUid[$row->user_id] );
184 if ( $isBlocked && $this->blockStatusByUid[$row->user_id] == 1 ) {
185 $item = "<span class=\"deleted\">$item</span>";
186 }
187 $count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits )
188 ->params( $userName )->numParams( $this->RCMaxAge )->escaped();
189 $blocked = $isBlocked ? ' ' . $this->msg( 'listusers-blocked', $userName )->escaped() : '';
190
191 return Html::rawElement( 'li', [], "{$item} [{$count}]{$blocked}" );
192 }
193
194 }