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