Whitespace indention fix for r96150 (seperate to make review easier)
[mediawiki.git] / includes / ChangesList.php
blobf75c5d5bc1a091f2154ef2a3a498ca7733d2436e
1 <?php
2 /**
3 * Classes to show various lists of changes:
4 * - watchlist
5 * - related changes
6 * - recent changes
8 * @file
9 */
11 /**
12 * @todo document
14 class RCCacheEntry extends RecentChange {
15 var $secureName, $link;
16 var $curlink , $difflink, $lastlink, $usertalklink, $versionlink;
17 var $userlink, $timestamp, $watched;
19 /**
20 * @param $rc RecentChange
21 * @return RCCacheEntry
23 static function newFromParent( $rc ) {
24 $rc2 = new RCCacheEntry;
25 $rc2->mAttribs = $rc->mAttribs;
26 $rc2->mExtra = $rc->mExtra;
27 return $rc2;
31 /**
32 * Base class for all changes lists
34 class ChangesList extends ContextSource {
36 /**
37 * @var Skin
39 public $skin;
41 protected $watchlist = false;
43 protected $message;
45 /**
46 * Changeslist contructor
48 * @param $obj Skin or RequestContext
50 public function __construct( $obj ) {
51 if ( $obj instanceof RequestContext ) {
52 $this->setContext( $obj );
53 $this->skin = $obj->getSkin();
54 } else {
55 $this->setContext( $obj->getContext() );
56 $this->skin = $obj;
58 $this->preCacheMessages();
61 /**
62 * Fetch an appropriate changes list class for the main context
63 * This first argument used to be an User object.
65 * @deprecated in 1.18; use newFromContext() instead
66 * @param $unused Unused
67 * @return ChangesList|EnhancedChangesList|OldChangesList derivative
69 public static function newFromUser( $unused ) {
70 return self::newFromContext( RequestContext::getMain() );
73 /**
74 * Fetch an appropriate changes list class for the specified context
75 * Some users might want to use an enhanced list format, for instance
77 * @param $context RequestContext to use
78 * @return ChangesList|EnhancedChangesList|OldChangesList derivative
80 public static function newFromContext( RequestContext $context ) {
81 $user = $context->getUser();
82 $sk = $context->getSkin();
83 $list = null;
84 if( wfRunHooks( 'FetchChangesList', array( $user, &$sk, &$list ) ) ) {
85 $new = $context->getRequest()->getBool( 'enhanced', $user->getOption( 'usenewrc' ) );
86 return $new ? new EnhancedChangesList( $context ) : new OldChangesList( $context );
87 } else {
88 return $list;
92 /**
93 * Sets the list to use a <li class="watchlist-(namespace)-(page)"> tag
94 * @param $value Boolean
96 public function setWatchlistDivs( $value = true ) {
97 $this->watchlist = $value;
101 * As we use the same small set of messages in various methods and that
102 * they are called often, we call them once and save them in $this->message
104 private function preCacheMessages() {
105 if( !isset( $this->message ) ) {
106 foreach ( explode( ' ', 'cur diff hist last blocklink history ' .
107 'semicolon-separator pipe-separator' ) as $msg ) {
108 $this->message[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
114 * Returns the appropriate flags for new page, minor change and patrolling
115 * @param $flags Array Associative array of 'flag' => Bool
116 * @param $nothing String to use for empty space
117 * @return String
119 protected function recentChangesFlags( $flags, $nothing = '&#160;' ) {
120 $f = '';
121 foreach( array( 'newpage', 'minor', 'bot', 'unpatrolled' ) as $flag ){
122 $f .= isset( $flags[$flag] ) && $flags[$flag]
123 ? self::flag( $flag )
124 : $nothing;
126 return $f;
130 * Provide the <abbr> element appropriate to a given abbreviated flag,
131 * namely the flag indicating a new page, a minor edit, a bot edit, or an
132 * unpatrolled edit. By default in English it will contain "N", "m", "b",
133 * "!" respectively, plus it will have an appropriate title and class.
135 * @param $flag String: 'newpage', 'unpatrolled', 'minor', or 'bot'
136 * @return String: Raw HTML
138 public static function flag( $flag ) {
139 static $messages = null;
140 if ( is_null( $messages ) ) {
141 $messages = array(
142 'newpage' => array( 'newpageletter', 'recentchanges-label-newpage' ),
143 'minoredit' => array( 'minoreditletter', 'recentchanges-label-minor' ),
144 'botedit' => array( 'boteditletter', 'recentchanges-label-bot' ),
145 'unpatrolled' => array( 'unpatrolledletter', 'recentchanges-label-unpatrolled' ),
147 foreach( $messages as &$value ) {
148 $value[0] = wfMsgExt( $value[0], 'escapenoentities' );
149 $value[1] = wfMsgExt( $value[1], 'escapenoentities' );
153 # Inconsistent naming, bleh
154 $map = array(
155 'newpage' => 'newpage',
156 'minor' => 'minoredit',
157 'bot' => 'botedit',
158 'unpatrolled' => 'unpatrolled',
159 'minoredit' => 'minoredit',
160 'botedit' => 'botedit',
162 $flag = $map[$flag];
164 return "<abbr class='$flag' title='" . $messages[$flag][1] . "'>" . $messages[$flag][0] . '</abbr>';
168 * Returns text for the start of the tabular part of RC
169 * @return String
171 public function beginRecentChangesList() {
172 $this->rc_cache = array();
173 $this->rcMoveIndex = 0;
174 $this->rcCacheIndex = 0;
175 $this->lastdate = '';
176 $this->rclistOpen = false;
177 return '';
181 * Show formatted char difference
182 * @param $old Integer: bytes
183 * @param $new Integer: bytes
184 * @return String
186 public static function showCharacterDifference( $old, $new ) {
187 global $wgRCChangedSizeThreshold, $wgLang, $wgMiserMode;
188 $szdiff = $new - $old;
190 $code = $wgLang->getCode();
191 static $fastCharDiff = array();
192 if ( !isset($fastCharDiff[$code]) ) {
193 $fastCharDiff[$code] = $wgMiserMode || wfMsgNoTrans( 'rc-change-size' ) === '$1';
196 $formatedSize = $wgLang->formatNum($szdiff);
198 if ( !$fastCharDiff[$code] ) {
199 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape' ), $formatedSize );
202 if( abs( $szdiff ) > abs( $wgRCChangedSizeThreshold ) ) {
203 $tag = 'strong';
204 } else {
205 $tag = 'span';
207 if( $szdiff === 0 ) {
208 return "<$tag class='mw-plusminus-null'>($formatedSize)</$tag>";
209 } elseif( $szdiff > 0 ) {
210 return "<$tag class='mw-plusminus-pos'>(+$formatedSize)</$tag>";
211 } else {
212 return "<$tag class='mw-plusminus-neg'>($formatedSize)</$tag>";
217 * Returns text for the end of RC
218 * @return String
220 public function endRecentChangesList() {
221 if( $this->rclistOpen ) {
222 return "</ul>\n";
223 } else {
224 return '';
229 * @param $s
230 * @param $rc RecentChange
231 * @return void
233 public function insertMove( &$s, $rc ) {
234 # Diff
235 $s .= '(' . $this->message['diff'] . ') (';
236 # Hist
237 $s .= Linker::linkKnown(
238 $rc->getMovedToTitle(),
239 $this->message['hist'],
240 array(),
241 array( 'action' => 'history' )
242 ) . ') . . ';
243 # "[[x]] moved to [[y]]"
244 $msg = ( $rc->mAttribs['rc_type'] == RC_MOVE ) ? '1movedto2' : '1movedto2_redir';
245 $s .= wfMsgHtml(
246 $msg,
247 Linker::linkKnown(
248 $rc->getTitle(),
249 null,
250 array(),
251 array( 'redirect' => 'no' )
253 Linker::linkKnown( $rc->getMovedToTitle() )
257 public function insertDateHeader( &$s, $rc_timestamp ) {
258 # Make date header if necessary
259 $date = $this->getLang()->date( $rc_timestamp, true, true );
260 if( $date != $this->lastdate ) {
261 if( $this->lastdate != '' ) {
262 $s .= "</ul>\n";
264 $s .= Xml::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
265 $this->lastdate = $date;
266 $this->rclistOpen = true;
270 public function insertLog( &$s, $title, $logtype ) {
271 $logname = LogPage::logName( $logtype );
272 $s .= '(' . Linker::linkKnown( $title, htmlspecialchars( $logname ) ) . ')';
276 * @param $s
277 * @param $rc RecentChange
278 * @param $unpatrolled
279 * @return void
281 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
282 # Diff link
283 if( $rc->mAttribs['rc_type'] == RC_NEW || $rc->mAttribs['rc_type'] == RC_LOG ) {
284 $diffLink = $this->message['diff'];
285 } elseif( !self::userCan($rc,Revision::DELETED_TEXT) ) {
286 $diffLink = $this->message['diff'];
287 } else {
288 $query = array(
289 'curid' => $rc->mAttribs['rc_cur_id'],
290 'diff' => $rc->mAttribs['rc_this_oldid'],
291 'oldid' => $rc->mAttribs['rc_last_oldid']
294 if( $unpatrolled ) {
295 $query['rcid'] = $rc->mAttribs['rc_id'];
298 $diffLink = Linker::linkKnown(
299 $rc->getTitle(),
300 $this->message['diff'],
301 array( 'tabindex' => $rc->counter ),
302 $query
305 $s .= '(' . $diffLink . $this->message['pipe-separator'];
306 # History link
307 $s .= Linker::linkKnown(
308 $rc->getTitle(),
309 $this->message['hist'],
310 array(),
311 array(
312 'curid' => $rc->mAttribs['rc_cur_id'],
313 'action' => 'history'
316 $s .= ') . . ';
320 * @param $s
321 * @param $rc RecentChange
322 * @param $unpatrolled
323 * @param $watched
324 * @return void
326 public function insertArticleLink( &$s, &$rc, $unpatrolled, $watched ) {
327 # If it's a new article, there is no diff link, but if it hasn't been
328 # patrolled yet, we need to give users a way to do so
329 $params = array();
331 if ( $unpatrolled && $rc->mAttribs['rc_type'] == RC_NEW ) {
332 $params['rcid'] = $rc->mAttribs['rc_id'];
335 if( $this->isDeleted($rc,Revision::DELETED_TEXT) ) {
336 $articlelink = Linker::linkKnown(
337 $rc->getTitle(),
338 null,
339 array(),
340 $params
342 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
343 } else {
344 $articlelink = ' '. Linker::linkKnown(
345 $rc->getTitle(),
346 null,
347 array(),
348 $params
351 # Bolden pages watched by this user
352 if( $watched ) {
353 $articlelink = "<strong class=\"mw-watched\">{$articlelink}</strong>";
355 # RTL/LTR marker
356 $articlelink .= $this->getLang()->getDirMark();
358 wfRunHooks( 'ChangesListInsertArticleLink',
359 array(&$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched) );
361 $s .= " $articlelink";
365 * @param $s
366 * @param $rc RecentChange
367 * @return void
369 public function insertTimestamp( &$s, $rc ) {
370 $s .= $this->message['semicolon-separator'] .
371 $this->getLang()->time( $rc->mAttribs['rc_timestamp'], true, true ) . ' . . ';
374 /** Insert links to user page, user talk page and eventually a blocking link
376 * @param $rc RecentChange
378 public function insertUserRelatedLinks( &$s, &$rc ) {
379 if( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
380 $s .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
381 } else {
382 $s .= Linker::userLink( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
383 $s .= Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
387 /** insert a formatted action
389 * @param $rc RecentChange
391 public function insertAction( &$s, &$rc ) {
392 if( $rc->mAttribs['rc_type'] == RC_LOG ) {
393 if( $this->isDeleted( $rc, LogPage::DELETED_ACTION ) ) {
394 $s .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-event' ) . '</span>';
395 } else {
396 $s .= ' '.LogPage::actionText( $rc->mAttribs['rc_log_type'], $rc->mAttribs['rc_log_action'],
397 $rc->getTitle(), $this->getSkin(), LogPage::extractParams( $rc->mAttribs['rc_params'] ), true, true );
402 /** insert a formatted comment
404 * @param $rc RecentChange
406 public function insertComment( &$s, &$rc ) {
407 if( $rc->mAttribs['rc_type'] != RC_MOVE && $rc->mAttribs['rc_type'] != RC_MOVE_OVER_REDIRECT ) {
408 if( $this->isDeleted( $rc, Revision::DELETED_COMMENT ) ) {
409 $s .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
410 } else {
411 $s .= Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
417 * Check whether to enable recent changes patrol features
418 * @return Boolean
420 public static function usePatrol() {
421 global $wgUser;
422 return $wgUser->useRCPatrol();
426 * Returns the string which indicates the number of watching users
428 protected function numberofWatchingusers( $count ) {
429 static $cache = array();
430 if( $count > 0 ) {
431 if( !isset( $cache[$count] ) ) {
432 $cache[$count] = wfMsgExt( 'number_of_watching_users_RCview',
433 array('parsemag', 'escape' ), $this->getLang()->formatNum( $count ) );
435 return $cache[$count];
436 } else {
437 return '';
442 * Determine if said field of a revision is hidden
443 * @param $rc RCCacheEntry
444 * @param $field Integer: one of DELETED_* bitfield constants
445 * @return Boolean
447 public static function isDeleted( $rc, $field ) {
448 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
452 * Determine if the current user is allowed to view a particular
453 * field of this revision, if it's marked as deleted.
454 * @param $rc RCCacheEntry
455 * @param $field Integer
456 * @return Boolean
458 public static function userCan( $rc, $field ) {
459 if( $rc->mAttribs['rc_type'] == RC_LOG ) {
460 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field );
461 } else {
462 return Revision::userCanBitfield( $rc->mAttribs['rc_deleted'], $field );
466 protected function maybeWatchedLink( $link, $watched = false ) {
467 if( $watched ) {
468 return '<strong class="mw-watched">' . $link . '</strong>';
469 } else {
470 return '<span class="mw-rc-unwatched">' . $link . '</span>';
474 /** Inserts a rollback link
476 * @param $s
477 * @param $rc RecentChange
479 public function insertRollback( &$s, &$rc ) {
480 if( !$rc->mAttribs['rc_new'] && $rc->mAttribs['rc_this_oldid'] && $rc->mAttribs['rc_cur_id'] ) {
481 $page = $rc->getTitle();
482 /** Check for rollback and edit permissions, disallow special pages, and only
483 * show a link on the top-most revision */
484 if ( $this->getUser()->isAllowed('rollback') && $rc->mAttribs['page_latest'] == $rc->mAttribs['rc_this_oldid'] )
486 $rev = new Revision( array(
487 'id' => $rc->mAttribs['rc_this_oldid'],
488 'user' => $rc->mAttribs['rc_user'],
489 'user_text' => $rc->mAttribs['rc_user_text'],
490 'deleted' => $rc->mAttribs['rc_deleted']
491 ) );
492 $rev->setTitle( $page );
493 $s .= ' '.Linker::generateRollback( $rev, $this->getContext() );
499 * @param $s
500 * @param $rc RecentChange
501 * @param $classes
502 * @return
504 public function insertTags( &$s, &$rc, &$classes ) {
505 if ( empty($rc->mAttribs['ts_tags']) )
506 return;
508 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $rc->mAttribs['ts_tags'], 'changeslist' );
509 $classes = array_merge( $classes, $newClasses );
510 $s .= ' ' . $tagSummary;
513 public function insertExtra( &$s, &$rc, &$classes ) {
514 ## Empty, used for subclassers to add anything special.
520 * Generate a list of changes using the good old system (no javascript)
522 class OldChangesList extends ChangesList {
524 * Format a line using the old system (aka without any javascript).
526 * @param $rc RecentChange
528 public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
529 global $wgRCShowChangedSize;
530 wfProfileIn( __METHOD__ );
531 # Should patrol-related stuff be shown?
532 $unpatrolled = $this->getUser()->useRCPatrol() && !$rc->mAttribs['rc_patrolled'];
534 $dateheader = ''; // $s now contains only <li>...</li>, for hooks' convenience.
535 $this->insertDateHeader( $dateheader, $rc->mAttribs['rc_timestamp'] );
537 $s = '';
538 $classes = array();
539 // use mw-line-even/mw-line-odd class only if linenumber is given (feature from bug 14468)
540 if( $linenumber ) {
541 if( $linenumber & 1 ) {
542 $classes[] = 'mw-line-odd';
544 else {
545 $classes[] = 'mw-line-even';
549 // Moved pages
550 if( $rc->mAttribs['rc_type'] == RC_MOVE || $rc->mAttribs['rc_type'] == RC_MOVE_OVER_REDIRECT ) {
551 $this->insertMove( $s, $rc );
552 // Log entries
553 } elseif( $rc->mAttribs['rc_log_type'] ) {
554 $logtitle = Title::newFromText( 'Log/'.$rc->mAttribs['rc_log_type'], NS_SPECIAL );
555 $this->insertLog( $s, $logtitle, $rc->mAttribs['rc_log_type'] );
556 // Log entries (old format) or log targets, and special pages
557 } elseif( $rc->mAttribs['rc_namespace'] == NS_SPECIAL ) {
558 list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $rc->mAttribs['rc_title'] );
559 if( $name == 'Log' ) {
560 $this->insertLog( $s, $rc->getTitle(), $subpage );
562 // Regular entries
563 } else {
564 $this->insertDiffHist( $s, $rc, $unpatrolled );
565 # M, N, b and ! (minor, new, bot and unpatrolled)
566 $s .= $this->recentChangesFlags(
567 array(
568 'newpage' => $rc->mAttribs['rc_new'],
569 'minor' => $rc->mAttribs['rc_minor'],
570 'unpatrolled' => $unpatrolled,
571 'bot' => $rc->mAttribs['rc_bot']
575 $this->insertArticleLink( $s, $rc, $unpatrolled, $watched );
577 # Edit/log timestamp
578 $this->insertTimestamp( $s, $rc );
579 # Bytes added or removed
580 if( $wgRCShowChangedSize ) {
581 $cd = $rc->getCharacterDifference();
582 if( $cd != '' ) {
583 $s .= "$cd . . ";
586 # User tool links
587 $this->insertUserRelatedLinks( $s, $rc );
588 # LTR/RTL direction mark
589 $s .= $this->getLang()->getDirMark();
590 # Log action text (if any)
591 $this->insertAction( $s, $rc );
592 # LTR/RTL direction mark
593 $s .= $this->getLang()->getDirMark();
594 # Edit or log comment
595 $this->insertComment( $s, $rc );
596 # Tags
597 $this->insertTags( $s, $rc, $classes );
598 # Rollback
599 $this->insertRollback( $s, $rc );
600 # For subclasses
601 $this->insertExtra( $s, $rc, $classes );
603 # How many users watch this page
604 if( $rc->numberofWatchingusers > 0 ) {
605 $s .= ' ' . wfMsgExt( 'number_of_watching_users_RCview',
606 array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $rc->numberofWatchingusers ) );
609 if( $this->watchlist ) {
610 $classes[] = Sanitizer::escapeClass( 'watchlist-'.$rc->mAttribs['rc_namespace'].'-'.$rc->mAttribs['rc_title'] );
613 wfRunHooks( 'OldChangesListRecentChangesLine', array(&$this, &$s, $rc) );
615 wfProfileOut( __METHOD__ );
616 return "$dateheader<li class=\"".implode( ' ', $classes )."\">".$s."</li>\n";
622 * Generate a list of changes using an Enhanced system (uses javascript).
624 class EnhancedChangesList extends ChangesList {
626 * Add the JavaScript file for enhanced changeslist
627 * @return String
629 public function beginRecentChangesList() {
630 $this->rc_cache = array();
631 $this->rcMoveIndex = 0;
632 $this->rcCacheIndex = 0;
633 $this->lastdate = '';
634 $this->rclistOpen = false;
635 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
636 return '';
639 * Format a line for enhanced recentchange (aka with javascript and block of lines).
641 * @param $baseRC RecentChange
642 * @param $watched bool
644 * @return string
646 public function recentChangesLine( &$baseRC, $watched = false ) {
647 wfProfileIn( __METHOD__ );
649 # Create a specialised object
650 $rc = RCCacheEntry::newFromParent( $baseRC );
652 $curIdEq = array( 'curid' => $rc->mAttribs['rc_cur_id'] );
654 # If it's a new day, add the headline and flush the cache
655 $date = $this->getLang()->date( $rc->mAttribs['rc_timestamp'], true );
656 $ret = '';
657 if( $date != $this->lastdate ) {
658 # Process current cache
659 $ret = $this->recentChangesBlock();
660 $this->rc_cache = array();
661 $ret .= Xml::element( 'h4', null, $date ) . "\n";
662 $this->lastdate = $date;
665 # Should patrol-related stuff be shown?
666 if( $this->getUser()->useRCPatrol() ) {
667 $rc->unpatrolled = !$rc->mAttribs['rc_patrolled'];
668 } else {
669 $rc->unpatrolled = false;
672 $showdifflinks = true;
673 # Make article link
674 $type = $rc->mAttribs['rc_type'];
675 $logType = $rc->mAttribs['rc_log_type'];
676 // Page moves
677 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
678 $msg = ( $type == RC_MOVE ) ? "1movedto2" : "1movedto2_redir";
679 $clink = wfMsg( $msg, Linker::linkKnown( $rc->getTitle(), null,
680 array(), array( 'redirect' => 'no' ) ),
681 Linker::linkKnown( $rc->getMovedToTitle() ) );
682 // New unpatrolled pages
683 } elseif( $rc->unpatrolled && $type == RC_NEW ) {
684 $clink = Linker::linkKnown( $rc->getTitle(), null, array(),
685 array( 'rcid' => $rc->mAttribs['rc_id'] ) );
686 // Log entries
687 } elseif( $type == RC_LOG ) {
688 if( $logType ) {
689 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
690 $clink = '(' . Linker::linkKnown( $logtitle,
691 LogPage::logName( $logType ) ) . ')';
692 } else {
693 $clink = Linker::link( $rc->getTitle() );
695 $watched = false;
696 // Log entries (old format) and special pages
697 } elseif( $rc->mAttribs['rc_namespace'] == NS_SPECIAL ) {
698 list( $specialName, $logtype ) = SpecialPageFactory::resolveAlias( $rc->mAttribs['rc_title'] );
699 if ( $specialName == 'Log' ) {
700 # Log updates, etc
701 $logname = LogPage::logName( $logtype );
702 $clink = '(' . Linker::linkKnown( $rc->getTitle(), $logname ) . ')';
703 } else {
704 wfDebug( "Unexpected special page in recentchanges\n" );
705 $clink = '';
707 // Edits
708 } else {
709 $clink = Linker::linkKnown( $rc->getTitle() );
712 # Don't show unusable diff links
713 if ( !ChangesList::userCan($rc,Revision::DELETED_TEXT) ) {
714 $showdifflinks = false;
717 $time = $this->getLang()->time( $rc->mAttribs['rc_timestamp'], true, true );
718 $rc->watched = $watched;
719 $rc->link = $clink;
720 $rc->timestamp = $time;
721 $rc->numberofWatchingusers = $baseRC->numberofWatchingusers;
723 # Make "cur" and "diff" links. Do not use link(), it is too slow if
724 # called too many times (50% of CPU time on RecentChanges!).
725 $thisOldid = $rc->mAttribs['rc_this_oldid'];
726 $lastOldid = $rc->mAttribs['rc_last_oldid'];
727 if( $rc->unpatrolled ) {
728 $rcIdQuery = array( 'rcid' => $rc->mAttribs['rc_id'] );
729 } else {
730 $rcIdQuery = array();
732 $querycur = $curIdEq + array( 'diff' => '0', 'oldid' => $thisOldid );
733 $querydiff = $curIdEq + array( 'diff' => $thisOldid, 'oldid' =>
734 $lastOldid ) + $rcIdQuery;
736 if( !$showdifflinks ) {
737 $curLink = $this->message['cur'];
738 $diffLink = $this->message['diff'];
739 } elseif( in_array( $type, array( RC_NEW, RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT ) ) ) {
740 if ( $type != RC_NEW ) {
741 $curLink = $this->message['cur'];
742 } else {
743 $curUrl = htmlspecialchars( $rc->getTitle()->getLinkUrl( $querycur ) );
744 $curLink = "<a href=\"$curUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['cur']}</a>";
746 $diffLink = $this->message['diff'];
747 } else {
748 $diffUrl = htmlspecialchars( $rc->getTitle()->getLinkUrl( $querydiff ) );
749 $curUrl = htmlspecialchars( $rc->getTitle()->getLinkUrl( $querycur ) );
750 $diffLink = "<a href=\"$diffUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['diff']}</a>";
751 $curLink = "<a href=\"$curUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['cur']}</a>";
754 # Make "last" link
755 if( !$showdifflinks || !$lastOldid ) {
756 $lastLink = $this->message['last'];
757 } elseif( in_array( $type, array( RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT ) ) ) {
758 $lastLink = $this->message['last'];
759 } else {
760 $lastLink = Linker::linkKnown( $rc->getTitle(), $this->message['last'],
761 array(), $curIdEq + array('diff' => $thisOldid, 'oldid' => $lastOldid) + $rcIdQuery );
764 # Make user links
765 if( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
766 $rc->userlink = ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
767 } else {
768 $rc->userlink = Linker::userLink( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
769 $rc->usertalklink = Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
772 $rc->lastlink = $lastLink;
773 $rc->curlink = $curLink;
774 $rc->difflink = $diffLink;
776 # Put accumulated information into the cache, for later display
777 # Page moves go on their own line
778 $title = $rc->getTitle();
779 $secureName = $title->getPrefixedDBkey();
780 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
781 # Use an @ character to prevent collision with page names
782 $this->rc_cache['@@' . ($this->rcMoveIndex++)] = array($rc);
783 } else {
784 # Logs are grouped by type
785 if( $type == RC_LOG ){
786 $secureName = SpecialPage::getTitleFor( 'Log', $logType )->getPrefixedDBkey();
788 if( !isset( $this->rc_cache[$secureName] ) ) {
789 $this->rc_cache[$secureName] = array();
792 array_push( $this->rc_cache[$secureName], $rc );
795 wfProfileOut( __METHOD__ );
797 return $ret;
801 * Enhanced RC group
803 protected function recentChangesBlockGroup( $block ) {
804 global $wgRCShowChangedSize;
806 wfProfileIn( __METHOD__ );
808 # Add the namespace and title of the block as part of the class
809 if ( $block[0]->mAttribs['rc_log_type'] ) {
810 # Log entry
811 $classes = 'mw-collapsible mw-collapsed mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-log-' . $block[0]->mAttribs['rc_log_type'] . '-' . $block[0]->mAttribs['rc_title'] );
812 } else {
813 $classes = 'mw-collapsible mw-collapsed mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-ns' . $block[0]->mAttribs['rc_namespace'] . '-' . $block[0]->mAttribs['rc_title'] );
815 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
816 Html::openElement( 'tr' );
818 # Collate list of users
819 $userlinks = array();
820 # Other properties
821 $unpatrolled = false;
822 $isnew = false;
823 $curId = $currentRevision = 0;
824 # Some catalyst variables...
825 $namehidden = true;
826 $allLogs = true;
827 foreach( $block as $rcObj ) {
828 $oldid = $rcObj->mAttribs['rc_last_oldid'];
829 if( $rcObj->mAttribs['rc_new'] ) {
830 $isnew = true;
832 // If all log actions to this page were hidden, then don't
833 // give the name of the affected page for this block!
834 if( !$this->isDeleted( $rcObj, LogPage::DELETED_ACTION ) ) {
835 $namehidden = false;
837 $u = $rcObj->userlink;
838 if( !isset( $userlinks[$u] ) ) {
839 $userlinks[$u] = 0;
841 if( $rcObj->unpatrolled ) {
842 $unpatrolled = true;
844 if( $rcObj->mAttribs['rc_type'] != RC_LOG ) {
845 $allLogs = false;
847 # Get the latest entry with a page_id and oldid
848 # since logs may not have these.
849 if( !$curId && $rcObj->mAttribs['rc_cur_id'] ) {
850 $curId = $rcObj->mAttribs['rc_cur_id'];
852 if( !$currentRevision && $rcObj->mAttribs['rc_this_oldid'] ) {
853 $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
856 $bot = $rcObj->mAttribs['rc_bot'];
857 $userlinks[$u]++;
860 # Sort the list and convert to text
861 krsort( $userlinks );
862 asort( $userlinks );
863 $users = array();
864 foreach( $userlinks as $userlink => $count) {
865 $text = $userlink;
866 $text .= $this->getLang()->getDirMark();
867 if( $count > 1 ) {
868 $text .= ' (' . $this->getLang()->formatNum( $count ) . '×)';
870 array_push( $users, $text );
873 $users = ' <span class="changedby">[' .
874 implode( $this->message['semicolon-separator'], $users ) . ']</span>';
876 # Title for <a> tags
877 $expandTitle = htmlspecialchars( wfMsg( 'rc-enhanced-expand' ) );
878 $closeTitle = htmlspecialchars( wfMsg( 'rc-enhanced-hide' ) );
880 $tl = "<span class='mw-collapsible-toggle'>"
881 . "<span class='mw-rc-openarrow'>"
882 . "<a href='#' title='$expandTitle'>{$this->sideArrow()}</a>"
883 . "</span><span class='mw-rc-closearrow'>"
884 . "<a href='#' title='$closeTitle'>{$this->downArrow()}</a>"
885 . "</span></span>";
886 $r .= "<td>$tl</td>";
888 # Main line
889 $r .= '<td class="mw-enhanced-rc">' . $this->recentChangesFlags( array(
890 'newpage' => $isnew,
891 'minor' => false,
892 'unpatrolled' => $unpatrolled,
893 'bot' => $bot ,
894 ) );
896 # Timestamp
897 $r .= '&#160;'.$block[0]->timestamp.'&#160;</td><td>';
899 # Article link
900 if( $namehidden ) {
901 $r .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-event' ) . '</span>';
902 } elseif( $allLogs ) {
903 $r .= $this->maybeWatchedLink( $block[0]->link, $block[0]->watched );
904 } else {
905 $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled, $block[0]->watched );
908 $r .= $this->getLang()->getDirMark();
910 $queryParams['curid'] = $curId;
911 # Changes message
912 $n = count($block);
913 static $nchanges = array();
914 if ( !isset( $nchanges[$n] ) ) {
915 $nchanges[$n] = wfMsgExt( 'nchanges', array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $n ) );
917 # Total change link
918 $r .= ' ';
919 if( !$allLogs ) {
920 $r .= '(';
921 if( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT ) ) {
922 $r .= $nchanges[$n];
923 } elseif( $isnew ) {
924 $r .= $nchanges[$n];
925 } else {
926 $params = $queryParams;
927 $params['diff'] = $currentRevision;
928 $params['oldid'] = $oldid;
930 $r .= Linker::link(
931 $block[0]->getTitle(),
932 $nchanges[$n],
933 array(),
934 $params,
935 array( 'known', 'noclasses' )
940 # History
941 if( $allLogs ) {
942 // don't show history link for logs
943 } elseif( $namehidden || !$block[0]->getTitle()->exists() ) {
944 $r .= $this->message['pipe-separator'] . $this->message['hist'] . ')';
945 } else {
946 $params = $queryParams;
947 $params['action'] = 'history';
949 $r .= $this->message['pipe-separator'] .
950 Linker::linkKnown(
951 $block[0]->getTitle(),
952 $this->message['hist'],
953 array(),
954 $params
955 ) . ')';
957 $r .= ' . . ';
959 # Character difference (does not apply if only log items)
960 if( $wgRCShowChangedSize && !$allLogs ) {
961 $last = 0;
962 $first = count($block) - 1;
963 # Some events (like logs) have an "empty" size, so we need to skip those...
964 while( $last < $first && $block[$last]->mAttribs['rc_new_len'] === null ) {
965 $last++;
967 while( $first > $last && $block[$first]->mAttribs['rc_old_len'] === null ) {
968 $first--;
970 # Get net change
971 $chardiff = $rcObj->getCharacterDifference( $block[$first]->mAttribs['rc_old_len'],
972 $block[$last]->mAttribs['rc_new_len'] );
974 if( $chardiff == '' ) {
975 $r .= ' ';
976 } else {
977 $r .= ' ' . $chardiff. ' . . ';
981 $r .= $users;
982 $r .= $this->numberofWatchingusers($block[0]->numberofWatchingusers);
984 # Sub-entries
985 foreach( $block as $rcObj ) {
986 # Classes to apply -- TODO implement
987 $classes = array();
988 $type = $rcObj->mAttribs['rc_type'];
990 #$r .= '<tr><td valign="top">'.$this->spacerArrow();
991 $r .= '<tr><td></td><td class="mw-enhanced-rc">';
992 $r .= $this->recentChangesFlags( array(
993 'newpage' => $rcObj->mAttribs['rc_new'],
994 'minor' => $rcObj->mAttribs['rc_minor'],
995 'unpatrolled' => $rcObj->unpatrolled,
996 'bot' => $rcObj->mAttribs['rc_bot'],
997 ) );
998 $r .= '&#160;</td><td class="mw-enhanced-rc-nested"><span class="mw-enhanced-rc-time">';
1000 $params = $queryParams;
1002 if( $rcObj->mAttribs['rc_this_oldid'] != 0 ) {
1003 $params['oldid'] = $rcObj->mAttribs['rc_this_oldid'];
1006 # Log timestamp
1007 if( $type == RC_LOG ) {
1008 $link = $rcObj->timestamp;
1009 # Revision link
1010 } elseif( !ChangesList::userCan($rcObj,Revision::DELETED_TEXT) ) {
1011 $link = '<span class="history-deleted">'.$rcObj->timestamp.'</span> ';
1012 } else {
1013 if ( $rcObj->unpatrolled && $type == RC_NEW) {
1014 $params['rcid'] = $rcObj->mAttribs['rc_id'];
1017 $link = Linker::linkKnown(
1018 $rcObj->getTitle(),
1019 $rcObj->timestamp,
1020 array(),
1021 $params
1023 if( $this->isDeleted($rcObj,Revision::DELETED_TEXT) )
1024 $link = '<span class="history-deleted">'.$link.'</span> ';
1026 $r .= $link . '</span>';
1028 if ( !$type == RC_LOG || $type == RC_NEW ) {
1029 $r .= ' (';
1030 $r .= $rcObj->curlink;
1031 $r .= $this->message['pipe-separator'];
1032 $r .= $rcObj->lastlink;
1033 $r .= ')';
1035 $r .= ' . . ';
1037 # Character diff
1038 if( $wgRCShowChangedSize && $rcObj->getCharacterDifference() ) {
1039 $r .= $rcObj->getCharacterDifference() . ' . . ' ;
1042 # User links
1043 $r .= $rcObj->userlink;
1044 $r .= $rcObj->usertalklink;
1045 // log action
1046 $this->insertAction( $r, $rcObj );
1047 // log comment
1048 $this->insertComment( $r, $rcObj );
1049 # Rollback
1050 $this->insertRollback( $r, $rcObj );
1051 # Tags
1052 $this->insertTags( $r, $rcObj, $classes );
1054 $r .= "</td></tr>\n";
1056 $r .= "</table>\n";
1058 $this->rcCacheIndex++;
1060 wfProfileOut( __METHOD__ );
1062 return $r;
1066 * Generate HTML for an arrow or placeholder graphic
1067 * @param $dir String: one of '', 'd', 'l', 'r'
1068 * @param $alt String: text
1069 * @param $title String: text
1070 * @return String: HTML <img> tag
1072 protected function arrow( $dir, $alt='', $title='' ) {
1073 global $wgStylePath;
1074 $encUrl = htmlspecialchars( $wgStylePath . '/common/images/Arr_' . $dir . '.png' );
1075 $encAlt = htmlspecialchars( $alt );
1076 $encTitle = htmlspecialchars( $title );
1077 return "<img src=\"$encUrl\" width=\"12\" height=\"12\" alt=\"$encAlt\" title=\"$encTitle\" />";
1081 * Generate HTML for a right- or left-facing arrow,
1082 * depending on language direction.
1083 * @return String: HTML <img> tag
1085 protected function sideArrow() {
1086 global $wgContLang;
1087 $dir = $wgContLang->isRTL() ? 'l' : 'r';
1088 return $this->arrow( $dir, '+', wfMsg( 'rc-enhanced-expand' ) );
1092 * Generate HTML for a down-facing arrow
1093 * depending on language direction.
1094 * @return String: HTML <img> tag
1096 protected function downArrow() {
1097 return $this->arrow( 'd', '-', wfMsg( 'rc-enhanced-hide' ) );
1101 * Generate HTML for a spacer image
1102 * @return String: HTML <img> tag
1104 protected function spacerArrow() {
1105 return $this->arrow( '', codepointToUtf8( 0xa0 ) ); // non-breaking space
1109 * Enhanced RC ungrouped line.
1111 * @param $rcObj RecentChange
1112 * @return String: a HTML formated line (generated using $r)
1114 protected function recentChangesBlockLine( $rcObj ) {
1115 global $wgRCShowChangedSize;
1117 wfProfileIn( __METHOD__ );
1118 $query['curid'] = $rcObj->mAttribs['rc_cur_id'];
1120 $type = $rcObj->mAttribs['rc_type'];
1121 $logType = $rcObj->mAttribs['rc_log_type'];
1122 if( $logType ) {
1123 # Log entry
1124 $classes = 'mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-log-' . $logType . '-' . $rcObj->mAttribs['rc_title'] );
1125 } else {
1126 $classes = 'mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-ns' . $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
1128 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
1129 Html::openElement( 'tr' );
1131 $r .= '<td class="mw-enhanced-rc">' . $this->spacerArrow();
1132 # Flag and Timestamp
1133 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
1134 $r .= '&#160;&#160;&#160;&#160;'; // 4 flags -> 4 spaces
1135 } else {
1136 $r .= $this->recentChangesFlags( array(
1137 'newpage' => $type == RC_NEW,
1138 'mino' => $rcObj->mAttribs['rc_minor'],
1139 'unpatrolled' => $rcObj->unpatrolled,
1140 'bot' => $rcObj->mAttribs['rc_bot'],
1141 ) );
1143 $r .= '&#160;'.$rcObj->timestamp.'&#160;</td><td>';
1144 # Article or log link
1145 if( $logType ) {
1146 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
1147 $logname = LogPage::logName( $logType );
1148 $r .= '(' . Linker::linkKnown( $logtitle, htmlspecialchars( $logname ) ) . ')';
1149 } else {
1150 $this->insertArticleLink( $r, $rcObj, $rcObj->unpatrolled, $rcObj->watched );
1152 # Diff and hist links
1153 if ( $type != RC_LOG ) {
1154 $r .= ' ('. $rcObj->difflink . $this->message['pipe-separator'];
1155 $query['action'] = 'history';
1156 $r .= Linker::linkKnown(
1157 $rcObj->getTitle(),
1158 $this->message['hist'],
1159 array(),
1160 $query
1161 ) . ')';
1163 $r .= ' . . ';
1164 # Character diff
1165 if( $wgRCShowChangedSize && ($cd = $rcObj->getCharacterDifference()) ) {
1166 $r .= "$cd . . ";
1168 # User/talk
1169 $r .= ' '.$rcObj->userlink . $rcObj->usertalklink;
1170 # Log action (if any)
1171 if( $logType ) {
1172 if( $this->isDeleted($rcObj,LogPage::DELETED_ACTION) ) {
1173 $r .= ' <span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
1174 } else {
1175 $r .= ' ' . LogPage::actionText( $logType, $rcObj->mAttribs['rc_log_action'], $rcObj->getTitle(),
1176 $this->getSkin(), LogPage::extractParams( $rcObj->mAttribs['rc_params'] ), true, true );
1179 $this->insertComment( $r, $rcObj );
1180 $this->insertRollback( $r, $rcObj );
1181 # Tags
1182 $classes = explode( ' ', $classes );
1183 $this->insertTags( $r, $rcObj, $classes );
1184 # Show how many people are watching this if enabled
1185 $r .= $this->numberofWatchingusers($rcObj->numberofWatchingusers);
1187 $r .= "</td></tr></table>\n";
1189 wfProfileOut( __METHOD__ );
1191 return $r;
1195 * If enhanced RC is in use, this function takes the previously cached
1196 * RC lines, arranges them, and outputs the HTML
1198 * @return string
1200 protected function recentChangesBlock() {
1201 if( count ( $this->rc_cache ) == 0 ) {
1202 return '';
1205 wfProfileIn( __METHOD__ );
1207 $blockOut = '';
1208 foreach( $this->rc_cache as $block ) {
1209 if( count( $block ) < 2 ) {
1210 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
1211 } else {
1212 $blockOut .= $this->recentChangesBlockGroup( $block );
1216 wfProfileOut( __METHOD__ );
1218 return '<div>'.$blockOut.'</div>';
1222 * Returns text for the end of RC
1223 * If enhanced RC is in use, returns pretty much all the text
1225 public function endRecentChangesList() {
1226 return $this->recentChangesBlock() . parent::endRecentChangesList();