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