3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
25 use MediaWiki\MediaWikiServices
;
26 use Wikimedia\Rdbms\ResultWrapper
;
27 use Wikimedia\Rdbms\FakeResultWrapper
;
29 class ImageListPager
extends TablePager
{
31 protected $mFieldNames = null;
33 // Subclasses should override buildQueryConds instead of using $mQueryConds variable.
34 protected $mQueryConds = [];
36 protected $mUserName = null;
43 protected $mUser = null;
45 protected $mSearch = '';
47 protected $mIncluding = false;
49 protected $mShowAll = false;
51 protected $mTableName = 'image';
53 function __construct( IContextSource
$context, $userName = null, $search = '',
54 $including = false, $showAll = false
56 $this->setContext( $context );
57 $this->mIncluding
= $including;
58 $this->mShowAll
= $showAll;
60 if ( $userName !== null && $userName !== '' ) {
61 $nt = Title
::makeTitleSafe( NS_USER
, $userName );
62 if ( is_null( $nt ) ) {
63 $this->outputUserDoesNotExist( $userName );
65 $this->mUserName
= $nt->getText();
66 $user = User
::newFromName( $this->mUserName
, false );
70 if ( !$user ||
( $user->isAnon() && !User
::isIP( $user->getName() ) ) ) {
71 $this->outputUserDoesNotExist( $userName );
76 if ( $search !== '' && !$this->getConfig()->get( 'MiserMode' ) ) {
77 $this->mSearch
= $search;
78 $nt = Title
::newFromText( $this->mSearch
);
81 $dbr = wfGetDB( DB_REPLICA
);
82 $this->mQueryConds
[] = 'LOWER(img_name)' .
83 $dbr->buildLike( $dbr->anyString(),
84 strtolower( $nt->getDBkey() ), $dbr->anyString() );
89 if ( $this->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
90 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
92 $this->mDefaultDirection
= IndexPager
::DIR_ASCENDING
;
95 $this->mDefaultDirection
= IndexPager
::DIR_DESCENDING
;
98 parent
::__construct( $context );
102 * Get the user relevant to the ImageList
106 function getRelevantUser() {
111 * Add a message to the output stating that the user doesn't exist
113 * @param string $userName Unescaped user name
115 protected function outputUserDoesNotExist( $userName ) {
116 $this->getOutput()->wrapWikiMsg(
117 "<div class=\"mw-userpage-userdoesnotexist error\">\n$1\n</div>",
119 'listfiles-userdoesnotexist',
120 wfEscapeWikiText( $userName ),
126 * Build the where clause of the query.
128 * Replaces the older mQueryConds member variable.
129 * @param string $table Either "image" or "oldimage"
130 * @return array The query conditions.
132 protected function buildQueryConds( $table ) {
133 $prefix = $table === 'image' ?
'img' : 'oi';
136 if ( !is_null( $this->mUserName
) ) {
137 $conds[$prefix . '_user_text'] = $this->mUserName
;
140 if ( $this->mSearch
!== '' ) {
141 $nt = Title
::newFromText( $this->mSearch
);
143 $dbr = wfGetDB( DB_REPLICA
);
144 $conds[] = 'LOWER(' . $prefix . '_name)' .
145 $dbr->buildLike( $dbr->anyString(),
146 strtolower( $nt->getDBkey() ), $dbr->anyString() );
150 if ( $table === 'oldimage' ) {
151 // Don't want to deal with revdel.
152 // Future fixme: Show partial information as appropriate.
153 // Would have to be careful about filtering by username when username is deleted.
154 $conds['oi_deleted'] = 0;
157 // Add mQueryConds in case anyone was subclassing and using the old variable.
158 return $conds +
$this->mQueryConds
;
164 function getFieldNames() {
165 if ( !$this->mFieldNames
) {
166 $this->mFieldNames
= [
167 'img_timestamp' => $this->msg( 'listfiles_date' )->text(),
168 'img_name' => $this->msg( 'listfiles_name' )->text(),
169 'thumb' => $this->msg( 'listfiles_thumb' )->text(),
170 'img_size' => $this->msg( 'listfiles_size' )->text(),
172 if ( is_null( $this->mUserName
) ) {
173 // Do not show username if filtering by username
174 $this->mFieldNames
['img_user_text'] = $this->msg( 'listfiles_user' )->text();
176 // img_description down here, in order so that its still after the username field.
177 $this->mFieldNames
['img_description'] = $this->msg( 'listfiles_description' )->text();
179 if ( !$this->getConfig()->get( 'MiserMode' ) && !$this->mShowAll
) {
180 $this->mFieldNames
['count'] = $this->msg( 'listfiles_count' )->text();
182 if ( $this->mShowAll
) {
183 $this->mFieldNames
['top'] = $this->msg( 'listfiles-latestversion' )->text();
187 return $this->mFieldNames
;
190 function isFieldSortable( $field ) {
191 if ( $this->mIncluding
) {
194 $sortable = [ 'img_timestamp', 'img_name', 'img_size' ];
195 /* For reference, the indicies we can use for sorting are:
196 * On the image table: img_user_timestamp, img_usertext_timestamp,
197 * img_size, img_timestamp
198 * On oldimage: oi_usertext_timestamp, oi_name_timestamp
200 * In particular that means we cannot sort by timestamp when not filtering
201 * by user and including old images in the results. Which is sad.
203 if ( $this->getConfig()->get( 'MiserMode' ) && !is_null( $this->mUserName
) ) {
204 // If we're sorting by user, the index only supports sorting by time.
205 if ( $field === 'img_timestamp' ) {
210 } elseif ( $this->getConfig()->get( 'MiserMode' )
211 && $this->mShowAll
/* && mUserName === null */
213 // no oi_timestamp index, so only alphabetical sorting in this case.
214 if ( $field === 'img_name' ) {
221 return in_array( $field, $sortable );
224 function getQueryInfo() {
225 // Hacky Hacky Hacky - I want to get query info
226 // for two different tables, without reimplementing
228 $qi = $this->getQueryInfoReal( $this->mTableName
);
234 * Actually get the query info.
236 * This is to allow displaying both stuff from image and oldimage table.
238 * This is a bit hacky.
240 * @param string $table Either 'image' or 'oldimage'
241 * @return array Query info
243 protected function getQueryInfoReal( $table ) {
244 $prefix = $table === 'oldimage' ?
'oi' : 'img';
246 $tables = [ $table ];
247 $fields = $this->getFieldNames();
248 unset( $fields['img_description'] );
249 $fields = array_keys( $fields );
251 if ( $table === 'oldimage' ) {
252 foreach ( $fields as $id => &$field ) {
253 if ( substr( $field, 0, 4 ) !== 'img_' ) {
256 $field = $prefix . substr( $field, 3 ) . ' AS ' . $field;
258 $fields[array_search( 'top', $fields )] = "'no' AS top";
260 if ( $this->mShowAll
) {
261 $fields[array_search( 'top', $fields )] = "'yes' AS top";
264 $fields[] = $prefix . '_user AS img_user';
265 $fields[array_search( 'thumb', $fields )] = $prefix . '_name AS thumb';
267 $options = $join_conds = [];
270 $commentQuery = CommentStore
::getStore()->getJoin( $prefix . '_description' );
271 $tables +
= $commentQuery['tables'];
272 $fields +
= $commentQuery['fields'];
273 $join_conds +
= $commentQuery['joins'];
274 $fields['description_field'] = "'{$prefix}_description'";
276 # Depends on $wgMiserMode
277 # Will also not happen if mShowAll is true.
278 if ( isset( $this->mFieldNames
['count'] ) ) {
279 $tables[] = 'oldimage';
281 # Need to rewrite this one
282 foreach ( $fields as &$field ) {
283 if ( $field == 'count' ) {
284 $field = 'COUNT(oi_archive_name) AS count';
289 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
290 $options = [ 'GROUP BY' => array_merge( [ 'img_user' ], $columnlist ) ];
291 $join_conds['oldimage'] = [ 'LEFT JOIN', 'oi_name = img_name' ];
297 'conds' => $this->buildQueryConds( $table ),
298 'options' => $options,
299 'join_conds' => $join_conds
304 * Override reallyDoQuery to mix together two queries.
306 * @note $asc is named $descending in IndexPager base class. However
307 * it is true when the order is ascending, and false when the order
308 * is descending, so I renamed it to $asc here.
313 * @throws MWException
315 function reallyDoQuery( $offset, $limit, $asc ) {
316 $prevTableName = $this->mTableName
;
317 $this->mTableName
= 'image';
318 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
319 $this->buildQueryInfo( $offset, $limit, $asc );
320 $imageRes = $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
321 $this->mTableName
= $prevTableName;
323 if ( !$this->mShowAll
) {
327 $this->mTableName
= 'oldimage';
330 $oldIndex = $this->mIndexField
;
331 if ( substr( $this->mIndexField
, 0, 4 ) !== 'img_' ) {
332 throw new MWException( "Expected to be sorting on an image table field" );
334 $this->mIndexField
= 'oi_' . substr( $this->mIndexField
, 4 );
336 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
337 $this->buildQueryInfo( $offset, $limit, $asc );
338 $oldimageRes = $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
340 $this->mTableName
= $prevTableName;
341 $this->mIndexField
= $oldIndex;
343 return $this->combineResult( $imageRes, $oldimageRes, $limit, $asc );
347 * Combine results from 2 tables.
349 * Note: This will throw away some results
351 * @param ResultWrapper $res1
352 * @param ResultWrapper $res2
354 * @param bool $ascending See note about $asc in $this->reallyDoQuery
355 * @return FakeResultWrapper $res1 and $res2 combined
357 protected function combineResult( $res1, $res2, $limit, $ascending ) {
360 $topRes1 = $res1->next();
361 $topRes2 = $res2->next();
363 for ( $i = 0; $i < $limit && $topRes1 && $topRes2; $i++
) {
364 if ( strcmp( $topRes1->{$this->mIndexField
}, $topRes2->{$this->mIndexField
} ) > 0 ) {
366 $resultArray[] = $topRes1;
367 $topRes1 = $res1->next();
369 $resultArray[] = $topRes2;
370 $topRes2 = $res2->next();
374 $resultArray[] = $topRes2;
375 $topRes2 = $res2->next();
377 $resultArray[] = $topRes1;
378 $topRes1 = $res1->next();
383 for ( ; $i < $limit && $topRes1; $i++
) {
384 $resultArray[] = $topRes1;
385 $topRes1 = $res1->next();
388 for ( ; $i < $limit && $topRes2; $i++
) {
389 $resultArray[] = $topRes2;
390 $topRes2 = $res2->next();
393 return new FakeResultWrapper( $resultArray );
396 function getDefaultSort() {
397 if ( $this->mShowAll
&& $this->getConfig()->get( 'MiserMode' ) && is_null( $this->mUserName
) ) {
398 // Unfortunately no index on oi_timestamp.
401 return 'img_timestamp';
405 function doBatchLookups() {
407 $this->mResult
->seek( 0 );
408 foreach ( $this->mResult
as $row ) {
409 $userIds[] = $row->img_user
;
411 # Do a link batch query for names and userpages
412 UserCache
::singleton()->doQuery( $userIds, [ 'userpage' ], __METHOD__
);
416 * @param string $field
417 * @param string $value
418 * @return Message|string|int The return type depends on the value of $field:
420 * - img_timestamp: string
422 * - img_user_text: string
424 * - img_description: string
427 * @throws MWException
429 function formatValue( $field, $value ) {
430 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
433 $opt = [ 'time' => wfTimestamp( TS_MW
, $this->mCurrentRow
->img_timestamp
) ];
434 $file = RepoGroup
::singleton()->getLocalRepo()->findFile( $value, $opt );
435 // If statement for paranoia
437 $thumb = $file->transform( [ 'width' => 180, 'height' => 360 ] );
439 return $thumb->toHtml( [ 'desc-link' => true ] );
441 return wfMessage( 'thumbnail_error', '' )->escaped();
444 return htmlspecialchars( $value );
446 case 'img_timestamp':
447 // We may want to make this a link to the "old" version when displaying old files
448 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
450 static $imgfile = null;
451 if ( $imgfile === null ) {
452 $imgfile = $this->msg( 'imgfile' )->text();
455 // Weird files can maybe exist? T24227
456 $filePage = Title
::makeTitleSafe( NS_FILE
, $value );
458 $link = $linkRenderer->makeKnownLink(
462 $download = Xml
::element( 'a',
463 [ 'href' => wfLocalFile( $filePage )->getUrl() ],
466 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
468 // Add delete links if allowed
469 // From https://github.com/Wikia/app/pull/3859
470 if ( $filePage->userCan( 'delete', $this->getUser() ) ) {
471 $deleteMsg = $this->msg( 'listfiles-delete' )->text();
473 $delete = $linkRenderer->makeKnownLink(
474 $filePage, $deleteMsg, [], [ 'action' => 'delete' ]
476 $delete = $this->msg( 'parentheses' )->rawParams( $delete )->escaped();
478 return "$link $download $delete";
481 return "$link $download";
483 return htmlspecialchars( $value );
485 case 'img_user_text':
486 if ( $this->mCurrentRow
->img_user
) {
487 $name = User
::whoIs( $this->mCurrentRow
->img_user
);
488 $link = $linkRenderer->makeLink(
489 Title
::makeTitle( NS_USER
, $name ),
493 $link = htmlspecialchars( $value );
498 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
499 case 'img_description':
500 $field = $this->mCurrentRow
->description_field
;
501 $value = CommentStore
::getStore()->getComment( $field, $this->mCurrentRow
)->text
;
502 return Linker
::formatComment( $value );
504 return $this->getLanguage()->formatNum( intval( $value ) +
1 );
506 // Messages: listfiles-latestversion-yes, listfiles-latestversion-no
507 return $this->msg( 'listfiles-latestversion-' . $value );
509 throw new MWException( "Unknown field '$field'" );
518 'label-message' => 'table_pager_limit_label',
519 'options' => $this->getLimitSelectList(),
520 'default' => $this->mLimit
,
523 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
524 $fields['ilsearch'] = [
526 'name' => 'ilsearch',
527 'id' => 'mw-ilsearch',
528 'label-message' => 'listfiles_search_for',
529 'default' => $this->mSearch
,
531 'maxlength' => '255',
535 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
539 'id' => 'mw-listfiles-user',
540 'label-message' => 'username',
541 'default' => $this->mUserName
,
543 'maxlength' => '255',
544 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
547 $fields['ilshowall'] = [
549 'name' => 'ilshowall',
550 'id' => 'mw-listfiles-show-all',
551 'label-message' => 'listfiles-show-all',
552 'default' => $this->mShowAll
,
555 $query = $this->getRequest()->getQueryValues();
556 unset( $query['title'] );
557 unset( $query['limit'] );
558 unset( $query['ilsearch'] );
559 unset( $query['ilshowall'] );
560 unset( $query['user'] );
562 $form = new HTMLForm( $fields, $this->getContext() );
564 $form->setMethod( 'get' );
565 $form->setTitle( $this->getTitle() );
566 $form->setId( 'mw-listfiles-form' );
567 $form->setWrapperLegendMsg( 'listfiles' );
568 $form->setSubmitTextMsg( 'table_pager_limit_submit' );
569 $form->addHiddenFields( $query );
571 $form->prepareForm();
572 $form->displayForm( '' );
575 protected function getTableClass() {
576 return parent
::getTableClass() . ' listfiles';
579 protected function getNavClass() {
580 return parent
::getNavClass() . ' listfiles_nav';
583 protected function getSortHeaderClass() {
584 return parent
::getSortHeaderClass() . ' listfiles_sort';
587 function getPagingQueries() {
588 $queries = parent
::getPagingQueries();
589 if ( !is_null( $this->mUserName
) ) {
590 # Append the username to the query string
591 foreach ( $queries as &$query ) {
592 if ( $query !== false ) {
593 $query['user'] = $this->mUserName
;
601 function getDefaultQuery() {
602 $queries = parent
::getDefaultQuery();
603 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName
) ) {
604 $queries['user'] = $this->mUserName
;
610 function getTitle() {
611 return SpecialPage
::getTitleFor( 'Listfiles' );