3 * Generates a list of changes using an Enhanced system (uses javascript).
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.
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.
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
23 class EnhancedChangesList
extends ChangesList
{
26 * @var RCCacheEntryFactory
28 protected $cacheEntryFactory;
31 * @var array Array of array of RCCacheEntry
38 protected $templateParser;
41 * @param IContextSource|Skin $obj
42 * @param array $filterGroups Array of ChangesListFilterGroup objects (currently optional)
45 public function __construct( $obj, array $filterGroups = [] ) {
46 if ( $obj instanceof Skin
) {
47 // @todo: deprecate constructing with Skin
48 $context = $obj->getContext();
50 if ( !$obj instanceof IContextSource
) {
51 throw new MWException( 'EnhancedChangesList must be constructed with a '
52 . 'context source or skin.' );
58 parent
::__construct( $context, $filterGroups );
60 // message is set by the parent ChangesList class
61 $this->cacheEntryFactory
= new RCCacheEntryFactory(
66 $this->templateParser
= new TemplateParser();
70 * Add the JavaScript file for enhanced changeslist
73 public function beginRecentChangesList() {
75 $this->rcMoveIndex
= 0;
76 $this->rcCacheIndex
= 0;
78 $this->rclistOpen
= false;
79 $this->getOutput()->addModuleStyles( [
80 'mediawiki.special.changeslist',
81 'mediawiki.special.changeslist.enhanced',
83 $this->getOutput()->addModules( [
84 'jquery.makeCollapsible',
88 return '<div class="mw-changeslist">';
92 * Format a line for enhanced recentchange (aka with javascript and block of lines).
94 * @param RecentChange $rc
95 * @param bool $watched
96 * @param int $linenumber (default null)
100 public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
102 $date = $this->getLanguage()->userDate(
103 $rc->mAttribs
['rc_timestamp'],
109 # If it's a new day, add the headline and flush the cache
110 if ( $date != $this->lastdate
) {
111 # Process current cache
112 $ret = $this->recentChangesBlock();
113 $this->rc_cache
= [];
114 $ret .= Xml
::element( 'h4', null, $date ) . "\n";
115 $this->lastdate
= $date;
118 $cacheEntry = $this->cacheEntryFactory
->newFromRecentChange( $rc, $watched );
119 $this->addCacheEntry( $cacheEntry );
125 * Put accumulated information into the cache, for later display.
126 * Page moves go on their own line.
128 * @param RCCacheEntry $cacheEntry
130 protected function addCacheEntry( RCCacheEntry
$cacheEntry ) {
131 $cacheGroupingKey = $this->makeCacheGroupingKey( $cacheEntry );
133 if ( !isset( $this->rc_cache
[$cacheGroupingKey] ) ) {
134 $this->rc_cache
[$cacheGroupingKey] = [];
137 array_push( $this->rc_cache
[$cacheGroupingKey], $cacheEntry );
141 * @todo use rc_source to group, if set; fallback to rc_type
143 * @param RCCacheEntry $cacheEntry
147 protected function makeCacheGroupingKey( RCCacheEntry
$cacheEntry ) {
148 $title = $cacheEntry->getTitle();
149 $cacheGroupingKey = $title->getPrefixedDBkey();
151 $type = $cacheEntry->mAttribs
['rc_type'];
153 if ( $type == RC_LOG
) {
155 $cacheGroupingKey = SpecialPage
::getTitleFor(
157 $cacheEntry->mAttribs
['rc_log_type']
158 )->getPrefixedDBkey();
161 return $cacheGroupingKey;
166 * @param RCCacheEntry[] $block
168 * @throws DomainException
170 protected function recentChangesBlockGroup( $block ) {
171 $recentChangesFlags = $this->getConfig()->get( 'RecentChangesFlags' );
173 # Add the namespace and title of the block as part of the class
174 $tableClasses = [ 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' ];
175 if ( $block[0]->mAttribs
['rc_log_type'] ) {
177 $tableClasses[] = Sanitizer
::escapeClass( 'mw-changeslist-log-'
178 . $block[0]->mAttribs
['rc_log_type'] );
180 $tableClasses[] = Sanitizer
::escapeClass( 'mw-changeslist-ns'
181 . $block[0]->mAttribs
['rc_namespace'] . '-' . $block[0]->mAttribs
['rc_title'] );
183 if ( $block[0]->watched
184 && $block[0]->mAttribs
['rc_timestamp'] >= $block[0]->watched
186 $tableClasses[] = 'mw-changeslist-line-watched';
188 $tableClasses[] = 'mw-changeslist-line-not-watched';
191 # Collate list of users
195 # Some catalyst variables...
198 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
200 # Default values for RC flags
201 $collectedRcFlags = [];
202 foreach ( $recentChangesFlags as $key => $value ) {
203 $flagGrouping = ( isset( $recentChangesFlags[$key]['grouping'] ) ?
204 $recentChangesFlags[$key]['grouping'] : 'any' );
205 switch ( $flagGrouping ) {
207 $collectedRcFlags[$key] = true;
210 $collectedRcFlags[$key] = false;
213 throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
216 foreach ( $block as $rcObj ) {
217 // If all log actions to this page were hidden, then don't
218 // give the name of the affected page for this block!
219 if ( !$this->isDeleted( $rcObj, LogPage
::DELETED_ACTION
) ) {
222 $u = $rcObj->userlink
;
223 if ( !isset( $userlinks[$u] ) ) {
226 if ( $rcObj->mAttribs
['rc_type'] != RC_LOG
) {
229 # Get the latest entry with a page_id and oldid
230 # since logs may not have these.
231 if ( !$curId && $rcObj->mAttribs
['rc_cur_id'] ) {
232 $curId = $rcObj->mAttribs
['rc_cur_id'];
238 # Sort the list and convert to text
239 krsort( $userlinks );
242 foreach ( $userlinks as $userlink => $count ) {
244 $text .= $this->getLanguage()->getDirMark();
246 $formattedCount = $this->msg( 'ntimes' )->numParams( $count )->escaped();
247 $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
249 array_push( $users, $text );
254 $revDeletedMsg = false;
256 $revDeletedMsg = $this->msg( 'rev-deleted-event' )->escaped();
257 } elseif ( $allLogs ) {
258 $articleLink = $this->maybeWatchedLink( $block[0]->link
, $block[0]->watched
);
260 $articleLink = $this->getArticleLink( $block[0], $block[0]->unpatrolled
, $block[0]->watched
);
263 $queryParams['curid'] = $curId;
267 foreach ( $block as $i => $rcObj ) {
268 $line = $this->getLineData( $block, $rcObj, $queryParams );
270 // completely ignore this RC entry if we don't want to render it
276 foreach ( $line['recentChangesFlagsRaw'] as $key => $value ) {
277 $flagGrouping = ( isset( $recentChangesFlags[$key]['grouping'] ) ?
278 $recentChangesFlags[$key]['grouping'] : 'any' );
279 switch ( $flagGrouping ) {
282 $collectedRcFlags[$key] = false;
287 $collectedRcFlags[$key] = true;
291 throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
298 // Further down are some assumptions that $block is a 0-indexed array
299 // with (count-1) as last key. Let's make sure it is.
300 $block = array_values( $block );
302 if ( empty( $block ) ||
!$lines ) {
303 // if we can't show anything, don't display this block altogether
307 $logText = $this->getLogText( $block, $queryParams, $allLogs,
308 $collectedRcFlags['newpage'], $namehidden
311 # Character difference (does not apply if only log items)
312 $charDifference = false;
313 if ( $RCShowChangedSize && !$allLogs ) {
315 $first = count( $block ) - 1;
316 # Some events (like logs and category changes) have an "empty" size, so we need to skip those...
317 while ( $last < $first && $block[$last]->mAttribs
['rc_new_len'] === null ) {
320 while ( $last < $first && $block[$first]->mAttribs
['rc_old_len'] === null ) {
324 $charDifference = $this->formatCharacterDifference( $block[$first], $block[$last] );
327 $numberofWatchingusers = $this->numberofWatchingusers( $block[0]->numberofWatchingusers
);
328 $usersList = $this->msg( 'brackets' )->rawParams(
329 implode( $this->message
['semicolon-separator'], $users )
333 'articleLink' => $articleLink,
334 'charDifference' => $charDifference,
335 'collectedRcFlags' => $this->recentChangesFlags( $collectedRcFlags ),
336 'languageDirMark' => $this->getLanguage()->getDirMark(),
338 'logText' => $logText,
339 'numberofWatchingusers' => $numberofWatchingusers,
340 'rev-deleted-event' => $revDeletedMsg,
341 'tableClasses' => $tableClasses,
342 'timestamp' => $block[0]->timestamp
,
343 'users' => $usersList,
346 $this->rcCacheIndex++
;
348 return $this->templateParser
->processTemplate(
349 'EnhancedChangesListGroup',
355 * @param RCCacheEntry[] $block
356 * @param RCCacheEntry $rcObj
357 * @param array $queryParams
361 * @throws MWException
363 protected function getLineData( array $block, RCCacheEntry
$rcObj, array $queryParams = [] ) {
364 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
366 $type = $rcObj->mAttribs
['rc_type'];
370 $classes = [ 'mw-enhanced-rc' ];
372 && $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
374 $classes[] = 'mw-enhanced-watched';
376 $classes = array_merge( $classes, $this->getHTMLClassesForFilters( $rcObj ) );
378 $separator = ' <span class="mw-changeslist-separator">. .</span> ';
380 $data['recentChangesFlags'] = [
381 'newpage' => $type == RC_NEW
,
382 'minor' => $rcObj->mAttribs
['rc_minor'],
383 'unpatrolled' => $rcObj->unpatrolled
,
384 'bot' => $rcObj->mAttribs
['rc_bot'],
387 $params = $queryParams;
389 if ( $rcObj->mAttribs
['rc_this_oldid'] != 0 ) {
390 $params['oldid'] = $rcObj->mAttribs
['rc_this_oldid'];
394 if ( $type == RC_LOG
) {
395 $link = $rcObj->timestamp
;
397 } elseif ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
398 $link = '<span class="history-deleted">' . $rcObj->timestamp
. '</span> ';
400 $link = $this->linkRenderer
->makeKnownLink(
402 new HtmlArmor( $rcObj->timestamp
),
406 if ( $this->isDeleted( $rcObj, Revision
::DELETED_TEXT
) ) {
407 $link = '<span class="history-deleted">' . $link . '</span> ';
410 $data['timestampLink'] = $link;
412 $currentAndLastLinks = '';
413 if ( !$type == RC_LOG ||
$type == RC_NEW
) {
414 $currentAndLastLinks .= ' ' . $this->msg( 'parentheses' )->rawParams(
416 $this->message
['pipe-separator'] .
420 $data['currentAndLastLinks'] = $currentAndLastLinks;
421 $data['separatorAfterCurrentAndLastLinks'] = $separator;
424 if ( $RCShowChangedSize ) {
425 $cd = $this->formatCharacterDifference( $rcObj );
427 $data['characterDiff'] = $cd;
428 $data['separatorAfterCharacterDiff'] = $separator;
432 if ( $rcObj->mAttribs
['rc_type'] == RC_LOG
) {
433 $data['logEntry'] = $this->insertLogEntry( $rcObj );
434 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
435 $data['comment'] = $this->insertComment( $rcObj );
438 $data['userLink'] = $rcObj->userlink
;
439 $data['userTalkLink'] = $rcObj->usertalklink
;
440 $data['comment'] = $this->insertComment( $rcObj );
444 $data['rollback'] = $this->getRollback( $rcObj );
447 $data['tags'] = $this->getTags( $rcObj, $classes );
449 $attribs = $this->getDataAttributes( $rcObj );
451 // give the hook a chance to modify the data
452 $success = Hooks
::run( 'EnhancedChangesListModifyLineData',
453 [ $this, &$data, $block, $rcObj, &$classes, &$attribs ] );
455 // skip entry if hook aborted it
458 $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer
::class, 'isReservedDataAttribute' ] );
460 $lineParams['recentChangesFlagsRaw'] = [];
461 if ( isset( $data['recentChangesFlags'] ) ) {
462 $lineParams['recentChangesFlags'] = $this->recentChangesFlags( $data['recentChangesFlags'] );
463 # FIXME: This is used by logic, don't return it in the template params.
464 $lineParams['recentChangesFlagsRaw'] = $data['recentChangesFlags'];
465 unset( $data['recentChangesFlags'] );
468 if ( isset( $data['timestampLink'] ) ) {
469 $lineParams['timestampLink'] = $data['timestampLink'];
470 unset( $data['timestampLink'] );
473 $lineParams['classes'] = array_values( $classes );
474 $lineParams['attribs'] = Html
::expandAttributes( $attribs );
476 // everything else: makes it easier for extensions to add or remove data
477 $lineParams['data'] = array_values( $data );
483 * Generates amount of changes (linking to diff ) & link to history.
485 * @param array $block
486 * @param array $queryParams
487 * @param bool $allLogs
489 * @param bool $namehidden
492 protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
493 if ( empty( $block ) ) {
498 static $nchanges = [];
499 static $sinceLastVisitMsg = [];
501 $n = count( $block );
502 if ( !isset( $nchanges[$n] ) ) {
503 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
507 $unvisitedOldid = null;
508 /** @var $rcObj RCCacheEntry */
509 foreach ( $block as $rcObj ) {
510 // Same logic as below inside main foreach
511 if ( $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
) {
513 $unvisitedOldid = $rcObj->mAttribs
['rc_last_oldid'];
516 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
517 $sinceLastVisitMsg[$sinceLast] =
518 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
521 $currentRevision = 0;
522 foreach ( $block as $rcObj ) {
523 if ( !$currentRevision ) {
524 $currentRevision = $rcObj->mAttribs
['rc_this_oldid'];
530 /** @var $block0 RecentChange */
532 $last = $block[count( $block ) - 1];
534 if ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ||
536 $rcObj->mAttribs
['rc_type'] == RC_CATEGORIZE
538 $links['total-changes'] = $nchanges[$n];
540 $links['total-changes'] = $this->linkRenderer
->makeKnownLink(
542 new HtmlArmor( $nchanges[$n] ),
543 [ 'class' => 'mw-changeslist-groupdiff' ],
545 'diff' => $currentRevision,
546 'oldid' => $last->mAttribs
['rc_last_oldid'],
549 if ( $sinceLast > 0 && $sinceLast < $n ) {
550 $links['total-changes-since-last'] = $this->linkRenderer
->makeKnownLink(
552 new HtmlArmor( $sinceLastVisitMsg[$sinceLast] ),
553 [ 'class' => 'mw-changeslist-groupdiff' ],
555 'diff' => $currentRevision,
556 'oldid' => $unvisitedOldid,
564 if ( $allLogs ||
$rcObj->mAttribs
['rc_type'] == RC_CATEGORIZE
) {
565 // don't show history link for logs
566 } elseif ( $namehidden ||
!$block0->getTitle()->exists() ) {
567 $links['history'] = $this->message
['enhancedrc-history'];
569 $params = $queryParams;
570 $params['action'] = 'history';
572 $links['history'] = $this->linkRenderer
->makeKnownLink(
574 new HtmlArmor( $this->message
['enhancedrc-history'] ),
575 [ 'class' => 'mw-changeslist-history' ],
580 # Allow others to alter, remove or add to these links
581 Hooks
::run( 'EnhancedChangesList::getLogText',
582 [ $this, &$links, $block ] );
588 $logtext = implode( $this->message
['pipe-separator'], $links );
589 $logtext = $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
590 return ' ' . $logtext;
594 * Enhanced RC ungrouped line.
596 * @param RecentChange|RCCacheEntry $rcObj
597 * @return string A HTML formatted line (generated using $r)
599 protected function recentChangesBlockLine( $rcObj ) {
602 $query['curid'] = $rcObj->mAttribs
['rc_cur_id'];
604 $type = $rcObj->mAttribs
['rc_type'];
605 $logType = $rcObj->mAttribs
['rc_log_type'];
606 $classes = $this->getHTMLClasses( $rcObj, $rcObj->watched
);
607 $classes[] = 'mw-enhanced-rc';
611 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-' . $logType );
613 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns' .
614 $rcObj->mAttribs
['rc_namespace'] . '-' . $rcObj->mAttribs
['rc_title'] );
618 $data['recentChangesFlags'] = [
619 'newpage' => $type == RC_NEW
,
620 'minor' => $rcObj->mAttribs
['rc_minor'],
621 'unpatrolled' => $rcObj->unpatrolled
,
622 'bot' => $rcObj->mAttribs
['rc_bot'],
624 // timestamp is not really a link here, but is called timestampLink
625 // for consistency with EnhancedChangesListModifyLineData
626 $data['timestampLink'] = $rcObj->timestamp
;
628 # Article or log link
630 $logPage = new LogPage( $logType );
631 $logTitle = SpecialPage
::getTitleFor( 'Log', $logType );
632 $logName = $logPage->getName()->text();
633 $data['logLink'] = $this->msg( 'parentheses' )
635 $this->linkRenderer
->makeKnownLink( $logTitle, $logName )
638 $data['articleLink'] = $this->getArticleLink( $rcObj, $rcObj->unpatrolled
, $rcObj->watched
);
641 # Diff and hist links
642 if ( $type != RC_LOG
&& $type != RC_CATEGORIZE
) {
643 $query['action'] = 'history';
644 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
646 $data['separatorAfterLinks'] = ' <span class="mw-changeslist-separator">. .</span> ';
649 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
650 $cd = $this->formatCharacterDifference( $rcObj );
652 $data['characterDiff'] = $cd;
653 $data['separatorAftercharacterDiff'] = ' <span class="mw-changeslist-separator">. .</span> ';
657 if ( $type == RC_LOG
) {
658 $data['logEntry'] = $this->insertLogEntry( $rcObj );
659 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
660 $data['comment'] = $this->insertComment( $rcObj );
662 $data['userLink'] = $rcObj->userlink
;
663 $data['userTalkLink'] = $rcObj->usertalklink
;
664 $data['comment'] = $this->insertComment( $rcObj );
665 if ( $type == RC_CATEGORIZE
) {
666 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
668 $data['rollback'] = $this->getRollback( $rcObj );
672 $data['tags'] = $this->getTags( $rcObj, $classes );
674 # Show how many people are watching this if enabled
675 $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers
);
677 $data['attribs'] = array_merge( $this->getDataAttributes( $rcObj ), [ 'class' => $classes ] );
679 // give the hook a chance to modify the data
680 $success = Hooks
::run( 'EnhancedChangesListModifyBlockLineData',
681 [ $this, &$data, $rcObj ] );
683 // skip entry if hook aborted it
686 $attribs = $data['attribs'];
687 unset( $data['attribs'] );
688 $attribs = wfArrayFilterByKey( $attribs, function ( $key ) {
689 return $key === 'class' || Sanitizer
::isReservedDataAttribute( $key );
692 $line = Html
::openElement( 'table', $attribs ) . Html
::openElement( 'tr' );
693 $line .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
695 if ( isset( $data['recentChangesFlags'] ) ) {
696 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
697 unset( $data['recentChangesFlags'] );
700 if ( isset( $data['timestampLink'] ) ) {
701 $line .= ' ' . $data['timestampLink'];
702 unset( $data['timestampLink'] );
704 $line .= ' </td><td>';
706 // everything else: makes it easier for extensions to add or remove data
707 $line .= implode( '', $data );
709 $line .= "</td></tr></table>\n";
715 * Returns value to be used in 'historyLink' element of $data param in
716 * EnhancedChangesListModifyBlockLineData hook.
720 * @param RCCacheEntry $rc
721 * @param array $query array of key/value pairs to append as a query string
722 * @return string HTML
724 public function getDiffHistLinks( RCCacheEntry
$rc, array $query ) {
725 $pageTitle = $rc->getTitle();
726 if ( $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
) {
727 // For categorizations we must swap the category title with the page title!
728 $pageTitle = Title
::newFromID( $rc->getAttribute( 'rc_cur_id' ) );
730 // The page has been deleted, but the RC entry
731 // deletion job has not run yet. Just skip.
736 $retVal = ' ' . $this->msg( 'parentheses' )
737 ->rawParams( $rc->difflink
. $this->message
['pipe-separator']
738 . $this->linkRenderer
->makeKnownLink(
740 new HtmlArmor( $this->message
['hist'] ),
741 [ 'class' => 'mw-changeslist-history' ],
748 * If enhanced RC is in use, this function takes the previously cached
749 * RC lines, arranges them, and outputs the HTML
753 protected function recentChangesBlock() {
754 if ( count( $this->rc_cache
) == 0 ) {
759 foreach ( $this->rc_cache
as $block ) {
760 if ( count( $block ) < 2 ) {
761 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
763 $blockOut .= $this->recentChangesBlockGroup( $block );
767 return '<div>' . $blockOut . '</div>';
771 * Returns text for the end of RC
772 * If enhanced RC is in use, returns pretty much all the text
775 public function endRecentChangesList() {
776 return $this->recentChangesBlock() . '</div>';