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
39 public function __construct( $obj ) {
40 if ( $obj instanceof Skin
) {
41 // @todo: deprecate constructing with Skin
42 $context = $obj->getContext();
44 if ( !$obj instanceof IContextSource
) {
45 throw new MWException( 'EnhancedChangesList must be constructed with a '
46 . 'context source or skin.' );
52 parent
::__construct( $context );
54 // message is set by the parent ChangesList class
55 $this->cacheEntryFactory
= new RCCacheEntryFactory(
62 * Add the JavaScript file for enhanced changeslist
65 public function beginRecentChangesList() {
67 $this->rcMoveIndex
= 0;
68 $this->rcCacheIndex
= 0;
70 $this->rclistOpen
= false;
71 $this->getOutput()->addModuleStyles( [
72 'mediawiki.special.changeslist',
73 'mediawiki.special.changeslist.enhanced',
75 $this->getOutput()->addModules( [
76 'jquery.makeCollapsible',
80 return '<div class="mw-changeslist">';
84 * Format a line for enhanced recentchange (aka with javascript and block of lines).
86 * @param RecentChange $rc
87 * @param bool $watched
88 * @param int $linenumber (default null)
92 public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
94 $date = $this->getLanguage()->userDate(
95 $rc->mAttribs
['rc_timestamp'],
101 # If it's a new day, add the headline and flush the cache
102 if ( $date != $this->lastdate
) {
103 # Process current cache
104 $ret = $this->recentChangesBlock();
105 $this->rc_cache
= [];
106 $ret .= Xml
::element( 'h4', null, $date ) . "\n";
107 $this->lastdate
= $date;
110 $cacheEntry = $this->cacheEntryFactory
->newFromRecentChange( $rc, $watched );
111 $this->addCacheEntry( $cacheEntry );
117 * Put accumulated information into the cache, for later display.
118 * Page moves go on their own line.
120 * @param RCCacheEntry $cacheEntry
122 protected function addCacheEntry( RCCacheEntry
$cacheEntry ) {
123 $cacheGroupingKey = $this->makeCacheGroupingKey( $cacheEntry );
125 if ( !isset( $this->rc_cache
[$cacheGroupingKey] ) ) {
126 $this->rc_cache
[$cacheGroupingKey] = [];
129 array_push( $this->rc_cache
[$cacheGroupingKey], $cacheEntry );
133 * @todo use rc_source to group, if set; fallback to rc_type
135 * @param RCCacheEntry $cacheEntry
139 protected function makeCacheGroupingKey( RCCacheEntry
$cacheEntry ) {
140 $title = $cacheEntry->getTitle();
141 $cacheGroupingKey = $title->getPrefixedDBkey();
143 $type = $cacheEntry->mAttribs
['rc_type'];
145 if ( $type == RC_LOG
) {
147 $cacheGroupingKey = SpecialPage
::getTitleFor(
149 $cacheEntry->mAttribs
['rc_log_type']
150 )->getPrefixedDBkey();
153 return $cacheGroupingKey;
158 * @param RCCacheEntry[] $block
160 * @throws DomainException
162 protected function recentChangesBlockGroup( $block ) {
163 $recentChangesFlags = $this->getConfig()->get( 'RecentChangesFlags' );
165 # Add the namespace and title of the block as part of the class
166 $tableClasses = [ 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' ];
167 if ( $block[0]->mAttribs
['rc_log_type'] ) {
169 $tableClasses[] = Sanitizer
::escapeClass( 'mw-changeslist-log-'
170 . $block[0]->mAttribs
['rc_log_type'] );
172 $tableClasses[] = Sanitizer
::escapeClass( 'mw-changeslist-ns'
173 . $block[0]->mAttribs
['rc_namespace'] . '-' . $block[0]->mAttribs
['rc_title'] );
175 if ( $block[0]->watched
176 && $block[0]->mAttribs
['rc_timestamp'] >= $block[0]->watched
178 $tableClasses[] = 'mw-changeslist-line-watched';
180 $tableClasses[] = 'mw-changeslist-line-not-watched';
183 # Collate list of users
187 # Some catalyst variables...
190 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
192 # Default values for RC flags
193 $collectedRcFlags = [];
194 foreach ( $recentChangesFlags as $key => $value ) {
195 $flagGrouping = ( isset( $recentChangesFlags[$key]['grouping'] ) ?
196 $recentChangesFlags[$key]['grouping'] : 'any' );
197 switch ( $flagGrouping ) {
199 $collectedRcFlags[$key] = true;
202 $collectedRcFlags[$key] = false;
205 throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
208 foreach ( $block as $rcObj ) {
209 // If all log actions to this page were hidden, then don't
210 // give the name of the affected page for this block!
211 if ( !$this->isDeleted( $rcObj, LogPage
::DELETED_ACTION
) ) {
214 $u = $rcObj->userlink
;
215 if ( !isset( $userlinks[$u] ) ) {
218 if ( $rcObj->mAttribs
['rc_type'] != RC_LOG
) {
221 # Get the latest entry with a page_id and oldid
222 # since logs may not have these.
223 if ( !$curId && $rcObj->mAttribs
['rc_cur_id'] ) {
224 $curId = $rcObj->mAttribs
['rc_cur_id'];
230 # Sort the list and convert to text
231 krsort( $userlinks );
234 foreach ( $userlinks as $userlink => $count ) {
236 $text .= $this->getLanguage()->getDirMark();
238 // @todo FIXME: Hardcoded '×'. Should be a message.
239 $formattedCount = $this->getLanguage()->formatNum( $count ) . '×';
240 $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
242 array_push( $users, $text );
247 $revDeletedMsg = false;
249 $revDeletedMsg = $this->msg( 'rev-deleted-event' )->escaped();
250 } elseif ( $allLogs ) {
251 $articleLink = $this->maybeWatchedLink( $block[0]->link
, $block[0]->watched
);
253 $articleLink = $this->getArticleLink( $block[0], $block[0]->unpatrolled
, $block[0]->watched
);
256 $queryParams['curid'] = $curId;
260 foreach ( $block as $i => $rcObj ) {
261 $line = $this->getLineData( $block, $rcObj, $queryParams );
263 // completely ignore this RC entry if we don't want to render it
269 foreach ( $line['recentChangesFlagsRaw'] as $key => $value ) {
270 $flagGrouping = ( isset( $recentChangesFlags[$key]['grouping'] ) ?
271 $recentChangesFlags[$key]['grouping'] : 'any' );
272 switch ( $flagGrouping ) {
275 $collectedRcFlags[$key] = false;
280 $collectedRcFlags[$key] = true;
284 throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
291 // Further down are some assumptions that $block is a 0-indexed array
292 // with (count-1) as last key. Let's make sure it is.
293 $block = array_values( $block );
295 if ( empty( $block ) ||
!$lines ) {
296 // if we can't show anything, don't display this block altogether
300 $logText = $this->getLogText( $block, $queryParams, $allLogs,
301 $collectedRcFlags['newpage'], $namehidden
304 # Character difference (does not apply if only log items)
305 $charDifference = false;
306 if ( $RCShowChangedSize && !$allLogs ) {
308 $first = count( $block ) - 1;
309 # Some events (like logs and category changes) have an "empty" size, so we need to skip those...
310 while ( $last < $first && $block[$last]->mAttribs
['rc_new_len'] === null ) {
313 while ( $last < $first && $block[$first]->mAttribs
['rc_old_len'] === null ) {
317 $charDifference = $this->formatCharacterDifference( $block[$first], $block[$last] );
320 $numberofWatchingusers = $this->numberofWatchingusers( $block[0]->numberofWatchingusers
);
321 $usersList = $this->msg( 'brackets' )->rawParams(
322 implode( $this->message
['semicolon-separator'], $users )
326 'articleLink' => $articleLink,
327 'charDifference' => $charDifference,
328 'collectedRcFlags' => $this->recentChangesFlags( $collectedRcFlags ),
329 'languageDirMark' => $this->getLanguage()->getDirMark(),
331 'logText' => $logText,
332 'numberofWatchingusers' => $numberofWatchingusers,
333 'rev-deleted-event' => $revDeletedMsg,
334 'tableClasses' => $tableClasses,
335 'timestamp' => $block[0]->timestamp
,
336 'users' => $usersList,
339 $this->rcCacheIndex++
;
341 $templateParser = new TemplateParser();
342 return $templateParser->processTemplate(
343 'EnhancedChangesListGroup',
349 * @param RCCacheEntry[] $block
350 * @param RCCacheEntry $rcObj
351 * @param array $queryParams
355 * @throws MWException
357 protected function getLineData( array $block, RCCacheEntry
$rcObj, array $queryParams = [] ) {
358 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
360 # Classes to apply -- TODO implement
362 $type = $rcObj->mAttribs
['rc_type'];
367 && $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
369 $lineParams['classes'] = [ 'mw-enhanced-watched' ];
371 $separator = ' <span class="mw-changeslist-separator">. .</span> ';
373 $data['recentChangesFlags'] = [
374 'newpage' => $type == RC_NEW
,
375 'minor' => $rcObj->mAttribs
['rc_minor'],
376 'unpatrolled' => $rcObj->unpatrolled
,
377 'bot' => $rcObj->mAttribs
['rc_bot'],
380 $params = $queryParams;
382 if ( $rcObj->mAttribs
['rc_this_oldid'] != 0 ) {
383 $params['oldid'] = $rcObj->mAttribs
['rc_this_oldid'];
387 if ( $type == RC_LOG
) {
388 $link = $rcObj->timestamp
;
390 } elseif ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
391 $link = '<span class="history-deleted">' . $rcObj->timestamp
. '</span> ';
393 $link = Linker
::linkKnown(
399 if ( $this->isDeleted( $rcObj, Revision
::DELETED_TEXT
) ) {
400 $link = '<span class="history-deleted">' . $link . '</span> ';
403 $data['timestampLink'] = $link;
405 $currentAndLastLinks = '';
406 if ( !$type == RC_LOG ||
$type == RC_NEW
) {
407 $currentAndLastLinks .= ' ' . $this->msg( 'parentheses' )->rawParams(
409 $this->message
['pipe-separator'] .
413 $data['currentAndLastLinks'] = $currentAndLastLinks;
414 $data['separatorAfterCurrentAndLastLinks'] = $separator;
417 if ( $RCShowChangedSize ) {
418 $cd = $this->formatCharacterDifference( $rcObj );
420 $data['characterDiff'] = $cd;
421 $data['separatorAfterCharacterDiff'] = $separator;
425 if ( $rcObj->mAttribs
['rc_type'] == RC_LOG
) {
426 $data['logEntry'] = $this->insertLogEntry( $rcObj );
427 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
428 $data['comment'] = $this->insertComment( $rcObj );
431 $data['userLink'] = $rcObj->userlink
;
432 $data['userTalkLink'] = $rcObj->usertalklink
;
433 $data['comment'] = $this->insertComment( $rcObj );
437 $data['rollback'] = $this->getRollback( $rcObj );
440 $data['tags'] = $this->getTags( $rcObj, $classes );
442 // give the hook a chance to modify the data
443 $success = Hooks
::run( 'EnhancedChangesListModifyLineData',
444 [ $this, &$data, $block, $rcObj ] );
446 // skip entry if hook aborted it
450 $lineParams['recentChangesFlagsRaw'] = [];
451 if ( isset( $data['recentChangesFlags'] ) ) {
452 $lineParams['recentChangesFlags'] = $this->recentChangesFlags( $data['recentChangesFlags'] );
453 # FIXME: This is used by logic, don't return it in the template params.
454 $lineParams['recentChangesFlagsRaw'] = $data['recentChangesFlags'];
455 unset( $data['recentChangesFlags'] );
458 if ( isset( $data['timestampLink'] ) ) {
459 $lineParams['timestampLink'] = $data['timestampLink'];
460 unset( $data['timestampLink'] );
463 // everything else: makes it easier for extensions to add or remove data
464 $lineParams['data'] = array_values( $data );
470 * Generates amount of changes (linking to diff ) & link to history.
472 * @param array $block
473 * @param array $queryParams
474 * @param bool $allLogs
476 * @param bool $namehidden
479 protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
480 if ( empty( $block ) ) {
485 static $nchanges = [];
486 static $sinceLastVisitMsg = [];
488 $n = count( $block );
489 if ( !isset( $nchanges[$n] ) ) {
490 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
494 $unvisitedOldid = null;
495 /** @var $rcObj RCCacheEntry */
496 foreach ( $block as $rcObj ) {
497 // Same logic as below inside main foreach
498 if ( $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
) {
500 $unvisitedOldid = $rcObj->mAttribs
['rc_last_oldid'];
503 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
504 $sinceLastVisitMsg[$sinceLast] =
505 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
508 $currentRevision = 0;
509 foreach ( $block as $rcObj ) {
510 if ( !$currentRevision ) {
511 $currentRevision = $rcObj->mAttribs
['rc_this_oldid'];
517 /** @var $block0 RecentChange */
519 $last = $block[count( $block ) - 1];
521 if ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ||
523 $rcObj->mAttribs
['rc_type'] == RC_CATEGORIZE
525 $links['total-changes'] = $nchanges[$n];
527 $links['total-changes'] = Linker
::link(
532 'diff' => $currentRevision,
533 'oldid' => $last->mAttribs
['rc_last_oldid'],
535 [ 'known', 'noclasses' ]
537 if ( $sinceLast > 0 && $sinceLast < $n ) {
538 $links['total-changes-since-last'] = Linker
::link(
540 $sinceLastVisitMsg[$sinceLast],
543 'diff' => $currentRevision,
544 'oldid' => $unvisitedOldid,
546 [ 'known', 'noclasses' ]
553 if ( $allLogs ||
$rcObj->mAttribs
['rc_type'] == RC_CATEGORIZE
) {
554 // don't show history link for logs
555 } elseif ( $namehidden ||
!$block0->getTitle()->exists() ) {
556 $links['history'] = $this->message
['enhancedrc-history'];
558 $params = $queryParams;
559 $params['action'] = 'history';
561 $links['history'] = Linker
::linkKnown(
563 $this->message
['enhancedrc-history'],
569 # Allow others to alter, remove or add to these links
570 Hooks
::run( 'EnhancedChangesList::getLogText',
571 [ $this, &$links, $block ] );
577 $logtext = implode( $this->message
['pipe-separator'], $links );
578 $logtext = $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
579 return ' ' . $logtext;
583 * Enhanced RC ungrouped line.
585 * @param RecentChange|RCCacheEntry $rcObj
586 * @return string A HTML formatted line (generated using $r)
588 protected function recentChangesBlockLine( $rcObj ) {
591 $query['curid'] = $rcObj->mAttribs
['rc_cur_id'];
593 $type = $rcObj->mAttribs
['rc_type'];
594 $logType = $rcObj->mAttribs
['rc_log_type'];
595 $classes = $this->getHTMLClasses( $rcObj, $rcObj->watched
);
596 $classes[] = 'mw-enhanced-rc';
600 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-' . $logType );
602 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns' .
603 $rcObj->mAttribs
['rc_namespace'] . '-' . $rcObj->mAttribs
['rc_title'] );
607 $data['recentChangesFlags'] = [
608 'newpage' => $type == RC_NEW
,
609 'minor' => $rcObj->mAttribs
['rc_minor'],
610 'unpatrolled' => $rcObj->unpatrolled
,
611 'bot' => $rcObj->mAttribs
['rc_bot'],
613 // timestamp is not really a link here, but is called timestampLink
614 // for consistency with EnhancedChangesListModifyLineData
615 $data['timestampLink'] = $rcObj->timestamp
;
617 # Article or log link
619 $logPage = new LogPage( $logType );
620 $logTitle = SpecialPage
::getTitleFor( 'Log', $logType );
621 $logName = $logPage->getName()->escaped();
622 $data['logLink'] = $this->msg( 'parentheses' )
623 ->rawParams( Linker
::linkKnown( $logTitle, $logName ) )->escaped();
625 $data['articleLink'] = $this->getArticleLink( $rcObj, $rcObj->unpatrolled
, $rcObj->watched
);
628 # Diff and hist links
629 if ( $type != RC_LOG
&& $type != RC_CATEGORIZE
) {
630 $query['action'] = 'history';
631 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
633 $data['separatorAfterLinks'] = ' <span class="mw-changeslist-separator">. .</span> ';
636 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
637 $cd = $this->formatCharacterDifference( $rcObj );
639 $data['characterDiff'] = $cd;
640 $data['separatorAftercharacterDiff'] = ' <span class="mw-changeslist-separator">. .</span> ';
644 if ( $type == RC_LOG
) {
645 $data['logEntry'] = $this->insertLogEntry( $rcObj );
646 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
647 $data['comment'] = $this->insertComment( $rcObj );
649 $data['userLink'] = $rcObj->userlink
;
650 $data['userTalkLink'] = $rcObj->usertalklink
;
651 $data['comment'] = $this->insertComment( $rcObj );
652 if ( $type == RC_CATEGORIZE
) {
653 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
655 $data['rollback'] = $this->getRollback( $rcObj );
659 $data['tags'] = $this->getTags( $rcObj, $classes );
661 # Show how many people are watching this if enabled
662 $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers
);
664 // give the hook a chance to modify the data
665 $success = Hooks
::run( 'EnhancedChangesListModifyBlockLineData',
666 [ $this, &$data, $rcObj ] );
668 // skip entry if hook aborted it
672 $line = Html
::openElement( 'table', [ 'class' => $classes ] ) .
673 Html
::openElement( 'tr' );
674 $line .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
676 if ( isset( $data['recentChangesFlags'] ) ) {
677 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
678 unset( $data['recentChangesFlags'] );
681 if ( isset( $data['timestampLink'] ) ) {
682 $line .= ' ' . $data['timestampLink'];
683 unset( $data['timestampLink'] );
685 $line .= ' </td><td>';
687 // everything else: makes it easier for extensions to add or remove data
688 $line .= implode( '', $data );
690 $line .= "</td></tr></table>\n";
696 * Returns value to be used in 'historyLink' element of $data param in
697 * EnhancedChangesListModifyBlockLineData hook.
701 * @param RCCacheEntry $rc
702 * @param array $query array of key/value pairs to append as a query string
703 * @return string HTML
705 public function getDiffHistLinks( RCCacheEntry
$rc, array $query ) {
706 $pageTitle = $rc->getTitle();
707 if ( $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
) {
708 // For categorizations we must swap the category title with the page title!
709 $pageTitle = Title
::newFromID( $rc->getAttribute( 'rc_cur_id' ) );
712 $retVal = ' ' . $this->msg( 'parentheses' )
713 ->rawParams( $rc->difflink
. $this->message
['pipe-separator'] . Linker
::linkKnown(
715 $this->message
['hist'],
723 * If enhanced RC is in use, this function takes the previously cached
724 * RC lines, arranges them, and outputs the HTML
728 protected function recentChangesBlock() {
729 if ( count( $this->rc_cache
) == 0 ) {
734 foreach ( $this->rc_cache
as $block ) {
735 if ( count( $block ) < 2 ) {
736 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
738 $blockOut .= $this->recentChangesBlockGroup( $block );
742 return '<div>' . $blockOut . '</div>';
746 * Returns text for the end of RC
747 * If enhanced RC is in use, returns pretty much all the text
750 public function endRecentChangesList() {
751 return $this->recentChangesBlock() . '</div>';