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\IResultWrapper
;
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 // getQueryInfoReal() should have handled the tables and joins.
138 $dbr = wfGetDB( DB_REPLICA
);
139 $actorWhere = ActorMigration
::newMigration()->getWhere(
142 User
::newFromName( $this->mUserName
, false )
144 $conds[] = $actorWhere['conds'];
147 if ( $this->mSearch
!== '' ) {
148 $nt = Title
::newFromText( $this->mSearch
);
150 $dbr = wfGetDB( DB_REPLICA
);
151 $conds[] = 'LOWER(' . $prefix . '_name)' .
152 $dbr->buildLike( $dbr->anyString(),
153 strtolower( $nt->getDBkey() ), $dbr->anyString() );
157 if ( $table === 'oldimage' ) {
158 // Don't want to deal with revdel.
159 // Future fixme: Show partial information as appropriate.
160 // Would have to be careful about filtering by username when username is deleted.
161 $conds['oi_deleted'] = 0;
164 // Add mQueryConds in case anyone was subclassing and using the old variable.
165 return $conds +
$this->mQueryConds
;
169 * The array keys (but not the array values) are used in sql. Phan
170 * gets confused by this, so mark this method as being ok for sql in general.
171 * @return-taint onlysafefor_sql
174 function getFieldNames() {
175 if ( !$this->mFieldNames
) {
176 $this->mFieldNames
= [
177 'img_timestamp' => $this->msg( 'listfiles_date' )->text(),
178 'img_name' => $this->msg( 'listfiles_name' )->text(),
179 'thumb' => $this->msg( 'listfiles_thumb' )->text(),
180 'img_size' => $this->msg( 'listfiles_size' )->text(),
182 if ( is_null( $this->mUserName
) ) {
183 // Do not show username if filtering by username
184 $this->mFieldNames
['img_user_text'] = $this->msg( 'listfiles_user' )->text();
186 // img_description down here, in order so that its still after the username field.
187 $this->mFieldNames
['img_description'] = $this->msg( 'listfiles_description' )->text();
189 if ( !$this->getConfig()->get( 'MiserMode' ) && !$this->mShowAll
) {
190 $this->mFieldNames
['count'] = $this->msg( 'listfiles_count' )->text();
192 if ( $this->mShowAll
) {
193 $this->mFieldNames
['top'] = $this->msg( 'listfiles-latestversion' )->text();
197 return $this->mFieldNames
;
200 function isFieldSortable( $field ) {
201 if ( $this->mIncluding
) {
204 $sortable = [ 'img_timestamp', 'img_name', 'img_size' ];
205 /* For reference, the indicies we can use for sorting are:
206 * On the image table: img_user_timestamp/img_usertext_timestamp/img_actor_timestamp,
207 * img_size, img_timestamp
208 * On oldimage: oi_usertext_timestamp/oi_actor_timestamp, oi_name_timestamp
210 * In particular that means we cannot sort by timestamp when not filtering
211 * by user and including old images in the results. Which is sad.
213 if ( $this->getConfig()->get( 'MiserMode' ) && !is_null( $this->mUserName
) ) {
214 // If we're sorting by user, the index only supports sorting by time.
215 if ( $field === 'img_timestamp' ) {
220 } elseif ( $this->getConfig()->get( 'MiserMode' )
221 && $this->mShowAll
/* && mUserName === null */
223 // no oi_timestamp index, so only alphabetical sorting in this case.
224 if ( $field === 'img_name' ) {
231 return in_array( $field, $sortable );
234 function getQueryInfo() {
235 // Hacky Hacky Hacky - I want to get query info
236 // for two different tables, without reimplementing
238 $qi = $this->getQueryInfoReal( $this->mTableName
);
244 * Actually get the query info.
246 * This is to allow displaying both stuff from image and oldimage table.
248 * This is a bit hacky.
250 * @param string $table Either 'image' or 'oldimage'
251 * @return array Query info
253 protected function getQueryInfoReal( $table ) {
254 $prefix = $table === 'oldimage' ?
'oi' : 'img';
256 $tables = [ $table ];
257 $fields = $this->getFieldNames();
258 unset( $fields['img_description'] );
259 unset( $fields['img_user_text'] );
260 $fields = array_keys( $fields );
262 if ( $table === 'oldimage' ) {
263 foreach ( $fields as $id => &$field ) {
264 if ( substr( $field, 0, 4 ) !== 'img_' ) {
267 $field = $prefix . substr( $field, 3 ) . ' AS ' . $field;
269 $fields[array_search( 'top', $fields )] = "'no' AS top";
271 if ( $this->mShowAll
) {
272 $fields[array_search( 'top', $fields )] = "'yes' AS top";
275 $fields[array_search( 'thumb', $fields )] = $prefix . '_name AS thumb';
277 $options = $join_conds = [];
280 $commentQuery = CommentStore
::getStore()->getJoin( $prefix . '_description' );
281 $tables +
= $commentQuery['tables'];
282 $fields +
= $commentQuery['fields'];
283 $join_conds +
= $commentQuery['joins'];
284 $fields['description_field'] = "'{$prefix}_description'";
287 $actorQuery = ActorMigration
::newMigration()->getJoin( $prefix . '_user' );
288 $tables +
= $actorQuery['tables'];
289 $join_conds +
= $actorQuery['joins'];
290 $fields['img_user'] = $actorQuery['fields'][$prefix . '_user'];
291 $fields['img_user_text'] = $actorQuery['fields'][$prefix . '_user_text'];
292 $fields['img_actor'] = $actorQuery['fields'][$prefix . '_actor'];
294 # Depends on $wgMiserMode
295 # Will also not happen if mShowAll is true.
296 if ( isset( $this->mFieldNames
['count'] ) ) {
297 $tables[] = 'oldimage';
299 # Need to rewrite this one
300 foreach ( $fields as &$field ) {
301 if ( $field == 'count' ) {
302 $field = 'COUNT(oi_archive_name) AS count';
307 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
308 $options = [ 'GROUP BY' => array_merge( [ $fields['img_user'] ], $columnlist ) ];
309 $join_conds['oldimage'] = [ 'LEFT JOIN', 'oi_name = img_name' ];
315 'conds' => $this->buildQueryConds( $table ),
316 'options' => $options,
317 'join_conds' => $join_conds
322 * Override reallyDoQuery to mix together two queries.
324 * @note $asc is named $descending in IndexPager base class. However
325 * it is true when the order is ascending, and false when the order
326 * is descending, so I renamed it to $asc here.
331 * @throws MWException
333 function reallyDoQuery( $offset, $limit, $asc ) {
334 $prevTableName = $this->mTableName
;
335 $this->mTableName
= 'image';
336 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
337 $this->buildQueryInfo( $offset, $limit, $asc );
338 $imageRes = $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
339 $this->mTableName
= $prevTableName;
341 if ( !$this->mShowAll
) {
345 $this->mTableName
= 'oldimage';
348 $oldIndex = $this->mIndexField
;
349 if ( substr( $this->mIndexField
, 0, 4 ) !== 'img_' ) {
350 throw new MWException( "Expected to be sorting on an image table field" );
352 $this->mIndexField
= 'oi_' . substr( $this->mIndexField
, 4 );
354 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
355 $this->buildQueryInfo( $offset, $limit, $asc );
356 $oldimageRes = $this->mDb
->select( $tables, $fields, $conds, $fname, $options, $join_conds );
358 $this->mTableName
= $prevTableName;
359 $this->mIndexField
= $oldIndex;
361 return $this->combineResult( $imageRes, $oldimageRes, $limit, $asc );
365 * Combine results from 2 tables.
367 * Note: This will throw away some results
369 * @param IResultWrapper $res1
370 * @param IResultWrapper $res2
372 * @param bool $ascending See note about $asc in $this->reallyDoQuery
373 * @return FakeResultWrapper $res1 and $res2 combined
375 protected function combineResult( $res1, $res2, $limit, $ascending ) {
378 $topRes1 = $res1->next();
379 $topRes2 = $res2->next();
381 for ( $i = 0; $i < $limit && $topRes1 && $topRes2; $i++
) {
382 if ( strcmp( $topRes1->{$this->mIndexField
}, $topRes2->{$this->mIndexField
} ) > 0 ) {
384 $resultArray[] = $topRes1;
385 $topRes1 = $res1->next();
387 $resultArray[] = $topRes2;
388 $topRes2 = $res2->next();
392 $resultArray[] = $topRes2;
393 $topRes2 = $res2->next();
395 $resultArray[] = $topRes1;
396 $topRes1 = $res1->next();
401 for ( ; $i < $limit && $topRes1; $i++
) {
402 $resultArray[] = $topRes1;
403 $topRes1 = $res1->next();
406 for ( ; $i < $limit && $topRes2; $i++
) {
407 $resultArray[] = $topRes2;
408 $topRes2 = $res2->next();
411 return new FakeResultWrapper( $resultArray );
414 function getDefaultSort() {
415 if ( $this->mShowAll
&& $this->getConfig()->get( 'MiserMode' ) && is_null( $this->mUserName
) ) {
416 // Unfortunately no index on oi_timestamp.
419 return 'img_timestamp';
423 function doBatchLookups() {
425 $this->mResult
->seek( 0 );
426 foreach ( $this->mResult
as $row ) {
427 $userIds[] = $row->img_user
;
429 # Do a link batch query for names and userpages
430 UserCache
::singleton()->doQuery( $userIds, [ 'userpage' ], __METHOD__
);
434 * @param string $field
435 * @param string $value
436 * @return Message|string|int The return type depends on the value of $field:
438 * - img_timestamp: string
440 * - img_user_text: string
442 * - img_description: string
445 * @throws MWException
447 function formatValue( $field, $value ) {
448 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
451 $opt = [ 'time' => wfTimestamp( TS_MW
, $this->mCurrentRow
->img_timestamp
) ];
452 $file = RepoGroup
::singleton()->getLocalRepo()->findFile( $value, $opt );
453 // If statement for paranoia
455 $thumb = $file->transform( [ 'width' => 180, 'height' => 360 ] );
457 return $thumb->toHtml( [ 'desc-link' => true ] );
459 return $this->msg( 'thumbnail_error', '' )->escaped();
462 return htmlspecialchars( $value );
464 case 'img_timestamp':
465 // We may want to make this a link to the "old" version when displaying old files
466 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
468 static $imgfile = null;
469 if ( $imgfile === null ) {
470 $imgfile = $this->msg( 'imgfile' )->text();
473 // Weird files can maybe exist? T24227
474 $filePage = Title
::makeTitleSafe( NS_FILE
, $value );
476 $link = $linkRenderer->makeKnownLink(
480 $download = Xml
::element( 'a',
481 [ 'href' => wfLocalFile( $filePage )->getUrl() ],
484 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
486 // Add delete links if allowed
487 // From https://github.com/Wikia/app/pull/3859
488 if ( $filePage->userCan( 'delete', $this->getUser() ) ) {
489 $deleteMsg = $this->msg( 'listfiles-delete' )->text();
491 $delete = $linkRenderer->makeKnownLink(
492 $filePage, $deleteMsg, [], [ 'action' => 'delete' ]
494 $delete = $this->msg( 'parentheses' )->rawParams( $delete )->escaped();
496 return "$link $download $delete";
499 return "$link $download";
501 return htmlspecialchars( $value );
503 case 'img_user_text':
504 if ( $this->mCurrentRow
->img_user
) {
505 $name = User
::whoIs( $this->mCurrentRow
->img_user
);
506 $link = $linkRenderer->makeLink(
507 Title
::makeTitle( NS_USER
, $name ),
511 $link = htmlspecialchars( $value );
516 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
517 case 'img_description':
518 $field = $this->mCurrentRow
->description_field
;
519 $value = CommentStore
::getStore()->getComment( $field, $this->mCurrentRow
)->text
;
520 return Linker
::formatComment( $value );
522 return $this->getLanguage()->formatNum( intval( $value ) +
1 );
524 // Messages: listfiles-latestversion-yes, listfiles-latestversion-no
525 return $this->msg( 'listfiles-latestversion-' . $value );
527 throw new MWException( "Unknown field '$field'" );
532 $formDescriptor = [];
533 $formDescriptor['limit'] = [
536 'label-message' => 'table_pager_limit_label',
537 'options' => $this->getLimitSelectList(),
538 'default' => $this->mLimit
,
541 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
542 $formDescriptor['ilsearch'] = [
544 'name' => 'ilsearch',
545 'id' => 'mw-ilsearch',
546 'label-message' => 'listfiles_search_for',
547 'default' => $this->mSearch
,
549 'maxlength' => '255',
553 $formDescriptor['user'] = [
556 'id' => 'mw-listfiles-user',
557 'label-message' => 'username',
558 'default' => $this->mUserName
,
560 'maxlength' => '255',
563 $formDescriptor['ilshowall'] = [
565 'name' => 'ilshowall',
566 'id' => 'mw-listfiles-show-all',
567 'label-message' => 'listfiles-show-all',
568 'default' => $this->mShowAll
,
571 $query = $this->getRequest()->getQueryValues();
572 unset( $query['title'] );
573 unset( $query['limit'] );
574 unset( $query['ilsearch'] );
575 unset( $query['ilshowall'] );
576 unset( $query['user'] );
578 $htmlForm = HTMLForm
::factory( 'ooui', $formDescriptor, $this->getContext() );
581 ->setId( 'mw-listfiles-form' )
582 ->setTitle( $this->getTitle() )
583 ->setSubmitTextMsg( 'table_pager_limit_submit' )
584 ->setWrapperLegendMsg( 'listfiles' )
585 ->addHiddenFields( $query )
590 protected function getTableClass() {
591 return parent
::getTableClass() . ' listfiles';
594 protected function getNavClass() {
595 return parent
::getNavClass() . ' listfiles_nav';
598 protected function getSortHeaderClass() {
599 return parent
::getSortHeaderClass() . ' listfiles_sort';
602 function getPagingQueries() {
603 $queries = parent
::getPagingQueries();
604 if ( !is_null( $this->mUserName
) ) {
605 # Append the username to the query string
606 foreach ( $queries as &$query ) {
607 if ( $query !== false ) {
608 $query['user'] = $this->mUserName
;
616 function getDefaultQuery() {
617 $queries = parent
::getDefaultQuery();
618 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName
) ) {
619 $queries['user'] = $this->mUserName
;
625 function getTitle() {
626 return SpecialPage
::getTitleFor( 'Listfiles' );