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