Merge "mediawiki.page.ready: Avoid duplicate DOM query on logout click"
[lhc/web/wiklou.git] / includes / changes / ChangesList.php
1 <?php
2 /**
3 * Base class for all changes lists.
4 *
5 * The class is used for formatting recent changes, related changes and watchlist.
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 */
24 use MediaWiki\Linker\LinkRenderer;
25 use MediaWiki\MediaWikiServices;
26 use MediaWiki\Revision\RevisionRecord;
27 use Wikimedia\Rdbms\IResultWrapper;
28
29 class ChangesList extends ContextSource {
30 const CSS_CLASS_PREFIX = 'mw-changeslist-';
31
32 /**
33 * @var Skin
34 */
35 public $skin;
36
37 protected $watchlist = false;
38 protected $lastdate;
39 protected $message;
40 protected $rc_cache;
41 protected $rcCacheIndex;
42 protected $rclistOpen;
43 protected $rcMoveIndex;
44
45 /** @var callable */
46 protected $changeLinePrefixer;
47
48 /** @var MapCacheLRU */
49 protected $watchMsgCache;
50
51 /**
52 * @var LinkRenderer
53 */
54 protected $linkRenderer;
55
56 /**
57 * @var array
58 */
59 protected $filterGroups;
60
61 /**
62 * @param Skin|IContextSource $obj
63 * @param array $filterGroups Array of ChangesListFilterGroup objects (currently optional)
64 */
65 public function __construct( $obj, array $filterGroups = [] ) {
66 if ( $obj instanceof IContextSource ) {
67 $this->setContext( $obj );
68 $this->skin = $obj->getSkin();
69 } else {
70 $this->setContext( $obj->getContext() );
71 $this->skin = $obj;
72 }
73 $this->preCacheMessages();
74 $this->watchMsgCache = new MapCacheLRU( 50 );
75 $this->linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
76 $this->filterGroups = $filterGroups;
77 }
78
79 /**
80 * Fetch an appropriate changes list class for the specified context
81 * Some users might want to use an enhanced list format, for instance
82 *
83 * @param IContextSource $context
84 * @param array $groups Array of ChangesListFilterGroup objects (currently optional)
85 * @return ChangesList
86 */
87 public static function newFromContext( IContextSource $context, array $groups = [] ) {
88 $user = $context->getUser();
89 $sk = $context->getSkin();
90 $list = null;
91 if ( Hooks::run( 'FetchChangesList', [ $user, &$sk, &$list, $groups ] ) ) {
92 $new = $context->getRequest()->getBool( 'enhanced', $user->getOption( 'usenewrc' ) );
93
94 return $new ?
95 new EnhancedChangesList( $context, $groups ) :
96 new OldChangesList( $context, $groups );
97 } else {
98 return $list;
99 }
100 }
101
102 /**
103 * Format a line
104 *
105 * @since 1.27
106 *
107 * @param RecentChange &$rc Passed by reference
108 * @param bool $watched (default false)
109 * @param int|null $linenumber (default null)
110 *
111 * @return string|bool
112 */
113 public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
114 throw new RuntimeException( 'recentChangesLine should be implemented' );
115 }
116
117 /**
118 * Get the container for highlights that are used in the new StructuredFilters
119 * system
120 *
121 * @return string HTML structure of the highlight container div
122 */
123 protected function getHighlightsContainerDiv() {
124 $highlightColorDivs = '';
125 foreach ( [ 'none', 'c1', 'c2', 'c3', 'c4', 'c5' ] as $color ) {
126 $highlightColorDivs .= Html::rawElement(
127 'div',
128 [
129 'class' => 'mw-rcfilters-ui-highlights-color-' . $color,
130 'data-color' => $color
131 ]
132 );
133 }
134
135 return Html::rawElement(
136 'div',
137 [ 'class' => 'mw-rcfilters-ui-highlights' ],
138 $highlightColorDivs
139 );
140 }
141
142 /**
143 * Sets the list to use a "<li class='watchlist-(namespace)-(page)'>" tag
144 * @param bool $value
145 */
146 public function setWatchlistDivs( $value = true ) {
147 $this->watchlist = $value;
148 }
149
150 /**
151 * @return bool True when setWatchlistDivs has been called
152 * @since 1.23
153 */
154 public function isWatchlist() {
155 return (bool)$this->watchlist;
156 }
157
158 /**
159 * As we use the same small set of messages in various methods and that
160 * they are called often, we call them once and save them in $this->message
161 */
162 private function preCacheMessages() {
163 if ( !isset( $this->message ) ) {
164 $this->message = [];
165 foreach ( [
166 'cur', 'diff', 'hist', 'enhancedrc-history', 'last', 'blocklink', 'history',
167 'semicolon-separator', 'pipe-separator' ] as $msg
168 ) {
169 $this->message[$msg] = $this->msg( $msg )->escaped();
170 }
171 }
172 }
173
174 /**
175 * Returns the appropriate flags for new page, minor change and patrolling
176 * @param array $flags Associative array of 'flag' => Bool
177 * @param string $nothing To use for empty space
178 * @return string
179 */
180 public function recentChangesFlags( $flags, $nothing = "\u{00A0}" ) {
181 $f = '';
182 foreach ( array_keys( $this->getConfig()->get( 'RecentChangesFlags' ) ) as $flag ) {
183 $f .= isset( $flags[$flag] ) && $flags[$flag]
184 ? self::flag( $flag, $this->getContext() )
185 : $nothing;
186 }
187
188 return $f;
189 }
190
191 /**
192 * Get an array of default HTML class attributes for the change.
193 *
194 * @param RecentChange|RCCacheEntry $rc
195 * @param string|bool $watched Optionally timestamp for adding watched class
196 *
197 * @return string[] List of CSS class names
198 */
199 protected function getHTMLClasses( $rc, $watched ) {
200 $classes = [ self::CSS_CLASS_PREFIX . 'line' ];
201 $logType = $rc->mAttribs['rc_log_type'];
202
203 if ( $logType ) {
204 $classes[] = self::CSS_CLASS_PREFIX . 'log';
205 $classes[] = Sanitizer::escapeClass( self::CSS_CLASS_PREFIX . 'log-' . $logType );
206 } else {
207 $classes[] = self::CSS_CLASS_PREFIX . 'edit';
208 $classes[] = Sanitizer::escapeClass( self::CSS_CLASS_PREFIX . 'ns' .
209 $rc->mAttribs['rc_namespace'] . '-' . $rc->mAttribs['rc_title'] );
210 }
211
212 // Indicate watched status on the line to allow for more
213 // comprehensive styling.
214 $classes[] = $watched && $rc->mAttribs['rc_timestamp'] >= $watched
215 ? self::CSS_CLASS_PREFIX . 'line-watched'
216 : self::CSS_CLASS_PREFIX . 'line-not-watched';
217
218 $classes = array_merge( $classes, $this->getHTMLClassesForFilters( $rc ) );
219
220 return $classes;
221 }
222
223 /**
224 * Get an array of CSS classes attributed to filters for this row. Used for highlighting
225 * in the front-end.
226 *
227 * @param RecentChange $rc
228 * @return array Array of CSS classes
229 */
230 protected function getHTMLClassesForFilters( $rc ) {
231 $classes = [];
232
233 $classes[] = Sanitizer::escapeClass( self::CSS_CLASS_PREFIX . 'ns-' .
234 $rc->mAttribs['rc_namespace'] );
235
236 $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
237 $classes[] = Sanitizer::escapeClass(
238 self::CSS_CLASS_PREFIX .
239 'ns-' .
240 ( $nsInfo->isTalk( $rc->mAttribs['rc_namespace'] ) ? 'talk' : 'subject' )
241 );
242
243 if ( $this->filterGroups !== null ) {
244 foreach ( $this->filterGroups as $filterGroup ) {
245 foreach ( $filterGroup->getFilters() as $filter ) {
246 $filter->applyCssClassIfNeeded( $this, $rc, $classes );
247 }
248 }
249 }
250
251 return $classes;
252 }
253
254 /**
255 * Make an "<abbr>" element for a given change flag. The flag indicating a new page, minor edit,
256 * bot edit, or unpatrolled edit. In English it typically contains "N", "m", "b", or "!".
257 *
258 * @param string $flag One key of $wgRecentChangesFlags
259 * @param IContextSource|null $context
260 * @return string HTML
261 */
262 public static function flag( $flag, IContextSource $context = null ) {
263 static $map = [ 'minoredit' => 'minor', 'botedit' => 'bot' ];
264 static $flagInfos = null;
265
266 if ( is_null( $flagInfos ) ) {
267 global $wgRecentChangesFlags;
268 $flagInfos = [];
269 foreach ( $wgRecentChangesFlags as $key => $value ) {
270 $flagInfos[$key]['letter'] = $value['letter'];
271 $flagInfos[$key]['title'] = $value['title'];
272 // Allow customized class name, fall back to flag name
273 $flagInfos[$key]['class'] = $value['class'] ?? $key;
274 }
275 }
276
277 $context = $context ?: RequestContext::getMain();
278
279 // Inconsistent naming, kepted for b/c
280 if ( isset( $map[$flag] ) ) {
281 $flag = $map[$flag];
282 }
283
284 $info = $flagInfos[$flag];
285 return Html::element( 'abbr', [
286 'class' => $info['class'],
287 'title' => wfMessage( $info['title'] )->setContext( $context )->text(),
288 ], wfMessage( $info['letter'] )->setContext( $context )->text() );
289 }
290
291 /**
292 * Returns text for the start of the tabular part of RC
293 * @return string
294 */
295 public function beginRecentChangesList() {
296 $this->rc_cache = [];
297 $this->rcMoveIndex = 0;
298 $this->rcCacheIndex = 0;
299 $this->lastdate = '';
300 $this->rclistOpen = false;
301 $this->getOutput()->addModuleStyles( [
302 'mediawiki.interface.helpers.styles',
303 'mediawiki.special.changeslist'
304 ] );
305
306 return '<div class="mw-changeslist">';
307 }
308
309 /**
310 * @param IResultWrapper|array $rows
311 */
312 public function initChangesListRows( $rows ) {
313 Hooks::run( 'ChangesListInitRows', [ $this, $rows ] );
314 }
315
316 /**
317 * Show formatted char difference
318 *
319 * Needs the css module 'mediawiki.special.changeslist' to style output
320 *
321 * @param int $old Number of bytes
322 * @param int $new Number of bytes
323 * @param IContextSource|null $context
324 * @return string
325 */
326 public static function showCharacterDifference( $old, $new, IContextSource $context = null ) {
327 if ( !$context ) {
328 $context = RequestContext::getMain();
329 }
330
331 $new = (int)$new;
332 $old = (int)$old;
333 $szdiff = $new - $old;
334
335 $lang = $context->getLanguage();
336 $config = $context->getConfig();
337 $code = $lang->getCode();
338 static $fastCharDiff = [];
339 if ( !isset( $fastCharDiff[$code] ) ) {
340 $fastCharDiff[$code] = $config->get( 'MiserMode' )
341 || $context->msg( 'rc-change-size' )->plain() === '$1';
342 }
343
344 $formattedSize = $lang->formatNum( $szdiff );
345
346 if ( !$fastCharDiff[$code] ) {
347 $formattedSize = $context->msg( 'rc-change-size', $formattedSize )->text();
348 }
349
350 if ( abs( $szdiff ) > abs( $config->get( 'RCChangedSizeThreshold' ) ) ) {
351 $tag = 'strong';
352 } else {
353 $tag = 'span';
354 }
355
356 if ( $szdiff === 0 ) {
357 $formattedSizeClass = 'mw-plusminus-null';
358 } elseif ( $szdiff > 0 ) {
359 $formattedSize = '+' . $formattedSize;
360 $formattedSizeClass = 'mw-plusminus-pos';
361 } else {
362 $formattedSizeClass = 'mw-plusminus-neg';
363 }
364 $formattedSizeClass .= ' mw-diff-bytes';
365
366 $formattedTotalSize = $context->msg( 'rc-change-size-new' )->numParams( $new )->text();
367
368 return Html::element( $tag,
369 [ 'dir' => 'ltr', 'class' => $formattedSizeClass, 'title' => $formattedTotalSize ],
370 $formattedSize ) . $lang->getDirMark();
371 }
372
373 /**
374 * Format the character difference of one or several changes.
375 *
376 * @param RecentChange $old
377 * @param RecentChange|null $new Last change to use, if not provided, $old will be used
378 * @return string HTML fragment
379 */
380 public function formatCharacterDifference( RecentChange $old, RecentChange $new = null ) {
381 $oldlen = $old->mAttribs['rc_old_len'];
382
383 if ( $new ) {
384 $newlen = $new->mAttribs['rc_new_len'];
385 } else {
386 $newlen = $old->mAttribs['rc_new_len'];
387 }
388
389 if ( $oldlen === null || $newlen === null ) {
390 return '';
391 }
392
393 return self::showCharacterDifference( $oldlen, $newlen, $this->getContext() );
394 }
395
396 /**
397 * Returns text for the end of RC
398 * @return string
399 */
400 public function endRecentChangesList() {
401 $out = $this->rclistOpen ? "</ul>\n" : '';
402 $out .= '</div>';
403
404 return $out;
405 }
406
407 /**
408 * Render the date and time of a revision in the current user language
409 * based on whether the user is able to view this information or not.
410 * @param Revision $rev
411 * @param User $user
412 * @param Language $lang
413 * @param Title|null $title (optional) where Title does not match
414 * the Title associated with the Revision
415 * @internal For usage by Pager classes only (e.g. HistoryPager and ContribsPager).
416 * @return string HTML
417 */
418 public static function revDateLink( Revision $rev, User $user, Language $lang, $title = null ) {
419 $ts = $rev->getTimestamp();
420 $date = $lang->userTimeAndDate( $ts, $user );
421 if ( $rev->userCan( RevisionRecord::DELETED_TEXT, $user ) ) {
422 $link = MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
423 $title ?? $rev->getTitle(),
424 $date,
425 [ 'class' => 'mw-changeslist-date' ],
426 [ 'oldid' => $rev->getId() ]
427 );
428 } else {
429 $link = htmlspecialchars( $date );
430 }
431 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
432 $link = "<span class=\"history-deleted mw-changeslist-date\">$link</span>";
433 }
434 return $link;
435 }
436
437 /**
438 * @param string &$s HTML to update
439 * @param mixed $rc_timestamp
440 */
441 public function insertDateHeader( &$s, $rc_timestamp ) {
442 # Make date header if necessary
443 $date = $this->getLanguage()->userDate( $rc_timestamp, $this->getUser() );
444 if ( $date != $this->lastdate ) {
445 if ( $this->lastdate != '' ) {
446 $s .= "</ul>\n";
447 }
448 $s .= Xml::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
449 $this->lastdate = $date;
450 $this->rclistOpen = true;
451 }
452 }
453
454 /**
455 * @param string &$s HTML to update
456 * @param Title $title
457 * @param string $logtype
458 */
459 public function insertLog( &$s, $title, $logtype ) {
460 $page = new LogPage( $logtype );
461 $logname = $page->getName()->setContext( $this->getContext() )->text();
462 $s .= Html::rawElement( 'span', [
463 'class' => 'mw-changeslist-links'
464 ], $this->linkRenderer->makeKnownLink( $title, $logname ) );
465 }
466
467 /**
468 * @param string &$s HTML to update
469 * @param RecentChange &$rc
470 * @param bool|null $unpatrolled Unused variable, since 1.27.
471 */
472 public function insertDiffHist( &$s, &$rc, $unpatrolled = null ) {
473 # Diff link
474 if (
475 $rc->mAttribs['rc_type'] == RC_NEW ||
476 $rc->mAttribs['rc_type'] == RC_LOG ||
477 $rc->mAttribs['rc_type'] == RC_CATEGORIZE
478 ) {
479 $diffLink = $this->message['diff'];
480 } elseif ( !self::userCan( $rc, RevisionRecord::DELETED_TEXT, $this->getUser() ) ) {
481 $diffLink = $this->message['diff'];
482 } else {
483 $query = [
484 'curid' => $rc->mAttribs['rc_cur_id'],
485 'diff' => $rc->mAttribs['rc_this_oldid'],
486 'oldid' => $rc->mAttribs['rc_last_oldid']
487 ];
488
489 $diffLink = $this->linkRenderer->makeKnownLink(
490 $rc->getTitle(),
491 new HtmlArmor( $this->message['diff'] ),
492 [ 'class' => 'mw-changeslist-diff' ],
493 $query
494 );
495 }
496 if ( $rc->mAttribs['rc_type'] == RC_CATEGORIZE ) {
497 $histLink = $this->message['hist'];
498 } else {
499 $histLink = $this->linkRenderer->makeKnownLink(
500 $rc->getTitle(),
501 new HtmlArmor( $this->message['hist'] ),
502 [ 'class' => 'mw-changeslist-history' ],
503 [
504 'curid' => $rc->mAttribs['rc_cur_id'],
505 'action' => 'history'
506 ]
507 );
508 }
509
510 $s .= Html::rawElement( 'div', [ 'class' => 'mw-changeslist-links' ],
511 Html::rawElement( 'span', [], $diffLink ) .
512 Html::rawElement( 'span', [], $histLink )
513 ) .
514 ' <span class="mw-changeslist-separator"></span> ';
515 }
516
517 /**
518 * @param RecentChange &$rc
519 * @param bool $unpatrolled
520 * @param bool $watched
521 * @return string HTML
522 * @since 1.26
523 */
524 public function getArticleLink( &$rc, $unpatrolled, $watched ) {
525 $params = [];
526 if ( $rc->getTitle()->isRedirect() ) {
527 $params = [ 'redirect' => 'no' ];
528 }
529
530 $articlelink = $this->linkRenderer->makeLink(
531 $rc->getTitle(),
532 null,
533 [ 'class' => 'mw-changeslist-title' ],
534 $params
535 );
536 if ( $this->isDeleted( $rc, RevisionRecord::DELETED_TEXT ) ) {
537 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
538 }
539 # To allow for boldening pages watched by this user
540 $articlelink = "<span class=\"mw-title\">{$articlelink}</span>";
541 # RTL/LTR marker
542 $articlelink .= $this->getLanguage()->getDirMark();
543
544 # TODO: Deprecate the $s argument, it seems happily unused.
545 $s = '';
546 # Avoid PHP 7.1 warning from passing $this by reference
547 $changesList = $this;
548 Hooks::run( 'ChangesListInsertArticleLink',
549 [ &$changesList, &$articlelink, &$s, &$rc, $unpatrolled, $watched ] );
550
551 return "{$s} {$articlelink}";
552 }
553
554 /**
555 * Get the timestamp from $rc formatted with current user's settings
556 * and a separator
557 *
558 * @param RecentChange $rc
559 * @deprecated use revDateLink instead.
560 * @return string HTML fragment
561 */
562 public function getTimestamp( $rc ) {
563 // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
564 return $this->message['semicolon-separator'] . '<span class="mw-changeslist-date">' .
565 htmlspecialchars( $this->getLanguage()->userTime(
566 $rc->mAttribs['rc_timestamp'],
567 $this->getUser()
568 ) ) . '</span> <span class="mw-changeslist-separator"></span> ';
569 }
570
571 /**
572 * Insert time timestamp string from $rc into $s
573 *
574 * @param string &$s HTML to update
575 * @param RecentChange $rc
576 */
577 public function insertTimestamp( &$s, $rc ) {
578 $s .= $this->getTimestamp( $rc );
579 }
580
581 /**
582 * Insert links to user page, user talk page and eventually a blocking link
583 *
584 * @param string &$s HTML to update
585 * @param RecentChange &$rc
586 */
587 public function insertUserRelatedLinks( &$s, &$rc ) {
588 if ( $this->isDeleted( $rc, RevisionRecord::DELETED_USER ) ) {
589 $s .= ' <span class="history-deleted">' .
590 $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
591 } else {
592 $s .= $this->getLanguage()->getDirMark() . Linker::userLink( $rc->mAttribs['rc_user'],
593 $rc->mAttribs['rc_user_text'] );
594 $s .= Linker::userToolLinks(
595 $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'],
596 false, 0, null,
597 // The text content of tools is not wrapped with parenthesises or "piped".
598 // This will be handled in CSS (T205581).
599 false
600 );
601 }
602 }
603
604 /**
605 * Insert a formatted action
606 *
607 * @param RecentChange $rc
608 * @return string
609 */
610 public function insertLogEntry( $rc ) {
611 $formatter = LogFormatter::newFromRow( $rc->mAttribs );
612 $formatter->setContext( $this->getContext() );
613 $formatter->setShowUserToolLinks( true );
614 $mark = $this->getLanguage()->getDirMark();
615
616 return Html::openElement( 'span', [ 'class' => 'mw-changeslist-log-entry' ] )
617 . $formatter->getActionText() . " $mark" . $formatter->getComment()
618 . Html::closeElement( 'span' );
619 }
620
621 /**
622 * Insert a formatted comment
623 * @param RecentChange $rc
624 * @return string
625 */
626 public function insertComment( $rc ) {
627 if ( $this->isDeleted( $rc, RevisionRecord::DELETED_COMMENT ) ) {
628 return ' <span class="history-deleted">' .
629 $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
630 } else {
631 return Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle(),
632 // Whether section links should refer to local page (using default false)
633 false,
634 // wikid to generate links for (using default null) */
635 null,
636 // whether parentheses should be rendered as part of the message
637 false );
638 }
639 }
640
641 /**
642 * Returns the string which indicates the number of watching users
643 * @param int $count Number of user watching a page
644 * @return string
645 */
646 protected function numberofWatchingusers( $count ) {
647 if ( $count <= 0 ) {
648 return '';
649 }
650
651 return $this->watchMsgCache->getWithSetCallback(
652 "watching-users-msg:$count",
653 function () use ( $count ) {
654 return $this->msg( 'number-of-watching-users-for-recent-changes' )
655 ->numParams( $count )->escaped();
656 }
657 );
658 }
659
660 /**
661 * Determine if said field of a revision is hidden
662 * @param RCCacheEntry|RecentChange $rc
663 * @param int $field One of DELETED_* bitfield constants
664 * @return bool
665 */
666 public static function isDeleted( $rc, $field ) {
667 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
668 }
669
670 /**
671 * Determine if the current user is allowed to view a particular
672 * field of this revision, if it's marked as deleted.
673 * @param RCCacheEntry|RecentChange $rc
674 * @param int $field
675 * @param User|null $user User object to check against. If null, the global RequestContext's
676 * User is assumed instead.
677 * @return bool
678 */
679 public static function userCan( $rc, $field, User $user = null ) {
680 if ( $user === null ) {
681 $user = RequestContext::getMain()->getUser();
682 }
683
684 if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
685 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
686 }
687
688 return RevisionRecord::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
689 }
690
691 /**
692 * @param string $link
693 * @param bool $watched
694 * @return string
695 */
696 protected function maybeWatchedLink( $link, $watched = false ) {
697 if ( $watched ) {
698 return '<strong class="mw-watched">' . $link . '</strong>';
699 } else {
700 return '<span class="mw-rc-unwatched">' . $link . '</span>';
701 }
702 }
703
704 /**
705 * Insert a rollback link
706 *
707 * @param string &$s
708 * @param RecentChange &$rc
709 */
710 public function insertRollback( &$s, &$rc ) {
711 if ( $rc->mAttribs['rc_type'] == RC_EDIT
712 && $rc->mAttribs['rc_this_oldid']
713 && $rc->mAttribs['rc_cur_id']
714 && $rc->getAttribute( 'page_latest' ) == $rc->mAttribs['rc_this_oldid']
715 ) {
716 $title = $rc->getTitle();
717 /** Check for rollback permissions, disallow special pages, and only
718 * show a link on the top-most revision
719 */
720 if ( MediaWikiServices::getInstance()->getPermissionManager()
721 ->quickUserCan( 'rollback', $this->getUser(), $title )
722 ) {
723 $rev = new Revision( [
724 'title' => $title,
725 'id' => $rc->mAttribs['rc_this_oldid'],
726 'user' => $rc->mAttribs['rc_user'],
727 'user_text' => $rc->mAttribs['rc_user_text'],
728 'actor' => $rc->mAttribs['rc_actor'] ?? null,
729 'deleted' => $rc->mAttribs['rc_deleted']
730 ] );
731 $s .= ' ' . Linker::generateRollback( $rev, $this->getContext(),
732 [ 'noBrackets' ] );
733 }
734 }
735 }
736
737 /**
738 * @param RecentChange $rc
739 * @return string
740 * @since 1.26
741 */
742 public function getRollback( RecentChange $rc ) {
743 $s = '';
744 $this->insertRollback( $s, $rc );
745 return $s;
746 }
747
748 /**
749 * @param string &$s
750 * @param RecentChange &$rc
751 * @param array &$classes
752 */
753 public function insertTags( &$s, &$rc, &$classes ) {
754 if ( empty( $rc->mAttribs['ts_tags'] ) ) {
755 return;
756 }
757
758 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
759 $rc->mAttribs['ts_tags'],
760 'changeslist',
761 $this->getContext()
762 );
763 $classes = array_merge( $classes, $newClasses );
764 $s .= ' ' . $tagSummary;
765 }
766
767 /**
768 * @param RecentChange $rc
769 * @param array &$classes
770 * @return string
771 * @since 1.26
772 */
773 public function getTags( RecentChange $rc, array &$classes ) {
774 $s = '';
775 $this->insertTags( $s, $rc, $classes );
776 return $s;
777 }
778
779 public function insertExtra( &$s, &$rc, &$classes ) {
780 // Empty, used for subclasses to add anything special.
781 }
782
783 protected function showAsUnpatrolled( RecentChange $rc ) {
784 return self::isUnpatrolled( $rc, $this->getUser() );
785 }
786
787 /**
788 * @param object|RecentChange $rc Database row from recentchanges or a RecentChange object
789 * @param User $user
790 * @return bool
791 */
792 public static function isUnpatrolled( $rc, User $user ) {
793 if ( $rc instanceof RecentChange ) {
794 $isPatrolled = $rc->mAttribs['rc_patrolled'];
795 $rcType = $rc->mAttribs['rc_type'];
796 $rcLogType = $rc->mAttribs['rc_log_type'];
797 } else {
798 $isPatrolled = $rc->rc_patrolled;
799 $rcType = $rc->rc_type;
800 $rcLogType = $rc->rc_log_type;
801 }
802
803 if ( !$isPatrolled ) {
804 if ( $user->useRCPatrol() ) {
805 return true;
806 }
807 if ( $user->useNPPatrol() && $rcType == RC_NEW ) {
808 return true;
809 }
810 if ( $user->useFilePatrol() && $rcLogType == 'upload' ) {
811 return true;
812 }
813 }
814
815 return false;
816 }
817
818 /**
819 * Determines whether a revision is linked to this change; this may not be the case
820 * when the categorization wasn't done by an edit but a conditional parser function
821 *
822 * @since 1.27
823 *
824 * @param RecentChange|RCCacheEntry $rcObj
825 * @return bool
826 */
827 protected function isCategorizationWithoutRevision( $rcObj ) {
828 return intval( $rcObj->getAttribute( 'rc_type' ) ) === RC_CATEGORIZE
829 && intval( $rcObj->getAttribute( 'rc_this_oldid' ) ) === 0;
830 }
831
832 /**
833 * Get recommended data attributes for a change line.
834 * @param RecentChange $rc
835 * @return string[] attribute name => value
836 */
837 protected function getDataAttributes( RecentChange $rc ) {
838 $attrs = [];
839
840 $type = $rc->getAttribute( 'rc_source' );
841 switch ( $type ) {
842 case RecentChange::SRC_EDIT:
843 case RecentChange::SRC_NEW:
844 $attrs['data-mw-revid'] = $rc->mAttribs['rc_this_oldid'];
845 break;
846 case RecentChange::SRC_LOG:
847 $attrs['data-mw-logid'] = $rc->mAttribs['rc_logid'];
848 $attrs['data-mw-logaction'] =
849 $rc->mAttribs['rc_log_type'] . '/' . $rc->mAttribs['rc_log_action'];
850 break;
851 }
852
853 $attrs[ 'data-mw-ts' ] = $rc->getAttribute( 'rc_timestamp' );
854
855 return $attrs;
856 }
857
858 /**
859 * Sets the callable that generates a change line prefix added to the beginning of each line.
860 *
861 * @param callable $prefixer Callable to run that generates the change line prefix.
862 * Takes three parameters: a RecentChange object, a ChangesList object,
863 * and whether the current entry is a grouped entry.
864 */
865 public function setChangeLinePrefixer( callable $prefixer ) {
866 $this->changeLinePrefixer = $prefixer;
867 }
868 }