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 */
87 * How to sort the results
89 * It's important that img_media_type come first, otherwise the
90 * tables will be fragmented.
91 * @return Array Fields to sort by
93 function getOrderFields() {
94 return array( 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' );
98 * Output the results of the query.
100 * @param $out OutputPage
101 * @param $skin Skin (deprecated presumably)
102 * @param $dbr IDatabase
103 * @param $res ResultWrapper Results from query
104 * @param $num integer Number of results
105 * @param $offset integer Paging offset (Should always be 0 in our case)
107 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
108 $prevMediaType = null;
109 foreach ( $res as $row ) {
110 list( $mediaType, $mime, $totalCount, $totalBytes ) = $this->splitFakeTitle( $row->title
);
111 if ( $prevMediaType !== $mediaType ) {
112 if ( $prevMediaType !== null ) {
113 // We're not at beginning, so we have to
114 // close the previous table.
115 $this->outputTableEnd();
117 $this->outputMediaType( $mediaType );
118 $this->outputTableStart( $mediaType );
119 $prevMediaType = $mediaType;
121 $this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) );
123 if ( $prevMediaType !== null ) {
124 $this->outputTableEnd();
129 * Output closing </table>
131 protected function outputTableEnd() {
132 $this->getOutput()->addHtml( Html
::closeElement( 'table' ) );
136 * Output a row of the stats table
138 * @param $mime String mime type (e.g. image/jpeg)
139 * @param $count integer Number of images of this type
140 * @param $totalBytes integer Total space for images of this type
142 protected function outputTableRow( $mime, $count, $bytes ) {
143 $mimeSearch = SpecialPage
::getTitleFor( 'MIMEsearch', $mime );
144 $row = Html
::rawElement(
147 Linker
::link( $mimeSearch, htmlspecialchars( $mime ) )
149 $row .= Html
::element(
152 $this->getExtensionList( $mime )
154 $row .= Html
::rawElement(
156 // Make sure js sorts it in numeric order
157 array( 'data-sort-value' => $count ),
158 $this->msg( 'mediastatistics-nfiles' )
159 ->numParams( $count )
160 /** @todo Check to be sure this really should have number formatting */
161 ->numParams( $this->makePercentPretty( $count / $this->totalCount
) )
164 $row .= Html
::rawElement(
166 // Make sure js sorts it in numeric order
167 array( 'data-sort-value' => $bytes ),
168 $this->msg( 'mediastatistics-nbytes' )
169 ->numParams( $bytes )
170 ->sizeParams( $bytes )
171 /** @todo Check to be sure this really should have number formatting */
172 ->numParams( $this->makePercentPretty( $bytes / $this->totalBytes
) )
176 $this->getOutput()->addHTML( Html
::rawElement( 'tr', array(), $row ) );
180 * @param float $decimal A decimal percentage (ie for 12.3%, this would be 0.123)
181 * @return String The percentage formatted so that 3 significant digits are shown.
183 protected function makePercentPretty( $decimal ) {
185 // Always show three useful digits
186 if ( $decimal == 0 ) {
189 if ( $decimal >= 100 ) {
192 $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal );
193 // Then remove any trailing 0's
194 return preg_replace( '/\.?0*$/', '', $percent );
198 * Given a mime type, return a comma separated list of allowed extensions.
200 * @param $mime String mime type
201 * @return String Comma separated list of allowed extensions (e.g. ".ogg, .oga")
203 private function getExtensionList( $mime ) {
204 $exts = MimeMagic
::singleton()->getExtensionsForType( $mime );
205 if ( $exts === null ) {
208 $extArray = explode( ' ', $exts );
209 $extArray = array_unique( $extArray );
210 foreach ( $extArray as &$ext ) {
214 return $this->getLanguage()->commaList( $extArray );
218 * Output the start of the table
220 * Including opening <table>, and first <tr> with column headers.
222 protected function outputTableStart( $mediaType ) {
223 $this->getOutput()->addHTML(
226 array( 'class' => array(
227 'mw-mediastats-table',
228 'mw-mediastats-table-' . strtolower( $mediaType ),
234 $this->getOutput()->addHTML( $this->getTableHeaderRow() );
238 * Get (not output) the header row for the table
240 * @return String the header row of the able
242 protected function getTableHeaderRow() {
243 $headers = array( 'mimetype', 'extensions', 'count', 'totalbytes' );
245 foreach ( $headers as $header ) {
246 $ths .= Html
::rawElement(
250 // mediastatistics-table-mimetype, mediastatistics-table-extensions
251 // tatistics-table-count, mediastatistics-table-totalbytes
252 $this->msg( 'mediastatistics-table-' . $header )->parse()
255 return Html
::rawElement( 'tr', array(), $ths );
259 * Output a header for a new media type section
261 * @param $mediaType string A media type (e.g. from the MEDIATYPE_xxx constants)
263 protected function outputMediaType( $mediaType ) {
264 $this->getOutput()->addHTML(
267 array( 'class' => array(
268 'mw-mediastats-mediatype',
269 'mw-mediastats-mediatype-' . strtolower( $mediaType )
272 // mediastatistics-header-unknown, mediastatistics-header-bitmap,
273 // mediastatistics-header-drawing, mediastatistics-header-audio,
274 // mediastatistics-header-video, mediastatistics-header-multimedia,
275 // mediastatistics-header-office, mediastatistics-header-text,
276 // mediastatistics-header-executable, mediastatistics-header-archive,
277 $this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text()
280 /** @todo Possibly could add a message here explaining what the different types are.
281 * not sure if it is needed though.
286 * parse the fake title format that this special page abuses querycache with.
288 * @param $fakeTitle String A string formatted as <media type>;<mime type>;<count>;<bytes>
289 * @return Array The constituant parts of $fakeTitle
291 private function splitFakeTitle( $fakeTitle ) {
292 return explode( ';', $fakeTitle, 4 );
296 * What group to put the page in
299 protected function getGroupName() {
304 * This method isn't used, since we override outputResults, but
305 * we need to implement since abstract in parent class.
308 * @param $result stdObject Result row
309 * @return bool|string|void
310 * @throws MWException
312 public function formatResult( $skin, $result ) {
313 throw new MWException( "unimplemented" );
317 * Initialize total values so we can figure out percentages later.
319 * @param $dbr IDatabase
320 * @param $res ResultWrapper
322 public function preprocessResults( $dbr, $res ) {
323 $this->totalCount
= $this->totalBytes
= 0;
324 foreach ( $res as $row ) {
325 list( , , $count, $bytes ) = $this->splitFakeTitle( $row->title
);
326 $this->totalCount +
= $count;
327 $this->totalBytes +
= $bytes;