| 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\Cache\LinkBatchFactory; |
| 24 |
use MediaWiki\Html\Html; |
| 25 |
use MediaWiki\Output\OutputPage; |
| 26 |
use MediaWiki\SpecialPage\QueryPage; |
| 27 |
use MediaWiki\SpecialPage\SpecialPage; |
| 28 |
use Skin; |
| 29 |
use Wikimedia\Mime\MimeAnalyzer; |
| 30 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 31 |
use Wikimedia\Rdbms\IDatabase; |
| 32 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 33 |
use Wikimedia\Rdbms\IResultWrapper; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class SpecialMediaStatistics extends QueryPage { |
| 42 |
|
| 43 |
public const MAX_LIMIT = 5000; |
| 44 |
|
| 45 |
protected int $totalCount = 0; |
| 46 |
protected int $totalBytes = 0; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
protected $totalPerType = 0; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
protected $countPerType = 0; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
protected $totalSize = 0; |
| 62 |
|
| 63 |
private MimeAnalyzer $mimeAnalyzer; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function __construct( |
| 71 |
MimeAnalyzer $mimeAnalyzer, |
| 72 |
IConnectionProvider $dbProvider, |
| 73 |
LinkBatchFactory $linkBatchFactory |
| 74 |
) { |
| 75 |
parent::__construct( 'MediaStatistics' ); |
| 76 |
|
| 77 |
|
| 78 |
$this->limit = self::MAX_LIMIT; |
| 79 |
$this->shownavigation = false; |
| 80 |
$this->mimeAnalyzer = $mimeAnalyzer; |
| 81 |
$this->setDatabaseProvider( $dbProvider ); |
| 82 |
$this->setLinkBatchFactory( $linkBatchFactory ); |
| 83 |
} |
| 84 |
|
| 85 |
public function isExpensive() { |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function getQueryInfo() { |
| 104 |
$dbr = $this->getDatabaseProvider()->getReplicaDatabase(); |
| 105 |
$fakeTitle = $dbr->buildConcat( [ |
| 106 |
'img_media_type', |
| 107 |
$dbr->addQuotes( ';' ), |
| 108 |
'img_major_mime', |
| 109 |
$dbr->addQuotes( '/' ), |
| 110 |
'img_minor_mime', |
| 111 |
$dbr->addQuotes( ';' ), |
| 112 |
$dbr->buildStringCast( 'COUNT(*)' ), |
| 113 |
$dbr->addQuotes( ';' ), |
| 114 |
$dbr->buildStringCast( 'SUM( img_size )' ) |
| 115 |
] ); |
| 116 |
return [ |
| 117 |
'tables' => [ 'image' ], |
| 118 |
'fields' => [ |
| 119 |
'title' => $fakeTitle, |
| 120 |
'namespace' => NS_MEDIA, |
| 121 |
'value' => '1' |
| 122 |
], |
| 123 |
'options' => [ |
| 124 |
'GROUP BY' => [ |
| 125 |
'img_media_type', |
| 126 |
'img_major_mime', |
| 127 |
'img_minor_mime', |
| 128 |
] |
| 129 |
] |
| 130 |
]; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
protected function getOrderFields() { |
| 141 |
return [ 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' ]; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) { |
| 155 |
$prevMediaType = null; |
| 156 |
foreach ( $res as $row ) { |
| 157 |
$mediaStats = $this->splitFakeTitle( $row->title ); |
| 158 |
if ( count( $mediaStats ) < 4 ) { |
| 159 |
continue; |
| 160 |
} |
| 161 |
[ $mediaType, $mime, $totalCount, $totalBytes ] = $mediaStats; |
| 162 |
if ( $prevMediaType !== $mediaType ) { |
| 163 |
if ( $prevMediaType !== null ) { |
| 164 |
|
| 165 |
|
| 166 |
$this->outputTableEnd(); |
| 167 |
} |
| 168 |
$this->outputMediaType( $mediaType ); |
| 169 |
$this->totalPerType = 0; |
| 170 |
$this->countPerType = 0; |
| 171 |
$this->outputTableStart( $mediaType ); |
| 172 |
$prevMediaType = $mediaType; |
| 173 |
} |
| 174 |
$this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) ); |
| 175 |
} |
| 176 |
if ( $prevMediaType !== null ) { |
| 177 |
$this->outputTableEnd(); |
| 178 |
|
| 179 |
$this->outputMediaType( 'total' ); |
| 180 |
$this->getOutput()->addWikiTextAsInterface( |
| 181 |
$this->msg( 'mediastatistics-allbytes' ) |
| 182 |
->numParams( $this->totalSize ) |
| 183 |
->sizeParams( $this->totalSize ) |
| 184 |
->numParams( $this->totalCount ) |
| 185 |
->text() |
| 186 |
); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
protected function outputTableEnd() { |
| 194 |
$this->getOutput()->addHTML( |
| 195 |
Html::closeElement( 'tbody' ) . |
| 196 |
Html::closeElement( 'table' ) |
| 197 |
); |
| 198 |
$this->getOutput()->addWikiTextAsInterface( |
| 199 |
$this->msg( 'mediastatistics-bytespertype' ) |
| 200 |
->numParams( $this->totalPerType ) |
| 201 |
->sizeParams( $this->totalPerType ) |
| 202 |
->numParams( $this->makePercentPretty( $this->totalPerType / $this->totalBytes ) ) |
| 203 |
->numParams( $this->countPerType ) |
| 204 |
->numParams( $this->makePercentPretty( $this->countPerType / $this->totalCount ) ) |
| 205 |
->text() |
| 206 |
); |
| 207 |
$this->totalSize += $this->totalPerType; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
protected function outputTableRow( $mime, $count, $bytes ) { |
| 218 |
$mimeSearch = SpecialPage::getTitleFor( 'MIMEsearch', $mime ); |
| 219 |
$linkRenderer = $this->getLinkRenderer(); |
| 220 |
$row = Html::rawElement( |
| 221 |
'td', |
| 222 |
[], |
| 223 |
$linkRenderer->makeLink( $mimeSearch, $mime ) |
| 224 |
); |
| 225 |
$row .= Html::rawElement( |
| 226 |
'td', |
| 227 |
[], |
| 228 |
$this->getExtensionList( $mime ) |
| 229 |
); |
| 230 |
$row .= Html::rawElement( |
| 231 |
'td', |
| 232 |
|
| 233 |
[ 'data-sort-value' => $count ], |
| 234 |
$this->msg( 'mediastatistics-nfiles' ) |
| 235 |
->numParams( $count ) |
| 236 |
|
| 237 |
->numParams( $this->makePercentPretty( $count / $this->totalCount ) ) |
| 238 |
->parse() |
| 239 |
); |
| 240 |
$row .= Html::rawElement( |
| 241 |
'td', |
| 242 |
|
| 243 |
[ 'data-sort-value' => $bytes ], |
| 244 |
$this->msg( 'mediastatistics-nbytes' ) |
| 245 |
->numParams( $bytes ) |
| 246 |
->sizeParams( $bytes ) |
| 247 |
|
| 248 |
->numParams( $this->makePercentPretty( $bytes / $this->totalBytes ) ) |
| 249 |
->parse() |
| 250 |
); |
| 251 |
$this->totalPerType += $bytes; |
| 252 |
$this->countPerType += $count; |
| 253 |
$this->getOutput()->addHTML( Html::rawElement( 'tr', [], $row ) ); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
protected function makePercentPretty( $decimal ) { |
| 261 |
$decimal *= 100; |
| 262 |
|
| 263 |
if ( $decimal == 0 ) { |
| 264 |
return '0'; |
| 265 |
} |
| 266 |
if ( $decimal >= 100 ) { |
| 267 |
return '100'; |
| 268 |
} |
| 269 |
$percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal ); |
| 270 |
|
| 271 |
return preg_replace( '/\.?0*$/', '', $percent ); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
private function getExtensionList( $mime ) { |
| 281 |
$exts = $this->mimeAnalyzer->getExtensionsFromMimeType( $mime ); |
| 282 |
if ( !$exts ) { |
| 283 |
return ''; |
| 284 |
} |
| 285 |
foreach ( $exts as &$ext ) { |
| 286 |
$ext = htmlspecialchars( '.' . $ext ); |
| 287 |
} |
| 288 |
|
| 289 |
return $this->getLanguage()->commaList( $exts ); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
protected function outputTableStart( $mediaType ) { |
| 299 |
$out = $this->getOutput(); |
| 300 |
$out->addModuleStyles( 'jquery.tablesorter.styles' ); |
| 301 |
$out->addModules( 'jquery.tablesorter' ); |
| 302 |
$out->addHTML( |
| 303 |
Html::openElement( |
| 304 |
'table', |
| 305 |
[ 'class' => [ |
| 306 |
'mw-mediastats-table', |
| 307 |
'mw-mediastats-table-' . strtolower( $mediaType ), |
| 308 |
'sortable', |
| 309 |
'wikitable' |
| 310 |
] ] |
| 311 |
) . |
| 312 |
Html::rawElement( 'thead', [], $this->getTableHeaderRow() ) . |
| 313 |
Html::openElement( 'tbody' ) |
| 314 |
); |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
protected function getTableHeaderRow() { |
| 323 |
$headers = [ 'mimetype', 'extensions', 'count', 'totalbytes' ]; |
| 324 |
$ths = ''; |
| 325 |
foreach ( $headers as $header ) { |
| 326 |
$ths .= Html::rawElement( |
| 327 |
'th', |
| 328 |
[], |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
$this->msg( 'mediastatistics-table-' . $header )->parse() |
| 333 |
); |
| 334 |
} |
| 335 |
return Html::rawElement( 'tr', [], $ths ); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
protected function outputMediaType( $mediaType ) { |
| 344 |
$this->getOutput()->addHTML( |
| 345 |
Html::element( |
| 346 |
'h2', |
| 347 |
[ 'class' => [ |
| 348 |
'mw-mediastats-mediatype', |
| 349 |
'mw-mediastats-mediatype-' . strtolower( $mediaType ) |
| 350 |
] ], |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
$this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text() |
| 359 |
) |
| 360 |
); |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
private function splitFakeTitle( $fakeTitle ) { |
| 373 |
return explode( ';', $fakeTitle, 4 ); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
protected function getGroupName() { |
| 381 |
return 'media'; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
public function formatResult( $skin, $result ) { |
| 386 |
return false; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
public function preprocessResults( $dbr, $res ) { |
| 396 |
$this->executeLBFromResultWrapper( $res ); |
| 397 |
$this->totalCount = $this->totalBytes = 0; |
| 398 |
foreach ( $res as $row ) { |
| 399 |
$mediaStats = $this->splitFakeTitle( $row->title ); |
| 400 |
$this->totalCount += $mediaStats[2] ?? 0; |
| 401 |
$this->totalBytes += $mediaStats[3] ?? 0; |
| 402 |
} |
| 403 |
$res->seek( 0 ); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
class_alias( SpecialMediaStatistics::class, 'SpecialMediaStatistics' ); |