3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Specials
;
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
;
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
;
36 * Implements Special:MediaStatistics
38 * @ingroup SpecialPage
41 class SpecialMediaStatistics
extends QueryPage
{
43 public const MAX_LIMIT
= 5000;
45 protected int $totalCount = 0;
46 protected int $totalBytes = 0;
49 * @var int Combined file size of all files in a section
51 protected $totalPerType = 0;
54 * @var int Combined file count of all files in a section
56 protected $countPerType = 0;
59 * @var int Combined file size of all files
61 protected $totalSize = 0;
63 private MimeAnalyzer
$mimeAnalyzer;
66 * @param MimeAnalyzer $mimeAnalyzer
67 * @param IConnectionProvider $dbProvider
68 * @param LinkBatchFactory $linkBatchFactory
70 public function __construct(
71 MimeAnalyzer
$mimeAnalyzer,
72 IConnectionProvider
$dbProvider,
73 LinkBatchFactory
$linkBatchFactory
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 );
85 public function isExpensive() {
92 * This abuses the query cache table by storing mime types as "titles".
94 * This will store entries like [[Media:BITMAP;image/jpeg;200;20000]]
95 * where the form is Media type;mime type;count;bytes.
97 * This relies on the behaviour that when value is tied, the order things
98 * come out of querycache table is the order they went in. Which is hacky.
99 * However, other special pages like Special:Deadendpages and
100 * Special:BrokenRedirects also rely on this.
103 public function getQueryInfo() {
104 $dbr = $this->getDatabaseProvider()->getReplicaDatabase();
105 $fakeTitle = $dbr->buildConcat( [
107 $dbr->addQuotes( ';' ),
109 $dbr->addQuotes( '/' ),
111 $dbr->addQuotes( ';' ),
112 $dbr->buildStringCast( 'COUNT(*)' ),
113 $dbr->addQuotes( ';' ),
114 $dbr->buildStringCast( 'SUM( img_size )' )
117 'tables' => [ 'image' ],
119 'title' => $fakeTitle,
120 'namespace' => NS_MEDIA
, /* needs to be something */
134 * How to sort the results
136 * It's important that img_media_type come first, otherwise the
137 * tables will be fragmented.
138 * @return array Fields to sort by
140 protected function getOrderFields() {
141 return [ 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' ];
145 * Output the results of the query.
147 * @param OutputPage $out
148 * @param Skin $skin (deprecated presumably)
149 * @param IReadableDatabase $dbr
150 * @param IResultWrapper $res Results from query
151 * @param int $num Number of results
152 * @param int $offset Paging offset (Should always be 0 in our case)
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 ) {
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();
168 $this->outputMediaType( $mediaType );
169 $this->totalPerType
= 0;
170 $this->countPerType
= 0;
171 $this->outputTableStart( $mediaType );
172 $prevMediaType = $mediaType;
174 $this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) );
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
)
191 * Output closing </table>
193 protected function outputTableEnd() {
194 $this->getOutput()->addHTML(
195 Html
::closeElement( 'tbody' ) .
196 Html
::closeElement( 'table' )
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
) )
207 $this->totalSize +
= $this->totalPerType
;
211 * Output a row of the stats table
213 * @param string $mime mime type (e.g. image/jpeg)
214 * @param int $count Number of images of this type
215 * @param int $bytes Total space for images of this type
217 protected function outputTableRow( $mime, $count, $bytes ) {
218 $mimeSearch = SpecialPage
::getTitleFor( 'MIMEsearch', $mime );
219 $linkRenderer = $this->getLinkRenderer();
220 $row = Html
::rawElement(
223 $linkRenderer->makeLink( $mimeSearch, $mime )
225 $row .= Html
::rawElement(
228 $this->getExtensionList( $mime )
230 $row .= Html
::rawElement(
232 // Make sure js sorts it in numeric order
233 [ 'data-sort-value' => $count ],
234 $this->msg( 'mediastatistics-nfiles' )
235 ->numParams( $count )
236 /** @todo Check to be sure this really should have number formatting */
237 ->numParams( $this->makePercentPretty( $count / $this->totalCount
) )
240 $row .= Html
::rawElement(
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 )
247 /** @todo Check to be sure this really should have number formatting */
248 ->numParams( $this->makePercentPretty( $bytes / $this->totalBytes
) )
251 $this->totalPerType +
= $bytes;
252 $this->countPerType +
= $count;
253 $this->getOutput()->addHTML( Html
::rawElement( 'tr', [], $row ) );
257 * @param float $decimal A decimal percentage (ie for 12.3%, this would be 0.123)
258 * @return string The percentage formatted so that 3 significant digits are shown.
260 protected function makePercentPretty( $decimal ) {
262 // Always show three useful digits
263 if ( $decimal == 0 ) {
266 if ( $decimal >= 100 ) {
269 $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal );
270 // Then remove any trailing 0's
271 return preg_replace( '/\.?0*$/', '', $percent );
275 * Given a mime type, return a comma separated list of allowed extensions.
277 * @param string $mime mime type
278 * @return string Comma separated list of allowed extensions (e.g. ".ogg, .oga")
280 private function getExtensionList( $mime ) {
281 $exts = $this->mimeAnalyzer
->getExtensionsFromMimeType( $mime );
285 foreach ( $exts as &$ext ) {
286 $ext = htmlspecialchars( '.' . $ext );
289 return $this->getLanguage()->commaList( $exts );
293 * Output the start of the table
295 * Including opening <table>, and first <tr> with column headers.
296 * @param string $mediaType
298 protected function outputTableStart( $mediaType ) {
299 $out = $this->getOutput();
300 $out->addModuleStyles( 'jquery.tablesorter.styles' );
301 $out->addModules( 'jquery.tablesorter' );
306 'mw-mediastats-table',
307 'mw-mediastats-table-' . strtolower( $mediaType ),
312 Html
::rawElement( 'thead', [], $this->getTableHeaderRow() ) .
313 Html
::openElement( 'tbody' )
318 * Get (not output) the header row for the table
320 * @return string The header row of the table
322 protected function getTableHeaderRow() {
323 $headers = [ 'mimetype', 'extensions', 'count', 'totalbytes' ];
325 foreach ( $headers as $header ) {
326 $ths .= Html
::rawElement(
330 // mediastatistics-table-mimetype, mediastatistics-table-extensions
331 // mediastatistics-table-count, mediastatistics-table-totalbytes
332 $this->msg( 'mediastatistics-table-' . $header )->parse()
335 return Html
::rawElement( 'tr', [], $ths );
339 * Output a header for a new media type section
341 * @param string $mediaType A media type (e.g. from the MEDIATYPE_xxx constants)
343 protected function outputMediaType( $mediaType ) {
344 $this->getOutput()->addHTML(
348 'mw-mediastats-mediatype',
349 'mw-mediastats-mediatype-' . strtolower( $mediaType )
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()
361 /** @todo Possibly could add a message here explaining what the different types are.
362 * not sure if it is needed though.
367 * parse the fake title format that this special page abuses querycache with.
369 * @param string $fakeTitle A string formatted as <media type>;<mime type>;<count>;<bytes>
370 * @return array The constituent parts of $fakeTitle
372 private function splitFakeTitle( $fakeTitle ) {
373 return explode( ';', $fakeTitle, 4 );
377 * What group to put the page in
380 protected function getGroupName() {
385 public function formatResult( $skin, $result ) {
390 * Initialize total values so we can figure out percentages later.
392 * @param IDatabase $dbr
393 * @param IResultWrapper $res
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;
408 * Retain the old class name for backwards compatibility.
409 * @deprecated since 1.41
411 class_alias( SpecialMediaStatistics
::class, 'SpecialMediaStatistics' );