Merge "Special:Newpages feed now shows first revision instead of latest revision"
[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 = ( isset( $recentChangesFlags[$key]['grouping'] ) ?
207 $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 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 = ( isset( $recentChangesFlags[$key]['grouping'] ) ?
281 $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 $lines[] = $line;
299 }
300
301 // Further down are some assumptions that $block is a 0-indexed array
302 // with (count-1) as last key. Let's make sure it is.
303 $block = array_values( $block );
304
305 if ( empty( $block ) || !$lines ) {
306 // if we can't show anything, don't display this block altogether
307 return '';
308 }
309
310 $logText = $this->getLogText( $block, $queryParams, $allLogs,
311 $collectedRcFlags['newpage'], $namehidden
312 );
313
314 # Character difference (does not apply if only log items)
315 $charDifference = false;
316 if ( $RCShowChangedSize && !$allLogs ) {
317 $last = 0;
318 $first = count( $block ) - 1;
319 # Some events (like logs and category changes) have an "empty" size, so we need to skip those...
320 while ( $last < $first && $block[$last]->mAttribs['rc_new_len'] === null ) {
321 $last++;
322 }
323 while ( $last < $first && $block[$first]->mAttribs['rc_old_len'] === null ) {
324 $first--;
325 }
326 # Get net change
327 $charDifference = $this->formatCharacterDifference( $block[$first], $block[$last] );
328 }
329
330 $numberofWatchingusers = $this->numberofWatchingusers( $block[0]->numberofWatchingusers );
331 $usersList = $this->msg( 'brackets' )->rawParams(
332 implode( $this->message['semicolon-separator'], $users )
333 )->escaped();
334
335 $prefix = '';
336 if ( is_callable( $this->changeLinePrefixer ) ) {
337 $prefix = call_user_func( $this->changeLinePrefixer, $block[0], $this, true );
338 }
339
340 $templateParams = [
341 'articleLink' => $articleLink,
342 'charDifference' => $charDifference,
343 'collectedRcFlags' => $this->recentChangesFlags( $collectedRcFlags ),
344 'languageDirMark' => $this->getLanguage()->getDirMark(),
345 'lines' => $lines,
346 'logText' => $logText,
347 'numberofWatchingusers' => $numberofWatchingusers,
348 'prefix' => $prefix,
349 'rev-deleted-event' => $revDeletedMsg,
350 'tableClasses' => $tableClasses,
351 'timestamp' => $block[0]->timestamp,
352 'users' => $usersList,
353 ];
354
355 $this->rcCacheIndex++;
356
357 return $this->templateParser->processTemplate(
358 'EnhancedChangesListGroup',
359 $templateParams
360 );
361 }
362
363 /**
364 * @param RCCacheEntry[] $block
365 * @param RCCacheEntry $rcObj
366 * @param array $queryParams
367 * @return array
368 * @throws Exception
369 * @throws FatalError
370 * @throws MWException
371 */
372 protected function getLineData( array $block, RCCacheEntry $rcObj, array $queryParams = [] ) {
373 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
374
375 $type = $rcObj->mAttribs['rc_type'];
376 $data = [];
377 $lineParams = [ 'targetTitle' => $rcObj->getTitle() ];
378
379 $classes = [ 'mw-enhanced-rc' ];
380 if ( $rcObj->watched
381 && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched
382 ) {
383 $classes[] = 'mw-enhanced-watched';
384 }
385 $classes = array_merge( $classes, $this->getHTMLClassesForFilters( $rcObj ) );
386
387 $separator = ' <span class="mw-changeslist-separator">. .</span> ';
388
389 $data['recentChangesFlags'] = [
390 'newpage' => $type == RC_NEW,
391 'minor' => $rcObj->mAttribs['rc_minor'],
392 'unpatrolled' => $rcObj->unpatrolled,
393 'bot' => $rcObj->mAttribs['rc_bot'],
394 ];
395
396 $params = $queryParams;
397
398 if ( $rcObj->mAttribs['rc_this_oldid'] != 0 ) {
399 $params['oldid'] = $rcObj->mAttribs['rc_this_oldid'];
400 }
401
402 # Log timestamp
403 if ( $type == RC_LOG ) {
404 $link = $rcObj->timestamp;
405 # Revision link
406 } elseif ( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT, $this->getUser() ) ) {
407 $link = '<span class="history-deleted">' . $rcObj->timestamp . '</span> ';
408 } else {
409 $link = $this->linkRenderer->makeKnownLink(
410 $rcObj->getTitle(),
411 new HtmlArmor( $rcObj->timestamp ),
412 [],
413 $params
414 );
415 if ( $this->isDeleted( $rcObj, Revision::DELETED_TEXT ) ) {
416 $link = '<span class="history-deleted">' . $link . '</span> ';
417 }
418 }
419 $data['timestampLink'] = $link;
420
421 $currentAndLastLinks = '';
422 if ( !$type == RC_LOG || $type == RC_NEW ) {
423 $currentAndLastLinks .= ' ' . $this->msg( 'parentheses' )->rawParams(
424 $rcObj->curlink .
425 $this->message['pipe-separator'] .
426 $rcObj->lastlink
427 )->escaped();
428 }
429 $data['currentAndLastLinks'] = $currentAndLastLinks;
430 $data['separatorAfterCurrentAndLastLinks'] = $separator;
431
432 # Character diff
433 if ( $RCShowChangedSize ) {
434 $cd = $this->formatCharacterDifference( $rcObj );
435 if ( $cd !== '' ) {
436 $data['characterDiff'] = $cd;
437 $data['separatorAfterCharacterDiff'] = $separator;
438 }
439 }
440
441 if ( $rcObj->mAttribs['rc_type'] == RC_LOG ) {
442 $data['logEntry'] = $this->insertLogEntry( $rcObj );
443 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
444 $data['comment'] = $this->insertComment( $rcObj );
445 } else {
446 # User links
447 $data['userLink'] = $rcObj->userlink;
448 $data['userTalkLink'] = $rcObj->usertalklink;
449 $data['comment'] = $this->insertComment( $rcObj );
450 }
451
452 # Rollback
453 $data['rollback'] = $this->getRollback( $rcObj );
454
455 # Tags
456 $data['tags'] = $this->getTags( $rcObj, $classes );
457
458 $attribs = $this->getDataAttributes( $rcObj );
459
460 // give the hook a chance to modify the data
461 $success = Hooks::run( 'EnhancedChangesListModifyLineData',
462 [ $this, &$data, $block, $rcObj, &$classes, &$attribs ] );
463 if ( !$success ) {
464 // skip entry if hook aborted it
465 return [];
466 }
467 $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] );
468
469 $lineParams['recentChangesFlagsRaw'] = [];
470 if ( isset( $data['recentChangesFlags'] ) ) {
471 $lineParams['recentChangesFlags'] = $this->recentChangesFlags( $data['recentChangesFlags'] );
472 # FIXME: This is used by logic, don't return it in the template params.
473 $lineParams['recentChangesFlagsRaw'] = $data['recentChangesFlags'];
474 unset( $data['recentChangesFlags'] );
475 }
476
477 if ( isset( $data['timestampLink'] ) ) {
478 $lineParams['timestampLink'] = $data['timestampLink'];
479 unset( $data['timestampLink'] );
480 }
481
482 $lineParams['classes'] = array_values( $classes );
483 $lineParams['attribs'] = Html::expandAttributes( $attribs );
484
485 // everything else: makes it easier for extensions to add or remove data
486 $lineParams['data'] = array_values( $data );
487
488 return $lineParams;
489 }
490
491 /**
492 * Generates amount of changes (linking to diff ) & link to history.
493 *
494 * @param array $block
495 * @param array $queryParams
496 * @param bool $allLogs
497 * @param bool $isnew
498 * @param bool $namehidden
499 * @return string
500 */
501 protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
502 if ( empty( $block ) ) {
503 return '';
504 }
505
506 # Changes message
507 static $nchanges = [];
508 static $sinceLastVisitMsg = [];
509
510 $n = count( $block );
511 if ( !isset( $nchanges[$n] ) ) {
512 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
513 }
514
515 $sinceLast = 0;
516 $unvisitedOldid = null;
517 /** @var $rcObj RCCacheEntry */
518 foreach ( $block as $rcObj ) {
519 // Same logic as below inside main foreach
520 if ( $rcObj->watched && $rcObj->mAttribs['rc_timestamp'] >= $rcObj->watched ) {
521 $sinceLast++;
522 $unvisitedOldid = $rcObj->mAttribs['rc_last_oldid'];
523 }
524 }
525 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
526 $sinceLastVisitMsg[$sinceLast] =
527 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
528 }
529
530 $currentRevision = 0;
531 foreach ( $block as $rcObj ) {
532 if ( !$currentRevision ) {
533 $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
534 }
535 }
536
537 # Total change link
538 $links = [];
539 /** @var $block0 RecentChange */
540 $block0 = $block[0];
541 $last = $block[count( $block ) - 1];
542 if ( !$allLogs ) {
543 if ( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT, $this->getUser() ) ||
544 $isnew ||
545 $rcObj->mAttribs['rc_type'] == RC_CATEGORIZE
546 ) {
547 $links['total-changes'] = $nchanges[$n];
548 } else {
549 $links['total-changes'] = $this->linkRenderer->makeKnownLink(
550 $block0->getTitle(),
551 new HtmlArmor( $nchanges[$n] ),
552 [ 'class' => 'mw-changeslist-groupdiff' ],
553 $queryParams + [
554 'diff' => $currentRevision,
555 'oldid' => $last->mAttribs['rc_last_oldid'],
556 ]
557 );
558 if ( $sinceLast > 0 && $sinceLast < $n ) {
559 $links['total-changes-since-last'] = $this->linkRenderer->makeKnownLink(
560 $block0->getTitle(),
561 new HtmlArmor( $sinceLastVisitMsg[$sinceLast] ),
562 [ 'class' => 'mw-changeslist-groupdiff' ],
563 $queryParams + [
564 'diff' => $currentRevision,
565 'oldid' => $unvisitedOldid,
566 ]
567 );
568 }
569 }
570 }
571
572 # History
573 if ( $allLogs || $rcObj->mAttribs['rc_type'] == RC_CATEGORIZE ) {
574 // don't show history link for logs
575 } elseif ( $namehidden || !$block0->getTitle()->exists() ) {
576 $links['history'] = $this->message['enhancedrc-history'];
577 } else {
578 $params = $queryParams;
579 $params['action'] = 'history';
580
581 $links['history'] = $this->linkRenderer->makeKnownLink(
582 $block0->getTitle(),
583 new HtmlArmor( $this->message['enhancedrc-history'] ),
584 [ 'class' => 'mw-changeslist-history' ],
585 $params
586 );
587 }
588
589 # Allow others to alter, remove or add to these links
590 Hooks::run( 'EnhancedChangesList::getLogText',
591 [ $this, &$links, $block ] );
592
593 if ( !$links ) {
594 return '';
595 }
596
597 $logtext = implode( $this->message['pipe-separator'], $links );
598 $logtext = $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
599 return ' ' . $logtext;
600 }
601
602 /**
603 * Enhanced RC ungrouped line.
604 *
605 * @param RecentChange|RCCacheEntry $rcObj
606 * @return string A HTML formatted line (generated using $r)
607 */
608 protected function recentChangesBlockLine( $rcObj ) {
609 $data = [];
610
611 $query['curid'] = $rcObj->mAttribs['rc_cur_id'];
612
613 $type = $rcObj->mAttribs['rc_type'];
614 $logType = $rcObj->mAttribs['rc_log_type'];
615 $classes = $this->getHTMLClasses( $rcObj, $rcObj->watched );
616 $classes[] = 'mw-enhanced-rc';
617
618 if ( $logType ) {
619 # Log entry
620 $classes[] = 'mw-changeslist-log';
621 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-' . $logType );
622 } else {
623 $classes[] = 'mw-changeslist-edit';
624 $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns' .
625 $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
626 }
627
628 # Flag and Timestamp
629 $data['recentChangesFlags'] = [
630 'newpage' => $type == RC_NEW,
631 'minor' => $rcObj->mAttribs['rc_minor'],
632 'unpatrolled' => $rcObj->unpatrolled,
633 'bot' => $rcObj->mAttribs['rc_bot'],
634 ];
635 // timestamp is not really a link here, but is called timestampLink
636 // for consistency with EnhancedChangesListModifyLineData
637 $data['timestampLink'] = $rcObj->timestamp;
638
639 # Article or log link
640 if ( $logType ) {
641 $logPage = new LogPage( $logType );
642 $logTitle = SpecialPage::getTitleFor( 'Log', $logType );
643 $logName = $logPage->getName()->text();
644 $data['logLink'] = $this->msg( 'parentheses' )
645 ->rawParams(
646 $this->linkRenderer->makeKnownLink( $logTitle, $logName )
647 )->escaped();
648 } else {
649 $data['articleLink'] = $this->getArticleLink( $rcObj, $rcObj->unpatrolled, $rcObj->watched );
650 }
651
652 # Diff and hist links
653 if ( $type != RC_LOG && $type != RC_CATEGORIZE ) {
654 $query['action'] = 'history';
655 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
656 }
657 $data['separatorAfterLinks'] = ' <span class="mw-changeslist-separator">. .</span> ';
658
659 # Character diff
660 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
661 $cd = $this->formatCharacterDifference( $rcObj );
662 if ( $cd !== '' ) {
663 $data['characterDiff'] = $cd;
664 $data['separatorAftercharacterDiff'] = ' <span class="mw-changeslist-separator">. .</span> ';
665 }
666 }
667
668 if ( $type == RC_LOG ) {
669 $data['logEntry'] = $this->insertLogEntry( $rcObj );
670 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
671 $data['comment'] = $this->insertComment( $rcObj );
672 } else {
673 $data['userLink'] = $rcObj->userlink;
674 $data['userTalkLink'] = $rcObj->usertalklink;
675 $data['comment'] = $this->insertComment( $rcObj );
676 if ( $type == RC_CATEGORIZE ) {
677 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
678 }
679 $data['rollback'] = $this->getRollback( $rcObj );
680 }
681
682 # Tags
683 $data['tags'] = $this->getTags( $rcObj, $classes );
684
685 # Show how many people are watching this if enabled
686 $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers );
687
688 $data['attribs'] = array_merge( $this->getDataAttributes( $rcObj ), [ 'class' => $classes ] );
689
690 // give the hook a chance to modify the data
691 $success = Hooks::run( 'EnhancedChangesListModifyBlockLineData',
692 [ $this, &$data, $rcObj ] );
693 if ( !$success ) {
694 // skip entry if hook aborted it
695 return '';
696 }
697 $attribs = $data['attribs'];
698 unset( $data['attribs'] );
699 $attribs = wfArrayFilterByKey( $attribs, function ( $key ) {
700 return $key === 'class' || Sanitizer::isReservedDataAttribute( $key );
701 } );
702
703 $prefix = '';
704 if ( is_callable( $this->changeLinePrefixer ) ) {
705 $prefix = call_user_func( $this->changeLinePrefixer, $rcObj, $this, false );
706 }
707
708 $line = Html::openElement( 'table', $attribs ) . Html::openElement( 'tr' );
709 $line .= Html::rawElement( 'td', [], '<span class="mw-enhancedchanges-arrow-space"></span>' );
710 $line .= Html::rawElement( 'td', [ 'class' => 'mw-changeslist-line-prefix' ], $prefix );
711 $line .= '<td class="mw-enhanced-rc">';
712
713 if ( isset( $data['recentChangesFlags'] ) ) {
714 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
715 unset( $data['recentChangesFlags'] );
716 }
717
718 if ( isset( $data['timestampLink'] ) ) {
719 $line .= '&#160;' . $data['timestampLink'];
720 unset( $data['timestampLink'] );
721 }
722 $line .= '&#160;</td>';
723 $line .= Html::openElement( 'td', [
724 'class' => 'mw-changeslist-line-inner',
725 // Used for reliable determination of the affiliated page
726 'data-target-page' => $rcObj->getTitle(),
727 ] );
728
729 // everything else: makes it easier for extensions to add or remove data
730 $line .= implode( '', $data );
731
732 $line .= "</td></tr></table>\n";
733
734 return $line;
735 }
736
737 /**
738 * Returns value to be used in 'historyLink' element of $data param in
739 * EnhancedChangesListModifyBlockLineData hook.
740 *
741 * @since 1.27
742 *
743 * @param RCCacheEntry $rc
744 * @param array $query array of key/value pairs to append as a query string
745 * @return string HTML
746 */
747 public function getDiffHistLinks( RCCacheEntry $rc, array $query ) {
748 $pageTitle = $rc->getTitle();
749 if ( $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
750 // For categorizations we must swap the category title with the page title!
751 $pageTitle = Title::newFromID( $rc->getAttribute( 'rc_cur_id' ) );
752 if ( !$pageTitle ) {
753 // The page has been deleted, but the RC entry
754 // deletion job has not run yet. Just skip.
755 return '';
756 }
757 }
758
759 $retVal = ' ' . $this->msg( 'parentheses' )
760 ->rawParams( $rc->difflink . $this->message['pipe-separator']
761 . $this->linkRenderer->makeKnownLink(
762 $pageTitle,
763 new HtmlArmor( $this->message['hist'] ),
764 [ 'class' => 'mw-changeslist-history' ],
765 $query
766 ) )->escaped();
767 return $retVal;
768 }
769
770 /**
771 * If enhanced RC is in use, this function takes the previously cached
772 * RC lines, arranges them, and outputs the HTML
773 *
774 * @return string
775 */
776 protected function recentChangesBlock() {
777 if ( count( $this->rc_cache ) == 0 ) {
778 return '';
779 }
780
781 $blockOut = '';
782 foreach ( $this->rc_cache as $block ) {
783 if ( count( $block ) < 2 ) {
784 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
785 } else {
786 $blockOut .= $this->recentChangesBlockGroup( $block );
787 }
788 }
789
790 if ( $blockOut === '' ) {
791 return '';
792 }
793 // $this->lastdate is kept up to date by recentChangesLine()
794 return Xml::element( 'h4', null, $this->lastdate ) . "\n<div>" . $blockOut . '</div>';
795 }
796
797 /**
798 * Returns text for the end of RC
799 * If enhanced RC is in use, returns pretty much all the text
800 * @return string
801 */
802 public function endRecentChangesList() {
803 return $this->recentChangesBlock() . '</div>';
804 }
805 }