Debian Codegraph @

includes/specials/SpecialMediaStatistics.php

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