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