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