Merge "Several tweaks to the install.php script"
[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 'img_user_text' => $this->msg( 'listfiles_user' )->text(),
174 'img_description' => $this->msg( 'listfiles_description' )->text(),
175 );
176 if ( !$wgMiserMode && !$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 global $wgMiserMode;
189 if ( $this->mIncluding ) {
190 return false;
191 }
192 $sortable = array( 'img_timestamp', 'img_name', 'img_size' );
193 /* For reference, the indicies we can use for sorting are:
194 * On the image table: img_usertext_timestamp, img_size, img_timestamp
195 * On oldimage: oi_usertext_timestamp, oi_name_timestamp
196 *
197 * In particular that means we cannot sort by timestamp when not filtering
198 * by user and including old images in the results. Which is sad.
199 */
200 if ( $wgMiserMode && !is_null( $this->mUserName ) ) {
201 // If we're sorting by user, the index only supports sorting by time.
202 if ( $field === 'img_timestamp' ) {
203 return true;
204 } else {
205 return false;
206 }
207 } elseif ( $wgMiserMode && $this->mShowAll /* && mUserName === null */ ) {
208 // no oi_timestamp index, so only alphabetical sorting in this case.
209 if ( $field === 'img_name' ) {
210 return true;
211 } else {
212 return false;
213 }
214 }
215
216 return in_array( $field, $sortable );
217 }
218
219 function getQueryInfo() {
220 // Hacky Hacky Hacky - I want to get query info
221 // for two different tables, without reimplementing
222 // the pager class.
223 $qi = $this->getQueryInfoReal( $this->mTableName );
224
225 return $qi;
226 }
227
228 /**
229 * Actually get the query info.
230 *
231 * This is to allow displaying both stuff from image and oldimage table.
232 *
233 * This is a bit hacky.
234 *
235 * @param string $table Either 'image' or 'oldimage'
236 * @return array Query info
237 */
238 protected function getQueryInfoReal( $table ) {
239 $prefix = $table === 'oldimage' ? 'oi' : 'img';
240
241 $tables = array( $table );
242 $fields = array_keys( $this->getFieldNames() );
243
244 if ( $table === 'oldimage' ) {
245 foreach ( $fields as $id => &$field ) {
246 if ( substr( $field, 0, 4 ) !== 'img_' ) {
247 continue;
248 }
249 $field = $prefix . substr( $field, 3 ) . ' AS ' . $field;
250 }
251 $fields[array_search( 'top', $fields )] = "'no' AS top";
252 } else {
253 if ( $this->mShowAll ) {
254 $fields[array_search( 'top', $fields )] = "'yes' AS top";
255 }
256 }
257 $fields[] = $prefix . '_user AS img_user';
258 $fields[array_search( 'thumb', $fields )] = $prefix . '_name AS thumb';
259
260 $options = $join_conds = array();
261
262 # Depends on $wgMiserMode
263 # Will also not happen if mShowAll is true.
264 if ( isset( $this->mFieldNames['count'] ) ) {
265 $tables[] = 'oldimage';
266
267 # Need to rewrite this one
268 foreach ( $fields as &$field ) {
269 if ( $field == 'count' ) {
270 $field = 'COUNT(oi_archive_name) AS count';
271 }
272 }
273 unset( $field );
274
275 $dbr = wfGetDB( DB_SLAVE );
276 if ( $dbr->implicitGroupby() ) {
277 $options = array( 'GROUP BY' => 'img_name' );
278 } else {
279 $columnlist = preg_grep( '/^img/', array_keys( $this->getFieldNames() ) );
280 $options = array( 'GROUP BY' => array_merge( array( 'img_user' ), $columnlist ) );
281 }
282 $join_conds = array( 'oldimage' => array( 'LEFT JOIN', 'oi_name = img_name' ) );
283 }
284
285 return array(
286 'tables' => $tables,
287 'fields' => $fields,
288 'conds' => $this->buildQueryConds( $table ),
289 'options' => $options,
290 'join_conds' => $join_conds
291 );
292 }
293
294 /**
295 * Override reallyDoQuery to mix together two queries.
296 *
297 * @note $asc is named $descending in IndexPager base class. However
298 * it is true when the order is ascending, and false when the order
299 * is descending, so I renamed it to $asc here.
300 * @param int $offset
301 * @param int $limit
302 * @param bool $asc
303 */
304 function reallyDoQuery( $offset, $limit, $asc ) {
305 $prevTableName = $this->mTableName;
306 $this->mTableName = 'image';
307 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
308 $this->buildQueryInfo( $offset, $limit, $asc );
309 $imageRes = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
310 $this->mTableName = $prevTableName;
311
312 if ( !$this->mShowAll ) {
313 return $imageRes;
314 }
315
316 $this->mTableName = 'oldimage';
317
318 # Hacky...
319 $oldIndex = $this->mIndexField;
320 if ( substr( $this->mIndexField, 0, 4 ) !== 'img_' ) {
321 throw new MWException( "Expected to be sorting on an image table field" );
322 }
323 $this->mIndexField = 'oi_' . substr( $this->mIndexField, 4 );
324
325 list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
326 $this->buildQueryInfo( $offset, $limit, $asc );
327 $oldimageRes = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
328
329 $this->mTableName = $prevTableName;
330 $this->mIndexField = $oldIndex;
331
332 return $this->combineResult( $imageRes, $oldimageRes, $limit, $asc );
333 }
334
335 /**
336 * Combine results from 2 tables.
337 *
338 * Note: This will throw away some results
339 *
340 * @param ResultWrapper $res1
341 * @param ResultWrapper $res2
342 * @param int $limit
343 * @param bool $ascending See note about $asc in $this->reallyDoQuery
344 * @return FakeResultWrapper $res1 and $res2 combined
345 */
346 protected function combineResult( $res1, $res2, $limit, $ascending ) {
347 $res1->rewind();
348 $res2->rewind();
349 $topRes1 = $res1->next();
350 $topRes2 = $res2->next();
351 $resultArray = array();
352 for ( $i = 0; $i < $limit && $topRes1 && $topRes2; $i++ ) {
353 if ( strcmp( $topRes1->{$this->mIndexField}, $topRes2->{$this->mIndexField} ) > 0 ) {
354 if ( !$ascending ) {
355 $resultArray[] = $topRes1;
356 $topRes1 = $res1->next();
357 } else {
358 $resultArray[] = $topRes2;
359 $topRes2 = $res2->next();
360 }
361 } else {
362 if ( !$ascending ) {
363 $resultArray[] = $topRes2;
364 $topRes2 = $res2->next();
365 } else {
366 $resultArray[] = $topRes1;
367 $topRes1 = $res1->next();
368 }
369 }
370 }
371
372 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
373 for ( ; $i < $limit && $topRes1; $i++ ) {
374 // @codingStandardsIgnoreEnd
375 $resultArray[] = $topRes1;
376 $topRes1 = $res1->next();
377 }
378
379 // @codingStandardsIgnoreStart Squiz.WhiteSpace.SemicolonSpacing.Incorrect
380 for ( ; $i < $limit && $topRes2; $i++ ) {
381 // @codingStandardsIgnoreEnd
382 $resultArray[] = $topRes2;
383 $topRes2 = $res2->next();
384 }
385
386 return new FakeResultWrapper( $resultArray );
387 }
388
389 function getDefaultSort() {
390 global $wgMiserMode;
391 if ( $this->mShowAll && $wgMiserMode && 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 return "$link $download";
459 } else {
460 return htmlspecialchars( $value );
461 }
462 case 'img_user_text':
463 if ( $this->mCurrentRow->img_user ) {
464 $name = User::whoIs( $this->mCurrentRow->img_user );
465 $link = Linker::link(
466 Title::makeTitle( NS_USER, $name ),
467 htmlspecialchars( $name )
468 );
469 } else {
470 $link = htmlspecialchars( $value );
471 }
472
473 return $link;
474 case 'img_size':
475 return htmlspecialchars( $this->getLanguage()->formatSize( $value ) );
476 case 'img_description':
477 return Linker::formatComment( $value );
478 case 'count':
479 return intval( $value ) + 1;
480 case 'top':
481 // Messages: listfiles-latestversion-yes, listfiles-latestversion-no
482 return $this->msg( 'listfiles-latestversion-' . $value );
483 default:
484 throw new MWException( "Unknown field '$field'" );
485 }
486 }
487
488 function getForm() {
489 global $wgScript, $wgMiserMode;
490 $inputForm = array();
491 $inputForm['table_pager_limit_label'] = $this->getLimitSelect( array( 'tabindex' => 1 ) );
492 if ( !$wgMiserMode ) {
493 $inputForm['listfiles_search_for'] = Html::input(
494 'ilsearch',
495 $this->mSearch,
496 'text',
497 array(
498 'size' => '40',
499 'maxlength' => '255',
500 'id' => 'mw-ilsearch',
501 'tabindex' => 2,
502 )
503 );
504 }
505 $inputForm['username'] = Html::input( 'user', $this->mUserName, 'text', array(
506 'size' => '40',
507 'maxlength' => '255',
508 'id' => 'mw-listfiles-user',
509 'tabindex' => 3,
510 ) );
511
512 $inputForm['listfiles-show-all'] = Html::input( 'ilshowall', 1, 'checkbox', array(
513 'checked' => $this->mShowAll,
514 'tabindex' => 4,
515 ) );
516
517 return Html::openElement( 'form',
518 array( 'method' => 'get', 'action' => $wgScript, 'id' => 'mw-listfiles-form' )
519 ) .
520 Xml::fieldset( $this->msg( 'listfiles' )->text() ) .
521 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
522 Xml::buildForm( $inputForm, 'table_pager_limit_submit', array( 'tabindex' => 5 ) ) .
523 $this->getHiddenFields( array( 'limit', 'ilsearch', 'user', 'title', 'ilshowall' ) ) .
524 Html::closeElement( 'fieldset' ) .
525 Html::closeElement( 'form' ) . "\n";
526 }
527
528 function getTableClass() {
529 return 'listfiles ' . parent::getTableClass();
530 }
531
532 function getNavClass() {
533 return 'listfiles_nav ' . parent::getNavClass();
534 }
535
536 function getSortHeaderClass() {
537 return 'listfiles_sort ' . parent::getSortHeaderClass();
538 }
539
540 function getPagingQueries() {
541 $queries = parent::getPagingQueries();
542 if ( !is_null( $this->mUserName ) ) {
543 # Append the username to the query string
544 foreach ( $queries as &$query ) {
545 $query['user'] = $this->mUserName;
546 }
547 }
548
549 return $queries;
550 }
551
552 function getDefaultQuery() {
553 $queries = parent::getDefaultQuery();
554 if ( !isset( $queries['user'] ) && !is_null( $this->mUserName ) ) {
555 $queries['user'] = $this->mUserName;
556 }
557
558 return $queries;
559 }
560
561 function getTitle() {
562 return SpecialPage::getTitleFor( 'Listfiles' );
563 }
564 }