MediaWiki 1.42.1
SpecialMediaStatistics.php
Go to the documentation of this file.
1<?php
25namespace MediaWiki\Specials;
26
32use MimeAnalyzer;
33use Skin;
37
42
43 public const MAX_LIMIT = 5000;
44
45 protected $totalCount = 0, $totalBytes = 0;
46
50 protected $totalPerType = 0;
51
55 protected $countPerType = 0;
56
60 protected $totalSize = 0;
61
62 private MimeAnalyzer $mimeAnalyzer;
63
69 public function __construct(
70 MimeAnalyzer $mimeAnalyzer,
71 IConnectionProvider $dbProvider,
72 LinkBatchFactory $linkBatchFactory
73 ) {
74 parent::__construct( 'MediaStatistics' );
75 // Generally speaking there is only a small number of file types,
76 // so just show all of them.
77 $this->limit = self::MAX_LIMIT;
78 $this->shownavigation = false;
79 $this->mimeAnalyzer = $mimeAnalyzer;
80 $this->setDatabaseProvider( $dbProvider );
81 $this->setLinkBatchFactory( $linkBatchFactory );
82 }
83
84 public function isExpensive() {
85 return true;
86 }
87
102 public function getQueryInfo() {
103 $dbr = $this->getDatabaseProvider()->getReplicaDatabase();
104 $fakeTitle = $dbr->buildConcat( [
105 'img_media_type',
106 $dbr->addQuotes( ';' ),
107 'img_major_mime',
108 $dbr->addQuotes( '/' ),
109 'img_minor_mime',
110 $dbr->addQuotes( ';' ),
111 $dbr->buildStringCast( 'COUNT(*)' ),
112 $dbr->addQuotes( ';' ),
113 $dbr->buildStringCast( 'SUM( img_size )' )
114 ] );
115 return [
116 'tables' => [ 'image' ],
117 'fields' => [
118 'title' => $fakeTitle,
119 'namespace' => NS_MEDIA, /* needs to be something */
120 'value' => '1'
121 ],
122 'options' => [
123 'GROUP BY' => [
124 'img_media_type',
125 'img_major_mime',
126 'img_minor_mime',
127 ]
128 ]
129 ];
130 }
131
139 protected function getOrderFields() {
140 return [ 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' ];
141 }
142
153 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
154 $prevMediaType = null;
155 foreach ( $res as $row ) {
156 $mediaStats = $this->splitFakeTitle( $row->title );
157 if ( count( $mediaStats ) < 4 ) {
158 continue;
159 }
160 [ $mediaType, $mime, $totalCount, $totalBytes ] = $mediaStats;
161 if ( $prevMediaType !== $mediaType ) {
162 if ( $prevMediaType !== null ) {
163 // We're not at beginning, so we have to
164 // close the previous table.
165 $this->outputTableEnd();
166 }
167 $this->outputMediaType( $mediaType );
168 $this->totalPerType = 0;
169 $this->countPerType = 0;
170 $this->outputTableStart( $mediaType );
171 $prevMediaType = $mediaType;
172 }
173 $this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) );
174 }
175 if ( $prevMediaType !== null ) {
176 $this->outputTableEnd();
177 // add total size of all files
178 $this->outputMediaType( 'total' );
179 $this->getOutput()->addWikiTextAsInterface(
180 $this->msg( 'mediastatistics-allbytes' )
181 ->numParams( $this->totalSize )
182 ->sizeParams( $this->totalSize )
183 ->numParams( $this->totalCount )
184 ->text()
185 );
186 }
187 }
188
192 protected function outputTableEnd() {
193 $this->getOutput()->addHTML(
194 Html::closeElement( 'tbody' ) .
195 Html::closeElement( 'table' )
196 );
197 $this->getOutput()->addWikiTextAsInterface(
198 $this->msg( 'mediastatistics-bytespertype' )
199 ->numParams( $this->totalPerType )
200 ->sizeParams( $this->totalPerType )
201 ->numParams( $this->makePercentPretty( $this->totalPerType / $this->totalBytes ) )
202 ->numParams( $this->countPerType )
203 ->numParams( $this->makePercentPretty( $this->countPerType / $this->totalCount ) )
204 ->text()
205 );
206 $this->totalSize += $this->totalPerType;
207 }
208
216 protected function outputTableRow( $mime, $count, $bytes ) {
217 $mimeSearch = SpecialPage::getTitleFor( 'MIMEsearch', $mime );
218 $linkRenderer = $this->getLinkRenderer();
219 $row = Html::rawElement(
220 'td',
221 [],
222 $linkRenderer->makeLink( $mimeSearch, $mime )
223 );
224 $row .= Html::rawElement(
225 'td',
226 [],
227 $this->getExtensionList( $mime )
228 );
229 $row .= Html::rawElement(
230 'td',
231 // Make sure js sorts it in numeric order
232 [ 'data-sort-value' => $count ],
233 $this->msg( 'mediastatistics-nfiles' )
234 ->numParams( $count )
236 ->numParams( $this->makePercentPretty( $count / $this->totalCount ) )
237 ->parse()
238 );
239 $row .= Html::rawElement(
240 'td',
241 // Make sure js sorts it in numeric order
242 [ 'data-sort-value' => $bytes ],
243 $this->msg( 'mediastatistics-nbytes' )
244 ->numParams( $bytes )
245 ->sizeParams( $bytes )
247 ->numParams( $this->makePercentPretty( $bytes / $this->totalBytes ) )
248 ->parse()
249 );
250 $this->totalPerType += $bytes;
251 $this->countPerType += $count;
252 $this->getOutput()->addHTML( Html::rawElement( 'tr', [], $row ) );
253 }
254
259 protected function makePercentPretty( $decimal ) {
260 $decimal *= 100;
261 // Always show three useful digits
262 if ( $decimal == 0 ) {
263 return '0';
264 }
265 if ( $decimal >= 100 ) {
266 return '100';
267 }
268 $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal );
269 // Then remove any trailing 0's
270 return preg_replace( '/\.?0*$/', '', $percent );
271 }
272
279 private function getExtensionList( $mime ) {
280 $exts = $this->mimeAnalyzer->getExtensionsFromMimeType( $mime );
281 if ( !$exts ) {
282 return '';
283 }
284 foreach ( $exts as &$ext ) {
285 $ext = htmlspecialchars( '.' . $ext );
286 }
287
288 return $this->getLanguage()->commaList( $exts );
289 }
290
297 protected function outputTableStart( $mediaType ) {
298 $out = $this->getOutput();
299 $out->addModuleStyles( 'jquery.tablesorter.styles' );
300 $out->addModules( 'jquery.tablesorter' );
301 $out->addHTML(
302 Html::openElement(
303 'table',
304 [ 'class' => [
305 'mw-mediastats-table',
306 'mw-mediastats-table-' . strtolower( $mediaType ),
307 'sortable',
308 'wikitable'
309 ] ]
310 ) .
311 Html::rawElement( 'thead', [], $this->getTableHeaderRow() ) .
312 Html::openElement( 'tbody' )
313 );
314 }
315
321 protected function getTableHeaderRow() {
322 $headers = [ 'mimetype', 'extensions', 'count', 'totalbytes' ];
323 $ths = '';
324 foreach ( $headers as $header ) {
325 $ths .= Html::rawElement(
326 'th',
327 [],
328 // for grep:
329 // mediastatistics-table-mimetype, mediastatistics-table-extensions
330 // mediastatistics-table-count, mediastatistics-table-totalbytes
331 $this->msg( 'mediastatistics-table-' . $header )->parse()
332 );
333 }
334 return Html::rawElement( 'tr', [], $ths );
335 }
336
342 protected function outputMediaType( $mediaType ) {
343 $this->getOutput()->addHTML(
345 'h2',
346 [ 'class' => [
347 'mw-mediastats-mediatype',
348 'mw-mediastats-mediatype-' . strtolower( $mediaType )
349 ] ],
350 // for grep
351 // mediastatistics-header-unknown, mediastatistics-header-bitmap,
352 // mediastatistics-header-drawing, mediastatistics-header-audio,
353 // mediastatistics-header-video, mediastatistics-header-multimedia,
354 // mediastatistics-header-office, mediastatistics-header-text,
355 // mediastatistics-header-executable, mediastatistics-header-archive,
356 // mediastatistics-header-3d,
357 $this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text()
358 )
359 );
363 }
364
371 private function splitFakeTitle( $fakeTitle ) {
372 return explode( ';', $fakeTitle, 4 );
373 }
374
379 protected function getGroupName() {
380 return 'media';
381 }
382
384 public function formatResult( $skin, $result ) {
385 return false;
386 }
387
394 public function preprocessResults( $dbr, $res ) {
395 $this->executeLBFromResultWrapper( $res );
396 $this->totalCount = $this->totalBytes = 0;
397 foreach ( $res as $row ) {
398 $mediaStats = $this->splitFakeTitle( $row->title );
399 $this->totalCount += $mediaStats[2] ?? 0;
400 $this->totalBytes += $mediaStats[3] ?? 0;
401 }
402 $res->seek( 0 );
403 }
404}
405
410class_alias( SpecialMediaStatistics::class, 'SpecialMediaStatistics' );
const NS_MEDIA
Definition Defines.php:52
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
This is one of the Core classes and should be read at least once by any new developers.
This is a class for doing query pages; since they're almost all the same, we factor out some of the f...
Definition QueryPage.php:88
setDatabaseProvider(IConnectionProvider $databaseProvider)
int $offset
The offset and limit in use, as passed to the query() function.
Definition QueryPage.php:93
executeLBFromResultWrapper(IResultWrapper $res, $ns=null)
Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include title and...
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getLanguage()
Shortcut to get user's language.
formatResult( $skin, $result)
Formats the results of the query for display.The skin is the current skin; you can use it for making ...
int $totalPerType
Combined file size of all files in a section.
isExpensive()
Should this query page only be updated offline on large wikis?
int $countPerType
Combined file count of all files in a section.
outputTableStart( $mediaType)
Output the start of the table.
getTableHeaderRow()
Get (not output) the header row for the table.
outputMediaType( $mediaType)
Output a header for a new media type section.
outputTableRow( $mime, $count, $bytes)
Output a row of the stats table.
__construct(MimeAnalyzer $mimeAnalyzer, IConnectionProvider $dbProvider, LinkBatchFactory $linkBatchFactory)
preprocessResults( $dbr, $res)
Initialize total values so we can figure out percentages later.
outputResults( $out, $skin, $dbr, $res, $num, $offset)
Output the results of the query.
int $totalSize
Combined file size of all files.
The base class for all skins.
Definition Skin.php:58
Provide primary and replica IDatabase connections.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:36
Result wrapper for grabbing data queried from an IDatabase object.
element(SerializerNode $parent, SerializerNode $node, $contents)
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...
$header