3 * Implements Special:Listfiles
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.
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.
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
21 * @ingroup SpecialPage
24 class SpecialListFiles
extends IncludableSpecialPage
{
25 public function __construct() {
26 parent
::__construct( 'Listfiles' );
29 public function execute( $par ) {
31 $this->outputHeader();
33 if ( $this->including() ) {
38 $userName = $this->getRequest()->getText( 'user', $par );
39 $search = $this->getRequest()->getText( 'ilsearch', '' );
40 $showAll = $this->getRequest()->getBool( 'ilshowall', false );
43 $pager = new ImageListPager(
51 if ( $this->including() ) {
52 $html = $pager->getBody();
54 $form = $pager->getForm();
55 $body = $pager->getBody();
56 $nav = $pager->getNavigationBar();
57 $html = "$form<br />\n$body<br />\n$nav";
59 $this->getOutput()->addHTML( $html );
62 protected function getGroupName() {
68 * @ingroup SpecialPage Pager
70 class ImageListPager
extends TablePager
{
71 protected $mFieldNames = null;
73 // Subclasses should override buildQueryConds instead of using $mQueryConds variable.
74 protected $mQueryConds = array();
76 protected $mUserName = null;
78 protected $mSearch = '';
80 protected $mIncluding = false;
82 protected $mShowAll = false;
84 protected $mTableName = 'image';
86 function __construct( IContextSource
$context, $userName = null, $search = '',
87 $including = false, $showAll = false
91 $this->mIncluding
= $including;
92 $this->mShowAll
= $showAll;
95 $nt = Title
::newFromText( $userName, NS_USER
);
96 if ( !is_null( $nt ) ) {
97 $this->mUserName
= $nt->getText();
101 if ( $search !== '' && !$wgMiserMode ) {
102 $this->mSearch
= $search;
103 $nt = Title
::newFromURL( $this->mSearch
);
106 $dbr = wfGetDB( DB_SLAVE
);
107 $this->mQueryConds
[] = 'LOWER(img_name)' .
108 $dbr->buildLike( $dbr->anyString(),
109 strtolower( $nt->getDBkey() ), $dbr->anyString() );
114 if ( $context->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
115 $this->mDefaultDirection
= true;
117 $this->mDefaultDirection
= false;
120 $this->mDefaultDirection
= true;
123 parent
::__construct( $context );
127 * Build the where clause of the query.
129 * Replaces the older mQueryConds member variable.
130 * @param string $table Either "image" or "oldimage"
131 * @return array The query conditions.
133 protected function buildQueryConds( $table ) {
134 $prefix = $table === 'image' ?
'img' : 'oi';
137 if ( !is_null( $this->mUserName
) ) {
138 $conds[$prefix . '_user_text'] = $this->mUserName
;
141 if ( $this->mSearch
!== '' ) {
142 $nt = Title
::newFromURL( $this->mSearch
);
144 $dbr = wfGetDB( DB_SLAVE
);
145 $conds[] = 'LOWER(' . $prefix . '_name)' .
146 $dbr->buildLike( $dbr->anyString(),
147 strtolower( $nt->getDBkey() ), $dbr->anyString() );
151 if ( $table === 'oldimage' ) {
152 // Don't want to deal with revdel.
153 // Future fixme: Show partial information as appropriate.
154 // Would have to be careful about filtering by username when username is deleted.
155 $conds['oi_deleted'] = 0;
158 // Add mQueryConds in case anyone was subclassing and using the old variable.
159 return $conds +
$this->mQueryConds
;
165 function getFieldNames() {
166 if ( !$this->mFieldNames
) {
168 $this->mFieldNames
= array(
169 'img_timestamp' => $this->msg( 'listfiles_date' )->text(),
170 'img_name' => $this->msg( 'listfiles_name' )->text(),
171 'thumb' => $this->msg( 'listfiles_thumb' )->text(),
172 'img_size' => $this->msg( 'listfiles_size' )->text(),
173 'img_user_text' => $this->msg( 'listfiles_user' )->text(),
174 'img_description' => $this->msg( 'listfiles_description' )->text(),
176 if ( !$wgMiserMode && !$this->mShowAll
) {
177 $this->mFieldNames
['count'] = $this->msg( 'listfiles_count' )->text();
179 if ( $this->mShowAll
) {
180 $this->mFieldNames
['top'] = $this->msg( 'listfiles-latestversion' )->text();
184 return $this->mFieldNames
;
187 function isFieldSortable( $field ) {
189 if ( $this->mIncluding
) {
192 $sortable = array( 'img_timestamp', 'img_name', 'img_size' );
193 /* For reference, the indicies we can use for sorting are:
194 * On the image table: img_usertext_timestamp, img_size, img_timestamp
195 * On oldimage: oi_usertext_timestamp, oi_name_timestamp
197 * In particular that means we cannot sort by timestamp when not filtering
198 * by user and including old images in the results. Which is sad.
200 if ( $wgMiserMode && !is_null( $this->mUserName
) ) {
201 // If we're sorting by user, the index only supports sorting by time.
202 if ( $field === 'img_timestamp' ) {
207 } elseif ( $wgMiserMode && $this->mShowAll
/* && mUserName === null */ ) {
208 // no oi_timestamp index, so only alphabetical sorting in this case.
209 if ( $field === 'img_name' ) {
216 return in_array( $field, $sortable );
219 function getQueryInfo() {
220 // Hacky Hacky Hacky - I want to get query info
221 // for two different tables, without reimplementing
223 $qi = $this->getQueryInfoReal( $this->mTableName
);
229 * Actually get the query info.
231 * This is to allow displaying both stuff from image and oldimage table.
233 * This is a bit hacky.
235 * @param string $table Either 'image' or 'oldimage'
236 * @return array Query info
238 protected function getQueryInfoReal( $table ) {
239 $prefix = $table === 'oldimage' ?
'oi' : 'img';
241 $tables = array( $table );
242 $fields = array_keys( $this->getFieldNames() );
244 if ( $table === 'oldimage' ) {
245 foreach ( $fields as $id => &$field ) {
246 if ( substr( $field, 0, 4 ) !== 'img_' ) {
249 $field = $prefix . substr( $field, 3 ) . ' AS ' . $field;
251 $fields[array_search( 'top', $fields )] = "'no' AS top";
253 if ( $this->mShowAll
) {
254 $fields[array_search( 'top', $fields )] = "'yes' AS top";
257 $fields[] = $prefix . '_user AS img_user';
258 $fields[array_search( 'thumb', $fields )] = $prefix . '_name AS thumb';
260 $options = $join_conds = array();
262 # Depends on $wgMiserMode
263 # Will also not happen if mShowAll is true.
264 if ( isset( $this->mFieldNames
['count'] ) ) {
265 $tables[] = 'oldimage';
267 # Need to rewrite this one
268 foreach ( $fields as &$field ) {
269 if ( $field == 'count' ) {
270 $field = 'COUNT(oi_archive_name) AS count';
275 $dbr = wfGetDB( DB_SLAVE
);
276 if ( $dbr->implicitGroupby() ) {
277 $options = array( 'GROUP BY' => 'img_name' );
279 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
280 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
282 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
288 'conds' => $this->buildQueryConds( $table ),
289 'options' => $options,
290 'join_conds' => $join_conds
295 * Override reallyDoQuery to mix together two queries.
297 * @note $asc is named $descending in IndexPager base class. However
298 * it is true when the order is ascending, and false when the order
299 * is descending, so I renamed it to $asc here.
304 function reallyDoQuery( $offset, $limit, $asc ) {
305 $prevTableName = $this->mTableName
;
306 $this->mTableName
= 'image';
307 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
308 $this->buildQueryInfo( $offset, $limit, $asc );
309 $imageRes = $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
310 $this->mTableName
= $prevTableName;
312 if ( !$this->mShowAll
) {
316 $this->mTableName
= 'oldimage';
319 $oldIndex = $this->mIndexField
;
320 if ( substr( $this->mIndexField
, 0, 4 ) !== 'img_' ) {
321 throw new MWException( "Expected to be sorting on an image table field" );
323 $this->mIndexField
= 'oi_' . substr( $this->mIndexField
, 4 );
325 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
326 $this->buildQueryInfo( $offset, $limit, $asc );
327 $oldimageRes = $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
329 $this->mTableName
= $prevTableName;
330 $this->mIndexField
= $oldIndex;
332 return $this->combineResult( $imageRes, $oldimageRes, $limit, $asc );
336 * Combine results from 2 tables.
338 * Note: This will throw away some results
340 * @param ResultWrapper $res1
341 * @param ResultWrapper $res2
343 * @param bool $ascending See note about $asc in $this->reallyDoQuery
344 * @return FakeResultWrapper $res1 and $res2 combined
346 protected function combineResult( $res1, $res2, $limit, $ascending ) {
349 $topRes1 = $res1->next();
350 $topRes2 = $res2->next();
351 $resultArray = array();
352 for ( $i = 0; $i < $limit && $topRes1 && $topRes2; $i++
) {
353 if ( strcmp( $topRes1->{$this->mIndexField
}, $topRes2->{$this->mIndexField
} ) > 0 ) {
355 $resultArray[] = $topRes1;
356 $topRes1 = $res1->next();
358 $resultArray[] = $topRes2;
359 $topRes2 = $res2->next();
363 $resultArray[] = $topRes2;
364 $topRes2 = $res2->next();
366 $resultArray[] = $topRes1;
367 $topRes1 = $res1->next();
372 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
373 for ( ; $i < $limit && $topRes1; $i++
) {
374 // @codingStandardsIgnoreEnd
375 $resultArray[] = $topRes1;
376 $topRes1 = $res1->next();
379 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
380 for ( ; $i < $limit && $topRes2; $i++
) {
381 // @codingStandardsIgnoreEnd
382 $resultArray[] = $topRes2;
383 $topRes2 = $res2->next();
386 return new FakeResultWrapper( $resultArray );
389 function getDefaultSort() {
391 if ( $this->mShowAll
&& $wgMiserMode && is_null( $this->mUserName
) ) {
392 // Unfortunately no index on oi_timestamp.
395 return 'img_timestamp';
399 function doBatchLookups() {
401 $this->mResult
->seek( 0 );
402 foreach ( $this->mResult
as $row ) {
403 $userIds[] = $row->img_user
;
405 # Do a link batch query for names and userpages
406 UserCache
::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__
);
410 * @param string $field
411 * @param string $value
412 * @return Message|string|int The return type depends on the value of $field:
414 * - img_timestamp: string
416 * - img_user_text: string
418 * - img_description: string
421 * @throws MWException
423 function formatValue( $field, $value ) {
426 $opt = array( 'time' => $this->mCurrentRow
->img_timestamp
);
427 $file = RepoGroup
::singleton()->getLocalRepo()->findFile( $value, $opt );
428 // If statement for paranoia
430 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
432 return $thumb->toHtml( array( 'desc-link' => true ) );
434 return htmlspecialchars( $value );
436 case 'img_timestamp':
437 // We may want to make this a link to the "old" version when displaying old files
438 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
440 static $imgfile = null;
441 if ( $imgfile === null ) {
442 $imgfile = $this->msg( 'imgfile' )->text();
445 // Weird files can maybe exist? Bug 22227
446 $filePage = Title
::makeTitleSafe( NS_FILE
, $value );
448 $link = Linker
::linkKnown(
450 htmlspecialchars( $filePage->getText() )
452 $download = Xml
::element( 'a',
453 array( 'href' => wfLocalFile( $filePage )->getURL() ),
456 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
458 return "$link $download";
460 return htmlspecialchars( $value );
462 case 'img_user_text':
463 if ( $this->mCurrentRow
->img_user
) {
464 $name = User
::whoIs( $this->mCurrentRow
->img_user
);
465 $link = Linker
::link(
466 Title
::makeTitle( NS_USER
, $name ),
467 htmlspecialchars( $name )
470 $link = htmlspecialchars( $value );
475 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
476 case 'img_description':
477 return Linker
::formatComment( $value );
479 return intval( $value ) +
1;
481 // Messages: listfiles-latestversion-yes, listfiles-latestversion-no
482 return $this->msg( 'listfiles-latestversion-' . $value );
484 throw new MWException( "Unknown field '$field'" );
489 global $wgScript, $wgMiserMode;
490 $inputForm = array();
491 $inputForm['table_pager_limit_label'] = $this->getLimitSelect( array( 'tabindex' => 1 ) );
492 if ( !$wgMiserMode ) {
493 $inputForm['listfiles_search_for'] = Html
::input(
499 'maxlength' => '255',
500 'id' => 'mw-ilsearch',
505 $inputForm['username'] = Html
::input( 'user', $this->mUserName
, 'text', array(
507 'maxlength' => '255',
508 'id' => 'mw-listfiles-user',
512 $inputForm['listfiles-show-all'] = Html
::input( 'ilshowall', 1, 'checkbox', array(
513 'checked' => $this->mShowAll
,
517 return Html
::openElement( 'form',
518 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' )
520 Xml
::fieldset( $this->msg( 'listfiles' )->text() ) .
521 Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
522 Xml
::buildForm( $inputForm, 'table_pager_limit_submit', array( 'tabindex' => 5 ) ) .
523 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user', 'title', 'ilshowall' ) ) .
524 Html
::closeElement( 'fieldset' ) .
525 Html
::closeElement( 'form' ) . "\n";
528 function getTableClass() {
529 return 'listfiles ' . parent
::getTableClass();
532 function getNavClass() {
533 return 'listfiles_nav ' . parent
::getNavClass();
536 function getSortHeaderClass() {
537 return 'listfiles_sort ' . parent
::getSortHeaderClass();
540 function getPagingQueries() {
541 $queries = parent
::getPagingQueries();
542 if ( !is_null( $this->mUserName
) ) {
543 # Append the username to the query string
544 foreach ( $queries as &$query ) {
545 $query['user'] = $this->mUserName
;
552 function getDefaultQuery() {
553 $queries = parent
::getDefaultQuery();
554 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName
) ) {
555 $queries['user'] = $this->mUserName
;
561 function getTitle() {
562 return SpecialPage
::getTitleFor( 'Listfiles' );