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;
40 * Changeslist constructor
42 * @param Skin|IContextSource $obj
44 public function __construct( $obj ) {
45 if ( $obj instanceof IContextSource
) {
46 $this->setContext( $obj );
47 $this->skin
= $obj->getSkin();
49 $this->setContext( $obj->getContext() );
52 $this->preCacheMessages();
56 * Fetch an appropriate changes list class for the specified context
57 * Some users might want to use an enhanced list format, for instance
59 * @param IContextSource $context
60 * @return ChangesList derivative
62 public static function newFromContext( IContextSource
$context ) {
63 $user = $context->getUser();
64 $sk = $context->getSkin();
66 if ( wfRunHooks( 'FetchChangesList', array( $user, &$sk, &$list ) ) ) {
67 $new = $context->getRequest()->getBool( 'enhanced', $user->getOption( 'usenewrc' ) );
69 return $new ?
new EnhancedChangesList( $context ) : new OldChangesList( $context );
76 * Sets the list to use a "<li class='watchlist-(namespace)-(page)'>" tag
77 * @param $value Boolean
79 public function setWatchlistDivs( $value = true ) {
80 $this->watchlist
= $value;
84 * As we use the same small set of messages in various methods and that
85 * they are called often, we call them once and save them in $this->message
87 private function preCacheMessages() {
88 if ( !isset( $this->message
) ) {
90 'cur', 'diff', 'hist', 'enhancedrc-history', 'last', 'blocklink', 'history',
91 'semicolon-separator', 'pipe-separator' ) as $msg
93 $this->message
[$msg] = $this->msg( $msg )->escaped();
99 * Returns the appropriate flags for new page, minor change and patrolling
100 * @param array $flags Associative array of 'flag' => Bool
101 * @param string $nothing to use for empty space
104 public function recentChangesFlags( $flags, $nothing = ' ' ) {
105 global $wgRecentChangesFlags;
107 foreach ( array_keys( $wgRecentChangesFlags ) as $flag ) {
108 $f .= isset( $flags[$flag] ) && $flags[$flag]
109 ? self
::flag( $flag )
117 * Provide the "<abbr>" element appropriate to a given abbreviated flag,
118 * namely the flag indicating a new page, a minor edit, a bot edit, or an
119 * unpatrolled edit. By default in English it will contain "N", "m", "b",
120 * "!" respectively, plus it will have an appropriate title and class.
122 * @param string $flag One key of $wgRecentChangesFlags
123 * @return string Raw HTML
125 public static function flag( $flag ) {
126 static $flagInfos = null;
127 if ( is_null( $flagInfos ) ) {
128 global $wgRecentChangesFlags;
129 $flagInfos = array();
130 foreach ( $wgRecentChangesFlags as $key => $value ) {
131 $flagInfos[$key]['letter'] = wfMessage( $value['letter'] )->escaped();
132 $flagInfos[$key]['title'] = wfMessage( $value['title'] )->escaped();
133 // Allow customized class name, fall back to flag name
134 $flagInfos[$key]['class'] = Sanitizer
::escapeClass(
135 isset( $value['class'] ) ?
$value['class'] : $key );
139 // Inconsistent naming, bleh, kepted for b/c
141 'minoredit' => 'minor',
144 if ( isset( $map[$flag] ) ) {
148 return "<abbr class='" . $flagInfos[$flag]['class'] . "' title='" .
149 $flagInfos[$flag]['title'] . "'>" . $flagInfos[$flag]['letter'] .
154 * Returns text for the start of the tabular part of RC
157 public function beginRecentChangesList() {
158 $this->rc_cache
= array();
159 $this->rcMoveIndex
= 0;
160 $this->rcCacheIndex
= 0;
161 $this->lastdate
= '';
162 $this->rclistOpen
= false;
163 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
164 $this->getOutput()->addModules( 'mediawiki.special.changeslist.js' );
166 return '<div class="mw-changeslist">';
170 * Show formatted char difference
171 * @param int $old Number of bytes
172 * @param int $new Number of bytes
173 * @param IContextSource $context
176 public static function showCharacterDifference( $old, $new, IContextSource
$context = null ) {
177 global $wgRCChangedSizeThreshold, $wgMiserMode;
180 $context = RequestContext
::getMain();
185 $szdiff = $new - $old;
187 $lang = $context->getLanguage();
188 $code = $lang->getCode();
189 static $fastCharDiff = array();
190 if ( !isset( $fastCharDiff[$code] ) ) {
191 $fastCharDiff[$code] = $wgMiserMode ||
$context->msg( 'rc-change-size' )->plain() === '$1';
194 $formattedSize = $lang->formatNum( $szdiff );
196 if ( !$fastCharDiff[$code] ) {
197 $formattedSize = $context->msg( 'rc-change-size', $formattedSize )->text();
200 if ( abs( $szdiff ) > abs( $wgRCChangedSizeThreshold ) ) {
206 if ( $szdiff === 0 ) {
207 $formattedSizeClass = 'mw-plusminus-null';
208 } elseif ( $szdiff > 0 ) {
209 $formattedSize = '+' . $formattedSize;
210 $formattedSizeClass = 'mw-plusminus-pos';
212 $formattedSizeClass = 'mw-plusminus-neg';
215 $formattedTotalSize = $context->msg( 'rc-change-size-new' )->numParams( $new )->text();
217 return Html
::element( $tag,
218 array( 'dir' => 'ltr', 'class' => $formattedSizeClass, 'title' => $formattedTotalSize ),
219 $context->msg( 'parentheses', $formattedSize )->plain() ) . $lang->getDirMark();
223 * Format the character difference of one or several changes.
225 * @param $old RecentChange
226 * @param $new RecentChange last change to use, if not provided, $old will be used
227 * @return string HTML fragment
229 public function formatCharacterDifference( RecentChange
$old, RecentChange
$new = null ) {
230 $oldlen = $old->mAttribs
['rc_old_len'];
233 $newlen = $new->mAttribs
['rc_new_len'];
235 $newlen = $old->mAttribs
['rc_new_len'];
238 if ( $oldlen === null ||
$newlen === null ) {
242 return self
::showCharacterDifference( $oldlen, $newlen, $this->getContext() );
246 * Returns text for the end of RC
249 public function endRecentChangesList() {
250 $out = $this->rclistOpen ?
"</ul>\n" : '';
256 * @param string $s HTML to update
257 * @param $rc_timestamp mixed
259 public function insertDateHeader( &$s, $rc_timestamp ) {
260 # Make date header if necessary
261 $date = $this->getLanguage()->userDate( $rc_timestamp, $this->getUser() );
262 if ( $date != $this->lastdate
) {
263 if ( $this->lastdate
!= '' ) {
266 $s .= Xml
::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
267 $this->lastdate
= $date;
268 $this->rclistOpen
= true;
273 * @param string $s HTML to update
274 * @param $title Title
275 * @param $logtype string
277 public function insertLog( &$s, $title, $logtype ) {
278 $page = new LogPage( $logtype );
279 $logname = $page->getName()->escaped();
280 $s .= $this->msg( 'parentheses' )->rawParams( Linker
::linkKnown( $title, $logname ) )->escaped();
284 * @param string $s HTML to update
285 * @param $rc RecentChange
286 * @param $unpatrolled
288 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
290 if ( $rc->mAttribs
['rc_type'] == RC_NEW ||
$rc->mAttribs
['rc_type'] == RC_LOG
) {
291 $diffLink = $this->message
['diff'];
292 } elseif ( !self
::userCan( $rc, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
293 $diffLink = $this->message
['diff'];
296 'curid' => $rc->mAttribs
['rc_cur_id'],
297 'diff' => $rc->mAttribs
['rc_this_oldid'],
298 'oldid' => $rc->mAttribs
['rc_last_oldid']
301 $diffLink = Linker
::linkKnown(
303 $this->message
['diff'],
304 array( 'tabindex' => $rc->counter
),
308 $diffhist = $diffLink . $this->message
['pipe-separator'];
310 $diffhist .= Linker
::linkKnown(
312 $this->message
['hist'],
315 'curid' => $rc->mAttribs
['rc_cur_id'],
316 'action' => 'history'
319 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
320 $s .= $this->msg( 'parentheses' )->rawParams( $diffhist )->escaped() .
321 ' <span class="mw-changeslist-separator">. .</span> ';
325 * @param string $s HTML to update
326 * @param $rc RecentChange
327 * @param $unpatrolled
330 public function insertArticleLink( &$s, &$rc, $unpatrolled, $watched ) {
333 $articlelink = Linker
::linkKnown(
336 array( 'class' => 'mw-changeslist-title' ),
339 if ( $this->isDeleted( $rc, Revision
::DELETED_TEXT
) ) {
340 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
342 # To allow for boldening pages watched by this user
343 $articlelink = "<span class=\"mw-title\">{$articlelink}</span>";
345 $articlelink .= $this->getLanguage()->getDirMark();
347 wfRunHooks( 'ChangesListInsertArticleLink',
348 array( &$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched ) );
350 $s .= " $articlelink";
354 * Get the timestamp from $rc formatted with current user's settings
357 * @param $rc RecentChange
358 * @return string HTML fragment
360 public function getTimestamp( $rc ) {
361 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
362 return $this->message
['semicolon-separator'] . '<span class="mw-changeslist-date">' .
363 $this->getLanguage()->userTime(
364 $rc->mAttribs
['rc_timestamp'],
366 ) . '</span> <span class="mw-changeslist-separator">. .</span> ';
370 * Insert time timestamp string from $rc into $s
372 * @param string $s HTML to update
373 * @param $rc RecentChange
375 public function insertTimestamp( &$s, $rc ) {
376 $s .= $this->getTimestamp( $rc );
380 * Insert links to user page, user talk page and eventually a blocking link
382 * @param &$s String HTML to update
383 * @param &$rc RecentChange
385 public function insertUserRelatedLinks( &$s, &$rc ) {
386 if ( $this->isDeleted( $rc, Revision
::DELETED_USER
) ) {
387 $s .= ' <span class="history-deleted">' .
388 $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
390 $s .= $this->getLanguage()->getDirMark() . Linker
::userLink( $rc->mAttribs
['rc_user'],
391 $rc->mAttribs
['rc_user_text'] );
392 $s .= Linker
::userToolLinks( $rc->mAttribs
['rc_user'], $rc->mAttribs
['rc_user_text'] );
397 * Insert a formatted action
399 * @param $rc RecentChange
402 public function insertLogEntry( $rc ) {
403 $formatter = LogFormatter
::newFromRow( $rc->mAttribs
);
404 $formatter->setContext( $this->getContext() );
405 $formatter->setShowUserToolLinks( true );
406 $mark = $this->getLanguage()->getDirMark();
408 return $formatter->getActionText() . " $mark" . $formatter->getComment();
412 * Insert a formatted comment
413 * @param $rc RecentChange
416 public function insertComment( $rc ) {
417 if ( $rc->mAttribs
['rc_type'] != RC_MOVE
&& $rc->mAttribs
['rc_type'] != RC_MOVE_OVER_REDIRECT
) {
418 if ( $this->isDeleted( $rc, Revision
::DELETED_COMMENT
) ) {
419 return ' <span class="history-deleted">' .
420 $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
422 return Linker
::commentBlock( $rc->mAttribs
['rc_comment'], $rc->getTitle() );
430 * Check whether to enable recent changes patrol features
432 * @deprecated since 1.22
435 public static function usePatrol() {
438 wfDeprecated( __METHOD__
, '1.22' );
440 return $wgUser->useRCPatrol();
444 * Returns the string which indicates the number of watching users
445 * @param int $count Number of user watching a page
448 protected function numberofWatchingusers( $count ) {
449 static $cache = array();
451 if ( !isset( $cache[$count] ) ) {
452 $cache[$count] = $this->msg( 'number_of_watching_users_RCview' )
453 ->numParams( $count )->escaped();
456 return $cache[$count];
463 * Determine if said field of a revision is hidden
464 * @param RCCacheEntry|RecentChange $rc
465 * @param int $field One of DELETED_* bitfield constants
468 public static function isDeleted( $rc, $field ) {
469 return ( $rc->mAttribs
['rc_deleted'] & $field ) == $field;
473 * Determine if the current user is allowed to view a particular
474 * field of this revision, if it's marked as deleted.
475 * @param RCCacheEntry|RecentChange $rc
477 * @param User $user User object to check, or null to use $wgUser
480 public static function userCan( $rc, $field, User
$user = null ) {
481 if ( $rc->mAttribs
['rc_type'] == RC_LOG
) {
482 return LogEventsList
::userCanBitfield( $rc->mAttribs
['rc_deleted'], $field, $user );
484 return Revision
::userCanBitfield( $rc->mAttribs
['rc_deleted'], $field, $user );
489 * @param $link string
490 * @param $watched bool
493 protected function maybeWatchedLink( $link, $watched = false ) {
495 return '<strong class="mw-watched">' . $link . '</strong>';
497 return '<span class="mw-rc-unwatched">' . $link . '</span>';
501 /** Inserts a rollback link
504 * @param $rc RecentChange
506 public function insertRollback( &$s, &$rc ) {
507 if ( $rc->mAttribs
['rc_type'] == RC_EDIT
508 && $rc->mAttribs
['rc_this_oldid']
509 && $rc->mAttribs
['rc_cur_id']
511 $page = $rc->getTitle();
512 /** Check for rollback and edit permissions, disallow special pages, and only
513 * show a link on the top-most revision */
514 if ( $this->getUser()->isAllowed( 'rollback' )
515 && $rc->mAttribs
['page_latest'] == $rc->mAttribs
['rc_this_oldid']
517 $rev = new Revision( array(
519 'id' => $rc->mAttribs
['rc_this_oldid'],
520 'user' => $rc->mAttribs
['rc_user'],
521 'user_text' => $rc->mAttribs
['rc_user_text'],
522 'deleted' => $rc->mAttribs
['rc_deleted']
524 $s .= ' ' . Linker
::generateRollback( $rev, $this->getContext() );
531 * @param $rc RecentChange
534 public function insertTags( &$s, &$rc, &$classes ) {
535 if ( empty( $rc->mAttribs
['ts_tags'] ) ) {
539 list( $tagSummary, $newClasses ) = ChangeTags
::formatSummaryRow(
540 $rc->mAttribs
['ts_tags'],
543 $classes = array_merge( $classes, $newClasses );
544 $s .= ' ' . $tagSummary;
547 public function insertExtra( &$s, &$rc, &$classes ) {
548 // Empty, used for subclasses to add anything special.
551 protected function showAsUnpatrolled( RecentChange
$rc ) {
552 return self
::isUnpatrolled( $rc, $this->getUser() );
556 * @param object|RecentChange $rc Database row from recentchanges or a RecentChange object
560 public static function isUnpatrolled( $rc, User
$user ) {
561 if ( $rc instanceof RecentChange
) {
562 $isPatrolled = $rc->mAttribs
['rc_patrolled'];
563 $rcType = $rc->mAttribs
['rc_type'];
565 $isPatrolled = $rc->rc_patrolled
;
566 $rcType = $rc->rc_type
;
569 if ( !$isPatrolled ) {
570 if ( $user->useRCPatrol() ) {
573 if ( $user->useNPPatrol() && $rcType == RC_NEW
) {