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