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 $out = $this->getOutput();
52 if ( $this->including() ) {
53 $out->addParserOutputContent( $pager->getBodyOutput() );
56 $out->addParserOutputContent( $pager->getFullOutput() );
61 * Return an array of subpages beginning with $search that this special page will accept.
63 * @param string $search Prefix to search for
64 * @param int $limit Maximum number of results to return (usually 10)
65 * @param int $offset Number of results to skip (usually 0)
66 * @return string[] Matching subpages
68 public function prefixSearchSubpages( $search, $limit, $offset ) {
69 $user = User
::newFromName( $search );
71 // No prefix suggestion for invalid user
74 // Autocomplete subpage as user list - public to allow caching
75 return UserNamePrefixSearch
::search( 'public', $search, $limit, $offset );
78 protected function getGroupName() {
84 * @ingroup SpecialPage Pager
86 class ImageListPager
extends TablePager
{
87 protected $mFieldNames = null;
89 // Subclasses should override buildQueryConds instead of using $mQueryConds variable.
90 protected $mQueryConds = array();
92 protected $mUserName = null;
94 protected $mSearch = '';
96 protected $mIncluding = false;
98 protected $mShowAll = false;
100 protected $mTableName = 'image';
102 function __construct( IContextSource
$context, $userName = null, $search = '',
103 $including = false, $showAll = false
105 $this->setContext( $context );
106 $this->mIncluding
= $including;
107 $this->mShowAll
= $showAll;
109 if ( $userName !== null && $userName !== '' ) {
110 $nt = Title
::newFromText( $userName, NS_USER
);
111 $user = User
::newFromName( $userName, false );
112 if ( !is_null( $nt ) ) {
113 $this->mUserName
= $nt->getText();
115 if ( !$user ||
( $user->isAnon() && !User
::isIP( $user->getName() ) ) ) {
116 $this->getOutput()->wrapWikiMsg(
117 "<div class=\"mw-userpage-userdoesnotexist error\">\n$1\n</div>",
119 'listfiles-userdoesnotexist',
120 wfEscapeWikiText( $userName ),
127 if ( $search !== '' && !$this->getConfig()->get( 'MiserMode' ) ) {
128 $this->mSearch
= $search;
129 $nt = Title
::newFromText( $this->mSearch
);
132 $dbr = wfGetDB( DB_SLAVE
);
133 $this->mQueryConds
[] = 'LOWER(img_name)' .
134 $dbr->buildLike( $dbr->anyString(),
135 strtolower( $nt->getDBkey() ), $dbr->anyString() );
140 if ( $this->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
141 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
143 $this->mDefaultDirection
= IndexPager
::DIR_ASCENDING
;
146 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
149 parent
::__construct( $context );
153 * Build the where clause of the query.
155 * Replaces the older mQueryConds member variable.
156 * @param string $table Either "image" or "oldimage"
157 * @return array The query conditions.
159 protected function buildQueryConds( $table ) {
160 $prefix = $table === 'image' ?
'img' : 'oi';
163 if ( !is_null( $this->mUserName
) ) {
164 $conds[$prefix . '_user_text'] = $this->mUserName
;
167 if ( $this->mSearch
!== '' ) {
168 $nt = Title
::newFromText( $this->mSearch
);
170 $dbr = wfGetDB( DB_SLAVE
);
171 $conds[] = 'LOWER(' . $prefix . '_name)' .
172 $dbr->buildLike( $dbr->anyString(),
173 strtolower( $nt->getDBkey() ), $dbr->anyString() );
177 if ( $table === 'oldimage' ) {
178 // Don't want to deal with revdel.
179 // Future fixme: Show partial information as appropriate.
180 // Would have to be careful about filtering by username when username is deleted.
181 $conds['oi_deleted'] = 0;
184 // Add mQueryConds in case anyone was subclassing and using the old variable.
185 return $conds +
$this->mQueryConds
;
191 function getFieldNames() {
192 if ( !$this->mFieldNames
) {
193 $this->mFieldNames
= array(
194 'img_timestamp' => $this->msg( 'listfiles_date' )->text(),
195 'img_name' => $this->msg( 'listfiles_name' )->text(),
196 'thumb' => $this->msg( 'listfiles_thumb' )->text(),
197 'img_size' => $this->msg( 'listfiles_size' )->text(),
199 if ( is_null( $this->mUserName
) ) {
200 // Do not show username if filtering by username
201 $this->mFieldNames
['img_user_text'] = $this->msg( 'listfiles_user' )->text();
203 // img_description down here, in order so that its still after the username field.
204 $this->mFieldNames
['img_description'] = $this->msg( 'listfiles_description' )->text();
206 if ( !$this->getConfig()->get( 'MiserMode' ) && !$this->mShowAll
) {
207 $this->mFieldNames
['count'] = $this->msg( 'listfiles_count' )->text();
209 if ( $this->mShowAll
) {
210 $this->mFieldNames
['top'] = $this->msg( 'listfiles-latestversion' )->text();
214 return $this->mFieldNames
;
217 function isFieldSortable( $field ) {
218 if ( $this->mIncluding
) {
221 $sortable = array( 'img_timestamp', 'img_name', 'img_size' );
222 /* For reference, the indicies we can use for sorting are:
223 * On the image table: img_usertext_timestamp, img_size, img_timestamp
224 * On oldimage: oi_usertext_timestamp, oi_name_timestamp
226 * In particular that means we cannot sort by timestamp when not filtering
227 * by user and including old images in the results. Which is sad.
229 if ( $this->getConfig()->get( 'MiserMode' ) && !is_null( $this->mUserName
) ) {
230 // If we're sorting by user, the index only supports sorting by time.
231 if ( $field === 'img_timestamp' ) {
236 } elseif ( $this->getConfig()->get( 'MiserMode' )
237 && $this->mShowAll
/* && mUserName === null */
239 // no oi_timestamp index, so only alphabetical sorting in this case.
240 if ( $field === 'img_name' ) {
247 return in_array( $field, $sortable );
250 function getQueryInfo() {
251 // Hacky Hacky Hacky - I want to get query info
252 // for two different tables, without reimplementing
254 $qi = $this->getQueryInfoReal( $this->mTableName
);
260 * Actually get the query info.
262 * This is to allow displaying both stuff from image and oldimage table.
264 * This is a bit hacky.
266 * @param string $table Either 'image' or 'oldimage'
267 * @return array Query info
269 protected function getQueryInfoReal( $table ) {
270 $prefix = $table === 'oldimage' ?
'oi' : 'img';
272 $tables = array( $table );
273 $fields = array_keys( $this->getFieldNames() );
275 if ( $table === 'oldimage' ) {
276 foreach ( $fields as $id => &$field ) {
277 if ( substr( $field, 0, 4 ) !== 'img_' ) {
280 $field = $prefix . substr( $field, 3 ) . ' AS ' . $field;
282 $fields[array_search( 'top', $fields )] = "'no' AS top";
284 if ( $this->mShowAll
) {
285 $fields[array_search( 'top', $fields )] = "'yes' AS top";
288 $fields[] = $prefix . '_user AS img_user';
289 $fields[array_search( 'thumb', $fields )] = $prefix . '_name AS thumb';
291 $options = $join_conds = array();
293 # Depends on $wgMiserMode
294 # Will also not happen if mShowAll is true.
295 if ( isset( $this->mFieldNames
['count'] ) ) {
296 $tables[] = 'oldimage';
298 # Need to rewrite this one
299 foreach ( $fields as &$field ) {
300 if ( $field == 'count' ) {
301 $field = 'COUNT(oi_archive_name) AS count';
306 $dbr = wfGetDB( DB_SLAVE
);
307 if ( $dbr->implicitGroupby() ) {
308 $options = array( 'GROUP BY' => 'img_name' );
310 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
311 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
313 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
319 'conds' => $this->buildQueryConds( $table ),
320 'options' => $options,
321 'join_conds' => $join_conds
326 * Override reallyDoQuery to mix together two queries.
328 * @note $asc is named $descending in IndexPager base class. However
329 * it is true when the order is ascending, and false when the order
330 * is descending, so I renamed it to $asc here.
335 * @throws MWException
337 function reallyDoQuery( $offset, $limit, $asc ) {
338 $prevTableName = $this->mTableName
;
339 $this->mTableName
= 'image';
340 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
341 $this->buildQueryInfo( $offset, $limit, $asc );
342 $imageRes = $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
343 $this->mTableName
= $prevTableName;
345 if ( !$this->mShowAll
) {
349 $this->mTableName
= 'oldimage';
352 $oldIndex = $this->mIndexField
;
353 if ( substr( $this->mIndexField
, 0, 4 ) !== 'img_' ) {
354 throw new MWException( "Expected to be sorting on an image table field" );
356 $this->mIndexField
= 'oi_' . substr( $this->mIndexField
, 4 );
358 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
359 $this->buildQueryInfo( $offset, $limit, $asc );
360 $oldimageRes = $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
362 $this->mTableName
= $prevTableName;
363 $this->mIndexField
= $oldIndex;
365 return $this->combineResult( $imageRes, $oldimageRes, $limit, $asc );
369 * Combine results from 2 tables.
371 * Note: This will throw away some results
373 * @param ResultWrapper $res1
374 * @param ResultWrapper $res2
376 * @param bool $ascending See note about $asc in $this->reallyDoQuery
377 * @return FakeResultWrapper $res1 and $res2 combined
379 protected function combineResult( $res1, $res2, $limit, $ascending ) {
382 $topRes1 = $res1->next();
383 $topRes2 = $res2->next();
384 $resultArray = array();
385 for ( $i = 0; $i < $limit && $topRes1 && $topRes2; $i++
) {
386 if ( strcmp( $topRes1->{$this->mIndexField
}, $topRes2->{$this->mIndexField
} ) > 0 ) {
388 $resultArray[] = $topRes1;
389 $topRes1 = $res1->next();
391 $resultArray[] = $topRes2;
392 $topRes2 = $res2->next();
396 $resultArray[] = $topRes2;
397 $topRes2 = $res2->next();
399 $resultArray[] = $topRes1;
400 $topRes1 = $res1->next();
405 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
406 for ( ; $i < $limit && $topRes1; $i++
) {
407 // @codingStandardsIgnoreEnd
408 $resultArray[] = $topRes1;
409 $topRes1 = $res1->next();
412 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
413 for ( ; $i < $limit && $topRes2; $i++
) {
414 // @codingStandardsIgnoreEnd
415 $resultArray[] = $topRes2;
416 $topRes2 = $res2->next();
419 return new FakeResultWrapper( $resultArray );
422 function getDefaultSort() {
423 if ( $this->mShowAll
&& $this->getConfig()->get( 'MiserMode' ) && is_null( $this->mUserName
) ) {
424 // Unfortunately no index on oi_timestamp.
427 return 'img_timestamp';
431 function doBatchLookups() {
433 $this->mResult
->seek( 0 );
434 foreach ( $this->mResult
as $row ) {
435 $userIds[] = $row->img_user
;
437 # Do a link batch query for names and userpages
438 UserCache
::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__
);
442 * @param string $field
443 * @param string $value
444 * @return Message|string|int The return type depends on the value of $field:
446 * - img_timestamp: string
448 * - img_user_text: string
450 * - img_description: string
453 * @throws MWException
455 function formatValue( $field, $value ) {
458 $opt = array( 'time' => wfTimestamp( TS_MW
, $this->mCurrentRow
->img_timestamp
) );
459 $file = RepoGroup
::singleton()->getLocalRepo()->findFile( $value, $opt );
460 // If statement for paranoia
462 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
464 return $thumb->toHtml( array( 'desc-link' => true ) );
466 return htmlspecialchars( $value );
468 case 'img_timestamp':
469 // We may want to make this a link to the "old" version when displaying old files
470 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
472 static $imgfile = null;
473 if ( $imgfile === null ) {
474 $imgfile = $this->msg( 'imgfile' )->text();
477 // Weird files can maybe exist? Bug 22227
478 $filePage = Title
::makeTitleSafe( NS_FILE
, $value );
480 $link = Linker
::linkKnown(
482 htmlspecialchars( $filePage->getText() )
484 $download = Xml
::element( 'a',
485 array( 'href' => wfLocalFile( $filePage )->getURL() ),
488 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
490 // Add delete links if allowed
491 // From https://github.com/Wikia/app/pull/3859
492 if ( $filePage->userCan( 'delete', $this->getUser() ) ) {
493 $deleteMsg = $this->msg( 'listfiles-delete' )->escaped();
495 $delete = Linker
::linkKnown(
496 $filePage, $deleteMsg, array(), array( 'action' => 'delete' )
498 $delete = $this->msg( 'parentheses' )->rawParams( $delete )->escaped();
500 return "$link $download $delete";
503 return "$link $download";
505 return htmlspecialchars( $value );
507 case 'img_user_text':
508 if ( $this->mCurrentRow
->img_user
) {
509 $name = User
::whoIs( $this->mCurrentRow
->img_user
);
510 $link = Linker
::link(
511 Title
::makeTitle( NS_USER
, $name ),
512 htmlspecialchars( $name )
515 $link = htmlspecialchars( $value );
520 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
521 case 'img_description':
522 return Linker
::formatComment( $value );
524 return intval( $value ) +
1;
526 // Messages: listfiles-latestversion-yes, listfiles-latestversion-no
527 return $this->msg( 'listfiles-latestversion-' . $value );
529 throw new MWException( "Unknown field '$field'" );
535 $fields['limit'] = array(
538 'label-message' => 'table_pager_limit_label',
539 'options' => $this->getLimitSelectList(),
540 'default' => $this->mLimit
,
543 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
544 $fields['ilsearch'] = array(
546 'name' => 'ilsearch',
547 'id' => 'mw-ilsearch',
548 'label-message' => 'listfiles_search_for',
549 'default' => $this->mSearch
,
551 'maxlength' => '255',
555 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
556 $fields['user'] = array(
559 'id' => 'mw-listfiles-user',
560 'label-message' => 'username',
561 'default' => $this->mUserName
,
563 'maxlength' => '255',
564 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
567 $fields['ilshowall'] = array(
569 'name' => 'ilshowall',
570 'id' => 'mw-listfiles-show-all',
571 'label-message' => 'listfiles-show-all',
572 'default' => $this->mShowAll
,
575 $query = $this->getRequest()->getQueryValues();
576 unset( $query['title'] );
577 unset( $query['limit'] );
578 unset( $query['ilsearch'] );
579 unset( $query['ilshowall'] );
580 unset( $query['user'] );
582 $form = new HTMLForm( $fields, $this->getContext() );
584 $form->setMethod( 'get' );
585 $form->setTitle( $this->getTitle() );
586 $form->setId( 'mw-listfiles-form' );
587 $form->setWrapperLegendMsg( 'listfiles' );
588 $form->setSubmitTextMsg( 'table_pager_limit_submit' );
589 $form->addHiddenFields( $query );
591 $form->prepareForm();
592 $form->displayForm( '' );
595 function getTableClass() {
596 return parent
::getTableClass() . ' listfiles';
599 function getNavClass() {
600 return parent
::getNavClass() . ' listfiles_nav';
603 function getSortHeaderClass() {
604 return parent
::getSortHeaderClass() . ' listfiles_sort';
607 function getPagingQueries() {
608 $queries = parent
::getPagingQueries();
609 if ( !is_null( $this->mUserName
) ) {
610 # Append the username to the query string
611 foreach ( $queries as &$query ) {
612 if ( $query !== false ) {
613 $query['user'] = $this->mUserName
;
621 function getDefaultQuery() {
622 $queries = parent
::getDefaultQuery();
623 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName
) ) {
624 $queries['user'] = $this->mUserName
;
630 function getTitle() {
631 return SpecialPage
::getTitleFor( 'Listfiles' );