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() );
60 protected function getGroupName() {
66 * @ingroup SpecialPage Pager
68 class ImageListPager
extends TablePager
{
69 protected $mFieldNames = null;
71 // Subclasses should override buildQueryConds instead of using $mQueryConds variable.
72 protected $mQueryConds = array();
74 protected $mUserName = null;
76 protected $mSearch = '';
78 protected $mIncluding = false;
80 protected $mShowAll = false;
82 protected $mTableName = 'image';
84 function __construct( IContextSource
$context, $userName = null, $search = '',
85 $including = false, $showAll = false
87 $this->setContext( $context );
88 $this->mIncluding
= $including;
89 $this->mShowAll
= $showAll;
91 if ( $userName !== null && $userName !== '' ) {
92 $nt = Title
::newFromText( $userName, NS_USER
);
93 $user = User
::newFromName( $userName, false );
94 if ( !is_null( $nt ) ) {
95 $this->mUserName
= $nt->getText();
97 if ( !$user ||
( $user->isAnon() && !User
::isIP( $user->getName() ) ) ) {
98 $this->getOutput()->wrapWikiMsg(
99 "<div class=\"mw-userpage-userdoesnotexist error\">\n$1\n</div>",
101 'listfiles-userdoesnotexist',
102 wfEscapeWikiText( $userName ),
109 if ( $search !== '' && !$this->getConfig()->get( 'MiserMode' ) ) {
110 $this->mSearch
= $search;
111 $nt = Title
::newFromText( $this->mSearch
);
114 $dbr = wfGetDB( DB_SLAVE
);
115 $this->mQueryConds
[] = 'LOWER(img_name)' .
116 $dbr->buildLike( $dbr->anyString(),
117 strtolower( $nt->getDBkey() ), $dbr->anyString() );
122 if ( $this->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
123 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
125 $this->mDefaultDirection
= IndexPager
::DIR_ASCENDING
;
128 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
131 parent
::__construct( $context );
135 * Build the where clause of the query.
137 * Replaces the older mQueryConds member variable.
138 * @param string $table Either "image" or "oldimage"
139 * @return array The query conditions.
141 protected function buildQueryConds( $table ) {
142 $prefix = $table === 'image' ?
'img' : 'oi';
145 if ( !is_null( $this->mUserName
) ) {
146 $conds[$prefix . '_user_text'] = $this->mUserName
;
149 if ( $this->mSearch
!== '' ) {
150 $nt = Title
::newFromText( $this->mSearch
);
152 $dbr = wfGetDB( DB_SLAVE
);
153 $conds[] = 'LOWER(' . $prefix . '_name)' .
154 $dbr->buildLike( $dbr->anyString(),
155 strtolower( $nt->getDBkey() ), $dbr->anyString() );
159 if ( $table === 'oldimage' ) {
160 // Don't want to deal with revdel.
161 // Future fixme: Show partial information as appropriate.
162 // Would have to be careful about filtering by username when username is deleted.
163 $conds['oi_deleted'] = 0;
166 // Add mQueryConds in case anyone was subclassing and using the old variable.
167 return $conds +
$this->mQueryConds
;
173 function getFieldNames() {
174 if ( !$this->mFieldNames
) {
175 $this->mFieldNames
= array(
176 'img_timestamp' => $this->msg( 'listfiles_date' )->text(),
177 'img_name' => $this->msg( 'listfiles_name' )->text(),
178 'thumb' => $this->msg( 'listfiles_thumb' )->text(),
179 'img_size' => $this->msg( 'listfiles_size' )->text(),
181 if ( is_null( $this->mUserName
) ) {
182 // Do not show username if filtering by username
183 $this->mFieldNames
['img_user_text'] = $this->msg( 'listfiles_user' )->text();
185 // img_description down here, in order so that its still after the username field.
186 $this->mFieldNames
['img_description'] = $this->msg( 'listfiles_description' )->text();
188 if ( !$this->getConfig()->get( 'MiserMode' ) && !$this->mShowAll
) {
189 $this->mFieldNames
['count'] = $this->msg( 'listfiles_count' )->text();
191 if ( $this->mShowAll
) {
192 $this->mFieldNames
['top'] = $this->msg( 'listfiles-latestversion' )->text();
196 return $this->mFieldNames
;
199 function isFieldSortable( $field ) {
200 if ( $this->mIncluding
) {
203 $sortable = array( 'img_timestamp', 'img_name', 'img_size' );
204 /* For reference, the indicies we can use for sorting are:
205 * On the image table: img_usertext_timestamp, img_size, img_timestamp
206 * On oldimage: oi_usertext_timestamp, oi_name_timestamp
208 * In particular that means we cannot sort by timestamp when not filtering
209 * by user and including old images in the results. Which is sad.
211 if ( $this->getConfig()->get( 'MiserMode' ) && !is_null( $this->mUserName
) ) {
212 // If we're sorting by user, the index only supports sorting by time.
213 if ( $field === 'img_timestamp' ) {
218 } elseif ( $this->getConfig()->get( 'MiserMode' )
219 && $this->mShowAll
/* && mUserName === null */
221 // no oi_timestamp index, so only alphabetical sorting in this case.
222 if ( $field === 'img_name' ) {
229 return in_array( $field, $sortable );
232 function getQueryInfo() {
233 // Hacky Hacky Hacky - I want to get query info
234 // for two different tables, without reimplementing
236 $qi = $this->getQueryInfoReal( $this->mTableName
);
242 * Actually get the query info.
244 * This is to allow displaying both stuff from image and oldimage table.
246 * This is a bit hacky.
248 * @param string $table Either 'image' or 'oldimage'
249 * @return array Query info
251 protected function getQueryInfoReal( $table ) {
252 $prefix = $table === 'oldimage' ?
'oi' : 'img';
254 $tables = array( $table );
255 $fields = array_keys( $this->getFieldNames() );
257 if ( $table === 'oldimage' ) {
258 foreach ( $fields as $id => &$field ) {
259 if ( substr( $field, 0, 4 ) !== 'img_' ) {
262 $field = $prefix . substr( $field, 3 ) . ' AS ' . $field;
264 $fields[array_search( 'top', $fields )] = "'no' AS top";
266 if ( $this->mShowAll
) {
267 $fields[array_search( 'top', $fields )] = "'yes' AS top";
270 $fields[] = $prefix . '_user AS img_user';
271 $fields[array_search( 'thumb', $fields )] = $prefix . '_name AS thumb';
273 $options = $join_conds = array();
275 # Depends on $wgMiserMode
276 # Will also not happen if mShowAll is true.
277 if ( isset( $this->mFieldNames
['count'] ) ) {
278 $tables[] = 'oldimage';
280 # Need to rewrite this one
281 foreach ( $fields as &$field ) {
282 if ( $field == 'count' ) {
283 $field = 'COUNT(oi_archive_name) AS count';
288 $dbr = wfGetDB( DB_SLAVE
);
289 if ( $dbr->implicitGroupby() ) {
290 $options = array( 'GROUP BY' => 'img_name' );
292 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
293 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
295 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
301 'conds' => $this->buildQueryConds( $table ),
302 'options' => $options,
303 'join_conds' => $join_conds
308 * Override reallyDoQuery to mix together two queries.
310 * @note $asc is named $descending in IndexPager base class. However
311 * it is true when the order is ascending, and false when the order
312 * is descending, so I renamed it to $asc here.
317 * @throws MWException
319 function reallyDoQuery( $offset, $limit, $asc ) {
320 $prevTableName = $this->mTableName
;
321 $this->mTableName
= 'image';
322 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
323 $this->buildQueryInfo( $offset, $limit, $asc );
324 $imageRes = $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
325 $this->mTableName
= $prevTableName;
327 if ( !$this->mShowAll
) {
331 $this->mTableName
= 'oldimage';
334 $oldIndex = $this->mIndexField
;
335 if ( substr( $this->mIndexField
, 0, 4 ) !== 'img_' ) {
336 throw new MWException( "Expected to be sorting on an image table field" );
338 $this->mIndexField
= 'oi_' . substr( $this->mIndexField
, 4 );
340 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
341 $this->buildQueryInfo( $offset, $limit, $asc );
342 $oldimageRes = $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
344 $this->mTableName
= $prevTableName;
345 $this->mIndexField
= $oldIndex;
347 return $this->combineResult( $imageRes, $oldimageRes, $limit, $asc );
351 * Combine results from 2 tables.
353 * Note: This will throw away some results
355 * @param ResultWrapper $res1
356 * @param ResultWrapper $res2
358 * @param bool $ascending See note about $asc in $this->reallyDoQuery
359 * @return FakeResultWrapper $res1 and $res2 combined
361 protected function combineResult( $res1, $res2, $limit, $ascending ) {
364 $topRes1 = $res1->next();
365 $topRes2 = $res2->next();
366 $resultArray = array();
367 for ( $i = 0; $i < $limit && $topRes1 && $topRes2; $i++
) {
368 if ( strcmp( $topRes1->{$this->mIndexField
}, $topRes2->{$this->mIndexField
} ) > 0 ) {
370 $resultArray[] = $topRes1;
371 $topRes1 = $res1->next();
373 $resultArray[] = $topRes2;
374 $topRes2 = $res2->next();
378 $resultArray[] = $topRes2;
379 $topRes2 = $res2->next();
381 $resultArray[] = $topRes1;
382 $topRes1 = $res1->next();
387 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
388 for ( ; $i < $limit && $topRes1; $i++
) {
389 // @codingStandardsIgnoreEnd
390 $resultArray[] = $topRes1;
391 $topRes1 = $res1->next();
394 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
395 for ( ; $i < $limit && $topRes2; $i++
) {
396 // @codingStandardsIgnoreEnd
397 $resultArray[] = $topRes2;
398 $topRes2 = $res2->next();
401 return new FakeResultWrapper( $resultArray );
404 function getDefaultSort() {
405 if ( $this->mShowAll
&& $this->getConfig()->get( 'MiserMode' ) && is_null( $this->mUserName
) ) {
406 // Unfortunately no index on oi_timestamp.
409 return 'img_timestamp';
413 function doBatchLookups() {
415 $this->mResult
->seek( 0 );
416 foreach ( $this->mResult
as $row ) {
417 $userIds[] = $row->img_user
;
419 # Do a link batch query for names and userpages
420 UserCache
::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__
);
424 * @param string $field
425 * @param string $value
426 * @return Message|string|int The return type depends on the value of $field:
428 * - img_timestamp: string
430 * - img_user_text: string
432 * - img_description: string
435 * @throws MWException
437 function formatValue( $field, $value ) {
440 $opt = array( 'time' => wfTimestamp( TS_MW
, $this->mCurrentRow
->img_timestamp
) );
441 $file = RepoGroup
::singleton()->getLocalRepo()->findFile( $value, $opt );
442 // If statement for paranoia
444 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
446 return $thumb->toHtml( array( 'desc-link' => true ) );
448 return htmlspecialchars( $value );
450 case 'img_timestamp':
451 // We may want to make this a link to the "old" version when displaying old files
452 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
454 static $imgfile = null;
455 if ( $imgfile === null ) {
456 $imgfile = $this->msg( 'imgfile' )->text();
459 // Weird files can maybe exist? Bug 22227
460 $filePage = Title
::makeTitleSafe( NS_FILE
, $value );
462 $link = Linker
::linkKnown(
464 htmlspecialchars( $filePage->getText() )
466 $download = Xml
::element( 'a',
467 array( 'href' => wfLocalFile( $filePage )->getURL() ),
470 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
472 // Add delete links if allowed
473 // From https://github.com/Wikia/app/pull/3859
474 if ( $filePage->userCan( 'delete', $this->getUser() ) ) {
475 $deleteMsg = $this->msg( 'listfiles-delete' )->escaped();
477 $delete = Linker
::linkKnown(
478 $filePage, $deleteMsg, array(), array( 'action' => 'delete' )
480 $delete = $this->msg( 'parentheses' )->rawParams( $delete )->escaped();
482 return "$link $download $delete";
485 return "$link $download";
487 return htmlspecialchars( $value );
489 case 'img_user_text':
490 if ( $this->mCurrentRow
->img_user
) {
491 $name = User
::whoIs( $this->mCurrentRow
->img_user
);
492 $link = Linker
::link(
493 Title
::makeTitle( NS_USER
, $name ),
494 htmlspecialchars( $name )
497 $link = htmlspecialchars( $value );
502 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
503 case 'img_description':
504 return Linker
::formatComment( $value );
506 return intval( $value ) +
1;
508 // Messages: listfiles-latestversion-yes, listfiles-latestversion-no
509 return $this->msg( 'listfiles-latestversion-' . $value );
511 throw new MWException( "Unknown field '$field'" );
517 $fields['limit'] = array(
520 'label-message' => 'table_pager_limit_label',
521 'options' => $this->getLimitSelectList(),
522 'default' => $this->mLimit
,
525 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
526 $fields['ilsearch'] = array(
528 'name' => 'ilsearch',
529 'id' => 'mw-ilsearch',
530 'label-message' => 'listfiles_search_for',
531 'default' => $this->mSearch
,
533 'maxlength' => '255',
537 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
538 $fields['user'] = array(
541 'id' => 'mw-listfiles-user',
542 'label-message' => 'username',
543 'default' => $this->mUserName
,
545 'maxlength' => '255',
546 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
549 $fields['ilshowall'] = array(
551 'name' => 'ilshowall',
552 'id' => 'mw-listfiles-show-all',
553 'label-message' => 'listfiles-show-all',
554 'default' => $this->mShowAll
,
557 $query = $this->getRequest()->getQueryValues();
558 unset( $query['title'] );
559 unset( $query['limit'] );
560 unset( $query['ilsearch'] );
561 unset( $query['ilshowall'] );
562 unset( $query['user'] );
564 $form = new HTMLForm( $fields, $this->getContext() );
566 $form->setMethod( 'get' );
567 $form->setTitle( $this->getTitle() );
568 $form->setId( 'mw-listfiles-form' );
569 $form->setWrapperLegendMsg( 'listfiles' );
570 $form->setSubmitTextMsg( 'table_pager_limit_submit' );
571 $form->addHiddenFields( $query );
573 $form->prepareForm();
574 $form->displayForm( '' );
577 function getTableClass() {
578 return parent
::getTableClass() . ' listfiles';
581 function getNavClass() {
582 return parent
::getNavClass() . ' listfiles_nav';
585 function getSortHeaderClass() {
586 return parent
::getSortHeaderClass() . ' listfiles_sort';
589 function getPagingQueries() {
590 $queries = parent
::getPagingQueries();
591 if ( !is_null( $this->mUserName
) ) {
592 # Append the username to the query string
593 foreach ( $queries as &$query ) {
594 if ( $query !== false ) {
595 $query['user'] = $this->mUserName
;
603 function getDefaultQuery() {
604 $queries = parent
::getDefaultQuery();
605 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName
) ) {
606 $queries['user'] = $this->mUserName
;
612 function getTitle() {
613 return SpecialPage
::getTitleFor( 'Listfiles' );