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(
63 * Add the JavaScript file for enhanced changeslist
66 public function beginRecentChangesList() {
68 $this->rcMoveIndex
= 0;
69 $this->rcCacheIndex
= 0;
71 $this->rclistOpen
= false;
72 $this->getOutput()->addModuleStyles( [
73 'mediawiki.special.changeslist',
74 'mediawiki.special.changeslist.enhanced',
76 $this->getOutput()->addModules( [
77 'jquery.makeCollapsible',
81 return '<div class="mw-changeslist">';
85 * Format a line for enhanced recentchange (aka with javascript and block of lines).
87 * @param RecentChange $rc
88 * @param bool $watched
89 * @param int $linenumber (default null)
93 public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
95 $date = $this->getLanguage()->userDate(
96 $rc->mAttribs
['rc_timestamp'],
102 # If it's a new day, add the headline and flush the cache
103 if ( $date != $this->lastdate
) {
104 # Process current cache
105 $ret = $this->recentChangesBlock();
106 $this->rc_cache
= [];
107 $ret .= Xml
::element( 'h4', null, $date ) . "\n";
108 $this->lastdate
= $date;
111 $cacheEntry = $this->cacheEntryFactory
->newFromRecentChange( $rc, $watched );
112 $this->addCacheEntry( $cacheEntry );
118 * Put accumulated information into the cache, for later display.
119 * Page moves go on their own line.
121 * @param RCCacheEntry $cacheEntry
123 protected function addCacheEntry( RCCacheEntry
$cacheEntry ) {
124 $cacheGroupingKey = $this->makeCacheGroupingKey( $cacheEntry );
126 if ( !isset( $this->rc_cache
[$cacheGroupingKey] ) ) {
127 $this->rc_cache
[$cacheGroupingKey] = [];
130 array_push( $this->rc_cache
[$cacheGroupingKey], $cacheEntry );
134 * @todo use rc_source to group, if set; fallback to rc_type
136 * @param RCCacheEntry $cacheEntry
140 protected function makeCacheGroupingKey( RCCacheEntry
$cacheEntry ) {
141 $title = $cacheEntry->getTitle();
142 $cacheGroupingKey = $title->getPrefixedDBkey();
144 $type = $cacheEntry->mAttribs
['rc_type'];
146 if ( $type == RC_LOG
) {
148 $cacheGroupingKey = SpecialPage
::getTitleFor(
150 $cacheEntry->mAttribs
['rc_log_type']
151 )->getPrefixedDBkey();
154 return $cacheGroupingKey;
159 * @param RCCacheEntry[] $block
161 * @throws DomainException
163 protected function recentChangesBlockGroup( $block ) {
164 $recentChangesFlags = $this->getConfig()->get( 'RecentChangesFlags' );
166 # Add the namespace and title of the block as part of the class
167 $tableClasses = [ 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' ];
168 if ( $block[0]->mAttribs
['rc_log_type'] ) {
170 $tableClasses[] = Sanitizer
::escapeClass( 'mw-changeslist-log-'
171 . $block[0]->mAttribs
['rc_log_type'] );
173 $tableClasses[] = Sanitizer
::escapeClass( 'mw-changeslist-ns'
174 . $block[0]->mAttribs
['rc_namespace'] . '-' . $block[0]->mAttribs
['rc_title'] );
176 if ( $block[0]->watched
177 && $block[0]->mAttribs
['rc_timestamp'] >= $block[0]->watched
179 $tableClasses[] = 'mw-changeslist-line-watched';
181 $tableClasses[] = 'mw-changeslist-line-not-watched';
184 # Collate list of users
188 # Some catalyst variables...
191 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
193 # Default values for RC flags
194 $collectedRcFlags = [];
195 foreach ( $recentChangesFlags as $key => $value ) {
196 $flagGrouping = ( isset( $recentChangesFlags[$key]['grouping'] ) ?
197 $recentChangesFlags[$key]['grouping'] : 'any' );
198 switch ( $flagGrouping ) {
200 $collectedRcFlags[$key] = true;
203 $collectedRcFlags[$key] = false;
206 throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
209 foreach ( $block as $rcObj ) {
210 // If all log actions to this page were hidden, then don't
211 // give the name of the affected page for this block!
212 if ( !$this->isDeleted( $rcObj, LogPage
::DELETED_ACTION
) ) {
215 $u = $rcObj->userlink
;
216 if ( !isset( $userlinks[$u] ) ) {
219 if ( $rcObj->mAttribs
['rc_type'] != RC_LOG
) {
222 # Get the latest entry with a page_id and oldid
223 # since logs may not have these.
224 if ( !$curId && $rcObj->mAttribs
['rc_cur_id'] ) {
225 $curId = $rcObj->mAttribs
['rc_cur_id'];
231 # Sort the list and convert to text
232 krsort( $userlinks );
235 foreach ( $userlinks as $userlink => $count ) {
237 $text .= $this->getLanguage()->getDirMark();
239 // @todo FIXME: Hardcoded '×'. Should be a message.
240 $formattedCount = $this->getLanguage()->formatNum( $count ) . '×';
241 $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
243 array_push( $users, $text );
248 $revDeletedMsg = false;
250 $revDeletedMsg = $this->msg( 'rev-deleted-event' )->escaped();
251 } elseif ( $allLogs ) {
252 $articleLink = $this->maybeWatchedLink( $block[0]->link
, $block[0]->watched
);
254 $articleLink = $this->getArticleLink( $block[0], $block[0]->unpatrolled
, $block[0]->watched
);
257 $queryParams['curid'] = $curId;
261 foreach ( $block as $i => $rcObj ) {
262 $line = $this->getLineData( $block, $rcObj, $queryParams );
264 // completely ignore this RC entry if we don't want to render it
270 foreach ( $line['recentChangesFlagsRaw'] as $key => $value ) {
271 $flagGrouping = ( isset( $recentChangesFlags[$key]['grouping'] ) ?
272 $recentChangesFlags[$key]['grouping'] : 'any' );
273 switch ( $flagGrouping ) {
276 $collectedRcFlags[$key] = false;
281 $collectedRcFlags[$key] = true;
285 throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
292 // Further down are some assumptions that $block is a 0-indexed array
293 // with (count-1) as last key. Let's make sure it is.
294 $block = array_values( $block );
296 if ( empty( $block ) ||
!$lines ) {
297 // if we can't show anything, don't display this block altogether
301 $logText = $this->getLogText( $block, $queryParams, $allLogs,
302 $collectedRcFlags['newpage'], $namehidden
305 # Character difference (does not apply if only log items)
306 $charDifference = false;
307 if ( $RCShowChangedSize && !$allLogs ) {
309 $first = count( $block ) - 1;
310 # Some events (like logs and category changes) have an "empty" size, so we need to skip those...
311 while ( $last < $first && $block[$last]->mAttribs
['rc_new_len'] === null ) {
314 while ( $last < $first && $block[$first]->mAttribs
['rc_old_len'] === null ) {
318 $charDifference = $this->formatCharacterDifference( $block[$first], $block[$last] );
321 $numberofWatchingusers = $this->numberofWatchingusers( $block[0]->numberofWatchingusers
);
322 $usersList = $this->msg( 'brackets' )->rawParams(
323 implode( $this->message
['semicolon-separator'], $users )
327 'articleLink' => $articleLink,
328 'charDifference' => $charDifference,
329 'collectedRcFlags' => $this->recentChangesFlags( $collectedRcFlags ),
330 'languageDirMark' => $this->getLanguage()->getDirMark(),
332 'logText' => $logText,
333 'numberofWatchingusers' => $numberofWatchingusers,
334 'rev-deleted-event' => $revDeletedMsg,
335 'tableClasses' => $tableClasses,
336 'timestamp' => $block[0]->timestamp
,
337 'users' => $usersList,
340 $this->rcCacheIndex++
;
342 $templateParser = new TemplateParser();
343 return $templateParser->processTemplate(
344 'EnhancedChangesListGroup',
350 * @param RCCacheEntry[] $block
351 * @param RCCacheEntry $rcObj
352 * @param array $queryParams
356 * @throws MWException
358 protected function getLineData( array $block, RCCacheEntry
$rcObj, array $queryParams = [] ) {
359 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
361 $classes = [ 'mw-enhanced-rc' ];
362 $type = $rcObj->mAttribs
['rc_type'];
367 && $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
369 $classes = [ 'mw-enhanced-watched' ];
372 $separator = ' <span class="mw-changeslist-separator">. .</span> ';
374 $data['recentChangesFlags'] = [
375 'newpage' => $type == RC_NEW
,
376 'minor' => $rcObj->mAttribs
['rc_minor'],
377 'unpatrolled' => $rcObj->unpatrolled
,
378 'bot' => $rcObj->mAttribs
['rc_bot'],
381 $params = $queryParams;
383 if ( $rcObj->mAttribs
['rc_this_oldid'] != 0 ) {
384 $params['oldid'] = $rcObj->mAttribs
['rc_this_oldid'];
388 if ( $type == RC_LOG
) {
389 $link = $rcObj->timestamp
;
391 } elseif ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
392 $link = '<span class="history-deleted">' . $rcObj->timestamp
. '</span> ';
394 $link = $this->linkRenderer
->makeKnownLink(
396 new HtmlArmor( $rcObj->timestamp
),
400 if ( $this->isDeleted( $rcObj, Revision
::DELETED_TEXT
) ) {
401 $link = '<span class="history-deleted">' . $link . '</span> ';
404 $data['timestampLink'] = $link;
406 $currentAndLastLinks = '';
407 if ( !$type == RC_LOG ||
$type == RC_NEW
) {
408 $currentAndLastLinks .= ' ' . $this->msg( 'parentheses' )->rawParams(
410 $this->message
['pipe-separator'] .
414 $data['currentAndLastLinks'] = $currentAndLastLinks;
415 $data['separatorAfterCurrentAndLastLinks'] = $separator;
418 if ( $RCShowChangedSize ) {
419 $cd = $this->formatCharacterDifference( $rcObj );
421 $data['characterDiff'] = $cd;
422 $data['separatorAfterCharacterDiff'] = $separator;
426 if ( $rcObj->mAttribs
['rc_type'] == RC_LOG
) {
427 $data['logEntry'] = $this->insertLogEntry( $rcObj );
428 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
429 $data['comment'] = $this->insertComment( $rcObj );
432 $data['userLink'] = $rcObj->userlink
;
433 $data['userTalkLink'] = $rcObj->usertalklink
;
434 $data['comment'] = $this->insertComment( $rcObj );
438 $data['rollback'] = $this->getRollback( $rcObj );
441 $data['tags'] = $this->getTags( $rcObj, $classes );
443 // give the hook a chance to modify the data
444 $success = Hooks
::run( 'EnhancedChangesListModifyLineData',
445 [ $this, &$data, $block, $rcObj, &$classes ] );
447 // skip entry if hook aborted it
451 $lineParams['recentChangesFlagsRaw'] = [];
452 if ( isset( $data['recentChangesFlags'] ) ) {
453 $lineParams['recentChangesFlags'] = $this->recentChangesFlags( $data['recentChangesFlags'] );
454 # FIXME: This is used by logic, don't return it in the template params.
455 $lineParams['recentChangesFlagsRaw'] = $data['recentChangesFlags'];
456 unset( $data['recentChangesFlags'] );
459 if ( isset( $data['timestampLink'] ) ) {
460 $lineParams['timestampLink'] = $data['timestampLink'];
461 unset( $data['timestampLink'] );
464 $lineParams['classes'] = array_values( $classes );
466 // everything else: makes it easier for extensions to add or remove data
467 $lineParams['data'] = array_values( $data );
473 * Generates amount of changes (linking to diff ) & link to history.
475 * @param array $block
476 * @param array $queryParams
477 * @param bool $allLogs
479 * @param bool $namehidden
482 protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
483 if ( empty( $block ) ) {
488 static $nchanges = [];
489 static $sinceLastVisitMsg = [];
491 $n = count( $block );
492 if ( !isset( $nchanges[$n] ) ) {
493 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
497 $unvisitedOldid = null;
498 /** @var $rcObj RCCacheEntry */
499 foreach ( $block as $rcObj ) {
500 // Same logic as below inside main foreach
501 if ( $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
) {
503 $unvisitedOldid = $rcObj->mAttribs
['rc_last_oldid'];
506 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
507 $sinceLastVisitMsg[$sinceLast] =
508 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
511 $currentRevision = 0;
512 foreach ( $block as $rcObj ) {
513 if ( !$currentRevision ) {
514 $currentRevision = $rcObj->mAttribs
['rc_this_oldid'];
520 /** @var $block0 RecentChange */
522 $last = $block[count( $block ) - 1];
524 if ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ||
526 $rcObj->mAttribs
['rc_type'] == RC_CATEGORIZE
528 $links['total-changes'] = $nchanges[$n];
530 $links['total-changes'] = $this->linkRenderer
->makeKnownLink(
532 new HtmlArmor( $nchanges[$n] ),
535 'diff' => $currentRevision,
536 'oldid' => $last->mAttribs
['rc_last_oldid'],
539 if ( $sinceLast > 0 && $sinceLast < $n ) {
540 $links['total-changes-since-last'] = $this->linkRenderer
->makeKnownLink(
542 new HtmlArmor( $sinceLastVisitMsg[$sinceLast] ),
545 'diff' => $currentRevision,
546 'oldid' => $unvisitedOldid,
554 if ( $allLogs ||
$rcObj->mAttribs
['rc_type'] == RC_CATEGORIZE
) {
555 // don't show history link for logs
556 } elseif ( $namehidden ||
!$block0->getTitle()->exists() ) {
557 $links['history'] = $this->message
['enhancedrc-history'];
559 $params = $queryParams;
560 $params['action'] = 'history';
562 $links['history'] = $this->linkRenderer
->makeKnownLink(
564 new HtmlArmor( $this->message
['enhancedrc-history'] ),
570 # Allow others to alter, remove or add to these links
571 Hooks
::run( 'EnhancedChangesList::getLogText',
572 [ $this, &$links, $block ] );
578 $logtext = implode( $this->message
['pipe-separator'], $links );
579 $logtext = $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
580 return ' ' . $logtext;
584 * Enhanced RC ungrouped line.
586 * @param RecentChange|RCCacheEntry $rcObj
587 * @return string A HTML formatted line (generated using $r)
589 protected function recentChangesBlockLine( $rcObj ) {
592 $query['curid'] = $rcObj->mAttribs
['rc_cur_id'];
594 $type = $rcObj->mAttribs
['rc_type'];
595 $logType = $rcObj->mAttribs
['rc_log_type'];
596 $classes = $this->getHTMLClasses( $rcObj, $rcObj->watched
);
597 $classes[] = 'mw-enhanced-rc';
601 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-' . $logType );
603 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns' .
604 $rcObj->mAttribs
['rc_namespace'] . '-' . $rcObj->mAttribs
['rc_title'] );
608 $data['recentChangesFlags'] = [
609 'newpage' => $type == RC_NEW
,
610 'minor' => $rcObj->mAttribs
['rc_minor'],
611 'unpatrolled' => $rcObj->unpatrolled
,
612 'bot' => $rcObj->mAttribs
['rc_bot'],
614 // timestamp is not really a link here, but is called timestampLink
615 // for consistency with EnhancedChangesListModifyLineData
616 $data['timestampLink'] = $rcObj->timestamp
;
618 # Article or log link
620 $logPage = new LogPage( $logType );
621 $logTitle = SpecialPage
::getTitleFor( 'Log', $logType );
622 $logName = $logPage->getName()->text();
623 $data['logLink'] = $this->msg( 'parentheses' )
625 $this->linkRenderer
->makeKnownLink( $logTitle, $logName )
628 $data['articleLink'] = $this->getArticleLink( $rcObj, $rcObj->unpatrolled
, $rcObj->watched
);
631 # Diff and hist links
632 if ( $type != RC_LOG
&& $type != RC_CATEGORIZE
) {
633 $query['action'] = 'history';
634 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
636 $data['separatorAfterLinks'] = ' <span class="mw-changeslist-separator">. .</span> ';
639 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
640 $cd = $this->formatCharacterDifference( $rcObj );
642 $data['characterDiff'] = $cd;
643 $data['separatorAftercharacterDiff'] = ' <span class="mw-changeslist-separator">. .</span> ';
647 if ( $type == RC_LOG
) {
648 $data['logEntry'] = $this->insertLogEntry( $rcObj );
649 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
650 $data['comment'] = $this->insertComment( $rcObj );
652 $data['userLink'] = $rcObj->userlink
;
653 $data['userTalkLink'] = $rcObj->usertalklink
;
654 $data['comment'] = $this->insertComment( $rcObj );
655 if ( $type == RC_CATEGORIZE
) {
656 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
658 $data['rollback'] = $this->getRollback( $rcObj );
662 $data['tags'] = $this->getTags( $rcObj, $classes );
664 # Show how many people are watching this if enabled
665 $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers
);
667 // give the hook a chance to modify the data
668 $success = Hooks
::run( 'EnhancedChangesListModifyBlockLineData',
669 [ $this, &$data, $rcObj ] );
671 // skip entry if hook aborted it
675 $line = Html
::openElement( 'table', [ 'class' => $classes ] ) .
676 Html
::openElement( 'tr' );
677 $line .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
679 if ( isset( $data['recentChangesFlags'] ) ) {
680 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
681 unset( $data['recentChangesFlags'] );
684 if ( isset( $data['timestampLink'] ) ) {
685 $line .= ' ' . $data['timestampLink'];
686 unset( $data['timestampLink'] );
688 $line .= ' </td><td>';
690 // everything else: makes it easier for extensions to add or remove data
691 $line .= implode( '', $data );
693 $line .= "</td></tr></table>\n";
699 * Returns value to be used in 'historyLink' element of $data param in
700 * EnhancedChangesListModifyBlockLineData hook.
704 * @param RCCacheEntry $rc
705 * @param array $query array of key/value pairs to append as a query string
706 * @return string HTML
708 public function getDiffHistLinks( RCCacheEntry
$rc, array $query ) {
709 $pageTitle = $rc->getTitle();
710 if ( $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
) {
711 // For categorizations we must swap the category title with the page title!
712 $pageTitle = Title
::newFromID( $rc->getAttribute( 'rc_cur_id' ) );
715 $retVal = ' ' . $this->msg( 'parentheses' )
716 ->rawParams( $rc->difflink
. $this->message
['pipe-separator']
717 . $this->linkRenderer
->makeKnownLink(
719 new HtmlArmor( $this->message
['hist'] ),
727 * If enhanced RC is in use, this function takes the previously cached
728 * RC lines, arranges them, and outputs the HTML
732 protected function recentChangesBlock() {
733 if ( count( $this->rc_cache
) == 0 ) {
738 foreach ( $this->rc_cache
as $block ) {
739 if ( count( $block ) < 2 ) {
740 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
742 $blockOut .= $this->recentChangesBlockGroup( $block );
746 return '<div>' . $blockOut . '</div>';
750 * Returns text for the end of RC
751 * If enhanced RC is in use, returns pretty much all the text
754 public function endRecentChangesList() {
755 return $this->recentChangesBlock() . '</div>';