Show the revision list immediately on "umerge" log action links
[lhc/web/wiklou.git] / includes / changes / ChangesList.php
1 <?php
2 /**
3 * Base class for all changes lists.
4 *
5 * The class is used for formatting recent changes, related changes and watchlist.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24
25 class ChangesList extends ContextSource {
26 /**
27 * @var Skin
28 */
29 public $skin;
30
31 protected $watchlist = false;
32 protected $lastdate;
33 protected $message;
34 protected $rc_cache;
35 protected $rcCacheIndex;
36 protected $rclistOpen;
37 protected $rcMoveIndex;
38
39 /**
40 * Changeslist constructor
41 *
42 * @param Skin|IContextSource $obj
43 */
44 public function __construct( $obj ) {
45 if ( $obj instanceof IContextSource ) {
46 $this->setContext( $obj );
47 $this->skin = $obj->getSkin();
48 } else {
49 $this->setContext( $obj->getContext() );
50 $this->skin = $obj;
51 }
52 $this->preCacheMessages();
53 }
54
55 /**
56 * Fetch an appropriate changes list class for the specified context
57 * Some users might want to use an enhanced list format, for instance
58 *
59 * @param IContextSource $context
60 * @return ChangesList
61 */
62 public static function newFromContext( IContextSource $context ) {
63 $user = $context->getUser();
64 $sk = $context->getSkin();
65 $list = null;
66 if ( wfRunHooks( 'FetchChangesList', array( $user, &$sk, &$list ) ) ) {
67 $new = $context->getRequest()->getBool( 'enhanced', $user->getOption( 'usenewrc' ) );
68
69 return $new ? new EnhancedChangesList( $context ) : new OldChangesList( $context );
70 } else {
71 return $list;
72 }
73 }
74
75 /**
76 * Sets the list to use a "<li class='watchlist-(namespace)-(page)'>" tag
77 * @param bool $value
78 */
79 public function setWatchlistDivs( $value = true ) {
80 $this->watchlist = $value;
81 }
82
83 /**
84 * @return bool True when setWatchlistDivs has been called
85 * @since 1.23
86 */
87 public function isWatchlist() {
88 return (bool)$this->watchlist;
89 }
90
91 /**
92 * As we use the same small set of messages in various methods and that
93 * they are called often, we call them once and save them in $this->message
94 */
95 private function preCacheMessages() {
96 if ( !isset( $this->message ) ) {
97 foreach ( array(
98 'cur', 'diff', 'hist', 'enhancedrc-history', 'last', 'blocklink', 'history',
99 'semicolon-separator', 'pipe-separator' ) as $msg
100 ) {
101 $this->message[$msg] = $this->msg( $msg )->escaped();
102 }
103 }
104 }
105
106 /**
107 * Returns the appropriate flags for new page, minor change and patrolling
108 * @param array $flags Associative array of 'flag' => Bool
109 * @param string $nothing To use for empty space
110 * @return string
111 */
112 public function recentChangesFlags( $flags, $nothing = '&#160;' ) {
113 $f = '';
114 foreach ( array_keys( $this->getConfig()->get( 'RecentChangesFlags' ) ) as $flag ) {
115 $f .= isset( $flags[$flag] ) && $flags[$flag]
116 ? self::flag( $flag )
117 : $nothing;
118 }
119
120 return $f;
121 }
122
123 /**
124 * Provide the "<abbr>" element appropriate to a given abbreviated flag,
125 * namely the flag indicating a new page, a minor edit, a bot edit, or an
126 * unpatrolled edit. By default in English it will contain "N", "m", "b",
127 * "!" respectively, plus it will have an appropriate title and class.
128 *
129 * @param string $flag One key of $wgRecentChangesFlags
130 * @return string Raw HTML
131 */
132 public static function flag( $flag ) {
133 static $flagInfos = null;
134 if ( is_null( $flagInfos ) ) {
135 global $wgRecentChangesFlags;
136 $flagInfos = array();
137 foreach ( $wgRecentChangesFlags as $key => $value ) {
138 $flagInfos[$key]['letter'] = wfMessage( $value['letter'] )->escaped();
139 $flagInfos[$key]['title'] = wfMessage( $value['title'] )->escaped();
140 // Allow customized class name, fall back to flag name
141 $flagInfos[$key]['class'] = Sanitizer::escapeClass(
142 isset( $value['class'] ) ? $value['class'] : $key );
143 }
144 }
145
146 // Inconsistent naming, bleh, kepted for b/c
147 $map = array(
148 'minoredit' => 'minor',
149 'botedit' => 'bot',
150 );
151 if ( isset( $map[$flag] ) ) {
152 $flag = $map[$flag];
153 }
154
155 return "<abbr class='" . $flagInfos[$flag]['class'] . "' title='" .
156 $flagInfos[$flag]['title'] . "'>" . $flagInfos[$flag]['letter'] .
157 '</abbr>';
158 }
159
160 /**
161 * Returns text for the start of the tabular part of RC
162 * @return string
163 */
164 public function beginRecentChangesList() {
165 $this->rc_cache = array();
166 $this->rcMoveIndex = 0;
167 $this->rcCacheIndex = 0;
168 $this->lastdate = '';
169 $this->rclistOpen = false;
170 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
171
172 return '<div class="mw-changeslist">';
173 }
174
175 /**
176 * @param ResultWrapper|array $rows
177 */
178 public function initChangesListRows( $rows ) {
179 wfRunHooks( 'ChangesListInitRows', array( $this, $rows ) );
180 }
181
182 /**
183 * Show formatted char difference
184 * @param int $old Number of bytes
185 * @param int $new Number of bytes
186 * @param IContextSource $context
187 * @return string
188 */
189 public static function showCharacterDifference( $old, $new, IContextSource $context = null ) {
190 if ( !$context ) {
191 $context = RequestContext::getMain();
192 }
193
194 $new = (int)$new;
195 $old = (int)$old;
196 $szdiff = $new - $old;
197
198 $lang = $context->getLanguage();
199 $config = $context->getConfig();
200 $code = $lang->getCode();
201 static $fastCharDiff = array();
202 if ( !isset( $fastCharDiff[$code] ) ) {
203 $fastCharDiff[$code] = $config->get( 'MiserMode' ) || $context->msg( 'rc-change-size' )->plain() === '$1';
204 }
205
206 $formattedSize = $lang->formatNum( $szdiff );
207
208 if ( !$fastCharDiff[$code] ) {
209 $formattedSize = $context->msg( 'rc-change-size', $formattedSize )->text();
210 }
211
212 if ( abs( $szdiff ) > abs( $config->get( 'RCChangedSizeThreshold' ) ) ) {
213 $tag = 'strong';
214 } else {
215 $tag = 'span';
216 }
217
218 if ( $szdiff === 0 ) {
219 $formattedSizeClass = 'mw-plusminus-null';
220 } elseif ( $szdiff > 0 ) {
221 $formattedSize = '+' . $formattedSize;
222 $formattedSizeClass = 'mw-plusminus-pos';
223 } else {
224 $formattedSizeClass = 'mw-plusminus-neg';
225 }
226
227 $formattedTotalSize = $context->msg( 'rc-change-size-new' )->numParams( $new )->text();
228
229 return Html::element( $tag,
230 array( 'dir' => 'ltr', 'class' => $formattedSizeClass, 'title' => $formattedTotalSize ),
231 $context->msg( 'parentheses', $formattedSize )->plain() ) . $lang->getDirMark();
232 }
233
234 /**
235 * Format the character difference of one or several changes.
236 *
237 * @param RecentChange $old
238 * @param RecentChange $new Last change to use, if not provided, $old will be used
239 * @return string HTML fragment
240 */
241 public function formatCharacterDifference( RecentChange $old, RecentChange $new = null ) {
242 $oldlen = $old->mAttribs['rc_old_len'];
243
244 if ( $new ) {
245 $newlen = $new->mAttribs['rc_new_len'];
246 } else {
247 $newlen = $old->mAttribs['rc_new_len'];
248 }
249
250 if ( $oldlen === null || $newlen === null ) {
251 return '';
252 }
253
254 return self::showCharacterDifference( $oldlen, $newlen, $this->getContext() );
255 }
256
257 /**
258 * Returns text for the end of RC
259 * @return string
260 */
261 public function endRecentChangesList() {
262 $out = $this->rclistOpen ? "</ul>\n" : '';
263 $out .= '</div>';
264
265 return $out;
266 }
267
268 /**
269 * @param string $s HTML to update
270 * @param mixed $rc_timestamp
271 */
272 public function insertDateHeader( &$s, $rc_timestamp ) {
273 # Make date header if necessary
274 $date = $this->getLanguage()->userDate( $rc_timestamp, $this->getUser() );
275 if ( $date != $this->lastdate ) {
276 if ( $this->lastdate != '' ) {
277 $s .= "</ul>\n";
278 }
279 $s .= Xml::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
280 $this->lastdate = $date;
281 $this->rclistOpen = true;
282 }
283 }
284
285 /**
286 * @param string $s HTML to update
287 * @param Title $title
288 * @param string $logtype
289 */
290 public function insertLog( &$s, $title, $logtype ) {
291 $page = new LogPage( $logtype );
292 $logname = $page->getName()->escaped();
293 $s .= $this->msg( 'parentheses' )->rawParams( Linker::linkKnown( $title, $logname ) )->escaped();
294 }
295
296 /**
297 * @param string $s HTML to update
298 * @param RecentChange $rc
299 * @param bool $unpatrolled
300 */
301 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
302 # Diff link
303 if ( $rc->mAttribs['rc_type'] == RC_NEW || $rc->mAttribs['rc_type'] == RC_LOG ) {
304 $diffLink = $this->message['diff'];
305 } elseif ( !self::userCan( $rc, Revision::DELETED_TEXT, $this->getUser() ) ) {
306 $diffLink = $this->message['diff'];
307 } else {
308 $query = array(
309 'curid' => $rc->mAttribs['rc_cur_id'],
310 'diff' => $rc->mAttribs['rc_this_oldid'],
311 'oldid' => $rc->mAttribs['rc_last_oldid']
312 );
313
314 $diffLink = Linker::linkKnown(
315 $rc->getTitle(),
316 $this->message['diff'],
317 array( 'tabindex' => $rc->counter ),
318 $query
319 );
320 }
321 $diffhist = $diffLink . $this->message['pipe-separator'];
322 # History link
323 $diffhist .= Linker::linkKnown(
324 $rc->getTitle(),
325 $this->message['hist'],
326 array(),
327 array(
328 'curid' => $rc->mAttribs['rc_cur_id'],
329 'action' => 'history'
330 )
331 );
332 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
333 $s .= $this->msg( 'parentheses' )->rawParams( $diffhist )->escaped() .
334 ' <span class="mw-changeslist-separator">. .</span> ';
335 }
336
337 /**
338 * @param string $s HTML to update
339 * @param RecentChange $rc
340 * @param bool $unpatrolled
341 * @param bool $watched
342 */
343 public function insertArticleLink( &$s, &$rc, $unpatrolled, $watched ) {
344 $params = array();
345 if ( $rc->getTitle()->isRedirect() ) {
346 $params = array( 'redirect' => 'no' );
347 }
348
349 $articlelink = Linker::linkKnown(
350 $rc->getTitle(),
351 null,
352 array( 'class' => 'mw-changeslist-title' ),
353 $params
354 );
355 if ( $this->isDeleted( $rc, Revision::DELETED_TEXT ) ) {
356 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
357 }
358 # To allow for boldening pages watched by this user
359 $articlelink = "<span class=\"mw-title\">{$articlelink}</span>";
360 # RTL/LTR marker
361 $articlelink .= $this->getLanguage()->getDirMark();
362
363 wfRunHooks( 'ChangesListInsertArticleLink',
364 array( &$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched ) );
365
366 $s .= " $articlelink";
367 }
368
369 /**
370 * Get the timestamp from $rc formatted with current user's settings
371 * and a separator
372 *
373 * @param RecentChange $rc
374 * @return string HTML fragment
375 */
376 public function getTimestamp( $rc ) {
377 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
378 return $this->message['semicolon-separator'] . '<span class="mw-changeslist-date">' .
379 $this->getLanguage()->userTime(
380 $rc->mAttribs['rc_timestamp'],
381 $this->getUser()
382 ) . '</span> <span class="mw-changeslist-separator">. .</span> ';
383 }
384
385 /**
386 * Insert time timestamp string from $rc into $s
387 *
388 * @param string $s HTML to update
389 * @param RecentChange $rc
390 */
391 public function insertTimestamp( &$s, $rc ) {
392 $s .= $this->getTimestamp( $rc );
393 }
394
395 /**
396 * Insert links to user page, user talk page and eventually a blocking link
397 *
398 * @param string &$s HTML to update
399 * @param RecentChange &$rc
400 */
401 public function insertUserRelatedLinks( &$s, &$rc ) {
402 if ( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
403 $s .= ' <span class="history-deleted">' .
404 $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
405 } else {
406 $s .= $this->getLanguage()->getDirMark() . Linker::userLink( $rc->mAttribs['rc_user'],
407 $rc->mAttribs['rc_user_text'] );
408 $s .= Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
409 }
410 }
411
412 /**
413 * Insert a formatted action
414 *
415 * @param RecentChange $rc
416 * @return string
417 */
418 public function insertLogEntry( $rc ) {
419 $formatter = LogFormatter::newFromRow( $rc->mAttribs );
420 $formatter->setContext( $this->getContext() );
421 $formatter->setShowUserToolLinks( true );
422 $mark = $this->getLanguage()->getDirMark();
423
424 return $formatter->getActionText() . " $mark" . $formatter->getComment();
425 }
426
427 /**
428 * Insert a formatted comment
429 * @param RecentChange $rc
430 * @return string
431 */
432 public function insertComment( $rc ) {
433 if ( $this->isDeleted( $rc, Revision::DELETED_COMMENT ) ) {
434 return ' <span class="history-deleted">' .
435 $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
436 } else {
437 return Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
438 }
439 }
440
441 /**
442 * Check whether to enable recent changes patrol features
443 *
444 * @deprecated since 1.22
445 * @return bool
446 */
447 public static function usePatrol() {
448 global $wgUser;
449
450 wfDeprecated( __METHOD__, '1.22' );
451
452 return $wgUser->useRCPatrol();
453 }
454
455 /**
456 * Returns the string which indicates the number of watching users
457 * @param int $count Number of user watching a page
458 * @return string
459 */
460 protected function numberofWatchingusers( $count ) {
461 static $cache = array();
462 if ( $count > 0 ) {
463 if ( !isset( $cache[$count] ) ) {
464 $cache[$count] = $this->msg( 'number_of_watching_users_RCview' )
465 ->numParams( $count )->escaped();
466 }
467
468 return $cache[$count];
469 } else {
470 return '';
471 }
472 }
473
474 /**
475 * Determine if said field of a revision is hidden
476 * @param RCCacheEntry|RecentChange $rc
477 * @param int $field One of DELETED_* bitfield constants
478 * @return bool
479 */
480 public static function isDeleted( $rc, $field ) {
481 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
482 }
483
484 /**
485 * Determine if the current user is allowed to view a particular
486 * field of this revision, if it's marked as deleted.
487 * @param RCCacheEntry|RecentChange $rc
488 * @param int $field
489 * @param User $user User object to check, or null to use $wgUser
490 * @return bool
491 */
492 public static function userCan( $rc, $field, User $user = null ) {
493 if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
494 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
495 } else {
496 return Revision::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
497 }
498 }
499
500 /**
501 * @param string $link
502 * @param bool $watched
503 * @return string
504 */
505 protected function maybeWatchedLink( $link, $watched = false ) {
506 if ( $watched ) {
507 return '<strong class="mw-watched">' . $link . '</strong>';
508 } else {
509 return '<span class="mw-rc-unwatched">' . $link . '</span>';
510 }
511 }
512
513 /** Inserts a rollback link
514 *
515 * @param string $s
516 * @param RecentChange $rc
517 */
518 public function insertRollback( &$s, &$rc ) {
519 if ( $rc->mAttribs['rc_type'] == RC_EDIT
520 && $rc->mAttribs['rc_this_oldid']
521 && $rc->mAttribs['rc_cur_id']
522 ) {
523 $page = $rc->getTitle();
524 /** Check for rollback and edit permissions, disallow special pages, and only
525 * show a link on the top-most revision */
526 if ( $this->getUser()->isAllowed( 'rollback' )
527 && $rc->mAttribs['page_latest'] == $rc->mAttribs['rc_this_oldid']
528 ) {
529 $rev = new Revision( array(
530 'title' => $page,
531 'id' => $rc->mAttribs['rc_this_oldid'],
532 'user' => $rc->mAttribs['rc_user'],
533 'user_text' => $rc->mAttribs['rc_user_text'],
534 'deleted' => $rc->mAttribs['rc_deleted']
535 ) );
536 $s .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
537 }
538 }
539 }
540
541 /**
542 * @param string $s
543 * @param RecentChange $rc
544 * @param array $classes
545 */
546 public function insertTags( &$s, &$rc, &$classes ) {
547 if ( empty( $rc->mAttribs['ts_tags'] ) ) {
548 return;
549 }
550
551 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
552 $rc->mAttribs['ts_tags'],
553 'changeslist'
554 );
555 $classes = array_merge( $classes, $newClasses );
556 $s .= ' ' . $tagSummary;
557 }
558
559 public function insertExtra( &$s, &$rc, &$classes ) {
560 // Empty, used for subclasses to add anything special.
561 }
562
563 protected function showAsUnpatrolled( RecentChange $rc ) {
564 return self::isUnpatrolled( $rc, $this->getUser() );
565 }
566
567 /**
568 * @param object|RecentChange $rc Database row from recentchanges or a RecentChange object
569 * @param User $user
570 * @return bool
571 */
572 public static function isUnpatrolled( $rc, User $user ) {
573 if ( $rc instanceof RecentChange ) {
574 $isPatrolled = $rc->mAttribs['rc_patrolled'];
575 $rcType = $rc->mAttribs['rc_type'];
576 } else {
577 $isPatrolled = $rc->rc_patrolled;
578 $rcType = $rc->rc_type;
579 }
580
581 if ( !$isPatrolled ) {
582 if ( $user->useRCPatrol() ) {
583 return true;
584 }
585 if ( $user->useNPPatrol() && $rcType == RC_NEW ) {
586 return true;
587 }
588 }
589
590 return false;
591 }
592 }