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() {
66 $this->rc_cache
= array();
67 $this->rcMoveIndex
= 0;
68 $this->rcCacheIndex
= 0;
70 $this->rclistOpen
= false;
71 $this->getOutput()->addModuleStyles( array(
72 'mediawiki.special.changeslist',
73 'mediawiki.special.changeslist.enhanced',
75 $this->getOutput()->addModules( array(
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
= array();
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] = array();
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
161 protected function recentChangesBlockGroup( $block ) {
163 # Add the namespace and title of the block as part of the class
164 $classes = array( 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' );
165 if ( $block[0]->mAttribs
['rc_log_type'] ) {
167 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-'
168 . $block[0]->mAttribs
['rc_log_type'] );
170 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns'
171 . $block[0]->mAttribs
['rc_namespace'] . '-' . $block[0]->mAttribs
['rc_title'] );
173 $classes[] = $block[0]->watched
&& $block[0]->mAttribs
['rc_timestamp'] >= $block[0]->watched
174 ?
'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
175 $r = Html
::openElement( 'table', array( 'class' => $classes ) ) .
176 Html
::openElement( 'tr' );
178 # Collate list of users
179 $userlinks = array();
182 # Some catalyst variables...
185 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
186 $collectedRcFlags = array(
189 // Includes a new page?
191 // All are minor edits?
193 // Contains an unpatrolled edit?
194 'unpatrolled' => false,
196 foreach ( $block as $rcObj ) {
197 if ( $rcObj->mAttribs
['rc_type'] == RC_NEW
) {
198 $collectedRcFlags['newpage'] = true;
200 // If all log actions to this page were hidden, then don't
201 // give the name of the affected page for this block!
202 if ( !$this->isDeleted( $rcObj, LogPage
::DELETED_ACTION
) ) {
205 $u = $rcObj->userlink
;
206 if ( !isset( $userlinks[$u] ) ) {
209 if ( $rcObj->unpatrolled
) {
210 $collectedRcFlags['unpatrolled'] = true;
212 if ( $rcObj->mAttribs
['rc_type'] != RC_LOG
) {
215 # Get the latest entry with a page_id and oldid
216 # since logs may not have these.
217 if ( !$curId && $rcObj->mAttribs
['rc_cur_id'] ) {
218 $curId = $rcObj->mAttribs
['rc_cur_id'];
221 if ( !$rcObj->mAttribs
['rc_bot'] ) {
222 $collectedRcFlags['bot'] = false;
224 if ( !$rcObj->mAttribs
['rc_minor'] ) {
225 $collectedRcFlags['minor'] = false;
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 );
246 $users = ' <span class="changedby">'
247 . $this->msg( 'brackets' )->rawParams(
248 implode( $this->message
['semicolon-separator'], $users )
249 )->escaped() . '</span>';
251 $tl = '<span class="mw-collapsible-toggle mw-collapsible-arrow ' .
252 'mw-enhancedchanges-arrow mw-enhancedchanges-arrow-space"></span>';
253 $r .= "<td>$tl</td>";
256 $r .= '<td class="mw-enhanced-rc">' . $this->recentChangesFlags(
261 $r .= ' ' . $block[0]->timestamp
. ' </td><td>';
265 $r .= ' <span class="history-deleted">' .
266 $this->msg( 'rev-deleted-event' )->escaped() . '</span>';
267 } elseif ( $allLogs ) {
268 $r .= $this->maybeWatchedLink( $block[0]->link
, $block[0]->watched
);
270 $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled
, $block[0]->watched
);
273 $r .= $this->getLanguage()->getDirMark();
275 $queryParams['curid'] = $curId;
279 foreach ( $block as $i => $rcObj ) {
280 $line = $this->getLineData( $block, $rcObj, $queryParams );
283 // completely ignore this RC entry if we don't want to render it
287 // Further down are some assumptions that $block is a 0-indexed array
288 // with (count-1) as last key. Let's make sure it is.
289 $block = array_values( $block );
290 if ( empty( $block ) ) {
291 // if we can't show anything, don't display this block altogether
295 $r .= $this->getLogText( $block, $queryParams, $allLogs,
296 $collectedRcFlags['newpage'], $namehidden );
298 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
300 # Character difference (does not apply if only log items)
301 if ( $RCShowChangedSize && !$allLogs ) {
303 $first = count( $block ) - 1;
304 # Some events (like logs) have an "empty" size, so we need to skip those...
305 while ( $last < $first && $block[$last]->mAttribs
['rc_new_len'] === null ) {
308 while ( $first > $last && $block[$first]->mAttribs
['rc_old_len'] === null ) {
312 $chardiff = $this->formatCharacterDifference( $block[$first], $block[$last] );
314 if ( $chardiff == '' ) {
317 $r .= ' ' . $chardiff . ' <span class="mw-changeslist-separator">. .</span> ';
322 $r .= $this->numberofWatchingusers( $block[0]->numberofWatchingusers
);
326 // if there are no lines to be rendered (all aborted by hook), don't render the block
333 $this->rcCacheIndex++
;
339 * @param RCCacheEntry[] $block
340 * @param RCCacheEntry $rcObj
341 * @param array $queryParams
345 * @throws MWException
347 protected function getLineData( array $block, RCCacheEntry
$rcObj, array $queryParams = array() ) {
348 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
350 # Classes to apply -- TODO implement
352 $type = $rcObj->mAttribs
['rc_type'];
355 $trClass = $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
356 ?
' class="mw-enhanced-watched"' : '';
357 $separator = ' <span class="mw-changeslist-separator">. .</span> ';
359 $data['recentChangesFlags'] = array(
360 'newpage' => $type == RC_NEW
,
361 'minor' => $rcObj->mAttribs
['rc_minor'],
362 'unpatrolled' => $rcObj->unpatrolled
,
363 'bot' => $rcObj->mAttribs
['rc_bot'],
366 $params = $queryParams;
368 if ( $rcObj->mAttribs
['rc_this_oldid'] != 0 ) {
369 $params['oldid'] = $rcObj->mAttribs
['rc_this_oldid'];
373 if ( $type == RC_LOG
) {
374 $link = $rcObj->timestamp
;
376 } elseif ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
377 $link = '<span class="history-deleted">' . $rcObj->timestamp
. '</span> ';
379 $link = Linker
::linkKnown(
385 if ( $this->isDeleted( $rcObj, Revision
::DELETED_TEXT
) ) {
386 $link = '<span class="history-deleted">' . $link . '</span> ';
389 $data['timestampLink'] = $link;
391 $currentAndLastLinks = '';
392 if ( !$type == RC_LOG ||
$type == RC_NEW
) {
393 $currentAndLastLinks .= ' ' . $this->msg( 'parentheses' )->rawParams(
395 $this->message
['pipe-separator'] .
399 $data['currentAndLastLinks'] = $currentAndLastLinks;
400 $data['separatorAfterCurrentAndLastLinks'] = $separator;
403 if ( $RCShowChangedSize ) {
404 $cd = $this->formatCharacterDifference( $rcObj );
406 $data['characterDiff'] = $cd;
407 $data['separatorAfterCharacterDiff'] = $separator;
411 if ( $rcObj->mAttribs
['rc_type'] == RC_LOG
) {
412 $data['logEntry'] = $this->insertLogEntry( $rcObj );
413 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
414 $data['comment'] = $this->insertComment( $rcObj );
417 $data['userLink'] = $rcObj->userlink
;
418 $data['userTalkLink'] = $rcObj->usertalklink
;
419 $data['comment'] = $this->insertComment( $rcObj );
423 $data['rollback'] = $this->getRollback( $rcObj );
426 $data['tags'] = $this->getTags( $rcObj, $classes );
428 // give the hook a chance to modify the data
429 $success = Hooks
::run( 'EnhancedChangesListModifyLineData',
430 array( $this, &$data, $block, $rcObj ) );
432 // skip entry if hook aborted it
436 $line = '<tr' . $trClass . '><td></td><td class="mw-enhanced-rc">';
437 if ( isset( $data['recentChangesFlags'] ) ) {
438 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
439 unset( $data['recentChangesFlags'] );
441 $line .= ' </td><td class="mw-enhanced-rc-nested">';
443 if ( isset( $data['timestampLink'] ) ) {
444 $line .= '<span class="mw-enhanced-rc-time">' . $data['timestampLink'] . '</span>';
445 unset( $data['timestampLink'] );
448 // everything else: makes it easier for extensions to add or remove data
449 $line .= implode( '', $data );
451 $line .= "</td></tr>\n";
457 * Generates amount of changes (linking to diff ) & link to history.
459 * @param array $block
460 * @param array $queryParams
461 * @param bool $allLogs
463 * @param bool $namehidden
466 protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
467 if ( empty( $block ) ) {
472 static $nchanges = array();
473 static $sinceLastVisitMsg = array();
475 $n = count( $block );
476 if ( !isset( $nchanges[$n] ) ) {
477 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
481 $unvisitedOldid = null;
482 /** @var $rcObj RCCacheEntry */
483 foreach ( $block as $rcObj ) {
484 // Same logic as below inside main foreach
485 if ( $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
) {
487 $unvisitedOldid = $rcObj->mAttribs
['rc_last_oldid'];
490 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
491 $sinceLastVisitMsg[$sinceLast] =
492 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
495 $currentRevision = 0;
496 foreach ( $block as $rcObj ) {
497 if ( !$currentRevision ) {
498 $currentRevision = $rcObj->mAttribs
['rc_this_oldid'];
504 /** @var $block0 RecentChange */
506 $last = $block[count( $block ) - 1];
507 if ( !$allLogs && $rcObj->mAttribs
['rc_type'] != RC_CATEGORIZE
) {
508 if ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
509 $links['total-changes'] = $nchanges[$n];
510 } elseif ( $isnew ) {
511 $links['total-changes'] = $nchanges[$n];
513 $links['total-changes'] = Linker
::link(
517 $queryParams +
array(
518 'diff' => $currentRevision,
519 'oldid' => $last->mAttribs
['rc_last_oldid'],
521 array( 'known', 'noclasses' )
523 if ( $sinceLast > 0 && $sinceLast < $n ) {
524 $links['total-changes-since-last'] = Linker
::link(
526 $sinceLastVisitMsg[$sinceLast],
528 $queryParams +
array(
529 'diff' => $currentRevision,
530 'oldid' => $unvisitedOldid,
532 array( 'known', 'noclasses' )
539 if ( $allLogs ||
$rcObj->mAttribs
['rc_type'] == RC_CATEGORIZE
) {
540 // don't show history link for logs
541 } elseif ( $namehidden ||
!$block0->getTitle()->exists() ) {
542 $links['history'] = $this->message
['enhancedrc-history'];
544 $params = $queryParams;
545 $params['action'] = 'history';
547 $links['history'] = Linker
::linkKnown(
549 $this->message
['enhancedrc-history'],
555 # Allow others to alter, remove or add to these links
556 Hooks
::run( 'EnhancedChangesList::getLogText',
557 array( $this, &$links, $block ) );
563 $logtext = implode( $this->message
['pipe-separator'], $links );
564 $logtext = $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
565 return ' ' . $logtext;
569 * Enhanced RC ungrouped line.
571 * @param RecentChange|RCCacheEntry $rcObj
572 * @return string A HTML formatted line (generated using $r)
574 protected function recentChangesBlockLine( $rcObj ) {
577 $query['curid'] = $rcObj->mAttribs
['rc_cur_id'];
579 $type = $rcObj->mAttribs
['rc_type'];
580 $logType = $rcObj->mAttribs
['rc_log_type'];
581 $classes = array( 'mw-enhanced-rc' );
584 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-' . $logType );
586 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns' .
587 $rcObj->mAttribs
['rc_namespace'] . '-' . $rcObj->mAttribs
['rc_title'] );
589 $classes[] = $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
590 ?
'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
593 $data['recentChangesFlags'] = array(
594 'newpage' => $type == RC_NEW
,
595 'minor' => $rcObj->mAttribs
['rc_minor'],
596 'unpatrolled' => $rcObj->unpatrolled
,
597 'bot' => $rcObj->mAttribs
['rc_bot'],
599 // timestamp is not really a link here, but is called timestampLink
600 // for consistency with EnhancedChangesListModifyLineData
601 $data['timestampLink'] = $rcObj->timestamp
;
603 # Article or log link
605 $logPage = new LogPage( $logType );
606 $logTitle = SpecialPage
::getTitleFor( 'Log', $logType );
607 $logName = $logPage->getName()->escaped();
608 $data['logLink'] = $this->msg( 'parentheses' )
609 ->rawParams( Linker
::linkKnown( $logTitle, $logName ) )->escaped();
611 $data['articleLink'] = $this->getArticleLink( $rcObj, $rcObj->unpatrolled
, $rcObj->watched
);
614 # Diff and hist links
615 if ( $type != RC_LOG
&& $type != RC_CATEGORIZE
) {
616 $query['action'] = 'history';
617 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
619 $data['separatorAfterLinks'] = ' <span class="mw-changeslist-separator">. .</span> ';
622 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
623 $cd = $this->formatCharacterDifference( $rcObj );
625 $data['characterDiff'] = $cd;
626 $data['separatorAftercharacterDiff'] = ' <span class="mw-changeslist-separator">. .</span> ';
630 if ( $type == RC_LOG
) {
631 $data['logEntry'] = $this->insertLogEntry( $rcObj );
632 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
633 $data['comment'] = $this->insertComment( $rcObj );
635 $data['userLink'] = $rcObj->userlink
;
636 $data['userTalkLink'] = $rcObj->usertalklink
;
637 $data['comment'] = $this->insertComment( $rcObj );
638 if ( $type == RC_CATEGORIZE
) {
639 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
641 $data['rollback'] = $this->getRollback( $rcObj );
645 $data['tags'] = $this->getTags( $rcObj, $classes );
647 # Show how many people are watching this if enabled
648 $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers
);
650 // give the hook a chance to modify the data
651 $success = Hooks
::run( 'EnhancedChangesListModifyBlockLineData',
652 array( $this, &$data, $rcObj ) );
654 // skip entry if hook aborted it
658 $line = Html
::openElement( 'table', array( 'class' => $classes ) ) .
659 Html
::openElement( 'tr' );
660 $line .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
662 if ( isset( $data['recentChangesFlags'] ) ) {
663 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
664 unset( $data['recentChangesFlags'] );
667 if ( isset( $data['timestampLink'] ) ) {
668 $line .= ' ' . $data['timestampLink'];
669 unset( $data['timestampLink'] );
671 $line .= ' </td><td>';
673 // everything else: makes it easier for extensions to add or remove data
674 $line .= implode( '', $data );
676 $line .= "</td></tr></table>\n";
682 * Returns value to be used in 'historyLink' element of $data param in
683 * EnhancedChangesListModifyBlockLineData hook.
687 * @param RCCacheEntry $rc
688 * @param array $query array of key/value pairs to append as a query string
689 * @return string HTML
691 public function getDiffHistLinks( RCCacheEntry
$rc, array $query ) {
692 $pageTitle = $rc->getTitle();
693 if ( $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
) {
694 // For categorizations we must swap the category title with the page title!
695 $pageTitle = Title
::newFromID( $rc->getAttribute( 'rc_cur_id' ) );
698 $retVal = ' ' . $this->msg( 'parentheses' )
699 ->rawParams( $rc->difflink
. $this->message
['pipe-separator'] . Linker
::linkKnown(
701 $this->message
['hist'],
709 * If enhanced RC is in use, this function takes the previously cached
710 * RC lines, arranges them, and outputs the HTML
714 protected function recentChangesBlock() {
715 if ( count( $this->rc_cache
) == 0 ) {
720 foreach ( $this->rc_cache
as $block ) {
721 if ( count( $block ) < 2 ) {
722 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
724 $blockOut .= $this->recentChangesBlockGroup( $block );
728 return '<div>' . $blockOut . '</div>';
732 * Returns text for the end of RC
733 * If enhanced RC is in use, returns pretty much all the text
736 public function endRecentChangesList() {
737 return $this->recentChangesBlock() . '</div>';