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
89 $this->mIncluding
= $including;
90 $this->mShowAll
= $showAll;
93 $nt = Title
::newFromText( $userName, NS_USER
);
94 if ( !is_null( $nt ) ) {
95 $this->mUserName
= $nt->getText();
99 if ( $search !== '' && !$this->getConfig()->get( 'MiserMode' ) ) {
100 $this->mSearch
= $search;
101 $nt = Title
::newFromURL( $this->mSearch
);
104 $dbr = wfGetDB( DB_SLAVE
);
105 $this->mQueryConds
[] = 'LOWER(img_name)' .
106 $dbr->buildLike( $dbr->anyString(),
107 strtolower( $nt->getDBkey() ), $dbr->anyString() );
112 if ( $context->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
113 $this->mDefaultDirection
= true;
115 $this->mDefaultDirection
= false;
118 $this->mDefaultDirection
= true;
121 parent
::__construct( $context );
125 * Build the where clause of the query.
127 * Replaces the older mQueryConds member variable.
128 * @param string $table Either "image" or "oldimage"
129 * @return array The query conditions.
131 protected function buildQueryConds( $table ) {
132 $prefix = $table === 'image' ?
'img' : 'oi';
135 if ( !is_null( $this->mUserName
) ) {
136 $conds[$prefix . '_user_text'] = $this->mUserName
;
139 if ( $this->mSearch
!== '' ) {
140 $nt = Title
::newFromURL( $this->mSearch
);
142 $dbr = wfGetDB( DB_SLAVE
);
143 $conds[] = 'LOWER(' . $prefix . '_name)' .
144 $dbr->buildLike( $dbr->anyString(),
145 strtolower( $nt->getDBkey() ), $dbr->anyString() );
149 if ( $table === 'oldimage' ) {
150 // Don't want to deal with revdel.
151 // Future fixme: Show partial information as appropriate.
152 // Would have to be careful about filtering by username when username is deleted.
153 $conds['oi_deleted'] = 0;
156 // Add mQueryConds in case anyone was subclassing and using the old variable.
157 return $conds +
$this->mQueryConds
;
163 function getFieldNames() {
164 if ( !$this->mFieldNames
) {
165 $this->mFieldNames
= array(
166 'img_timestamp' => $this->msg( 'listfiles_date' )->text(),
167 'img_name' => $this->msg( 'listfiles_name' )->text(),
168 'thumb' => $this->msg( 'listfiles_thumb' )->text(),
169 'img_size' => $this->msg( 'listfiles_size' )->text(),
171 if ( is_null( $this->mUserName
) ) {
172 // Do not show username if filtering by username
173 $this->mFieldNames
['img_user_text'] = $this->msg( 'listfiles_user' )->text();
175 // img_description down here, in order so that its still after the username field.
176 $this->mFieldNames
['img_description'] = $this->msg( 'listfiles_description' )->text();
178 if ( !$this->getConfig()->get( 'MiserMode' ) && !$this->mShowAll
) {
179 $this->mFieldNames
['count'] = $this->msg( 'listfiles_count' )->text();
181 if ( $this->mShowAll
) {
182 $this->mFieldNames
['top'] = $this->msg( 'listfiles-latestversion' )->text();
186 return $this->mFieldNames
;
189 function isFieldSortable( $field ) {
190 if ( $this->mIncluding
) {
193 $sortable = array( 'img_timestamp', 'img_name', 'img_size' );
194 /* For reference, the indicies we can use for sorting are:
195 * On the image table: img_usertext_timestamp, img_size, img_timestamp
196 * On oldimage: oi_usertext_timestamp, oi_name_timestamp
198 * In particular that means we cannot sort by timestamp when not filtering
199 * by user and including old images in the results. Which is sad.
201 if ( $this->getConfig()->get( 'MiserMode' ) && !is_null( $this->mUserName
) ) {
202 // If we're sorting by user, the index only supports sorting by time.
203 if ( $field === 'img_timestamp' ) {
208 } elseif ( $this->getConfig()->get( 'MiserMode' ) && $this->mShowAll
/* && mUserName === null */ ) {
209 // no oi_timestamp index, so only alphabetical sorting in this case.
210 if ( $field === 'img_name' ) {
217 return in_array( $field, $sortable );
220 function getQueryInfo() {
221 // Hacky Hacky Hacky - I want to get query info
222 // for two different tables, without reimplementing
224 $qi = $this->getQueryInfoReal( $this->mTableName
);
230 * Actually get the query info.
232 * This is to allow displaying both stuff from image and oldimage table.
234 * This is a bit hacky.
236 * @param string $table Either 'image' or 'oldimage'
237 * @return array Query info
239 protected function getQueryInfoReal( $table ) {
240 $prefix = $table === 'oldimage' ?
'oi' : 'img';
242 $tables = array( $table );
243 $fields = array_keys( $this->getFieldNames() );
245 if ( $table === 'oldimage' ) {
246 foreach ( $fields as $id => &$field ) {
247 if ( substr( $field, 0, 4 ) !== 'img_' ) {
250 $field = $prefix . substr( $field, 3 ) . ' AS ' . $field;
252 $fields[array_search( 'top', $fields )] = "'no' AS top";
254 if ( $this->mShowAll
) {
255 $fields[array_search( 'top', $fields )] = "'yes' AS top";
258 $fields[] = $prefix . '_user AS img_user';
259 $fields[array_search( 'thumb', $fields )] = $prefix . '_name AS thumb';
261 $options = $join_conds = array();
263 # Depends on $wgMiserMode
264 # Will also not happen if mShowAll is true.
265 if ( isset( $this->mFieldNames
['count'] ) ) {
266 $tables[] = 'oldimage';
268 # Need to rewrite this one
269 foreach ( $fields as &$field ) {
270 if ( $field == 'count' ) {
271 $field = 'COUNT(oi_archive_name) AS count';
276 $dbr = wfGetDB( DB_SLAVE
);
277 if ( $dbr->implicitGroupby() ) {
278 $options = array( 'GROUP BY' => 'img_name' );
280 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
281 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
283 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
289 'conds' => $this->buildQueryConds( $table ),
290 'options' => $options,
291 'join_conds' => $join_conds
296 * Override reallyDoQuery to mix together two queries.
298 * @note $asc is named $descending in IndexPager base class. However
299 * it is true when the order is ascending, and false when the order
300 * is descending, so I renamed it to $asc here.
305 function reallyDoQuery( $offset, $limit, $asc ) {
306 $prevTableName = $this->mTableName
;
307 $this->mTableName
= 'image';
308 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
309 $this->buildQueryInfo( $offset, $limit, $asc );
310 $imageRes = $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
311 $this->mTableName
= $prevTableName;
313 if ( !$this->mShowAll
) {
317 $this->mTableName
= 'oldimage';
320 $oldIndex = $this->mIndexField
;
321 if ( substr( $this->mIndexField
, 0, 4 ) !== 'img_' ) {
322 throw new MWException( "Expected to be sorting on an image table field" );
324 $this->mIndexField
= 'oi_' . substr( $this->mIndexField
, 4 );
326 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
327 $this->buildQueryInfo( $offset, $limit, $asc );
328 $oldimageRes = $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
330 $this->mTableName
= $prevTableName;
331 $this->mIndexField
= $oldIndex;
333 return $this->combineResult( $imageRes, $oldimageRes, $limit, $asc );
337 * Combine results from 2 tables.
339 * Note: This will throw away some results
341 * @param ResultWrapper $res1
342 * @param ResultWrapper $res2
344 * @param bool $ascending See note about $asc in $this->reallyDoQuery
345 * @return FakeResultWrapper $res1 and $res2 combined
347 protected function combineResult( $res1, $res2, $limit, $ascending ) {
350 $topRes1 = $res1->next();
351 $topRes2 = $res2->next();
352 $resultArray = array();
353 for ( $i = 0; $i < $limit && $topRes1 && $topRes2; $i++
) {
354 if ( strcmp( $topRes1->{$this->mIndexField
}, $topRes2->{$this->mIndexField
} ) > 0 ) {
356 $resultArray[] = $topRes1;
357 $topRes1 = $res1->next();
359 $resultArray[] = $topRes2;
360 $topRes2 = $res2->next();
364 $resultArray[] = $topRes2;
365 $topRes2 = $res2->next();
367 $resultArray[] = $topRes1;
368 $topRes1 = $res1->next();
373 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
374 for ( ; $i < $limit && $topRes1; $i++
) {
375 // @codingStandardsIgnoreEnd
376 $resultArray[] = $topRes1;
377 $topRes1 = $res1->next();
380 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
381 for ( ; $i < $limit && $topRes2; $i++
) {
382 // @codingStandardsIgnoreEnd
383 $resultArray[] = $topRes2;
384 $topRes2 = $res2->next();
387 return new FakeResultWrapper( $resultArray );
390 function getDefaultSort() {
391 if ( $this->mShowAll
&& $this->getConfig()->get( 'MiserMode' ) && 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 // Add delete links if allowed
459 // From https://github.com/Wikia/app/pull/3859
460 if ( $filePage->userCan( 'delete', $this->getUser() ) ) {
461 $deleteMsg = $this->msg( 'listfiles-delete' )->escaped();
463 $delete = Linker
::linkKnown(
464 $filePage, $deleteMsg, array(), array( 'action' => 'delete' )
466 $delete = $this->msg( 'parentheses' )->rawParams( $delete )->escaped();
468 return "$link $download $delete";
471 return "$link $download";
473 return htmlspecialchars( $value );
475 case 'img_user_text':
476 if ( $this->mCurrentRow
->img_user
) {
477 $name = User
::whoIs( $this->mCurrentRow
->img_user
);
478 $link = Linker
::link(
479 Title
::makeTitle( NS_USER
, $name ),
480 htmlspecialchars( $name )
483 $link = htmlspecialchars( $value );
488 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
489 case 'img_description':
490 return Linker
::formatComment( $value );
492 return intval( $value ) +
1;
494 // Messages: listfiles-latestversion-yes, listfiles-latestversion-no
495 return $this->msg( 'listfiles-latestversion-' . $value );
497 throw new MWException( "Unknown field '$field'" );
502 $inputForm = array();
503 $inputForm['table_pager_limit_label'] = $this->getLimitSelect( array( 'tabindex' => 1 ) );
504 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
505 $inputForm['listfiles_search_for'] = Html
::input(
511 'maxlength' => '255',
512 'id' => 'mw-ilsearch',
517 $inputForm['username'] = Html
::input( 'user', $this->mUserName
, 'text', array(
519 'maxlength' => '255',
520 'id' => 'mw-listfiles-user',
524 $inputForm['listfiles-show-all'] = Html
::input( 'ilshowall', 1, 'checkbox', array(
525 'checked' => $this->mShowAll
,
529 return Html
::openElement( 'form',
530 array( 'method' => 'get', 'action' => wfScript(), 'id' => 'mw-listfiles-form' )
532 Xml
::fieldset( $this->msg( 'listfiles' )->text() ) .
533 Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
534 Xml
::buildForm( $inputForm, 'table_pager_limit_submit', array( 'tabindex' => 5 ) ) .
535 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user', 'title', 'ilshowall' ) ) .
536 Html
::closeElement( 'fieldset' ) .
537 Html
::closeElement( 'form' ) . "\n";
540 function getTableClass() {
541 return 'listfiles ' . parent
::getTableClass();
544 function getNavClass() {
545 return 'listfiles_nav ' . parent
::getNavClass();
548 function getSortHeaderClass() {
549 return 'listfiles_sort ' . parent
::getSortHeaderClass();
552 function getPagingQueries() {
553 $queries = parent
::getPagingQueries();
554 if ( !is_null( $this->mUserName
) ) {
555 # Append the username to the query string
556 foreach ( $queries as &$query ) {
557 if ( $query !== false ) {
558 $query['user'] = $this->mUserName
;
566 function getDefaultQuery() {
567 $queries = parent
::getDefaultQuery();
568 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName
) ) {
569 $queries['user'] = $this->mUserName
;
575 function getTitle() {
576 return SpecialPage
::getTitleFor( 'Listfiles' );