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.
67 public function getQueryInfo() {
68 $dbr = wfGetDB( DB_REPLICA
);
69 $fakeTitle = $dbr->buildConcat( [
71 $dbr->addQuotes( ';' ),
73 $dbr->addQuotes( '/' ),
75 $dbr->addQuotes( ';' ),
77 $dbr->addQuotes( ';' ),
81 'tables' => [ 'image' ],
83 'title' => $fakeTitle,
84 'namespace' => NS_MEDIA
, /* needs to be something */
88 // WMF has a random null row in the db
89 'img_media_type IS NOT NULL'
102 * How to sort the results
104 * It's important that img_media_type come first, otherwise the
105 * tables will be fragmented.
106 * @return Array Fields to sort by
108 function getOrderFields() {
109 return [ 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' ];
113 * Output the results of the query.
115 * @param OutputPage $out
116 * @param Skin $skin (deprecated presumably)
117 * @param IDatabase $dbr
118 * @param ResultWrapper $res Results from query
119 * @param int $num Number of results
120 * @param int $offset Paging offset (Should always be 0 in our case)
122 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
123 $prevMediaType = null;
124 foreach ( $res as $row ) {
125 $mediaStats = $this->splitFakeTitle( $row->title
);
126 if ( count( $mediaStats ) < 4 ) {
129 list( $mediaType, $mime, $totalCount, $totalBytes ) = $mediaStats;
130 if ( $prevMediaType !== $mediaType ) {
131 if ( $prevMediaType !== null ) {
132 // We're not at beginning, so we have to
133 // close the previous table.
134 $this->outputTableEnd();
136 $this->outputMediaType( $mediaType );
137 $this->totalPerType
= 0;
138 $this->outputTableStart( $mediaType );
139 $prevMediaType = $mediaType;
141 $this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) );
143 if ( $prevMediaType !== null ) {
144 $this->outputTableEnd();
145 // add total size of all files
146 $this->outputMediaType( 'total' );
147 $this->getOutput()->addWikiText(
148 $this->msg( 'mediastatistics-allbytes' )
149 ->numParams( $this->totalSize
)
150 ->sizeParams( $this->totalSize
)
157 * Output closing </table>
159 protected function outputTableEnd() {
160 $this->getOutput()->addHTML( Html
::closeElement( 'table' ) );
161 $this->getOutput()->addWikiText(
162 $this->msg( 'mediastatistics-bytespertype' )
163 ->numParams( $this->totalPerType
)
164 ->sizeParams( $this->totalPerType
)
165 ->numParams( $this->makePercentPretty( $this->totalPerType
/ $this->totalBytes
) )
168 $this->totalSize +
= $this->totalPerType
;
172 * Output a row of the stats table
174 * @param string $mime mime type (e.g. image/jpeg)
175 * @param int $count Number of images of this type
176 * @param int $totalBytes Total space for images of this type
178 protected function outputTableRow( $mime, $count, $bytes ) {
179 $mimeSearch = SpecialPage
::getTitleFor( 'MIMEsearch', $mime );
180 $linkRenderer = $this->getLinkRenderer();
181 $row = Html
::rawElement(
184 $linkRenderer->makeLink( $mimeSearch, $mime )
186 $row .= Html
::element(
189 $this->getExtensionList( $mime )
191 $row .= Html
::rawElement(
193 // Make sure js sorts it in numeric order
194 [ 'data-sort-value' => $count ],
195 $this->msg( 'mediastatistics-nfiles' )
196 ->numParams( $count )
197 /** @todo Check to be sure this really should have number formatting */
198 ->numParams( $this->makePercentPretty( $count / $this->totalCount
) )
201 $row .= Html
::rawElement(
203 // Make sure js sorts it in numeric order
204 [ 'data-sort-value' => $bytes ],
205 $this->msg( 'mediastatistics-nbytes' )
206 ->numParams( $bytes )
207 ->sizeParams( $bytes )
208 /** @todo Check to be sure this really should have number formatting */
209 ->numParams( $this->makePercentPretty( $bytes / $this->totalBytes
) )
212 $this->totalPerType +
= $bytes;
213 $this->getOutput()->addHTML( Html
::rawElement( 'tr', [], $row ) );
217 * @param float $decimal A decimal percentage (ie for 12.3%, this would be 0.123)
218 * @return String The percentage formatted so that 3 significant digits are shown.
220 protected function makePercentPretty( $decimal ) {
222 // Always show three useful digits
223 if ( $decimal == 0 ) {
226 if ( $decimal >= 100 ) {
229 $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal );
230 // Then remove any trailing 0's
231 return preg_replace( '/\.?0*$/', '', $percent );
235 * Given a mime type, return a comma separated list of allowed extensions.
237 * @param string $mime mime type
238 * @return string Comma separated list of allowed extensions (e.g. ".ogg, .oga")
240 private function getExtensionList( $mime ) {
241 $exts = MimeMagic
::singleton()->getExtensionsForType( $mime );
242 if ( $exts === null ) {
245 $extArray = explode( ' ', $exts );
246 $extArray = array_unique( $extArray );
247 foreach ( $extArray as &$ext ) {
251 return $this->getLanguage()->commaList( $extArray );
255 * Output the start of the table
257 * Including opening <table>, and first <tr> with column headers.
259 protected function outputTableStart( $mediaType ) {
260 $this->getOutput()->addHTML(
264 'mw-mediastats-table',
265 'mw-mediastats-table-' . strtolower( $mediaType ),
271 $this->getOutput()->addHTML( $this->getTableHeaderRow() );
275 * Get (not output) the header row for the table
277 * @return String the header row of the able
279 protected function getTableHeaderRow() {
280 $headers = [ 'mimetype', 'extensions', 'count', 'totalbytes' ];
282 foreach ( $headers as $header ) {
283 $ths .= Html
::rawElement(
287 // mediastatistics-table-mimetype, mediastatistics-table-extensions
288 // tatistics-table-count, mediastatistics-table-totalbytes
289 $this->msg( 'mediastatistics-table-' . $header )->parse()
292 return Html
::rawElement( 'tr', [], $ths );
296 * Output a header for a new media type section
298 * @param string $mediaType A media type (e.g. from the MEDIATYPE_xxx constants)
300 protected function outputMediaType( $mediaType ) {
301 $this->getOutput()->addHTML(
305 'mw-mediastats-mediatype',
306 'mw-mediastats-mediatype-' . strtolower( $mediaType )
309 // mediastatistics-header-unknown, mediastatistics-header-bitmap,
310 // mediastatistics-header-drawing, mediastatistics-header-audio,
311 // mediastatistics-header-video, mediastatistics-header-multimedia,
312 // mediastatistics-header-office, mediastatistics-header-text,
313 // mediastatistics-header-executable, mediastatistics-header-archive,
314 $this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text()
317 /** @todo Possibly could add a message here explaining what the different types are.
318 * not sure if it is needed though.
323 * parse the fake title format that this special page abuses querycache with.
325 * @param string $fakeTitle A string formatted as <media type>;<mime type>;<count>;<bytes>
326 * @return array The constituant parts of $fakeTitle
328 private function splitFakeTitle( $fakeTitle ) {
329 return explode( ';', $fakeTitle, 4 );
333 * What group to put the page in
336 protected function getGroupName() {
341 * This method isn't used, since we override outputResults, but
342 * we need to implement since abstract in parent class.
345 * @param stdClass $result Result row
346 * @return bool|string|void
347 * @throws MWException
349 public function formatResult( $skin, $result ) {
350 throw new MWException( "unimplemented" );
354 * Initialize total values so we can figure out percentages later.
356 * @param IDatabase $dbr
357 * @param ResultWrapper $res
359 public function preprocessResults( $dbr, $res ) {
360 $this->executeLBFromResultWrapper( $res );
361 $this->totalCount
= $this->totalBytes
= 0;
362 foreach ( $res as $row ) {
363 $mediaStats = $this->splitFakeTitle( $row->title
);
364 $this->totalCount +
= isset( $mediaStats[2] ) ?
$mediaStats[2] : 0;
365 $this->totalBytes +
= isset( $mediaStats[3] ) ?
$mediaStats[3] : 0;