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