Merge "Mention translatewiki.net on edits only, when edit a default message"
[lhc/web/wiklou.git] / includes / specials / SpecialListfiles.php
1 <?php
2 /**
3 * Implements Special:Listfiles
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 class SpecialListFiles extends IncludableSpecialPage {
25 public function __construct() {
26 parent::__construct( 'Listfiles' );
27 }
28
29 public function execute( $par ) {
30 $this->setHeaders();
31 $this->outputHeader();
32
33 if ( $this->including() ) {
34 $userName = $par;
35 $search = '';
36 $showAll = false;
37 } else {
38 $userName = $this->getRequest()->getText( 'user', $par );
39 $search = $this->getRequest()->getText( 'ilsearch', '' );
40 $showAll = $this->getRequest()->getBool( 'ilshowall', false );
41 }
42
43 $pager = new ImageListPager(
44 $this->getContext(),
45 $userName,
46 $search,
47 $this->including(),
48 $showAll
49 );
50
51 $out = $this->getOutput();
52 if ( $this->including() ) {
53 $out->addParserOutputContent( $pager->getBodyOutput() );
54 } else {
55 $out->addHTML( $pager->getForm() );
56 $out->addParserOutputContent( $pager->getFullOutput() );
57 }
58 }
59
60 protected function getGroupName() {
61 return 'media';
62 }
63 }
64
65 /**
66 * @ingroup SpecialPage Pager
67 */
68 class ImageListPager extends TablePager {
69 protected $mFieldNames = null;
70
71 // Subclasses should override buildQueryConds instead of using $mQueryConds variable.
72 protected $mQueryConds = array();
73
74 protected $mUserName = null;
75
76 protected $mSearch = '';
77
78 protected $mIncluding = false;
79
80 protected $mShowAll = false;
81
82 protected $mTableName = 'image';
83
84 function __construct( IContextSource $context, $userName = null, $search = '',
85 $including = false, $showAll = false
86 ) {
87 $this->mIncluding = $including;
88 $this->mShowAll = $showAll;
89
90 if ( $userName !== null && $userName !== '' ) {
91 $nt = Title::newFromText( $userName, NS_USER );
92 if ( !is_null( $nt ) ) {
93 $this->mUserName = $nt->getText();
94 }
95 }
96
97 if ( $search !== '' && !$this->getConfig()->get( 'MiserMode' ) ) {
98 $this->mSearch = $search;
99 $nt = Title::newFromURL( $this->mSearch );
100
101 if ( $nt ) {
102 $dbr = wfGetDB( DB_SLAVE );
103 $this->mQueryConds[] = 'LOWER(img_name)' .
104 $dbr->buildLike( $dbr->anyString(),
105 strtolower( $nt->getDBkey() ), $dbr->anyString() );
106 }
107 }
108
109 if ( !$including ) {
110 if ( $context->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
111 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
112 } else {
113 $this->mDefaultDirection = IndexPager::DIR_ASCENDING;
114 }
115 } else {
116 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
117 }
118
119 parent::__construct( $context );
120 }
121
122 /**
123 * Build the where clause of the query.
124 *
125 * Replaces the older mQueryConds member variable.
126 * @param string $table Either "image" or "oldimage"
127 * @return array The query conditions.
128 */
129 protected function buildQueryConds( $table ) {
130 $prefix = $table === 'image' ? 'img' : 'oi';
131 $conds = array();
132
133 if ( !is_null( $this->mUserName ) ) {
134 $conds[$prefix . '_user_text'] = $this->mUserName;
135 }
136
137 if ( $this->mSearch !== '' ) {
138 $nt = Title::newFromURL( $this->mSearch );
139 if ( $nt ) {
140 $dbr = wfGetDB( DB_SLAVE );
141 $conds[] = 'LOWER(' . $prefix . '_name)' .
142 $dbr->buildLike( $dbr->anyString(),
143 strtolower( $nt->getDBkey() ), $dbr->anyString() );
144 }
145 }
146
147 if ( $table === 'oldimage' ) {
148 // Don't want to deal with revdel.
149 // Future fixme: Show partial information as appropriate.
150 // Would have to be careful about filtering by username when username is deleted.
151 $conds['oi_deleted'] = 0;
152 }
153
154 // Add mQueryConds in case anyone was subclassing and using the old variable.
155 return $conds + $this->mQueryConds;
156 }
157
158 /**
159 * @return array
160 */
161 function getFieldNames() {
162 if ( !$this->mFieldNames ) {
163 $this->mFieldNames = array(
164 'img_timestamp' => $this->msg( 'listfiles_date' )->text(),
165 'img_name' => $this->msg( 'listfiles_name' )->text(),
166 'thumb' => $this->msg( 'listfiles_thumb' )->text(),
167 'img_size' => $this->msg( 'listfiles_size' )->text(),
168 );
169 if ( is_null( $this->mUserName ) ) {
170 // Do not show username if filtering by username
171 $this->mFieldNames['img_user_text'] = $this->msg( 'listfiles_user' )->text();
172 }
173 // img_description down here, in order so that its still after the username field.
174 $this->mFieldNames['img_description'] = $this->msg( 'listfiles_description' )->text();
175
176 if ( !$this->getConfig()->get( 'MiserMode' ) && !$this->mShowAll ) {
177 $this->mFieldNames['count'] = $this->msg( 'listfiles_count' )->text();
178 }
179 if ( $this->mShowAll ) {
180 $this->mFieldNames['top'] = $this->msg( 'listfiles-latestversion' )->text();
181 }
182 }
183
184 return $this->mFieldNames;
185 }
186
187 function isFieldSortable( $field ) {
188 if ( $this->mIncluding ) {
189 return false;
190 }
191 $sortable = array( 'img_timestamp', 'img_name', 'img_size' );
192 /* For reference, the indicies we can use for sorting are:
193 * On the image table: img_usertext_timestamp, img_size, img_timestamp
194 * On oldimage: oi_usertext_timestamp, oi_name_timestamp
195 *
196 * In particular that means we cannot sort by timestamp when not filtering
197 * by user and including old images in the results. Which is sad.
198 */
199 if ( $this->getConfig()->get( 'MiserMode' ) && !is_null( $this->mUserName ) ) {
200 // If we're sorting by user, the index only supports sorting by time.
201 if ( $field === 'img_timestamp' ) {
202 return true;
203 } else {
204 return false;
205 }
206 } elseif ( $this->getConfig()->get( 'MiserMode' )
207 && $this->mShowAll /* && mUserName === null */
208 ) {
209 // no oi_timestamp index, so only alphabetical sorting in this case.
210 if ( $field === 'img_name' ) {
211 return true;
212 } else {
213 return false;
214 }
215 }
216
217 return in_array( $field, $sortable );
218 }
219
220 function getQueryInfo() {
221 // Hacky Hacky Hacky - I want to get query info
222 // for two different tables, without reimplementing
223 // the pager class.
224 $qi = $this->getQueryInfoReal( $this->mTableName );
225
226 return $qi;
227 }
228
229 /**
230 * Actually get the query info.
231 *
232 * This is to allow displaying both stuff from image and oldimage table.
233 *
234 * This is a bit hacky.
235 *
236 * @param string $table Either 'image' or 'oldimage'
237 * @return array Query info
238 */
239 protected function getQueryInfoReal( $table ) {
240 $prefix = $table === 'oldimage' ? 'oi' : 'img';
241
242 $tables = array( $table );
243 $fields = array_keys( $this->getFieldNames() );
244
245 if ( $table === 'oldimage' ) {
246 foreach ( $fields as $id => &$field ) {
247 if ( substr( $field, 0, 4 ) !== 'img_' ) {
248 continue;
249 }
250 $field = $prefix . substr( $field, 3 ) . ' AS ' . $field;
251 }
252 $fields[array_search( 'top', $fields )] = "'no' AS top";
253 } else {
254 if ( $this->mShowAll ) {
255 $fields[array_search( 'top', $fields )] = "'yes' AS top";
256 }
257 }
258 $fields[] = $prefix . '_user AS img_user';
259 $fields[array_search( 'thumb', $fields )] = $prefix . '_name AS thumb';
260
261 $options = $join_conds = array();
262
263 # Depends on $wgMiserMode
264 # Will also not happen if mShowAll is true.
265 if ( isset( $this->mFieldNames['count'] ) ) {
266 $tables[] = 'oldimage';
267
268 # Need to rewrite this one
269 foreach ( $fields as &$field ) {
270 if ( $field == 'count' ) {
271 $field = 'COUNT(oi_archive_name) AS count';
272 }
273 }
274 unset( $field );
275
276 $dbr = wfGetDB( DB_SLAVE );
277 if ( $dbr->implicitGroupby() ) {
278 $options = array( 'GROUP BY' => 'img_name' );
279 } else {
280 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
281 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
282 }
283 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
284 }
285
286 return array(
287 'tables' => $tables,
288 'fields' => $fields,
289 'conds' => $this->buildQueryConds( $table ),
290 'options' => $options,
291 'join_conds' => $join_conds
292 );
293 }
294
295 /**
296 * Override reallyDoQuery to mix together two queries.
297 *
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.
301 * @param int $offset
302 * @param int $limit
303 * @param bool $asc
304 * @return array
305 */
306 function reallyDoQuery( $offset, $limit, $asc ) {
307 $prevTableName = $this->mTableName;
308 $this->mTableName = 'image';
309 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
310 $this->buildQueryInfo( $offset, $limit, $asc );
311 $imageRes = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
312 $this->mTableName = $prevTableName;
313
314 if ( !$this->mShowAll ) {
315 return $imageRes;
316 }
317
318 $this->mTableName = 'oldimage';
319
320 # Hacky...
321 $oldIndex = $this->mIndexField;
322 if ( substr( $this->mIndexField, 0, 4 ) !== 'img_' ) {
323 throw new MWException( "Expected to be sorting on an image table field" );
324 }
325 $this->mIndexField = 'oi_' . substr( $this->mIndexField, 4 );
326
327 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
328 $this->buildQueryInfo( $offset, $limit, $asc );
329 $oldimageRes = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
330
331 $this->mTableName = $prevTableName;
332 $this->mIndexField = $oldIndex;
333
334 return $this->combineResult( $imageRes, $oldimageRes, $limit, $asc );
335 }
336
337 /**
338 * Combine results from 2 tables.
339 *
340 * Note: This will throw away some results
341 *
342 * @param ResultWrapper $res1
343 * @param ResultWrapper $res2
344 * @param int $limit
345 * @param bool $ascending See note about $asc in $this->reallyDoQuery
346 * @return FakeResultWrapper $res1 and $res2 combined
347 */
348 protected function combineResult( $res1, $res2, $limit, $ascending ) {
349 $res1->rewind();
350 $res2->rewind();
351 $topRes1 = $res1->next();
352 $topRes2 = $res2->next();
353 $resultArray = array();
354 for ( $i = 0; $i < $limit && $topRes1 && $topRes2; $i++ ) {
355 if ( strcmp( $topRes1->{$this->mIndexField}, $topRes2->{$this->mIndexField} ) > 0 ) {
356 if ( !$ascending ) {
357 $resultArray[] = $topRes1;
358 $topRes1 = $res1->next();
359 } else {
360 $resultArray[] = $topRes2;
361 $topRes2 = $res2->next();
362 }
363 } else {
364 if ( !$ascending ) {
365 $resultArray[] = $topRes2;
366 $topRes2 = $res2->next();
367 } else {
368 $resultArray[] = $topRes1;
369 $topRes1 = $res1->next();
370 }
371 }
372 }
373
374 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
375 for ( ; $i < $limit && $topRes1; $i++ ) {
376 // @codingStandardsIgnoreEnd
377 $resultArray[] = $topRes1;
378 $topRes1 = $res1->next();
379 }
380
381 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
382 for ( ; $i < $limit && $topRes2; $i++ ) {
383 // @codingStandardsIgnoreEnd
384 $resultArray[] = $topRes2;
385 $topRes2 = $res2->next();
386 }
387
388 return new FakeResultWrapper( $resultArray );
389 }
390
391 function getDefaultSort() {
392 if ( $this->mShowAll && $this->getConfig()->get( 'MiserMode' ) && is_null( $this->mUserName ) ) {
393 // Unfortunately no index on oi_timestamp.
394 return 'img_name';
395 } else {
396 return 'img_timestamp';
397 }
398 }
399
400 function doBatchLookups() {
401 $userIds = array();
402 $this->mResult->seek( 0 );
403 foreach ( $this->mResult as $row ) {
404 $userIds[] = $row->img_user;
405 }
406 # Do a link batch query for names and userpages
407 UserCache::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__ );
408 }
409
410 /**
411 * @param string $field
412 * @param string $value
413 * @return Message|string|int The return type depends on the value of $field:
414 * - thumb: string
415 * - img_timestamp: string
416 * - img_name: string
417 * - img_user_text: string
418 * - img_size: string
419 * - img_description: string
420 * - count: int
421 * - top: Message
422 * @throws MWException
423 */
424 function formatValue( $field, $value ) {
425 switch ( $field ) {
426 case 'thumb':
427 $opt = array( 'time' => $this->mCurrentRow->img_timestamp );
428 $file = RepoGroup::singleton()->getLocalRepo()->findFile( $value, $opt );
429 // If statement for paranoia
430 if ( $file ) {
431 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
432
433 return $thumb->toHtml( array( 'desc-link' => true ) );
434 } else {
435 return htmlspecialchars( $value );
436 }
437 case 'img_timestamp':
438 // We may want to make this a link to the "old" version when displaying old files
439 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
440 case 'img_name':
441 static $imgfile = null;
442 if ( $imgfile === null ) {
443 $imgfile = $this->msg( 'imgfile' )->text();
444 }
445
446 // Weird files can maybe exist? Bug 22227
447 $filePage = Title::makeTitleSafe( NS_FILE, $value );
448 if ( $filePage ) {
449 $link = Linker::linkKnown(
450 $filePage,
451 htmlspecialchars( $filePage->getText() )
452 );
453 $download = Xml::element( 'a',
454 array( 'href' => wfLocalFile( $filePage )->getURL() ),
455 $imgfile
456 );
457 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
458
459 // Add delete links if allowed
460 // From https://github.com/Wikia/app/pull/3859
461 if ( $filePage->userCan( 'delete', $this->getUser() ) ) {
462 $deleteMsg = $this->msg( 'listfiles-delete' )->escaped();
463
464 $delete = Linker::linkKnown(
465 $filePage, $deleteMsg, array(), array( 'action' => 'delete' )
466 );
467 $delete = $this->msg( 'parentheses' )->rawParams( $delete )->escaped();
468
469 return "$link $download $delete";
470 }
471
472 return "$link $download";
473 } else {
474 return htmlspecialchars( $value );
475 }
476 case 'img_user_text':
477 if ( $this->mCurrentRow->img_user ) {
478 $name = User::whoIs( $this->mCurrentRow->img_user );
479 $link = Linker::link(
480 Title::makeTitle( NS_USER, $name ),
481 htmlspecialchars( $name )
482 );
483 } else {
484 $link = htmlspecialchars( $value );
485 }
486
487 return $link;
488 case 'img_size':
489 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
490 case 'img_description':
491 return Linker::formatComment( $value );
492 case 'count':
493 return intval( $value ) + 1;
494 case 'top':
495 // Messages: listfiles-latestversion-yes, listfiles-latestversion-no
496 return $this->msg( 'listfiles-latestversion-' . $value );
497 default:
498 throw new MWException( "Unknown field '$field'" );
499 }
500 }
501
502 function getForm() {
503 $fields = array();
504 $fields['limit'] = array(
505 'type' => 'select',
506 'name' => 'limit',
507 'label-message' => 'table_pager_limit_label',
508 'options' => $this->getLimitSelectList(),
509 'default' => $this->mLimit,
510 );
511
512 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
513 $fields['ilsearch'] = array(
514 'type' => 'text',
515 'name' => 'ilsearch',
516 'id' => 'mw-ilsearch',
517 'label-message' => 'listfiles_search_for',
518 'default' => $this->mSearch,
519 'size' => '40',
520 'maxlength' => '255',
521 );
522 }
523
524 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
525 $fields['user'] = array(
526 'type' => 'text',
527 'name' => 'user',
528 'id' => 'mw-listfiles-user',
529 'label-message' => 'username',
530 'default' => $this->mUserName,
531 'size' => '40',
532 'maxlength' => '255',
533 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
534 );
535
536 $fields['ilshowall'] = array(
537 'type' => 'check',
538 'name' => 'ilshowall',
539 'id' => 'mw-listfiles-show-all',
540 'label-message' => 'listfiles-show-all',
541 'default' => $this->mShowAll,
542 );
543
544 $query = $this->getRequest()->getQueryValues();
545 unset( $query['title'] );
546 unset( $query['limit'] );
547 unset( $query['ilsearch'] );
548 unset( $query['ilshowall'] );
549 unset( $query['user'] );
550
551 $form = new HTMLForm( $fields, $this->getContext() );
552
553 $form->setMethod( 'get' );
554 $form->setTitle( $this->getTitle() );
555 $form->setId( 'mw-listfiles-form' );
556 $form->setWrapperLegendMsg( 'listfiles' );
557 $form->setSubmitTextMsg( 'table_pager_limit_submit' );
558 $form->addHiddenFields( $query );
559
560 $form->prepareForm();
561 $form->displayForm( '' );
562 }
563
564 function getTableClass() {
565 return parent::getTableClass() . ' listfiles';
566 }
567
568 function getNavClass() {
569 return parent::getNavClass() . ' listfiles_nav';
570 }
571
572 function getSortHeaderClass() {
573 return parent::getSortHeaderClass() . ' listfiles_sort';
574 }
575
576 function getPagingQueries() {
577 $queries = parent::getPagingQueries();
578 if ( !is_null( $this->mUserName ) ) {
579 # Append the username to the query string
580 foreach ( $queries as &$query ) {
581 if ( $query !== false ) {
582 $query['user'] = $this->mUserName;
583 }
584 }
585 }
586
587 return $queries;
588 }
589
590 function getDefaultQuery() {
591 $queries = parent::getDefaultQuery();
592 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName ) ) {
593 $queries['user'] = $this->mUserName;
594 }
595
596 return $queries;
597 }
598
599 function getTitle() {
600 return SpecialPage::getTitleFor( 'Listfiles' );
601 }
602 }