Merge "Fix positioning of jQuery.tipsy tooltip arrows"
[mediawiki.git] / includes / changes / ChangesList.php
blob9567700914b62065debeda07f61ab9b986111faf
1 <?php
2 /**
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
22 * @file
25 class ChangesList extends ContextSource {
26 /**
27 * @var Skin
29 public $skin;
31 protected $watchlist = false;
32 protected $lastdate;
33 protected $message;
34 protected $rc_cache;
35 protected $rcCacheIndex;
36 protected $rclistOpen;
37 protected $rcMoveIndex;
39 /** @var BagOStuff */
40 protected $watchMsgCache;
42 /**
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();
51 } else {
52 $this->setContext( $obj->getContext() );
53 $this->skin = $obj;
55 $this->preCacheMessages();
56 $this->watchMsgCache = new HashBagOStuff( array( 'maxKeys' => 50 ) );
59 /**
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
64 * @return ChangesList
66 public static function newFromContext( IContextSource $context ) {
67 $user = $context->getUser();
68 $sk = $context->getSkin();
69 $list = null;
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 );
74 } else {
75 return $list;
79 /**
80 * Format a line
82 * @since 1.27
84 * @param RecentChange $rc Passed by reference
85 * @param bool $watched (default false)
86 * @param int $linenumber (default null)
88 * @return string|bool
90 public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
91 throw new RuntimeException( 'recentChangesLine should be implemented' );
94 /**
95 * Sets the list to use a "<li class='watchlist-(namespace)-(page)'>" tag
96 * @param bool $value
98 public function setWatchlistDivs( $value = true ) {
99 $this->watchlist = $value;
103 * @return bool True when setWatchlistDivs has been called
104 * @since 1.23
106 public function isWatchlist() {
107 return (bool)$this->watchlist;
111 * As we use the same small set of messages in various methods and that
112 * they are called often, we call them once and save them in $this->message
114 private function preCacheMessages() {
115 if ( !isset( $this->message ) ) {
116 foreach ( array(
117 'cur', 'diff', 'hist', 'enhancedrc-history', 'last', 'blocklink', 'history',
118 'semicolon-separator', 'pipe-separator' ) as $msg
120 $this->message[$msg] = $this->msg( $msg )->escaped();
126 * Returns the appropriate flags for new page, minor change and patrolling
127 * @param array $flags Associative array of 'flag' => Bool
128 * @param string $nothing To use for empty space
129 * @return string
131 public function recentChangesFlags( $flags, $nothing = '&#160;' ) {
132 $f = '';
133 foreach ( array_keys( $this->getConfig()->get( 'RecentChangesFlags' ) ) as $flag ) {
134 $f .= isset( $flags[$flag] ) && $flags[$flag]
135 ? self::flag( $flag )
136 : $nothing;
139 return $f;
143 * Provide the "<abbr>" element appropriate to a given abbreviated flag,
144 * namely the flag indicating a new page, a minor edit, a bot edit, or an
145 * unpatrolled edit. By default in English it will contain "N", "m", "b",
146 * "!" respectively, plus it will have an appropriate title and class.
148 * @param string $flag One key of $wgRecentChangesFlags
149 * @return string Raw HTML
151 public static function flag( $flag ) {
152 static $flagInfos = null;
153 if ( is_null( $flagInfos ) ) {
154 global $wgRecentChangesFlags;
155 $flagInfos = array();
156 foreach ( $wgRecentChangesFlags as $key => $value ) {
157 $flagInfos[$key]['letter'] = wfMessage( $value['letter'] )->escaped();
158 $flagInfos[$key]['title'] = wfMessage( $value['title'] )->escaped();
159 // Allow customized class name, fall back to flag name
160 $flagInfos[$key]['class'] = Sanitizer::escapeClass(
161 isset( $value['class'] ) ? $value['class'] : $key );
165 // Inconsistent naming, bleh, kepted for b/c
166 $map = array(
167 'minoredit' => 'minor',
168 'botedit' => 'bot',
170 if ( isset( $map[$flag] ) ) {
171 $flag = $map[$flag];
174 return "<abbr class='" . $flagInfos[$flag]['class'] . "' title='" .
175 $flagInfos[$flag]['title'] . "'>" . $flagInfos[$flag]['letter'] .
176 '</abbr>';
180 * Returns text for the start of the tabular part of RC
181 * @return string
183 public function beginRecentChangesList() {
184 $this->rc_cache = array();
185 $this->rcMoveIndex = 0;
186 $this->rcCacheIndex = 0;
187 $this->lastdate = '';
188 $this->rclistOpen = false;
189 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
191 return '<div class="mw-changeslist">';
195 * @param ResultWrapper|array $rows
197 public function initChangesListRows( $rows ) {
198 Hooks::run( 'ChangesListInitRows', array( $this, $rows ) );
202 * Show formatted char difference
203 * @param int $old Number of bytes
204 * @param int $new Number of bytes
205 * @param IContextSource $context
206 * @return string
208 public static function showCharacterDifference( $old, $new, IContextSource $context = null ) {
209 if ( !$context ) {
210 $context = RequestContext::getMain();
213 $new = (int)$new;
214 $old = (int)$old;
215 $szdiff = $new - $old;
217 $lang = $context->getLanguage();
218 $config = $context->getConfig();
219 $code = $lang->getCode();
220 static $fastCharDiff = array();
221 if ( !isset( $fastCharDiff[$code] ) ) {
222 $fastCharDiff[$code] = $config->get( 'MiserMode' )
223 || $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( $config->get( 'RCChangedSizeThreshold' ) ) ) {
233 $tag = 'strong';
234 } else {
235 $tag = 'span';
238 if ( $szdiff === 0 ) {
239 $formattedSizeClass = 'mw-plusminus-null';
240 } elseif ( $szdiff > 0 ) {
241 $formattedSize = '+' . $formattedSize;
242 $formattedSizeClass = 'mw-plusminus-pos';
243 } else {
244 $formattedSizeClass = 'mw-plusminus-neg';
247 $formattedTotalSize = $context->msg( 'rc-change-size-new' )->numParams( $new )->text();
249 return Html::element( $tag,
250 array( 'dir' => 'ltr', 'class' => $formattedSizeClass, 'title' => $formattedTotalSize ),
251 $context->msg( 'parentheses', $formattedSize )->plain() ) . $lang->getDirMark();
255 * Format the character difference of one or several changes.
257 * @param RecentChange $old
258 * @param RecentChange $new Last change to use, if not provided, $old will be used
259 * @return string HTML fragment
261 public function formatCharacterDifference( RecentChange $old, RecentChange $new = null ) {
262 $oldlen = $old->mAttribs['rc_old_len'];
264 if ( $new ) {
265 $newlen = $new->mAttribs['rc_new_len'];
266 } else {
267 $newlen = $old->mAttribs['rc_new_len'];
270 if ( $oldlen === null || $newlen === null ) {
271 return '';
274 return self::showCharacterDifference( $oldlen, $newlen, $this->getContext() );
278 * Returns text for the end of RC
279 * @return string
281 public function endRecentChangesList() {
282 $out = $this->rclistOpen ? "</ul>\n" : '';
283 $out .= '</div>';
285 return $out;
289 * @param string $s HTML to update
290 * @param mixed $rc_timestamp
292 public function insertDateHeader( &$s, $rc_timestamp ) {
293 # Make date header if necessary
294 $date = $this->getLanguage()->userDate( $rc_timestamp, $this->getUser() );
295 if ( $date != $this->lastdate ) {
296 if ( $this->lastdate != '' ) {
297 $s .= "</ul>\n";
299 $s .= Xml::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
300 $this->lastdate = $date;
301 $this->rclistOpen = true;
306 * @param string $s HTML to update
307 * @param Title $title
308 * @param string $logtype
310 public function insertLog( &$s, $title, $logtype ) {
311 $page = new LogPage( $logtype );
312 $logname = $page->getName()->escaped();
313 $s .= $this->msg( 'parentheses' )->rawParams( Linker::linkKnown( $title, $logname ) )->escaped();
317 * @param string $s HTML to update
318 * @param RecentChange $rc
319 * @param bool $unpatrolled
321 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
322 # Diff link
323 if (
324 $rc->mAttribs['rc_type'] == RC_NEW ||
325 $rc->mAttribs['rc_type'] == RC_LOG ||
326 $rc->mAttribs['rc_type'] == RC_CATEGORIZE
328 $diffLink = $this->message['diff'];
329 } elseif ( !self::userCan( $rc, Revision::DELETED_TEXT, $this->getUser() ) ) {
330 $diffLink = $this->message['diff'];
331 } else {
332 $query = array(
333 'curid' => $rc->mAttribs['rc_cur_id'],
334 'diff' => $rc->mAttribs['rc_this_oldid'],
335 'oldid' => $rc->mAttribs['rc_last_oldid']
338 $diffLink = Linker::linkKnown(
339 $rc->getTitle(),
340 $this->message['diff'],
341 array( 'tabindex' => $rc->counter ),
342 $query
345 if ( $rc->mAttribs['rc_type'] == RC_CATEGORIZE ) {
346 $diffhist = $diffLink . $this->message['pipe-separator'] . $this->message['hist'];
347 } else {
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'
361 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
362 $s .= $this->msg( 'parentheses' )->rawParams( $diffhist )->escaped() .
363 ' <span class="mw-changeslist-separator">. .</span> ';
367 * @param string $s Article link will be appended to this string, in place.
368 * @param RecentChange $rc
369 * @param bool $unpatrolled
370 * @param bool $watched
371 * @deprecated since 1.27, use getArticleLink instead.
373 public function insertArticleLink( &$s, RecentChange $rc, $unpatrolled, $watched ) {
374 $s .= $this->getArticleLink( $rc, $unpatrolled, $watched );
378 * @param RecentChange $rc
379 * @param bool $unpatrolled
380 * @param bool $watched
381 * @return string HTML
382 * @since 1.26
384 public function getArticleLink( &$rc, $unpatrolled, $watched ) {
385 $params = array();
386 if ( $rc->getTitle()->isRedirect() ) {
387 $params = array( 'redirect' => 'no' );
390 $articlelink = Linker::linkKnown(
391 $rc->getTitle(),
392 null,
393 array( 'class' => 'mw-changeslist-title' ),
394 $params
396 if ( $this->isDeleted( $rc, Revision::DELETED_TEXT ) ) {
397 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
399 # To allow for boldening pages watched by this user
400 $articlelink = "<span class=\"mw-title\">{$articlelink}</span>";
401 # RTL/LTR marker
402 $articlelink .= $this->getLanguage()->getDirMark();
404 # TODO: Deprecate the $s argument, it seems happily unused.
405 $s = '';
406 Hooks::run( 'ChangesListInsertArticleLink',
407 array( &$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched ) );
409 return "{$s} {$articlelink}";
413 * Get the timestamp from $rc formatted with current user's settings
414 * and a separator
416 * @param RecentChange $rc
417 * @return string HTML fragment
419 public function getTimestamp( $rc ) {
420 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
421 return $this->message['semicolon-separator'] . '<span class="mw-changeslist-date">' .
422 $this->getLanguage()->userTime(
423 $rc->mAttribs['rc_timestamp'],
424 $this->getUser()
425 ) . '</span> <span class="mw-changeslist-separator">. .</span> ';
429 * Insert time timestamp string from $rc into $s
431 * @param string $s HTML to update
432 * @param RecentChange $rc
434 public function insertTimestamp( &$s, $rc ) {
435 $s .= $this->getTimestamp( $rc );
439 * Insert links to user page, user talk page and eventually a blocking link
441 * @param string &$s HTML to update
442 * @param RecentChange &$rc
444 public function insertUserRelatedLinks( &$s, &$rc ) {
445 if ( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
446 $s .= ' <span class="history-deleted">' .
447 $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
448 } else {
449 $s .= $this->getLanguage()->getDirMark() . Linker::userLink( $rc->mAttribs['rc_user'],
450 $rc->mAttribs['rc_user_text'] );
451 $s .= Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
456 * Insert a formatted action
458 * @param RecentChange $rc
459 * @return string
461 public function insertLogEntry( $rc ) {
462 $formatter = LogFormatter::newFromRow( $rc->mAttribs );
463 $formatter->setContext( $this->getContext() );
464 $formatter->setShowUserToolLinks( true );
465 $mark = $this->getLanguage()->getDirMark();
467 return $formatter->getActionText() . " $mark" . $formatter->getComment();
471 * Insert a formatted comment
472 * @param RecentChange $rc
473 * @return string
475 public function insertComment( $rc ) {
476 if ( $this->isDeleted( $rc, Revision::DELETED_COMMENT ) ) {
477 return ' <span class="history-deleted">' .
478 $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
479 } else {
480 return Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
485 * Check whether to enable recent changes patrol features
487 * @deprecated since 1.22
488 * @return bool
490 public static function usePatrol() {
491 global $wgUser;
493 wfDeprecated( __METHOD__, '1.22' );
495 return $wgUser->useRCPatrol();
499 * Returns the string which indicates the number of watching users
500 * @param int $count Number of user watching a page
501 * @return string
503 protected function numberofWatchingusers( $count ) {
504 if ( $count <= 0 ) {
505 return '';
507 $cache = $this->watchMsgCache;
508 $that = $this;
509 return $cache->getWithSetCallback( $count, $cache::TTL_INDEFINITE,
510 function () use ( $that, $count ) {
511 return $that->msg( 'number_of_watching_users_RCview' )
512 ->numParams( $count )->escaped();
518 * Determine if said field of a revision is hidden
519 * @param RCCacheEntry|RecentChange $rc
520 * @param int $field One of DELETED_* bitfield constants
521 * @return bool
523 public static function isDeleted( $rc, $field ) {
524 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
528 * Determine if the current user is allowed to view a particular
529 * field of this revision, if it's marked as deleted.
530 * @param RCCacheEntry|RecentChange $rc
531 * @param int $field
532 * @param User $user User object to check, or null to use $wgUser
533 * @return bool
535 public static function userCan( $rc, $field, User $user = null ) {
536 if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
537 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
538 } else {
539 return Revision::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
544 * @param string $link
545 * @param bool $watched
546 * @return string
548 protected function maybeWatchedLink( $link, $watched = false ) {
549 if ( $watched ) {
550 return '<strong class="mw-watched">' . $link . '</strong>';
551 } else {
552 return '<span class="mw-rc-unwatched">' . $link . '</span>';
556 /** Inserts a rollback link
558 * @param string $s
559 * @param RecentChange $rc
561 public function insertRollback( &$s, &$rc ) {
562 if ( $rc->mAttribs['rc_type'] == RC_EDIT
563 && $rc->mAttribs['rc_this_oldid']
564 && $rc->mAttribs['rc_cur_id']
566 $page = $rc->getTitle();
567 /** Check for rollback and edit permissions, disallow special pages, and only
568 * show a link on the top-most revision */
569 if ( $this->getUser()->isAllowed( 'rollback' )
570 && $rc->mAttribs['page_latest'] == $rc->mAttribs['rc_this_oldid']
572 $rev = new Revision( array(
573 'title' => $page,
574 'id' => $rc->mAttribs['rc_this_oldid'],
575 'user' => $rc->mAttribs['rc_user'],
576 'user_text' => $rc->mAttribs['rc_user_text'],
577 'deleted' => $rc->mAttribs['rc_deleted']
578 ) );
579 $s .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
585 * @param RecentChange $rc
586 * @return string
587 * @since 1.26
589 public function getRollback( RecentChange $rc ) {
590 $s = '';
591 $this->insertRollback( $s, $rc );
592 return $s;
596 * @param string $s
597 * @param RecentChange $rc
598 * @param array $classes
600 public function insertTags( &$s, &$rc, &$classes ) {
601 if ( empty( $rc->mAttribs['ts_tags'] ) ) {
602 return;
605 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
606 $rc->mAttribs['ts_tags'],
607 'changeslist'
609 $classes = array_merge( $classes, $newClasses );
610 $s .= ' ' . $tagSummary;
614 * @param RecentChange $rc
615 * @param array $classes
616 * @return string
617 * @since 1.26
619 public function getTags( RecentChange $rc, array &$classes ) {
620 $s = '';
621 $this->insertTags( $s, $rc, $classes );
622 return $s;
625 public function insertExtra( &$s, &$rc, &$classes ) {
626 // Empty, used for subclasses to add anything special.
629 protected function showAsUnpatrolled( RecentChange $rc ) {
630 return self::isUnpatrolled( $rc, $this->getUser() );
634 * @param object|RecentChange $rc Database row from recentchanges or a RecentChange object
635 * @param User $user
636 * @return bool
638 public static function isUnpatrolled( $rc, User $user ) {
639 if ( $rc instanceof RecentChange ) {
640 $isPatrolled = $rc->mAttribs['rc_patrolled'];
641 $rcType = $rc->mAttribs['rc_type'];
642 } else {
643 $isPatrolled = $rc->rc_patrolled;
644 $rcType = $rc->rc_type;
647 if ( !$isPatrolled ) {
648 if ( $user->useRCPatrol() ) {
649 return true;
651 if ( $user->useNPPatrol() && $rcType == RC_NEW ) {
652 return true;
656 return false;
660 * Determines whether a revision is linked to this change; this may not be the case
661 * when the categorization wasn't done by an edit but a conditional parser function
663 * @since 1.27
665 * @param RecentChange|RCCacheEntry $rcObj
666 * @return bool
668 protected function isCategorizationWithoutRevision( $rcObj ) {
669 return intval( $rcObj->getAttribute( 'rc_type' ) ) === RC_CATEGORIZE
670 && intval( $rcObj->getAttribute( 'rc_this_oldid' ) ) === 0;