Merge "Add ss_active_users in SiteStats::isSane"
[mediawiki.git] / includes / ChangesList.php
blob1b6b396c93c3875a989710a479a71d442be85fa7
1 <?php
2 /**
3 * Classes to show lists of changes.
5 * These can be:
6 * - watchlist
7 * - related changes
8 * - recent changes
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
25 * @file
28 /**
29 * @todo document
31 class RCCacheEntry extends RecentChange {
32 var $secureName, $link;
33 var $curlink, $difflink, $lastlink, $usertalklink, $versionlink;
34 var $userlink, $timestamp, $watched;
36 /**
37 * @param $rc RecentChange
38 * @return RCCacheEntry
40 static function newFromParent( $rc ) {
41 $rc2 = new RCCacheEntry;
42 $rc2->mAttribs = $rc->mAttribs;
43 $rc2->mExtra = $rc->mExtra;
44 return $rc2;
48 /**
49 * Base class for all changes lists
51 class ChangesList extends ContextSource {
53 /**
54 * @var Skin
56 public $skin;
58 protected $watchlist = false;
60 protected $message;
62 /**
63 * Changeslist constructor
65 * @param $obj Skin or IContextSource
67 public function __construct( $obj ) {
68 if ( $obj instanceof IContextSource ) {
69 $this->setContext( $obj );
70 $this->skin = $obj->getSkin();
71 } else {
72 $this->setContext( $obj->getContext() );
73 $this->skin = $obj;
75 $this->preCacheMessages();
78 /**
79 * Fetch an appropriate changes list class for the main context
80 * This first argument used to be an User object.
82 * @deprecated in 1.18; use newFromContext() instead
83 * @param string|User $unused Unused
84 * @return ChangesList|EnhancedChangesList|OldChangesList derivative
86 public static function newFromUser( $unused ) {
87 wfDeprecated( __METHOD__, '1.18' );
88 return self::newFromContext( RequestContext::getMain() );
91 /**
92 * Fetch an appropriate changes list class for the specified context
93 * Some users might want to use an enhanced list format, for instance
95 * @param $context IContextSource to use
96 * @return ChangesList|EnhancedChangesList|OldChangesList derivative
98 public static function newFromContext( IContextSource $context ) {
99 $user = $context->getUser();
100 $sk = $context->getSkin();
101 $list = null;
102 if ( wfRunHooks( 'FetchChangesList', array( $user, &$sk, &$list ) ) ) {
103 $new = $context->getRequest()->getBool( 'enhanced', $user->getOption( 'usenewrc' ) );
104 return $new ? new EnhancedChangesList( $context ) : new OldChangesList( $context );
105 } else {
106 return $list;
111 * Sets the list to use a "<li class='watchlist-(namespace)-(page)'>" tag
112 * @param $value Boolean
114 public function setWatchlistDivs( $value = true ) {
115 $this->watchlist = $value;
119 * As we use the same small set of messages in various methods and that
120 * they are called often, we call them once and save them in $this->message
122 private function preCacheMessages() {
123 if ( !isset( $this->message ) ) {
124 foreach ( array(
125 'cur', 'diff', 'hist', 'last', 'blocklink', 'history',
126 'semicolon-separator', 'pipe-separator' ) as $msg
128 $this->message[$msg] = $this->msg( $msg )->escaped();
134 * Returns the appropriate flags for new page, minor change and patrolling
135 * @param array $flags Associative array of 'flag' => Bool
136 * @param string $nothing to use for empty space
137 * @return String
139 protected function recentChangesFlags( $flags, $nothing = '&#160;' ) {
140 $f = '';
141 foreach ( array( 'newpage', 'minor', 'bot', 'unpatrolled' ) as $flag ) {
142 $f .= isset( $flags[$flag] ) && $flags[$flag]
143 ? self::flag( $flag )
144 : $nothing;
146 return $f;
150 * Provide the "<abbr>" element appropriate to a given abbreviated flag,
151 * namely the flag indicating a new page, a minor edit, a bot edit, or an
152 * unpatrolled edit. By default in English it will contain "N", "m", "b",
153 * "!" respectively, plus it will have an appropriate title and class.
155 * @param string $flag 'newpage', 'unpatrolled', 'minor', or 'bot'
156 * @return String: Raw HTML
158 public static function flag( $flag ) {
159 static $messages = null;
160 if ( is_null( $messages ) ) {
161 $messages = array(
162 'newpage' => array( 'newpageletter', 'recentchanges-label-newpage' ),
163 'minoredit' => array( 'minoreditletter', 'recentchanges-label-minor' ),
164 'botedit' => array( 'boteditletter', 'recentchanges-label-bot' ),
165 'unpatrolled' => array( 'unpatrolledletter', 'recentchanges-label-unpatrolled' ),
167 foreach ( $messages as &$value ) {
168 $value[0] = wfMessage( $value[0] )->escaped();
169 $value[1] = wfMessage( $value[1] )->escaped();
173 # Inconsistent naming, bleh
174 $map = array(
175 'newpage' => 'newpage',
176 'minor' => 'minoredit',
177 'bot' => 'botedit',
178 'unpatrolled' => 'unpatrolled',
179 'minoredit' => 'minoredit',
180 'botedit' => 'botedit',
182 $flag = $map[$flag];
184 return "<abbr class='$flag' title='" . $messages[$flag][1] . "'>" . $messages[$flag][0] . '</abbr>';
188 * Returns text for the start of the tabular part of RC
189 * @return String
191 public function beginRecentChangesList() {
192 $this->rc_cache = array();
193 $this->rcMoveIndex = 0;
194 $this->rcCacheIndex = 0;
195 $this->lastdate = '';
196 $this->rclistOpen = false;
197 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
198 return '';
202 * Show formatted char difference
203 * @param $old Integer: bytes
204 * @param $new Integer: bytes
205 * @param $context IContextSource context to use
206 * @return String
208 public static function showCharacterDifference( $old, $new, IContextSource $context = null ) {
209 global $wgRCChangedSizeThreshold, $wgMiserMode;
211 if ( !$context ) {
212 $context = RequestContext::getMain();
215 $new = (int)$new;
216 $old = (int)$old;
217 $szdiff = $new - $old;
219 $lang = $context->getLanguage();
220 $code = $lang->getCode();
221 static $fastCharDiff = array();
222 if ( !isset( $fastCharDiff[$code] ) ) {
223 $fastCharDiff[$code] = $wgMiserMode || $context->msg( 'rc-change-size' )->plain() === '$1';
226 $formattedSize = $lang->formatNum( $szdiff );
228 if ( !$fastCharDiff[$code] ) {
229 $formattedSize = $context->msg( 'rc-change-size', $formattedSize )->text();
232 if ( abs( $szdiff ) > abs( $wgRCChangedSizeThreshold ) ) {
233 $tag = 'strong';
234 } else {
235 $tag = 'span';
238 if ( $szdiff === 0 ) {
239 $formattedSizeClass = 'mw-plusminus-null';
241 if ( $szdiff > 0 ) {
242 $formattedSize = '+' . $formattedSize;
243 $formattedSizeClass = 'mw-plusminus-pos';
245 if ( $szdiff < 0 ) {
246 $formattedSizeClass = 'mw-plusminus-neg';
249 $formattedTotalSize = $context->msg( 'rc-change-size-new' )->numParams( $new )->text();
251 return Html::element( $tag,
252 array( 'dir' => 'ltr', 'class' => $formattedSizeClass, 'title' => $formattedTotalSize ),
253 $context->msg( 'parentheses', $formattedSize )->plain() ) . $lang->getDirMark();
257 * Format the character difference of one or several changes.
259 * @param $old RecentChange
260 * @param $new RecentChange last change to use, if not provided, $old will be used
261 * @return string HTML fragment
263 public function formatCharacterDifference( RecentChange $old, RecentChange $new = null ) {
264 $oldlen = $old->mAttribs['rc_old_len'];
266 if ( $new ) {
267 $newlen = $new->mAttribs['rc_new_len'];
268 } else {
269 $newlen = $old->mAttribs['rc_new_len'];
272 if ( $oldlen === null || $newlen === null ) {
273 return '';
276 return self::showCharacterDifference( $oldlen, $newlen, $this->getContext() );
280 * Returns text for the end of RC
281 * @return String
283 public function endRecentChangesList() {
284 if ( $this->rclistOpen ) {
285 return "</ul>\n";
286 } else {
287 return '';
292 * @param string $s HTML to update
293 * @param $rc_timestamp mixed
295 public function insertDateHeader( &$s, $rc_timestamp ) {
296 # Make date header if necessary
297 $date = $this->getLanguage()->userDate( $rc_timestamp, $this->getUser() );
298 if ( $date != $this->lastdate ) {
299 if ( $this->lastdate != '' ) {
300 $s .= "</ul>\n";
302 $s .= Xml::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
303 $this->lastdate = $date;
304 $this->rclistOpen = true;
309 * @param string $s HTML to update
310 * @param $title Title
311 * @param $logtype string
313 public function insertLog( &$s, $title, $logtype ) {
314 $page = new LogPage( $logtype );
315 $logname = $page->getName()->escaped();
316 $s .= $this->msg( 'parentheses' )->rawParams( Linker::linkKnown( $title, $logname ) )->escaped();
320 * @param string $s HTML to update
321 * @param $rc RecentChange
322 * @param $unpatrolled
324 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
325 # Diff link
326 if ( $rc->mAttribs['rc_type'] == RC_NEW || $rc->mAttribs['rc_type'] == RC_LOG ) {
327 $diffLink = $this->message['diff'];
328 } elseif ( !self::userCan( $rc, Revision::DELETED_TEXT, $this->getUser() ) ) {
329 $diffLink = $this->message['diff'];
330 } else {
331 $query = array(
332 'curid' => $rc->mAttribs['rc_cur_id'],
333 'diff' => $rc->mAttribs['rc_this_oldid'],
334 'oldid' => $rc->mAttribs['rc_last_oldid']
337 if ( $unpatrolled ) {
338 $query['rcid'] = $rc->mAttribs['rc_id'];
341 $diffLink = Linker::linkKnown(
342 $rc->getTitle(),
343 $this->message['diff'],
344 array( 'tabindex' => $rc->counter ),
345 $query
348 $diffhist = $diffLink . $this->message['pipe-separator'];
349 # History link
350 $diffhist .= Linker::linkKnown(
351 $rc->getTitle(),
352 $this->message['hist'],
353 array(),
354 array(
355 'curid' => $rc->mAttribs['rc_cur_id'],
356 'action' => 'history'
359 $s .= $this->msg( 'parentheses' )->rawParams( $diffhist )->escaped() . ' <span class="mw-changeslist-separator">. .</span> ';
363 * @param string $s HTML to update
364 * @param $rc RecentChange
365 * @param $unpatrolled
366 * @param $watched
368 public function insertArticleLink( &$s, &$rc, $unpatrolled, $watched ) {
369 # If it's a new article, there is no diff link, but if it hasn't been
370 # patrolled yet, we need to give users a way to do so
371 $params = array();
373 if ( $unpatrolled && $rc->mAttribs['rc_type'] == RC_NEW ) {
374 $params['rcid'] = $rc->mAttribs['rc_id'];
377 $articlelink = Linker::linkKnown(
378 $rc->getTitle(),
379 null,
380 array( 'class' => 'mw-changeslist-title' ),
381 $params
383 if ( $this->isDeleted( $rc, Revision::DELETED_TEXT ) ) {
384 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
386 # To allow for boldening pages watched by this user
387 $articlelink = "<span class=\"mw-title\">{$articlelink}</span>";
388 # RTL/LTR marker
389 $articlelink .= $this->getLanguage()->getDirMark();
391 wfRunHooks( 'ChangesListInsertArticleLink',
392 array( &$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched ) );
394 $s .= " $articlelink";
398 * Get the timestamp from $rc formatted with current user's settings
399 * and a separator
401 * @param $rc RecentChange
402 * @return string HTML fragment
404 public function getTimestamp( $rc ) {
405 return $this->message['semicolon-separator'] . '<span class="mw-changeslist-date">' .
406 $this->getLanguage()->userTime( $rc->mAttribs['rc_timestamp'], $this->getUser() ) . '</span> <span class="mw-changeslist-separator">. .</span> ';
410 * Insert time timestamp string from $rc into $s
412 * @param string $s HTML to update
413 * @param $rc RecentChange
415 public function insertTimestamp( &$s, $rc ) {
416 $s .= $this->getTimestamp( $rc );
420 * Insert links to user page, user talk page and eventually a blocking link
422 * @param &$s String HTML to update
423 * @param &$rc RecentChange
425 public function insertUserRelatedLinks( &$s, &$rc ) {
426 if ( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
427 $s .= ' <span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
428 } else {
429 $s .= $this->getLanguage()->getDirMark() . Linker::userLink( $rc->mAttribs['rc_user'],
430 $rc->mAttribs['rc_user_text'] );
431 $s .= Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
436 * Insert a formatted action
438 * @param $rc RecentChange
439 * @return string
441 public function insertLogEntry( $rc ) {
442 $formatter = LogFormatter::newFromRow( $rc->mAttribs );
443 $formatter->setContext( $this->getContext() );
444 $formatter->setShowUserToolLinks( true );
445 $mark = $this->getLanguage()->getDirMark();
446 return $formatter->getActionText() . " $mark" . $formatter->getComment();
450 * Insert a formatted comment
451 * @param $rc RecentChange
452 * @return string
454 public function insertComment( $rc ) {
455 if ( $rc->mAttribs['rc_type'] != RC_MOVE && $rc->mAttribs['rc_type'] != RC_MOVE_OVER_REDIRECT ) {
456 if ( $this->isDeleted( $rc, Revision::DELETED_COMMENT ) ) {
457 return ' <span class="history-deleted">' . $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
458 } else {
459 return Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
462 return '';
466 * Check whether to enable recent changes patrol features
467 * @return Boolean
469 public static function usePatrol() {
470 global $wgUser;
471 return $wgUser->useRCPatrol();
475 * Returns the string which indicates the number of watching users
476 * @return string
478 protected function numberofWatchingusers( $count ) {
479 static $cache = array();
480 if ( $count > 0 ) {
481 if ( !isset( $cache[$count] ) ) {
482 $cache[$count] = $this->msg( 'number_of_watching_users_RCview' )->numParams( $count )->escaped();
484 return $cache[$count];
485 } else {
486 return '';
491 * Determine if said field of a revision is hidden
492 * @param $rc RCCacheEntry
493 * @param $field Integer: one of DELETED_* bitfield constants
494 * @return Boolean
496 public static function isDeleted( $rc, $field ) {
497 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
501 * Determine if the current user is allowed to view a particular
502 * field of this revision, if it's marked as deleted.
503 * @param $rc RCCacheEntry
504 * @param $field Integer
505 * @param $user User object to check, or null to use $wgUser
506 * @return Boolean
508 public static function userCan( $rc, $field, User $user = null ) {
509 if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
510 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
511 } else {
512 return Revision::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
517 * @param $link string
518 * @param $watched bool
519 * @return string
521 protected function maybeWatchedLink( $link, $watched = false ) {
522 if ( $watched ) {
523 return '<strong class="mw-watched">' . $link . '</strong>';
524 } else {
525 return '<span class="mw-rc-unwatched">' . $link . '</span>';
529 /** Inserts a rollback link
531 * @param $s string
532 * @param $rc RecentChange
534 public function insertRollback( &$s, &$rc ) {
535 if ( $rc->mAttribs['rc_type'] != RC_NEW && $rc->mAttribs['rc_this_oldid'] && $rc->mAttribs['rc_cur_id'] ) {
536 $page = $rc->getTitle();
537 /** Check for rollback and edit permissions, disallow special pages, and only
538 * show a link on the top-most revision */
539 if ( $this->getUser()->isAllowed( 'rollback' ) && $rc->mAttribs['page_latest'] == $rc->mAttribs['rc_this_oldid'] )
541 $rev = new Revision( array(
542 'title' => $page,
543 'id' => $rc->mAttribs['rc_this_oldid'],
544 'user' => $rc->mAttribs['rc_user'],
545 'user_text' => $rc->mAttribs['rc_user_text'],
546 'deleted' => $rc->mAttribs['rc_deleted']
547 ) );
548 $s .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
554 * @param $s string
555 * @param $rc RecentChange
556 * @param $classes
558 public function insertTags( &$s, &$rc, &$classes ) {
559 if ( empty( $rc->mAttribs['ts_tags'] ) ) {
560 return;
563 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow( $rc->mAttribs['ts_tags'], 'changeslist' );
564 $classes = array_merge( $classes, $newClasses );
565 $s .= ' ' . $tagSummary;
568 public function insertExtra( &$s, &$rc, &$classes ) {
569 // Empty, used for subclasses to add anything special.
572 protected function showAsUnpatrolled( RecentChange $rc ) {
573 $unpatrolled = false;
574 if ( !$rc->mAttribs['rc_patrolled'] ) {
575 if ( $this->getUser()->useRCPatrol() ) {
576 $unpatrolled = true;
577 } elseif ( $this->getUser()->useNPPatrol() && $rc->mAttribs['rc_type'] == RC_NEW ) {
578 $unpatrolled = true;
581 return $unpatrolled;
586 * Generate a list of changes using the good old system (no javascript)
588 class OldChangesList extends ChangesList {
590 * Format a line using the old system (aka without any javascript).
592 * @param $rc RecentChange, passed by reference
593 * @param bool $watched (default false)
594 * @param int $linenumber (default null)
596 * @return string|bool
598 public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
599 global $wgRCShowChangedSize;
600 wfProfileIn( __METHOD__ );
602 # Should patrol-related stuff be shown?
603 $unpatrolled = $this->showAsUnpatrolled( $rc );
605 $dateheader = ''; // $s now contains only <li>...</li>, for hooks' convenience.
606 $this->insertDateHeader( $dateheader, $rc->mAttribs['rc_timestamp'] );
608 $s = '';
609 $classes = array();
610 // use mw-line-even/mw-line-odd class only if linenumber is given (feature from bug 14468)
611 if ( $linenumber ) {
612 if ( $linenumber & 1 ) {
613 $classes[] = 'mw-line-odd';
614 } else {
615 $classes[] = 'mw-line-even';
619 // Indicate watched status on the line to allow for more
620 // comprehensive styling.
621 $classes[] = $watched && $rc->mAttribs['rc_timestamp'] >= $watched
622 ? 'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
624 // Moved pages (very very old, not supported anymore)
625 if ( $rc->mAttribs['rc_type'] == RC_MOVE || $rc->mAttribs['rc_type'] == RC_MOVE_OVER_REDIRECT ) {
626 // Log entries
627 } elseif ( $rc->mAttribs['rc_log_type'] ) {
628 $logtitle = SpecialPage::getTitleFor( 'Log', $rc->mAttribs['rc_log_type'] );
629 $this->insertLog( $s, $logtitle, $rc->mAttribs['rc_log_type'] );
630 // Log entries (old format) or log targets, and special pages
631 } elseif ( $rc->mAttribs['rc_namespace'] == NS_SPECIAL ) {
632 list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $rc->mAttribs['rc_title'] );
633 if ( $name == 'Log' ) {
634 $this->insertLog( $s, $rc->getTitle(), $subpage );
636 // Regular entries
637 } else {
638 $this->insertDiffHist( $s, $rc, $unpatrolled );
639 # M, N, b and ! (minor, new, bot and unpatrolled)
640 $s .= $this->recentChangesFlags(
641 array(
642 'newpage' => $rc->mAttribs['rc_type'] == RC_NEW,
643 'minor' => $rc->mAttribs['rc_minor'],
644 'unpatrolled' => $unpatrolled,
645 'bot' => $rc->mAttribs['rc_bot']
649 $this->insertArticleLink( $s, $rc, $unpatrolled, $watched );
651 # Edit/log timestamp
652 $this->insertTimestamp( $s, $rc );
653 # Bytes added or removed
654 if ( $wgRCShowChangedSize ) {
655 $cd = $this->formatCharacterDifference( $rc );
656 if ( $cd !== '' ) {
657 $s .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
661 if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
662 $s .= $this->insertLogEntry( $rc );
663 } else {
664 # User tool links
665 $this->insertUserRelatedLinks( $s, $rc );
666 # LTR/RTL direction mark
667 $s .= $this->getLanguage()->getDirMark();
668 $s .= $this->insertComment( $rc );
671 # Tags
672 $this->insertTags( $s, $rc, $classes );
673 # Rollback
674 $this->insertRollback( $s, $rc );
675 # For subclasses
676 $this->insertExtra( $s, $rc, $classes );
678 # How many users watch this page
679 if ( $rc->numberofWatchingusers > 0 ) {
680 $s .= ' ' . $this->numberofWatchingusers( $rc->numberofWatchingusers );
683 if ( $this->watchlist ) {
684 $classes[] = Sanitizer::escapeClass( 'watchlist-' . $rc->mAttribs['rc_namespace'] . '-' . $rc->mAttribs['rc_title'] );
687 if ( !wfRunHooks( 'OldChangesListRecentChangesLine', array( &$this, &$s, $rc, &$classes ) ) ) {
688 wfProfileOut( __METHOD__ );
689 return false;
692 wfProfileOut( __METHOD__ );
693 return "$dateheader<li class=\"" . implode( ' ', $classes ) . "\">" . $s . "</li>\n";
698 * Generate a list of changes using an Enhanced system (uses javascript).
700 class EnhancedChangesList extends ChangesList {
702 protected $rc_cache;
705 * Add the JavaScript file for enhanced changeslist
706 * @return String
708 public function beginRecentChangesList() {
709 $this->rc_cache = array();
710 $this->rcMoveIndex = 0;
711 $this->rcCacheIndex = 0;
712 $this->lastdate = '';
713 $this->rclistOpen = false;
714 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
715 return '';
718 * Format a line for enhanced recentchange (aka with javascript and block of lines).
720 * @param $baseRC RecentChange
721 * @param $watched bool
723 * @return string
725 public function recentChangesLine( &$baseRC, $watched = false ) {
726 wfProfileIn( __METHOD__ );
728 # Create a specialised object
729 $rc = RCCacheEntry::newFromParent( $baseRC );
731 $curIdEq = array( 'curid' => $rc->mAttribs['rc_cur_id'] );
733 # If it's a new day, add the headline and flush the cache
734 $date = $this->getLanguage()->userDate( $rc->mAttribs['rc_timestamp'], $this->getUser() );
735 $ret = '';
736 if ( $date != $this->lastdate ) {
737 # Process current cache
738 $ret = $this->recentChangesBlock();
739 $this->rc_cache = array();
740 $ret .= Xml::element( 'h4', null, $date ) . "\n";
741 $this->lastdate = $date;
744 # Should patrol-related stuff be shown?
745 $rc->unpatrolled = $this->showAsUnpatrolled( $rc );
747 $showdifflinks = true;
748 # Make article link
749 $type = $rc->mAttribs['rc_type'];
750 $logType = $rc->mAttribs['rc_log_type'];
751 // Page moves, very old style, not supported anymore
752 if ( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
753 // New unpatrolled pages
754 } elseif ( $rc->unpatrolled && $type == RC_NEW ) {
755 $clink = Linker::linkKnown( $rc->getTitle(), null, array(),
756 array( 'rcid' => $rc->mAttribs['rc_id'] ) );
757 // Log entries
758 } elseif ( $type == RC_LOG ) {
759 if ( $logType ) {
760 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
761 $logpage = new LogPage( $logType );
762 $logname = $logpage->getName()->escaped();
763 $clink = $this->msg( 'parentheses' )->rawParams( Linker::linkKnown( $logtitle, $logname ) )->escaped();
764 } else {
765 $clink = Linker::link( $rc->getTitle() );
767 $watched = false;
768 // Log entries (old format) and special pages
769 } elseif ( $rc->mAttribs['rc_namespace'] == NS_SPECIAL ) {
770 wfDebug( "Unexpected special page in recentchanges\n" );
771 $clink = '';
772 // Edits
773 } else {
774 $clink = Linker::linkKnown( $rc->getTitle() );
777 # Don't show unusable diff links
778 if ( !ChangesList::userCan( $rc, Revision::DELETED_TEXT, $this->getUser() ) ) {
779 $showdifflinks = false;
782 $time = $this->getLanguage()->userTime( $rc->mAttribs['rc_timestamp'], $this->getUser() );
783 $rc->watched = $watched;
784 $rc->link = $clink;
785 $rc->timestamp = $time;
786 $rc->numberofWatchingusers = $baseRC->numberofWatchingusers;
788 # Make "cur" and "diff" links. Do not use link(), it is too slow if
789 # called too many times (50% of CPU time on RecentChanges!).
790 $thisOldid = $rc->mAttribs['rc_this_oldid'];
791 $lastOldid = $rc->mAttribs['rc_last_oldid'];
792 if ( $rc->unpatrolled ) {
793 $rcIdQuery = array( 'rcid' => $rc->mAttribs['rc_id'] );
794 } else {
795 $rcIdQuery = array();
797 $querycur = $curIdEq + array( 'diff' => '0', 'oldid' => $thisOldid );
798 $querydiff = $curIdEq + array( 'diff' => $thisOldid, 'oldid' =>
799 $lastOldid ) + $rcIdQuery;
801 if ( !$showdifflinks ) {
802 $curLink = $this->message['cur'];
803 $diffLink = $this->message['diff'];
804 } elseif ( in_array( $type, array( RC_NEW, RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT ) ) ) {
805 if ( $type != RC_NEW ) {
806 $curLink = $this->message['cur'];
807 } else {
808 $curUrl = htmlspecialchars( $rc->getTitle()->getLinkURL( $querycur ) );
809 $curLink = "<a href=\"$curUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['cur']}</a>";
811 $diffLink = $this->message['diff'];
812 } else {
813 $diffUrl = htmlspecialchars( $rc->getTitle()->getLinkURL( $querydiff ) );
814 $curUrl = htmlspecialchars( $rc->getTitle()->getLinkURL( $querycur ) );
815 $diffLink = "<a href=\"$diffUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['diff']}</a>";
816 $curLink = "<a href=\"$curUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['cur']}</a>";
819 # Make "last" link
820 if ( !$showdifflinks || !$lastOldid ) {
821 $lastLink = $this->message['last'];
822 } elseif ( in_array( $type, array( RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT ) ) ) {
823 $lastLink = $this->message['last'];
824 } else {
825 $lastLink = Linker::linkKnown( $rc->getTitle(), $this->message['last'],
826 array(), $curIdEq + array( 'diff' => $thisOldid, 'oldid' => $lastOldid ) + $rcIdQuery );
829 # Make user links
830 if ( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
831 $rc->userlink = ' <span class="history-deleted">' . $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
832 } else {
833 $rc->userlink = Linker::userLink( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
834 $rc->usertalklink = Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
837 $rc->lastlink = $lastLink;
838 $rc->curlink = $curLink;
839 $rc->difflink = $diffLink;
841 # Put accumulated information into the cache, for later display
842 # Page moves go on their own line
843 $title = $rc->getTitle();
844 $secureName = $title->getPrefixedDBkey();
845 if ( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
846 # Use an @ character to prevent collision with page names
847 $this->rc_cache['@@' . ( $this->rcMoveIndex++ )] = array( $rc );
848 } else {
849 # Logs are grouped by type
850 if ( $type == RC_LOG ) {
851 $secureName = SpecialPage::getTitleFor( 'Log', $logType )->getPrefixedDBkey();
853 if ( !isset( $this->rc_cache[$secureName] ) ) {
854 $this->rc_cache[$secureName] = array();
857 array_push( $this->rc_cache[$secureName], $rc );
860 wfProfileOut( __METHOD__ );
862 return $ret;
866 * Enhanced RC group
867 * @return string
869 protected function recentChangesBlockGroup( $block ) {
870 global $wgRCShowChangedSize;
872 wfProfileIn( __METHOD__ );
874 # Add the namespace and title of the block as part of the class
875 $classes = array( 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' );
876 if ( $block[0]->mAttribs['rc_log_type'] ) {
877 # Log entry
878 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-'
879 . $block[0]->mAttribs['rc_log_type'] . '-' . $block[0]->mAttribs['rc_title'] );
880 } else {
881 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns'
882 . $block[0]->mAttribs['rc_namespace'] . '-' . $block[0]->mAttribs['rc_title'] );
884 $classes[] = $block[0]->watched && $block[0]->mAttribs['rc_timestamp'] >= $block[0]->watched
885 ? 'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
886 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
887 Html::openElement( 'tr' );
889 # Collate list of users
890 $userlinks = array();
891 # Other properties
892 $unpatrolled = false;
893 $isnew = false;
894 $allBots = true;
895 $allMinors = true;
896 $curId = $currentRevision = 0;
897 # Some catalyst variables...
898 $namehidden = true;
899 $allLogs = true;
900 foreach ( $block as $rcObj ) {
901 $oldid = $rcObj->mAttribs['rc_last_oldid'];
902 if ( $rcObj->mAttribs['rc_type'] == RC_NEW ) {
903 $isnew = true;
905 // If all log actions to this page were hidden, then don't
906 // give the name of the affected page for this block!
907 if ( !$this->isDeleted( $rcObj, LogPage::DELETED_ACTION ) ) {
908 $namehidden = false;
910 $u = $rcObj->userlink;
911 if ( !isset( $userlinks[$u] ) ) {
912 $userlinks[$u] = 0;
914 if ( $rcObj->unpatrolled ) {
915 $unpatrolled = true;
917 if ( $rcObj->mAttribs['rc_type'] != RC_LOG ) {
918 $allLogs = false;
920 # Get the latest entry with a page_id and oldid
921 # since logs may not have these.
922 if ( !$curId && $rcObj->mAttribs['rc_cur_id'] ) {
923 $curId = $rcObj->mAttribs['rc_cur_id'];
925 if ( !$currentRevision && $rcObj->mAttribs['rc_this_oldid'] ) {
926 $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
929 if ( !$rcObj->mAttribs['rc_bot'] ) {
930 $allBots = false;
932 if ( !$rcObj->mAttribs['rc_minor'] ) {
933 $allMinors = false;
936 $userlinks[$u]++;
939 # Sort the list and convert to text
940 krsort( $userlinks );
941 asort( $userlinks );
942 $users = array();
943 foreach ( $userlinks as $userlink => $count ) {
944 $text = $userlink;
945 $text .= $this->getLanguage()->getDirMark();
946 if ( $count > 1 ) {
947 $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->formatNum( $count ) . '×' )->escaped();
949 array_push( $users, $text );
952 $users = ' <span class="changedby">'
953 . $this->msg( 'brackets' )->rawParams(
954 implode( $this->message['semicolon-separator'], $users )
955 )->escaped() . '</span>';
957 $tl = '<span class="mw-collapsible-toggle mw-enhancedchanges-arrow mw-enhancedchanges-arrow-space"></span>';
958 $r .= "<td>$tl</td>";
960 # Main line
961 $r .= '<td class="mw-enhanced-rc">' . $this->recentChangesFlags( array(
962 'newpage' => $isnew, # show, when one have this flag
963 'minor' => $allMinors, # show only, when all have this flag
964 'unpatrolled' => $unpatrolled, # show, when one have this flag
965 'bot' => $allBots, # show only, when all have this flag
966 ) );
968 # Timestamp
969 $r .= '&#160;' . $block[0]->timestamp . '&#160;</td><td>';
971 # Article link
972 if ( $namehidden ) {
973 $r .= ' <span class="history-deleted">' . $this->msg( 'rev-deleted-event' )->escaped() . '</span>';
974 } elseif ( $allLogs ) {
975 $r .= $this->maybeWatchedLink( $block[0]->link, $block[0]->watched );
976 } else {
977 $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled, $block[0]->watched );
980 $r .= $this->getLanguage()->getDirMark();
982 $queryParams['curid'] = $curId;
983 # Changes message
984 $n = count( $block );
985 static $nchanges = array();
986 if ( !isset( $nchanges[$n] ) ) {
987 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
989 # Total change link
990 $r .= ' ';
991 $logtext = '';
992 if ( !$allLogs ) {
993 if ( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT, $this->getUser() ) ) {
994 $logtext .= $nchanges[$n];
995 } elseif ( $isnew ) {
996 $logtext .= $nchanges[$n];
997 } else {
998 $params = $queryParams;
999 $params['diff'] = $currentRevision;
1000 $params['oldid'] = $oldid;
1002 $logtext .= Linker::link(
1003 $block[0]->getTitle(),
1004 $nchanges[$n],
1005 array(),
1006 $params,
1007 array( 'known', 'noclasses' )
1012 # History
1013 if ( $allLogs ) {
1014 // don't show history link for logs
1015 } elseif ( $namehidden || !$block[0]->getTitle()->exists() ) {
1016 $logtext .= $this->message['pipe-separator'] . $this->message['hist'];
1017 } else {
1018 $params = $queryParams;
1019 $params['action'] = 'history';
1021 $logtext .= $this->message['pipe-separator'] .
1022 Linker::linkKnown(
1023 $block[0]->getTitle(),
1024 $this->message['hist'],
1025 array(),
1026 $params
1030 if ( $logtext !== '' ) {
1031 $r .= $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
1034 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
1036 # Character difference (does not apply if only log items)
1037 if ( $wgRCShowChangedSize && !$allLogs ) {
1038 $last = 0;
1039 $first = count( $block ) - 1;
1040 # Some events (like logs) have an "empty" size, so we need to skip those...
1041 while ( $last < $first && $block[$last]->mAttribs['rc_new_len'] === null ) {
1042 $last++;
1044 while ( $first > $last && $block[$first]->mAttribs['rc_old_len'] === null ) {
1045 $first--;
1047 # Get net change
1048 $chardiff = $this->formatCharacterDifference( $block[$first], $block[$last] );
1050 if ( $chardiff == '' ) {
1051 $r .= ' ';
1052 } else {
1053 $r .= ' ' . $chardiff . ' <span class="mw-changeslist-separator">. .</span> ';
1057 $r .= $users;
1058 $r .= $this->numberofWatchingusers( $block[0]->numberofWatchingusers );
1060 # Sub-entries
1061 foreach ( $block as $rcObj ) {
1062 # Classes to apply -- TODO implement
1063 $classes = array();
1064 $type = $rcObj->mAttribs['rc_type'];
1066 $trClass = $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched
1067 ? ' class="mw-enhanced-watched"' : '';
1069 $r .= '<tr' . $trClass . '><td></td><td class="mw-enhanced-rc">';
1070 $r .= $this->recentChangesFlags( array(
1071 'newpage' => $type == RC_NEW,
1072 'minor' => $rcObj->mAttribs['rc_minor'],
1073 'unpatrolled' => $rcObj->unpatrolled,
1074 'bot' => $rcObj->mAttribs['rc_bot'],
1075 ) );
1076 $r .= '&#160;</td><td class="mw-enhanced-rc-nested"><span class="mw-enhanced-rc-time">';
1078 $params = $queryParams;
1080 if ( $rcObj->mAttribs['rc_this_oldid'] != 0 ) {
1081 $params['oldid'] = $rcObj->mAttribs['rc_this_oldid'];
1084 # Log timestamp
1085 if ( $type == RC_LOG ) {
1086 $link = $rcObj->timestamp;
1087 # Revision link
1088 } elseif ( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT, $this->getUser() ) ) {
1089 $link = '<span class="history-deleted">' . $rcObj->timestamp . '</span> ';
1090 } else {
1091 if ( $rcObj->unpatrolled && $type == RC_NEW ) {
1092 $params['rcid'] = $rcObj->mAttribs['rc_id'];
1095 $link = Linker::linkKnown(
1096 $rcObj->getTitle(),
1097 $rcObj->timestamp,
1098 array(),
1099 $params
1101 if ( $this->isDeleted( $rcObj, Revision::DELETED_TEXT ) ) {
1102 $link = '<span class="history-deleted">' . $link . '</span> ';
1105 $r .= $link . '</span>';
1107 if ( !$type == RC_LOG || $type == RC_NEW ) {
1108 $r .= ' ' . $this->msg( 'parentheses' )->rawParams( $rcObj->curlink . $this->message['pipe-separator'] . $rcObj->lastlink )->escaped();
1110 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
1112 # Character diff
1113 if ( $wgRCShowChangedSize ) {
1114 $cd = $this->formatCharacterDifference( $rcObj );
1115 if ( $cd !== '' ) {
1116 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
1120 if ( $rcObj->mAttribs['rc_type'] == RC_LOG ) {
1121 $r .= $this->insertLogEntry( $rcObj );
1122 } else {
1123 # User links
1124 $r .= $rcObj->userlink;
1125 $r .= $rcObj->usertalklink;
1126 $r .= $this->insertComment( $rcObj );
1129 # Rollback
1130 $this->insertRollback( $r, $rcObj );
1131 # Tags
1132 $this->insertTags( $r, $rcObj, $classes );
1134 $r .= "</td></tr>\n";
1136 $r .= "</table>\n";
1138 $this->rcCacheIndex++;
1140 wfProfileOut( __METHOD__ );
1142 return $r;
1146 * Generate HTML for an arrow or placeholder graphic
1147 * @param string $dir one of '', 'd', 'l', 'r'
1148 * @param string $alt text
1149 * @param string $title text
1150 * @return String: HTML "<img>" tag
1152 protected function arrow( $dir, $alt = '', $title = '' ) {
1153 global $wgStylePath;
1154 $encUrl = htmlspecialchars( $wgStylePath . '/common/images/Arr_' . $dir . '.png' );
1155 $encAlt = htmlspecialchars( $alt );
1156 $encTitle = htmlspecialchars( $title );
1157 return "<img src=\"$encUrl\" width=\"12\" height=\"12\" alt=\"$encAlt\" title=\"$encTitle\" />";
1161 * Generate HTML for a right- or left-facing arrow,
1162 * depending on language direction.
1163 * @return String: HTML "<img>" tag
1165 protected function sideArrow() {
1166 $dir = $this->getLanguage()->isRTL() ? 'l' : 'r';
1167 return $this->arrow( $dir, '+', $this->msg( 'rc-enhanced-expand' )->text() );
1171 * Generate HTML for a down-facing arrow
1172 * depending on language direction.
1173 * @return String: HTML "<img>" tag
1175 protected function downArrow() {
1176 return $this->arrow( 'd', '-', $this->msg( 'rc-enhanced-hide' )->text() );
1180 * Generate HTML for a spacer image
1181 * @return String: HTML "<img>" tag
1183 protected function spacerArrow() {
1184 return $this->arrow( '', codepointToUtf8( 0xa0 ) ); // non-breaking space
1188 * Enhanced RC ungrouped line.
1190 * @param $rcObj RecentChange
1191 * @return String: a HTML formatted line (generated using $r)
1193 protected function recentChangesBlockLine( $rcObj ) {
1194 global $wgRCShowChangedSize;
1196 wfProfileIn( __METHOD__ );
1197 $query['curid'] = $rcObj->mAttribs['rc_cur_id'];
1199 $type = $rcObj->mAttribs['rc_type'];
1200 $logType = $rcObj->mAttribs['rc_log_type'];
1201 $classes = array( 'mw-enhanced-rc' );
1202 if ( $logType ) {
1203 # Log entry
1204 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-'
1205 . $logType . '-' . $rcObj->mAttribs['rc_title'] );
1206 } else {
1207 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns' .
1208 $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
1210 $classes[] = $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched
1211 ? 'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
1212 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
1213 Html::openElement( 'tr' );
1215 $r .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
1216 # Flag and Timestamp
1217 if ( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
1218 $r .= '&#160;&#160;&#160;&#160;'; // 4 flags -> 4 spaces
1219 } else {
1220 $r .= $this->recentChangesFlags( array(
1221 'newpage' => $type == RC_NEW,
1222 'minor' => $rcObj->mAttribs['rc_minor'],
1223 'unpatrolled' => $rcObj->unpatrolled,
1224 'bot' => $rcObj->mAttribs['rc_bot'],
1225 ) );
1227 $r .= '&#160;' . $rcObj->timestamp . '&#160;</td><td>';
1228 # Article or log link
1229 if ( $logType ) {
1230 $logPage = new LogPage( $logType );
1231 $logTitle = SpecialPage::getTitleFor( 'Log', $logType );
1232 $logName = $logPage->getName()->escaped();
1233 $r .= $this->msg( 'parentheses' )->rawParams( Linker::linkKnown( $logTitle, $logName ) )->escaped();
1234 } else {
1235 $this->insertArticleLink( $r, $rcObj, $rcObj->unpatrolled, $rcObj->watched );
1237 # Diff and hist links
1238 if ( $type != RC_LOG ) {
1239 $query['action'] = 'history';
1240 $r .= ' ' . $this->msg( 'parentheses' )->rawParams( $rcObj->difflink . $this->message['pipe-separator'] . Linker::linkKnown(
1241 $rcObj->getTitle(),
1242 $this->message['hist'],
1243 array(),
1244 $query
1245 ) )->escaped();
1247 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
1248 # Character diff
1249 if ( $wgRCShowChangedSize ) {
1250 $cd = $this->formatCharacterDifference( $rcObj );
1251 if ( $cd !== '' ) {
1252 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
1256 if ( $type == RC_LOG ) {
1257 $r .= $this->insertLogEntry( $rcObj );
1258 } else {
1259 $r .= ' ' . $rcObj->userlink . $rcObj->usertalklink;
1260 $r .= $this->insertComment( $rcObj );
1261 $this->insertRollback( $r, $rcObj );
1264 # Tags
1265 $this->insertTags( $r, $rcObj, $classes );
1266 # Show how many people are watching this if enabled
1267 $r .= $this->numberofWatchingusers( $rcObj->numberofWatchingusers );
1269 $r .= "</td></tr></table>\n";
1271 wfProfileOut( __METHOD__ );
1273 return $r;
1277 * If enhanced RC is in use, this function takes the previously cached
1278 * RC lines, arranges them, and outputs the HTML
1280 * @return string
1282 protected function recentChangesBlock() {
1283 if ( count ( $this->rc_cache ) == 0 ) {
1284 return '';
1287 wfProfileIn( __METHOD__ );
1289 $blockOut = '';
1290 foreach ( $this->rc_cache as $block ) {
1291 if ( count( $block ) < 2 ) {
1292 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
1293 } else {
1294 $blockOut .= $this->recentChangesBlockGroup( $block );
1298 wfProfileOut( __METHOD__ );
1300 return '<div>' . $blockOut . '</div>';
1304 * Returns text for the end of RC
1305 * If enhanced RC is in use, returns pretty much all the text
1306 * @return string
1308 public function endRecentChangesList() {
1309 return $this->recentChangesBlock() . parent::endRecentChangesList();