MediaWiki  master
SpecialMediaStatistics.php
Go to the documentation of this file.
1 <?php
29 
34  protected $totalCount = 0, $totalBytes = 0;
35 
39  protected $totalPerType = 0;
40 
44  protected $totalSize = 0;
45 
47  private $mimeAnalyzer;
48 
54  public function __construct(
58  ) {
59  parent::__construct( 'MediaStatistics' );
60  // Generally speaking there is only a small number of file types,
61  // so just show all of them.
62  $this->limit = 5000;
63  $this->shownavigation = false;
64  $this->mimeAnalyzer = $mimeAnalyzer;
65  $this->setDBLoadBalancer( $loadBalancer );
66  $this->setLinkBatchFactory( $linkBatchFactory );
67  }
68 
69  public function isExpensive() {
70  return true;
71  }
72 
87  public function getQueryInfo() {
88  $dbr = $this->getDBLoadBalancer()->getConnectionRef( ILoadBalancer::DB_REPLICA );
89  $fakeTitle = $dbr->buildConcat( [
90  'img_media_type',
91  $dbr->addQuotes( ';' ),
92  'img_major_mime',
93  $dbr->addQuotes( '/' ),
94  'img_minor_mime',
95  $dbr->addQuotes( ';' ),
96  $dbr->buildStringCast( 'COUNT(*)' ),
97  $dbr->addQuotes( ';' ),
98  $dbr->buildStringCast( 'SUM( img_size )' )
99  ] );
100  return [
101  'tables' => [ 'image' ],
102  'fields' => [
103  'title' => $fakeTitle,
104  'namespace' => NS_MEDIA, /* needs to be something */
105  'value' => '1'
106  ],
107  'options' => [
108  'GROUP BY' => [
109  'img_media_type',
110  'img_major_mime',
111  'img_minor_mime',
112  ]
113  ]
114  ];
115  }
116 
124  protected function getOrderFields() {
125  return [ 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' ];
126  }
127 
138  protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) {
139  $prevMediaType = null;
140  foreach ( $res as $row ) {
141  $mediaStats = $this->splitFakeTitle( $row->title );
142  if ( count( $mediaStats ) < 4 ) {
143  continue;
144  }
145  list( $mediaType, $mime, $totalCount, $totalBytes ) = $mediaStats;
146  if ( $prevMediaType !== $mediaType ) {
147  if ( $prevMediaType !== null ) {
148  // We're not at beginning, so we have to
149  // close the previous table.
150  $this->outputTableEnd();
151  }
152  $this->outputMediaType( $mediaType );
153  $this->totalPerType = 0;
154  $this->outputTableStart( $mediaType );
155  $prevMediaType = $mediaType;
156  }
157  $this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) );
158  }
159  if ( $prevMediaType !== null ) {
160  $this->outputTableEnd();
161  // add total size of all files
162  $this->outputMediaType( 'total' );
163  $this->getOutput()->addWikiTextAsInterface(
164  $this->msg( 'mediastatistics-allbytes' )
165  ->numParams( $this->totalSize )
166  ->sizeParams( $this->totalSize )
167  ->text()
168  );
169  }
170  }
171 
175  protected function outputTableEnd() {
176  $this->getOutput()->addHTML(
177  Html::closeElement( 'tbody' ) .
178  Html::closeElement( 'table' )
179  );
180  $this->getOutput()->addWikiTextAsInterface(
181  $this->msg( 'mediastatistics-bytespertype' )
182  ->numParams( $this->totalPerType )
183  ->sizeParams( $this->totalPerType )
184  ->numParams( $this->makePercentPretty( $this->totalPerType / $this->totalBytes ) )
185  ->text()
186  );
187  $this->totalSize += $this->totalPerType;
188  }
189 
197  protected function outputTableRow( $mime, $count, $bytes ) {
198  $mimeSearch = SpecialPage::getTitleFor( 'MIMEsearch', $mime );
199  $linkRenderer = $this->getLinkRenderer();
200  $row = Html::rawElement(
201  'td',
202  [],
203  $linkRenderer->makeLink( $mimeSearch, $mime )
204  );
205  $row .= Html::rawElement(
206  'td',
207  [],
208  $this->getExtensionList( $mime )
209  );
210  $row .= Html::rawElement(
211  'td',
212  // Make sure js sorts it in numeric order
213  [ 'data-sort-value' => $count ],
214  $this->msg( 'mediastatistics-nfiles' )
215  ->numParams( $count )
217  ->numParams( $this->makePercentPretty( $count / $this->totalCount ) )
218  ->parse()
219  );
220  $row .= Html::rawElement(
221  'td',
222  // Make sure js sorts it in numeric order
223  [ 'data-sort-value' => $bytes ],
224  $this->msg( 'mediastatistics-nbytes' )
225  ->numParams( $bytes )
226  ->sizeParams( $bytes )
228  ->numParams( $this->makePercentPretty( $bytes / $this->totalBytes ) )
229  ->parse()
230  );
231  $this->totalPerType += $bytes;
232  $this->getOutput()->addHTML( Html::rawElement( 'tr', [], $row ) );
233  }
234 
239  protected function makePercentPretty( $decimal ) {
240  $decimal *= 100;
241  // Always show three useful digits
242  if ( $decimal == 0 ) {
243  return '0';
244  }
245  if ( $decimal >= 100 ) {
246  return '100';
247  }
248  $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal );
249  // Then remove any trailing 0's
250  return preg_replace( '/\.?0*$/', '', $percent );
251  }
252 
259  private function getExtensionList( $mime ) {
260  $exts = $this->mimeAnalyzer->getExtensionsFromMimeType( $mime );
261  if ( !$exts ) {
262  return '';
263  }
264  foreach ( $exts as &$ext ) {
265  $ext = htmlspecialchars( '.' . $ext );
266  }
267 
268  return $this->getLanguage()->commaList( $exts );
269  }
270 
277  protected function outputTableStart( $mediaType ) {
278  $out = $this->getOutput();
279  $out->addModuleStyles( 'jquery.tablesorter.styles' );
280  $out->addModules( 'jquery.tablesorter' );
281  $out->addHTML(
283  'table',
284  [ 'class' => [
285  'mw-mediastats-table',
286  'mw-mediastats-table-' . strtolower( $mediaType ),
287  'sortable',
288  'wikitable'
289  ] ]
290  ) .
291  Html::rawElement( 'thead', [], $this->getTableHeaderRow() ) .
292  Html::openElement( 'tbody' )
293  );
294  }
295 
301  protected function getTableHeaderRow() {
302  $headers = [ 'mimetype', 'extensions', 'count', 'totalbytes' ];
303  $ths = '';
304  foreach ( $headers as $header ) {
305  $ths .= Html::rawElement(
306  'th',
307  [],
308  // for grep:
309  // mediastatistics-table-mimetype, mediastatistics-table-extensions
310  // tatistics-table-count, mediastatistics-table-totalbytes
311  $this->msg( 'mediastatistics-table-' . $header )->parse()
312  );
313  }
314  return Html::rawElement( 'tr', [], $ths );
315  }
316 
322  protected function outputMediaType( $mediaType ) {
323  $this->getOutput()->addHTML(
325  'h2',
326  [ 'class' => [
327  'mw-mediastats-mediatype',
328  'mw-mediastats-mediatype-' . strtolower( $mediaType )
329  ] ],
330  // for grep
331  // mediastatistics-header-unknown, mediastatistics-header-bitmap,
332  // mediastatistics-header-drawing, mediastatistics-header-audio,
333  // mediastatistics-header-video, mediastatistics-header-multimedia,
334  // mediastatistics-header-office, mediastatistics-header-text,
335  // mediastatistics-header-executable, mediastatistics-header-archive,
336  // mediastatistics-header-3d,
337  $this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text()
338  )
339  );
343  }
344 
351  private function splitFakeTitle( $fakeTitle ) {
352  return explode( ';', $fakeTitle, 4 );
353  }
354 
359  protected function getGroupName() {
360  return 'media';
361  }
362 
372  public function formatResult( $skin, $result ) {
373  throw new MWException( "unimplemented" );
374  }
375 
382  public function preprocessResults( $dbr, $res ) {
384  $this->totalCount = $this->totalBytes = 0;
385  foreach ( $res as $row ) {
386  $mediaStats = $this->splitFakeTitle( $row->title );
387  $this->totalCount += $mediaStats[2] ?? 0;
388  $this->totalBytes += $mediaStats[3] ?? 0;
389  }
390  $res->seek( 0 );
391  }
392 }
SpecialMediaStatistics\getGroupName
getGroupName()
What group to put the page in.
Definition: SpecialMediaStatistics.php:359
SpecialPage\$linkRenderer
LinkRenderer null $linkRenderer
Definition: SpecialPage.php:79
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:900
QueryPage\getDBLoadBalancer
getDBLoadBalancer()
Definition: QueryPage.php:881
SpecialMediaStatistics\outputResults
outputResults( $out, $skin, $dbr, $res, $num, $offset)
Output the results of the query.
Definition: SpecialMediaStatistics.php:138
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:788
MimeAnalyzer
Implements functions related to MIME types such as detection and mapping to file extension.
Definition: MimeAnalyzer.php:33
SpecialMediaStatistics\outputTableRow
outputTableRow( $mime, $count, $bytes)
Output a row of the stats table.
Definition: SpecialMediaStatistics.php:197
SpecialMediaStatistics\formatResult
formatResult( $skin, $result)
This method isn't used, since we override outputResults, but we need to implement since abstract in p...
Definition: SpecialMediaStatistics.php:372
SpecialMediaStatistics\isExpensive
isExpensive()
Is this query expensive (for some definition of expensive)? Then we don't let it run in miser mode.
Definition: SpecialMediaStatistics.php:69
SpecialMediaStatistics\getTableHeaderRow
getTableHeaderRow()
Get (not output) the header row for the table.
Definition: SpecialMediaStatistics.php:301
SpecialMediaStatistics\getExtensionList
getExtensionList( $mime)
Given a mime type, return a comma separated list of allowed extensions.
Definition: SpecialMediaStatistics.php:259
SpecialPage\getTitleFor
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,...
Definition: SpecialPage.php:106
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:818
$res
$res
Definition: testCompression.php:57
QueryPage
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:41
QueryPage\executeLBFromResultWrapper
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:855
SpecialMediaStatistics\getQueryInfo
getQueryInfo()
Query to do.
Definition: SpecialMediaStatistics.php:87
QueryPage\$offset
int $offset
The offset and limit in use, as passed to the query() function.
Definition: QueryPage.php:46
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
$dbr
$dbr
Definition: testCompression.php:54
QueryPage\setLinkBatchFactory
setLinkBatchFactory(LinkBatchFactory $linkBatchFactory)
Definition: QueryPage.php:136
Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:318
SpecialMediaStatistics\getOrderFields
getOrderFields()
How to sort the results.
Definition: SpecialMediaStatistics.php:124
MWException
MediaWiki exception.
Definition: MWException.php:29
Wikimedia\Rdbms\IResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: IResultWrapper.php:24
SpecialMediaStatistics\$totalCount
$totalCount
Definition: SpecialMediaStatistics.php:34
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:38
SpecialMediaStatistics\$totalBytes
$totalBytes
Definition: SpecialMediaStatistics.php:34
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
SpecialMediaStatistics\outputTableEnd
outputTableEnd()
Output closing
Definition: SpecialMediaStatistics.php:175
SpecialMediaStatistics\makePercentPretty
makePercentPretty( $decimal)
Definition: SpecialMediaStatistics.php:239
SpecialMediaStatistics\$totalSize
int $totalSize
Combined file size of all files.
Definition: SpecialMediaStatistics.php:44
SpecialMediaStatistics\$totalPerType
int $totalPerType
Combined file size of all files in a section.
Definition: SpecialMediaStatistics.php:39
SpecialMediaStatistics\outputTableStart
outputTableStart( $mediaType)
Output the start of the table.
Definition: SpecialMediaStatistics.php:277
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:51
$header
$header
Definition: updateCredits.php:37
SpecialMediaStatistics\$mimeAnalyzer
MimeAnalyzer $mimeAnalyzer
Definition: SpecialMediaStatistics.php:47
QueryPage\setDBLoadBalancer
setDBLoadBalancer(ILoadBalancer $loadBalancer)
Definition: QueryPage.php:873
SpecialMediaStatistics\preprocessResults
preprocessResults( $dbr, $res)
Initialize total values so we can figure out percentages later.
Definition: SpecialMediaStatistics.php:382
SpecialMediaStatistics
Definition: SpecialMediaStatistics.php:33
QueryPage\$linkBatchFactory
LinkBatchFactory null $linkBatchFactory
Definition: QueryPage.php:74
SpecialMediaStatistics\splitFakeTitle
splitFakeTitle( $fakeTitle)
parse the fake title format that this special page abuses querycache with.
Definition: SpecialMediaStatistics.php:351
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:1016
SpecialMediaStatistics\outputMediaType
outputMediaType( $mediaType)
Output a header for a new media type section.
Definition: SpecialMediaStatistics.php:322
Html\openElement
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:254
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:212
QueryPage\$loadBalancer
ILoadBalancer null $loadBalancer
Definition: QueryPage.php:71
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:234
$mime
$mime
Definition: router.php:60
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
SpecialMediaStatistics\__construct
__construct(MimeAnalyzer $mimeAnalyzer, ILoadBalancer $loadBalancer, LinkBatchFactory $linkBatchFactory)
Definition: SpecialMediaStatistics.php:54