Merge "Revert "Restore search box tabindex""
[mediawiki.git] / includes / changes / ChangesList.php
blob246f95d211233b36d34a6d58f806afa1ff2ae4bc
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 /**
40 * Changeslist constructor
42 * @param Skin|IContextSource $obj
44 public function __construct( $obj ) {
45 if ( $obj instanceof IContextSource ) {
46 $this->setContext( $obj );
47 $this->skin = $obj->getSkin();
48 } else {
49 $this->setContext( $obj->getContext() );
50 $this->skin = $obj;
52 $this->preCacheMessages();
55 /**
56 * Fetch an appropriate changes list class for the specified context
57 * Some users might want to use an enhanced list format, for instance
59 * @param IContextSource $context
60 * @return ChangesList
62 public static function newFromContext( IContextSource $context ) {
63 $user = $context->getUser();
64 $sk = $context->getSkin();
65 $list = null;
66 if ( wfRunHooks( 'FetchChangesList', array( $user, &$sk, &$list ) ) ) {
67 $new = $context->getRequest()->getBool( 'enhanced', $user->getOption( 'usenewrc' ) );
69 return $new ? new EnhancedChangesList( $context ) : new OldChangesList( $context );
70 } else {
71 return $list;
75 /**
76 * Sets the list to use a "<li class='watchlist-(namespace)-(page)'>" tag
77 * @param bool $value
79 public function setWatchlistDivs( $value = true ) {
80 $this->watchlist = $value;
83 /**
84 * @return bool True when setWatchlistDivs has been called
85 * @since 1.23
87 public function isWatchlist() {
88 return (bool)$this->watchlist;
91 /**
92 * As we use the same small set of messages in various methods and that
93 * they are called often, we call them once and save them in $this->message
95 private function preCacheMessages() {
96 if ( !isset( $this->message ) ) {
97 foreach ( array(
98 'cur', 'diff', 'hist', 'enhancedrc-history', 'last', 'blocklink', 'history',
99 'semicolon-separator', 'pipe-separator' ) as $msg
101 $this->message[$msg] = $this->msg( $msg )->escaped();
107 * Returns the appropriate flags for new page, minor change and patrolling
108 * @param array $flags Associative array of 'flag' => Bool
109 * @param string $nothing To use for empty space
110 * @return string
112 public function recentChangesFlags( $flags, $nothing = '&#160;' ) {
113 global $wgRecentChangesFlags;
114 $f = '';
115 foreach ( array_keys( $wgRecentChangesFlags ) as $flag ) {
116 $f .= isset( $flags[$flag] ) && $flags[$flag]
117 ? self::flag( $flag )
118 : $nothing;
121 return $f;
125 * Provide the "<abbr>" element appropriate to a given abbreviated flag,
126 * namely the flag indicating a new page, a minor edit, a bot edit, or an
127 * unpatrolled edit. By default in English it will contain "N", "m", "b",
128 * "!" respectively, plus it will have an appropriate title and class.
130 * @param string $flag One key of $wgRecentChangesFlags
131 * @return string Raw HTML
133 public static function flag( $flag ) {
134 static $flagInfos = null;
135 if ( is_null( $flagInfos ) ) {
136 global $wgRecentChangesFlags;
137 $flagInfos = array();
138 foreach ( $wgRecentChangesFlags as $key => $value ) {
139 $flagInfos[$key]['letter'] = wfMessage( $value['letter'] )->escaped();
140 $flagInfos[$key]['title'] = wfMessage( $value['title'] )->escaped();
141 // Allow customized class name, fall back to flag name
142 $flagInfos[$key]['class'] = Sanitizer::escapeClass(
143 isset( $value['class'] ) ? $value['class'] : $key );
147 // Inconsistent naming, bleh, kepted for b/c
148 $map = array(
149 'minoredit' => 'minor',
150 'botedit' => 'bot',
152 if ( isset( $map[$flag] ) ) {
153 $flag = $map[$flag];
156 return "<abbr class='" . $flagInfos[$flag]['class'] . "' title='" .
157 $flagInfos[$flag]['title'] . "'>" . $flagInfos[$flag]['letter'] .
158 '</abbr>';
162 * Returns text for the start of the tabular part of RC
163 * @return string
165 public function beginRecentChangesList() {
166 $this->rc_cache = array();
167 $this->rcMoveIndex = 0;
168 $this->rcCacheIndex = 0;
169 $this->lastdate = '';
170 $this->rclistOpen = false;
171 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
173 return '<div class="mw-changeslist">';
177 * @param ResultWrapper|array $rows
179 public function initChangesListRows( $rows ) {
180 wfRunHooks( 'ChangesListInitRows', array( $this, $rows ) );
184 * Show formatted char difference
185 * @param int $old Number of bytes
186 * @param int $new Number of bytes
187 * @param IContextSource $context
188 * @return string
190 public static function showCharacterDifference( $old, $new, IContextSource $context = null ) {
191 global $wgRCChangedSizeThreshold, $wgMiserMode;
193 if ( !$context ) {
194 $context = RequestContext::getMain();
197 $new = (int)$new;
198 $old = (int)$old;
199 $szdiff = $new - $old;
201 $lang = $context->getLanguage();
202 $code = $lang->getCode();
203 static $fastCharDiff = array();
204 if ( !isset( $fastCharDiff[$code] ) ) {
205 $fastCharDiff[$code] = $wgMiserMode || $context->msg( 'rc-change-size' )->plain() === '$1';
208 $formattedSize = $lang->formatNum( $szdiff );
210 if ( !$fastCharDiff[$code] ) {
211 $formattedSize = $context->msg( 'rc-change-size', $formattedSize )->text();
214 if ( abs( $szdiff ) > abs( $wgRCChangedSizeThreshold ) ) {
215 $tag = 'strong';
216 } else {
217 $tag = 'span';
220 if ( $szdiff === 0 ) {
221 $formattedSizeClass = 'mw-plusminus-null';
222 } elseif ( $szdiff > 0 ) {
223 $formattedSize = '+' . $formattedSize;
224 $formattedSizeClass = 'mw-plusminus-pos';
225 } else {
226 $formattedSizeClass = 'mw-plusminus-neg';
229 $formattedTotalSize = $context->msg( 'rc-change-size-new' )->numParams( $new )->text();
231 return Html::element( $tag,
232 array( 'dir' => 'ltr', 'class' => $formattedSizeClass, 'title' => $formattedTotalSize ),
233 $context->msg( 'parentheses', $formattedSize )->plain() ) . $lang->getDirMark();
237 * Format the character difference of one or several changes.
239 * @param RecentChange $old
240 * @param RecentChange $new Last change to use, if not provided, $old will be used
241 * @return string HTML fragment
243 public function formatCharacterDifference( RecentChange $old, RecentChange $new = null ) {
244 $oldlen = $old->mAttribs['rc_old_len'];
246 if ( $new ) {
247 $newlen = $new->mAttribs['rc_new_len'];
248 } else {
249 $newlen = $old->mAttribs['rc_new_len'];
252 if ( $oldlen === null || $newlen === null ) {
253 return '';
256 return self::showCharacterDifference( $oldlen, $newlen, $this->getContext() );
260 * Returns text for the end of RC
261 * @return string
263 public function endRecentChangesList() {
264 $out = $this->rclistOpen ? "</ul>\n" : '';
265 $out .= '</div>';
267 return $out;
271 * @param string $s HTML to update
272 * @param mixed $rc_timestamp
274 public function insertDateHeader( &$s, $rc_timestamp ) {
275 # Make date header if necessary
276 $date = $this->getLanguage()->userDate( $rc_timestamp, $this->getUser() );
277 if ( $date != $this->lastdate ) {
278 if ( $this->lastdate != '' ) {
279 $s .= "</ul>\n";
281 $s .= Xml::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
282 $this->lastdate = $date;
283 $this->rclistOpen = true;
288 * @param string $s HTML to update
289 * @param Title $title
290 * @param string $logtype
292 public function insertLog( &$s, $title, $logtype ) {
293 $page = new LogPage( $logtype );
294 $logname = $page->getName()->escaped();
295 $s .= $this->msg( 'parentheses' )->rawParams( Linker::linkKnown( $title, $logname ) )->escaped();
299 * @param string $s HTML to update
300 * @param RecentChange $rc
301 * @param bool $unpatrolled
303 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
304 # Diff link
305 if ( $rc->mAttribs['rc_type'] == RC_NEW || $rc->mAttribs['rc_type'] == RC_LOG ) {
306 $diffLink = $this->message['diff'];
307 } elseif ( !self::userCan( $rc, Revision::DELETED_TEXT, $this->getUser() ) ) {
308 $diffLink = $this->message['diff'];
309 } else {
310 $query = array(
311 'curid' => $rc->mAttribs['rc_cur_id'],
312 'diff' => $rc->mAttribs['rc_this_oldid'],
313 'oldid' => $rc->mAttribs['rc_last_oldid']
316 $diffLink = Linker::linkKnown(
317 $rc->getTitle(),
318 $this->message['diff'],
319 array( 'tabindex' => $rc->counter ),
320 $query
323 $diffhist = $diffLink . $this->message['pipe-separator'];
324 # History link
325 $diffhist .= Linker::linkKnown(
326 $rc->getTitle(),
327 $this->message['hist'],
328 array(),
329 array(
330 'curid' => $rc->mAttribs['rc_cur_id'],
331 'action' => 'history'
334 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
335 $s .= $this->msg( 'parentheses' )->rawParams( $diffhist )->escaped() .
336 ' <span class="mw-changeslist-separator">. .</span> ';
340 * @param string $s HTML to update
341 * @param RecentChange $rc
342 * @param bool $unpatrolled
343 * @param bool $watched
345 public function insertArticleLink( &$s, &$rc, $unpatrolled, $watched ) {
346 $params = array();
347 if ( $rc->getTitle()->isRedirect() ) {
348 $params = array( 'redirect' => 'no' );
351 $articlelink = Linker::linkKnown(
352 $rc->getTitle(),
353 null,
354 array( 'class' => 'mw-changeslist-title' ),
355 $params
357 if ( $this->isDeleted( $rc, Revision::DELETED_TEXT ) ) {
358 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
360 # To allow for boldening pages watched by this user
361 $articlelink = "<span class=\"mw-title\">{$articlelink}</span>";
362 # RTL/LTR marker
363 $articlelink .= $this->getLanguage()->getDirMark();
365 wfRunHooks( 'ChangesListInsertArticleLink',
366 array( &$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched ) );
368 $s .= " $articlelink";
372 * Get the timestamp from $rc formatted with current user's settings
373 * and a separator
375 * @param RecentChange $rc
376 * @return string HTML fragment
378 public function getTimestamp( $rc ) {
379 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
380 return $this->message['semicolon-separator'] . '<span class="mw-changeslist-date">' .
381 $this->getLanguage()->userTime(
382 $rc->mAttribs['rc_timestamp'],
383 $this->getUser()
384 ) . '</span> <span class="mw-changeslist-separator">. .</span> ';
388 * Insert time timestamp string from $rc into $s
390 * @param string $s HTML to update
391 * @param RecentChange $rc
393 public function insertTimestamp( &$s, $rc ) {
394 $s .= $this->getTimestamp( $rc );
398 * Insert links to user page, user talk page and eventually a blocking link
400 * @param string &$s HTML to update
401 * @param RecentChange &$rc
403 public function insertUserRelatedLinks( &$s, &$rc ) {
404 if ( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
405 $s .= ' <span class="history-deleted">' .
406 $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
407 } else {
408 $s .= $this->getLanguage()->getDirMark() . Linker::userLink( $rc->mAttribs['rc_user'],
409 $rc->mAttribs['rc_user_text'] );
410 $s .= Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
415 * Insert a formatted action
417 * @param RecentChange $rc
418 * @return string
420 public function insertLogEntry( $rc ) {
421 $formatter = LogFormatter::newFromRow( $rc->mAttribs );
422 $formatter->setContext( $this->getContext() );
423 $formatter->setShowUserToolLinks( true );
424 $mark = $this->getLanguage()->getDirMark();
426 return $formatter->getActionText() . " $mark" . $formatter->getComment();
430 * Insert a formatted comment
431 * @param RecentChange $rc
432 * @return string
434 public function insertComment( $rc ) {
435 if ( $rc->mAttribs['rc_type'] != RC_MOVE && $rc->mAttribs['rc_type'] != RC_MOVE_OVER_REDIRECT ) {
436 if ( $this->isDeleted( $rc, Revision::DELETED_COMMENT ) ) {
437 return ' <span class="history-deleted">' .
438 $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
439 } else {
440 return Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
444 return '';
448 * Check whether to enable recent changes patrol features
450 * @deprecated since 1.22
451 * @return bool
453 public static function usePatrol() {
454 global $wgUser;
456 wfDeprecated( __METHOD__, '1.22' );
458 return $wgUser->useRCPatrol();
462 * Returns the string which indicates the number of watching users
463 * @param int $count Number of user watching a page
464 * @return string
466 protected function numberofWatchingusers( $count ) {
467 static $cache = array();
468 if ( $count > 0 ) {
469 if ( !isset( $cache[$count] ) ) {
470 $cache[$count] = $this->msg( 'number_of_watching_users_RCview' )
471 ->numParams( $count )->escaped();
474 return $cache[$count];
475 } else {
476 return '';
481 * Determine if said field of a revision is hidden
482 * @param RCCacheEntry|RecentChange $rc
483 * @param int $field One of DELETED_* bitfield constants
484 * @return bool
486 public static function isDeleted( $rc, $field ) {
487 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
491 * Determine if the current user is allowed to view a particular
492 * field of this revision, if it's marked as deleted.
493 * @param RCCacheEntry|RecentChange $rc
494 * @param int $field
495 * @param User $user User object to check, or null to use $wgUser
496 * @return bool
498 public static function userCan( $rc, $field, User $user = null ) {
499 if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
500 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
501 } else {
502 return Revision::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
507 * @param string $link
508 * @param bool $watched
509 * @return string
511 protected function maybeWatchedLink( $link, $watched = false ) {
512 if ( $watched ) {
513 return '<strong class="mw-watched">' . $link . '</strong>';
514 } else {
515 return '<span class="mw-rc-unwatched">' . $link . '</span>';
519 /** Inserts a rollback link
521 * @param string $s
522 * @param RecentChange $rc
524 public function insertRollback( &$s, &$rc ) {
525 if ( $rc->mAttribs['rc_type'] == RC_EDIT
526 && $rc->mAttribs['rc_this_oldid']
527 && $rc->mAttribs['rc_cur_id']
529 $page = $rc->getTitle();
530 /** Check for rollback and edit permissions, disallow special pages, and only
531 * show a link on the top-most revision */
532 if ( $this->getUser()->isAllowed( 'rollback' )
533 && $rc->mAttribs['page_latest'] == $rc->mAttribs['rc_this_oldid']
535 $rev = new Revision( array(
536 'title' => $page,
537 'id' => $rc->mAttribs['rc_this_oldid'],
538 'user' => $rc->mAttribs['rc_user'],
539 'user_text' => $rc->mAttribs['rc_user_text'],
540 'deleted' => $rc->mAttribs['rc_deleted']
541 ) );
542 $s .= ' ' . Linker::generateRollback( $rev, $this->getContext() );
548 * @param string $s
549 * @param RecentChange $rc
550 * @param array $classes
552 public function insertTags( &$s, &$rc, &$classes ) {
553 if ( empty( $rc->mAttribs['ts_tags'] ) ) {
554 return;
557 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
558 $rc->mAttribs['ts_tags'],
559 'changeslist'
561 $classes = array_merge( $classes, $newClasses );
562 $s .= ' ' . $tagSummary;
565 public function insertExtra( &$s, &$rc, &$classes ) {
566 // Empty, used for subclasses to add anything special.
569 protected function showAsUnpatrolled( RecentChange $rc ) {
570 return self::isUnpatrolled( $rc, $this->getUser() );
574 * @param object|RecentChange $rc Database row from recentchanges or a RecentChange object
575 * @param User $user
576 * @return bool
578 public static function isUnpatrolled( $rc, User $user ) {
579 if ( $rc instanceof RecentChange ) {
580 $isPatrolled = $rc->mAttribs['rc_patrolled'];
581 $rcType = $rc->mAttribs['rc_type'];
582 } else {
583 $isPatrolled = $rc->rc_patrolled;
584 $rcType = $rc->rc_type;
587 if ( !$isPatrolled ) {
588 if ( $user->useRCPatrol() ) {
589 return true;
591 if ( $user->useNPPatrol() && $rcType == RC_NEW ) {
592 return true;
596 return false;