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 ) {
101 $date = $this->getLanguage()->userDate(
102 $rc->mAttribs
['rc_timestamp'],
108 # If it's a new day, add the headline and flush the cache
109 if ( $date != $this->lastdate
) {
110 # Process current cache
111 $ret = $this->recentChangesBlock();
112 $this->rc_cache
= [];
113 $ret .= Xml
::element( 'h4', null, $date ) . "\n";
114 $this->lastdate
= $date;
117 $cacheEntry = $this->cacheEntryFactory
->newFromRecentChange( $rc, $watched );
118 $this->addCacheEntry( $cacheEntry );
124 * Put accumulated information into the cache, for later display.
125 * Page moves go on their own line.
127 * @param RCCacheEntry $cacheEntry
129 protected function addCacheEntry( RCCacheEntry
$cacheEntry ) {
130 $cacheGroupingKey = $this->makeCacheGroupingKey( $cacheEntry );
132 if ( !isset( $this->rc_cache
[$cacheGroupingKey] ) ) {
133 $this->rc_cache
[$cacheGroupingKey] = [];
136 array_push( $this->rc_cache
[$cacheGroupingKey], $cacheEntry );
140 * @todo use rc_source to group, if set; fallback to rc_type
142 * @param RCCacheEntry $cacheEntry
146 protected function makeCacheGroupingKey( RCCacheEntry
$cacheEntry ) {
147 $title = $cacheEntry->getTitle();
148 $cacheGroupingKey = $title->getPrefixedDBkey();
150 $type = $cacheEntry->mAttribs
['rc_type'];
152 if ( $type == RC_LOG
) {
154 $cacheGroupingKey = SpecialPage
::getTitleFor(
156 $cacheEntry->mAttribs
['rc_log_type']
157 )->getPrefixedDBkey();
160 return $cacheGroupingKey;
165 * @param RCCacheEntry[] $block
167 * @throws DomainException
169 protected function recentChangesBlockGroup( $block ) {
170 $recentChangesFlags = $this->getConfig()->get( 'RecentChangesFlags' );
172 # Add the namespace and title of the block as part of the class
173 $tableClasses = [ 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' ];
174 if ( $block[0]->mAttribs
['rc_log_type'] ) {
176 $tableClasses[] = Sanitizer
::escapeClass( 'mw-changeslist-log-'
177 . $block[0]->mAttribs
['rc_log_type'] );
179 $tableClasses[] = Sanitizer
::escapeClass( 'mw-changeslist-ns'
180 . $block[0]->mAttribs
['rc_namespace'] . '-' . $block[0]->mAttribs
['rc_title'] );
182 if ( $block[0]->watched
183 && $block[0]->mAttribs
['rc_timestamp'] >= $block[0]->watched
185 $tableClasses[] = 'mw-changeslist-line-watched';
187 $tableClasses[] = 'mw-changeslist-line-not-watched';
190 # Collate list of users
194 # Some catalyst variables...
197 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
199 # Default values for RC flags
200 $collectedRcFlags = [];
201 foreach ( $recentChangesFlags as $key => $value ) {
202 $flagGrouping = ( isset( $recentChangesFlags[$key]['grouping'] ) ?
203 $recentChangesFlags[$key]['grouping'] : 'any' );
204 switch ( $flagGrouping ) {
206 $collectedRcFlags[$key] = true;
209 $collectedRcFlags[$key] = false;
212 throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
215 foreach ( $block as $rcObj ) {
216 // If all log actions to this page were hidden, then don't
217 // give the name of the affected page for this block!
218 if ( !$this->isDeleted( $rcObj, LogPage
::DELETED_ACTION
) ) {
221 $u = $rcObj->userlink
;
222 if ( !isset( $userlinks[$u] ) ) {
225 if ( $rcObj->mAttribs
['rc_type'] != RC_LOG
) {
228 # Get the latest entry with a page_id and oldid
229 # since logs may not have these.
230 if ( !$curId && $rcObj->mAttribs
['rc_cur_id'] ) {
231 $curId = $rcObj->mAttribs
['rc_cur_id'];
237 # Sort the list and convert to text
238 krsort( $userlinks );
241 foreach ( $userlinks as $userlink => $count ) {
243 $text .= $this->getLanguage()->getDirMark();
245 $formattedCount = $this->msg( 'ntimes' )->numParams( $count )->escaped();
246 $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
248 array_push( $users, $text );
253 $revDeletedMsg = false;
255 $revDeletedMsg = $this->msg( 'rev-deleted-event' )->escaped();
256 } elseif ( $allLogs ) {
257 $articleLink = $this->maybeWatchedLink( $block[0]->link
, $block[0]->watched
);
259 $articleLink = $this->getArticleLink( $block[0], $block[0]->unpatrolled
, $block[0]->watched
);
262 $queryParams['curid'] = $curId;
266 foreach ( $block as $i => $rcObj ) {
267 $line = $this->getLineData( $block, $rcObj, $queryParams );
269 // completely ignore this RC entry if we don't want to render it
275 foreach ( $line['recentChangesFlagsRaw'] as $key => $value ) {
276 $flagGrouping = ( isset( $recentChangesFlags[$key]['grouping'] ) ?
277 $recentChangesFlags[$key]['grouping'] : 'any' );
278 switch ( $flagGrouping ) {
281 $collectedRcFlags[$key] = false;
286 $collectedRcFlags[$key] = true;
290 throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
297 // Further down are some assumptions that $block is a 0-indexed array
298 // with (count-1) as last key. Let's make sure it is.
299 $block = array_values( $block );
301 if ( empty( $block ) ||
!$lines ) {
302 // if we can't show anything, don't display this block altogether
306 $logText = $this->getLogText( $block, $queryParams, $allLogs,
307 $collectedRcFlags['newpage'], $namehidden
310 # Character difference (does not apply if only log items)
311 $charDifference = false;
312 if ( $RCShowChangedSize && !$allLogs ) {
314 $first = count( $block ) - 1;
315 # Some events (like logs and category changes) have an "empty" size, so we need to skip those...
316 while ( $last < $first && $block[$last]->mAttribs
['rc_new_len'] === null ) {
319 while ( $last < $first && $block[$first]->mAttribs
['rc_old_len'] === null ) {
323 $charDifference = $this->formatCharacterDifference( $block[$first], $block[$last] );
326 $numberofWatchingusers = $this->numberofWatchingusers( $block[0]->numberofWatchingusers
);
327 $usersList = $this->msg( 'brackets' )->rawParams(
328 implode( $this->message
['semicolon-separator'], $users )
332 'articleLink' => $articleLink,
333 'charDifference' => $charDifference,
334 'collectedRcFlags' => $this->recentChangesFlags( $collectedRcFlags ),
335 'languageDirMark' => $this->getLanguage()->getDirMark(),
337 'logText' => $logText,
338 'numberofWatchingusers' => $numberofWatchingusers,
339 'rev-deleted-event' => $revDeletedMsg,
340 'tableClasses' => $tableClasses,
341 'timestamp' => $block[0]->timestamp
,
342 'users' => $usersList,
345 $this->rcCacheIndex++
;
347 return $this->templateParser
->processTemplate(
348 'EnhancedChangesListGroup',
354 * @param RCCacheEntry[] $block
355 * @param RCCacheEntry $rcObj
356 * @param array $queryParams
360 * @throws MWException
362 protected function getLineData( array $block, RCCacheEntry
$rcObj, array $queryParams = [] ) {
363 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
365 $type = $rcObj->mAttribs
['rc_type'];
369 $classes = [ 'mw-enhanced-rc' ];
371 && $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
373 $classes[] = 'mw-enhanced-watched';
375 $classes = array_merge( $classes, $this->getHTMLClassesForFilters( $rcObj ) );
377 $separator = ' <span class="mw-changeslist-separator">. .</span> ';
379 $data['recentChangesFlags'] = [
380 'newpage' => $type == RC_NEW
,
381 'minor' => $rcObj->mAttribs
['rc_minor'],
382 'unpatrolled' => $rcObj->unpatrolled
,
383 'bot' => $rcObj->mAttribs
['rc_bot'],
386 $params = $queryParams;
388 if ( $rcObj->mAttribs
['rc_this_oldid'] != 0 ) {
389 $params['oldid'] = $rcObj->mAttribs
['rc_this_oldid'];
393 if ( $type == RC_LOG
) {
394 $link = $rcObj->timestamp
;
396 } elseif ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
397 $link = '<span class="history-deleted">' . $rcObj->timestamp
. '</span> ';
399 $link = $this->linkRenderer
->makeKnownLink(
401 new HtmlArmor( $rcObj->timestamp
),
405 if ( $this->isDeleted( $rcObj, Revision
::DELETED_TEXT
) ) {
406 $link = '<span class="history-deleted">' . $link . '</span> ';
409 $data['timestampLink'] = $link;
411 $currentAndLastLinks = '';
412 if ( !$type == RC_LOG ||
$type == RC_NEW
) {
413 $currentAndLastLinks .= ' ' . $this->msg( 'parentheses' )->rawParams(
415 $this->message
['pipe-separator'] .
419 $data['currentAndLastLinks'] = $currentAndLastLinks;
420 $data['separatorAfterCurrentAndLastLinks'] = $separator;
423 if ( $RCShowChangedSize ) {
424 $cd = $this->formatCharacterDifference( $rcObj );
426 $data['characterDiff'] = $cd;
427 $data['separatorAfterCharacterDiff'] = $separator;
431 if ( $rcObj->mAttribs
['rc_type'] == RC_LOG
) {
432 $data['logEntry'] = $this->insertLogEntry( $rcObj );
433 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
434 $data['comment'] = $this->insertComment( $rcObj );
437 $data['userLink'] = $rcObj->userlink
;
438 $data['userTalkLink'] = $rcObj->usertalklink
;
439 $data['comment'] = $this->insertComment( $rcObj );
443 $data['rollback'] = $this->getRollback( $rcObj );
446 $data['tags'] = $this->getTags( $rcObj, $classes );
448 $attribs = $this->getDataAttributes( $rcObj );
450 // give the hook a chance to modify the data
451 $success = Hooks
::run( 'EnhancedChangesListModifyLineData',
452 [ $this, &$data, $block, $rcObj, &$classes, &$attribs ] );
454 // skip entry if hook aborted it
457 $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer
::class, 'isReservedDataAttribute' ] );
459 $lineParams['recentChangesFlagsRaw'] = [];
460 if ( isset( $data['recentChangesFlags'] ) ) {
461 $lineParams['recentChangesFlags'] = $this->recentChangesFlags( $data['recentChangesFlags'] );
462 # FIXME: This is used by logic, don't return it in the template params.
463 $lineParams['recentChangesFlagsRaw'] = $data['recentChangesFlags'];
464 unset( $data['recentChangesFlags'] );
467 if ( isset( $data['timestampLink'] ) ) {
468 $lineParams['timestampLink'] = $data['timestampLink'];
469 unset( $data['timestampLink'] );
472 $lineParams['classes'] = array_values( $classes );
473 $lineParams['attribs'] = Html
::expandAttributes( $attribs );
475 // everything else: makes it easier for extensions to add or remove data
476 $lineParams['data'] = array_values( $data );
482 * Generates amount of changes (linking to diff ) & link to history.
484 * @param array $block
485 * @param array $queryParams
486 * @param bool $allLogs
488 * @param bool $namehidden
491 protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
492 if ( empty( $block ) ) {
497 static $nchanges = [];
498 static $sinceLastVisitMsg = [];
500 $n = count( $block );
501 if ( !isset( $nchanges[$n] ) ) {
502 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
506 $unvisitedOldid = null;
507 /** @var $rcObj RCCacheEntry */
508 foreach ( $block as $rcObj ) {
509 // Same logic as below inside main foreach
510 if ( $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
) {
512 $unvisitedOldid = $rcObj->mAttribs
['rc_last_oldid'];
515 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
516 $sinceLastVisitMsg[$sinceLast] =
517 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
520 $currentRevision = 0;
521 foreach ( $block as $rcObj ) {
522 if ( !$currentRevision ) {
523 $currentRevision = $rcObj->mAttribs
['rc_this_oldid'];
529 /** @var $block0 RecentChange */
531 $last = $block[count( $block ) - 1];
533 if ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ||
535 $rcObj->mAttribs
['rc_type'] == RC_CATEGORIZE
537 $links['total-changes'] = $nchanges[$n];
539 $links['total-changes'] = $this->linkRenderer
->makeKnownLink(
541 new HtmlArmor( $nchanges[$n] ),
542 [ 'class' => 'mw-changeslist-groupdiff' ],
544 'diff' => $currentRevision,
545 'oldid' => $last->mAttribs
['rc_last_oldid'],
548 if ( $sinceLast > 0 && $sinceLast < $n ) {
549 $links['total-changes-since-last'] = $this->linkRenderer
->makeKnownLink(
551 new HtmlArmor( $sinceLastVisitMsg[$sinceLast] ),
552 [ 'class' => 'mw-changeslist-groupdiff' ],
554 'diff' => $currentRevision,
555 'oldid' => $unvisitedOldid,
563 if ( $allLogs ||
$rcObj->mAttribs
['rc_type'] == RC_CATEGORIZE
) {
564 // don't show history link for logs
565 } elseif ( $namehidden ||
!$block0->getTitle()->exists() ) {
566 $links['history'] = $this->message
['enhancedrc-history'];
568 $params = $queryParams;
569 $params['action'] = 'history';
571 $links['history'] = $this->linkRenderer
->makeKnownLink(
573 new HtmlArmor( $this->message
['enhancedrc-history'] ),
574 [ 'class' => 'mw-changeslist-history' ],
579 # Allow others to alter, remove or add to these links
580 Hooks
::run( 'EnhancedChangesList::getLogText',
581 [ $this, &$links, $block ] );
587 $logtext = implode( $this->message
['pipe-separator'], $links );
588 $logtext = $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
589 return ' ' . $logtext;
593 * Enhanced RC ungrouped line.
595 * @param RecentChange|RCCacheEntry $rcObj
596 * @return string A HTML formatted line (generated using $r)
598 protected function recentChangesBlockLine( $rcObj ) {
601 $query['curid'] = $rcObj->mAttribs
['rc_cur_id'];
603 $type = $rcObj->mAttribs
['rc_type'];
604 $logType = $rcObj->mAttribs
['rc_log_type'];
605 $classes = $this->getHTMLClasses( $rcObj, $rcObj->watched
);
606 $classes[] = 'mw-enhanced-rc';
610 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-' . $logType );
612 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns' .
613 $rcObj->mAttribs
['rc_namespace'] . '-' . $rcObj->mAttribs
['rc_title'] );
617 $data['recentChangesFlags'] = [
618 'newpage' => $type == RC_NEW
,
619 'minor' => $rcObj->mAttribs
['rc_minor'],
620 'unpatrolled' => $rcObj->unpatrolled
,
621 'bot' => $rcObj->mAttribs
['rc_bot'],
623 // timestamp is not really a link here, but is called timestampLink
624 // for consistency with EnhancedChangesListModifyLineData
625 $data['timestampLink'] = $rcObj->timestamp
;
627 # Article or log link
629 $logPage = new LogPage( $logType );
630 $logTitle = SpecialPage
::getTitleFor( 'Log', $logType );
631 $logName = $logPage->getName()->text();
632 $data['logLink'] = $this->msg( 'parentheses' )
634 $this->linkRenderer
->makeKnownLink( $logTitle, $logName )
637 $data['articleLink'] = $this->getArticleLink( $rcObj, $rcObj->unpatrolled
, $rcObj->watched
);
640 # Diff and hist links
641 if ( $type != RC_LOG
&& $type != RC_CATEGORIZE
) {
642 $query['action'] = 'history';
643 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
645 $data['separatorAfterLinks'] = ' <span class="mw-changeslist-separator">. .</span> ';
648 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
649 $cd = $this->formatCharacterDifference( $rcObj );
651 $data['characterDiff'] = $cd;
652 $data['separatorAftercharacterDiff'] = ' <span class="mw-changeslist-separator">. .</span> ';
656 if ( $type == RC_LOG
) {
657 $data['logEntry'] = $this->insertLogEntry( $rcObj );
658 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
659 $data['comment'] = $this->insertComment( $rcObj );
661 $data['userLink'] = $rcObj->userlink
;
662 $data['userTalkLink'] = $rcObj->usertalklink
;
663 $data['comment'] = $this->insertComment( $rcObj );
664 if ( $type == RC_CATEGORIZE
) {
665 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
667 $data['rollback'] = $this->getRollback( $rcObj );
671 $data['tags'] = $this->getTags( $rcObj, $classes );
673 # Show how many people are watching this if enabled
674 $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers
);
676 $data['attribs'] = array_merge( $this->getDataAttributes( $rcObj ), [ 'class' => $classes ] );
678 // give the hook a chance to modify the data
679 $success = Hooks
::run( 'EnhancedChangesListModifyBlockLineData',
680 [ $this, &$data, $rcObj ] );
682 // skip entry if hook aborted it
685 $attribs = $data['attribs'];
686 unset( $data['attribs'] );
687 $attribs = wfArrayFilterByKey( $attribs, function ( $key ) {
688 return $key === 'class' || Sanitizer
::isReservedDataAttribute( $key );
691 $line = Html
::openElement( 'table', $attribs ) . Html
::openElement( 'tr' );
692 $line .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
694 if ( isset( $data['recentChangesFlags'] ) ) {
695 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
696 unset( $data['recentChangesFlags'] );
699 if ( isset( $data['timestampLink'] ) ) {
700 $line .= ' ' . $data['timestampLink'];
701 unset( $data['timestampLink'] );
703 $line .= ' </td><td>';
705 // everything else: makes it easier for extensions to add or remove data
706 $line .= implode( '', $data );
708 $line .= "</td></tr></table>\n";
714 * Returns value to be used in 'historyLink' element of $data param in
715 * EnhancedChangesListModifyBlockLineData hook.
719 * @param RCCacheEntry $rc
720 * @param array $query array of key/value pairs to append as a query string
721 * @return string HTML
723 public function getDiffHistLinks( RCCacheEntry
$rc, array $query ) {
724 $pageTitle = $rc->getTitle();
725 if ( $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
) {
726 // For categorizations we must swap the category title with the page title!
727 $pageTitle = Title
::newFromID( $rc->getAttribute( 'rc_cur_id' ) );
729 // The page has been deleted, but the RC entry
730 // deletion job has not run yet. Just skip.
735 $retVal = ' ' . $this->msg( 'parentheses' )
736 ->rawParams( $rc->difflink
. $this->message
['pipe-separator']
737 . $this->linkRenderer
->makeKnownLink(
739 new HtmlArmor( $this->message
['hist'] ),
740 [ 'class' => 'mw-changeslist-history' ],
747 * If enhanced RC is in use, this function takes the previously cached
748 * RC lines, arranges them, and outputs the HTML
752 protected function recentChangesBlock() {
753 if ( count( $this->rc_cache
) == 0 ) {
758 foreach ( $this->rc_cache
as $block ) {
759 if ( count( $block ) < 2 ) {
760 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
762 $blockOut .= $this->recentChangesBlockGroup( $block );
766 return '<div>' . $blockOut . '</div>';
770 * Returns text for the end of RC
771 * If enhanced RC is in use, returns pretty much all the text
774 public function endRecentChangesList() {
775 return $this->recentChangesBlock() . '</div>';