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 // @todo FIXME: Hardcoded '×'. Should be a message.
247 $formattedCount = $this->getLanguage()->formatNum( $count ) . '×';
248 $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
250 array_push( $users, $text );
255 $revDeletedMsg = false;
257 $revDeletedMsg = $this->msg( 'rev-deleted-event' )->escaped();
258 } elseif ( $allLogs ) {
259 $articleLink = $this->maybeWatchedLink( $block[0]->link
, $block[0]->watched
);
261 $articleLink = $this->getArticleLink( $block[0], $block[0]->unpatrolled
, $block[0]->watched
);
264 $queryParams['curid'] = $curId;
268 foreach ( $block as $i => $rcObj ) {
269 $line = $this->getLineData( $block, $rcObj, $queryParams );
271 // completely ignore this RC entry if we don't want to render it
277 foreach ( $line['recentChangesFlagsRaw'] as $key => $value ) {
278 $flagGrouping = ( isset( $recentChangesFlags[$key]['grouping'] ) ?
279 $recentChangesFlags[$key]['grouping'] : 'any' );
280 switch ( $flagGrouping ) {
283 $collectedRcFlags[$key] = false;
288 $collectedRcFlags[$key] = true;
292 throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
299 // Further down are some assumptions that $block is a 0-indexed array
300 // with (count-1) as last key. Let's make sure it is.
301 $block = array_values( $block );
303 if ( empty( $block ) ||
!$lines ) {
304 // if we can't show anything, don't display this block altogether
308 $logText = $this->getLogText( $block, $queryParams, $allLogs,
309 $collectedRcFlags['newpage'], $namehidden
312 # Character difference (does not apply if only log items)
313 $charDifference = false;
314 if ( $RCShowChangedSize && !$allLogs ) {
316 $first = count( $block ) - 1;
317 # Some events (like logs and category changes) have an "empty" size, so we need to skip those...
318 while ( $last < $first && $block[$last]->mAttribs
['rc_new_len'] === null ) {
321 while ( $last < $first && $block[$first]->mAttribs
['rc_old_len'] === null ) {
325 $charDifference = $this->formatCharacterDifference( $block[$first], $block[$last] );
328 $numberofWatchingusers = $this->numberofWatchingusers( $block[0]->numberofWatchingusers
);
329 $usersList = $this->msg( 'brackets' )->rawParams(
330 implode( $this->message
['semicolon-separator'], $users )
334 'articleLink' => $articleLink,
335 'charDifference' => $charDifference,
336 'collectedRcFlags' => $this->recentChangesFlags( $collectedRcFlags ),
337 'languageDirMark' => $this->getLanguage()->getDirMark(),
339 'logText' => $logText,
340 'numberofWatchingusers' => $numberofWatchingusers,
341 'rev-deleted-event' => $revDeletedMsg,
342 'tableClasses' => $tableClasses,
343 'timestamp' => $block[0]->timestamp
,
344 'users' => $usersList,
347 $this->rcCacheIndex++
;
349 return $this->templateParser
->processTemplate(
350 'EnhancedChangesListGroup',
356 * @param RCCacheEntry[] $block
357 * @param RCCacheEntry $rcObj
358 * @param array $queryParams
362 * @throws MWException
364 protected function getLineData( array $block, RCCacheEntry
$rcObj, array $queryParams = [] ) {
365 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
367 $type = $rcObj->mAttribs
['rc_type'];
371 $classes = [ 'mw-enhanced-rc' ];
373 && $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
375 $classes[] = 'mw-enhanced-watched';
377 $classes = array_merge( $classes, $this->getHTMLClassesForFilters( $rcObj ) );
379 $separator = ' <span class="mw-changeslist-separator">. .</span> ';
381 $data['recentChangesFlags'] = [
382 'newpage' => $type == RC_NEW
,
383 'minor' => $rcObj->mAttribs
['rc_minor'],
384 'unpatrolled' => $rcObj->unpatrolled
,
385 'bot' => $rcObj->mAttribs
['rc_bot'],
388 $params = $queryParams;
390 if ( $rcObj->mAttribs
['rc_this_oldid'] != 0 ) {
391 $params['oldid'] = $rcObj->mAttribs
['rc_this_oldid'];
395 if ( $type == RC_LOG
) {
396 $link = $rcObj->timestamp
;
398 } elseif ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
399 $link = '<span class="history-deleted">' . $rcObj->timestamp
. '</span> ';
401 $link = $this->linkRenderer
->makeKnownLink(
403 new HtmlArmor( $rcObj->timestamp
),
407 if ( $this->isDeleted( $rcObj, Revision
::DELETED_TEXT
) ) {
408 $link = '<span class="history-deleted">' . $link . '</span> ';
411 $data['timestampLink'] = $link;
413 $currentAndLastLinks = '';
414 if ( !$type == RC_LOG ||
$type == RC_NEW
) {
415 $currentAndLastLinks .= ' ' . $this->msg( 'parentheses' )->rawParams(
417 $this->message
['pipe-separator'] .
421 $data['currentAndLastLinks'] = $currentAndLastLinks;
422 $data['separatorAfterCurrentAndLastLinks'] = $separator;
425 if ( $RCShowChangedSize ) {
426 $cd = $this->formatCharacterDifference( $rcObj );
428 $data['characterDiff'] = $cd;
429 $data['separatorAfterCharacterDiff'] = $separator;
433 if ( $rcObj->mAttribs
['rc_type'] == RC_LOG
) {
434 $data['logEntry'] = $this->insertLogEntry( $rcObj );
435 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
436 $data['comment'] = $this->insertComment( $rcObj );
439 $data['userLink'] = $rcObj->userlink
;
440 $data['userTalkLink'] = $rcObj->usertalklink
;
441 $data['comment'] = $this->insertComment( $rcObj );
445 $data['rollback'] = $this->getRollback( $rcObj );
448 $data['tags'] = $this->getTags( $rcObj, $classes );
450 // give the hook a chance to modify the data
451 $success = Hooks
::run( 'EnhancedChangesListModifyLineData',
452 [ $this, &$data, $block, $rcObj, &$classes ] );
454 // skip entry if hook aborted it
458 $lineParams['recentChangesFlagsRaw'] = [];
459 if ( isset( $data['recentChangesFlags'] ) ) {
460 $lineParams['recentChangesFlags'] = $this->recentChangesFlags( $data['recentChangesFlags'] );
461 # FIXME: This is used by logic, don't return it in the template params.
462 $lineParams['recentChangesFlagsRaw'] = $data['recentChangesFlags'];
463 unset( $data['recentChangesFlags'] );
466 if ( isset( $data['timestampLink'] ) ) {
467 $lineParams['timestampLink'] = $data['timestampLink'];
468 unset( $data['timestampLink'] );
471 $lineParams['classes'] = array_values( $classes );
473 // everything else: makes it easier for extensions to add or remove data
474 $lineParams['data'] = array_values( $data );
480 * Generates amount of changes (linking to diff ) & link to history.
482 * @param array $block
483 * @param array $queryParams
484 * @param bool $allLogs
486 * @param bool $namehidden
489 protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
490 if ( empty( $block ) ) {
495 static $nchanges = [];
496 static $sinceLastVisitMsg = [];
498 $n = count( $block );
499 if ( !isset( $nchanges[$n] ) ) {
500 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
504 $unvisitedOldid = null;
505 /** @var $rcObj RCCacheEntry */
506 foreach ( $block as $rcObj ) {
507 // Same logic as below inside main foreach
508 if ( $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
) {
510 $unvisitedOldid = $rcObj->mAttribs
['rc_last_oldid'];
513 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
514 $sinceLastVisitMsg[$sinceLast] =
515 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
518 $currentRevision = 0;
519 foreach ( $block as $rcObj ) {
520 if ( !$currentRevision ) {
521 $currentRevision = $rcObj->mAttribs
['rc_this_oldid'];
527 /** @var $block0 RecentChange */
529 $last = $block[count( $block ) - 1];
531 if ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ||
533 $rcObj->mAttribs
['rc_type'] == RC_CATEGORIZE
535 $links['total-changes'] = $nchanges[$n];
537 $links['total-changes'] = $this->linkRenderer
->makeKnownLink(
539 new HtmlArmor( $nchanges[$n] ),
540 [ 'class' => 'mw-changeslist-groupdiff' ],
542 'diff' => $currentRevision,
543 'oldid' => $last->mAttribs
['rc_last_oldid'],
546 if ( $sinceLast > 0 && $sinceLast < $n ) {
547 $links['total-changes-since-last'] = $this->linkRenderer
->makeKnownLink(
549 new HtmlArmor( $sinceLastVisitMsg[$sinceLast] ),
550 [ 'class' => 'mw-changeslist-groupdiff' ],
552 'diff' => $currentRevision,
553 'oldid' => $unvisitedOldid,
561 if ( $allLogs ||
$rcObj->mAttribs
['rc_type'] == RC_CATEGORIZE
) {
562 // don't show history link for logs
563 } elseif ( $namehidden ||
!$block0->getTitle()->exists() ) {
564 $links['history'] = $this->message
['enhancedrc-history'];
566 $params = $queryParams;
567 $params['action'] = 'history';
569 $links['history'] = $this->linkRenderer
->makeKnownLink(
571 new HtmlArmor( $this->message
['enhancedrc-history'] ),
572 [ 'class' => 'mw-changeslist-history' ],
577 # Allow others to alter, remove or add to these links
578 Hooks
::run( 'EnhancedChangesList::getLogText',
579 [ $this, &$links, $block ] );
585 $logtext = implode( $this->message
['pipe-separator'], $links );
586 $logtext = $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
587 return ' ' . $logtext;
591 * Enhanced RC ungrouped line.
593 * @param RecentChange|RCCacheEntry $rcObj
594 * @return string A HTML formatted line (generated using $r)
596 protected function recentChangesBlockLine( $rcObj ) {
599 $query['curid'] = $rcObj->mAttribs
['rc_cur_id'];
601 $type = $rcObj->mAttribs
['rc_type'];
602 $logType = $rcObj->mAttribs
['rc_log_type'];
603 $classes = $this->getHTMLClasses( $rcObj, $rcObj->watched
);
604 $classes[] = 'mw-enhanced-rc';
608 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-' . $logType );
610 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns' .
611 $rcObj->mAttribs
['rc_namespace'] . '-' . $rcObj->mAttribs
['rc_title'] );
615 $data['recentChangesFlags'] = [
616 'newpage' => $type == RC_NEW
,
617 'minor' => $rcObj->mAttribs
['rc_minor'],
618 'unpatrolled' => $rcObj->unpatrolled
,
619 'bot' => $rcObj->mAttribs
['rc_bot'],
621 // timestamp is not really a link here, but is called timestampLink
622 // for consistency with EnhancedChangesListModifyLineData
623 $data['timestampLink'] = $rcObj->timestamp
;
625 # Article or log link
627 $logPage = new LogPage( $logType );
628 $logTitle = SpecialPage
::getTitleFor( 'Log', $logType );
629 $logName = $logPage->getName()->text();
630 $data['logLink'] = $this->msg( 'parentheses' )
632 $this->linkRenderer
->makeKnownLink( $logTitle, $logName )
635 $data['articleLink'] = $this->getArticleLink( $rcObj, $rcObj->unpatrolled
, $rcObj->watched
);
638 # Diff and hist links
639 if ( $type != RC_LOG
&& $type != RC_CATEGORIZE
) {
640 $query['action'] = 'history';
641 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
643 $data['separatorAfterLinks'] = ' <span class="mw-changeslist-separator">. .</span> ';
646 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
647 $cd = $this->formatCharacterDifference( $rcObj );
649 $data['characterDiff'] = $cd;
650 $data['separatorAftercharacterDiff'] = ' <span class="mw-changeslist-separator">. .</span> ';
654 if ( $type == RC_LOG
) {
655 $data['logEntry'] = $this->insertLogEntry( $rcObj );
656 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
657 $data['comment'] = $this->insertComment( $rcObj );
659 $data['userLink'] = $rcObj->userlink
;
660 $data['userTalkLink'] = $rcObj->usertalklink
;
661 $data['comment'] = $this->insertComment( $rcObj );
662 if ( $type == RC_CATEGORIZE
) {
663 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
665 $data['rollback'] = $this->getRollback( $rcObj );
669 $data['tags'] = $this->getTags( $rcObj, $classes );
671 # Show how many people are watching this if enabled
672 $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers
);
674 // give the hook a chance to modify the data
675 $success = Hooks
::run( 'EnhancedChangesListModifyBlockLineData',
676 [ $this, &$data, $rcObj ] );
678 // skip entry if hook aborted it
682 $line = Html
::openElement( 'table', [ 'class' => $classes ] ) .
683 Html
::openElement( 'tr' );
684 $line .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
686 if ( isset( $data['recentChangesFlags'] ) ) {
687 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
688 unset( $data['recentChangesFlags'] );
691 if ( isset( $data['timestampLink'] ) ) {
692 $line .= ' ' . $data['timestampLink'];
693 unset( $data['timestampLink'] );
695 $line .= ' </td><td>';
697 // everything else: makes it easier for extensions to add or remove data
698 $line .= implode( '', $data );
700 $line .= "</td></tr></table>\n";
706 * Returns value to be used in 'historyLink' element of $data param in
707 * EnhancedChangesListModifyBlockLineData hook.
711 * @param RCCacheEntry $rc
712 * @param array $query array of key/value pairs to append as a query string
713 * @return string HTML
715 public function getDiffHistLinks( RCCacheEntry
$rc, array $query ) {
716 $pageTitle = $rc->getTitle();
717 if ( $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
) {
718 // For categorizations we must swap the category title with the page title!
719 $pageTitle = Title
::newFromID( $rc->getAttribute( 'rc_cur_id' ) );
721 // The page has been deleted, but the RC entry
722 // deletion job has not run yet. Just skip.
727 $retVal = ' ' . $this->msg( 'parentheses' )
728 ->rawParams( $rc->difflink
. $this->message
['pipe-separator']
729 . $this->linkRenderer
->makeKnownLink(
731 new HtmlArmor( $this->message
['hist'] ),
732 [ 'class' => 'mw-changeslist-history' ],
739 * If enhanced RC is in use, this function takes the previously cached
740 * RC lines, arranges them, and outputs the HTML
744 protected function recentChangesBlock() {
745 if ( count( $this->rc_cache
) == 0 ) {
750 foreach ( $this->rc_cache
as $block ) {
751 if ( count( $block ) < 2 ) {
752 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
754 $blockOut .= $this->recentChangesBlockGroup( $block );
758 return '<div>' . $blockOut . '</div>';
762 * Returns text for the end of RC
763 * If enhanced RC is in use, returns pretty much all the text
766 public function endRecentChangesList() {
767 return $this->recentChangesBlock() . '</div>';