MediaWiki  master
SpecialMediaStatistics.php
Go to the documentation of this file.
1 <?php
25 namespace MediaWiki\Specials;
26 
27 use LogicException;
33 use MimeAnalyzer;
34 use Skin;
35 use stdClass;
39 
44 
45  public const MAX_LIMIT = 5000;
46 
47  protected $totalCount = 0, $totalBytes = 0;
48 
52  protected $totalPerType = 0;
53 
57  protected $countPerType = 0;
58 
62  protected $totalSize = 0;
63 
64  private MimeAnalyzer $mimeAnalyzer;
65 
71  public function __construct(
72  MimeAnalyzer $mimeAnalyzer,
73  IConnectionProvider $dbProvider,
74  LinkBatchFactory $linkBatchFactory
75  ) {
76  parent::__construct( 'MediaStatistics' );
77  // Generally speaking there is only a small number of file types,
78  // so just show all of them.
79  $this->limit = self::MAX_LIMIT;
80  $this->shownavigation = false;
81  $this->mimeAnalyzer = $mimeAnalyzer;
82  $this->setDatabaseProvider( $dbProvider );
83  $this->setLinkBatchFactory( $linkBatchFactory );
84  }
85 
86  public function isExpensive() {
87  return true;
88  }
89 
104  public function getQueryInfo() {
105  $dbr = $this->getDatabaseProvider()->getReplicaDatabase();
106  $fakeTitle = $dbr->buildConcat( [
107  'img_media_type',
108  $dbr->addQuotes( ';' ),
109  'img_major_mime',
110  $dbr->addQuotes( '/' ),
111  'img_minor_mime',
112  $dbr->addQuotes( ';' ),
113  $dbr->buildStringCast( 'COUNT(*)' ),
114  $dbr->addQuotes( ';' ),
115  $dbr->buildStringCast( 'SUM( img_size )' )
116  ] );
117  return [
118  'tables' => [ 'image' ],
119  'fields' => [
120  'title' => $fakeTitle,
121  'namespace' => NS_MEDIA, /* needs to be something */
122  'value' => '1'
123  ],
124  'options' => [
125  'GROUP BY' => [
126  'img_media_type',
127  'img_major_mime',
128  'img_minor_mime',
129  ]
130  ]
131  ];
132  }
133 
141  protected function getOrderFields() {
142  return [ 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' ];
143  }
144 
155  protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
156  $prevMediaType = null;
157  foreach ( $res as $row ) {
158  $mediaStats = $this->splitFakeTitle( $row->title );
159  if ( count( $mediaStats ) < 4 ) {
160  continue;
161  }
162  [ $mediaType, $mime, $totalCount, $totalBytes ] = $mediaStats;
163  if ( $prevMediaType !== $mediaType ) {
164  if ( $prevMediaType !== null ) {
165  // We're not at beginning, so we have to
166  // close the previous table.
167  $this->outputTableEnd();
168  }
169  $this->outputMediaType( $mediaType );
170  $this->totalPerType = 0;
171  $this->countPerType = 0;
172  $this->outputTableStart( $mediaType );
173  $prevMediaType = $mediaType;
174  }
175  $this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) );
176  }
177  if ( $prevMediaType !== null ) {
178  $this->outputTableEnd();
179  // add total size of all files
180  $this->outputMediaType( 'total' );
181  $this->getOutput()->addWikiTextAsInterface(
182  $this->msg( 'mediastatistics-allbytes' )
183  ->numParams( $this->totalSize )
184  ->sizeParams( $this->totalSize )
185  ->numParams( $this->totalCount )
186  ->text()
187  );
188  }
189  }
190 
194  protected function outputTableEnd() {
195  $this->getOutput()->addHTML(
196  Html::closeElement( 'tbody' ) .
197  Html::closeElement( 'table' )
198  );
199  $this->getOutput()->addWikiTextAsInterface(
200  $this->msg( 'mediastatistics-bytespertype' )
201  ->numParams( $this->totalPerType )
202  ->sizeParams( $this->totalPerType )
203  ->numParams( $this->makePercentPretty( $this->totalPerType / $this->totalBytes ) )
204  ->numParams( $this->countPerType )
205  ->numParams( $this->makePercentPretty( $this->countPerType / $this->totalCount ) )
206  ->text()
207  );
208  $this->totalSize += $this->totalPerType;
209  }
210 
218  protected function outputTableRow( $mime, $count, $bytes ) {
219  $mimeSearch = SpecialPage::getTitleFor( 'MIMEsearch', $mime );
220  $linkRenderer = $this->getLinkRenderer();
221  $row = Html::rawElement(
222  'td',
223  [],
224  $linkRenderer->makeLink( $mimeSearch, $mime )
225  );
226  $row .= Html::rawElement(
227  'td',
228  [],
229  $this->getExtensionList( $mime )
230  );
231  $row .= Html::rawElement(
232  'td',
233  // Make sure js sorts it in numeric order
234  [ 'data-sort-value' => $count ],
235  $this->msg( 'mediastatistics-nfiles' )
236  ->numParams( $count )
238  ->numParams( $this->makePercentPretty( $count / $this->totalCount ) )
239  ->parse()
240  );
241  $row .= Html::rawElement(
242  'td',
243  // Make sure js sorts it in numeric order
244  [ 'data-sort-value' => $bytes ],
245  $this->msg( 'mediastatistics-nbytes' )
246  ->numParams( $bytes )
247  ->sizeParams( $bytes )
249  ->numParams( $this->makePercentPretty( $bytes / $this->totalBytes ) )
250  ->parse()
251  );
252  $this->totalPerType += $bytes;
253  $this->countPerType += $count;
254  $this->getOutput()->addHTML( Html::rawElement( 'tr', [], $row ) );
255  }
256 
261  protected function makePercentPretty( $decimal ) {
262  $decimal *= 100;
263  // Always show three useful digits
264  if ( $decimal == 0 ) {
265  return '0';
266  }
267  if ( $decimal >= 100 ) {
268  return '100';
269  }
270  $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal );
271  // Then remove any trailing 0's
272  return preg_replace( '/\.?0*$/', '', $percent );
273  }
274 
281  private function getExtensionList( $mime ) {
282  $exts = $this->mimeAnalyzer->getExtensionsFromMimeType( $mime );
283  if ( !$exts ) {
284  return '';
285  }
286  foreach ( $exts as &$ext ) {
287  $ext = htmlspecialchars( '.' . $ext );
288  }
289 
290  return $this->getLanguage()->commaList( $exts );
291  }
292 
299  protected function outputTableStart( $mediaType ) {
300  $out = $this->getOutput();
301  $out->addModuleStyles( 'jquery.tablesorter.styles' );
302  $out->addModules( 'jquery.tablesorter' );
303  $out->addHTML(
305  'table',
306  [ 'class' => [
307  'mw-mediastats-table',
308  'mw-mediastats-table-' . strtolower( $mediaType ),
309  'sortable',
310  'wikitable'
311  ] ]
312  ) .
313  Html::rawElement( 'thead', [], $this->getTableHeaderRow() ) .
314  Html::openElement( 'tbody' )
315  );
316  }
317 
323  protected function getTableHeaderRow() {
324  $headers = [ 'mimetype', 'extensions', 'count', 'totalbytes' ];
325  $ths = '';
326  foreach ( $headers as $header ) {
327  $ths .= Html::rawElement(
328  'th',
329  [],
330  // for grep:
331  // mediastatistics-table-mimetype, mediastatistics-table-extensions
332  // mediastatistics-table-count, mediastatistics-table-totalbytes
333  $this->msg( 'mediastatistics-table-' . $header )->parse()
334  );
335  }
336  return Html::rawElement( 'tr', [], $ths );
337  }
338 
344  protected function outputMediaType( $mediaType ) {
345  $this->getOutput()->addHTML(
347  'h2',
348  [ 'class' => [
349  'mw-mediastats-mediatype',
350  'mw-mediastats-mediatype-' . strtolower( $mediaType )
351  ] ],
352  // for grep
353  // mediastatistics-header-unknown, mediastatistics-header-bitmap,
354  // mediastatistics-header-drawing, mediastatistics-header-audio,
355  // mediastatistics-header-video, mediastatistics-header-multimedia,
356  // mediastatistics-header-office, mediastatistics-header-text,
357  // mediastatistics-header-executable, mediastatistics-header-archive,
358  // mediastatistics-header-3d,
359  $this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text()
360  )
361  );
365  }
366 
373  private function splitFakeTitle( $fakeTitle ) {
374  return explode( ';', $fakeTitle, 4 );
375  }
376 
381  protected function getGroupName() {
382  return 'media';
383  }
384 
394  public function formatResult( $skin, $result ) {
395  throw new LogicException( "unimplemented" );
396  }
397 
404  public function preprocessResults( $dbr, $res ) {
405  $this->executeLBFromResultWrapper( $res );
406  $this->totalCount = $this->totalBytes = 0;
407  foreach ( $res as $row ) {
408  $mediaStats = $this->splitFakeTitle( $row->title );
409  $this->totalCount += $mediaStats[2] ?? 0;
410  $this->totalBytes += $mediaStats[3] ?? 0;
411  }
412  $res->seek( 0 );
413  }
414 }
415 
420 class_alias( SpecialMediaStatistics::class, 'SpecialMediaStatistics' );
const NS_MEDIA
Definition: Defines.php:52
This class is a collection of static functions that serve two purposes:
Definition: Html.php:57
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:288
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:239
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:352
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:264
This is one of the Core classes and should be read at least once by any new developers.
Definition: OutputPage.php:93
This is a class for doing query pages; since they're almost all the same, we factor out some of the f...
Definition: QueryPage.php:88
setDatabaseProvider(IConnectionProvider $databaseProvider)
Definition: QueryPage.php:985
int $offset
The offset and limit in use, as passed to the query() function.
Definition: QueryPage.php:93
executeLBFromResultWrapper(IResultWrapper $res, $ns=null)
Creates a new LinkBatch object, adds all pages from the passed result wrapper (MUST include title and...
Definition: QueryPage.php:946
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
Definition: QueryPage.php:185
Parent class for all special pages.
Definition: SpecialPage.php:65
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getLanguage()
Shortcut to get user's language.
formatResult( $skin, $result)
This method isn't used, since we override outputResults, but we need to implement since abstract in p...
int $totalPerType
Combined file size of all files in a section.
isExpensive()
Should this query page only be updated offline on large wikis?
int $countPerType
Combined file count of all files in a section.
outputTableStart( $mediaType)
Output the start of the table.
getTableHeaderRow()
Get (not output) the header row for the table.
outputMediaType( $mediaType)
Output a header for a new media type section.
outputTableRow( $mime, $count, $bytes)
Output a row of the stats table.
__construct(MimeAnalyzer $mimeAnalyzer, IConnectionProvider $dbProvider, LinkBatchFactory $linkBatchFactory)
preprocessResults( $dbr, $res)
Initialize total values so we can figure out percentages later.
getGroupName()
What group to put the page in.
outputResults( $out, $skin, $dbr, $res, $num, $offset)
Output the results of the query.
int $totalSize
Combined file size of all files.
Implements functions related to MIME types such as detection and mapping to file extension.
The base class for all skins.
Definition: Skin.php:60
Provide primary and replica IDatabase connections.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:36
Result wrapper for grabbing data queried from an IDatabase object.
$mime
Definition: router.php:60
if(!is_readable( $file)) $ext
Definition: router.php:48
$header