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 $baseRC
87 * @param bool $watched
91 public function recentChangesLine( &$baseRC, $watched = false ) {
93 $date = $this->getLanguage()->userDate(
94 $baseRC->mAttribs
['rc_timestamp'],
100 # If it's a new day, add the headline and flush the cache
101 if ( $date != $this->lastdate
) {
102 # Process current cache
103 $ret = $this->recentChangesBlock();
104 $this->rc_cache
= array();
105 $ret .= Xml
::element( 'h4', null, $date ) . "\n";
106 $this->lastdate
= $date;
109 $cacheEntry = $this->cacheEntryFactory
->newFromRecentChange( $baseRC, $watched );
110 $this->addCacheEntry( $cacheEntry );
116 * Put accumulated information into the cache, for later display.
117 * Page moves go on their own line.
119 * @param RCCacheEntry $cacheEntry
121 protected function addCacheEntry( RCCacheEntry
$cacheEntry ) {
122 $cacheGroupingKey = $this->makeCacheGroupingKey( $cacheEntry );
124 if ( !isset( $this->rc_cache
[$cacheGroupingKey] ) ) {
125 $this->rc_cache
[$cacheGroupingKey] = array();
128 array_push( $this->rc_cache
[$cacheGroupingKey], $cacheEntry );
132 * @todo use rc_source to group, if set; fallback to rc_type
134 * @param RCCacheEntry $cacheEntry
138 protected function makeCacheGroupingKey( RCCacheEntry
$cacheEntry ) {
139 $title = $cacheEntry->getTitle();
140 $cacheGroupingKey = $title->getPrefixedDBkey();
142 $type = $cacheEntry->mAttribs
['rc_type'];
144 if ( $type == RC_LOG
) {
146 $cacheGroupingKey = SpecialPage
::getTitleFor(
148 $cacheEntry->mAttribs
['rc_log_type']
149 )->getPrefixedDBkey();
152 return $cacheGroupingKey;
157 * @param RCCacheEntry[] $block
160 protected function recentChangesBlockGroup( $block ) {
162 # Add the namespace and title of the block as part of the class
163 $classes = array( 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' );
164 if ( $block[0]->mAttribs
['rc_log_type'] ) {
166 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-'
167 . $block[0]->mAttribs
['rc_log_type'] );
169 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns'
170 . $block[0]->mAttribs
['rc_namespace'] . '-' . $block[0]->mAttribs
['rc_title'] );
172 $classes[] = $block[0]->watched
&& $block[0]->mAttribs
['rc_timestamp'] >= $block[0]->watched
173 ?
'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
174 $r = Html
::openElement( 'table', array( 'class' => $classes ) ) .
175 Html
::openElement( 'tr' );
177 # Collate list of users
178 $userlinks = array();
180 $unpatrolled = false;
185 # Some catalyst variables...
188 $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
189 foreach ( $block as $rcObj ) {
190 if ( $rcObj->mAttribs
['rc_type'] == RC_NEW
) {
193 // If all log actions to this page were hidden, then don't
194 // give the name of the affected page for this block!
195 if ( !$this->isDeleted( $rcObj, LogPage
::DELETED_ACTION
) ) {
198 $u = $rcObj->userlink
;
199 if ( !isset( $userlinks[$u] ) ) {
202 if ( $rcObj->unpatrolled
) {
205 if ( $rcObj->mAttribs
['rc_type'] != RC_LOG
) {
208 # Get the latest entry with a page_id and oldid
209 # since logs may not have these.
210 if ( !$curId && $rcObj->mAttribs
['rc_cur_id'] ) {
211 $curId = $rcObj->mAttribs
['rc_cur_id'];
214 if ( !$rcObj->mAttribs
['rc_bot'] ) {
217 if ( !$rcObj->mAttribs
['rc_minor'] ) {
224 # Sort the list and convert to text
225 krsort( $userlinks );
228 foreach ( $userlinks as $userlink => $count ) {
230 $text .= $this->getLanguage()->getDirMark();
232 // @todo FIXME: Hardcoded '×'. Should be a message.
233 $formattedCount = $this->getLanguage()->formatNum( $count ) . '×';
234 $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
236 array_push( $users, $text );
239 $users = ' <span class="changedby">'
240 . $this->msg( 'brackets' )->rawParams(
241 implode( $this->message
['semicolon-separator'], $users )
242 )->escaped() . '</span>';
244 $tl = '<span class="mw-collapsible-toggle mw-collapsible-arrow ' .
245 'mw-enhancedchanges-arrow mw-enhancedchanges-arrow-space"></span>';
246 $r .= "<td>$tl</td>";
249 $r .= '<td class="mw-enhanced-rc">' . $this->recentChangesFlags( array(
250 'newpage' => $isnew, # show, when one have this flag
251 'minor' => $allMinors, # show only, when all have this flag
252 'unpatrolled' => $unpatrolled, # show, when one have this flag
253 'bot' => $allBots, # show only, when all have this flag
257 $r .= ' ' . $block[0]->timestamp
. ' </td><td>';
261 $r .= ' <span class="history-deleted">' .
262 $this->msg( 'rev-deleted-event' )->escaped() . '</span>';
263 } elseif ( $allLogs ) {
264 $r .= $this->maybeWatchedLink( $block[0]->link
, $block[0]->watched
);
266 $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled
, $block[0]->watched
);
269 $r .= $this->getLanguage()->getDirMark();
271 $queryParams['curid'] = $curId;
273 $r .= $this->getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden );
275 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
277 # Character difference (does not apply if only log items)
278 if ( $RCShowChangedSize && !$allLogs ) {
280 $first = count( $block ) - 1;
281 # Some events (like logs) have an "empty" size, so we need to skip those...
282 while ( $last < $first && $block[$last]->mAttribs
['rc_new_len'] === null ) {
285 while ( $first > $last && $block[$first]->mAttribs
['rc_old_len'] === null ) {
289 $chardiff = $this->formatCharacterDifference( $block[$first], $block[$last] );
291 if ( $chardiff == '' ) {
294 $r .= ' ' . $chardiff . ' <span class="mw-changeslist-separator">. .</span> ';
299 $r .= $this->numberofWatchingusers( $block[0]->numberofWatchingusers
);
303 foreach ( $block as $rcObj ) {
304 # Classes to apply -- TODO implement
306 $type = $rcObj->mAttribs
['rc_type'];
309 $trClass = $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
310 ?
' class="mw-enhanced-watched"' : '';
311 $separator = ' <span class="mw-changeslist-separator">. .</span> ';
313 $data['recentChangesFlags'] = array(
314 'newpage' => $type == RC_NEW
,
315 'minor' => $rcObj->mAttribs
['rc_minor'],
316 'unpatrolled' => $rcObj->unpatrolled
,
317 'bot' => $rcObj->mAttribs
['rc_bot'],
320 $params = $queryParams;
322 if ( $rcObj->mAttribs
['rc_this_oldid'] != 0 ) {
323 $params['oldid'] = $rcObj->mAttribs
['rc_this_oldid'];
327 if ( $type == RC_LOG
) {
328 $link = $rcObj->timestamp
;
330 } elseif ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
331 $link = '<span class="history-deleted">' . $rcObj->timestamp
. '</span> ';
333 $link = Linker
::linkKnown(
339 if ( $this->isDeleted( $rcObj, Revision
::DELETED_TEXT
) ) {
340 $link = '<span class="history-deleted">' . $link . '</span> ';
343 $data['timestampLink'] = $link;
345 $currentAndLastLinks = '';
346 if ( !$type == RC_LOG ||
$type == RC_NEW
) {
347 $currentAndLastLinks .= ' ' . $this->msg( 'parentheses' )->rawParams(
349 $this->message
['pipe-separator'] .
353 $data['currentAndLastLinks'] = $currentAndLastLinks;
354 $data['separatorAfterCurrentAndLastLinks'] = $separator;
357 if ( $RCShowChangedSize ) {
358 $cd = $this->formatCharacterDifference( $rcObj );
360 $data['characterDiff'] = $cd;
361 $data['separatorAfterCharacterDiff'] = $separator;
365 if ( $rcObj->mAttribs
['rc_type'] == RC_LOG
) {
366 $data['logEntry'] = $this->insertLogEntry( $rcObj );
369 $data['userLink'] = $rcObj->userlink
;
370 $data['userTalkLink'] = $rcObj->usertalklink
;
371 $data['comment'] = $this->insertComment( $rcObj );
375 $data['rollback'] = $this->getRollback( $rcObj );
378 $data['tags'] = $this->getTags( $rcObj, $classes );
380 // give the hook a chance to modify the data
381 $success = Hooks
::run( 'EnhancedChangesListModifyLineData',
382 array( $this, &$data, $block, $rcObj ) );
384 // skip entry if hook aborted it
388 $line = '<tr' . $trClass . '><td></td><td class="mw-enhanced-rc">';
389 if ( isset( $data['recentChangesFlags'] ) ) {
390 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
391 unset( $data['recentChangesFlags'] );
393 $line .= ' </td><td class="mw-enhanced-rc-nested">';
395 if ( isset( $data['timestampLink'] ) ) {
396 $line .= '<span class="mw-enhanced-rc-time">' . $data['timestampLink'] . '</span>';
397 unset( $data['timestampLink'] );
400 // everything else: makes it easier for extensions to add or remove data
401 $line .= implode( '', $data );
403 $line .= "</td></tr>\n";
409 $this->rcCacheIndex++
;
415 * Generates amount of changes (linking to diff ) & link to history.
417 * @param array $block
418 * @param array $queryParams
419 * @param bool $allLogs
421 * @param bool $namehidden
424 protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
426 static $nchanges = array();
427 static $sinceLastVisitMsg = array();
429 $n = count( $block );
430 if ( !isset( $nchanges[$n] ) ) {
431 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
435 $unvisitedOldid = null;
436 /** @var $rcObj RCCacheEntry */
437 foreach ( $block as $rcObj ) {
438 // Same logic as below inside main foreach
439 if ( $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
) {
441 $unvisitedOldid = $rcObj->mAttribs
['rc_last_oldid'];
444 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
445 $sinceLastVisitMsg[$sinceLast] =
446 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
449 $currentRevision = 0;
450 foreach ( $block as $rcObj ) {
451 if ( !$currentRevision ) {
452 $currentRevision = $rcObj->mAttribs
['rc_this_oldid'];
458 /** @var $block0 RecentChange */
460 $last = $block[count( $block ) - 1];
462 if ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
463 $links['total-changes'] = $nchanges[$n];
464 } elseif ( $isnew ) {
465 $links['total-changes'] = $nchanges[$n];
467 $links['total-changes'] = Linker
::link(
471 $queryParams +
array(
472 'diff' => $currentRevision,
473 'oldid' => $last->mAttribs
['rc_last_oldid'],
475 array( 'known', 'noclasses' )
477 if ( $sinceLast > 0 && $sinceLast < $n ) {
478 $links['total-changes-since-last'] = Linker
::link(
480 $sinceLastVisitMsg[$sinceLast],
482 $queryParams +
array(
483 'diff' => $currentRevision,
484 'oldid' => $unvisitedOldid,
486 array( 'known', 'noclasses' )
494 // don't show history link for logs
495 } elseif ( $namehidden ||
!$block0->getTitle()->exists() ) {
496 $links['history'] = $this->message
['enhancedrc-history'];
498 $params = $queryParams;
499 $params['action'] = 'history';
501 $links['history'] = Linker
::linkKnown(
503 $this->message
['enhancedrc-history'],
509 # Allow others to alter, remove or add to these links
510 Hooks
::run( 'EnhancedChangesList::getLogText',
511 array( $this, &$links, $block ) );
517 $logtext = implode( $this->message
['pipe-separator'], $links );
518 $logtext = $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
519 return ' ' . $logtext;
523 * Enhanced RC ungrouped line.
525 * @param RecentChange|RCCacheEntry $rcObj
526 * @return string A HTML formatted line (generated using $r)
528 protected function recentChangesBlockLine( $rcObj ) {
531 $query['curid'] = $rcObj->mAttribs
['rc_cur_id'];
533 $type = $rcObj->mAttribs
['rc_type'];
534 $logType = $rcObj->mAttribs
['rc_log_type'];
535 $classes = array( 'mw-enhanced-rc' );
538 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-' . $logType );
540 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns' .
541 $rcObj->mAttribs
['rc_namespace'] . '-' . $rcObj->mAttribs
['rc_title'] );
543 $classes[] = $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
544 ?
'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
547 $data['recentChangesFlags'] = array(
548 'newpage' => $type == RC_NEW
,
549 'minor' => $rcObj->mAttribs
['rc_minor'],
550 'unpatrolled' => $rcObj->unpatrolled
,
551 'bot' => $rcObj->mAttribs
['rc_bot'],
553 // timestamp is not really a link here, but is called timestampLink
554 // for consistency with EnhancedChangesListModifyLineData
555 $data['timestampLink'] = $rcObj->timestamp
;
557 # Article or log link
559 $logPage = new LogPage( $logType );
560 $logTitle = SpecialPage
::getTitleFor( 'Log', $logType );
561 $logName = $logPage->getName()->escaped();
562 $data['logLink'] = $this->msg( 'parentheses' )
563 ->rawParams( Linker
::linkKnown( $logTitle, $logName ) )->escaped();
565 $data['articleLink'] = $this->getArticleLink( $rcObj, $rcObj->unpatrolled
, $rcObj->watched
);
568 # Diff and hist links
569 if ( $type != RC_LOG
) {
570 $query['action'] = 'history';
571 $data['historyLink'] = ' ' . $this->msg( 'parentheses' )
572 ->rawParams( $rcObj->difflink
. $this->message
['pipe-separator'] . Linker
::linkKnown(
574 $this->message
['hist'],
579 $data['separatorAfterLinks'] = ' <span class="mw-changeslist-separator">. .</span> ';
582 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
583 $cd = $this->formatCharacterDifference( $rcObj );
585 $data['characterDiff'] = $cd;
586 $data['separatorAftercharacterDiff'] = ' <span class="mw-changeslist-separator">. .</span> ';
590 if ( $type == RC_LOG
) {
591 $data['logEntry'] = $this->insertLogEntry( $rcObj );
593 $data['userLink'] = $rcObj->userlink
;
594 $data['userTalkLink'] = $rcObj->usertalklink
;
595 $data['comment'] = $this->insertComment( $rcObj );
596 $data['rollback'] = $this->getRollback( $rcObj );
600 $data['tags'] = $this->getTags( $rcObj, $classes );
602 # Show how many people are watching this if enabled
603 $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers
);
605 // give the hook a chance to modify the data
606 $success = Hooks
::run( 'EnhancedChangesListModifyBlockLineData',
607 array( $this, &$data, $rcObj ) );
609 // skip entry if hook aborted it
613 $line = Html
::openElement( 'table', array( 'class' => $classes ) ) .
614 Html
::openElement( 'tr' );
615 $line .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
617 if ( isset( $data['recentChangesFlags'] ) ) {
618 $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
619 unset( $data['recentChangesFlags'] );
622 if ( isset( $data['timestampLink'] ) ) {
623 $line .= ' ' . $data['timestampLink'];
624 unset( $data['timestampLink'] );
626 $line .= ' </td><td>';
628 // everything else: makes it easier for extensions to add or remove data
629 $line .= implode( '', $data );
631 $line .= "</td></tr></table>\n";
637 * If enhanced RC is in use, this function takes the previously cached
638 * RC lines, arranges them, and outputs the HTML
642 protected function recentChangesBlock() {
643 if ( count( $this->rc_cache
) == 0 ) {
648 foreach ( $this->rc_cache
as $block ) {
649 if ( count( $block ) < 2 ) {
650 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
652 $blockOut .= $this->recentChangesBlockGroup( $block );
656 return '<div>' . $blockOut . '</div>';
660 * Returns text for the end of RC
661 * If enhanced RC is in use, returns pretty much all the text
664 public function endRecentChangesList() {
665 return $this->recentChangesBlock() . '</div>';