Improve performance of ActiveUsersPager query
[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 $useActor = (bool)(
83 $this->getConfig()->get( 'ActorTableSchemaMigrationStage' ) & SCHEMA_COMPAT_READ_NEW
84 );
85
86 $activeUserSeconds = $this->getConfig()->get( 'ActiveUserDays' ) * 86400;
87 $timestamp = $dbr->timestamp( wfTimestamp( TS_UNIX ) - $activeUserSeconds );
88 $fname = __METHOD__ . ' (' . $this->getSqlComment() . ')';
89
90 // Inner subselect to pull the active users out of querycachetwo
91 $tables = [ 'querycachetwo', 'user' ];
92 $fields = [ 'qcc_title', 'user_id' ];
93 $jconds = [
94 'user' => [ 'JOIN', 'user_name = qcc_title' ],
95 ];
96 $conds = [
97 'qcc_type' => 'activeusers',
98 'qcc_namespace' => NS_USER,
99 ];
100 $options = [];
101 if ( $data !== null ) {
102 $options['ORDER BY'] = 'qcc_title ' . $data['dir'];
103 $options['LIMIT'] = $data['limit'];
104 $conds = array_merge( $conds, $data['conds'] );
105 }
106 if ( $this->requestedUser != '' ) {
107 $conds[] = 'qcc_title >= ' . $dbr->addQuotes( $this->requestedUser );
108 }
109 if ( $this->groups !== [] ) {
110 $tables['ug1'] = 'user_groups';
111 $jconds['ug1'] = [ 'JOIN', 'ug1.ug_user = user_id' ];
112 $conds['ug1.ug_group'] = $this->groups;
113 $conds[] = 'ug1.ug_expiry IS NULL OR ug1.ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() );
114 }
115 if ( $this->excludegroups !== [] ) {
116 $tables['ug2'] = 'user_groups';
117 $jconds['ug2'] = [ 'LEFT JOIN', [
118 'ug2.ug_user = user_id',
119 'ug2.ug_group' => $this->excludegroups,
120 'ug2.ug_expiry IS NULL OR ug2.ug_expiry >= ' . $dbr->addQuotes( $dbr->timestamp() ),
121 ] ];
122 $conds['ug2.ug_user'] = null;
123 }
124 if ( !$this->getUser()->isAllowed( 'hideuser' ) ) {
125 $conds[] = 'NOT EXISTS (' . $dbr->selectSQLText(
126 'ipblocks', '1', [ 'ipb_user=user_id', 'ipb_deleted' => 1 ]
127 ) . ')';
128 }
129 if ( $useActor ) {
130 $tables[] = 'actor';
131 $jconds['actor'] = [
132 'JOIN',
133 'actor_user = user_id',
134 ];
135 $fields[] = 'actor_id';
136 }
137 $subquery = $dbr->buildSelectSubquery( $tables, $fields, $conds, $fname, $options, $jconds );
138
139 // Outer query to select the recent edit counts for the selected active users
140 $tables = [ 'qcc_users' => $subquery, 'recentchanges' ];
141 $jconds = [ 'recentchanges' => [
142 'JOIN', $useActor ? 'rc_actor = actor_id' : 'rc_user_text = qcc_title',
143 ] ];
144 $conds = [
145 'rc_type != ' . $dbr->addQuotes( RC_EXTERNAL ), // Don't count wikidata.
146 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE ), // Don't count categorization changes.
147 'rc_log_type IS NULL OR rc_log_type != ' . $dbr->addQuotes( 'newusers' ),
148 'rc_timestamp >= ' . $dbr->addQuotes( $timestamp ),
149 ];
150
151 return [
152 'tables' => $tables,
153 'fields' => [
154 'qcc_title',
155 'user_name' => 'qcc_title',
156 'user_id' => 'user_id',
157 'recentedits' => 'COUNT(*)'
158 ],
159 'options' => [ 'GROUP BY' => [ 'qcc_title' ] ],
160 'conds' => $conds,
161 'join_conds' => $jconds,
162 ];
163 }
164
165 protected function buildQueryInfo( $offset, $limit, $descending ) {
166 $fname = __METHOD__ . ' (' . $this->getSqlComment() . ')';
167
168 $sortColumns = array_merge( [ $this->mIndexField ], $this->mExtraSortFields );
169 if ( $descending ) {
170 $orderBy = $sortColumns;
171 $operator = $this->mIncludeOffset ? '>=' : '>';
172 } else {
173 $orderBy = [];
174 foreach ( $sortColumns as $col ) {
175 $orderBy[] = $col . ' DESC';
176 }
177 $operator = $this->mIncludeOffset ? '<=' : '<';
178 }
179 $info = $this->getQueryInfo( [
180 'limit' => intval( $limit ),
181 'order' => $descending ? 'DESC' : 'ASC',
182 'conds' =>
183 $offset != '' ? [ $this->mIndexField . $operator . $this->mDb->addQuotes( $offset ) ] : [],
184 ] );
185
186 $tables = $info['tables'];
187 $fields = $info['fields'];
188 $conds = $info['conds'];
189 $options = $info['options'];
190 $join_conds = $info['join_conds'];
191 $options['ORDER BY'] = $orderBy;
192 return [ $tables, $fields, $conds, $fname, $options, $join_conds ];
193 }
194
195 protected function doBatchLookups() {
196 parent::doBatchLookups();
197
198 $uids = [];
199 foreach ( $this->mResult as $row ) {
200 $uids[] = $row->user_id;
201 }
202 // Fetch the block status of the user for showing "(blocked)" text and for
203 // striking out names of suppressed users when privileged user views the list.
204 // Although the first query already hits the block table for un-privileged, this
205 // is done in two queries to avoid huge quicksorts and to make COUNT(*) correct.
206 $dbr = $this->getDatabase();
207 $res = $dbr->select( 'ipblocks',
208 [ 'ipb_user', 'MAX(ipb_deleted) AS deleted, MAX(ipb_sitewide) AS sitewide' ],
209 [ 'ipb_user' => $uids ],
210 __METHOD__,
211 [ 'GROUP BY' => [ 'ipb_user' ] ]
212 );
213 $this->blockStatusByUid = [];
214 foreach ( $res as $row ) {
215 $this->blockStatusByUid[$row->ipb_user] = [
216 'deleted' => $row->deleted,
217 'sitewide' => $row->sitewide,
218 ];
219 }
220 $this->mResult->seek( 0 );
221 }
222
223 function formatRow( $row ) {
224 $userName = $row->user_name;
225
226 $ulinks = Linker::userLink( $row->user_id, $userName );
227 $ulinks .= Linker::userToolLinks( $row->user_id, $userName );
228
229 $lang = $this->getLanguage();
230
231 $list = [];
232 $user = User::newFromId( $row->user_id );
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 }