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