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
26 * @ingroup SpecialPage
28 class MediaStatisticsPage
extends QueryPage
{
29 protected $totalCount = 0, $totalBytes = 0;
31 function __construct( $name = 'MediaStatistics' ) {
32 parent
::__construct( $name );
33 // Generally speaking there is only a small number of file types,
34 // so just show all of them.
36 $this->shownavigation
= false;
39 function isExpensive() {
46 * This abuses the query cache table by storing mime types as "titles".
48 * This will store entries like [[Media:BITMAP;image/jpeg;200;20000]]
49 * where the form is Media type;mime type;count;bytes.
51 * This relies on the behaviour that when value is tied, the order things
52 * come out of querycache table is the order they went in. Which is hacky.
53 * However, other special pages like Special:Deadendpages and
54 * Special:BrokenRedirects also rely on this.
56 public function getQueryInfo() {
57 $dbr = wfGetDB( DB_SLAVE
);
58 $fakeTitle = $dbr->buildConcat( array(
60 $dbr->addQuotes( ';' ),
62 $dbr->addQuotes( '/' ),
64 $dbr->addQuotes( ';' ),
66 $dbr->addQuotes( ';' ),
70 'tables' => array( 'image' ),
72 'title' => $fakeTitle,
73 'namespace' => NS_MEDIA
, /* needs to be something */
77 // WMF has a random null row in the db
78 'img_media_type IS NOT NULL'
91 * How to sort the results
93 * It's important that img_media_type come first, otherwise the
94 * tables will be fragmented.
95 * @return Array Fields to sort by
97 function getOrderFields() {
98 return array( 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' );
102 * Output the results of the query.
104 * @param $out OutputPage
105 * @param $skin Skin (deprecated presumably)
106 * @param $dbr IDatabase
107 * @param $res ResultWrapper Results from query
108 * @param $num integer Number of results
109 * @param $offset integer Paging offset (Should always be 0 in our case)
111 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
112 $prevMediaType = null;
113 foreach ( $res as $row ) {
114 list( $mediaType, $mime, $totalCount, $totalBytes ) = $this->splitFakeTitle( $row->title
);
115 if ( $prevMediaType !== $mediaType ) {
116 if ( $prevMediaType !== null ) {
117 // We're not at beginning, so we have to
118 // close the previous table.
119 $this->outputTableEnd();
121 $this->outputMediaType( $mediaType );
122 $this->outputTableStart( $mediaType );
123 $prevMediaType = $mediaType;
125 $this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) );
127 if ( $prevMediaType !== null ) {
128 $this->outputTableEnd();
133 * Output closing </table>
135 protected function outputTableEnd() {
136 $this->getOutput()->addHtml( Html
::closeElement( 'table' ) );
140 * Output a row of the stats table
142 * @param $mime String mime type (e.g. image/jpeg)
143 * @param $count integer Number of images of this type
144 * @param $totalBytes integer Total space for images of this type
146 protected function outputTableRow( $mime, $count, $bytes ) {
147 $mimeSearch = SpecialPage
::getTitleFor( 'MIMEsearch', $mime );
148 $row = Html
::rawElement(
151 Linker
::link( $mimeSearch, htmlspecialchars( $mime ) )
153 $row .= Html
::element(
156 $this->getExtensionList( $mime )
158 $row .= Html
::rawElement(
160 // Make sure js sorts it in numeric order
161 array( 'data-sort-value' => $count ),
162 $this->msg( 'mediastatistics-nfiles' )
163 ->numParams( $count )
164 /** @todo Check to be sure this really should have number formatting */
165 ->numParams( $this->makePercentPretty( $count / $this->totalCount
) )
168 $row .= Html
::rawElement(
170 // Make sure js sorts it in numeric order
171 array( 'data-sort-value' => $bytes ),
172 $this->msg( 'mediastatistics-nbytes' )
173 ->numParams( $bytes )
174 ->sizeParams( $bytes )
175 /** @todo Check to be sure this really should have number formatting */
176 ->numParams( $this->makePercentPretty( $bytes / $this->totalBytes
) )
180 $this->getOutput()->addHTML( Html
::rawElement( 'tr', array(), $row ) );
184 * @param float $decimal A decimal percentage (ie for 12.3%, this would be 0.123)
185 * @return String The percentage formatted so that 3 significant digits are shown.
187 protected function makePercentPretty( $decimal ) {
189 // Always show three useful digits
190 if ( $decimal == 0 ) {
193 if ( $decimal >= 100 ) {
196 $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal );
197 // Then remove any trailing 0's
198 return preg_replace( '/\.?0*$/', '', $percent );
202 * Given a mime type, return a comma separated list of allowed extensions.
204 * @param $mime String mime type
205 * @return String Comma separated list of allowed extensions (e.g. ".ogg, .oga")
207 private function getExtensionList( $mime ) {
208 $exts = MimeMagic
::singleton()->getExtensionsForType( $mime );
209 if ( $exts === null ) {
212 $extArray = explode( ' ', $exts );
213 $extArray = array_unique( $extArray );
214 foreach ( $extArray as &$ext ) {
218 return $this->getLanguage()->commaList( $extArray );
222 * Output the start of the table
224 * Including opening <table>, and first <tr> with column headers.
226 protected function outputTableStart( $mediaType ) {
227 $this->getOutput()->addHTML(
230 array( 'class' => array(
231 'mw-mediastats-table',
232 'mw-mediastats-table-' . strtolower( $mediaType ),
238 $this->getOutput()->addHTML( $this->getTableHeaderRow() );
242 * Get (not output) the header row for the table
244 * @return String the header row of the able
246 protected function getTableHeaderRow() {
247 $headers = array( 'mimetype', 'extensions', 'count', 'totalbytes' );
249 foreach ( $headers as $header ) {
250 $ths .= Html
::rawElement(
254 // mediastatistics-table-mimetype, mediastatistics-table-extensions
255 // tatistics-table-count, mediastatistics-table-totalbytes
256 $this->msg( 'mediastatistics-table-' . $header )->parse()
259 return Html
::rawElement( 'tr', array(), $ths );
263 * Output a header for a new media type section
265 * @param $mediaType string A media type (e.g. from the MEDIATYPE_xxx constants)
267 protected function outputMediaType( $mediaType ) {
268 $this->getOutput()->addHTML(
271 array( 'class' => array(
272 'mw-mediastats-mediatype',
273 'mw-mediastats-mediatype-' . strtolower( $mediaType )
276 // mediastatistics-header-unknown, mediastatistics-header-bitmap,
277 // mediastatistics-header-drawing, mediastatistics-header-audio,
278 // mediastatistics-header-video, mediastatistics-header-multimedia,
279 // mediastatistics-header-office, mediastatistics-header-text,
280 // mediastatistics-header-executable, mediastatistics-header-archive,
281 $this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text()
284 /** @todo Possibly could add a message here explaining what the different types are.
285 * not sure if it is needed though.
290 * parse the fake title format that this special page abuses querycache with.
292 * @param $fakeTitle String A string formatted as <media type>;<mime type>;<count>;<bytes>
293 * @return Array The constituant parts of $fakeTitle
295 private function splitFakeTitle( $fakeTitle ) {
296 return explode( ';', $fakeTitle, 4 );
300 * What group to put the page in
303 protected function getGroupName() {
308 * This method isn't used, since we override outputResults, but
309 * we need to implement since abstract in parent class.
312 * @param $result stdObject Result row
313 * @return bool|string|void
314 * @throws MWException
316 public function formatResult( $skin, $result ) {
317 throw new MWException( "unimplemented" );
321 * Initialize total values so we can figure out percentages later.
323 * @param $dbr IDatabase
324 * @param $res ResultWrapper
326 public function preprocessResults( $dbr, $res ) {
327 $this->totalCount
= $this->totalBytes
= 0;
328 foreach ( $res as $row ) {
329 $mediaStats = $this->splitFakeTitle( $row->title
);
330 $this->totalCount +
= isset( $mediaStats[2] ) ?
$mediaStats[2] : 0;
331 $this->totalBytes +
= isset( $mediaStats[3] ) ?
$mediaStats[3] : 0;