MediaWiki master
SpecialMediaStatistics.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
28use MimeAnalyzer;
29use Skin;
34
42
43 public const MAX_LIMIT = 5000;
44
45 protected int $totalCount = 0;
46 protected int $totalBytes = 0;
47
51 protected $totalPerType = 0;
52
56 protected $countPerType = 0;
57
61 protected $totalSize = 0;
62
63 private MimeAnalyzer $mimeAnalyzer;
64
70 public function __construct(
71 MimeAnalyzer $mimeAnalyzer,
72 IConnectionProvider $dbProvider,
73 LinkBatchFactory $linkBatchFactory
74 ) {
75 parent::__construct( 'MediaStatistics' );
76 // Generally speaking there is only a small number of file types,
77 // so just show all of them.
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
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, /* needs to be something */
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
140 protected function getOrderFields() {
141 return [ 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' ];
142 }
143
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 // We're not at beginning, so we have to
165 // close the previous table.
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 // add total size of all files
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
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
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 // Make sure js sorts it in numeric order
233 [ 'data-sort-value' => $count ],
234 $this->msg( 'mediastatistics-nfiles' )
235 ->numParams( $count )
237 ->numParams( $this->makePercentPretty( $count / $this->totalCount ) )
238 ->parse()
239 );
240 $row .= Html::rawElement(
241 'td',
242 // Make sure js sorts it in numeric order
243 [ 'data-sort-value' => $bytes ],
244 $this->msg( 'mediastatistics-nbytes' )
245 ->numParams( $bytes )
246 ->sizeParams( $bytes )
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
260 protected function makePercentPretty( $decimal ) {
261 $decimal *= 100;
262 // Always show three useful digits
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 // Then remove any trailing 0's
271 return preg_replace( '/\.?0*$/', '', $percent );
272 }
273
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
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
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 // for grep:
330 // mediastatistics-table-mimetype, mediastatistics-table-extensions
331 // mediastatistics-table-count, mediastatistics-table-totalbytes
332 $this->msg( 'mediastatistics-table-' . $header )->parse()
333 );
334 }
335 return Html::rawElement( 'tr', [], $ths );
336 }
337
343 protected function outputMediaType( $mediaType ) {
344 $this->getOutput()->addHTML(
346 'h2',
347 [ 'class' => [
348 'mw-mediastats-mediatype',
349 'mw-mediastats-mediatype-' . strtolower( $mediaType )
350 ] ],
351 // for grep
352 // mediastatistics-header-unknown, mediastatistics-header-bitmap,
353 // mediastatistics-header-drawing, mediastatistics-header-audio,
354 // mediastatistics-header-video, mediastatistics-header-multimedia,
355 // mediastatistics-header-office, mediastatistics-header-text,
356 // mediastatistics-header-executable, mediastatistics-header-archive,
357 // mediastatistics-header-3d,
358 $this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text()
359 )
360 );
364 }
365
372 private function splitFakeTitle( $fakeTitle ) {
373 return explode( ';', $fakeTitle, 4 );
374 }
375
380 protected function getGroupName() {
381 return 'media';
382 }
383
385 public function formatResult( $skin, $result ) {
386 return false;
387 }
388
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
411class_alias( SpecialMediaStatistics::class, 'SpecialMediaStatistics' );
const NS_MEDIA
Definition Defines.php:53
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:89
setDatabaseProvider(IConnectionProvider $databaseProvider)
int $offset
The offset and limit in use, as passed to the query() function.
Definition QueryPage.php:94
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:59
Provide primary and replica IDatabase connections.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:39
A database connection without write operations.
Result wrapper for grabbing data queried from an IDatabase object.
element(SerializerNode $parent, SerializerNode $node, $contents)
$header