MediaWiki
REL1_24
|
00001 <?php 00028 class MediaStatisticsPage extends QueryPage { 00029 protected $totalCount = 0, $totalBytes = 0; 00030 00031 function __construct( $name = 'MediaStatistics' ) { 00032 parent::__construct( $name ); 00033 // Generally speaking there is only a small number of file types, 00034 // so just show all of them. 00035 $this->limit = 5000; 00036 $this->shownavigation = false; 00037 } 00038 00039 function isExpensive() { 00040 return true; 00041 } 00042 00056 public function getQueryInfo() { 00057 $dbr = wfGetDB( DB_SLAVE ); 00058 $fakeTitle = $dbr->buildConcat( array( 00059 'img_media_type', 00060 $dbr->addQuotes( ';' ), 00061 'img_major_mime', 00062 $dbr->addQuotes( '/' ), 00063 'img_minor_mime', 00064 $dbr->addQuotes( ';' ), 00065 'COUNT(*)', 00066 $dbr->addQuotes( ';' ), 00067 'SUM( img_size )' 00068 ) ); 00069 return array( 00070 'tables' => array( 'image' ), 00071 'fields' => array( 00072 'title' => $fakeTitle, 00073 'namespace' => NS_MEDIA, /* needs to be something */ 00074 'value' => '1' 00075 ), 00076 'options' => array( 00077 'GROUP BY' => array( 00078 'img_media_type', 00079 'img_major_mime', 00080 'img_minor_mime', 00081 ) 00082 ) 00083 ); 00084 } 00085 00093 function getOrderFields() { 00094 return array( 'img_media_type', 'count(*)', 'img_major_mime', 'img_minor_mime' ); 00095 } 00096 00107 protected function outputResults( $out, $skin, $dbr, $res, $num, $offset ) { 00108 $prevMediaType = null; 00109 foreach ( $res as $row ) { 00110 list( $mediaType, $mime, $totalCount, $totalBytes ) = $this->splitFakeTitle( $row->title ); 00111 if ( $prevMediaType !== $mediaType ) { 00112 if ( $prevMediaType !== null ) { 00113 // We're not at beginning, so we have to 00114 // close the previous table. 00115 $this->outputTableEnd(); 00116 } 00117 $this->outputMediaType( $mediaType ); 00118 $this->outputTableStart( $mediaType ); 00119 $prevMediaType = $mediaType; 00120 } 00121 $this->outputTableRow( $mime, intval( $totalCount ), intval( $totalBytes ) ); 00122 } 00123 if ( $prevMediaType !== null ) { 00124 $this->outputTableEnd(); 00125 } 00126 } 00127 00131 protected function outputTableEnd() { 00132 $this->getOutput()->addHtml( Html::closeElement( 'table' ) ); 00133 } 00134 00142 protected function outputTableRow( $mime, $count, $bytes ) { 00143 $mimeSearch = SpecialPage::getTitleFor( 'MIMEsearch', $mime ); 00144 $row = Html::rawElement( 00145 'td', 00146 array(), 00147 Linker::link( $mimeSearch, htmlspecialchars( $mime ) ) 00148 ); 00149 $row .= Html::element( 00150 'td', 00151 array(), 00152 $this->getExtensionList( $mime ) 00153 ); 00154 $row .= Html::rawElement( 00155 'td', 00156 array(), 00157 $this->msg( 'mediastatistics-nfiles' ) 00158 ->numParams( $count ) 00160 ->numParams( $this->makePercentPretty( $count / $this->totalCount ) ) 00161 ->parse() 00162 ); 00163 $row .= Html::rawElement( 00164 'td', 00165 // Make sure js sorts it in numeric order 00166 array( 'data-sort-value' => $bytes ), 00167 $this->msg( 'mediastatistics-nbytes' ) 00168 ->numParams( $bytes ) 00169 ->sizeParams( $bytes ) 00171 ->numParams( $this->makePercentPretty( $bytes / $this->totalBytes ) ) 00172 ->parse() 00173 ); 00174 00175 $this->getOutput()->addHTML( Html::rawElement( 'tr', array(), $row ) ); 00176 } 00177 00182 protected function makePercentPretty( $decimal ) { 00183 $decimal *= 100; 00184 // Always show three useful digits 00185 if ( $decimal == 0 ) { 00186 return '0'; 00187 } 00188 $percent = sprintf( "%." . max( 0, 2 - floor( log10( $decimal ) ) ) . "f", $decimal ); 00189 // Then remove any trailing 0's 00190 return preg_replace( '/\.?0*$/', '', $percent ); 00191 } 00192 00199 private function getExtensionList( $mime ) { 00200 $exts = MimeMagic::singleton()->getExtensionsForType( $mime ); 00201 if ( $exts === null ) { 00202 return ''; 00203 } 00204 $extArray = explode( ' ', $exts ); 00205 $extArray = array_unique( $extArray ); 00206 foreach ( $extArray as &$ext ) { 00207 $ext = '.' . $ext; 00208 } 00209 00210 return $this->getLanguage()->commaList( $extArray ); 00211 } 00212 00218 protected function outputTableStart( $mediaType ) { 00219 $this->getOutput()->addHTML( 00220 Html::openElement( 00221 'table', 00222 array( 'class' => array( 00223 'mw-mediastats-table', 00224 'mw-mediastats-table-' . strtolower( $mediaType ), 00225 'sortable', 00226 'wikitable' 00227 )) 00228 ) 00229 ); 00230 $this->getOutput()->addHTML( $this->getTableHeaderRow() ); 00231 } 00232 00238 protected function getTableHeaderRow() { 00239 $headers = array( 'mimetype', 'extensions', 'count', 'totalbytes' ); 00240 $ths = ''; 00241 foreach ( $headers as $header ) { 00242 $ths .= Html::rawElement( 00243 'th', 00244 array(), 00245 // for grep: 00246 // mediastatistics-table-mimetype, mediastatistics-table-extensions 00247 // tatistics-table-count, mediastatistics-table-totalbytes 00248 $this->msg( 'mediastatistics-table-' . $header )->parse() 00249 ); 00250 } 00251 return Html::rawElement( 'tr', array(), $ths ); 00252 } 00253 00259 protected function outputMediaType( $mediaType ) { 00260 $this->getOutput()->addHTML( 00261 Html::element( 00262 'h2', 00263 array( 'class' => array( 00264 'mw-mediastats-mediatype', 00265 'mw-mediastats-mediatype-' . strtolower( $mediaType ) 00266 )), 00267 // for grep 00268 // mediastatistics-header-unknown, mediastatistics-header-bitmap, 00269 // mediastatistics-header-drawing, mediastatistics-header-audio, 00270 // mediastatistics-header-video, mediastatistics-header-multimedia, 00271 // mediastatistics-header-office, mediastatistics-header-text, 00272 // mediastatistics-header-executable, mediastatistics-header-archive, 00273 $this->msg( 'mediastatistics-header-' . strtolower( $mediaType ) )->text() 00274 ) 00275 ); 00279 } 00280 00287 private function splitFakeTitle( $fakeTitle ) { 00288 return explode( ';', $fakeTitle, 4 ); 00289 } 00290 00295 protected function getGroupName() { 00296 return 'media'; 00297 } 00298 00306 public function formatResult( $skin, $result ) { 00307 throw new MWException( "unimplemented" ); 00308 } 00309 00316 public function preprocessResults( $dbr, $res ) { 00317 $this->totalCount = $this->totalBytes = 0; 00318 foreach ( $res as $row ) { 00319 list( , , $count, $bytes ) = $this->splitFakeTitle( $row->title ); 00320 $this->totalCount += $count; 00321 $this->totalBytes += $bytes; 00322 } 00323 $res->seek( 0 ); 00324 } 00325 }