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();
181 $unpatrolled = false;
186 # Some catalyst variables...
189 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
190 foreach ( $block as $rcObj ) {
191 if ( $rcObj->mAttribs
['rc_type'] == RC_NEW
) {
194 // If all log actions to this page were hidden, then don't
195 // give the name of the affected page for this block!
196 if ( !$this->isDeleted( $rcObj, LogPage
::DELETED_ACTION
) ) {
199 $u = $rcObj->userlink
;
200 if ( !isset( $userlinks[$u] ) ) {
203 if ( $rcObj->unpatrolled
) {
206 if ( $rcObj->mAttribs
['rc_type'] != RC_LOG
) {
209 # Get the latest entry with a page_id and oldid
210 # since logs may not have these.
211 if ( !$curId && $rcObj->mAttribs
['rc_cur_id'] ) {
212 $curId = $rcObj->mAttribs
['rc_cur_id'];
215 if ( !$rcObj->mAttribs
['rc_bot'] ) {
218 if ( !$rcObj->mAttribs
['rc_minor'] ) {
225 # Sort the list and convert to text
226 krsort( $userlinks );
229 foreach ( $userlinks as $userlink => $count ) {
231 $text .= $this->getLanguage()->getDirMark();
233 // @todo FIXME: Hardcoded '×'. Should be a message.
234 $formattedCount = $this->getLanguage()->formatNum( $count ) . '×';
235 $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
237 array_push( $users, $text );
240 $users = ' <span class="changedby">'
241 . $this->msg( 'brackets' )->rawParams(
242 implode( $this->message
['semicolon-separator'], $users )
243 )->escaped() . '</span>';
245 $tl = '<span class="mw-collapsible-toggle mw-collapsible-arrow ' .
246 'mw-enhancedchanges-arrow mw-enhancedchanges-arrow-space"></span>';
247 $r .= "<td>$tl</td>";
250 $r .= '<td class="mw-enhanced-rc">' . $this->recentChangesFlags( array(
251 'newpage' => $isnew, # show, when one have this flag
252 'minor' => $allMinors, # show only, when all have this flag
253 'unpatrolled' => $unpatrolled, # show, when one have this flag
254 'bot' => $allBots, # show only, when all have this flag
258 $r .= ' ' . $block[0]->timestamp
. ' </td><td>';
262 $r .= ' <span class="history-deleted">' .
263 $this->msg( 'rev-deleted-event' )->escaped() . '</span>';
264 } elseif ( $allLogs ) {
265 $r .= $this->maybeWatchedLink( $block[0]->link
, $block[0]->watched
);
267 $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled
, $block[0]->watched
);
270 $r .= $this->getLanguage()->getDirMark();
272 $queryParams['curid'] = $curId;
276 foreach ( $block as $i => $rcObj ) {
277 $line = $this->getLineData( $block, $rcObj, $queryParams );
280 // completely ignore this RC entry if we don't want to render it
284 // Further down are some assumptions that $block is a 0-indexed array
285 // with (count-1) as last key. Let's make sure it is.
286 $block = array_values( $block );
287 if ( empty( $block ) ) {
288 // if we can't show anything, don't display this block altogether
292 $r .= $this->getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden );
294 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
296 # Character difference (does not apply if only log items)
297 if ( $RCShowChangedSize && !$allLogs ) {
299 $first = count( $block ) - 1;
300 # Some events (like logs) have an "empty" size, so we need to skip those...
301 while ( $last < $first && $block[$last]->mAttribs
['rc_new_len'] === null ) {
304 while ( $first > $last && $block[$first]->mAttribs
['rc_old_len'] === null ) {
308 $chardiff = $this->formatCharacterDifference( $block[$first], $block[$last] );
310 if ( $chardiff == '' ) {
313 $r .= ' ' . $chardiff . ' <span class="mw-changeslist-separator">. .</span> ';
318 $r .= $this->numberofWatchingusers( $block[0]->numberofWatchingusers
);
322 // if there are no lines to be rendered (all aborted by hook), don't render the block
329 $this->rcCacheIndex++
;
335 * @param RCCacheEntry[] $block
336 * @param RCCacheEntry $rcObj
337 * @param array $queryParams
341 * @throws MWException
343 protected function getLineData( array $block, RCCacheEntry
$rcObj, array $queryParams = array() ) {
344 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
346 # Classes to apply -- TODO implement
348 $type = $rcObj->mAttribs
['rc_type'];
351 $trClass = $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
352 ?
' class="mw-enhanced-watched"' : '';
353 $separator = ' <span class="mw-changeslist-separator">. .</span> ';
355 $data['recentChangesFlags'] = array(
356 'newpage' => $type == RC_NEW
,
357 'minor' => $rcObj->mAttribs
['rc_minor'],
358 'unpatrolled' => $rcObj->unpatrolled
,
359 'bot' => $rcObj->mAttribs
['rc_bot'],
362 $params = $queryParams;
364 if ( $rcObj->mAttribs
['rc_this_oldid'] != 0 ) {
365 $params['oldid'] = $rcObj->mAttribs
['rc_this_oldid'];
369 if ( $type == RC_LOG
) {
370 $link = $rcObj->timestamp
;
372 } elseif ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
373 $link = '<span class="history-deleted">' . $rcObj->timestamp
. '</span> ';
375 $link = Linker
::linkKnown(
381 if ( $this->isDeleted( $rcObj, Revision
::DELETED_TEXT
) ) {
382 $link = '<span class="history-deleted">' . $link . '</span> ';
385 $data['timestampLink'] = $link;
387 $currentAndLastLinks = '';
388 if ( !$type == RC_LOG ||
$type == RC_NEW
) {
389 $currentAndLastLinks .= ' ' . $this->msg( 'parentheses' )->rawParams(
391 $this->message
['pipe-separator'] .
395 $data['currentAndLastLinks'] = $currentAndLastLinks;
396 $data['separatorAfterCurrentAndLastLinks'] = $separator;
399 if ( $RCShowChangedSize ) {
400 $cd = $this->formatCharacterDifference( $rcObj );
402 $data['characterDiff'] = $cd;
403 $data['separatorAfterCharacterDiff'] = $separator;
407 if ( $rcObj->mAttribs
['rc_type'] == RC_LOG
) {
408 $data['logEntry'] = $this->insertLogEntry( $rcObj );
409 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
410 $data['comment'] = $this->insertComment( $rcObj );
413 $data['userLink'] = $rcObj->userlink
;
414 $data['userTalkLink'] = $rcObj->usertalklink
;
415 $data['comment'] = $this->insertComment( $rcObj );
419 $data['rollback'] = $this->getRollback( $rcObj );
422 $data['tags'] = $this->getTags( $rcObj, $classes );
424 // give the hook a chance to modify the data
425 $success = Hooks
::run( 'EnhancedChangesListModifyLineData',
426 array( $this, &$data, $block, $rcObj ) );
428 // skip entry if hook aborted it
432 $line = '<tr' . $trClass . '><td></td><td class="mw-enhanced-rc">';
433 if ( isset( $data['recentChangesFlags'] ) ) {
434 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
435 unset( $data['recentChangesFlags'] );
437 $line .= ' </td><td class="mw-enhanced-rc-nested">';
439 if ( isset( $data['timestampLink'] ) ) {
440 $line .= '<span class="mw-enhanced-rc-time">' . $data['timestampLink'] . '</span>';
441 unset( $data['timestampLink'] );
444 // everything else: makes it easier for extensions to add or remove data
445 $line .= implode( '', $data );
447 $line .= "</td></tr>\n";
453 * Generates amount of changes (linking to diff ) & link to history.
455 * @param array $block
456 * @param array $queryParams
457 * @param bool $allLogs
459 * @param bool $namehidden
462 protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
463 if ( empty( $block ) ) {
468 static $nchanges = array();
469 static $sinceLastVisitMsg = array();
471 $n = count( $block );
472 if ( !isset( $nchanges[$n] ) ) {
473 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
477 $unvisitedOldid = null;
478 /** @var $rcObj RCCacheEntry */
479 foreach ( $block as $rcObj ) {
480 // Same logic as below inside main foreach
481 if ( $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
) {
483 $unvisitedOldid = $rcObj->mAttribs
['rc_last_oldid'];
486 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
487 $sinceLastVisitMsg[$sinceLast] =
488 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
491 $currentRevision = 0;
492 foreach ( $block as $rcObj ) {
493 if ( !$currentRevision ) {
494 $currentRevision = $rcObj->mAttribs
['rc_this_oldid'];
500 /** @var $block0 RecentChange */
502 $last = $block[count( $block ) - 1];
503 if ( !$allLogs && $rcObj->mAttribs
['rc_type'] != RC_CATEGORIZE
) {
504 if ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
505 $links['total-changes'] = $nchanges[$n];
506 } elseif ( $isnew ) {
507 $links['total-changes'] = $nchanges[$n];
509 $links['total-changes'] = Linker
::link(
513 $queryParams +
array(
514 'diff' => $currentRevision,
515 'oldid' => $last->mAttribs
['rc_last_oldid'],
517 array( 'known', 'noclasses' )
519 if ( $sinceLast > 0 && $sinceLast < $n ) {
520 $links['total-changes-since-last'] = Linker
::link(
522 $sinceLastVisitMsg[$sinceLast],
524 $queryParams +
array(
525 'diff' => $currentRevision,
526 'oldid' => $unvisitedOldid,
528 array( 'known', 'noclasses' )
535 if ( $allLogs ||
$rcObj->mAttribs
['rc_type'] == RC_CATEGORIZE
) {
536 // don't show history link for logs
537 } elseif ( $namehidden ||
!$block0->getTitle()->exists() ) {
538 $links['history'] = $this->message
['enhancedrc-history'];
540 $params = $queryParams;
541 $params['action'] = 'history';
543 $links['history'] = Linker
::linkKnown(
545 $this->message
['enhancedrc-history'],
551 # Allow others to alter, remove or add to these links
552 Hooks
::run( 'EnhancedChangesList::getLogText',
553 array( $this, &$links, $block ) );
559 $logtext = implode( $this->message
['pipe-separator'], $links );
560 $logtext = $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
561 return ' ' . $logtext;
565 * Enhanced RC ungrouped line.
567 * @param RecentChange|RCCacheEntry $rcObj
568 * @return string A HTML formatted line (generated using $r)
570 protected function recentChangesBlockLine( $rcObj ) {
573 $query['curid'] = $rcObj->mAttribs
['rc_cur_id'];
575 $type = $rcObj->mAttribs
['rc_type'];
576 $logType = $rcObj->mAttribs
['rc_log_type'];
577 $classes = array( 'mw-enhanced-rc' );
580 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-' . $logType );
582 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns' .
583 $rcObj->mAttribs
['rc_namespace'] . '-' . $rcObj->mAttribs
['rc_title'] );
585 $classes[] = $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
586 ?
'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
589 $data['recentChangesFlags'] = array(
590 'newpage' => $type == RC_NEW
,
591 'minor' => $rcObj->mAttribs
['rc_minor'],
592 'unpatrolled' => $rcObj->unpatrolled
,
593 'bot' => $rcObj->mAttribs
['rc_bot'],
595 // timestamp is not really a link here, but is called timestampLink
596 // for consistency with EnhancedChangesListModifyLineData
597 $data['timestampLink'] = $rcObj->timestamp
;
599 # Article or log link
601 $logPage = new LogPage( $logType );
602 $logTitle = SpecialPage
::getTitleFor( 'Log', $logType );
603 $logName = $logPage->getName()->escaped();
604 $data['logLink'] = $this->msg( 'parentheses' )
605 ->rawParams( Linker
::linkKnown( $logTitle, $logName ) )->escaped();
607 $data['articleLink'] = $this->getArticleLink( $rcObj, $rcObj->unpatrolled
, $rcObj->watched
);
610 # Diff and hist links
611 if ( $type != RC_LOG
&& $type != RC_CATEGORIZE
) {
612 $query['action'] = 'history';
613 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
615 $data['separatorAfterLinks'] = ' <span class="mw-changeslist-separator">. .</span> ';
618 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
619 $cd = $this->formatCharacterDifference( $rcObj );
621 $data['characterDiff'] = $cd;
622 $data['separatorAftercharacterDiff'] = ' <span class="mw-changeslist-separator">. .</span> ';
626 if ( $type == RC_LOG
) {
627 $data['logEntry'] = $this->insertLogEntry( $rcObj );
628 } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
629 $data['comment'] = $this->insertComment( $rcObj );
631 $data['userLink'] = $rcObj->userlink
;
632 $data['userTalkLink'] = $rcObj->usertalklink
;
633 $data['comment'] = $this->insertComment( $rcObj );
634 if ( $type == RC_CATEGORIZE
) {
635 $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query );
637 $data['rollback'] = $this->getRollback( $rcObj );
641 $data['tags'] = $this->getTags( $rcObj, $classes );
643 # Show how many people are watching this if enabled
644 $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers
);
646 // give the hook a chance to modify the data
647 $success = Hooks
::run( 'EnhancedChangesListModifyBlockLineData',
648 array( $this, &$data, $rcObj ) );
650 // skip entry if hook aborted it
654 $line = Html
::openElement( 'table', array( 'class' => $classes ) ) .
655 Html
::openElement( 'tr' );
656 $line .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
658 if ( isset( $data['recentChangesFlags'] ) ) {
659 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
660 unset( $data['recentChangesFlags'] );
663 if ( isset( $data['timestampLink'] ) ) {
664 $line .= ' ' . $data['timestampLink'];
665 unset( $data['timestampLink'] );
667 $line .= ' </td><td>';
669 // everything else: makes it easier for extensions to add or remove data
670 $line .= implode( '', $data );
672 $line .= "</td></tr></table>\n";
678 * Returns value to be used in 'historyLink' element of $data param in
679 * EnhancedChangesListModifyBlockLineData hook.
683 * @param RCCacheEntry $rc
684 * @param array $query array of key/value pairs to append as a query string
685 * @return string HTML
687 public function getDiffHistLinks( RCCacheEntry
$rc, array $query ) {
688 $pageTitle = $rc->getTitle();
689 if ( $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE
) {
690 // For categorizations we must swap the category title with the page title!
691 $pageTitle = Title
::newFromID( $rc->getAttribute( 'rc_cur_id' ) );
694 $retVal = ' ' . $this->msg( 'parentheses' )
695 ->rawParams( $rc->difflink
. $this->message
['pipe-separator'] . Linker
::linkKnown(
697 $this->message
['hist'],
705 * If enhanced RC is in use, this function takes the previously cached
706 * RC lines, arranges them, and outputs the HTML
710 protected function recentChangesBlock() {
711 if ( count( $this->rc_cache
) == 0 ) {
716 foreach ( $this->rc_cache
as $block ) {
717 if ( count( $block ) < 2 ) {
718 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
720 $blockOut .= $this->recentChangesBlockGroup( $block );
724 return '<div>' . $blockOut . '</div>';
728 * Returns text for the end of RC
729 * If enhanced RC is in use, returns pretty much all the text
732 public function endRecentChangesList() {
733 return $this->recentChangesBlock() . '</div>';