3 * Base class for all changes lists.
5 * The class is used for formatting recent changes, related changes and watchlist.
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.
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.
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
25 class ChangesList
extends ContextSource
{
32 protected $watchlist = false;
37 * Changeslist constructor
39 * @param $obj Skin or IContextSource
41 public function __construct( $obj ) {
42 if ( $obj instanceof IContextSource
) {
43 $this->setContext( $obj );
44 $this->skin
= $obj->getSkin();
46 $this->setContext( $obj->getContext() );
49 $this->preCacheMessages();
53 * Fetch an appropriate changes list class for the specified context
54 * Some users might want to use an enhanced list format, for instance
56 * @param $context IContextSource to use
57 * @return ChangesList|EnhancedChangesList|OldChangesList derivative
59 public static function newFromContext( IContextSource
$context ) {
60 $user = $context->getUser();
61 $sk = $context->getSkin();
63 if ( wfRunHooks( 'FetchChangesList', array( $user, &$sk, &$list ) ) ) {
64 $new = $context->getRequest()->getBool( 'enhanced', $user->getOption( 'usenewrc' ) );
65 return $new ?
new EnhancedChangesList( $context ) : new OldChangesList( $context );
72 * Sets the list to use a "<li class='watchlist-(namespace)-(page)'>" tag
73 * @param $value Boolean
75 public function setWatchlistDivs( $value = true ) {
76 $this->watchlist
= $value;
80 * As we use the same small set of messages in various methods and that
81 * they are called often, we call them once and save them in $this->message
83 private function preCacheMessages() {
84 if ( !isset( $this->message
) ) {
86 'cur', 'diff', 'hist', 'enhancedrc-history', 'last', 'blocklink', 'history',
87 'semicolon-separator', 'pipe-separator' ) as $msg
89 $this->message
[$msg] = $this->msg( $msg )->escaped();
95 * Returns the appropriate flags for new page, minor change and patrolling
96 * @param array $flags Associative array of 'flag' => Bool
97 * @param string $nothing to use for empty space
100 public function recentChangesFlags( $flags, $nothing = ' ' ) {
101 global $wgRecentChangesFlags;
103 foreach ( array_keys( $wgRecentChangesFlags ) as $flag ) {
104 $f .= isset( $flags[$flag] ) && $flags[$flag]
105 ? self
::flag( $flag )
112 * Provide the "<abbr>" element appropriate to a given abbreviated flag,
113 * namely the flag indicating a new page, a minor edit, a bot edit, or an
114 * unpatrolled edit. By default in English it will contain "N", "m", "b",
115 * "!" respectively, plus it will have an appropriate title and class.
117 * @param string $flag One key of $wgRecentChangesFlags
118 * @return String: Raw HTML
120 public static function flag( $flag ) {
121 static $flagInfos = null;
122 if ( is_null( $flagInfos ) ) {
123 global $wgRecentChangesFlags;
124 $flagInfos = array();
125 foreach ( $wgRecentChangesFlags as $key => $value ) {
126 $flagInfos[$key]['letter'] = wfMessage( $value['letter'] )->escaped();
127 $flagInfos[$key]['title'] = wfMessage( $value['title'] )->escaped();
128 // Allow customized class name, fall back to flag name
129 $flagInfos[$key]['class'] = Sanitizer
::escapeClass(
130 isset( $value['class'] ) ?
$value['class'] : $key );
134 // Inconsistent naming, bleh, kepted for b/c
136 'minoredit' => 'minor',
139 if ( isset( $map[$flag] ) ) {
143 return "<abbr class='" . $flagInfos[$flag]['class'] . "' title='" . $flagInfos[$flag]['title'] . "'>" .
144 $flagInfos[$flag]['letter'] .
149 * Returns text for the start of the tabular part of RC
152 public function beginRecentChangesList() {
153 $this->rc_cache
= array();
154 $this->rcMoveIndex
= 0;
155 $this->rcCacheIndex
= 0;
156 $this->lastdate
= '';
157 $this->rclistOpen
= false;
158 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
163 * Show formatted char difference
164 * @param $old Integer: bytes
165 * @param $new Integer: bytes
166 * @param $context IContextSource context to use
169 public static function showCharacterDifference( $old, $new, IContextSource
$context = null ) {
170 global $wgRCChangedSizeThreshold, $wgMiserMode;
173 $context = RequestContext
::getMain();
178 $szdiff = $new - $old;
180 $lang = $context->getLanguage();
181 $code = $lang->getCode();
182 static $fastCharDiff = array();
183 if ( !isset( $fastCharDiff[$code] ) ) {
184 $fastCharDiff[$code] = $wgMiserMode ||
$context->msg( 'rc-change-size' )->plain() === '$1';
187 $formattedSize = $lang->formatNum( $szdiff );
189 if ( !$fastCharDiff[$code] ) {
190 $formattedSize = $context->msg( 'rc-change-size', $formattedSize )->text();
193 if ( abs( $szdiff ) > abs( $wgRCChangedSizeThreshold ) ) {
199 if ( $szdiff === 0 ) {
200 $formattedSizeClass = 'mw-plusminus-null';
203 $formattedSize = '+' . $formattedSize;
204 $formattedSizeClass = 'mw-plusminus-pos';
207 $formattedSizeClass = 'mw-plusminus-neg';
210 $formattedTotalSize = $context->msg( 'rc-change-size-new' )->numParams( $new )->text();
212 return Html
::element( $tag,
213 array( 'dir' => 'ltr', 'class' => $formattedSizeClass, 'title' => $formattedTotalSize ),
214 $context->msg( 'parentheses', $formattedSize )->plain() ) . $lang->getDirMark();
218 * Format the character difference of one or several changes.
220 * @param $old RecentChange
221 * @param $new RecentChange last change to use, if not provided, $old will be used
222 * @return string HTML fragment
224 public function formatCharacterDifference( RecentChange
$old, RecentChange
$new = null ) {
225 $oldlen = $old->mAttribs
['rc_old_len'];
228 $newlen = $new->mAttribs
['rc_new_len'];
230 $newlen = $old->mAttribs
['rc_new_len'];
233 if ( $oldlen === null ||
$newlen === null ) {
237 return self
::showCharacterDifference( $oldlen, $newlen, $this->getContext() );
241 * Returns text for the end of RC
244 public function endRecentChangesList() {
245 if ( $this->rclistOpen
) {
253 * @param string $s HTML to update
254 * @param $rc_timestamp mixed
256 public function insertDateHeader( &$s, $rc_timestamp ) {
257 # Make date header if necessary
258 $date = $this->getLanguage()->userDate( $rc_timestamp, $this->getUser() );
259 if ( $date != $this->lastdate
) {
260 if ( $this->lastdate
!= '' ) {
263 $s .= Xml
::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
264 $this->lastdate
= $date;
265 $this->rclistOpen
= true;
270 * @param string $s HTML to update
271 * @param $title Title
272 * @param $logtype string
274 public function insertLog( &$s, $title, $logtype ) {
275 $page = new LogPage( $logtype );
276 $logname = $page->getName()->escaped();
277 $s .= $this->msg( 'parentheses' )->rawParams( Linker
::linkKnown( $title, $logname ) )->escaped();
281 * @param string $s HTML to update
282 * @param $rc RecentChange
283 * @param $unpatrolled
285 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
287 if ( $rc->mAttribs
['rc_type'] == RC_NEW ||
$rc->mAttribs
['rc_type'] == RC_LOG
) {
288 $diffLink = $this->message
['diff'];
289 } elseif ( !self
::userCan( $rc, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
290 $diffLink = $this->message
['diff'];
293 'curid' => $rc->mAttribs
['rc_cur_id'],
294 'diff' => $rc->mAttribs
['rc_this_oldid'],
295 'oldid' => $rc->mAttribs
['rc_last_oldid']
298 $diffLink = Linker
::linkKnown(
300 $this->message
['diff'],
301 array( 'tabindex' => $rc->counter
),
305 $diffhist = $diffLink . $this->message
['pipe-separator'];
307 $diffhist .= Linker
::linkKnown(
309 $this->message
['hist'],
312 'curid' => $rc->mAttribs
['rc_cur_id'],
313 'action' => 'history'
316 $s .= $this->msg( 'parentheses' )->rawParams( $diffhist )->escaped() . ' <span class="mw-changeslist-separator">. .</span> ';
320 * @param string $s HTML to update
321 * @param $rc RecentChange
322 * @param $unpatrolled
325 public function insertArticleLink( &$s, &$rc, $unpatrolled, $watched ) {
328 $articlelink = Linker
::linkKnown(
331 array( 'class' => 'mw-changeslist-title' ),
334 if ( $this->isDeleted( $rc, Revision
::DELETED_TEXT
) ) {
335 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
337 # To allow for boldening pages watched by this user
338 $articlelink = "<span class=\"mw-title\">{$articlelink}</span>";
340 $articlelink .= $this->getLanguage()->getDirMark();
342 wfRunHooks( 'ChangesListInsertArticleLink',
343 array( &$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched ) );
345 $s .= " $articlelink";
349 * Get the timestamp from $rc formatted with current user's settings
352 * @param $rc RecentChange
353 * @return string HTML fragment
355 public function getTimestamp( $rc ) {
356 return $this->message
['semicolon-separator'] . '<span class="mw-changeslist-date">' .
357 $this->getLanguage()->userTime( $rc->mAttribs
['rc_timestamp'], $this->getUser() ) . '</span> <span class="mw-changeslist-separator">. .</span> ';
361 * Insert time timestamp string from $rc into $s
363 * @param string $s HTML to update
364 * @param $rc RecentChange
366 public function insertTimestamp( &$s, $rc ) {
367 $s .= $this->getTimestamp( $rc );
371 * Insert links to user page, user talk page and eventually a blocking link
373 * @param &$s String HTML to update
374 * @param &$rc RecentChange
376 public function insertUserRelatedLinks( &$s, &$rc ) {
377 if ( $this->isDeleted( $rc, Revision
::DELETED_USER
) ) {
378 $s .= ' <span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
380 $s .= $this->getLanguage()->getDirMark() . Linker
::userLink( $rc->mAttribs
['rc_user'],
381 $rc->mAttribs
['rc_user_text'] );
382 $s .= Linker
::userToolLinks( $rc->mAttribs
['rc_user'], $rc->mAttribs
['rc_user_text'] );
387 * Insert a formatted action
389 * @param $rc RecentChange
392 public function insertLogEntry( $rc ) {
393 $formatter = LogFormatter
::newFromRow( $rc->mAttribs
);
394 $formatter->setContext( $this->getContext() );
395 $formatter->setShowUserToolLinks( true );
396 $mark = $this->getLanguage()->getDirMark();
397 return $formatter->getActionText() . " $mark" . $formatter->getComment();
401 * Insert a formatted comment
402 * @param $rc RecentChange
405 public function insertComment( $rc ) {
406 if ( $rc->mAttribs
['rc_type'] != RC_MOVE
&& $rc->mAttribs
['rc_type'] != RC_MOVE_OVER_REDIRECT
) {
407 if ( $this->isDeleted( $rc, Revision
::DELETED_COMMENT
) ) {
408 return ' <span class="history-deleted">' . $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
410 return Linker
::commentBlock( $rc->mAttribs
['rc_comment'], $rc->getTitle() );
417 * Check whether to enable recent changes patrol features
419 * @deprecated since 1.22
422 public static function usePatrol() {
425 wfDeprecated( __METHOD__
, '1.22' );
427 return $wgUser->useRCPatrol();
431 * Returns the string which indicates the number of watching users
434 protected function numberofWatchingusers( $count ) {
435 static $cache = array();
437 if ( !isset( $cache[$count] ) ) {
438 $cache[$count] = $this->msg( 'number_of_watching_users_RCview' )->numParams( $count )->escaped();
440 return $cache[$count];
447 * Determine if said field of a revision is hidden
448 * @param $rc RCCacheEntry
449 * @param $field Integer: one of DELETED_* bitfield constants
452 public static function isDeleted( $rc, $field ) {
453 return ( $rc->mAttribs
['rc_deleted'] & $field ) == $field;
457 * Determine if the current user is allowed to view a particular
458 * field of this revision, if it's marked as deleted.
459 * @param $rc RCCacheEntry
460 * @param $field Integer
461 * @param $user User object to check, or null to use $wgUser
464 public static function userCan( $rc, $field, User
$user = null ) {
465 if ( $rc->mAttribs
['rc_type'] == RC_LOG
) {
466 return LogEventsList
::userCanBitfield( $rc->mAttribs
['rc_deleted'], $field, $user );
468 return Revision
::userCanBitfield( $rc->mAttribs
['rc_deleted'], $field, $user );
473 * @param $link string
474 * @param $watched bool
477 protected function maybeWatchedLink( $link, $watched = false ) {
479 return '<strong class="mw-watched">' . $link . '</strong>';
481 return '<span class="mw-rc-unwatched">' . $link . '</span>';
485 /** Inserts a rollback link
488 * @param $rc RecentChange
490 public function insertRollback( &$s, &$rc ) {
491 if ( $rc->mAttribs
['rc_type'] == RC_EDIT
&& $rc->mAttribs
['rc_this_oldid'] && $rc->mAttribs
['rc_cur_id'] ) {
492 $page = $rc->getTitle();
493 /** Check for rollback and edit permissions, disallow special pages, and only
494 * show a link on the top-most revision */
495 if ( $this->getUser()->isAllowed( 'rollback' ) && $rc->mAttribs
['page_latest'] == $rc->mAttribs
['rc_this_oldid'] )
497 $rev = new Revision( array(
499 'id' => $rc->mAttribs
['rc_this_oldid'],
500 'user' => $rc->mAttribs
['rc_user'],
501 'user_text' => $rc->mAttribs
['rc_user_text'],
502 'deleted' => $rc->mAttribs
['rc_deleted']
504 $s .= ' ' . Linker
::generateRollback( $rev, $this->getContext() );
511 * @param $rc RecentChange
514 public function insertTags( &$s, &$rc, &$classes ) {
515 if ( empty( $rc->mAttribs
['ts_tags'] ) ) {
519 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow( $rc->mAttribs
['ts_tags'], 'changeslist' );
520 $classes = array_merge( $classes, $newClasses );
521 $s .= ' ' . $tagSummary;
524 public function insertExtra( &$s, &$rc, &$classes ) {
525 // Empty, used for subclasses to add anything special.
528 protected function showAsUnpatrolled( RecentChange
$rc ) {
529 return self
::isUnpatrolled( $rc, $this->getUser() );
533 * @param object|RecentChange $rc Database row from recentchanges or a RecentChange object
537 public static function isUnpatrolled( $rc, User
$user ) {
538 if ( $rc instanceof RecentChange
) {
539 $isPatrolled = $rc->mAttribs
['rc_patrolled'];
540 $rcType = $rc->mAttribs
['rc_type'];
542 $isPatrolled = $rc->rc_patrolled
;
543 $rcType = $rc->rc_type
;
546 if ( !$isPatrolled ) {
547 if ( $user->useRCPatrol() ) {
550 if ( $user->useNPPatrol() && $rcType == RC_NEW
) {