Add PLURAL support and "human readable" sizes for mediastatistics messages
[lhc/web/wiklou.git] / includes / specials / SpecialMediaStatistics.php
1 <?php
2 /**
3 * Implements Special:MediaStatistics
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 * @author Brian Wolff
23 */
24
25 /**
26 * @ingroup SpecialPage
27 */
28 class MediaStatisticsPage extends QueryPage {
29 protected $totalCount = 0, $totalBytes = 0;
30 /**
31 * @var integer $totalPerType Combined file size of all files in a section
32 */
33 protected $totalPerType = 0;
34 /**
35 * @var integer $totalSize Combined file size of all files
36 */
37 protected $totalSize = 0;
38
39 function __construct( $name = 'MediaStatistics' ) {
40 parent::__construct( $name );
41 // Generally speaking there is only a small number of file types,
42 // so just show all of them.
43 $this->limit = 5000;
44 $this->shownavigation = false;
45 }
46
47 public function isExpensive() {
48 return true;
49 }
50
51 /**
52 * Query to do.
53 *
54 * This abuses the query cache table by storing mime types as "titles".
55 *
56 * This will store entries like [[Media:BITMAP;image/jpeg;200;20000]]
57 * where the form is Media type;mime type;count;bytes.
58 *
59 * This relies on the behaviour that when value is tied, the order things
60 * come out of querycache table is the order they went in. Which is hacky.
61 * However, other special pages like Special:Deadendpages and
62 * Special:BrokenRedirects also rely on this.
63 */
64 public function getQueryInfo() {
65 $dbr = wfGetDB( DB_SLAVE );
66 $fakeTitle = $dbr->buildConcat( array(
67 'img_media_type',
68 $dbr->addQuotes( ';' ),
69 'img_major_mime',
70 $dbr->addQuotes( '/' ),
71 'img_minor_mime',
72 $dbr->addQuotes( ';' ),
73 'COUNT(*)',
74 $dbr->addQuotes( ';' ),
75 'SUM( img_size )'
76 ) );
77 return array(
78 'tables' => array( 'image' ),
79 'fields' => array(
80 'title' => $fakeTitle,
81 'namespace' => NS_MEDIA, /* needs to be something */
82 'value' => '1'
83 ),
84 'conds' => array(
85 // WMF has a random null row in the db
86 'img_media_type IS NOT NULL'
87 ),
88 'options' => array(
89 'GROUP BY' => array(
90 'img_media_type',
91 'img_major_mime',
92 'img_minor_mime',
93 )
94 )
95 );
96 }
97
98 /**
99 * How to sort the results
100 *
101 * It's important that img_media_type come first, otherwise the
102 * tables will be fragmented.
103 * @return Array Fields to sort by
104 */
105 function getOrderFields() {
106 return array( 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' );
107 }
108
109 /**
110 * Output the results of the query.
111 *
112 * @param $out OutputPage
113 * @param $skin Skin (deprecated presumably)
114 * @param $dbr IDatabase
115 * @param $res ResultWrapper Results from query
116 * @param $num integer Number of results
117 * @param $offset integer Paging offset (Should always be 0 in our case)
118 */
119 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
120 $prevMediaType = null;
121 foreach ( $res as $row ) {
122 $mediaStats = $this->splitFakeTitle( $row->title );
123 if ( count( $mediaStats ) < 4 ) {
124 continue;
125 }
126 list( $mediaType, $mime, $totalCount, $totalBytes ) = $mediaStats;
127 if ( $prevMediaType !== $mediaType ) {
128 if ( $prevMediaType !== null ) {
129 // We're not at beginning, so we have to
130 // close the previous table.
131 $this->outputTableEnd();
132 }
133 $this->outputMediaType( $mediaType );
134 $this->totalPerType = 0;
135 $this->outputTableStart( $mediaType );
136 $prevMediaType = $mediaType;
137 }
138 $this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) );
139 }
140 if ( $prevMediaType !== null ) {
141 $this->outputTableEnd();
142 $this->getOutput()->addWikiText(
143 $this->msg( 'mediastatistics-allbytes' )
144 ->numParams( $this->totalSize )
145 ->sizeParams( $this->totalSize )
146 ->text()
147 );
148 }
149 }
150
151 /**
152 * Output closing </table>
153 */
154 protected function outputTableEnd() {
155 $this->getOutput()->addHtml( Html::closeElement( 'table' ) );
156 $this->getOutput()->addWikiText(
157 $this->msg( 'mediastatistics-bytespertype' )
158 ->numParams( $this->totalPerType )
159 ->sizeParams( $this->totalPerType )
160 ->numParams( $this->makePercentPretty( $this->totalPerType / $this->totalBytes ) )
161 ->text()
162 );
163 $this->totalSize += $this->totalPerType;
164 }
165
166 /**
167 * Output a row of the stats table
168 *
169 * @param $mime String mime type (e.g. image/jpeg)
170 * @param $count integer Number of images of this type
171 * @param $totalBytes integer Total space for images of this type
172 */
173 protected function outputTableRow( $mime, $count, $bytes ) {
174 $mimeSearch = SpecialPage::getTitleFor( 'MIMEsearch', $mime );
175 $row = Html::rawElement(
176 'td',
177 array(),
178 Linker::link( $mimeSearch, htmlspecialchars( $mime ) )
179 );
180 $row .= Html::element(
181 'td',
182 array(),
183 $this->getExtensionList( $mime )
184 );
185 $row .= Html::rawElement(
186 'td',
187 // Make sure js sorts it in numeric order
188 array( 'data-sort-value' => $count ),
189 $this->msg( 'mediastatistics-nfiles' )
190 ->numParams( $count )
191 /** @todo Check to be sure this really should have number formatting */
192 ->numParams( $this->makePercentPretty( $count / $this->totalCount ) )
193 ->parse()
194 );
195 $row .= Html::rawElement(
196 'td',
197 // Make sure js sorts it in numeric order
198 array( 'data-sort-value' => $bytes ),
199 $this->msg( 'mediastatistics-nbytes' )
200 ->numParams( $bytes )
201 ->sizeParams( $bytes )
202 /** @todo Check to be sure this really should have number formatting */
203 ->numParams( $this->makePercentPretty( $bytes / $this->totalBytes ) )
204 ->parse()
205 );
206 $this->totalPerType += $bytes;
207 $this->getOutput()->addHTML( Html::rawElement( 'tr', array(), $row ) );
208 }
209
210 /**
211 * @param float $decimal A decimal percentage (ie for 12.3%, this would be 0.123)
212 * @return String The percentage formatted so that 3 significant digits are shown.
213 */
214 protected function makePercentPretty( $decimal ) {
215 $decimal *= 100;
216 // Always show three useful digits
217 if ( $decimal == 0 ) {
218 return '0';
219 }
220 if ( $decimal >= 100 ) {
221 return '100';
222 }
223 $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal );
224 // Then remove any trailing 0's
225 return preg_replace( '/\.?0*$/', '', $percent );
226 }
227
228 /**
229 * Given a mime type, return a comma separated list of allowed extensions.
230 *
231 * @param $mime String mime type
232 * @return String Comma separated list of allowed extensions (e.g. ".ogg, .oga")
233 */
234 private function getExtensionList( $mime ) {
235 $exts = MimeMagic::singleton()->getExtensionsForType( $mime );
236 if ( $exts === null ) {
237 return '';
238 }
239 $extArray = explode( ' ', $exts );
240 $extArray = array_unique( $extArray );
241 foreach ( $extArray as &$ext ) {
242 $ext = '.' . $ext;
243 }
244
245 return $this->getLanguage()->commaList( $extArray );
246 }
247
248 /**
249 * Output the start of the table
250 *
251 * Including opening <table>, and first <tr> with column headers.
252 */
253 protected function outputTableStart( $mediaType ) {
254 $this->getOutput()->addHTML(
255 Html::openElement(
256 'table',
257 array( 'class' => array(
258 'mw-mediastats-table',
259 'mw-mediastats-table-' . strtolower( $mediaType ),
260 'sortable',
261 'wikitable'
262 ) )
263 )
264 );
265 $this->getOutput()->addHTML( $this->getTableHeaderRow() );
266 }
267
268 /**
269 * Get (not output) the header row for the table
270 *
271 * @return String the header row of the able
272 */
273 protected function getTableHeaderRow() {
274 $headers = array( 'mimetype', 'extensions', 'count', 'totalbytes' );
275 $ths = '';
276 foreach ( $headers as $header ) {
277 $ths .= Html::rawElement(
278 'th',
279 array(),
280 // for grep:
281 // mediastatistics-table-mimetype, mediastatistics-table-extensions
282 // tatistics-table-count, mediastatistics-table-totalbytes
283 $this->msg( 'mediastatistics-table-' . $header )->parse()
284 );
285 }
286 return Html::rawElement( 'tr', array(), $ths );
287 }
288
289 /**
290 * Output a header for a new media type section
291 *
292 * @param $mediaType string A media type (e.g. from the MEDIATYPE_xxx constants)
293 */
294 protected function outputMediaType( $mediaType ) {
295 $this->getOutput()->addHTML(
296 Html::element(
297 'h2',
298 array( 'class' => array(
299 'mw-mediastats-mediatype',
300 'mw-mediastats-mediatype-' . strtolower( $mediaType )
301 ) ),
302 // for grep
303 // mediastatistics-header-unknown, mediastatistics-header-bitmap,
304 // mediastatistics-header-drawing, mediastatistics-header-audio,
305 // mediastatistics-header-video, mediastatistics-header-multimedia,
306 // mediastatistics-header-office, mediastatistics-header-text,
307 // mediastatistics-header-executable, mediastatistics-header-archive,
308 $this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text()
309 )
310 );
311 /** @todo Possibly could add a message here explaining what the different types are.
312 * not sure if it is needed though.
313 */
314 }
315
316 /**
317 * parse the fake title format that this special page abuses querycache with.
318 *
319 * @param $fakeTitle String A string formatted as <media type>;<mime type>;<count>;<bytes>
320 * @return Array The constituant parts of $fakeTitle
321 */
322 private function splitFakeTitle( $fakeTitle ) {
323 return explode( ';', $fakeTitle, 4 );
324 }
325
326 /**
327 * What group to put the page in
328 * @return string
329 */
330 protected function getGroupName() {
331 return 'media';
332 }
333
334 /**
335 * This method isn't used, since we override outputResults, but
336 * we need to implement since abstract in parent class.
337 *
338 * @param $skin Skin
339 * @param $result stdObject Result row
340 * @return bool|string|void
341 * @throws MWException
342 */
343 public function formatResult( $skin, $result ) {
344 throw new MWException( "unimplemented" );
345 }
346
347 /**
348 * Initialize total values so we can figure out percentages later.
349 *
350 * @param $dbr IDatabase
351 * @param $res ResultWrapper
352 */
353 public function preprocessResults( $dbr, $res ) {
354 $this->totalCount = $this->totalBytes = 0;
355 foreach ( $res as $row ) {
356 $mediaStats = $this->splitFakeTitle( $row->title );
357 $this->totalCount += isset( $mediaStats[2] ) ? $mediaStats[2] : 0;
358 $this->totalBytes += isset( $mediaStats[3] ) ? $mediaStats[3] : 0;
359 }
360 $res->seek( 0 );
361 }
362 }