MediaWiki  master
SpecialMediaStatistics.php
Go to the documentation of this file.
1 <?php
27 
32  protected $totalCount = 0, $totalBytes = 0;
33 
37  protected $totalPerType = 0;
38 
42  protected $totalSize = 0;
43 
44  public function __construct( $name = 'MediaStatistics' ) {
45  parent::__construct( $name );
46  // Generally speaking there is only a small number of file types,
47  // so just show all of them.
48  $this->limit = 5000;
49  $this->shownavigation = false;
50  }
51 
52  public function isExpensive() {
53  return true;
54  }
55 
70  public function getQueryInfo() {
71  $dbr = wfGetDB( DB_REPLICA );
72  $fakeTitle = $dbr->buildConcat( [
73  'img_media_type',
74  $dbr->addQuotes( ';' ),
75  'img_major_mime',
76  $dbr->addQuotes( '/' ),
77  'img_minor_mime',
78  $dbr->addQuotes( ';' ),
79  $dbr->buildStringCast( 'COUNT(*)' ),
80  $dbr->addQuotes( ';' ),
81  $dbr->buildStringCast( 'SUM( img_size )' )
82  ] );
83  return [
84  'tables' => [ 'image' ],
85  'fields' => [
86  'title' => $fakeTitle,
87  'namespace' => NS_MEDIA, /* needs to be something */
88  'value' => '1'
89  ],
90  'options' => [
91  'GROUP BY' => [
92  'img_media_type',
93  'img_major_mime',
94  'img_minor_mime',
95  ]
96  ]
97  ];
98  }
99 
107  protected function getOrderFields() {
108  return [ 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' ];
109  }
110 
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()->addWikiTextAsInterface(
147  $this->msg( 'mediastatistics-allbytes' )
148  ->numParams( $this->totalSize )
149  ->sizeParams( $this->totalSize )
150  ->text()
151  );
152  }
153  }
154 
158  protected function outputTableEnd() {
159  $this->getOutput()->addHTML(
160  Html::closeElement( 'tbody' ) .
161  Html::closeElement( 'table' )
162  );
163  $this->getOutput()->addWikiTextAsInterface(
164  $this->msg( 'mediastatistics-bytespertype' )
165  ->numParams( $this->totalPerType )
166  ->sizeParams( $this->totalPerType )
167  ->numParams( $this->makePercentPretty( $this->totalPerType / $this->totalBytes ) )
168  ->text()
169  );
170  $this->totalSize += $this->totalPerType;
171  }
172 
180  protected function outputTableRow( $mime, $count, $bytes ) {
181  $mimeSearch = SpecialPage::getTitleFor( 'MIMEsearch', $mime );
182  $linkRenderer = $this->getLinkRenderer();
183  $row = Html::rawElement(
184  'td',
185  [],
186  $linkRenderer->makeLink( $mimeSearch, $mime )
187  );
188  $row .= Html::rawElement(
189  'td',
190  [],
191  $this->getExtensionList( $mime )
192  );
193  $row .= Html::rawElement(
194  'td',
195  // Make sure js sorts it in numeric order
196  [ 'data-sort-value' => $count ],
197  $this->msg( 'mediastatistics-nfiles' )
198  ->numParams( $count )
200  ->numParams( $this->makePercentPretty( $count / $this->totalCount ) )
201  ->parse()
202  );
203  $row .= Html::rawElement(
204  'td',
205  // Make sure js sorts it in numeric order
206  [ 'data-sort-value' => $bytes ],
207  $this->msg( 'mediastatistics-nbytes' )
208  ->numParams( $bytes )
209  ->sizeParams( $bytes )
211  ->numParams( $this->makePercentPretty( $bytes / $this->totalBytes ) )
212  ->parse()
213  );
214  $this->totalPerType += $bytes;
215  $this->getOutput()->addHTML( Html::rawElement( 'tr', [], $row ) );
216  }
217 
222  protected function makePercentPretty( $decimal ) {
223  $decimal *= 100;
224  // Always show three useful digits
225  if ( $decimal == 0 ) {
226  return '0';
227  }
228  if ( $decimal >= 100 ) {
229  return '100';
230  }
231  $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal );
232  // Then remove any trailing 0's
233  return preg_replace( '/\.?0*$/', '', $percent );
234  }
235 
242  private function getExtensionList( $mime ) {
243  $exts = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer()
244  ->getExtensionsFromMimeType( $mime );
245  if ( !$exts ) {
246  return '';
247  }
248  foreach ( $exts as &$ext ) {
249  $ext = htmlspecialchars( '.' . $ext );
250  }
251 
252  return $this->getLanguage()->commaList( $exts );
253  }
254 
261  protected function outputTableStart( $mediaType ) {
262  $out = $this->getOutput();
263  $out->addModuleStyles( 'jquery.tablesorter.styles' );
264  $out->addModules( 'jquery.tablesorter' );
265  $out->addHTML(
267  'table',
268  [ 'class' => [
269  'mw-mediastats-table',
270  'mw-mediastats-table-' . strtolower( $mediaType ),
271  'sortable',
272  'wikitable'
273  ] ]
274  ) .
275  Html::rawElement( 'thead', [], $this->getTableHeaderRow() ) .
276  Html::openElement( 'tbody' )
277  );
278  }
279 
285  protected function getTableHeaderRow() {
286  $headers = [ 'mimetype', 'extensions', 'count', 'totalbytes' ];
287  $ths = '';
288  foreach ( $headers as $header ) {
289  $ths .= Html::rawElement(
290  'th',
291  [],
292  // for grep:
293  // mediastatistics-table-mimetype, mediastatistics-table-extensions
294  // tatistics-table-count, mediastatistics-table-totalbytes
295  $this->msg( 'mediastatistics-table-' . $header )->parse()
296  );
297  }
298  return Html::rawElement( 'tr', [], $ths );
299  }
300 
306  protected function outputMediaType( $mediaType ) {
307  $this->getOutput()->addHTML(
309  'h2',
310  [ 'class' => [
311  'mw-mediastats-mediatype',
312  'mw-mediastats-mediatype-' . strtolower( $mediaType )
313  ] ],
314  // for grep
315  // mediastatistics-header-unknown, mediastatistics-header-bitmap,
316  // mediastatistics-header-drawing, mediastatistics-header-audio,
317  // mediastatistics-header-video, mediastatistics-header-multimedia,
318  // mediastatistics-header-office, mediastatistics-header-text,
319  // mediastatistics-header-executable, mediastatistics-header-archive,
320  // mediastatistics-header-3d,
321  $this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text()
322  )
323  );
327  }
328 
335  private function splitFakeTitle( $fakeTitle ) {
336  return explode( ';', $fakeTitle, 4 );
337  }
338 
343  protected function getGroupName() {
344  return 'media';
345  }
346 
356  public function formatResult( $skin, $result ) {
357  throw new MWException( "unimplemented" );
358  }
359 
366  public function preprocessResults( $dbr, $res ) {
368  $this->totalCount = $this->totalBytes = 0;
369  foreach ( $res as $row ) {
370  $mediaStats = $this->splitFakeTitle( $row->title );
371  $this->totalCount += $mediaStats[2] ?? 0;
372  $this->totalBytes += $mediaStats[3] ?? 0;
373  }
374  $res->seek( 0 );
375  }
376 }
SpecialMediaStatistics\getGroupName
getGroupName()
What group to put the page in.
Definition: SpecialMediaStatistics.php:343
SpecialPage\msg
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
Definition: SpecialPage.php:828
SpecialMediaStatistics\outputResults
outputResults( $out, $skin, $dbr, $res, $num, $offset)
Output the results of the query.
Definition: SpecialMediaStatistics.php:121
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:744
SpecialMediaStatistics\outputTableRow
outputTableRow( $mime, $count, $bytes)
Output a row of the stats table.
Definition: SpecialMediaStatistics.php:180
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:356
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:52
SpecialMediaStatistics\getTableHeaderRow
getTableHeaderRow()
Get (not output) the header row for the table.
Definition: SpecialMediaStatistics.php:285
SpecialMediaStatistics\getExtensionList
getExtensionList( $mime)
Given a mime type, return a comma separated list of allowed extensions.
Definition: SpecialMediaStatistics.php:242
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:92
SpecialPage\getLanguage
getLanguage()
Shortcut to get user's language.
Definition: SpecialPage.php:774
$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:39
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:823
SpecialMediaStatistics\getQueryInfo
getQueryInfo()
Query to do.
Definition: SpecialMediaStatistics.php:70
QueryPage\$offset
int $offset
The offset and limit in use, as passed to the query() function.
Definition: QueryPage.php:44
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
$dbr
$dbr
Definition: testCompression.php:54
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:185
Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:315
SpecialMediaStatistics\getOrderFields
getOrderFields()
How to sort the results.
Definition: SpecialMediaStatistics.php:107
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:32
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2467
SpecialMediaStatistics\$totalBytes
$totalBytes
Definition: SpecialMediaStatistics.php:32
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
SpecialMediaStatistics\outputTableEnd
outputTableEnd()
Output closing
Definition: SpecialMediaStatistics.php:158
SpecialMediaStatistics\makePercentPretty
makePercentPretty( $decimal)
Definition: SpecialMediaStatistics.php:222
SpecialMediaStatistics\$totalSize
int $totalSize
Combined file size of all files.
Definition: SpecialMediaStatistics.php:42
SpecialMediaStatistics\$totalPerType
int $totalPerType
Combined file size of all files in a section.
Definition: SpecialMediaStatistics.php:37
SpecialMediaStatistics\__construct
__construct( $name='MediaStatistics')
Definition: SpecialMediaStatistics.php:44
SpecialMediaStatistics\outputTableStart
outputTableStart( $mediaType)
Output the start of the table.
Definition: SpecialMediaStatistics.php:261
NS_MEDIA
const NS_MEDIA
Definition: Defines.php:57
$header
$header
Definition: updateCredits.php:41
SpecialMediaStatistics\preprocessResults
preprocessResults( $dbr, $res)
Initialize total values so we can figure out percentages later.
Definition: SpecialMediaStatistics.php:366
SpecialMediaStatistics
Definition: SpecialMediaStatistics.php:31
SpecialMediaStatistics\splitFakeTitle
splitFakeTitle( $fakeTitle)
parse the fake title format that this special page abuses querycache with.
Definition: SpecialMediaStatistics.php:335
SpecialPage\getLinkRenderer
getLinkRenderer()
Definition: SpecialPage.php:944
SpecialMediaStatistics\outputMediaType
outputMediaType( $mediaType)
Output a header for a new media type section.
Definition: SpecialMediaStatistics.php:306
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:251
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
SpecialPage\$linkRenderer
MediaWiki Linker LinkRenderer null $linkRenderer
Definition: SpecialPage.php:71
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
$mime
$mime
Definition: router.php:60