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
{
31 protected $watchlist = false;
35 protected $rcCacheIndex;
36 protected $rclistOpen;
37 protected $rcMoveIndex;
39 /** @var MapCacheLRU */
40 protected $watchingCache;
43 * Changeslist constructor
45 * @param Skin|IContextSource $obj
47 public function __construct( $obj ) {
48 if ( $obj instanceof IContextSource
) {
49 $this->setContext( $obj );
50 $this->skin
= $obj->getSkin();
52 $this->setContext( $obj->getContext() );
55 $this->preCacheMessages();
56 $this->watchingCache
= new MapCacheLRU( 50 );
60 * Fetch an appropriate changes list class for the specified context
61 * Some users might want to use an enhanced list format, for instance
63 * @param IContextSource $context
66 public static function newFromContext( IContextSource
$context ) {
67 $user = $context->getUser();
68 $sk = $context->getSkin();
70 if ( Hooks
::run( 'FetchChangesList', array( $user, &$sk, &$list ) ) ) {
71 $new = $context->getRequest()->getBool( 'enhanced', $user->getOption( 'usenewrc' ) );
73 return $new ?
new EnhancedChangesList( $context ) : new OldChangesList( $context );
80 * Sets the list to use a "<li class='watchlist-(namespace)-(page)'>" tag
83 public function setWatchlistDivs( $value = true ) {
84 $this->watchlist
= $value;
88 * @return bool True when setWatchlistDivs has been called
91 public function isWatchlist() {
92 return (bool)$this->watchlist
;
96 * As we use the same small set of messages in various methods and that
97 * they are called often, we call them once and save them in $this->message
99 private function preCacheMessages() {
100 if ( !isset( $this->message
) ) {
102 'cur', 'diff', 'hist', 'enhancedrc-history', 'last', 'blocklink', 'history',
103 'semicolon-separator', 'pipe-separator' ) as $msg
105 $this->message
[$msg] = $this->msg( $msg )->escaped();
111 * Returns the appropriate flags for new page, minor change and patrolling
112 * @param array $flags Associative array of 'flag' => Bool
113 * @param string $nothing To use for empty space
116 public function recentChangesFlags( $flags, $nothing = ' ' ) {
118 foreach ( array_keys( $this->getConfig()->get( 'RecentChangesFlags' ) ) as $flag ) {
119 $f .= isset( $flags[$flag] ) && $flags[$flag]
120 ? self
::flag( $flag )
128 * Provide the "<abbr>" element appropriate to a given abbreviated flag,
129 * namely the flag indicating a new page, a minor edit, a bot edit, or an
130 * unpatrolled edit. By default in English it will contain "N", "m", "b",
131 * "!" respectively, plus it will have an appropriate title and class.
133 * @param string $flag One key of $wgRecentChangesFlags
134 * @return string Raw HTML
136 public static function flag( $flag ) {
137 static $flagInfos = null;
138 if ( is_null( $flagInfos ) ) {
139 global $wgRecentChangesFlags;
140 $flagInfos = array();
141 foreach ( $wgRecentChangesFlags as $key => $value ) {
142 $flagInfos[$key]['letter'] = wfMessage( $value['letter'] )->escaped();
143 $flagInfos[$key]['title'] = wfMessage( $value['title'] )->escaped();
144 // Allow customized class name, fall back to flag name
145 $flagInfos[$key]['class'] = Sanitizer
::escapeClass(
146 isset( $value['class'] ) ?
$value['class'] : $key );
150 // Inconsistent naming, bleh, kepted for b/c
152 'minoredit' => 'minor',
155 if ( isset( $map[$flag] ) ) {
159 return "<abbr class='" . $flagInfos[$flag]['class'] . "' title='" .
160 $flagInfos[$flag]['title'] . "'>" . $flagInfos[$flag]['letter'] .
165 * Returns text for the start of the tabular part of RC
168 public function beginRecentChangesList() {
169 $this->rc_cache
= array();
170 $this->rcMoveIndex
= 0;
171 $this->rcCacheIndex
= 0;
172 $this->lastdate
= '';
173 $this->rclistOpen
= false;
174 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
176 return '<div class="mw-changeslist">';
180 * @param ResultWrapper|array $rows
182 public function initChangesListRows( $rows ) {
183 Hooks
::run( 'ChangesListInitRows', array( $this, $rows ) );
187 * Show formatted char difference
188 * @param int $old Number of bytes
189 * @param int $new Number of bytes
190 * @param IContextSource $context
193 public static function showCharacterDifference( $old, $new, IContextSource
$context = null ) {
195 $context = RequestContext
::getMain();
200 $szdiff = $new - $old;
202 $lang = $context->getLanguage();
203 $config = $context->getConfig();
204 $code = $lang->getCode();
205 static $fastCharDiff = array();
206 if ( !isset( $fastCharDiff[$code] ) ) {
207 $fastCharDiff[$code] = $config->get( 'MiserMode' )
208 ||
$context->msg( 'rc-change-size' )->plain() === '$1';
211 $formattedSize = $lang->formatNum( $szdiff );
213 if ( !$fastCharDiff[$code] ) {
214 $formattedSize = $context->msg( 'rc-change-size', $formattedSize )->text();
217 if ( abs( $szdiff ) > abs( $config->get( 'RCChangedSizeThreshold' ) ) ) {
223 if ( $szdiff === 0 ) {
224 $formattedSizeClass = 'mw-plusminus-null';
225 } elseif ( $szdiff > 0 ) {
226 $formattedSize = '+' . $formattedSize;
227 $formattedSizeClass = 'mw-plusminus-pos';
229 $formattedSizeClass = 'mw-plusminus-neg';
232 $formattedTotalSize = $context->msg( 'rc-change-size-new' )->numParams( $new )->text();
234 return Html
::element( $tag,
235 array( 'dir' => 'ltr', 'class' => $formattedSizeClass, 'title' => $formattedTotalSize ),
236 $context->msg( 'parentheses', $formattedSize )->plain() ) . $lang->getDirMark();
240 * Format the character difference of one or several changes.
242 * @param RecentChange $old
243 * @param RecentChange $new Last change to use, if not provided, $old will be used
244 * @return string HTML fragment
246 public function formatCharacterDifference( RecentChange
$old, RecentChange
$new = null ) {
247 $oldlen = $old->mAttribs
['rc_old_len'];
250 $newlen = $new->mAttribs
['rc_new_len'];
252 $newlen = $old->mAttribs
['rc_new_len'];
255 if ( $oldlen === null ||
$newlen === null ) {
259 return self
::showCharacterDifference( $oldlen, $newlen, $this->getContext() );
263 * Returns text for the end of RC
266 public function endRecentChangesList() {
267 $out = $this->rclistOpen ?
"</ul>\n" : '';
274 * @param string $s HTML to update
275 * @param mixed $rc_timestamp
277 public function insertDateHeader( &$s, $rc_timestamp ) {
278 # Make date header if necessary
279 $date = $this->getLanguage()->userDate( $rc_timestamp, $this->getUser() );
280 if ( $date != $this->lastdate
) {
281 if ( $this->lastdate
!= '' ) {
284 $s .= Xml
::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
285 $this->lastdate
= $date;
286 $this->rclistOpen
= true;
291 * @param string $s HTML to update
292 * @param Title $title
293 * @param string $logtype
295 public function insertLog( &$s, $title, $logtype ) {
296 $page = new LogPage( $logtype );
297 $logname = $page->getName()->escaped();
298 $s .= $this->msg( 'parentheses' )->rawParams( Linker
::linkKnown( $title, $logname ) )->escaped();
302 * @param string $s HTML to update
303 * @param RecentChange $rc
304 * @param bool $unpatrolled
306 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
308 if ( $rc->mAttribs
['rc_type'] == RC_NEW ||
$rc->mAttribs
['rc_type'] == RC_LOG
) {
309 $diffLink = $this->message
['diff'];
310 } elseif ( !self
::userCan( $rc, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
311 $diffLink = $this->message
['diff'];
314 'curid' => $rc->mAttribs
['rc_cur_id'],
315 'diff' => $rc->mAttribs
['rc_this_oldid'],
316 'oldid' => $rc->mAttribs
['rc_last_oldid']
319 $diffLink = Linker
::linkKnown(
321 $this->message
['diff'],
322 array( 'tabindex' => $rc->counter
),
326 $diffhist = $diffLink . $this->message
['pipe-separator'];
328 $diffhist .= Linker
::linkKnown(
330 $this->message
['hist'],
333 'curid' => $rc->mAttribs
['rc_cur_id'],
334 'action' => 'history'
337 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
338 $s .= $this->msg( 'parentheses' )->rawParams( $diffhist )->escaped() .
339 ' <span class="mw-changeslist-separator">. .</span> ';
343 * @param string $s HTML to update
344 * @param RecentChange $rc
345 * @param bool $unpatrolled
346 * @param bool $watched
348 public function insertArticleLink( &$s, &$rc, $unpatrolled, $watched ) {
350 if ( $rc->getTitle()->isRedirect() ) {
351 $params = array( 'redirect' => 'no' );
354 $articlelink = Linker
::linkKnown(
357 array( 'class' => 'mw-changeslist-title' ),
360 if ( $this->isDeleted( $rc, Revision
::DELETED_TEXT
) ) {
361 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
363 # To allow for boldening pages watched by this user
364 $articlelink = "<span class=\"mw-title\">{$articlelink}</span>";
366 $articlelink .= $this->getLanguage()->getDirMark();
368 Hooks
::run( 'ChangesListInsertArticleLink',
369 array( &$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched ) );
371 $s .= " $articlelink";
375 * @param RecentChange $rc
376 * @param bool $unpatrolled
377 * @param bool $watched
381 public function getArticleLink( RecentChange
$rc, $unpatrolled, $watched ) {
383 $this->insertArticleLink( $s, $rc, $unpatrolled, $watched );
388 * Get the timestamp from $rc formatted with current user's settings
391 * @param RecentChange $rc
392 * @return string HTML fragment
394 public function getTimestamp( $rc ) {
395 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
396 return $this->message
['semicolon-separator'] . '<span class="mw-changeslist-date">' .
397 $this->getLanguage()->userTime(
398 $rc->mAttribs
['rc_timestamp'],
400 ) . '</span> <span class="mw-changeslist-separator">. .</span> ';
404 * Insert time timestamp string from $rc into $s
406 * @param string $s HTML to update
407 * @param RecentChange $rc
409 public function insertTimestamp( &$s, $rc ) {
410 $s .= $this->getTimestamp( $rc );
414 * Insert links to user page, user talk page and eventually a blocking link
416 * @param string &$s HTML to update
417 * @param RecentChange &$rc
419 public function insertUserRelatedLinks( &$s, &$rc ) {
420 if ( $this->isDeleted( $rc, Revision
::DELETED_USER
) ) {
421 $s .= ' <span class="history-deleted">' .
422 $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
424 $s .= $this->getLanguage()->getDirMark() . Linker
::userLink( $rc->mAttribs
['rc_user'],
425 $rc->mAttribs
['rc_user_text'] );
426 $s .= Linker
::userToolLinks( $rc->mAttribs
['rc_user'], $rc->mAttribs
['rc_user_text'] );
431 * Insert a formatted action
433 * @param RecentChange $rc
436 public function insertLogEntry( $rc ) {
437 $formatter = LogFormatter
::newFromRow( $rc->mAttribs
);
438 $formatter->setContext( $this->getContext() );
439 $formatter->setShowUserToolLinks( true );
440 $mark = $this->getLanguage()->getDirMark();
442 return $formatter->getActionText() . " $mark" . $formatter->getComment();
446 * Insert a formatted comment
447 * @param RecentChange $rc
450 public function insertComment( $rc ) {
451 if ( $this->isDeleted( $rc, Revision
::DELETED_COMMENT
) ) {
452 return ' <span class="history-deleted">' .
453 $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
455 return Linker
::commentBlock( $rc->mAttribs
['rc_comment'], $rc->getTitle() );
460 * Check whether to enable recent changes patrol features
462 * @deprecated since 1.22
465 public static function usePatrol() {
468 wfDeprecated( __METHOD__
, '1.22' );
470 return $wgUser->useRCPatrol();
474 * Returns the string which indicates the number of watching users
475 * @param int $count Number of user watching a page
478 protected function numberofWatchingusers( $count ) {
479 $cache = $this->watchingCache
;
481 if ( !$cache->has( $count ) ) {
482 $cache->set( $count, $this->msg( 'number_of_watching_users_RCview' )
483 ->numParams( $count )->escaped() );
486 return $cache->get( $count );
493 * Determine if said field of a revision is hidden
494 * @param RCCacheEntry|RecentChange $rc
495 * @param int $field One of DELETED_* bitfield constants
498 public static function isDeleted( $rc, $field ) {
499 return ( $rc->mAttribs
['rc_deleted'] & $field ) == $field;
503 * Determine if the current user is allowed to view a particular
504 * field of this revision, if it's marked as deleted.
505 * @param RCCacheEntry|RecentChange $rc
507 * @param User $user User object to check, or null to use $wgUser
510 public static function userCan( $rc, $field, User
$user = null ) {
511 if ( $rc->mAttribs
['rc_type'] == RC_LOG
) {
512 return LogEventsList
::userCanBitfield( $rc->mAttribs
['rc_deleted'], $field, $user );
514 return Revision
::userCanBitfield( $rc->mAttribs
['rc_deleted'], $field, $user );
519 * @param string $link
520 * @param bool $watched
523 protected function maybeWatchedLink( $link, $watched = false ) {
525 return '<strong class="mw-watched">' . $link . '</strong>';
527 return '<span class="mw-rc-unwatched">' . $link . '</span>';
531 /** Inserts a rollback link
534 * @param RecentChange $rc
536 public function insertRollback( &$s, &$rc ) {
537 if ( $rc->mAttribs
['rc_type'] == RC_EDIT
538 && $rc->mAttribs
['rc_this_oldid']
539 && $rc->mAttribs
['rc_cur_id']
541 $page = $rc->getTitle();
542 /** Check for rollback and edit permissions, disallow special pages, and only
543 * show a link on the top-most revision */
544 if ( $this->getUser()->isAllowed( 'rollback' )
545 && $rc->mAttribs
['page_latest'] == $rc->mAttribs
['rc_this_oldid']
547 $rev = new Revision( array(
549 'id' => $rc->mAttribs
['rc_this_oldid'],
550 'user' => $rc->mAttribs
['rc_user'],
551 'user_text' => $rc->mAttribs
['rc_user_text'],
552 'deleted' => $rc->mAttribs
['rc_deleted']
554 $s .= ' ' . Linker
::generateRollback( $rev, $this->getContext() );
560 * @param RecentChange $rc
564 public function getRollback( RecentChange
$rc ) {
566 $this->insertRollback( $s, $rc );
572 * @param RecentChange $rc
573 * @param array $classes
575 public function insertTags( &$s, &$rc, &$classes ) {
576 if ( empty( $rc->mAttribs
['ts_tags'] ) ) {
580 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow(
581 $rc->mAttribs
['ts_tags'],
584 $classes = array_merge( $classes, $newClasses );
585 $s .= ' ' . $tagSummary;
589 * @param RecentChange $rc
590 * @param array $classes
594 public function getTags( RecentChange
$rc, array &$classes ) {
596 $this->insertTags( $s, $rc, $classes );
600 public function insertExtra( &$s, &$rc, &$classes ) {
601 // Empty, used for subclasses to add anything special.
604 protected function showAsUnpatrolled( RecentChange
$rc ) {
605 return self
::isUnpatrolled( $rc, $this->getUser() );
609 * @param object|RecentChange $rc Database row from recentchanges or a RecentChange object
613 public static function isUnpatrolled( $rc, User
$user ) {
614 if ( $rc instanceof RecentChange
) {
615 $isPatrolled = $rc->mAttribs
['rc_patrolled'];
616 $rcType = $rc->mAttribs
['rc_type'];
618 $isPatrolled = $rc->rc_patrolled
;
619 $rcType = $rc->rc_type
;
622 if ( !$isPatrolled ) {
623 if ( $user->useRCPatrol() ) {
626 if ( $user->useNPPatrol() && $rcType == RC_NEW
) {