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
36 * @param IContextSource|Skin $obj
37 * @param array $filterGroups Array of ChangesListFilterGroup objects (currently optional)
40 public function __construct( $obj, array $filterGroups = [] ) {
41 if ( $obj instanceof Skin
) {
42 // @todo: deprecate constructing with Skin
43 $context = $obj->getContext();
45 if ( !$obj instanceof IContextSource
) {
46 throw new MWException( 'EnhancedChangesList must be constructed with a '
47 . 'context source or skin.' );
53 parent
::__construct( $context, $filterGroups );
55 // message is set by the parent ChangesList class
56 $this->cacheEntryFactory
= new RCCacheEntryFactory(
64 * Add the JavaScript file for enhanced changeslist
67 public function beginRecentChangesList() {
69 $this->rcMoveIndex
= 0;
70 $this->rcCacheIndex
= 0;
72 $this->rclistOpen
= false;
73 $this->getOutput()->addModuleStyles( [
74 'mediawiki.special.changeslist',
75 'mediawiki.special.changeslist.enhanced',
77 $this->getOutput()->addModules( [
78 'jquery.makeCollapsible',
82 return '<div class="mw-changeslist">';
86 * Format a line for enhanced recentchange (aka with javascript and block of lines).
88 * @param RecentChange $rc
89 * @param bool $watched
90 * @param int $linenumber (default null)
94 public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
96 $date = $this->getLanguage()->userDate(
97 $rc->mAttribs
['rc_timestamp'],
103 # If it's a new day, add the headline and flush the cache
104 if ( $date != $this->lastdate
) {
105 # Process current cache
106 $ret = $this->recentChangesBlock();
107 $this->rc_cache
= [];
108 $ret .= Xml
::element( 'h4', null, $date ) . "\n";
109 $this->lastdate
= $date;
112 $cacheEntry = $this->cacheEntryFactory
->newFromRecentChange( $rc, $watched );
113 $this->addCacheEntry( $cacheEntry );
119 * Put accumulated information into the cache, for later display.
120 * Page moves go on their own line.
122 * @param RCCacheEntry $cacheEntry
124 protected function addCacheEntry( RCCacheEntry
$cacheEntry ) {
125 $cacheGroupingKey = $this->makeCacheGroupingKey( $cacheEntry );
127 if ( !isset( $this->rc_cache
[$cacheGroupingKey] ) ) {
128 $this->rc_cache
[$cacheGroupingKey] = [];
131 array_push( $this->rc_cache
[$cacheGroupingKey], $cacheEntry );
135 * @todo use rc_source to group, if set; fallback to rc_type
137 * @param RCCacheEntry $cacheEntry
141 protected function makeCacheGroupingKey( RCCacheEntry
$cacheEntry ) {
142 $title = $cacheEntry->getTitle();
143 $cacheGroupingKey = $title->getPrefixedDBkey();
145 $type = $cacheEntry->mAttribs
['rc_type'];
147 if ( $type == RC_LOG
) {
149 $cacheGroupingKey = SpecialPage
::getTitleFor(
151 $cacheEntry->mAttribs
['rc_log_type']
152 )->getPrefixedDBkey();
155 return $cacheGroupingKey;
160 * @param RCCacheEntry[] $block
162 * @throws DomainException
164 protected function recentChangesBlockGroup( $block ) {
165 $recentChangesFlags = $this->getConfig()->get( 'RecentChangesFlags' );
167 # Add the namespace and title of the block as part of the class
168 $tableClasses = [ 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' ];
169 if ( $block[0]->mAttribs
['rc_log_type'] ) {
171 $tableClasses[] = Sanitizer
::escapeClass( 'mw-changeslist-log-'
172 . $block[0]->mAttribs
['rc_log_type'] );
174 $tableClasses[] = Sanitizer
::escapeClass( 'mw-changeslist-ns'
175 . $block[0]->mAttribs
['rc_namespace'] . '-' . $block[0]->mAttribs
['rc_title'] );
177 if ( $block[0]->watched
178 && $block[0]->mAttribs
['rc_timestamp'] >= $block[0]->watched
180 $tableClasses[] = 'mw-changeslist-line-watched';
182 $tableClasses[] = 'mw-changeslist-line-not-watched';
185 # Collate list of users
189 # Some catalyst variables...
192 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
194 # Default values for RC flags
195 $collectedRcFlags = [];
196 foreach ( $recentChangesFlags as $key => $value ) {
197 $flagGrouping = ( isset( $recentChangesFlags[$key]['grouping'] ) ?
198 $recentChangesFlags[$key]['grouping'] : 'any' );
199 switch ( $flagGrouping ) {
201 $collectedRcFlags[$key] = true;
204 $collectedRcFlags[$key] = false;
207 throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
210 foreach ( $block as $rcObj ) {
211 // If all log actions to this page were hidden, then don't
212 // give the name of the affected page for this block!
213 if ( !$this->isDeleted( $rcObj, LogPage
::DELETED_ACTION
) ) {
216 $u = $rcObj->userlink
;
217 if ( !isset( $userlinks[$u] ) ) {
220 if ( $rcObj->mAttribs
['rc_type'] != RC_LOG
) {
223 # Get the latest entry with a page_id and oldid
224 # since logs may not have these.
225 if ( !$curId && $rcObj->mAttribs
['rc_cur_id'] ) {
226 $curId = $rcObj->mAttribs
['rc_cur_id'];
232 # Sort the list and convert to text
233 krsort( $userlinks );
236 foreach ( $userlinks as $userlink => $count ) {
238 $text .= $this->getLanguage()->getDirMark();
240 // @todo FIXME: Hardcoded '×'. Should be a message.
241 $formattedCount = $this->getLanguage()->formatNum( $count ) . '×';
242 $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
244 array_push( $users, $text );
249 $revDeletedMsg = false;
251 $revDeletedMsg = $this->msg( 'rev-deleted-event' )->escaped();
252 } elseif ( $allLogs ) {
253 $articleLink = $this->maybeWatchedLink( $block[0]->link
, $block[0]->watched
);
255 $articleLink = $this->getArticleLink( $block[0], $block[0]->unpatrolled
, $block[0]->watched
);
258 $queryParams['curid'] = $curId;
262 foreach ( $block as $i => $rcObj ) {
263 $line = $this->getLineData( $block, $rcObj, $queryParams );
265 // completely ignore this RC entry if we don't want to render it
271 foreach ( $line['recentChangesFlagsRaw'] as $key => $value ) {
272 $flagGrouping = ( isset( $recentChangesFlags[$key]['grouping'] ) ?
273 $recentChangesFlags[$key]['grouping'] : 'any' );
274 switch ( $flagGrouping ) {
277 $collectedRcFlags[$key] = false;
282 $collectedRcFlags[$key] = true;
286 throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
293 // Further down are some assumptions that $block is a 0-indexed array
294 // with (count-1) as last key. Let's make sure it is.
295 $block = array_values( $block );
297 if ( empty( $block ) ||
!$lines ) {
298 // if we can't show anything, don't display this block altogether
302 $logText = $this->getLogText( $block, $queryParams, $allLogs,
303 $collectedRcFlags['newpage'], $namehidden
306 # Character difference (does not apply if only log items)
307 $charDifference = false;
308 if ( $RCShowChangedSize && !$allLogs ) {
310 $first = count( $block ) - 1;
311 # Some events (like logs and category changes) have an "empty" size, so we need to skip those...
312 while ( $last < $first && $block[$last]->mAttribs
['rc_new_len'] === null ) {
315 while ( $last < $first && $block[$first]->mAttribs
['rc_old_len'] === null ) {
319 $charDifference = $this->formatCharacterDifference( $block[$first], $block[$last] );
322 $numberofWatchingusers = $this->numberofWatchingusers( $block[0]->numberofWatchingusers
);
323 $usersList = $this->msg( 'brackets' )->rawParams(
324 implode( $this->message
['semicolon-separator'], $users )
328 'articleLink' => $articleLink,
329 'charDifference' => $charDifference,
330 'collectedRcFlags' => $this->recentChangesFlags( $collectedRcFlags ),
331 'languageDirMark' => $this->getLanguage()->getDirMark(),
333 'logText' => $logText,
334 'numberofWatchingusers' => $numberofWatchingusers,
335 'rev-deleted-event' => $revDeletedMsg,
336 'tableClasses' => $tableClasses,
337 'timestamp' => $block[0]->timestamp
,
338 'users' => $usersList,
341 $this->rcCacheIndex++
;
343 $templateParser = new TemplateParser();
344 return $templateParser->processTemplate(
345 'EnhancedChangesListGroup',
351 * @param RCCacheEntry[] $block
352 * @param RCCacheEntry $rcObj
353 * @param array $queryParams
357 * @throws MWException
359 protected function getLineData( array $block, RCCacheEntry
$rcObj, array $queryParams = [] ) {
360 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
362 $type = $rcObj->mAttribs
['rc_type'];
366 $classes = [ 'mw-enhanced-rc' ];
368 && $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
370 $classes[] = 'mw-enhanced-watched';
372 $classes = array_merge( $classes, $this->getHTMLClassesForFilters( $rcObj ) );
374 $separator = ' <span class="mw-changeslist-separator">. .</span> ';
376 $data['recentChangesFlags'] = [
377 'newpage' => $type == RC_NEW
,
378 'minor' => $rcObj->mAttribs
['rc_minor'],
379 'unpatrolled' => $rcObj->unpatrolled
,
380 'bot' => $rcObj->mAttribs
['rc_bot'],
383 $params = $queryParams;
385 if ( $rcObj->mAttribs
['rc_this_oldid'] != 0 ) {
386 $params['oldid'] = $rcObj->mAttribs
['rc_this_oldid'];
390 if ( $type == RC_LOG
) {
391 $link = $rcObj->timestamp
;
393 } elseif ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
394 $link = '<span class="history-deleted">' . $rcObj->timestamp
. '</span> ';
396 $link = $this->linkRenderer
->makeKnownLink(
398 new HtmlArmor( $rcObj->timestamp
),
402 if ( $this->isDeleted( $rcObj, Revision
::DELETED_TEXT
) ) {
403 $link = '<span class="history-deleted">' . $link . '</span> ';
406 $data['timestampLink'] = $link;
408 $currentAndLastLinks = '';
409 if ( !$type == RC_LOG ||
$type == RC_NEW
) {
410 $currentAndLastLinks .= ' ' . $this->msg( 'parentheses' )->rawParams(
412 $this->message
['pipe-separator'] .
416 $data['currentAndLastLinks'] = $currentAndLastLinks;
417 $data['separatorAfterCurrentAndLastLinks'] = $separator;
420 if ( $RCShowChangedSize ) {
421 $cd = $this->formatCharacterDifference( $rcObj );
423 $data['characterDiff'] = $cd;
424 $data['separatorAfterCharacterDiff'] = $separator;
428 if ( $rcObj->mAttribs
['rc_type'] == RC_LOG
) {
429 $data['logEntry'] = $this->insertLogEntry( $rcObj );
430 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
431 $data['comment'] = $this->insertComment( $rcObj );
434 $data['userLink'] = $rcObj->userlink
;
435 $data['userTalkLink'] = $rcObj->usertalklink
;
436 $data['comment'] = $this->insertComment( $rcObj );
440 $data['rollback'] = $this->getRollback( $rcObj );
443 $data['tags'] = $this->getTags( $rcObj, $classes );
445 // give the hook a chance to modify the data
446 $success = Hooks
::run( 'EnhancedChangesListModifyLineData',
447 [ $this, &$data, $block, $rcObj, &$classes ] );
449 // skip entry if hook aborted it
453 $lineParams['recentChangesFlagsRaw'] = [];
454 if ( isset( $data['recentChangesFlags'] ) ) {
455 $lineParams['recentChangesFlags'] = $this->recentChangesFlags( $data['recentChangesFlags'] );
456 # FIXME: This is used by logic, don't return it in the template params.
457 $lineParams['recentChangesFlagsRaw'] = $data['recentChangesFlags'];
458 unset( $data['recentChangesFlags'] );
461 if ( isset( $data['timestampLink'] ) ) {
462 $lineParams['timestampLink'] = $data['timestampLink'];
463 unset( $data['timestampLink'] );
466 $lineParams['classes'] = array_values( $classes );
468 // everything else: makes it easier for extensions to add or remove data
469 $lineParams['data'] = array_values( $data );
475 * Generates amount of changes (linking to diff ) & link to history.
477 * @param array $block
478 * @param array $queryParams
479 * @param bool $allLogs
481 * @param bool $namehidden
484 protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
485 if ( empty( $block ) ) {
490 static $nchanges = [];
491 static $sinceLastVisitMsg = [];
493 $n = count( $block );
494 if ( !isset( $nchanges[$n] ) ) {
495 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
499 $unvisitedOldid = null;
500 /** @var $rcObj RCCacheEntry */
501 foreach ( $block as $rcObj ) {
502 // Same logic as below inside main foreach
503 if ( $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
) {
505 $unvisitedOldid = $rcObj->mAttribs
['rc_last_oldid'];
508 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
509 $sinceLastVisitMsg[$sinceLast] =
510 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
513 $currentRevision = 0;
514 foreach ( $block as $rcObj ) {
515 if ( !$currentRevision ) {
516 $currentRevision = $rcObj->mAttribs
['rc_this_oldid'];
522 /** @var $block0 RecentChange */
524 $last = $block[count( $block ) - 1];
526 if ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ||
528 $rcObj->mAttribs
['rc_type'] == RC_CATEGORIZE
530 $links['total-changes'] = $nchanges[$n];
532 $links['total-changes'] = $this->linkRenderer
->makeKnownLink(
534 new HtmlArmor( $nchanges[$n] ),
535 [ 'class' => 'mw-changeslist-groupdiff' ],
537 'diff' => $currentRevision,
538 'oldid' => $last->mAttribs
['rc_last_oldid'],
541 if ( $sinceLast > 0 && $sinceLast < $n ) {
542 $links['total-changes-since-last'] = $this->linkRenderer
->makeKnownLink(
544 new HtmlArmor( $sinceLastVisitMsg[$sinceLast] ),
545 [ 'class' => 'mw-changeslist-groupdiff' ],
547 'diff' => $currentRevision,
548 'oldid' => $unvisitedOldid,
556 if ( $allLogs ||
$rcObj->mAttribs
['rc_type'] == RC_CATEGORIZE
) {
557 // don't show history link for logs
558 } elseif ( $namehidden ||
!$block0->getTitle()->exists() ) {
559 $links['history'] = $this->message
['enhancedrc-history'];
561 $params = $queryParams;
562 $params['action'] = 'history';
564 $links['history'] = $this->linkRenderer
->makeKnownLink(
566 new HtmlArmor( $this->message
['enhancedrc-history'] ),
567 [ 'class' => 'mw-changeslist-history' ],
572 # Allow others to alter, remove or add to these links
573 Hooks
::run( 'EnhancedChangesList::getLogText',
574 [ $this, &$links, $block ] );
580 $logtext = implode( $this->message
['pipe-separator'], $links );
581 $logtext = $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
582 return ' ' . $logtext;
586 * Enhanced RC ungrouped line.
588 * @param RecentChange|RCCacheEntry $rcObj
589 * @return string A HTML formatted line (generated using $r)
591 protected function recentChangesBlockLine( $rcObj ) {
594 $query['curid'] = $rcObj->mAttribs
['rc_cur_id'];
596 $type = $rcObj->mAttribs
['rc_type'];
597 $logType = $rcObj->mAttribs
['rc_log_type'];
598 $classes = $this->getHTMLClasses( $rcObj, $rcObj->watched
);
599 $classes[] = 'mw-enhanced-rc';
603 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-' . $logType );
605 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns' .
606 $rcObj->mAttribs
['rc_namespace'] . '-' . $rcObj->mAttribs
['rc_title'] );
610 $data['recentChangesFlags'] = [
611 'newpage' => $type == RC_NEW
,
612 'minor' => $rcObj->mAttribs
['rc_minor'],
613 'unpatrolled' => $rcObj->unpatrolled
,
614 'bot' => $rcObj->mAttribs
['rc_bot'],
616 // timestamp is not really a link here, but is called timestampLink
617 // for consistency with EnhancedChangesListModifyLineData
618 $data['timestampLink'] = $rcObj->timestamp
;
620 # Article or log link
622 $logPage = new LogPage( $logType );
623 $logTitle = SpecialPage
::getTitleFor( 'Log', $logType );
624 $logName = $logPage->getName()->text();
625 $data['logLink'] = $this->msg( 'parentheses' )
627 $this->linkRenderer
->makeKnownLink( $logTitle, $logName )
630 $data['articleLink'] = $this->getArticleLink( $rcObj, $rcObj->unpatrolled
, $rcObj->watched
);
633 # Diff and hist links
634 if ( $type != RC_LOG
&& $type != RC_CATEGORIZE
) {
635 $query['action'] = 'history';
636 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
638 $data['separatorAfterLinks'] = ' <span class="mw-changeslist-separator">. .</span> ';
641 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
642 $cd = $this->formatCharacterDifference( $rcObj );
644 $data['characterDiff'] = $cd;
645 $data['separatorAftercharacterDiff'] = ' <span class="mw-changeslist-separator">. .</span> ';
649 if ( $type == RC_LOG
) {
650 $data['logEntry'] = $this->insertLogEntry( $rcObj );
651 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
652 $data['comment'] = $this->insertComment( $rcObj );
654 $data['userLink'] = $rcObj->userlink
;
655 $data['userTalkLink'] = $rcObj->usertalklink
;
656 $data['comment'] = $this->insertComment( $rcObj );
657 if ( $type == RC_CATEGORIZE
) {
658 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
660 $data['rollback'] = $this->getRollback( $rcObj );
664 $data['tags'] = $this->getTags( $rcObj, $classes );
666 # Show how many people are watching this if enabled
667 $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers
);
669 // give the hook a chance to modify the data
670 $success = Hooks
::run( 'EnhancedChangesListModifyBlockLineData',
671 [ $this, &$data, $rcObj ] );
673 // skip entry if hook aborted it
677 $line = Html
::openElement( 'table', [ 'class' => $classes ] ) .
678 Html
::openElement( 'tr' );
679 $line .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
681 if ( isset( $data['recentChangesFlags'] ) ) {
682 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
683 unset( $data['recentChangesFlags'] );
686 if ( isset( $data['timestampLink'] ) ) {
687 $line .= ' ' . $data['timestampLink'];
688 unset( $data['timestampLink'] );
690 $line .= ' </td><td>';
692 // everything else: makes it easier for extensions to add or remove data
693 $line .= implode( '', $data );
695 $line .= "</td></tr></table>\n";
701 * Returns value to be used in 'historyLink' element of $data param in
702 * EnhancedChangesListModifyBlockLineData hook.
706 * @param RCCacheEntry $rc
707 * @param array $query array of key/value pairs to append as a query string
708 * @return string HTML
710 public function getDiffHistLinks( RCCacheEntry
$rc, array $query ) {
711 $pageTitle = $rc->getTitle();
712 if ( $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
) {
713 // For categorizations we must swap the category title with the page title!
714 $pageTitle = Title
::newFromID( $rc->getAttribute( 'rc_cur_id' ) );
717 $retVal = ' ' . $this->msg( 'parentheses' )
718 ->rawParams( $rc->difflink
. $this->message
['pipe-separator']
719 . $this->linkRenderer
->makeKnownLink(
721 new HtmlArmor( $this->message
['hist'] ),
722 [ 'class' => 'mw-changeslist-history' ],
729 * If enhanced RC is in use, this function takes the previously cached
730 * RC lines, arranges them, and outputs the HTML
734 protected function recentChangesBlock() {
735 if ( count( $this->rc_cache
) == 0 ) {
740 foreach ( $this->rc_cache
as $block ) {
741 if ( count( $block ) < 2 ) {
742 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
744 $blockOut .= $this->recentChangesBlockGroup( $block );
748 return '<div>' . $blockOut . '</div>';
752 * Returns text for the end of RC
753 * If enhanced RC is in use, returns pretty much all the text
756 public function endRecentChangesList() {
757 return $this->recentChangesBlock() . '</div>';