Merge "Support tighter rate limiting for "non-standard" thumbnails"
[lhc/web/wiklou.git] / includes / specials / SpecialListfiles.php
1 <?php
2 /**
3 * Implements Special:Listfiles
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 */
23
24 class SpecialListFiles extends IncludableSpecialPage {
25 public function __construct() {
26 parent::__construct( 'Listfiles' );
27 }
28
29 public function execute( $par ) {
30 $this->setHeaders();
31 $this->outputHeader();
32
33 if ( $this->including() ) {
34 $userName = $par;
35 $search = '';
36 $showAll = false;
37 } else {
38 $userName = $this->getRequest()->getText( 'user', $par );
39 $search = $this->getRequest()->getText( 'ilsearch', '' );
40 $showAll = $this->getRequest()->getBool( 'ilshowall', false );
41 }
42
43 $pager = new ImageListPager(
44 $this->getContext(),
45 $userName,
46 $search,
47 $this->including(),
48 $showAll
49 );
50
51 if ( $this->including() ) {
52 $html = $pager->getBody();
53 } else {
54 $form = $pager->getForm();
55 $body = $pager->getBody();
56 $nav = $pager->getNavigationBar();
57 $html = "$form<br />\n$body<br />\n$nav";
58 }
59 $this->getOutput()->addHTML( $html );
60 }
61
62 protected function getGroupName() {
63 return 'media';
64 }
65 }
66
67 /**
68 * @ingroup SpecialPage Pager
69 */
70 class ImageListPager extends TablePager {
71 var $mFieldNames = null;
72 // Subclasses should override buildQueryConds instead of using $mQueryConds variable.
73 var $mQueryConds = array();
74 var $mUserName = null;
75 var $mSearch = '';
76 var $mIncluding = false;
77 var $mShowAll = false;
78 var $mTableName = 'image';
79
80 function __construct( IContextSource $context, $userName = null, $search = '',
81 $including = false, $showAll = false
82 ) {
83 global $wgMiserMode;
84
85 $this->mIncluding = $including;
86 $this->mShowAll = $showAll;
87
88 if ( $userName ) {
89 $nt = Title::newFromText( $userName, NS_USER );
90 if ( !is_null( $nt ) ) {
91 $this->mUserName = $nt->getText();
92 }
93 }
94
95 if ( $search !== '' && !$wgMiserMode ) {
96 $this->mSearch = $search;
97 $nt = Title::newFromURL( $this->mSearch );
98
99 if ( $nt ) {
100 $dbr = wfGetDB( DB_SLAVE );
101 $this->mQueryConds[] = 'LOWER(img_name)' .
102 $dbr->buildLike( $dbr->anyString(),
103 strtolower( $nt->getDBkey() ), $dbr->anyString() );
104 }
105 }
106
107 if ( !$including ) {
108 if ( $context->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
109 $this->mDefaultDirection = true;
110 } else {
111 $this->mDefaultDirection = false;
112 }
113 } else {
114 $this->mDefaultDirection = true;
115 }
116
117 parent::__construct( $context );
118 }
119
120 /**
121 * Build the where clause of the query.
122 *
123 * Replaces the older mQueryConds member variable.
124 * @param string $table Either "image" or "oldimage"
125 * @return array The query conditions.
126 */
127 protected function buildQueryConds( $table ) {
128 $prefix = $table === 'image' ? 'img' : 'oi';
129 $conds = array();
130
131 if ( !is_null( $this->mUserName ) ) {
132 $conds[$prefix . '_user_text'] = $this->mUserName;
133 }
134
135 if ( $this->mSearch !== '' ) {
136 $nt = Title::newFromURL( $this->mSearch );
137 if ( $nt ) {
138 $dbr = wfGetDB( DB_SLAVE );
139 $conds[] = 'LOWER(' . $prefix . '_name)' .
140 $dbr->buildLike( $dbr->anyString(),
141 strtolower( $nt->getDBkey() ), $dbr->anyString() );
142 }
143 }
144
145 if ( $table === 'oldimage' ) {
146 // Don't want to deal with revdel.
147 // Future fixme: Show partial information as appropriate.
148 // Would have to be careful about filtering by username when username is deleted.
149 $conds['oi_deleted'] = 0;
150 }
151
152 // Add mQueryConds in case anyone was subclassing and using the old variable.
153 return $conds + $this->mQueryConds;
154 }
155
156 /**
157 * @return array
158 */
159 function getFieldNames() {
160 if ( !$this->mFieldNames ) {
161 global $wgMiserMode;
162 $this->mFieldNames = array(
163 'img_timestamp' => $this->msg( 'listfiles_date' )->text(),
164 'img_name' => $this->msg( 'listfiles_name' )->text(),
165 'thumb' => $this->msg( 'listfiles_thumb' )->text(),
166 'img_size' => $this->msg( 'listfiles_size' )->text(),
167 'img_user_text' => $this->msg( 'listfiles_user' )->text(),
168 'img_description' => $this->msg( 'listfiles_description' )->text(),
169 );
170 if ( !$wgMiserMode && !$this->mShowAll ) {
171 $this->mFieldNames['count'] = $this->msg( 'listfiles_count' )->text();
172 }
173 if ( $this->mShowAll ) {
174 $this->mFieldNames['top'] = $this->msg( 'listfiles-latestversion' )->text();
175 }
176 }
177
178 return $this->mFieldNames;
179 }
180
181 function isFieldSortable( $field ) {
182 global $wgMiserMode;
183 if ( $this->mIncluding ) {
184 return false;
185 }
186 $sortable = array( 'img_timestamp', 'img_name', 'img_size' );
187 /* For reference, the indicies we can use for sorting are:
188 * On the image table: img_usertext_timestamp, img_size, img_timestamp
189 * On oldimage: oi_usertext_timestamp, oi_name_timestamp
190 *
191 * In particular that means we cannot sort by timestamp when not filtering
192 * by user and including old images in the results. Which is sad.
193 */
194 if ( $wgMiserMode && !is_null( $this->mUserName ) ) {
195 // If we're sorting by user, the index only supports sorting by time.
196 if ( $field === 'img_timestamp' ) {
197 return true;
198 } else {
199 return false;
200 }
201 } elseif ( $wgMiserMode && $this->mShowAll /* && mUserName === null */ ) {
202 // no oi_timestamp index, so only alphabetical sorting in this case.
203 if ( $field === 'img_name' ) {
204 return true;
205 } else {
206 return false;
207 }
208 }
209
210 return in_array( $field, $sortable );
211 }
212
213 function getQueryInfo() {
214 // Hacky Hacky Hacky - I want to get query info
215 // for two different tables, without reimplementing
216 // the pager class.
217 $qi = $this->getQueryInfoReal( $this->mTableName );
218
219 return $qi;
220 }
221
222 /**
223 * Actually get the query info.
224 *
225 * This is to allow displaying both stuff from image and oldimage table.
226 *
227 * This is a bit hacky.
228 *
229 * @param string $table Either 'image' or 'oldimage'
230 * @return array Query info
231 */
232 protected function getQueryInfoReal( $table ) {
233 $prefix = $table === 'oldimage' ? 'oi' : 'img';
234
235 $tables = array( $table );
236 $fields = array_keys( $this->getFieldNames() );
237
238 if ( $table === 'oldimage' ) {
239 foreach ( $fields as $id => &$field ) {
240 if ( substr( $field, 0, 4 ) !== 'img_' ) {
241 continue;
242 }
243 $field = $prefix . substr( $field, 3 ) . ' AS ' . $field;
244 }
245 $fields[array_search( 'top', $fields )] = "'no' AS top";
246 } else {
247 if ( $this->mShowAll ) {
248 $fields[array_search( 'top', $fields )] = "'yes' AS top";
249 }
250 }
251 $fields[] = $prefix . '_user AS img_user';
252 $fields[array_search( 'thumb', $fields )] = $prefix . '_name AS thumb';
253
254 $options = $join_conds = array();
255
256 # Depends on $wgMiserMode
257 # Will also not happen if mShowAll is true.
258 if ( isset( $this->mFieldNames['count'] ) ) {
259 $tables[] = 'oldimage';
260
261 # Need to rewrite this one
262 foreach ( $fields as &$field ) {
263 if ( $field == 'count' ) {
264 $field = 'COUNT(oi_archive_name) AS count';
265 }
266 }
267 unset( $field );
268
269 $dbr = wfGetDB( DB_SLAVE );
270 if ( $dbr->implicitGroupby() ) {
271 $options = array( 'GROUP BY' => 'img_name' );
272 } else {
273 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
274 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
275 }
276 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
277 }
278
279 return array(
280 'tables' => $tables,
281 'fields' => $fields,
282 'conds' => $this->buildQueryConds( $table ),
283 'options' => $options,
284 'join_conds' => $join_conds
285 );
286 }
287
288 /**
289 * Override reallyDoQuery to mix together two queries.
290 *
291 * @note $asc is named $descending in IndexPager base class. However
292 * it is true when the order is ascending, and false when the order
293 * is descending, so I renamed it to $asc here.
294 * @param int $offset
295 * @param int $limit
296 * @param bool $asc
297 */
298 function reallyDoQuery( $offset, $limit, $asc ) {
299 $prevTableName = $this->mTableName;
300 $this->mTableName = 'image';
301 list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo( $offset, $limit, $asc );
302 $imageRes = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
303 $this->mTableName = $prevTableName;
304
305 if ( !$this->mShowAll ) {
306 return $imageRes;
307 }
308
309 $this->mTableName = 'oldimage';
310
311 # Hacky...
312 $oldIndex = $this->mIndexField;
313 if ( substr( $this->mIndexField, 0, 4 ) !== 'img_' ) {
314 throw new MWException( "Expected to be sorting on an image table field" );
315 }
316 $this->mIndexField = 'oi_' . substr( $this->mIndexField, 4 );
317
318 list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo( $offset, $limit, $asc );
319 $oldimageRes = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
320
321 $this->mTableName = $prevTableName;
322 $this->mIndexField = $oldIndex;
323
324 return $this->combineResult( $imageRes, $oldimageRes, $limit, $asc );
325 }
326
327 /**
328 * Combine results from 2 tables.
329 *
330 * Note: This will throw away some results
331 *
332 * @param ResultWrapper $res1
333 * @param ResultWrapper $res2
334 * @param int $limit
335 * @param bool $ascending See note about $asc in $this->reallyDoQuery
336 * @return FakeResultWrapper $res1 and $res2 combined
337 */
338 protected function combineResult( $res1, $res2, $limit, $ascending ) {
339 $res1->rewind();
340 $res2->rewind();
341 $topRes1 = $res1->next();
342 $topRes2 = $res2->next();
343 $resultArray = array();
344 for ( $i = 0; $i < $limit && $topRes1 && $topRes2; $i++ ) {
345 if ( strcmp( $topRes1->{$this->mIndexField}, $topRes2->{$this->mIndexField} ) > 0 ) {
346 if ( !$ascending ) {
347 $resultArray[] = $topRes1;
348 $topRes1 = $res1->next();
349 } else {
350 $resultArray[] = $topRes2;
351 $topRes2 = $res2->next();
352 }
353 } else {
354 if ( !$ascending ) {
355 $resultArray[] = $topRes2;
356 $topRes2 = $res2->next();
357 } else {
358 $resultArray[] = $topRes1;
359 $topRes1 = $res1->next();
360 }
361 }
362 }
363 for ( ; $i < $limit && $topRes1; $i++ ) {
364 $resultArray[] = $topRes1;
365 $topRes1 = $res1->next();
366 }
367 for ( ; $i < $limit && $topRes2; $i++ ) {
368 $resultArray[] = $topRes2;
369 $topRes2 = $res2->next();
370 }
371
372 return new FakeResultWrapper( $resultArray );
373 }
374
375 function getDefaultSort() {
376 global $wgMiserMode;
377 if ( $this->mShowAll && $wgMiserMode && is_null( $this->mUserName ) ) {
378 // Unfortunately no index on oi_timestamp.
379 return 'img_name';
380 } else {
381 return 'img_timestamp';
382 }
383 }
384
385 function doBatchLookups() {
386 $userIds = array();
387 $this->mResult->seek( 0 );
388 foreach ( $this->mResult as $row ) {
389 $userIds[] = $row->img_user;
390 }
391 # Do a link batch query for names and userpages
392 UserCache::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__ );
393 }
394
395 /**
396 * @param string $field
397 * @param string $value
398 * @return Message|string|int The return type depends on the value of $field:
399 * - thumb: string
400 * - img_timestamp: string
401 * - img_name: string
402 * - img_user_text: string
403 * - img_size: string
404 * - img_description: string
405 * - count: int
406 * - top: Message
407 * @throws MWException
408 */
409 function formatValue( $field, $value ) {
410 switch ( $field ) {
411 case 'thumb':
412 $opt = array( 'time' => $this->mCurrentRow->img_timestamp );
413 $file = RepoGroup::singleton()->getLocalRepo()->findFile( $value, $opt );
414 // If statement for paranoia
415 if ( $file ) {
416 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
417
418 return $thumb->toHtml( array( 'desc-link' => true ) );
419 } else {
420 return htmlspecialchars( $value );
421 }
422 case 'img_timestamp':
423 // We may want to make this a link to the "old" version when displaying old files
424 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
425 case 'img_name':
426 static $imgfile = null;
427 if ( $imgfile === null ) {
428 $imgfile = $this->msg( 'imgfile' )->text();
429 }
430
431 // Weird files can maybe exist? Bug 22227
432 $filePage = Title::makeTitleSafe( NS_FILE, $value );
433 if ( $filePage ) {
434 $link = Linker::linkKnown(
435 $filePage,
436 htmlspecialchars( $filePage->getText() )
437 );
438 $download = Xml::element( 'a',
439 array( 'href' => wfLocalFile( $filePage )->getURL() ),
440 $imgfile
441 );
442 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
443
444 return "$link $download";
445 } else {
446 return htmlspecialchars( $value );
447 }
448 case 'img_user_text':
449 if ( $this->mCurrentRow->img_user ) {
450 $name = User::whoIs( $this->mCurrentRow->img_user );
451 $link = Linker::link(
452 Title::makeTitle( NS_USER, $name ),
453 htmlspecialchars( $name )
454 );
455 } else {
456 $link = htmlspecialchars( $value );
457 }
458
459 return $link;
460 case 'img_size':
461 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
462 case 'img_description':
463 return Linker::formatComment( $value );
464 case 'count':
465 return intval( $value ) + 1;
466 case 'top':
467 // Messages: listfiles-latestversion-yes, listfiles-latestversion-no
468 return $this->msg( 'listfiles-latestversion-' . $value );
469 default:
470 throw new MWException( "Unknown field '$field'" );
471 }
472 }
473
474 function getForm() {
475 global $wgScript, $wgMiserMode;
476 $inputForm = array();
477 $inputForm['table_pager_limit_label'] = $this->getLimitSelect( array( 'tabindex' => 1 ) );
478 if ( !$wgMiserMode ) {
479 $inputForm['listfiles_search_for'] = Html::input(
480 'ilsearch',
481 $this->mSearch,
482 'text',
483 array(
484 'size' => '40',
485 'maxlength' => '255',
486 'id' => 'mw-ilsearch',
487 'tabindex' => 2,
488 )
489 );
490 }
491 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
492 'size' => '40',
493 'maxlength' => '255',
494 'id' => 'mw-listfiles-user',
495 'tabindex' => 3,
496 ) );
497
498 $inputForm['listfiles-show-all'] = Html::input( 'ilshowall', 1, 'checkbox', array(
499 'checked' => $this->mShowAll,
500 'tabindex' => 4,
501 ) );
502
503 return Html::openElement( 'form',
504 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' )
505 ) .
506 Xml::fieldset( $this->msg( 'listfiles' )->text() ) .
507 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
508 Xml::buildForm( $inputForm, 'table_pager_limit_submit', array( 'tabindex' => 5 ) ) .
509 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user', 'title', 'ilshowall' ) ) .
510 Html::closeElement( 'fieldset' ) .
511 Html::closeElement( 'form' ) . "\n";
512 }
513
514 function getTableClass() {
515 return 'listfiles ' . parent::getTableClass();
516 }
517
518 function getNavClass() {
519 return 'listfiles_nav ' . parent::getNavClass();
520 }
521
522 function getSortHeaderClass() {
523 return 'listfiles_sort ' . parent::getSortHeaderClass();
524 }
525
526 function getPagingQueries() {
527 $queries = parent::getPagingQueries();
528 if ( !is_null( $this->mUserName ) ) {
529 # Append the username to the query string
530 foreach ( $queries as &$query ) {
531 $query['user'] = $this->mUserName;
532 }
533 }
534
535 return $queries;
536 }
537
538 function getDefaultQuery() {
539 $queries = parent::getDefaultQuery();
540 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName ) ) {
541 $queries['user'] = $this->mUserName;
542 }
543
544 return $queries;
545 }
546
547 function getTitle() {
548 return SpecialPage::getTitleFor( 'Listfiles' );
549 }
550 }