3 * Implements Special:MediaStatistics
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
25 use Wikimedia\Rdbms\IResultWrapper
;
26 use Wikimedia\Rdbms\IDatabase
;
29 * @ingroup SpecialPage
31 class MediaStatisticsPage
extends QueryPage
{
32 protected $totalCount = 0, $totalBytes = 0;
35 * @var int $totalPerType Combined file size of all files in a section
37 protected $totalPerType = 0;
40 * @var int $totalSize Combined file size of all files
42 protected $totalSize = 0;
44 function __construct( $name = 'MediaStatistics' ) {
45 parent
::__construct( $name );
46 // Generally speaking there is only a small number of file types,
47 // so just show all of them.
49 $this->shownavigation
= false;
52 public function isExpensive() {
59 * This abuses the query cache table by storing mime types as "titles".
61 * This will store entries like [[Media:BITMAP;image/jpeg;200;20000]]
62 * where the form is Media type;mime type;count;bytes.
64 * This relies on the behaviour that when value is tied, the order things
65 * come out of querycache table is the order they went in. Which is hacky.
66 * However, other special pages like Special:Deadendpages and
67 * Special:BrokenRedirects also rely on this.
70 public function getQueryInfo() {
71 $dbr = wfGetDB( DB_REPLICA
);
72 $fakeTitle = $dbr->buildConcat( [
74 $dbr->addQuotes( ';' ),
76 $dbr->addQuotes( '/' ),
78 $dbr->addQuotes( ';' ),
79 $dbr->buildStringCast( 'COUNT(*)' ),
80 $dbr->addQuotes( ';' ),
81 $dbr->buildStringCast( 'SUM( img_size )' )
84 'tables' => [ 'image' ],
86 'title' => $fakeTitle,
87 'namespace' => NS_MEDIA
, /* needs to be something */
101 * How to sort the results
103 * It's important that img_media_type come first, otherwise the
104 * tables will be fragmented.
105 * @return array Fields to sort by
107 function getOrderFields() {
108 return [ 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' ];
112 * Output the results of the query.
114 * @param OutputPage $out
115 * @param Skin $skin (deprecated presumably)
116 * @param IDatabase $dbr
117 * @param IResultWrapper $res Results from query
118 * @param int $num Number of results
119 * @param int $offset Paging offset (Should always be 0 in our case)
121 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
122 $prevMediaType = null;
123 foreach ( $res as $row ) {
124 $mediaStats = $this->splitFakeTitle( $row->title
);
125 if ( count( $mediaStats ) < 4 ) {
128 list( $mediaType, $mime, $totalCount, $totalBytes ) = $mediaStats;
129 if ( $prevMediaType !== $mediaType ) {
130 if ( $prevMediaType !== null ) {
131 // We're not at beginning, so we have to
132 // close the previous table.
133 $this->outputTableEnd();
135 $this->outputMediaType( $mediaType );
136 $this->totalPerType
= 0;
137 $this->outputTableStart( $mediaType );
138 $prevMediaType = $mediaType;
140 $this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) );
142 if ( $prevMediaType !== null ) {
143 $this->outputTableEnd();
144 // add total size of all files
145 $this->outputMediaType( 'total' );
146 $this->getOutput()->addWikiTextAsInterface(
147 $this->msg( 'mediastatistics-allbytes' )
148 ->numParams( $this->totalSize
)
149 ->sizeParams( $this->totalSize
)
156 * Output closing </table>
158 protected function outputTableEnd() {
159 $this->getOutput()->addHTML(
160 Html
::closeElement( 'tbody' ) .
161 Html
::closeElement( 'table' )
163 $this->getOutput()->addWikiTextAsInterface(
164 $this->msg( 'mediastatistics-bytespertype' )
165 ->numParams( $this->totalPerType
)
166 ->sizeParams( $this->totalPerType
)
167 ->numParams( $this->makePercentPretty( $this->totalPerType
/ $this->totalBytes
) )
170 $this->totalSize +
= $this->totalPerType
;
174 * Output a row of the stats table
176 * @param string $mime mime type (e.g. image/jpeg)
177 * @param int $count Number of images of this type
178 * @param int $bytes Total space for images of this type
180 protected function outputTableRow( $mime, $count, $bytes ) {
181 $mimeSearch = SpecialPage
::getTitleFor( 'MIMEsearch', $mime );
182 $linkRenderer = $this->getLinkRenderer();
183 $row = Html
::rawElement(
186 $linkRenderer->makeLink( $mimeSearch, $mime )
188 $row .= Html
::rawElement(
191 $this->getExtensionList( $mime )
193 $row .= Html
::rawElement(
195 // Make sure js sorts it in numeric order
196 [ 'data-sort-value' => $count ],
197 $this->msg( 'mediastatistics-nfiles' )
198 ->numParams( $count )
199 /** @todo Check to be sure this really should have number formatting */
200 ->numParams( $this->makePercentPretty( $count / $this->totalCount
) )
203 $row .= Html
::rawElement(
205 // Make sure js sorts it in numeric order
206 [ 'data-sort-value' => $bytes ],
207 $this->msg( 'mediastatistics-nbytes' )
208 ->numParams( $bytes )
209 ->sizeParams( $bytes )
210 /** @todo Check to be sure this really should have number formatting */
211 ->numParams( $this->makePercentPretty( $bytes / $this->totalBytes
) )
214 $this->totalPerType +
= $bytes;
215 $this->getOutput()->addHTML( Html
::rawElement( 'tr', [], $row ) );
219 * @param float $decimal A decimal percentage (ie for 12.3%, this would be 0.123)
220 * @return string The percentage formatted so that 3 significant digits are shown.
222 protected function makePercentPretty( $decimal ) {
224 // Always show three useful digits
225 if ( $decimal == 0 ) {
228 if ( $decimal >= 100 ) {
231 $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal );
232 // Then remove any trailing 0's
233 return preg_replace( '/\.?0*$/', '', $percent );
237 * Given a mime type, return a comma separated list of allowed extensions.
239 * @param string $mime mime type
240 * @return string Comma separated list of allowed extensions (e.g. ".ogg, .oga")
242 private function getExtensionList( $mime ) {
243 $exts = MediaWiki\MediaWikiServices
::getInstance()->getMimeAnalyzer()
244 ->getExtensionsForType( $mime );
245 if ( $exts === null ) {
248 $extArray = explode( ' ', $exts );
249 $extArray = array_unique( $extArray );
250 foreach ( $extArray as &$ext ) {
251 $ext = htmlspecialchars( '.' . $ext );
254 return $this->getLanguage()->commaList( $extArray );
258 * Output the start of the table
260 * Including opening <table>, and first <tr> with column headers.
261 * @param string $mediaType
263 protected function outputTableStart( $mediaType ) {
264 $out = $this->getOutput();
265 $out->addModuleStyles( 'jquery.tablesorter.styles' );
266 $out->addModules( 'jquery.tablesorter' );
271 'mw-mediastats-table',
272 'mw-mediastats-table-' . strtolower( $mediaType ),
277 Html
::rawElement( 'thead', [], $this->getTableHeaderRow() ) .
278 Html
::openElement( 'tbody' )
283 * Get (not output) the header row for the table
285 * @return string The header row of the table
287 protected function getTableHeaderRow() {
288 $headers = [ 'mimetype', 'extensions', 'count', 'totalbytes' ];
290 foreach ( $headers as $header ) {
291 $ths .= Html
::rawElement(
295 // mediastatistics-table-mimetype, mediastatistics-table-extensions
296 // tatistics-table-count, mediastatistics-table-totalbytes
297 $this->msg( 'mediastatistics-table-' . $header )->parse()
300 return Html
::rawElement( 'tr', [], $ths );
304 * Output a header for a new media type section
306 * @param string $mediaType A media type (e.g. from the MEDIATYPE_xxx constants)
308 protected function outputMediaType( $mediaType ) {
309 $this->getOutput()->addHTML(
313 'mw-mediastats-mediatype',
314 'mw-mediastats-mediatype-' . strtolower( $mediaType )
317 // mediastatistics-header-unknown, mediastatistics-header-bitmap,
318 // mediastatistics-header-drawing, mediastatistics-header-audio,
319 // mediastatistics-header-video, mediastatistics-header-multimedia,
320 // mediastatistics-header-office, mediastatistics-header-text,
321 // mediastatistics-header-executable, mediastatistics-header-archive,
322 // mediastatistics-header-3d,
323 $this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text()
326 /** @todo Possibly could add a message here explaining what the different types are.
327 * not sure if it is needed though.
332 * parse the fake title format that this special page abuses querycache with.
334 * @param string $fakeTitle A string formatted as <media type>;<mime type>;<count>;<bytes>
335 * @return array The constituant parts of $fakeTitle
337 private function splitFakeTitle( $fakeTitle ) {
338 return explode( ';', $fakeTitle, 4 );
342 * What group to put the page in
345 protected function getGroupName() {
350 * This method isn't used, since we override outputResults, but
351 * we need to implement since abstract in parent class.
354 * @param stdClass $result Result row
355 * @return bool|string|void
356 * @throws MWException
358 public function formatResult( $skin, $result ) {
359 throw new MWException( "unimplemented" );
363 * Initialize total values so we can figure out percentages later.
365 * @param IDatabase $dbr
366 * @param IResultWrapper $res
368 public function preprocessResults( $dbr, $res ) {
369 $this->executeLBFromResultWrapper( $res );
370 $this->totalCount
= $this->totalBytes
= 0;
371 foreach ( $res as $row ) {
372 $mediaStats = $this->splitFakeTitle( $row->title
);
373 $this->totalCount +
= $mediaStats[2] ??
0;
374 $this->totalBytes +
= $mediaStats[3] ??
0;