Debian Codegraph @

includes/recentchanges/EnhancedChangesList.php

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