| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Pager; |
| 23 |
|
| 24 |
use MediaWiki\Block\HideUserUtils; |
| 25 |
use MediaWiki\Cache\LinkBatchFactory; |
| 26 |
use MediaWiki\Context\IContextSource; |
| 27 |
use MediaWiki\HookContainer\HookContainer; |
| 28 |
use MediaWiki\Html\FormOptions; |
| 29 |
use MediaWiki\Html\Html; |
| 30 |
use MediaWiki\Linker\Linker; |
| 31 |
use MediaWiki\MainConfigNames; |
| 32 |
use MediaWiki\Title\Title; |
| 33 |
use MediaWiki\User\UserGroupManager; |
| 34 |
use MediaWiki\User\UserIdentityLookup; |
| 35 |
use MediaWiki\User\UserIdentityValue; |
| 36 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 37 |
use Wikimedia\Rdbms\Subquery; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
class ActiveUsersPager extends UsersPager { |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
protected $opts; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
protected $groups; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private $blockStatusByUid; |
| 61 |
|
| 62 |
|
| 63 |
private $RCMaxAge; |
| 64 |
|
| 65 |
|
| 66 |
private $excludegroups; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function __construct( |
| 79 |
IContextSource $context, |
| 80 |
HookContainer $hookContainer, |
| 81 |
LinkBatchFactory $linkBatchFactory, |
| 82 |
IConnectionProvider $dbProvider, |
| 83 |
UserGroupManager $userGroupManager, |
| 84 |
UserIdentityLookup $userIdentityLookup, |
| 85 |
HideUserUtils $hideUserUtils, |
| 86 |
FormOptions $opts |
| 87 |
) { |
| 88 |
parent::__construct( |
| 89 |
$context, |
| 90 |
$hookContainer, |
| 91 |
$linkBatchFactory, |
| 92 |
$dbProvider, |
| 93 |
$userGroupManager, |
| 94 |
$userIdentityLookup, |
| 95 |
$hideUserUtils, |
| 96 |
null, |
| 97 |
null |
| 98 |
); |
| 99 |
|
| 100 |
$this->RCMaxAge = $this->getConfig()->get( MainConfigNames::ActiveUserDays ); |
| 101 |
$this->requestedUser = ''; |
| 102 |
|
| 103 |
$un = $opts->getValue( 'username' ); |
| 104 |
if ( $un != '' ) { |
| 105 |
$username = Title::makeTitleSafe( NS_USER, $un ); |
| 106 |
if ( $username !== null ) { |
| 107 |
$this->requestedUser = $username->getText(); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
$this->groups = $opts->getValue( 'groups' ); |
| 112 |
$this->excludegroups = $opts->getValue( 'excludegroups' ); |
| 113 |
|
| 114 |
if ( $opts->getValue( 'hidebots' ) ) { |
| 115 |
$this->excludegroups[] = 'bot'; |
| 116 |
} |
| 117 |
if ( $opts->getValue( 'hidesysops' ) ) { |
| 118 |
$this->excludegroups[] = 'sysop'; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
public function getIndexField() { |
| 123 |
return 'qcc_title'; |
| 124 |
} |
| 125 |
|
| 126 |
public function getQueryInfo( $data = null ) { |
| 127 |
$dbr = $this->getDatabase(); |
| 128 |
|
| 129 |
$activeUserSeconds = $this->getConfig()->get( MainConfigNames::ActiveUserDays ) * 86400; |
| 130 |
$timestamp = $dbr->timestamp( (int)wfTimestamp( TS_UNIX ) - $activeUserSeconds ); |
| 131 |
$fname = __METHOD__ . ' (' . $this->getSqlComment() . ')'; |
| 132 |
|
| 133 |
|
| 134 |
$subquery = $dbr->newSelectQueryBuilder() |
| 135 |
->select( [ 'qcc_title', 'user_id', 'actor_id' ] ) |
| 136 |
->from( 'querycachetwo' ) |
| 137 |
->join( 'user', null, 'user_name = qcc_title' ) |
| 138 |
->join( 'actor', null, 'actor_user = user_id' ) |
| 139 |
->where( [ |
| 140 |
'qcc_type' => 'activeusers', |
| 141 |
'qcc_namespace' => NS_USER, |
| 142 |
] ) |
| 143 |
->caller( $fname ); |
| 144 |
if ( $data !== null ) { |
| 145 |
$subquery |
| 146 |
->orderBy( 'qcc_title', $data['order'] ) |
| 147 |
->limit( $data['limit'] ) |
| 148 |
->andWhere( $data['conds'] ); |
| 149 |
} |
| 150 |
if ( $this->requestedUser != '' ) { |
| 151 |
$subquery->andWhere( $dbr->expr( 'qcc_title', '>=', $this->requestedUser ) ); |
| 152 |
} |
| 153 |
if ( $this->groups !== [] ) { |
| 154 |
$subquery |
| 155 |
->join( 'user_groups', 'ug1', 'ug1.ug_user = user_id' ) |
| 156 |
->andWhere( [ |
| 157 |
'ug1.ug_group' => $this->groups, |
| 158 |
$dbr->expr( 'ug1.ug_expiry', '=', null )->or( 'ug1.ug_expiry', '>=', $dbr->timestamp() ), |
| 159 |
] ); |
| 160 |
} |
| 161 |
if ( $this->excludegroups !== [] ) { |
| 162 |
$subquery |
| 163 |
->leftJoin( 'user_groups', 'ug2', [ |
| 164 |
'ug2.ug_user = user_id', |
| 165 |
'ug2.ug_group' => $this->excludegroups, |
| 166 |
$dbr->expr( 'ug2.ug_expiry', '=', null )->or( 'ug2.ug_expiry', '>=', $dbr->timestamp() ), |
| 167 |
] ) |
| 168 |
->andWhere( [ 'ug2.ug_user' => null ] ); |
| 169 |
} |
| 170 |
if ( !$this->canSeeHideuser() ) { |
| 171 |
$subquery->andWhere( $this->hideUserUtils->getExpression( $dbr ) ); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
return [ |
| 176 |
'tables' => [ 'qcc_users' => new Subquery( $subquery->getSQL() ), 'recentchanges' ], |
| 177 |
'fields' => [ |
| 178 |
'qcc_title', |
| 179 |
'user_name' => 'qcc_title', |
| 180 |
'user_id' => 'user_id', |
| 181 |
'recentedits' => 'COUNT(DISTINCT rc_id)' |
| 182 |
], |
| 183 |
'options' => [ 'GROUP BY' => [ 'qcc_title', 'user_id' ] ], |
| 184 |
'conds' => [], |
| 185 |
'join_conds' => [ 'recentchanges' => [ 'LEFT JOIN', [ |
| 186 |
'rc_actor = actor_id', |
| 187 |
$dbr->expr( 'rc_type', '!=', RC_EXTERNAL ), |
| 188 |
$dbr->expr( 'rc_type', '!=', RC_CATEGORIZE ), |
| 189 |
$dbr->expr( 'rc_log_type', '=', null )->or( 'rc_log_type', '!=', 'newusers' ), |
| 190 |
$dbr->expr( 'rc_timestamp', '>=', $timestamp ), |
| 191 |
] ] ], |
| 192 |
]; |
| 193 |
} |
| 194 |
|
| 195 |
protected function buildQueryInfo( $offset, $limit, $order ) { |
| 196 |
$fname = __METHOD__ . ' (' . $this->getSqlComment() . ')'; |
| 197 |
|
| 198 |
$sortColumns = array_merge( [ $this->mIndexField ], $this->mExtraSortFields ); |
| 199 |
if ( $order === self::QUERY_ASCENDING ) { |
| 200 |
$dir = 'ASC'; |
| 201 |
$orderBy = $sortColumns; |
| 202 |
$operator = $this->mIncludeOffset ? '>=' : '>'; |
| 203 |
} else { |
| 204 |
$dir = 'DESC'; |
| 205 |
$orderBy = []; |
| 206 |
foreach ( $sortColumns as $col ) { |
| 207 |
$orderBy[] = $col . ' DESC'; |
| 208 |
} |
| 209 |
$operator = $this->mIncludeOffset ? '<=' : '<'; |
| 210 |
} |
| 211 |
$info = $this->getQueryInfo( [ |
| 212 |
'limit' => intval( $limit ), |
| 213 |
'order' => $dir, |
| 214 |
'conds' => |
| 215 |
$offset != '' ? [ $this->getDatabase()->expr( $this->mIndexField, $operator, $offset ) ] : [], |
| 216 |
] ); |
| 217 |
|
| 218 |
$tables = $info['tables']; |
| 219 |
$fields = $info['fields']; |
| 220 |
$conds = $info['conds']; |
| 221 |
$options = $info['options']; |
| 222 |
$join_conds = $info['join_conds']; |
| 223 |
$options['ORDER BY'] = $orderBy; |
| 224 |
return [ $tables, $fields, $conds, $fname, $options, $join_conds ]; |
| 225 |
} |
| 226 |
|
| 227 |
protected function doBatchLookups() { |
| 228 |
parent::doBatchLookups(); |
| 229 |
|
| 230 |
$uids = []; |
| 231 |
foreach ( $this->mResult as $row ) { |
| 232 |
$uids[] = (int)$row->user_id; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
$dbr = $this->getDatabase(); |
| 239 |
$res = $dbr->newSelectQueryBuilder() |
| 240 |
->select( [ |
| 241 |
'bt_user', |
| 242 |
'deleted' => 'MAX(bl_deleted)', |
| 243 |
'sitewide' => 'MAX(bl_sitewide)' |
| 244 |
] ) |
| 245 |
->from( 'block_target' ) |
| 246 |
->join( 'block', null, 'bl_target=bt_id' ) |
| 247 |
->where( [ 'bt_user' => $uids ] ) |
| 248 |
->groupBy( [ 'bt_user' ] ) |
| 249 |
->caller( __METHOD__ )->fetchResultSet(); |
| 250 |
$this->blockStatusByUid = []; |
| 251 |
foreach ( $res as $row ) { |
| 252 |
$this->blockStatusByUid[$row->bt_user] = [ |
| 253 |
'deleted' => $row->deleted, |
| 254 |
'sitewide' => $row->sitewide, |
| 255 |
]; |
| 256 |
} |
| 257 |
$this->mResult->seek( 0 ); |
| 258 |
} |
| 259 |
|
| 260 |
public function formatRow( $row ) { |
| 261 |
$userName = $row->user_name; |
| 262 |
|
| 263 |
$ulinks = Linker::userLink( $row->user_id, $userName ); |
| 264 |
$ulinks .= Linker::userToolLinks( |
| 265 |
$row->user_id, |
| 266 |
$userName, |
| 267 |
|
| 268 |
false, |
| 269 |
|
| 270 |
0, |
| 271 |
|
| 272 |
null, |
| 273 |
|
| 274 |
false |
| 275 |
); |
| 276 |
|
| 277 |
$lang = $this->getLanguage(); |
| 278 |
|
| 279 |
$list = []; |
| 280 |
|
| 281 |
$userIdentity = new UserIdentityValue( intval( $row->user_id ), $userName ); |
| 282 |
$ugms = $this->getGroupMemberships( $userIdentity ); |
| 283 |
foreach ( $ugms as $ugm ) { |
| 284 |
$list[] = $this->buildGroupLink( $ugm, $userName ); |
| 285 |
} |
| 286 |
|
| 287 |
$groups = $lang->commaList( $list ); |
| 288 |
|
| 289 |
$item = $lang->specialList( $ulinks, $groups ); |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
$blocked = ''; |
| 294 |
$isBlocked = isset( $this->blockStatusByUid[$row->user_id] ); |
| 295 |
if ( $isBlocked ) { |
| 296 |
if ( $this->blockStatusByUid[$row->user_id]['deleted'] == 1 ) { |
| 297 |
$item = "<span class=\"deleted\">$item</span>"; |
| 298 |
} |
| 299 |
if ( $this->blockStatusByUid[$row->user_id]['sitewide'] == 1 ) { |
| 300 |
$blocked = ' ' . $this->msg( 'listusers-blocked', $userName )->escaped(); |
| 301 |
} |
| 302 |
} |
| 303 |
$count = $this->msg( 'activeusers-count' )->numParams( $row->recentedits ) |
| 304 |
->params( $userName )->numParams( $this->RCMaxAge )->escaped(); |
| 305 |
|
| 306 |
return Html::rawElement( 'li', [], "{$item} [{$count}]{$blocked}" ); |
| 307 |
} |
| 308 |
|
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
class_alias( ActiveUsersPager::class, 'ActiveUsersPager' ); |