Switch some HTMLForms in special pages to OOUI
[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->setContext( $context );
88 $this->mIncluding = $including;
89 $this->mShowAll = $showAll;
90
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();
96 }
97 if ( !$user || ( $user->isAnon() && !User::isIP( $user->getName() ) ) ) {
98 $this->getOutput()->wrapWikiMsg(
99 "<div class=\"mw-userpage-userdoesnotexist error\">\n$1\n</div>",
100 array(
101 'listfiles-userdoesnotexist',
102 wfEscapeWikiText( $userName ),
103 )
104 );
105 }
106
107 }
108
109 if ( $search !== '' && !$this->getConfig()->get( 'MiserMode' ) ) {
110 $this->mSearch = $search;
111 $nt = Title::newFromURL( $this->mSearch );
112
113 if ( $nt ) {
114 $dbr = wfGetDB( DB_SLAVE );
115 $this->mQueryConds[] = 'LOWER(img_name)' .
116 $dbr->buildLike( $dbr->anyString(),
117 strtolower( $nt->getDBkey() ), $dbr->anyString() );
118 }
119 }
120
121 if ( !$including ) {
122 if ( $this->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
123 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
124 } else {
125 $this->mDefaultDirection = IndexPager::DIR_ASCENDING;
126 }
127 } else {
128 $this->mDefaultDirection = IndexPager::DIR_DESCENDING;
129 }
130
131 parent::__construct( $context );
132 }
133
134 /**
135 * Build the where clause of the query.
136 *
137 * Replaces the older mQueryConds member variable.
138 * @param string $table Either "image" or "oldimage"
139 * @return array The query conditions.
140 */
141 protected function buildQueryConds( $table ) {
142 $prefix = $table === 'image' ? 'img' : 'oi';
143 $conds = array();
144
145 if ( !is_null( $this->mUserName ) ) {
146 $conds[$prefix . '_user_text'] = $this->mUserName;
147 }
148
149 if ( $this->mSearch !== '' ) {
150 $nt = Title::newFromURL( $this->mSearch );
151 if ( $nt ) {
152 $dbr = wfGetDB( DB_SLAVE );
153 $conds[] = 'LOWER(' . $prefix . '_name)' .
154 $dbr->buildLike( $dbr->anyString(),
155 strtolower( $nt->getDBkey() ), $dbr->anyString() );
156 }
157 }
158
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;
164 }
165
166 // Add mQueryConds in case anyone was subclassing and using the old variable.
167 return $conds + $this->mQueryConds;
168 }
169
170 /**
171 * @return array
172 */
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(),
180 );
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();
184 }
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();
187
188 if ( !$this->getConfig()->get( 'MiserMode' ) && !$this->mShowAll ) {
189 $this->mFieldNames['count'] = $this->msg( 'listfiles_count' )->text();
190 }
191 if ( $this->mShowAll ) {
192 $this->mFieldNames['top'] = $this->msg( 'listfiles-latestversion' )->text();
193 }
194 }
195
196 return $this->mFieldNames;
197 }
198
199 function isFieldSortable( $field ) {
200 if ( $this->mIncluding ) {
201 return false;
202 }
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
207 *
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.
210 */
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' ) {
214 return true;
215 } else {
216 return false;
217 }
218 } elseif ( $this->getConfig()->get( 'MiserMode' )
219 && $this->mShowAll /* && mUserName === null */
220 ) {
221 // no oi_timestamp index, so only alphabetical sorting in this case.
222 if ( $field === 'img_name' ) {
223 return true;
224 } else {
225 return false;
226 }
227 }
228
229 return in_array( $field, $sortable );
230 }
231
232 function getQueryInfo() {
233 // Hacky Hacky Hacky - I want to get query info
234 // for two different tables, without reimplementing
235 // the pager class.
236 $qi = $this->getQueryInfoReal( $this->mTableName );
237
238 return $qi;
239 }
240
241 /**
242 * Actually get the query info.
243 *
244 * This is to allow displaying both stuff from image and oldimage table.
245 *
246 * This is a bit hacky.
247 *
248 * @param string $table Either 'image' or 'oldimage'
249 * @return array Query info
250 */
251 protected function getQueryInfoReal( $table ) {
252 $prefix = $table === 'oldimage' ? 'oi' : 'img';
253
254 $tables = array( $table );
255 $fields = array_keys( $this->getFieldNames() );
256
257 if ( $table === 'oldimage' ) {
258 foreach ( $fields as $id => &$field ) {
259 if ( substr( $field, 0, 4 ) !== 'img_' ) {
260 continue;
261 }
262 $field = $prefix . substr( $field, 3 ) . ' AS ' . $field;
263 }
264 $fields[array_search( 'top', $fields )] = "'no' AS top";
265 } else {
266 if ( $this->mShowAll ) {
267 $fields[array_search( 'top', $fields )] = "'yes' AS top";
268 }
269 }
270 $fields[] = $prefix . '_user AS img_user';
271 $fields[array_search( 'thumb', $fields )] = $prefix . '_name AS thumb';
272
273 $options = $join_conds = array();
274
275 # Depends on $wgMiserMode
276 # Will also not happen if mShowAll is true.
277 if ( isset( $this->mFieldNames['count'] ) ) {
278 $tables[] = 'oldimage';
279
280 # Need to rewrite this one
281 foreach ( $fields as &$field ) {
282 if ( $field == 'count' ) {
283 $field = 'COUNT(oi_archive_name) AS count';
284 }
285 }
286 unset( $field );
287
288 $dbr = wfGetDB( DB_SLAVE );
289 if ( $dbr->implicitGroupby() ) {
290 $options = array( 'GROUP BY' => 'img_name' );
291 } else {
292 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
293 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
294 }
295 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
296 }
297
298 return array(
299 'tables' => $tables,
300 'fields' => $fields,
301 'conds' => $this->buildQueryConds( $table ),
302 'options' => $options,
303 'join_conds' => $join_conds
304 );
305 }
306
307 /**
308 * Override reallyDoQuery to mix together two queries.
309 *
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.
313 * @param int $offset
314 * @param int $limit
315 * @param bool $asc
316 * @return array
317 * @throws MWException
318 */
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;
326
327 if ( !$this->mShowAll ) {
328 return $imageRes;
329 }
330
331 $this->mTableName = 'oldimage';
332
333 # Hacky...
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" );
337 }
338 $this->mIndexField = 'oi_' . substr( $this->mIndexField, 4 );
339
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 );
343
344 $this->mTableName = $prevTableName;
345 $this->mIndexField = $oldIndex;
346
347 return $this->combineResult( $imageRes, $oldimageRes, $limit, $asc );
348 }
349
350 /**
351 * Combine results from 2 tables.
352 *
353 * Note: This will throw away some results
354 *
355 * @param ResultWrapper $res1
356 * @param ResultWrapper $res2
357 * @param int $limit
358 * @param bool $ascending See note about $asc in $this->reallyDoQuery
359 * @return FakeResultWrapper $res1 and $res2 combined
360 */
361 protected function combineResult( $res1, $res2, $limit, $ascending ) {
362 $res1->rewind();
363 $res2->rewind();
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 ) {
369 if ( !$ascending ) {
370 $resultArray[] = $topRes1;
371 $topRes1 = $res1->next();
372 } else {
373 $resultArray[] = $topRes2;
374 $topRes2 = $res2->next();
375 }
376 } else {
377 if ( !$ascending ) {
378 $resultArray[] = $topRes2;
379 $topRes2 = $res2->next();
380 } else {
381 $resultArray[] = $topRes1;
382 $topRes1 = $res1->next();
383 }
384 }
385 }
386
387 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
388 for ( ; $i < $limit && $topRes1; $i++ ) {
389 // @codingStandardsIgnoreEnd
390 $resultArray[] = $topRes1;
391 $topRes1 = $res1->next();
392 }
393
394 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
395 for ( ; $i < $limit && $topRes2; $i++ ) {
396 // @codingStandardsIgnoreEnd
397 $resultArray[] = $topRes2;
398 $topRes2 = $res2->next();
399 }
400
401 return new FakeResultWrapper( $resultArray );
402 }
403
404 function getDefaultSort() {
405 if ( $this->mShowAll && $this->getConfig()->get( 'MiserMode' ) && is_null( $this->mUserName ) ) {
406 // Unfortunately no index on oi_timestamp.
407 return 'img_name';
408 } else {
409 return 'img_timestamp';
410 }
411 }
412
413 function doBatchLookups() {
414 $userIds = array();
415 $this->mResult->seek( 0 );
416 foreach ( $this->mResult as $row ) {
417 $userIds[] = $row->img_user;
418 }
419 # Do a link batch query for names and userpages
420 UserCache::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__ );
421 }
422
423 /**
424 * @param string $field
425 * @param string $value
426 * @return Message|string|int The return type depends on the value of $field:
427 * - thumb: string
428 * - img_timestamp: string
429 * - img_name: string
430 * - img_user_text: string
431 * - img_size: string
432 * - img_description: string
433 * - count: int
434 * - top: Message
435 * @throws MWException
436 */
437 function formatValue( $field, $value ) {
438 switch ( $field ) {
439 case 'thumb':
440 $opt = array( 'time' => wfTimestamp( TS_MW, $this->mCurrentRow->img_timestamp ) );
441 $file = RepoGroup::singleton()->getLocalRepo()->findFile( $value, $opt );
442 // If statement for paranoia
443 if ( $file ) {
444 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
445
446 return $thumb->toHtml( array( 'desc-link' => true ) );
447 } else {
448 return htmlspecialchars( $value );
449 }
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() ) );
453 case 'img_name':
454 static $imgfile = null;
455 if ( $imgfile === null ) {
456 $imgfile = $this->msg( 'imgfile' )->text();
457 }
458
459 // Weird files can maybe exist? Bug 22227
460 $filePage = Title::makeTitleSafe( NS_FILE, $value );
461 if ( $filePage ) {
462 $link = Linker::linkKnown(
463 $filePage,
464 htmlspecialchars( $filePage->getText() )
465 );
466 $download = Xml::element( 'a',
467 array( 'href' => wfLocalFile( $filePage )->getURL() ),
468 $imgfile
469 );
470 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
471
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();
476
477 $delete = Linker::linkKnown(
478 $filePage, $deleteMsg, array(), array( 'action' => 'delete' )
479 );
480 $delete = $this->msg( 'parentheses' )->rawParams( $delete )->escaped();
481
482 return "$link $download $delete";
483 }
484
485 return "$link $download";
486 } else {
487 return htmlspecialchars( $value );
488 }
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 )
495 );
496 } else {
497 $link = htmlspecialchars( $value );
498 }
499
500 return $link;
501 case 'img_size':
502 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
503 case 'img_description':
504 return Linker::formatComment( $value );
505 case 'count':
506 return intval( $value ) + 1;
507 case 'top':
508 // Messages: listfiles-latestversion-yes, listfiles-latestversion-no
509 return $this->msg( 'listfiles-latestversion-' . $value );
510 default:
511 throw new MWException( "Unknown field '$field'" );
512 }
513 }
514
515 function getForm() {
516 $fields = array();
517 $fields['limit'] = array(
518 'type' => 'select',
519 'name' => 'limit',
520 'label-message' => 'table_pager_limit_label',
521 'options' => $this->getLimitSelectList(),
522 'default' => $this->mLimit,
523 );
524
525 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
526 $fields['ilsearch'] = array(
527 'type' => 'text',
528 'name' => 'ilsearch',
529 'id' => 'mw-ilsearch',
530 'label-message' => 'listfiles_search_for',
531 'default' => $this->mSearch,
532 'size' => '40',
533 'maxlength' => '255',
534 );
535 }
536
537 $this->getOutput()->addModules( 'mediawiki.userSuggest' );
538 $fields['user'] = array(
539 'type' => 'text',
540 'name' => 'user',
541 'id' => 'mw-listfiles-user',
542 'label-message' => 'username',
543 'default' => $this->mUserName,
544 'size' => '40',
545 'maxlength' => '255',
546 'cssclass' => 'mw-autocomplete-user', // used by mediawiki.userSuggest
547 );
548
549 $fields['ilshowall'] = array(
550 'type' => 'check',
551 'name' => 'ilshowall',
552 'id' => 'mw-listfiles-show-all',
553 'label-message' => 'listfiles-show-all',
554 'default' => $this->mShowAll,
555 );
556
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'] );
563
564 $form = new HTMLForm( $fields, $this->getContext() );
565
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 );
572
573 $form->prepareForm();
574 $form->displayForm( '' );
575 }
576
577 function getTableClass() {
578 return parent::getTableClass() . ' listfiles';
579 }
580
581 function getNavClass() {
582 return parent::getNavClass() . ' listfiles_nav';
583 }
584
585 function getSortHeaderClass() {
586 return parent::getSortHeaderClass() . ' listfiles_sort';
587 }
588
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;
596 }
597 }
598 }
599
600 return $queries;
601 }
602
603 function getDefaultQuery() {
604 $queries = parent::getDefaultQuery();
605 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName ) ) {
606 $queries['user'] = $this->mUserName;
607 }
608
609 return $queries;
610 }
611
612 function getTitle() {
613 return SpecialPage::getTitleFor( 'Listfiles' );
614 }
615 }