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'];
308 $trClass = $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
309 ?
' class="mw-enhanced-watched"' : '';
311 $r .= '<tr' . $trClass . '><td></td><td class="mw-enhanced-rc">';
312 $r .= $this->recentChangesFlags( array(
313 'newpage' => $type == RC_NEW
,
314 'minor' => $rcObj->mAttribs
['rc_minor'],
315 'unpatrolled' => $rcObj->unpatrolled
,
316 'bot' => $rcObj->mAttribs
['rc_bot'],
318 $r .= ' </td><td class="mw-enhanced-rc-nested"><span class="mw-enhanced-rc-time">';
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 $r .= $link . '</span>';
345 if ( !$type == RC_LOG ||
$type == RC_NEW
) {
346 $r .= ' ' . $this->msg( 'parentheses' )->rawParams(
348 $this->message
['pipe-separator'] .
352 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
355 if ( $RCShowChangedSize ) {
356 $cd = $this->formatCharacterDifference( $rcObj );
358 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
362 if ( $rcObj->mAttribs
['rc_type'] == RC_LOG
) {
363 $r .= $this->insertLogEntry( $rcObj );
366 $r .= $rcObj->userlink
;
367 $r .= $rcObj->usertalklink
;
368 $r .= $this->insertComment( $rcObj );
372 $this->insertRollback( $r, $rcObj );
374 $this->insertTags( $r, $rcObj, $classes );
376 $r .= "</td></tr>\n";
380 $this->rcCacheIndex++
;
386 * Generates amount of changes (linking to diff ) & link to history.
388 * @param array $block
389 * @param array $queryParams
390 * @param bool $allLogs
392 * @param bool $namehidden
395 protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
397 static $nchanges = array();
398 static $sinceLastVisitMsg = array();
400 $n = count( $block );
401 if ( !isset( $nchanges[$n] ) ) {
402 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
406 $unvisitedOldid = null;
407 /** @var $rcObj RCCacheEntry */
408 foreach ( $block as $rcObj ) {
409 // Same logic as below inside main foreach
410 if ( $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
) {
412 $unvisitedOldid = $rcObj->mAttribs
['rc_last_oldid'];
415 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
416 $sinceLastVisitMsg[$sinceLast] =
417 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
420 $currentRevision = 0;
421 foreach ( $block as $rcObj ) {
422 if ( !$currentRevision ) {
423 $currentRevision = $rcObj->mAttribs
['rc_this_oldid'];
429 /** @var $block0 RecentChange */
431 $last = $block[count( $block ) - 1];
433 if ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
434 $links['total-changes'] = $nchanges[$n];
435 } elseif ( $isnew ) {
436 $links['total-changes'] = $nchanges[$n];
438 $links['total-changes'] = Linker
::link(
442 $queryParams +
array(
443 'diff' => $currentRevision,
444 'oldid' => $last->mAttribs
['rc_last_oldid'],
446 array( 'known', 'noclasses' )
448 if ( $sinceLast > 0 && $sinceLast < $n ) {
449 $links['total-changes-since-last'] = Linker
::link(
451 $sinceLastVisitMsg[$sinceLast],
453 $queryParams +
array(
454 'diff' => $currentRevision,
455 'oldid' => $unvisitedOldid,
457 array( 'known', 'noclasses' )
465 // don't show history link for logs
466 } elseif ( $namehidden ||
!$block0->getTitle()->exists() ) {
467 $links['history'] = $this->message
['enhancedrc-history'];
469 $params = $queryParams;
470 $params['action'] = 'history';
472 $links['history'] = Linker
::linkKnown(
474 $this->message
['enhancedrc-history'],
480 # Allow others to alter, remove or add to these links
481 Hooks
::run( 'EnhancedChangesList::getLogText',
482 array( $this, &$links, $block ) );
488 $logtext = implode( $this->message
['pipe-separator'], $links );
489 $logtext = $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
490 return ' ' . $logtext;
494 * Enhanced RC ungrouped line.
496 * @param RecentChange|RCCacheEntry $rcObj
497 * @return string A HTML formatted line (generated using $r)
499 protected function recentChangesBlockLine( $rcObj ) {
500 $query['curid'] = $rcObj->mAttribs
['rc_cur_id'];
502 $type = $rcObj->mAttribs
['rc_type'];
503 $logType = $rcObj->mAttribs
['rc_log_type'];
504 $classes = array( 'mw-enhanced-rc' );
507 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-' . $logType );
509 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns' .
510 $rcObj->mAttribs
['rc_namespace'] . '-' . $rcObj->mAttribs
['rc_title'] );
512 $classes[] = $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
513 ?
'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
514 $r = Html
::openElement( 'table', array( 'class' => $classes ) ) .
515 Html
::openElement( 'tr' );
517 $r .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
519 $r .= $this->recentChangesFlags( array(
520 'newpage' => $type == RC_NEW
,
521 'minor' => $rcObj->mAttribs
['rc_minor'],
522 'unpatrolled' => $rcObj->unpatrolled
,
523 'bot' => $rcObj->mAttribs
['rc_bot'],
525 $r .= ' ' . $rcObj->timestamp
. ' </td><td>';
526 # Article or log link
528 $logPage = new LogPage( $logType );
529 $logTitle = SpecialPage
::getTitleFor( 'Log', $logType );
530 $logName = $logPage->getName()->escaped();
531 $r .= $this->msg( 'parentheses' )
532 ->rawParams( Linker
::linkKnown( $logTitle, $logName ) )->escaped();
534 $this->insertArticleLink( $r, $rcObj, $rcObj->unpatrolled
, $rcObj->watched
);
536 # Diff and hist links
537 if ( $type != RC_LOG
) {
538 $query['action'] = 'history';
539 $r .= ' ' . $this->msg( 'parentheses' )
540 ->rawParams( $rcObj->difflink
. $this->message
['pipe-separator'] . Linker
::linkKnown(
542 $this->message
['hist'],
547 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
549 if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
550 $cd = $this->formatCharacterDifference( $rcObj );
552 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
556 if ( $type == RC_LOG
) {
557 $r .= $this->insertLogEntry( $rcObj );
559 $r .= ' ' . $rcObj->userlink
. $rcObj->usertalklink
;
560 $r .= $this->insertComment( $rcObj );
561 $this->insertRollback( $r, $rcObj );
565 $this->insertTags( $r, $rcObj, $classes );
566 # Show how many people are watching this if enabled
567 $r .= $this->numberofWatchingusers( $rcObj->numberofWatchingusers
);
569 $r .= "</td></tr></table>\n";
575 * If enhanced RC is in use, this function takes the previously cached
576 * RC lines, arranges them, and outputs the HTML
580 protected function recentChangesBlock() {
581 if ( count( $this->rc_cache
) == 0 ) {
586 foreach ( $this->rc_cache
as $block ) {
587 if ( count( $block ) < 2 ) {
588 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
590 $blockOut .= $this->recentChangesBlockGroup( $block );
594 return '<div>' . $blockOut . '</div>';
598 * Returns text for the end of RC
599 * If enhanced RC is in use, returns pretty much all the text
602 public function endRecentChangesList() {
603 return $this->recentChangesBlock() . '</div>';