| 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\Html\Html; |
| 24 |
use MediaWiki\MainConfigNames; |
| 25 |
use MediaWiki\Parser\Sanitizer; |
| 26 |
use MediaWiki\SiteStats\SiteStats; |
| 27 |
use MediaWiki\SpecialPage\SpecialPage; |
| 28 |
use MediaWiki\Title\Title; |
| 29 |
use MediaWiki\User\UserGroupManager; |
| 30 |
use MediaWiki\User\UserGroupMembership; |
| 31 |
use MediaWiki\Xml\Xml; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class SpecialStatistics extends SpecialPage { |
| 40 |
private int $edits; |
| 41 |
private int $good; |
| 42 |
private int $images; |
| 43 |
private int $total; |
| 44 |
private int $users; |
| 45 |
private int $activeUsers; |
| 46 |
|
| 47 |
private UserGroupManager $userGroupManager; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function __construct( UserGroupManager $userGroupManager ) { |
| 53 |
parent::__construct( 'Statistics' ); |
| 54 |
$this->userGroupManager = $userGroupManager; |
| 55 |
} |
| 56 |
|
| 57 |
public function execute( $par ) { |
| 58 |
$this->setHeaders(); |
| 59 |
$this->outputHeader(); |
| 60 |
$this->getOutput()->addModuleStyles( 'mediawiki.special' ); |
| 61 |
|
| 62 |
$this->edits = SiteStats::edits(); |
| 63 |
$this->good = SiteStats::articles(); |
| 64 |
$this->images = SiteStats::images(); |
| 65 |
$this->total = SiteStats::pages(); |
| 66 |
$this->users = SiteStats::users(); |
| 67 |
$this->activeUsers = SiteStats::activeUsers(); |
| 68 |
|
| 69 |
$text = Xml::openElement( 'table', [ 'class' => 'wikitable mw-statistics-table' ] ); |
| 70 |
|
| 71 |
|
| 72 |
$text .= $this->getPageStats(); |
| 73 |
|
| 74 |
|
| 75 |
$text .= $this->getEditStats(); |
| 76 |
|
| 77 |
|
| 78 |
$text .= $this->getUserStats(); |
| 79 |
|
| 80 |
|
| 81 |
$text .= $this->getGroupStats(); |
| 82 |
|
| 83 |
|
| 84 |
$extraStats = []; |
| 85 |
if ( $this->getHookRunner()->onSpecialStatsAddExtra( |
| 86 |
$extraStats, $this->getContext() ) |
| 87 |
) { |
| 88 |
$text .= $this->getOtherStats( $extraStats ); |
| 89 |
} |
| 90 |
|
| 91 |
$text .= Xml::closeElement( 'table' ); |
| 92 |
|
| 93 |
|
| 94 |
$footer = $this->msg( 'statistics-footer' ); |
| 95 |
if ( !$footer->isBlank() ) { |
| 96 |
$text .= "\n" . $footer->parse(); |
| 97 |
} |
| 98 |
|
| 99 |
$this->getOutput()->addHTML( $text ); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private function formatRow( $text, $number, $trExtraParams = [], |
| 112 |
$descMsg = '', $descMsgParam = '' |
| 113 |
) { |
| 114 |
if ( $descMsg ) { |
| 115 |
$msg = $this->msg( $descMsg, $descMsgParam ); |
| 116 |
if ( !$msg->isDisabled() ) { |
| 117 |
$descriptionHtml = $this->msg( 'parentheses' )->rawParams( $msg->parse() ) |
| 118 |
->escaped(); |
| 119 |
$text .= "<br />" . Html::rawElement( |
| 120 |
'small', |
| 121 |
[ 'class' => 'mw-statistic-desc' ], |
| 122 |
" $descriptionHtml" |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return Html::rawElement( 'tr', $trExtraParams, |
| 128 |
Html::rawElement( 'td', [], $text ) . |
| 129 |
Html::rawElement( 'td', [ 'class' => 'mw-statistics-numbers' ], $number ) |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
private function getPageStats() { |
| 139 |
$linkRenderer = $this->getLinkRenderer(); |
| 140 |
|
| 141 |
$specialAllPagesTitle = SpecialPage::getTitleFor( 'Allpages' ); |
| 142 |
$pageStatsHtml = Html::rawElement( 'tr', [], |
| 143 |
Xml::tags( 'th', [ 'colspan' => '2' ], |
| 144 |
$this->msg( 'statistics-header-pages' )->parse() |
| 145 |
) ) . |
| 146 |
$this->formatRow( |
| 147 |
$this->getConfig()->get( MainConfigNames::MiserMode ) |
| 148 |
? $this->msg( 'statistics-articles' )->escaped() |
| 149 |
: $linkRenderer->makeKnownLink( |
| 150 |
$specialAllPagesTitle, |
| 151 |
$this->msg( 'statistics-articles' )->text(), |
| 152 |
[], [ 'hideredirects' => 1 ] ), |
| 153 |
$this->getLanguage()->formatNum( $this->good ), |
| 154 |
[ 'class' => 'mw-statistics-articles' ], |
| 155 |
'statistics-articles-desc' ) . |
| 156 |
$this->formatRow( $linkRenderer->makeKnownLink( $specialAllPagesTitle, |
| 157 |
$this->msg( 'statistics-pages' )->text() ), |
| 158 |
$this->getLanguage()->formatNum( $this->total ), |
| 159 |
[ 'class' => 'mw-statistics-pages' ], |
| 160 |
'statistics-pages-desc' ); |
| 161 |
|
| 162 |
|
| 163 |
if ( $this->images !== 0 || $this->getConfig()->get( MainConfigNames::EnableUploads ) ) { |
| 164 |
$pageStatsHtml .= $this->formatRow( |
| 165 |
$linkRenderer->makeKnownLink( SpecialPage::getTitleFor( 'MediaStatistics' ), |
| 166 |
$this->msg( 'statistics-files' )->text() ), |
| 167 |
$this->getLanguage()->formatNum( $this->images ), |
| 168 |
[ 'class' => 'mw-statistics-files' ], 'statistics-files-desc' ); |
| 169 |
} |
| 170 |
|
| 171 |
return $pageStatsHtml; |
| 172 |
} |
| 173 |
|
| 174 |
private function getEditStats() { |
| 175 |
return Html::rawElement( 'tr', [], |
| 176 |
Xml::tags( 'th', [ 'colspan' => '2' ], |
| 177 |
$this->msg( 'statistics-header-edits' )->parse() |
| 178 |
) ) . |
| 179 |
$this->formatRow( $this->msg( 'statistics-edits' )->parse(), |
| 180 |
$this->getLanguage()->formatNum( $this->edits ), |
| 181 |
[ 'class' => 'mw-statistics-edits' ] |
| 182 |
) . |
| 183 |
$this->formatRow( $this->msg( 'statistics-edits-average' )->parse(), |
| 184 |
$this->getLanguage()->formatNum( |
| 185 |
sprintf( '%.2f', $this->total ? $this->edits / $this->total : 0 ) |
| 186 |
), [ 'class' => 'mw-statistics-edits-average' ] |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
private function getUserStats() { |
| 191 |
return Html::rawElement( 'tr', [], |
| 192 |
Xml::tags( 'th', [ 'colspan' => '2' ], |
| 193 |
$this->msg( 'statistics-header-users' )->parse() |
| 194 |
) ) . |
| 195 |
$this->formatRow( $this->msg( 'statistics-users' )->parse() . ' ' . |
| 196 |
$this->getLinkRenderer()->makeKnownLink( |
| 197 |
SpecialPage::getTitleFor( 'Listusers' ), |
| 198 |
$this->msg( 'listgrouprights-members' )->text() |
| 199 |
), |
| 200 |
$this->getLanguage()->formatNum( $this->users ), |
| 201 |
[ 'class' => 'mw-statistics-users' ] |
| 202 |
) . |
| 203 |
$this->formatRow( $this->msg( 'statistics-users-active' )->parse() . ' ' . |
| 204 |
$this->getLinkRenderer()->makeKnownLink( |
| 205 |
SpecialPage::getTitleFor( 'Activeusers' ), |
| 206 |
$this->msg( 'listgrouprights-members' )->text() |
| 207 |
), |
| 208 |
$this->getLanguage()->formatNum( $this->activeUsers ), |
| 209 |
[ 'class' => 'mw-statistics-users-active' ], |
| 210 |
'statistics-users-active-desc', |
| 211 |
$this->getLanguage()->formatNum( |
| 212 |
$this->getConfig()->get( MainConfigNames::ActiveUserDays ) ) |
| 213 |
); |
| 214 |
} |
| 215 |
|
| 216 |
private function getGroupStats() { |
| 217 |
$linkRenderer = $this->getLinkRenderer(); |
| 218 |
$lang = $this->getLanguage(); |
| 219 |
$text = ''; |
| 220 |
foreach ( $this->userGroupManager->listAllGroups() as $group ) { |
| 221 |
$groupnameLocalized = $lang->getGroupName( $group ); |
| 222 |
$linkTarget = UserGroupMembership::getGroupPage( $group ) |
| 223 |
?: Title::makeTitleSafe( NS_PROJECT, $group ); |
| 224 |
|
| 225 |
if ( $linkTarget ) { |
| 226 |
$grouppage = $linkRenderer->makeLink( |
| 227 |
$linkTarget, |
| 228 |
$groupnameLocalized |
| 229 |
); |
| 230 |
} else { |
| 231 |
$grouppage = htmlspecialchars( $groupnameLocalized ); |
| 232 |
} |
| 233 |
|
| 234 |
$grouplink = $linkRenderer->makeKnownLink( |
| 235 |
SpecialPage::getTitleFor( 'Listusers' ), |
| 236 |
$this->msg( 'listgrouprights-members' )->text(), |
| 237 |
[], |
| 238 |
[ 'group' => $group ] |
| 239 |
); |
| 240 |
|
| 241 |
$classZero = ''; |
| 242 |
$countUsers = SiteStats::numberingroup( $group ); |
| 243 |
if ( $countUsers == 0 ) { |
| 244 |
$classZero = ' statistics-group-zero'; |
| 245 |
} |
| 246 |
$text .= $this->formatRow( $grouppage . ' ' . $grouplink, |
| 247 |
$this->getLanguage()->formatNum( $countUsers ), |
| 248 |
[ 'class' => 'statistics-group-' . Sanitizer::escapeClass( $group ) . |
| 249 |
$classZero ] ); |
| 250 |
} |
| 251 |
|
| 252 |
return $text; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
private function getOtherStats( array $stats ) { |
| 263 |
$return = ''; |
| 264 |
|
| 265 |
foreach ( $stats as $header => $items ) { |
| 266 |
|
| 267 |
if ( is_array( $items ) ) { |
| 268 |
|
| 269 |
if ( $header !== 'statistics-header-hooks' ) { |
| 270 |
$return .= $this->formatRowHeader( $header ); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
foreach ( $items as $key => $value ) { |
| 275 |
if ( is_array( $value ) ) { |
| 276 |
$name = $value['name']; |
| 277 |
$number = $value['number']; |
| 278 |
} else { |
| 279 |
$name = $this->msg( $key )->parse(); |
| 280 |
$number = $value; |
| 281 |
} |
| 282 |
|
| 283 |
$return .= $this->formatRow( |
| 284 |
$name, |
| 285 |
$this->getLanguage()->formatNum( htmlspecialchars( $number ) ), |
| 286 |
[ 'class' => 'mw-statistics-hook', 'id' => 'mw-' . $key ] |
| 287 |
); |
| 288 |
} |
| 289 |
} else { |
| 290 |
|
| 291 |
if ( $return === '' ) { |
| 292 |
$return .= $this->formatRowHeader( 'statistics-header-hooks' ); |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
$return .= $this->getOtherStats( [ 'statistics-header-hooks' => |
| 297 |
[ $header => $items ] ] ); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
return $return; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
private function formatRowHeader( $header ) { |
| 311 |
return Html::rawElement( 'tr', [], |
| 312 |
Xml::tags( 'th', [ 'colspan' => '2' ], $this->msg( $header )->parse() ) |
| 313 |
); |
| 314 |
} |
| 315 |
|
| 316 |
protected function getGroupName() { |
| 317 |
return 'wiki'; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
class_alias( SpecialStatistics::class, 'SpecialStatistics' ); |