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 public 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 $mediaStats = $this->splitFakeTitle( $row->title
);
115 if ( count( $mediaStats ) < 4 ) {
118 list( $mediaType, $mime, $totalCount, $totalBytes ) = $mediaStats;
119 if ( $prevMediaType !== $mediaType ) {
120 if ( $prevMediaType !== null ) {
121 // We're not at beginning, so we have to
122 // close the previous table.
123 $this->outputTableEnd();
125 $this->outputMediaType( $mediaType );
126 $this->outputTableStart( $mediaType );
127 $prevMediaType = $mediaType;
129 $this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) );
131 if ( $prevMediaType !== null ) {
132 $this->outputTableEnd();
137 * Output closing </table>
139 protected function outputTableEnd() {
140 $this->getOutput()->addHtml( Html
::closeElement( 'table' ) );
144 * Output a row of the stats table
146 * @param $mime String mime type (e.g. image/jpeg)
147 * @param $count integer Number of images of this type
148 * @param $totalBytes integer Total space for images of this type
150 protected function outputTableRow( $mime, $count, $bytes ) {
151 $mimeSearch = SpecialPage
::getTitleFor( 'MIMEsearch', $mime );
152 $row = Html
::rawElement(
155 Linker
::link( $mimeSearch, htmlspecialchars( $mime ) )
157 $row .= Html
::element(
160 $this->getExtensionList( $mime )
162 $row .= Html
::rawElement(
164 // Make sure js sorts it in numeric order
165 array( 'data-sort-value' => $count ),
166 $this->msg( 'mediastatistics-nfiles' )
167 ->numParams( $count )
168 /** @todo Check to be sure this really should have number formatting */
169 ->numParams( $this->makePercentPretty( $count / $this->totalCount
) )
172 $row .= Html
::rawElement(
174 // Make sure js sorts it in numeric order
175 array( 'data-sort-value' => $bytes ),
176 $this->msg( 'mediastatistics-nbytes' )
177 ->numParams( $bytes )
178 ->sizeParams( $bytes )
179 /** @todo Check to be sure this really should have number formatting */
180 ->numParams( $this->makePercentPretty( $bytes / $this->totalBytes
) )
184 $this->getOutput()->addHTML( Html
::rawElement( 'tr', array(), $row ) );
188 * @param float $decimal A decimal percentage (ie for 12.3%, this would be 0.123)
189 * @return String The percentage formatted so that 3 significant digits are shown.
191 protected function makePercentPretty( $decimal ) {
193 // Always show three useful digits
194 if ( $decimal == 0 ) {
197 if ( $decimal >= 100 ) {
200 $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal );
201 // Then remove any trailing 0's
202 return preg_replace( '/\.?0*$/', '', $percent );
206 * Given a mime type, return a comma separated list of allowed extensions.
208 * @param $mime String mime type
209 * @return String Comma separated list of allowed extensions (e.g. ".ogg, .oga")
211 private function getExtensionList( $mime ) {
212 $exts = MimeMagic
::singleton()->getExtensionsForType( $mime );
213 if ( $exts === null ) {
216 $extArray = explode( ' ', $exts );
217 $extArray = array_unique( $extArray );
218 foreach ( $extArray as &$ext ) {
222 return $this->getLanguage()->commaList( $extArray );
226 * Output the start of the table
228 * Including opening <table>, and first <tr> with column headers.
230 protected function outputTableStart( $mediaType ) {
231 $this->getOutput()->addHTML(
234 array( 'class' => array(
235 'mw-mediastats-table',
236 'mw-mediastats-table-' . strtolower( $mediaType ),
242 $this->getOutput()->addHTML( $this->getTableHeaderRow() );
246 * Get (not output) the header row for the table
248 * @return String the header row of the able
250 protected function getTableHeaderRow() {
251 $headers = array( 'mimetype', 'extensions', 'count', 'totalbytes' );
253 foreach ( $headers as $header ) {
254 $ths .= Html
::rawElement(
258 // mediastatistics-table-mimetype, mediastatistics-table-extensions
259 // tatistics-table-count, mediastatistics-table-totalbytes
260 $this->msg( 'mediastatistics-table-' . $header )->parse()
263 return Html
::rawElement( 'tr', array(), $ths );
267 * Output a header for a new media type section
269 * @param $mediaType string A media type (e.g. from the MEDIATYPE_xxx constants)
271 protected function outputMediaType( $mediaType ) {
272 $this->getOutput()->addHTML(
275 array( 'class' => array(
276 'mw-mediastats-mediatype',
277 'mw-mediastats-mediatype-' . strtolower( $mediaType )
280 // mediastatistics-header-unknown, mediastatistics-header-bitmap,
281 // mediastatistics-header-drawing, mediastatistics-header-audio,
282 // mediastatistics-header-video, mediastatistics-header-multimedia,
283 // mediastatistics-header-office, mediastatistics-header-text,
284 // mediastatistics-header-executable, mediastatistics-header-archive,
285 $this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text()
288 /** @todo Possibly could add a message here explaining what the different types are.
289 * not sure if it is needed though.
294 * parse the fake title format that this special page abuses querycache with.
296 * @param $fakeTitle String A string formatted as <media type>;<mime type>;<count>;<bytes>
297 * @return Array The constituant parts of $fakeTitle
299 private function splitFakeTitle( $fakeTitle ) {
300 return explode( ';', $fakeTitle, 4 );
304 * What group to put the page in
307 protected function getGroupName() {
312 * This method isn't used, since we override outputResults, but
313 * we need to implement since abstract in parent class.
316 * @param $result stdObject Result row
317 * @return bool|string|void
318 * @throws MWException
320 public function formatResult( $skin, $result ) {
321 throw new MWException( "unimplemented" );
325 * Initialize total values so we can figure out percentages later.
327 * @param $dbr IDatabase
328 * @param $res ResultWrapper
330 public function preprocessResults( $dbr, $res ) {
331 $this->totalCount
= $this->totalBytes
= 0;
332 foreach ( $res as $row ) {
333 $mediaStats = $this->splitFakeTitle( $row->title
);
334 $this->totalCount +
= isset( $mediaStats[2] ) ?
$mediaStats[2] : 0;
335 $this->totalBytes +
= isset( $mediaStats[3] ) ?
$mediaStats[3] : 0;