Merge "Fix the (un)watch token to include the namespace name."
[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 if ( $this->including() ) {
52 $html = $pager->getBody();
53 } else {
54 $form = $pager->getForm();
55 $body = $pager->getBody();
56 $nav = $pager->getNavigationBar();
57 $html = "$form<br />\n$body<br />\n$nav";
58 }
59 $this->getOutput()->addHTML( $html );
60 }
61
62 protected function getGroupName() {
63 return 'media';
64 }
65 }
66
67 /**
68 * @ingroup SpecialPage Pager
69 */
70 class ImageListPager extends TablePager {
71 protected $mFieldNames = null;
72
73 // Subclasses should override buildQueryConds instead of using $mQueryConds variable.
74 protected $mQueryConds = array();
75
76 protected $mUserName = null;
77
78 protected $mSearch = '';
79
80 protected $mIncluding = false;
81
82 protected $mShowAll = false;
83
84 protected $mTableName = 'image';
85
86 function __construct( IContextSource $context, $userName = null, $search = '',
87 $including = false, $showAll = false
88 ) {
89 $this->mIncluding = $including;
90 $this->mShowAll = $showAll;
91
92 if ( $userName ) {
93 $nt = Title::newFromText( $userName, NS_USER );
94 if ( !is_null( $nt ) ) {
95 $this->mUserName = $nt->getText();
96 }
97 }
98
99 if ( $search !== '' && !$this->getConfig()->get( 'MiserMode' ) ) {
100 $this->mSearch = $search;
101 $nt = Title::newFromURL( $this->mSearch );
102
103 if ( $nt ) {
104 $dbr = wfGetDB( DB_SLAVE );
105 $this->mQueryConds[] = 'LOWER(img_name)' .
106 $dbr->buildLike( $dbr->anyString(),
107 strtolower( $nt->getDBkey() ), $dbr->anyString() );
108 }
109 }
110
111 if ( !$including ) {
112 if ( $context->getRequest()->getText( 'sort', 'img_date' ) == 'img_date' ) {
113 $this->mDefaultDirection = true;
114 } else {
115 $this->mDefaultDirection = false;
116 }
117 } else {
118 $this->mDefaultDirection = true;
119 }
120
121 parent::__construct( $context );
122 }
123
124 /**
125 * Build the where clause of the query.
126 *
127 * Replaces the older mQueryConds member variable.
128 * @param string $table Either "image" or "oldimage"
129 * @return array The query conditions.
130 */
131 protected function buildQueryConds( $table ) {
132 $prefix = $table === 'image' ? 'img' : 'oi';
133 $conds = array();
134
135 if ( !is_null( $this->mUserName ) ) {
136 $conds[$prefix . '_user_text'] = $this->mUserName;
137 }
138
139 if ( $this->mSearch !== '' ) {
140 $nt = Title::newFromURL( $this->mSearch );
141 if ( $nt ) {
142 $dbr = wfGetDB( DB_SLAVE );
143 $conds[] = 'LOWER(' . $prefix . '_name)' .
144 $dbr->buildLike( $dbr->anyString(),
145 strtolower( $nt->getDBkey() ), $dbr->anyString() );
146 }
147 }
148
149 if ( $table === 'oldimage' ) {
150 // Don't want to deal with revdel.
151 // Future fixme: Show partial information as appropriate.
152 // Would have to be careful about filtering by username when username is deleted.
153 $conds['oi_deleted'] = 0;
154 }
155
156 // Add mQueryConds in case anyone was subclassing and using the old variable.
157 return $conds + $this->mQueryConds;
158 }
159
160 /**
161 * @return array
162 */
163 function getFieldNames() {
164 if ( !$this->mFieldNames ) {
165 $this->mFieldNames = array(
166 'img_timestamp' => $this->msg( 'listfiles_date' )->text(),
167 'img_name' => $this->msg( 'listfiles_name' )->text(),
168 'thumb' => $this->msg( 'listfiles_thumb' )->text(),
169 'img_size' => $this->msg( 'listfiles_size' )->text(),
170 );
171 if ( is_null( $this->mUserName ) ) {
172 // Do not show username if filtering by username
173 $this->mFieldNames['img_user_text'] = $this->msg( 'listfiles_user' )->text();
174 }
175 // img_description down here, in order so that its still after the username field.
176 $this->mFieldNames['img_description'] = $this->msg( 'listfiles_description' )->text();
177
178 if ( !$this->getConfig()->get( 'MiserMode' ) && !$this->mShowAll ) {
179 $this->mFieldNames['count'] = $this->msg( 'listfiles_count' )->text();
180 }
181 if ( $this->mShowAll ) {
182 $this->mFieldNames['top'] = $this->msg( 'listfiles-latestversion' )->text();
183 }
184 }
185
186 return $this->mFieldNames;
187 }
188
189 function isFieldSortable( $field ) {
190 if ( $this->mIncluding ) {
191 return false;
192 }
193 $sortable = array( 'img_timestamp', 'img_name', 'img_size' );
194 /* For reference, the indicies we can use for sorting are:
195 * On the image table: img_usertext_timestamp, img_size, img_timestamp
196 * On oldimage: oi_usertext_timestamp, oi_name_timestamp
197 *
198 * In particular that means we cannot sort by timestamp when not filtering
199 * by user and including old images in the results. Which is sad.
200 */
201 if ( $this->getConfig()->get( 'MiserMode' ) && !is_null( $this->mUserName ) ) {
202 // If we're sorting by user, the index only supports sorting by time.
203 if ( $field === 'img_timestamp' ) {
204 return true;
205 } else {
206 return false;
207 }
208 } elseif ( $this->getConfig()->get( 'MiserMode' ) && $this->mShowAll /* && mUserName === null */ ) {
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 */
305 function reallyDoQuery( $offset, $limit, $asc ) {
306 $prevTableName = $this->mTableName;
307 $this->mTableName = 'image';
308 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
309 $this->buildQueryInfo( $offset, $limit, $asc );
310 $imageRes = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
311 $this->mTableName = $prevTableName;
312
313 if ( !$this->mShowAll ) {
314 return $imageRes;
315 }
316
317 $this->mTableName = 'oldimage';
318
319 # Hacky...
320 $oldIndex = $this->mIndexField;
321 if ( substr( $this->mIndexField, 0, 4 ) !== 'img_' ) {
322 throw new MWException( "Expected to be sorting on an image table field" );
323 }
324 $this->mIndexField = 'oi_' . substr( $this->mIndexField, 4 );
325
326 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
327 $this->buildQueryInfo( $offset, $limit, $asc );
328 $oldimageRes = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
329
330 $this->mTableName = $prevTableName;
331 $this->mIndexField = $oldIndex;
332
333 return $this->combineResult( $imageRes, $oldimageRes, $limit, $asc );
334 }
335
336 /**
337 * Combine results from 2 tables.
338 *
339 * Note: This will throw away some results
340 *
341 * @param ResultWrapper $res1
342 * @param ResultWrapper $res2
343 * @param int $limit
344 * @param bool $ascending See note about $asc in $this->reallyDoQuery
345 * @return FakeResultWrapper $res1 and $res2 combined
346 */
347 protected function combineResult( $res1, $res2, $limit, $ascending ) {
348 $res1->rewind();
349 $res2->rewind();
350 $topRes1 = $res1->next();
351 $topRes2 = $res2->next();
352 $resultArray = array();
353 for ( $i = 0; $i < $limit && $topRes1 && $topRes2; $i++ ) {
354 if ( strcmp( $topRes1->{$this->mIndexField}, $topRes2->{$this->mIndexField} ) > 0 ) {
355 if ( !$ascending ) {
356 $resultArray[] = $topRes1;
357 $topRes1 = $res1->next();
358 } else {
359 $resultArray[] = $topRes2;
360 $topRes2 = $res2->next();
361 }
362 } else {
363 if ( !$ascending ) {
364 $resultArray[] = $topRes2;
365 $topRes2 = $res2->next();
366 } else {
367 $resultArray[] = $topRes1;
368 $topRes1 = $res1->next();
369 }
370 }
371 }
372
373 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
374 for ( ; $i < $limit && $topRes1; $i++ ) {
375 // @codingStandardsIgnoreEnd
376 $resultArray[] = $topRes1;
377 $topRes1 = $res1->next();
378 }
379
380 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
381 for ( ; $i < $limit && $topRes2; $i++ ) {
382 // @codingStandardsIgnoreEnd
383 $resultArray[] = $topRes2;
384 $topRes2 = $res2->next();
385 }
386
387 return new FakeResultWrapper( $resultArray );
388 }
389
390 function getDefaultSort() {
391 if ( $this->mShowAll && $this->getConfig()->get( 'MiserMode' ) && is_null( $this->mUserName ) ) {
392 // Unfortunately no index on oi_timestamp.
393 return 'img_name';
394 } else {
395 return 'img_timestamp';
396 }
397 }
398
399 function doBatchLookups() {
400 $userIds = array();
401 $this->mResult->seek( 0 );
402 foreach ( $this->mResult as $row ) {
403 $userIds[] = $row->img_user;
404 }
405 # Do a link batch query for names and userpages
406 UserCache::singleton()->doQuery( $userIds, array( 'userpage' ), __METHOD__ );
407 }
408
409 /**
410 * @param string $field
411 * @param string $value
412 * @return Message|string|int The return type depends on the value of $field:
413 * - thumb: string
414 * - img_timestamp: string
415 * - img_name: string
416 * - img_user_text: string
417 * - img_size: string
418 * - img_description: string
419 * - count: int
420 * - top: Message
421 * @throws MWException
422 */
423 function formatValue( $field, $value ) {
424 switch ( $field ) {
425 case 'thumb':
426 $opt = array( 'time' => $this->mCurrentRow->img_timestamp );
427 $file = RepoGroup::singleton()->getLocalRepo()->findFile( $value, $opt );
428 // If statement for paranoia
429 if ( $file ) {
430 $thumb = $file->transform( array( 'width' => 180, 'height' => 360 ) );
431
432 return $thumb->toHtml( array( 'desc-link' => true ) );
433 } else {
434 return htmlspecialchars( $value );
435 }
436 case 'img_timestamp':
437 // We may want to make this a link to the "old" version when displaying old files
438 return htmlspecialchars( $this->getLanguage()->userTimeAndDate( $value, $this->getUser() ) );
439 case 'img_name':
440 static $imgfile = null;
441 if ( $imgfile === null ) {
442 $imgfile = $this->msg( 'imgfile' )->text();
443 }
444
445 // Weird files can maybe exist? Bug 22227
446 $filePage = Title::makeTitleSafe( NS_FILE, $value );
447 if ( $filePage ) {
448 $link = Linker::linkKnown(
449 $filePage,
450 htmlspecialchars( $filePage->getText() )
451 );
452 $download = Xml::element( 'a',
453 array( 'href' => wfLocalFile( $filePage )->getURL() ),
454 $imgfile
455 );
456 $download = $this->msg( 'parentheses' )->rawParams( $download )->escaped();
457
458 // Add delete links if allowed
459 // From https://github.com/Wikia/app/pull/3859
460 if ( $filePage->userCan( 'delete', $this->getUser() ) ) {
461 $deleteMsg = $this->msg( 'listfiles-delete' )->escaped();
462
463 $delete = Linker::linkKnown(
464 $filePage, $deleteMsg, array(), array( 'action' => 'delete' )
465 );
466 $delete = $this->msg( 'parentheses' )->rawParams( $delete )->escaped();
467
468 return "$link $download $delete";
469 }
470
471 return "$link $download";
472 } else {
473 return htmlspecialchars( $value );
474 }
475 case 'img_user_text':
476 if ( $this->mCurrentRow->img_user ) {
477 $name = User::whoIs( $this->mCurrentRow->img_user );
478 $link = Linker::link(
479 Title::makeTitle( NS_USER, $name ),
480 htmlspecialchars( $name )
481 );
482 } else {
483 $link = htmlspecialchars( $value );
484 }
485
486 return $link;
487 case 'img_size':
488 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
489 case 'img_description':
490 return Linker::formatComment( $value );
491 case 'count':
492 return intval( $value ) + 1;
493 case 'top':
494 // Messages: listfiles-latestversion-yes, listfiles-latestversion-no
495 return $this->msg( 'listfiles-latestversion-' . $value );
496 default:
497 throw new MWException( "Unknown field '$field'" );
498 }
499 }
500
501 function getForm() {
502 $inputForm = array();
503 $inputForm['table_pager_limit_label'] = $this->getLimitSelect( array( 'tabindex' => 1 ) );
504 if ( !$this->getConfig()->get( 'MiserMode' ) ) {
505 $inputForm['listfiles_search_for'] = Html::input(
506 'ilsearch',
507 $this->mSearch,
508 'text',
509 array(
510 'size' => '40',
511 'maxlength' => '255',
512 'id' => 'mw-ilsearch',
513 'tabindex' => 2,
514 )
515 );
516 }
517 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
518 'size' => '40',
519 'maxlength' => '255',
520 'id' => 'mw-listfiles-user',
521 'tabindex' => 3,
522 ) );
523
524 $inputForm['listfiles-show-all'] = Html::input( 'ilshowall', 1, 'checkbox', array(
525 'checked' => $this->mShowAll,
526 'tabindex' => 4,
527 ) );
528
529 return Html::openElement( 'form',
530 array( 'method' => 'get', 'action' => wfScript(), 'id' => 'mw-listfiles-form' )
531 ) .
532 Xml::fieldset( $this->msg( 'listfiles' )->text() ) .
533 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
534 Xml::buildForm( $inputForm, 'table_pager_limit_submit', array( 'tabindex' => 5 ) ) .
535 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user', 'title', 'ilshowall' ) ) .
536 Html::closeElement( 'fieldset' ) .
537 Html::closeElement( 'form' ) . "\n";
538 }
539
540 function getTableClass() {
541 return 'listfiles ' . parent::getTableClass();
542 }
543
544 function getNavClass() {
545 return 'listfiles_nav ' . parent::getNavClass();
546 }
547
548 function getSortHeaderClass() {
549 return 'listfiles_sort ' . parent::getSortHeaderClass();
550 }
551
552 function getPagingQueries() {
553 $queries = parent::getPagingQueries();
554 if ( !is_null( $this->mUserName ) ) {
555 # Append the username to the query string
556 foreach ( $queries as &$query ) {
557 if ( $query !== false ) {
558 $query['user'] = $this->mUserName;
559 }
560 }
561 }
562
563 return $queries;
564 }
565
566 function getDefaultQuery() {
567 $queries = parent::getDefaultQuery();
568 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName ) ) {
569 $queries['user'] = $this->mUserName;
570 }
571
572 return $queries;
573 }
574
575 function getTitle() {
576 return SpecialPage::getTitleFor( 'Listfiles' );
577 }
578 }