Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.34% covered (warning)
84.34%
377 / 447
41.67% covered (danger)
41.67%
5 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
EnhancedChangesList
84.53% covered (warning)
84.53%
377 / 446
41.67% covered (danger)
41.67%
5 / 12
147.61
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 beginRecentChangesList
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 recentChangesLine
78.57% covered (warning)
78.57%
11 / 14
0.00% covered (danger)
0.00%
0 / 1
3.09
 addCacheEntry
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 makeCacheGroupingKey
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 recentChangesBlockGroup
86.61% covered (warning)
86.61%
110 / 127
0.00% covered (danger)
0.00%
0 / 1
36.77
 getLineData
89.87% covered (warning)
89.87%
71 / 79
0.00% covered (danger)
0.00%
0 / 1
16.27
 getLogText
60.00% covered (warning)
60.00%
48 / 80
0.00% covered (danger)
0.00%
0 / 1
45.60
 recentChangesBlockLine
98.81% covered (success)
98.81%
83 / 84
0.00% covered (danger)
0.00%
0 / 1
16
 getDiffHistLinks
77.78% covered (warning)
77.78%
21 / 27
0.00% covered (danger)
0.00%
0 / 1
6.40
 recentChangesBlock
80.00% covered (warning)
80.00%
8 / 10
0.00% covered (danger)
0.00%
0 / 1
5.20
 endRecentChangesList
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\RecentChanges;
8
9use DomainException;
10use MediaWiki\Context\IContextSource;
11use MediaWiki\Html\Html;
12use MediaWiki\Html\TemplateParser;
13use MediaWiki\Logging\LogPage;
14use MediaWiki\MainConfigNames;
15use MediaWiki\Parser\Sanitizer;
16use MediaWiki\Revision\RevisionRecord;
17use MediaWiki\SpecialPage\SpecialPage;
18use MediaWiki\Title\Title;
19use Wikimedia\HtmlArmor\HtmlArmor;
20
21/**
22 * Generate a list of changes using an Enhanced system (uses javascript).
23 *
24 * @ingroup RecentChanges
25 */
26class EnhancedChangesList extends ChangesList {
27
28    /**
29     * @var RCCacheEntryFactory
30     */
31    protected $cacheEntryFactory;
32
33    /**
34     * @var RCCacheEntry[][]
35     */
36    protected $rc_cache;
37
38    /**
39     * @var TemplateParser
40     */
41    protected $templateParser;
42
43    /**
44     * @param IContextSource $context
45     * @param ChangesListFilterGroupContainer|null $filterGroups
46     */
47    public function __construct( $context, ?ChangesListFilterGroupContainer $filterGroups = null ) {
48        parent::__construct( $context, $filterGroups );
49
50        // message is set by the parent ChangesList class
51        $this->cacheEntryFactory = new RCCacheEntryFactory(
52            $context,
53            $this->message,
54            $this->linkRenderer,
55            $this->userLinkRenderer
56        );
57        $this->templateParser = new TemplateParser();
58    }
59
60    /**
61     * Add the JavaScript file for enhanced changeslist
62     * @return string
63     */
64    public function beginRecentChangesList() {
65        $this->getOutput()->addModuleStyles( [
66            'mediawiki.special.changeslist.enhanced',
67        ] );
68
69        parent::beginRecentChangesList();
70        return '<div class="mw-changeslist" aria-live="polite">';
71    }
72
73    /**
74     * Format a line for enhanced recentchange (aka with javascript and block of lines).
75     *
76     * @param RecentChange &$rc
77     * @param bool $watched
78     * @param int|null $linenumber (default null)
79     *
80     * @return string
81     */
82    public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
83        $date = $this->getLanguage()->userDate(
84            $rc->mAttribs['rc_timestamp'],
85            $this->getUser()
86        );
87        if ( $this->lastdate === '' ) {
88            $this->lastdate = $date;
89        }
90
91        $ret = '';
92
93        # If it's a new day, flush the cache and update $this->lastdate
94        if ( $date !== $this->lastdate ) {
95            # Process current cache (uses $this->lastdate to generate a heading)
96            $ret = $this->recentChangesBlock();
97            $this->rc_cache = [];
98            $this->lastdate = $date;
99        }
100
101        $cacheEntry = $this->cacheEntryFactory->newFromRecentChange( $rc, $watched );
102        $this->addCacheEntry( $cacheEntry );
103
104        return $ret;
105    }
106
107    /**
108     * Put accumulated information into the cache, for later display.
109     * Page moves go on their own line.
110     */
111    protected function addCacheEntry( RCCacheEntry $cacheEntry ) {
112        $cacheGroupingKey = $this->makeCacheGroupingKey( $cacheEntry );
113        $this->rc_cache[$cacheGroupingKey][] = $cacheEntry;
114    }
115
116    /**
117     * @param RCCacheEntry $cacheEntry
118     *
119     * @return string
120     */
121    protected function makeCacheGroupingKey( RCCacheEntry $cacheEntry ) {
122        $title = $cacheEntry->getTitle();
123        $cacheGroupingKey = $title->getPrefixedDBkey();
124
125        $source = $cacheEntry->mAttribs['rc_source'];
126
127        if ( $source == RecentChange::SRC_LOG ) {
128            // Group by log type
129            $cacheGroupingKey = SpecialPage::getTitleFor(
130                'Log',
131                $cacheEntry->mAttribs['rc_log_type']
132            )->getPrefixedDBkey();
133        }
134
135        return $cacheGroupingKey;
136    }
137
138    /**
139     * Enhanced RC group
140     * @param RCCacheEntry[] $block
141     * @return string
142     */
143    protected function recentChangesBlockGroup( $block ) {
144        $recentChangesFlags = $this->getConfig()->get( MainConfigNames::RecentChangesFlags );
145
146        # Add the namespace and title of the block as part of the class
147        $tableClasses = [ 'mw-enhanced-rc', 'mw-changeslist-line' ];
148        if ( $block[0]->mAttribs['rc_log_type'] ) {
149            # Log entry
150            $tableClasses[] = 'mw-changeslist-log';
151            $tableClasses[] = Sanitizer::escapeClass( 'mw-changeslist-log-'
152                . $block[0]->mAttribs['rc_log_type'] );
153        } else {
154            $tableClasses[] = 'mw-changeslist-edit';
155            $tableClasses[] = Sanitizer::escapeClass( 'mw-changeslist-ns'
156                . $block[0]->mAttribs['rc_namespace'] . '-' . $block[0]->mAttribs['rc_title'] );
157        }
158        if ( $block[0]->watched ) {
159            $tableClasses[] = 'mw-changeslist-line-watched';
160        } else {
161            $tableClasses[] = 'mw-changeslist-line-not-watched';
162        }
163
164        # Collate list of users
165        $usercounts = [];
166        $userlinks = [];
167        # Some catalyst variables...
168        $namehidden = true;
169        $allLogs = true;
170        $RCShowChangedSize = $this->getConfig()->get( MainConfigNames::RCShowChangedSize );
171
172        # Default values for RC flags
173        $collectedRcFlags = [];
174        foreach ( $recentChangesFlags as $key => $value ) {
175            $flagGrouping = $value['grouping'] ?? 'any';
176            switch ( $flagGrouping ) {
177                case 'all':
178                    $collectedRcFlags[$key] = true;
179                    break;
180                case 'any':
181                    $collectedRcFlags[$key] = false;
182                    break;
183                default:
184                    throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
185            }
186        }
187        foreach ( $block as $rcObj ) {
188            // If all log actions to this page were hidden, then don't
189            // give the name of the affected page for this block!
190            if ( !static::isDeleted( $rcObj, LogPage::DELETED_ACTION ) ) {
191                $namehidden = false;
192            }
193            $username = $rcObj->getPerformerIdentity()->getName();
194            $userlink = $rcObj->userlink;
195
196            // Redact the username from tabulation if it is rc_deleted
197            if ( static::isDeleted( $rcObj, LogPage::DELETED_USER ) ) {
198                $username = '';
199            }
200
201            if ( !isset( $usercounts[$username] ) ) {
202                $usercounts[$username] = 0;
203                $userlinks[$username] = $userlink;
204            }
205            if ( $rcObj->mAttribs['rc_source'] !== RecentChange::SRC_LOG ) {
206                $allLogs = false;
207            }
208
209            $usercounts[$username]++;
210        }
211
212        # Sort the list and convert to text
213        krsort( $usercounts );
214        asort( $usercounts );
215        $users = [];
216        foreach ( $usercounts as $username => $count ) {
217            $text = (string)$userlinks[$username];
218            if ( $count > 1 ) {
219                $formattedCount = $this->msg( 'ntimes' )->numParams( $count )->escaped();
220                $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
221            }
222            $users[] = Html::rawElement(
223                'span',
224                [ 'class' => 'mw-changeslist-user-in-group' ],
225                $text
226            );
227        }
228
229        # Article link
230        $articleLink = '';
231        $revDeletedMsg = false;
232        if ( $namehidden ) {
233            $revDeletedMsg = $this->msg( 'rev-deleted-event' )->escaped();
234        } elseif ( $allLogs ) {
235            $articleLink = $this->maybeWatchedLink( $block[0]->link, $block[0]->watched );
236        } else {
237            $articleLink = $this->getArticleLink(
238                $block[0], $block[0]->unpatrolled, $block[0]->watched );
239        }
240
241        # Sub-entries
242        $lines = [];
243        $filterClasses = [];
244        foreach ( $block as $i => $rcObj ) {
245            $line = $this->getLineData( $block, $rcObj );
246            if ( !$line ) {
247                // completely ignore this RC entry if we don't want to render it
248                unset( $block[$i] );
249                continue;
250            }
251
252            // Roll up flags
253            foreach ( $line['recentChangesFlagsRaw'] as $key => $value ) {
254                $flagGrouping = ( $recentChangesFlags[$key]['grouping'] ?? 'any' );
255                switch ( $flagGrouping ) {
256                    case 'all':
257                        if ( !$value ) {
258                            $collectedRcFlags[$key] = false;
259                        }
260                        break;
261                    case 'any':
262                        if ( $value ) {
263                            $collectedRcFlags[$key] = true;
264                        }
265                        break;
266                    default:
267                        throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
268                }
269            }
270
271            // Roll up filter-based CSS classes
272            $filterClasses = array_merge( $filterClasses, $this->getHTMLClassesForFilters( $rcObj ) );
273            // Add classes for change tags separately, getHTMLClassesForFilters() doesn't add them
274            $this->getTags( $rcObj, $filterClasses );
275            $filterClasses = array_unique( $filterClasses );
276
277            $lines[] = $line;
278        }
279
280        // Further down are some assumptions that $block is a 0-indexed array
281        // with (count-1) as last key. Let's make sure it is.
282        $block = array_values( $block );
283        $filterClasses = array_values( $filterClasses );
284
285        if ( !$block || !$lines ) {
286            // if we can't show anything, don't display this block altogether
287            return '';
288        }
289
290        $logText = $this->getLogText( $block, [], $allLogs,
291            $collectedRcFlags['newpage'], $namehidden
292        );
293
294        # Character difference (does not apply if only log items)
295        $charDifference = false;
296        if ( $RCShowChangedSize && !$allLogs ) {
297            $last = 0;
298            $first = count( $block ) - 1;
299            # Some events (like logs and category changes) have an "empty" size, so we need to skip those...
300            while ( $last < $first && $block[$last]->mAttribs['rc_new_len'] === null ) {
301                $last++;
302            }
303            while ( $last < $first && $block[$first]->mAttribs['rc_old_len'] === null ) {
304                $first--;
305            }
306            # Get net change
307            $charDifference = $this->formatCharacterDifference( $block[$first], $block[$last] ) ?: false;
308        }
309
310        $numberofWatchingusers = $this->numberofWatchingusers( $block[0]->numberofWatchingusers );
311        $usersList = $this->msg( 'brackets' )->rawParams(
312            implode( $this->message['semicolon-separator'], $users )
313        )->escaped();
314
315        $prefix = '';
316        if ( is_callable( $this->changeLinePrefixer ) ) {
317            $prefix = ( $this->changeLinePrefixer )( $block[0], $this, true );
318        }
319
320        $templateParams = [
321            'checkboxId' => 'mw-checkbox-' . base64_encode( random_bytes( 3 ) ),
322            'articleLink' => $articleLink,
323            'charDifference' => $charDifference,
324            'collectedRcFlags' => $this->recentChangesFlags( $collectedRcFlags ),
325            'filterClasses' => $filterClasses,
326            'lines' => $lines,
327            'logText' => $logText,
328            'numberofWatchingusers' => $numberofWatchingusers,
329            'prefix' => $prefix,
330            'rev-deleted-event' => $revDeletedMsg,
331            'tableClasses' => $tableClasses,
332            'timestamp' => $block[0]->timestamp,
333            'fullTimestamp' => $block[0]->getAttribute( 'rc_timestamp' ),
334            'users' => $usersList,
335        ];
336
337        $this->rcCacheIndex++;
338
339        return $this->templateParser->processTemplate(
340            'EnhancedChangesListGroup',
341            $templateParams
342        );
343    }
344
345    /**
346     * @param RCCacheEntry[] $block
347     * @param RCCacheEntry $rcObj
348     * @param array $queryParams
349     * @return array
350     */
351    protected function getLineData( array $block, RCCacheEntry $rcObj, array $queryParams = [] ) {
352        $RCShowChangedSize = $this->getConfig()->get( MainConfigNames::RCShowChangedSize );
353
354        $source = $rcObj->mAttribs['rc_source'];
355        $data = [];
356        $lineParams = [ 'targetTitle' => $rcObj->getTitle() ];
357
358        $classes = [ 'mw-enhanced-rc' ];
359        if ( $rcObj->watched ) {
360            $classes[] = 'mw-enhanced-watched';
361        }
362        $classes = array_merge( $classes, $this->getHTMLClasses( $rcObj, $rcObj->watched ) );
363
364        $separator = ' <span class="mw-changeslist-separator"></span> ';
365
366        $data['recentChangesFlags'] = [
367            'newpage' => $source == RecentChange::SRC_NEW,
368            'minor' => $rcObj->mAttribs['rc_minor'],
369            'unpatrolled' => $rcObj->unpatrolled,
370            'bot' => $rcObj->mAttribs['rc_bot'],
371        ];
372
373        # Log timestamp
374        if ( $source == RecentChange::SRC_LOG ) {
375            $link = htmlspecialchars( $rcObj->timestamp );
376            # Revision link
377        } elseif ( !ChangesList::userCan( $rcObj, RevisionRecord::DELETED_TEXT, $this->getAuthority() ) ) {
378            $link = Html::element( 'span', [ 'class' => 'history-deleted' ], $rcObj->timestamp );
379        } else {
380            $params = [];
381            $params['curid'] = $rcObj->mAttribs['rc_cur_id'];
382            if ( $rcObj->mAttribs['rc_this_oldid'] != 0 ) {
383                $params['oldid'] = $rcObj->mAttribs['rc_this_oldid'];
384            }
385            // FIXME: The link has incorrect "title=" when rc_source = RecentChange::SRC_CATEGORIZE.
386            // rc_cur_id refers to the page that was categorized
387            // whereas RecentChange::getTitle refers to the category.
388            $link = $this->linkRenderer->makeKnownLink(
389                $rcObj->getTitle(),
390                $rcObj->timestamp,
391                [],
392                $params + $queryParams
393            );
394            if ( static::isDeleted( $rcObj, RevisionRecord::DELETED_TEXT ) ) {
395                $link = '<span class="history-deleted">' . $link . '</span> ';
396            }
397        }
398        $data['timestampLink'] = $link;
399
400        $currentAndLastLinks = '';
401        if ( $source == RecentChange::SRC_EDIT || $source == RecentChange::SRC_NEW ) {
402            $currentAndLastLinks .= ' ' . $this->msg( 'parentheses' )->rawParams(
403                    $rcObj->curlink .
404                    $this->message['pipe-separator'] .
405                    $rcObj->lastlink
406                )->escaped();
407        }
408        $data['currentAndLastLinks'] = $currentAndLastLinks;
409        $data['separatorAfterCurrentAndLastLinks'] = $separator;
410
411        # Character diff
412        if ( $RCShowChangedSize ) {
413            $cd = $this->formatCharacterDifference( $rcObj );
414            if ( $cd !== '' ) {
415                $data['characterDiff'] = $cd;
416                $data['separatorAfterCharacterDiff'] = $separator;
417            }
418        }
419
420        if ( $source == RecentChange::SRC_LOG ) {
421            $data['logEntry'] = $this->insertLogEntry( $rcObj );
422        } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
423            $data['comment'] = $this->insertComment( $rcObj );
424        } else {
425            # User links
426            $data['userLink'] = $rcObj->userlink;
427            $data['userTalkLink'] = $rcObj->usertalklink;
428            $data['comment'] = $this->insertComment( $rcObj );
429            if ( $source == RecentChange::SRC_CATEGORIZE ) {
430                $data['historyLink'] = $this->getDiffHistLinks( $rcObj, false );
431            }
432            # Rollback, thanks etc...
433            $data['rollback'] = $this->getRollback( $rcObj );
434        }
435
436        # Tags
437        $data['tags'] = $this->getTags( $rcObj, $classes );
438
439        # Watchlist labels
440        $data['labels'] = $this->getLabels( $rcObj, $classes );
441
442        $attribs = $this->getDataAttributes( $rcObj );
443
444        // give the hook a chance to modify the data
445        $success = $this->getHookRunner()->onEnhancedChangesListModifyLineData(
446            $this, $data, $block, $rcObj, $classes, $attribs );
447        if ( !$success ) {
448            // skip entry if hook aborted it
449            return [];
450        }
451        $attribs = array_filter( $attribs,
452            Sanitizer::isReservedDataAttribute( ... ),
453            ARRAY_FILTER_USE_KEY
454        );
455
456        $lineParams['recentChangesFlagsRaw'] = [];
457        if ( isset( $data['recentChangesFlags'] ) ) {
458            $lineParams['recentChangesFlags'] = $this->recentChangesFlags( $data['recentChangesFlags'] );
459            # FIXME: This is used by logic, don't return it in the template params.
460            $lineParams['recentChangesFlagsRaw'] = $data['recentChangesFlags'];
461            unset( $data['recentChangesFlags'] );
462        }
463
464        if ( isset( $data['timestampLink'] ) ) {
465            $lineParams['timestampLink'] = $data['timestampLink'];
466            unset( $data['timestampLink'] );
467        }
468
469        $lineParams['classes'] = array_values( $classes );
470        $lineParams['attribs'] = Html::expandAttributes( $attribs );
471
472        // everything else: makes it easier for extensions to add or remove data
473        $lineParams['data'] = array_values( $data );
474
475        return $lineParams;
476    }
477
478    /**
479     * Generates amount of changes (linking to diff ) & link to history.
480     *
481     * @param RCCacheEntry[] $block
482     * @param array $queryParams
483     * @param bool $allLogs
484     * @param bool $isnew
485     * @param bool $namehidden
486     * @return string
487     */
488    protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
489        if ( !$block ) {
490            return '';
491        }
492
493        // Changes message
494        static $nchanges = [];
495        static $sinceLastVisitMsg = [];
496
497        $n = count( $block );
498        if ( !isset( $nchanges[$n] ) ) {
499            $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
500        }
501
502        $sinceLast = 0;
503        $unvisitedOldid = null;
504        $currentRevision = 0;
505        $previousRevision = 0;
506        $curId = 0;
507        $allCategorization = true;
508        /** @var RCCacheEntry $rcObj */
509        foreach ( $block as $rcObj ) {
510            // Fields of categorization entries refer to the changed page
511            // rather than the category for which we are building the log text.
512            if ( $rcObj->mAttribs['rc_source'] == RecentChange::SRC_CATEGORIZE ) {
513                continue;
514            }
515
516            $allCategorization = false;
517            $previousRevision = $rcObj->mAttribs['rc_last_oldid'];
518            // Same logic as below inside main foreach
519            if ( $rcObj->watched ) {
520                $sinceLast++;
521                $unvisitedOldid = $previousRevision;
522            }
523            if ( !$currentRevision ) {
524                $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
525            }
526            if ( !$curId ) {
527                $curId = $rcObj->mAttribs['rc_cur_id'];
528            }
529        }
530
531        // Total change link
532        $links = [];
533        $title = $block[0]->getTitle();
534        if ( !$allLogs ) {
535            // TODO: Disable the link if the user cannot see it (rc_deleted).
536            // Beware of possibly interspersed categorization entries.
537            if ( $isnew || $allCategorization ) {
538                $links['total-changes'] = Html::rawElement( 'span', [], $nchanges[$n] );
539            } else {
540                $links['total-changes'] = Html::rawElement( 'span', [],
541                    $this->linkRenderer->makeKnownLink(
542                        $title,
543                        new HtmlArmor( $nchanges[$n] ),
544                        [ 'class' => 'mw-changeslist-groupdiff' ],
545                        $queryParams + [
546                            'curid' => $curId,
547                            'diff' => $currentRevision,
548                            'oldid' => $previousRevision,
549                        ]
550                    )
551                );
552            }
553
554            if (
555                !$allCategorization &&
556                $sinceLast > 0 &&
557                $sinceLast < $n
558            ) {
559                if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
560                    $sinceLastVisitMsg[$sinceLast] =
561                        $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
562                }
563                $links['total-changes-since-last'] = Html::rawElement( 'span', [],
564                    $this->linkRenderer->makeKnownLink(
565                        $title,
566                        new HtmlArmor( $sinceLastVisitMsg[$sinceLast] ),
567                        [ 'class' => 'mw-changeslist-groupdiff' ],
568                        $queryParams + [
569                            'curid' => $curId,
570                            'diff' => $currentRevision,
571                            'oldid' => $unvisitedOldid,
572                        ]
573                    )
574                );
575            }
576        }
577
578        // History
579        if ( $allLogs || $allCategorization ) {
580            // don't show history link for logs
581        } elseif ( $namehidden || !$title->exists() ) {
582            $links['history'] = Html::rawElement( 'span', [], $this->message['enhancedrc-history'] );
583        } else {
584            $links['history'] = Html::rawElement( 'span', [],
585                $this->linkRenderer->makeKnownLink(
586                    $title,
587                    new HtmlArmor( $this->message['enhancedrc-history'] ),
588                    [ 'class' => 'mw-changeslist-history' ],
589                    [
590                        'curid' => $curId,
591                        'action' => 'history',
592                    ] + $queryParams
593                )
594            );
595        }
596
597        // Allow others to alter, remove or add to these links
598        $this->getHookRunner()->onEnhancedChangesList__getLogText( $this, $links, $block );
599
600        if ( !$links ) {
601            return '';
602        }
603
604        $logtext = Html::rawElement( 'span', [ 'class' => 'mw-changeslist-links' ],
605            implode( ' ', $links ) );
606        return ' ' . $logtext;
607    }
608
609    /**
610     * Enhanced RC ungrouped line.
611     *
612     * @param RecentChange|RCCacheEntry $rcObj
613     * @return string A HTML formatted line (generated using $r)
614     */
615    protected function recentChangesBlockLine( $rcObj ) {
616        $data = [];
617
618        $source = $rcObj->mAttribs['rc_source'];
619        $logType = $rcObj->mAttribs['rc_log_type'];
620        $classes = $this->getHTMLClasses( $rcObj, $rcObj->watched );
621        $classes[] = 'mw-enhanced-rc';
622
623        if ( $logType ) {
624            # Log entry
625            $classes[] = 'mw-changeslist-log';
626            $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-' . $logType );
627        } else {
628            $classes[] = 'mw-changeslist-edit';
629            $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns' .
630                $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
631        }
632
633        # Flag and Timestamp
634        $data['recentChangesFlags'] = [
635            'newpage' => $source == RecentChange::SRC_NEW,
636            'minor' => $rcObj->mAttribs['rc_minor'],
637            'unpatrolled' => $rcObj->unpatrolled,
638            'bot' => $rcObj->mAttribs['rc_bot'],
639        ];
640        // timestamp is not really a link here, but is called timestampLink
641        // for consistency with EnhancedChangesListModifyLineData
642        $data['timestampLink'] = htmlspecialchars( $rcObj->timestamp );
643
644        # Article or log link
645        if ( $logType ) {
646            $logPage = new LogPage( $logType );
647            $logTitle = SpecialPage::getTitleFor( 'Log', $logType );
648            $logName = $logPage->getName()->text();
649            $data['logLink'] = Html::rawElement( 'span', [ 'class' => 'mw-changeslist-links' ],
650                $this->linkRenderer->makeKnownLink( $logTitle, $logName )
651            );
652        } else {
653            $data['articleLink'] = $this->getArticleLink( $rcObj, $rcObj->unpatrolled, $rcObj->watched );
654        }
655
656        # Diff and hist links
657        if ( $source != RecentChange::SRC_LOG && $source != RecentChange::SRC_CATEGORIZE ) {
658            $data['historyLink'] = $this->getDiffHistLinks( $rcObj, false );
659        }
660        $data['separatorAfterLinks'] = ' <span class="mw-changeslist-separator"></span> ';
661
662        # Character diff
663        if ( $this->getConfig()->get( MainConfigNames::RCShowChangedSize ) ) {
664            $cd = $this->formatCharacterDifference( $rcObj );
665            if ( $cd !== '' ) {
666                $data['characterDiff'] = $cd;
667                $data['separatorAftercharacterDiff'] = ' <span class="mw-changeslist-separator"></span> ';
668            }
669        }
670
671        if ( $source == RecentChange::SRC_LOG ) {
672            $data['logEntry'] = $this->insertLogEntry( $rcObj );
673        } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
674            $data['comment'] = $this->insertComment( $rcObj );
675        } else {
676            $data['userLink'] = $rcObj->userlink;
677            $data['userTalkLink'] = $rcObj->usertalklink;
678            $data['comment'] = $this->insertComment( $rcObj );
679            if ( $source == RecentChange::SRC_CATEGORIZE ) {
680                $data['historyLink'] = $this->getDiffHistLinks( $rcObj, false );
681            }
682            $data['rollback'] = $this->getRollback( $rcObj );
683        }
684
685        # Tags
686        $data['tags'] = $this->getTags( $rcObj, $classes );
687
688        # WatchlistLabels
689        $data['labels'] = $this->getLabels( $rcObj, $classes );
690
691        # Show how many people are watching this if enabled
692        $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers );
693
694        $data['attribs'] = array_merge( $this->getDataAttributes( $rcObj ), [ 'class' => $classes ] );
695
696        // give the hook a chance to modify the data
697        $success = $this->getHookRunner()->onEnhancedChangesListModifyBlockLineData(
698            $this, $data, $rcObj );
699        if ( !$success ) {
700            // skip entry if hook aborted it
701            return '';
702        }
703        $attribs = $data['attribs'];
704        unset( $data['attribs'] );
705        $attribs = array_filter( $attribs, static function ( $key ) {
706            return $key === 'class' || Sanitizer::isReservedDataAttribute( $key );
707        }, ARRAY_FILTER_USE_KEY );
708
709        $prefix = '';
710        if ( is_callable( $this->changeLinePrefixer ) ) {
711            $prefix = ( $this->changeLinePrefixer )( $rcObj, $this, false );
712        }
713
714        $line = Html::openElement( 'table', $attribs ) . Html::openElement( 'tr' );
715        // Highlight block
716        $line .= Html::rawElement( 'td', [],
717            $this->getHighlightsContainerDiv()
718        );
719
720        $line .= Html::rawElement( 'td', [], '<span class="mw-enhancedchanges-arrow-space"></span>' );
721        $line .= Html::rawElement( 'td', [ 'class' => 'mw-changeslist-line-prefix' ], $prefix );
722        $line .= '<td class="mw-enhanced-rc" colspan="2">';
723
724        if ( isset( $data['recentChangesFlags'] ) ) {
725            $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
726            unset( $data['recentChangesFlags'] );
727        }
728
729        if ( isset( $data['timestampLink'] ) ) {
730            $line .= "\u{00A0}" . $data['timestampLink'];
731            unset( $data['timestampLink'] );
732        }
733        $line .= "\u{00A0}</td>";
734        $line .= Html::openElement( 'td', [
735            'class' => 'mw-changeslist-line-inner',
736            // Used for reliable determination of the affiliated page
737            'data-target-page' => $rcObj->getTitle(),
738        ] );
739
740        // everything else: makes it easier for extensions to add or remove data
741        foreach ( $data as $key => $dataItem ) {
742            $line .= Html::rawElement( 'span', [
743                'class' => 'mw-changeslist-line-inner-' . $key,
744            ], $dataItem );
745        }
746
747        $line .= "</td></tr></table>\n";
748
749        return $line;
750    }
751
752    /**
753     * Returns value to be used in 'historyLink' element of $data param in
754     * EnhancedChangesListModifyBlockLineData hook.
755     *
756     * @since 1.27
757     *
758     * @param RCCacheEntry $rc
759     * @param bool|array|null $query deprecated
760     * @param bool|null $useParentheses (optional) Wrap comments in parentheses where needed
761     * @return string HTML
762     */
763    public function getDiffHistLinks( RCCacheEntry $rc, $query = null, $useParentheses = null ) {
764        if ( is_bool( $query ) ) {
765            $useParentheses = $query;
766        } elseif ( $query !== null ) {
767            wfDeprecated( __METHOD__ . ' with $query parameter', '1.36' );
768        }
769        $pageTitle = $rc->getTitle();
770        if ( $rc->getAttribute( 'rc_source' ) == RecentChange::SRC_CATEGORIZE ) {
771            // For categorizations we must swap the category title with the page title!
772            $pageTitle = Title::newFromID( $rc->getAttribute( 'rc_cur_id' ) );
773            if ( !$pageTitle ) {
774                // The page has been deleted, but the RC entry
775                // deletion job has not run yet. Just skip.
776                return '';
777            }
778        }
779
780        $histLink = $this->linkRenderer->makeKnownLink(
781            $pageTitle,
782            new HtmlArmor( $this->message['hist'] ),
783            [ 'class' => 'mw-changeslist-history' ],
784            [
785                'curid' => $rc->getAttribute( 'rc_cur_id' ),
786                'action' => 'history'
787            ]
788        );
789        if ( $useParentheses !== false ) {
790            $retVal = $this->msg( 'parentheses' )
791            ->rawParams( $rc->difflink . $this->message['pipe-separator']
792                . $histLink )->escaped();
793        } else {
794            $retVal = Html::rawElement( 'span', [ 'class' => 'mw-changeslist-links' ],
795                Html::rawElement( 'span', [], $rc->difflink ) .
796                Html::rawElement( 'span', [], $histLink )
797            );
798        }
799        return ' ' . $retVal;
800    }
801
802    /**
803     * If enhanced RC is in use, this function takes the previously cached
804     * RC lines, arranges them, and outputs the HTML
805     *
806     * @return string
807     */
808    protected function recentChangesBlock() {
809        if ( count( $this->rc_cache ) == 0 ) {
810            return '';
811        }
812
813        $blockOut = '';
814        foreach ( $this->rc_cache as $block ) {
815            if ( count( $block ) < 2 ) {
816                $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
817            } else {
818                $blockOut .= $this->recentChangesBlockGroup( $block );
819            }
820        }
821
822        if ( $blockOut === '' ) {
823            return '';
824        }
825        // $this->lastdate is kept up to date by recentChangesLine()
826        return Html::element( 'h4', [], $this->lastdate ) . "\n<div>" . $blockOut . '</div>';
827    }
828
829    /**
830     * Returns text for the end of RC
831     * If enhanced RC is in use, returns pretty much all the text
832     * @return string
833     */
834    public function endRecentChangesList() {
835        return $this->recentChangesBlock() . '</div>';
836    }
837}
838
839/** @deprecated class alias since 1.44 */
840class_alias( EnhancedChangesList::class, 'EnhancedChangesList' );