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