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