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 );
411 $data['userLink'] = $rcObj->userlink
;
412 $data['userTalkLink'] = $rcObj->usertalklink
;
413 $data['comment'] = $this->insertComment( $rcObj );
417 $data['rollback'] = $this->getRollback( $rcObj );
420 $data['tags'] = $this->getTags( $rcObj, $classes );
422 // give the hook a chance to modify the data
423 $success = Hooks
::run( 'EnhancedChangesListModifyLineData',
424 array( $this, &$data, $block, $rcObj ) );
426 // skip entry if hook aborted it
430 $line = '<tr' . $trClass . '><td></td><td class="mw-enhanced-rc">';
431 if ( isset( $data['recentChangesFlags'] ) ) {
432 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
433 unset( $data['recentChangesFlags'] );
435 $line .= ' </td><td class="mw-enhanced-rc-nested">';
437 if ( isset( $data['timestampLink'] ) ) {
438 $line .= '<span class="mw-enhanced-rc-time">' . $data['timestampLink'] . '</span>';
439 unset( $data['timestampLink'] );
442 // everything else: makes it easier for extensions to add or remove data
443 $line .= implode( '', $data );
445 $line .= "</td></tr>\n";
451 * Generates amount of changes (linking to diff ) & link to history.
453 * @param array $block
454 * @param array $queryParams
455 * @param bool $allLogs
457 * @param bool $namehidden
460 protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
461 if ( empty( $block ) ) {
466 static $nchanges = array();
467 static $sinceLastVisitMsg = array();
469 $n = count( $block );
470 if ( !isset( $nchanges[$n] ) ) {
471 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
475 $unvisitedOldid = null;
476 /** @var $rcObj RCCacheEntry */
477 foreach ( $block as $rcObj ) {
478 // Same logic as below inside main foreach
479 if ( $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
) {
481 $unvisitedOldid = $rcObj->mAttribs
['rc_last_oldid'];
484 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
485 $sinceLastVisitMsg[$sinceLast] =
486 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
489 $currentRevision = 0;
490 foreach ( $block as $rcObj ) {
491 if ( !$currentRevision ) {
492 $currentRevision = $rcObj->mAttribs
['rc_this_oldid'];
498 /** @var $block0 RecentChange */
500 $last = $block[count( $block ) - 1];
502 if ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
503 $links['total-changes'] = $nchanges[$n];
504 } elseif ( $isnew ) {
505 $links['total-changes'] = $nchanges[$n];
507 $links['total-changes'] = Linker
::link(
511 $queryParams +
array(
512 'diff' => $currentRevision,
513 'oldid' => $last->mAttribs
['rc_last_oldid'],
515 array( 'known', 'noclasses' )
517 if ( $sinceLast > 0 && $sinceLast < $n ) {
518 $links['total-changes-since-last'] = Linker
::link(
520 $sinceLastVisitMsg[$sinceLast],
522 $queryParams +
array(
523 'diff' => $currentRevision,
524 'oldid' => $unvisitedOldid,
526 array( 'known', 'noclasses' )
534 // don't show history link for logs
535 } elseif ( $namehidden ||
!$block0->getTitle()->exists() ) {
536 $links['history'] = $this->message
['enhancedrc-history'];
538 $params = $queryParams;
539 $params['action'] = 'history';
541 $links['history'] = Linker
::linkKnown(
543 $this->message
['enhancedrc-history'],
549 # Allow others to alter, remove or add to these links
550 Hooks
::run( 'EnhancedChangesList::getLogText',
551 array( $this, &$links, $block ) );
557 $logtext = implode( $this->message
['pipe-separator'], $links );
558 $logtext = $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
559 return ' ' . $logtext;
563 * Enhanced RC ungrouped line.
565 * @param RecentChange|RCCacheEntry $rcObj
566 * @return string A HTML formatted line (generated using $r)
568 protected function recentChangesBlockLine( $rcObj ) {
571 $query['curid'] = $rcObj->mAttribs
['rc_cur_id'];
573 $type = $rcObj->mAttribs
['rc_type'];
574 $logType = $rcObj->mAttribs
['rc_log_type'];
575 $classes = array( 'mw-enhanced-rc' );
578 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-' . $logType );
580 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns' .
581 $rcObj->mAttribs
['rc_namespace'] . '-' . $rcObj->mAttribs
['rc_title'] );
583 $classes[] = $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
584 ?
'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
587 $data['recentChangesFlags'] = array(
588 'newpage' => $type == RC_NEW
,
589 'minor' => $rcObj->mAttribs
['rc_minor'],
590 'unpatrolled' => $rcObj->unpatrolled
,
591 'bot' => $rcObj->mAttribs
['rc_bot'],
593 // timestamp is not really a link here, but is called timestampLink
594 // for consistency with EnhancedChangesListModifyLineData
595 $data['timestampLink'] = $rcObj->timestamp
;
597 # Article or log link
599 $logPage = new LogPage( $logType );
600 $logTitle = SpecialPage
::getTitleFor( 'Log', $logType );
601 $logName = $logPage->getName()->escaped();
602 $data['logLink'] = $this->msg( 'parentheses' )
603 ->rawParams( Linker
::linkKnown( $logTitle, $logName ) )->escaped();
605 $data['articleLink'] = $this->getArticleLink( $rcObj, $rcObj->unpatrolled
, $rcObj->watched
);
608 # Diff and hist links
609 if ( $type != RC_LOG
) {
610 $query['action'] = 'history';
611 $data['historyLink'] = ' ' . $this->msg( 'parentheses' )
612 ->rawParams( $rcObj->difflink
. $this->message
['pipe-separator'] . Linker
::linkKnown(
614 $this->message
['hist'],
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 );
633 $data['userLink'] = $rcObj->userlink
;
634 $data['userTalkLink'] = $rcObj->usertalklink
;
635 $data['comment'] = $this->insertComment( $rcObj );
636 $data['rollback'] = $this->getRollback( $rcObj );
640 $data['tags'] = $this->getTags( $rcObj, $classes );
642 # Show how many people are watching this if enabled
643 $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers
);
645 // give the hook a chance to modify the data
646 $success = Hooks
::run( 'EnhancedChangesListModifyBlockLineData',
647 array( $this, &$data, $rcObj ) );
649 // skip entry if hook aborted it
653 $line = Html
::openElement( 'table', array( 'class' => $classes ) ) .
654 Html
::openElement( 'tr' );
655 $line .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
657 if ( isset( $data['recentChangesFlags'] ) ) {
658 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
659 unset( $data['recentChangesFlags'] );
662 if ( isset( $data['timestampLink'] ) ) {
663 $line .= ' ' . $data['timestampLink'];
664 unset( $data['timestampLink'] );
666 $line .= ' </td><td>';
668 // everything else: makes it easier for extensions to add or remove data
669 $line .= implode( '', $data );
671 $line .= "</td></tr></table>\n";
677 * If enhanced RC is in use, this function takes the previously cached
678 * RC lines, arranges them, and outputs the HTML
682 protected function recentChangesBlock() {
683 if ( count( $this->rc_cache
) == 0 ) {
688 foreach ( $this->rc_cache
as $block ) {
689 if ( count( $block ) < 2 ) {
690 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
692 $blockOut .= $this->recentChangesBlockGroup( $block );
696 return '<div>' . $blockOut . '</div>';
700 * Returns text for the end of RC
701 * If enhanced RC is in use, returns pretty much all the text
704 public function endRecentChangesList() {
705 return $this->recentChangesBlock() . '</div>';