Merge "inputs.less: Change focus state"
[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 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.
35 $this->limit = 5000;
36 $this->shownavigation = false;
37 }
38
39 function isExpensive() {
40 return true;
41 }
42
43 /**
44 * Query to do.
45 *
46 * This abuses the query cache table by storing mime types as "titles".
47 *
48 * This will store entries like [[Media:BITMAP;image/jpeg;200;20000]]
49 * where the form is Media type;mime type;count;bytes.
50 *
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.
55 */
56 public function getQueryInfo() {
57 $dbr = wfGetDB( DB_SLAVE );
58 $fakeTitle = $dbr->buildConcat( array(
59 'img_media_type',
60 $dbr->addQuotes( ';' ),
61 'img_major_mime',
62 $dbr->addQuotes( '/' ),
63 'img_minor_mime',
64 $dbr->addQuotes( ';' ),
65 'COUNT(*)',
66 $dbr->addQuotes( ';' ),
67 'SUM( img_size )'
68 ) );
69 return array(
70 'tables' => array( 'image' ),
71 'fields' => array(
72 'title' => $fakeTitle,
73 'namespace' => NS_MEDIA, /* needs to be something */
74 'value' => '1'
75 ),
76 'conds' => array(
77 // WMF has a random null row in the db
78 'img_media_type IS NOT NULL'
79 ),
80 'options' => array(
81 'GROUP BY' => array(
82 'img_media_type',
83 'img_major_mime',
84 'img_minor_mime',
85 )
86 )
87 );
88 }
89
90 /**
91 * How to sort the results
92 *
93 * It's important that img_media_type come first, otherwise the
94 * tables will be fragmented.
95 * @return Array Fields to sort by
96 */
97 function getOrderFields() {
98 return array( 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' );
99 }
100
101 /**
102 * Output the results of the query.
103 *
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)
110 */
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();
120 }
121 $this->outputMediaType( $mediaType );
122 $this->outputTableStart( $mediaType );
123 $prevMediaType = $mediaType;
124 }
125 $this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) );
126 }
127 if ( $prevMediaType !== null ) {
128 $this->outputTableEnd();
129 }
130 }
131
132 /**
133 * Output closing </table>
134 */
135 protected function outputTableEnd() {
136 $this->getOutput()->addHtml( Html::closeElement( 'table' ) );
137 }
138
139 /**
140 * Output a row of the stats table
141 *
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
145 */
146 protected function outputTableRow( $mime, $count, $bytes ) {
147 $mimeSearch = SpecialPage::getTitleFor( 'MIMEsearch', $mime );
148 $row = Html::rawElement(
149 'td',
150 array(),
151 Linker::link( $mimeSearch, htmlspecialchars( $mime ) )
152 );
153 $row .= Html::element(
154 'td',
155 array(),
156 $this->getExtensionList( $mime )
157 );
158 $row .= Html::rawElement(
159 'td',
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 ) )
166 ->parse()
167 );
168 $row .= Html::rawElement(
169 'td',
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 ) )
177 ->parse()
178 );
179
180 $this->getOutput()->addHTML( Html::rawElement( 'tr', array(), $row ) );
181 }
182
183 /**
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.
186 */
187 protected function makePercentPretty( $decimal ) {
188 $decimal *= 100;
189 // Always show three useful digits
190 if ( $decimal == 0 ) {
191 return '0';
192 }
193 if ( $decimal >= 100 ) {
194 return '100';
195 }
196 $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal );
197 // Then remove any trailing 0's
198 return preg_replace( '/\.?0*$/', '', $percent );
199 }
200
201 /**
202 * Given a mime type, return a comma separated list of allowed extensions.
203 *
204 * @param $mime String mime type
205 * @return String Comma separated list of allowed extensions (e.g. ".ogg, .oga")
206 */
207 private function getExtensionList( $mime ) {
208 $exts = MimeMagic::singleton()->getExtensionsForType( $mime );
209 if ( $exts === null ) {
210 return '';
211 }
212 $extArray = explode( ' ', $exts );
213 $extArray = array_unique( $extArray );
214 foreach ( $extArray as &$ext ) {
215 $ext = '.' . $ext;
216 }
217
218 return $this->getLanguage()->commaList( $extArray );
219 }
220
221 /**
222 * Output the start of the table
223 *
224 * Including opening <table>, and first <tr> with column headers.
225 */
226 protected function outputTableStart( $mediaType ) {
227 $this->getOutput()->addHTML(
228 Html::openElement(
229 'table',
230 array( 'class' => array(
231 'mw-mediastats-table',
232 'mw-mediastats-table-' . strtolower( $mediaType ),
233 'sortable',
234 'wikitable'
235 ))
236 )
237 );
238 $this->getOutput()->addHTML( $this->getTableHeaderRow() );
239 }
240
241 /**
242 * Get (not output) the header row for the table
243 *
244 * @return String the header row of the able
245 */
246 protected function getTableHeaderRow() {
247 $headers = array( 'mimetype', 'extensions', 'count', 'totalbytes' );
248 $ths = '';
249 foreach ( $headers as $header ) {
250 $ths .= Html::rawElement(
251 'th',
252 array(),
253 // for grep:
254 // mediastatistics-table-mimetype, mediastatistics-table-extensions
255 // tatistics-table-count, mediastatistics-table-totalbytes
256 $this->msg( 'mediastatistics-table-' . $header )->parse()
257 );
258 }
259 return Html::rawElement( 'tr', array(), $ths );
260 }
261
262 /**
263 * Output a header for a new media type section
264 *
265 * @param $mediaType string A media type (e.g. from the MEDIATYPE_xxx constants)
266 */
267 protected function outputMediaType( $mediaType ) {
268 $this->getOutput()->addHTML(
269 Html::element(
270 'h2',
271 array( 'class' => array(
272 'mw-mediastats-mediatype',
273 'mw-mediastats-mediatype-' . strtolower( $mediaType )
274 )),
275 // for grep
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()
282 )
283 );
284 /** @todo Possibly could add a message here explaining what the different types are.
285 * not sure if it is needed though.
286 */
287 }
288
289 /**
290 * parse the fake title format that this special page abuses querycache with.
291 *
292 * @param $fakeTitle String A string formatted as <media type>;<mime type>;<count>;<bytes>
293 * @return Array The constituant parts of $fakeTitle
294 */
295 private function splitFakeTitle( $fakeTitle ) {
296 return explode( ';', $fakeTitle, 4 );
297 }
298
299 /**
300 * What group to put the page in
301 * @return string
302 */
303 protected function getGroupName() {
304 return 'media';
305 }
306
307 /**
308 * This method isn't used, since we override outputResults, but
309 * we need to implement since abstract in parent class.
310 *
311 * @param $skin Skin
312 * @param $result stdObject Result row
313 * @return bool|string|void
314 * @throws MWException
315 */
316 public function formatResult( $skin, $result ) {
317 throw new MWException( "unimplemented" );
318 }
319
320 /**
321 * Initialize total values so we can figure out percentages later.
322 *
323 * @param $dbr IDatabase
324 * @param $res ResultWrapper
325 */
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;
332 }
333 $res->seek( 0 );
334 }
335 }