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\ResultWrapper
;
26 use Wikimedia\Rdbms\IDatabase
;
29 * @ingroup SpecialPage
31 class MediaStatisticsPage
extends QueryPage
{
32 protected $totalCount = 0, $totalBytes = 0;
34 * @var integer $totalPerType Combined file size of all files in a section
36 protected $totalPerType = 0;
38 * @var integer $totalSize Combined file size of all files
40 protected $totalSize = 0;
42 function __construct( $name = 'MediaStatistics' ) {
43 parent
::__construct( $name );
44 // Generally speaking there is only a small number of file types,
45 // so just show all of them.
47 $this->shownavigation
= false;
50 public function isExpensive() {
57 * This abuses the query cache table by storing mime types as "titles".
59 * This will store entries like [[Media:BITMAP;image/jpeg;200;20000]]
60 * where the form is Media type;mime type;count;bytes.
62 * This relies on the behaviour that when value is tied, the order things
63 * come out of querycache table is the order they went in. Which is hacky.
64 * However, other special pages like Special:Deadendpages and
65 * Special:BrokenRedirects also rely on this.
68 public function getQueryInfo() {
69 $dbr = wfGetDB( DB_REPLICA
);
70 $fakeTitle = $dbr->buildConcat( [
72 $dbr->addQuotes( ';' ),
74 $dbr->addQuotes( '/' ),
76 $dbr->addQuotes( ';' ),
78 $dbr->addQuotes( ';' ),
82 'tables' => [ 'image' ],
84 'title' => $fakeTitle,
85 'namespace' => NS_MEDIA
, /* needs to be something */
99 * How to sort the results
101 * It's important that img_media_type come first, otherwise the
102 * tables will be fragmented.
103 * @return Array Fields to sort by
105 function getOrderFields() {
106 return [ 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' ];
110 * Output the results of the query.
112 * @param OutputPage $out
113 * @param Skin $skin (deprecated presumably)
114 * @param IDatabase $dbr
115 * @param ResultWrapper $res Results from query
116 * @param int $num Number of results
117 * @param int $offset Paging offset (Should always be 0 in our case)
119 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
120 $prevMediaType = null;
121 foreach ( $res as $row ) {
122 $mediaStats = $this->splitFakeTitle( $row->title
);
123 if ( count( $mediaStats ) < 4 ) {
126 list( $mediaType, $mime, $totalCount, $totalBytes ) = $mediaStats;
127 if ( $prevMediaType !== $mediaType ) {
128 if ( $prevMediaType !== null ) {
129 // We're not at beginning, so we have to
130 // close the previous table.
131 $this->outputTableEnd();
133 $this->outputMediaType( $mediaType );
134 $this->totalPerType
= 0;
135 $this->outputTableStart( $mediaType );
136 $prevMediaType = $mediaType;
138 $this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) );
140 if ( $prevMediaType !== null ) {
141 $this->outputTableEnd();
142 // add total size of all files
143 $this->outputMediaType( 'total' );
144 $this->getOutput()->addWikiText(
145 $this->msg( 'mediastatistics-allbytes' )
146 ->numParams( $this->totalSize
)
147 ->sizeParams( $this->totalSize
)
154 * Output closing </table>
156 protected function outputTableEnd() {
157 $this->getOutput()->addHTML( Html
::closeElement( 'table' ) );
158 $this->getOutput()->addWikiText(
159 $this->msg( 'mediastatistics-bytespertype' )
160 ->numParams( $this->totalPerType
)
161 ->sizeParams( $this->totalPerType
)
162 ->numParams( $this->makePercentPretty( $this->totalPerType
/ $this->totalBytes
) )
165 $this->totalSize +
= $this->totalPerType
;
169 * Output a row of the stats table
171 * @param string $mime mime type (e.g. image/jpeg)
172 * @param int $count Number of images of this type
173 * @param int $bytes Total space for images of this type
175 protected function outputTableRow( $mime, $count, $bytes ) {
176 $mimeSearch = SpecialPage
::getTitleFor( 'MIMEsearch', $mime );
177 $linkRenderer = $this->getLinkRenderer();
178 $row = Html
::rawElement(
181 $linkRenderer->makeLink( $mimeSearch, $mime )
183 $row .= Html
::element(
186 $this->getExtensionList( $mime )
188 $row .= Html
::rawElement(
190 // Make sure js sorts it in numeric order
191 [ 'data-sort-value' => $count ],
192 $this->msg( 'mediastatistics-nfiles' )
193 ->numParams( $count )
194 /** @todo Check to be sure this really should have number formatting */
195 ->numParams( $this->makePercentPretty( $count / $this->totalCount
) )
198 $row .= Html
::rawElement(
200 // Make sure js sorts it in numeric order
201 [ 'data-sort-value' => $bytes ],
202 $this->msg( 'mediastatistics-nbytes' )
203 ->numParams( $bytes )
204 ->sizeParams( $bytes )
205 /** @todo Check to be sure this really should have number formatting */
206 ->numParams( $this->makePercentPretty( $bytes / $this->totalBytes
) )
209 $this->totalPerType +
= $bytes;
210 $this->getOutput()->addHTML( Html
::rawElement( 'tr', [], $row ) );
214 * @param float $decimal A decimal percentage (ie for 12.3%, this would be 0.123)
215 * @return String The percentage formatted so that 3 significant digits are shown.
217 protected function makePercentPretty( $decimal ) {
219 // Always show three useful digits
220 if ( $decimal == 0 ) {
223 if ( $decimal >= 100 ) {
226 $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal );
227 // Then remove any trailing 0's
228 return preg_replace( '/\.?0*$/', '', $percent );
232 * Given a mime type, return a comma separated list of allowed extensions.
234 * @param string $mime mime type
235 * @return string Comma separated list of allowed extensions (e.g. ".ogg, .oga")
237 private function getExtensionList( $mime ) {
238 $exts = MimeMagic
::singleton()->getExtensionsForType( $mime );
239 if ( $exts === null ) {
242 $extArray = explode( ' ', $exts );
243 $extArray = array_unique( $extArray );
244 foreach ( $extArray as &$ext ) {
248 return $this->getLanguage()->commaList( $extArray );
252 * Output the start of the table
254 * Including opening <table>, and first <tr> with column headers.
255 * @param string $mediaType
257 protected function outputTableStart( $mediaType ) {
258 $this->getOutput()->addHTML(
262 'mw-mediastats-table',
263 'mw-mediastats-table-' . strtolower( $mediaType ),
269 $this->getOutput()->addHTML( $this->getTableHeaderRow() );
273 * Get (not output) the header row for the table
275 * @return String the header row of the able
277 protected function getTableHeaderRow() {
278 $headers = [ 'mimetype', 'extensions', 'count', 'totalbytes' ];
280 foreach ( $headers as $header ) {
281 $ths .= Html
::rawElement(
285 // mediastatistics-table-mimetype, mediastatistics-table-extensions
286 // tatistics-table-count, mediastatistics-table-totalbytes
287 $this->msg( 'mediastatistics-table-' . $header )->parse()
290 return Html
::rawElement( 'tr', [], $ths );
294 * Output a header for a new media type section
296 * @param string $mediaType A media type (e.g. from the MEDIATYPE_xxx constants)
298 protected function outputMediaType( $mediaType ) {
299 $this->getOutput()->addHTML(
303 'mw-mediastats-mediatype',
304 'mw-mediastats-mediatype-' . strtolower( $mediaType )
307 // mediastatistics-header-unknown, mediastatistics-header-bitmap,
308 // mediastatistics-header-drawing, mediastatistics-header-audio,
309 // mediastatistics-header-video, mediastatistics-header-multimedia,
310 // mediastatistics-header-office, mediastatistics-header-text,
311 // mediastatistics-header-executable, mediastatistics-header-archive,
312 // mediastatistics-header-3d,
313 $this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text()
316 /** @todo Possibly could add a message here explaining what the different types are.
317 * not sure if it is needed though.
322 * parse the fake title format that this special page abuses querycache with.
324 * @param string $fakeTitle A string formatted as <media type>;<mime type>;<count>;<bytes>
325 * @return array The constituant parts of $fakeTitle
327 private function splitFakeTitle( $fakeTitle ) {
328 return explode( ';', $fakeTitle, 4 );
332 * What group to put the page in
335 protected function getGroupName() {
340 * This method isn't used, since we override outputResults, but
341 * we need to implement since abstract in parent class.
344 * @param stdClass $result Result row
345 * @return bool|string|void
346 * @throws MWException
348 public function formatResult( $skin, $result ) {
349 throw new MWException( "unimplemented" );
353 * Initialize total values so we can figure out percentages later.
355 * @param IDatabase $dbr
356 * @param ResultWrapper $res
358 public function preprocessResults( $dbr, $res ) {
359 $this->executeLBFromResultWrapper( $res );
360 $this->totalCount
= $this->totalBytes
= 0;
361 foreach ( $res as $row ) {
362 $mediaStats = $this->splitFakeTitle( $row->title
);
363 $this->totalCount +
= isset( $mediaStats[2] ) ?
$mediaStats[2] : 0;
364 $this->totalBytes +
= isset( $mediaStats[3] ) ?
$mediaStats[3] : 0;