Merge "Have ?download parameter trigger Content-Disposition: attachment"
[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 $table String 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 $table String 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 */
295 function reallyDoQuery( $offset, $limit, $asc ) {
296 $prevTableName = $this->mTableName;
297 $this->mTableName = 'image';
298 list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo( $offset, $limit, $asc );
299 $imageRes = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
300 $this->mTableName = $prevTableName;
301
302 if ( !$this->mShowAll ) {
303 return $imageRes;
304 }
305
306 $this->mTableName = 'oldimage';
307
308 # Hacky...
309 $oldIndex = $this->mIndexField;
310 if ( substr( $this->mIndexField, 0, 4 ) !== 'img_' ) {
311 throw new MWException( "Expected to be sorting on an image table field" );
312 }
313 $this->mIndexField = 'oi_' . substr( $this->mIndexField, 4 );
314
315 list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo( $offset, $limit, $asc );
316 $oldimageRes = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
317
318 $this->mTableName = $prevTableName;
319 $this->mIndexField = $oldIndex;
320
321 return $this->combineResult( $imageRes, $oldimageRes, $limit, $asc );
322 }
323
324 /**
325 * Combine results from 2 tables.
326 *
327 * Note: This will throw away some results
328 *
329 * @param $res1 ResultWrapper
330 * @param $res2 ResultWrapper
331 * @param $limit int
332 * @param $ascending boolean See note about $asc in $this->reallyDoQuery
333 * @return FakeResultWrapper $res1 and $res2 combined
334 */
335 protected function combineResult( $res1, $res2, $limit, $ascending ) {
336 $res1->rewind();
337 $res2->rewind();
338 $topRes1 = $res1->next();
339 $topRes2 = $res2->next();
340 $resultArray = array();
341 for ( $i = 0; $i < $limit && $topRes1 && $topRes2; $i++ ) {
342 if ( strcmp( $topRes1->{$this->mIndexField}, $topRes2->{$this->mIndexField} ) > 0 ) {
343 if ( !$ascending ) {
344 $resultArray[] = $topRes1;
345 $topRes1 = $res1->next();
346 } else {
347 $resultArray[] = $topRes2;
348 $topRes2 = $res2->next();
349 }
350 } else {
351 if ( !$ascending ) {
352 $resultArray[] = $topRes2;
353 $topRes2 = $res2->next();
354 } else {
355 $resultArray[] = $topRes1;
356 $topRes1 = $res1->next();
357 }
358 }
359 }
360 for ( ; $i < $limit && $topRes1; $i++ ) {
361 $resultArray[] = $topRes1;
362 $topRes1 = $res1->next();
363 }
364 for ( ; $i < $limit && $topRes2; $i++ ) {
365 $resultArray[] = $topRes2;
366 $topRes2 = $res2->next();
367 }
368
369 return new FakeResultWrapper( $resultArray );
370 }
371
372 function getDefaultSort() {
373 global $wgMiserMode;
374 if ( $this->mShowAll && $wgMiserMode && is_null( $this->mUserName ) ) {
375 // Unfortunately no index on oi_timestamp.
376 return 'img_name';
377 } else {
378 return 'img_timestamp';
379 }
380 }
381
382 function doBatchLookups() {
383 $userIds = array();
384 $this->mResult->seek( 0 );
385 foreach ( $this->mResult as $row ) {
386 $userIds[] = $row->img_user;
387 }
388 # Do a link batch query for names and userpages
389 UserCache::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__ );
390 }
391
392 /**
393 * @param string $field
394 * @param string $value
395 * @return Message|string|int The return type depends on the value of $field:
396 * - thumb: string
397 * - img_timestamp: string
398 * - img_name: string
399 * - img_user_text: string
400 * - img_size: string
401 * - img_description: string
402 * - count: int
403 * - top: Message
404 * @throws MWException
405 */
406 function formatValue( $field, $value ) {
407 switch ( $field ) {
408 case 'thumb':
409 $opt = array( 'time' => $this->mCurrentRow->img_timestamp );
410 $file = RepoGroup::singleton()->getLocalRepo()->findFile( $value, $opt );
411 // If statement for paranoia
412 if ( $file ) {
413 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
414
415 return $thumb->toHtml( array( 'desc-link' => true ) );
416 } else {
417 return htmlspecialchars( $value );
418 }
419 case 'img_timestamp':
420 // We may want to make this a link to the "old" version when displaying old files
421 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
422 case 'img_name':
423 static $imgfile = null;
424 if ( $imgfile === null ) {
425 $imgfile = $this->msg( 'imgfile' )->text();
426 }
427
428 // Weird files can maybe exist? Bug 22227
429 $filePage = Title::makeTitleSafe( NS_FILE, $value );
430 if ( $filePage ) {
431 $link = Linker::linkKnown(
432 $filePage,
433 htmlspecialchars( $filePage->getText() )
434 );
435 $download = Xml::element( 'a',
436 array( 'href' => wfLocalFile( $filePage )->getURL() ),
437 $imgfile
438 );
439 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
440
441 return "$link $download";
442 } else {
443 return htmlspecialchars( $value );
444 }
445 case 'img_user_text':
446 if ( $this->mCurrentRow->img_user ) {
447 $name = User::whoIs( $this->mCurrentRow->img_user );
448 $link = Linker::link(
449 Title::makeTitle( NS_USER, $name ),
450 htmlspecialchars( $name )
451 );
452 } else {
453 $link = htmlspecialchars( $value );
454 }
455
456 return $link;
457 case 'img_size':
458 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
459 case 'img_description':
460 return Linker::formatComment( $value );
461 case 'count':
462 return intval( $value ) + 1;
463 case 'top':
464 // Messages: listfiles-latestversion-yes, listfiles-latestversion-no
465 return $this->msg( 'listfiles-latestversion-' . $value );
466 default:
467 throw new MWException( "Unknown field '$field'" );
468 }
469 }
470
471 function getForm() {
472 global $wgScript, $wgMiserMode;
473 $inputForm = array();
474 $inputForm['table_pager_limit_label'] = $this->getLimitSelect( array( 'tabindex' => 1 ) );
475 if ( !$wgMiserMode ) {
476 $inputForm['listfiles_search_for'] = Html::input(
477 'ilsearch',
478 $this->mSearch,
479 'text',
480 array(
481 'size' => '40',
482 'maxlength' => '255',
483 'id' => 'mw-ilsearch',
484 'tabindex' => 2,
485 )
486 );
487 }
488 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
489 'size' => '40',
490 'maxlength' => '255',
491 'id' => 'mw-listfiles-user',
492 'tabindex' => 3,
493 ) );
494
495 $inputForm['listfiles-show-all'] = Html::input( 'ilshowall', 1, 'checkbox', array(
496 'checked' => $this->mShowAll,
497 'tabindex' => 4,
498 ) );
499
500 return Html::openElement( 'form',
501 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' )
502 ) .
503 Xml::fieldset( $this->msg( 'listfiles' )->text() ) .
504 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
505 Xml::buildForm( $inputForm, 'table_pager_limit_submit', array( 'tabindex' => 5 ) ) .
506 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user', 'title', 'ilshowall' ) ) .
507 Html::closeElement( 'fieldset' ) .
508 Html::closeElement( 'form' ) . "\n";
509 }
510
511 function getTableClass() {
512 return 'listfiles ' . parent::getTableClass();
513 }
514
515 function getNavClass() {
516 return 'listfiles_nav ' . parent::getNavClass();
517 }
518
519 function getSortHeaderClass() {
520 return 'listfiles_sort ' . parent::getSortHeaderClass();
521 }
522
523 function getPagingQueries() {
524 $queries = parent::getPagingQueries();
525 if ( !is_null( $this->mUserName ) ) {
526 # Append the username to the query string
527 foreach ( $queries as &$query ) {
528 $query['user'] = $this->mUserName;
529 }
530 }
531
532 return $queries;
533 }
534
535 function getDefaultQuery() {
536 $queries = parent::getDefaultQuery();
537 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName ) ) {
538 $queries['user'] = $this->mUserName;
539 }
540
541 return $queries;
542 }
543
544 function getTitle() {
545 return SpecialPage::getTitleFor( 'Listfiles' );
546 }
547 }