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