| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Specials; |
| 22 |
|
| 23 |
use MediaWiki\Block\HideUserUtils; |
| 24 |
use MediaWiki\Cache\LinkBatchFactory; |
| 25 |
use MediaWiki\Html\FormOptions; |
| 26 |
use MediaWiki\Html\Html; |
| 27 |
use MediaWiki\HTMLForm\HTMLForm; |
| 28 |
use MediaWiki\MainConfigNames; |
| 29 |
use MediaWiki\Pager\ActiveUsersPager; |
| 30 |
use MediaWiki\SpecialPage\SpecialPage; |
| 31 |
use MediaWiki\User\UserGroupManager; |
| 32 |
use MediaWiki\User\UserIdentityLookup; |
| 33 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
class SpecialActiveUsers extends SpecialPage { |
| 41 |
|
| 42 |
private LinkBatchFactory $linkBatchFactory; |
| 43 |
private IConnectionProvider $dbProvider; |
| 44 |
private UserGroupManager $userGroupManager; |
| 45 |
private UserIdentityLookup $userIdentityLookup; |
| 46 |
private HideUserUtils $hideUserUtils; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct( |
| 56 |
LinkBatchFactory $linkBatchFactory, |
| 57 |
IConnectionProvider $dbProvider, |
| 58 |
UserGroupManager $userGroupManager, |
| 59 |
UserIdentityLookup $userIdentityLookup, |
| 60 |
HideUserUtils $hideUserUtils |
| 61 |
) { |
| 62 |
parent::__construct( 'Activeusers' ); |
| 63 |
$this->linkBatchFactory = $linkBatchFactory; |
| 64 |
$this->dbProvider = $dbProvider; |
| 65 |
$this->userGroupManager = $userGroupManager; |
| 66 |
$this->userIdentityLookup = $userIdentityLookup; |
| 67 |
$this->hideUserUtils = $hideUserUtils; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function execute( $par ) { |
| 74 |
$out = $this->getOutput(); |
| 75 |
|
| 76 |
$this->setHeaders(); |
| 77 |
$this->outputHeader(); |
| 78 |
|
| 79 |
$opts = new FormOptions(); |
| 80 |
|
| 81 |
$opts->add( 'username', '' ); |
| 82 |
$opts->add( 'groups', [] ); |
| 83 |
$opts->add( 'excludegroups', [] ); |
| 84 |
|
| 85 |
$opts->add( 'hidebots', false, FormOptions::BOOL ); |
| 86 |
$opts->add( 'hidesysops', false, FormOptions::BOOL ); |
| 87 |
|
| 88 |
$opts->fetchValuesFromRequest( $this->getRequest() ); |
| 89 |
|
| 90 |
if ( $par !== null ) { |
| 91 |
$opts->setValue( 'username', $par ); |
| 92 |
} |
| 93 |
|
| 94 |
$pager = new ActiveUsersPager( |
| 95 |
$this->getContext(), |
| 96 |
$this->getHookContainer(), |
| 97 |
$this->linkBatchFactory, |
| 98 |
$this->dbProvider, |
| 99 |
$this->userGroupManager, |
| 100 |
$this->userIdentityLookup, |
| 101 |
$this->hideUserUtils, |
| 102 |
$opts |
| 103 |
); |
| 104 |
$usersBody = $pager->getBody(); |
| 105 |
|
| 106 |
$this->buildForm(); |
| 107 |
|
| 108 |
if ( $usersBody ) { |
| 109 |
$out->addHTML( |
| 110 |
$pager->getNavigationBar() . |
| 111 |
Html::rawElement( 'ul', [], $usersBody ) . |
| 112 |
$pager->getNavigationBar() |
| 113 |
); |
| 114 |
$out->addModuleStyles( 'mediawiki.interface.helpers.styles' ); |
| 115 |
} else { |
| 116 |
$out->addWikiMsg( 'activeusers-noresult' ); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
protected function buildForm() { |
| 124 |
$groups = $this->userGroupManager->listAllGroups(); |
| 125 |
|
| 126 |
$options = []; |
| 127 |
$lang = $this->getLanguage(); |
| 128 |
foreach ( $groups as $group ) { |
| 129 |
$msg = htmlspecialchars( $lang->getGroupName( $group ) ); |
| 130 |
$options[$msg] = $group; |
| 131 |
} |
| 132 |
ksort( $options ); |
| 133 |
|
| 134 |
|
| 135 |
$req = $this->getRequest(); |
| 136 |
$excludeDefault = []; |
| 137 |
if ( $req->getCheck( 'hidebots' ) ) { |
| 138 |
$excludeDefault[] = 'bot'; |
| 139 |
} |
| 140 |
if ( $req->getCheck( 'hidesysops' ) ) { |
| 141 |
$excludeDefault[] = 'sysop'; |
| 142 |
} |
| 143 |
|
| 144 |
$formDescriptor = [ |
| 145 |
'username' => [ |
| 146 |
'type' => 'user', |
| 147 |
'name' => 'username', |
| 148 |
'label-message' => 'activeusers-from', |
| 149 |
], |
| 150 |
'groups' => [ |
| 151 |
'type' => 'multiselect', |
| 152 |
'dropdown' => true, |
| 153 |
'flatlist' => true, |
| 154 |
'name' => 'groups', |
| 155 |
'label-message' => 'activeusers-groups', |
| 156 |
'options' => $options, |
| 157 |
], |
| 158 |
'excludegroups' => [ |
| 159 |
'type' => 'multiselect', |
| 160 |
'dropdown' => true, |
| 161 |
'flatlist' => true, |
| 162 |
'name' => 'excludegroups', |
| 163 |
'label-message' => 'activeusers-excludegroups', |
| 164 |
'options' => $options, |
| 165 |
'default' => $excludeDefault, |
| 166 |
], |
| 167 |
]; |
| 168 |
|
| 169 |
HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() ) |
| 170 |
|
| 171 |
->setFormIdentifier( 'specialactiveusers' ) |
| 172 |
->setPreHtml( $this->getIntroText() ) |
| 173 |
->setWrapperLegendMsg( 'activeusers' ) |
| 174 |
->setSubmitTextMsg( 'activeusers-submit' ) |
| 175 |
|
| 176 |
->setTitle( $this->getPageTitle() ) |
| 177 |
->setMethod( 'get' ) |
| 178 |
->prepareForm() |
| 179 |
->displayForm( false ); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
protected function getIntroText() { |
| 187 |
$days = $this->getConfig()->get( MainConfigNames::ActiveUserDays ); |
| 188 |
|
| 189 |
$intro = $this->msg( 'activeusers-intro' )->numParams( $days )->parse(); |
| 190 |
|
| 191 |
|
| 192 |
$dbr = $this->dbProvider->getReplicaDatabase(); |
| 193 |
|
| 194 |
$rcMax = $dbr->newSelectQueryBuilder() |
| 195 |
->select( 'MAX(rc_timestamp)' ) |
| 196 |
->from( 'recentchanges' ) |
| 197 |
->caller( __METHOD__ )->fetchField(); |
| 198 |
if ( $rcMax ) { |
| 199 |
$cTime = $dbr->newSelectQueryBuilder() |
| 200 |
->select( 'qci_timestamp' ) |
| 201 |
->from( 'querycache_info' ) |
| 202 |
->where( [ 'qci_type' => 'activeusers' ] ) |
| 203 |
->caller( __METHOD__ )->fetchField(); |
| 204 |
if ( $cTime ) { |
| 205 |
$secondsOld = (int)wfTimestamp( TS_UNIX, $rcMax ) - (int)wfTimestamp( TS_UNIX, $cTime ); |
| 206 |
} else { |
| 207 |
$rcMin = $dbr->newSelectQueryBuilder() |
| 208 |
->select( 'MIN(rc_timestamp)' ) |
| 209 |
->from( 'recentchanges' ) |
| 210 |
->caller( __METHOD__ )->fetchField(); |
| 211 |
$secondsOld = time() - (int)wfTimestamp( TS_UNIX, $rcMin ); |
| 212 |
} |
| 213 |
if ( $secondsOld > 0 ) { |
| 214 |
$intro .= $this->msg( 'cachedspecial-viewing-cached-ttl' ) |
| 215 |
->durationParams( $secondsOld )->parseAsBlock(); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
return $intro; |
| 220 |
} |
| 221 |
|
| 222 |
protected function getGroupName() { |
| 223 |
return 'users'; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
class_alias( SpecialActiveUsers::class, 'SpecialActiveUsers' ); |