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 */
98 * How to sort the results
100 * It's important that img_media_type come first, otherwise the
101 * tables will be fragmented.
102 * @return Array Fields to sort by
104 function getOrderFields() {
105 return [ 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' ];
109 * Output the results of the query.
111 * @param OutputPage $out
112 * @param Skin $skin (deprecated presumably)
113 * @param IDatabase $dbr
114 * @param ResultWrapper $res Results from query
115 * @param int $num Number of results
116 * @param int $offset Paging offset (Should always be 0 in our case)
118 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
119 $prevMediaType = null;
120 foreach ( $res as $row ) {
121 $mediaStats = $this->splitFakeTitle( $row->title
);
122 if ( count( $mediaStats ) < 4 ) {
125 list( $mediaType, $mime, $totalCount, $totalBytes ) = $mediaStats;
126 if ( $prevMediaType !== $mediaType ) {
127 if ( $prevMediaType !== null ) {
128 // We're not at beginning, so we have to
129 // close the previous table.
130 $this->outputTableEnd();
132 $this->outputMediaType( $mediaType );
133 $this->totalPerType
= 0;
134 $this->outputTableStart( $mediaType );
135 $prevMediaType = $mediaType;
137 $this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) );
139 if ( $prevMediaType !== null ) {
140 $this->outputTableEnd();
141 // add total size of all files
142 $this->outputMediaType( 'total' );
143 $this->getOutput()->addWikiText(
144 $this->msg( 'mediastatistics-allbytes' )
145 ->numParams( $this->totalSize
)
146 ->sizeParams( $this->totalSize
)
153 * Output closing </table>
155 protected function outputTableEnd() {
156 $this->getOutput()->addHTML( Html
::closeElement( 'table' ) );
157 $this->getOutput()->addWikiText(
158 $this->msg( 'mediastatistics-bytespertype' )
159 ->numParams( $this->totalPerType
)
160 ->sizeParams( $this->totalPerType
)
161 ->numParams( $this->makePercentPretty( $this->totalPerType
/ $this->totalBytes
) )
164 $this->totalSize +
= $this->totalPerType
;
168 * Output a row of the stats table
170 * @param string $mime mime type (e.g. image/jpeg)
171 * @param int $count Number of images of this type
172 * @param int $bytes Total space for images of this type
174 protected function outputTableRow( $mime, $count, $bytes ) {
175 $mimeSearch = SpecialPage
::getTitleFor( 'MIMEsearch', $mime );
176 $linkRenderer = $this->getLinkRenderer();
177 $row = Html
::rawElement(
180 $linkRenderer->makeLink( $mimeSearch, $mime )
182 $row .= Html
::element(
185 $this->getExtensionList( $mime )
187 $row .= Html
::rawElement(
189 // Make sure js sorts it in numeric order
190 [ 'data-sort-value' => $count ],
191 $this->msg( 'mediastatistics-nfiles' )
192 ->numParams( $count )
193 /** @todo Check to be sure this really should have number formatting */
194 ->numParams( $this->makePercentPretty( $count / $this->totalCount
) )
197 $row .= Html
::rawElement(
199 // Make sure js sorts it in numeric order
200 [ 'data-sort-value' => $bytes ],
201 $this->msg( 'mediastatistics-nbytes' )
202 ->numParams( $bytes )
203 ->sizeParams( $bytes )
204 /** @todo Check to be sure this really should have number formatting */
205 ->numParams( $this->makePercentPretty( $bytes / $this->totalBytes
) )
208 $this->totalPerType +
= $bytes;
209 $this->getOutput()->addHTML( Html
::rawElement( 'tr', [], $row ) );
213 * @param float $decimal A decimal percentage (ie for 12.3%, this would be 0.123)
214 * @return String The percentage formatted so that 3 significant digits are shown.
216 protected function makePercentPretty( $decimal ) {
218 // Always show three useful digits
219 if ( $decimal == 0 ) {
222 if ( $decimal >= 100 ) {
225 $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal );
226 // Then remove any trailing 0's
227 return preg_replace( '/\.?0*$/', '', $percent );
231 * Given a mime type, return a comma separated list of allowed extensions.
233 * @param string $mime mime type
234 * @return string Comma separated list of allowed extensions (e.g. ".ogg, .oga")
236 private function getExtensionList( $mime ) {
237 $exts = MimeMagic
::singleton()->getExtensionsForType( $mime );
238 if ( $exts === null ) {
241 $extArray = explode( ' ', $exts );
242 $extArray = array_unique( $extArray );
243 foreach ( $extArray as &$ext ) {
247 return $this->getLanguage()->commaList( $extArray );
251 * Output the start of the table
253 * Including opening <table>, and first <tr> with column headers.
255 protected function outputTableStart( $mediaType ) {
256 $this->getOutput()->addHTML(
260 'mw-mediastats-table',
261 'mw-mediastats-table-' . strtolower( $mediaType ),
267 $this->getOutput()->addHTML( $this->getTableHeaderRow() );
271 * Get (not output) the header row for the table
273 * @return String the header row of the able
275 protected function getTableHeaderRow() {
276 $headers = [ 'mimetype', 'extensions', 'count', 'totalbytes' ];
278 foreach ( $headers as $header ) {
279 $ths .= Html
::rawElement(
283 // mediastatistics-table-mimetype, mediastatistics-table-extensions
284 // tatistics-table-count, mediastatistics-table-totalbytes
285 $this->msg( 'mediastatistics-table-' . $header )->parse()
288 return Html
::rawElement( 'tr', [], $ths );
292 * Output a header for a new media type section
294 * @param string $mediaType A media type (e.g. from the MEDIATYPE_xxx constants)
296 protected function outputMediaType( $mediaType ) {
297 $this->getOutput()->addHTML(
301 'mw-mediastats-mediatype',
302 'mw-mediastats-mediatype-' . strtolower( $mediaType )
305 // mediastatistics-header-unknown, mediastatistics-header-bitmap,
306 // mediastatistics-header-drawing, mediastatistics-header-audio,
307 // mediastatistics-header-video, mediastatistics-header-multimedia,
308 // mediastatistics-header-office, mediastatistics-header-text,
309 // mediastatistics-header-executable, mediastatistics-header-archive,
310 // mediastatistics-header-3d,
311 $this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text()
314 /** @todo Possibly could add a message here explaining what the different types are.
315 * not sure if it is needed though.
320 * parse the fake title format that this special page abuses querycache with.
322 * @param string $fakeTitle A string formatted as <media type>;<mime type>;<count>;<bytes>
323 * @return array The constituant parts of $fakeTitle
325 private function splitFakeTitle( $fakeTitle ) {
326 return explode( ';', $fakeTitle, 4 );
330 * What group to put the page in
333 protected function getGroupName() {
338 * This method isn't used, since we override outputResults, but
339 * we need to implement since abstract in parent class.
342 * @param stdClass $result Result row
343 * @return bool|string|void
344 * @throws MWException
346 public function formatResult( $skin, $result ) {
347 throw new MWException( "unimplemented" );
351 * Initialize total values so we can figure out percentages later.
353 * @param IDatabase $dbr
354 * @param ResultWrapper $res
356 public function preprocessResults( $dbr, $res ) {
357 $this->executeLBFromResultWrapper( $res );
358 $this->totalCount
= $this->totalBytes
= 0;
359 foreach ( $res as $row ) {
360 $mediaStats = $this->splitFakeTitle( $row->title
);
361 $this->totalCount +
= isset( $mediaStats[2] ) ?
$mediaStats[2] : 0;
362 $this->totalBytes +
= isset( $mediaStats[3] ) ?
$mediaStats[3] : 0;