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
38 public function __construct( $obj ) {
39 if ( $obj instanceof Skin
) {
40 // @todo: deprecate constructing with Skin
41 $context = $obj->getContext();
43 if ( ! $obj instanceof IContextSource
) {
44 throw new MWException( 'EnhancedChangesList must be constructed with a '
45 . 'context source or skin.' );
51 parent
::__construct( $context );
53 // message is set by the parent ChangesList class
54 $this->cacheEntryFactory
= new RCCacheEntryFactory(
61 * Add the JavaScript file for enhanced changeslist
63 * @param ResultWrapper $res
66 public function beginRecentChangesList( $res ) {
67 $this->rc_cache
= array();
68 $this->rcMoveIndex
= 0;
69 $this->rcCacheIndex
= 0;
71 $this->rclistOpen
= false;
72 $this->getOutput()->addModuleStyles( array(
73 'mediawiki.special.changeslist',
74 'mediawiki.special.changeslist.enhanced',
76 $this->getOutput()->addModules( array(
77 'jquery.makeCollapsible',
81 wfRunHooks( 'EnhancedChangesListBegin', array( $this, $res ) );
83 return '<div class="mw-changeslist">';
87 * Format a line for enhanced recentchange (aka with javascript and block of lines).
89 * @param RecentChange $baseRC
90 * @param bool $watched
94 public function recentChangesLine( &$baseRC, $watched = false ) {
95 wfProfileIn( __METHOD__
);
97 # If it's a new day, add the headline and flush the cache
98 $date = $this->getLanguage()->userDate(
99 $baseRC->mAttribs
['rc_timestamp'],
105 if ( $date != $this->lastdate
) {
106 # Process current cache
107 $ret = $this->recentChangesBlock();
108 $this->rc_cache
= array();
109 $ret .= Xml
::element( 'h4', null, $date ) . "\n";
110 $this->lastdate
= $date;
113 $cacheEntry = $this->cacheEntryFactory
->newFromRecentChange( $baseRC, $watched );
115 # Put accumulated information into the cache, for later display
116 # Page moves go on their own line
117 $title = $cacheEntry->getTitle();
118 $secureName = $title->getPrefixedDBkey();
120 $type = $cacheEntry->mAttribs
['rc_type'];
122 if ( $type == RC_MOVE ||
$type == RC_MOVE_OVER_REDIRECT
) {
123 # Use an @ character to prevent collision with page names
124 $this->rc_cache
['@@' . ( $this->rcMoveIndex++
)] = array( $cacheEntry );
126 # Logs are grouped by type
127 if ( $type == RC_LOG
) {
128 $secureName = SpecialPage
::getTitleFor(
130 $cacheEntry->mAttribs
['rc_log_type']
131 )->getPrefixedDBkey();
133 if ( !isset( $this->rc_cache
[$secureName] ) ) {
134 $this->rc_cache
[$secureName] = array();
137 array_push( $this->rc_cache
[$secureName], $cacheEntry );
140 wfProfileOut( __METHOD__
);
147 * @param RCCacheEntry[] $block
150 protected function recentChangesBlockGroup( $block ) {
151 global $wgRCShowChangedSize;
153 wfProfileIn( __METHOD__
);
155 # Add the namespace and title of the block as part of the class
156 $classes = array( 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc' );
157 if ( $block[0]->mAttribs
['rc_log_type'] ) {
159 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-'
160 . $block[0]->mAttribs
['rc_log_type'] );
162 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns'
163 . $block[0]->mAttribs
['rc_namespace'] . '-' . $block[0]->mAttribs
['rc_title'] );
165 $classes[] = $block[0]->watched
&& $block[0]->mAttribs
['rc_timestamp'] >= $block[0]->watched
166 ?
'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
167 $r = Html
::openElement( 'table', array( 'class' => $classes ) ) .
168 Html
::openElement( 'tr' );
170 # Collate list of users
171 $userlinks = array();
173 $unpatrolled = false;
177 $curId = $currentRevision = 0;
178 # Some catalyst variables...
182 foreach ( $block as $rcObj ) {
183 $oldid = $rcObj->mAttribs
['rc_last_oldid'];
184 if ( $rcObj->mAttribs
['rc_type'] == RC_NEW
) {
187 // If all log actions to this page were hidden, then don't
188 // give the name of the affected page for this block!
189 if ( !$this->isDeleted( $rcObj, LogPage
::DELETED_ACTION
) ) {
192 $u = $rcObj->userlink
;
193 if ( !isset( $userlinks[$u] ) ) {
196 if ( $rcObj->unpatrolled
) {
199 if ( $rcObj->mAttribs
['rc_type'] != RC_LOG
) {
202 # Get the latest entry with a page_id and oldid
203 # since logs may not have these.
204 if ( !$curId && $rcObj->mAttribs
['rc_cur_id'] ) {
205 $curId = $rcObj->mAttribs
['rc_cur_id'];
207 if ( !$currentRevision && $rcObj->mAttribs
['rc_this_oldid'] ) {
208 $currentRevision = $rcObj->mAttribs
['rc_this_oldid'];
211 if ( !$rcObj->mAttribs
['rc_bot'] ) {
214 if ( !$rcObj->mAttribs
['rc_minor'] ) {
221 # Sort the list and convert to text
222 krsort( $userlinks );
225 foreach ( $userlinks as $userlink => $count ) {
227 $text .= $this->getLanguage()->getDirMark();
229 // @todo FIXME: Hardcoded '×'. Should be a message.
230 $formattedCount = $this->getLanguage()->formatNum( $count ) . '×';
231 $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
233 array_push( $users, $text );
236 $users = ' <span class="changedby">'
237 . $this->msg( 'brackets' )->rawParams(
238 implode( $this->message
['semicolon-separator'], $users )
239 )->escaped() . '</span>';
241 $tl = '<span class="mw-collapsible-toggle mw-collapsible-arrow ' .
242 'mw-enhancedchanges-arrow mw-enhancedchanges-arrow-space"></span>';
243 $r .= "<td>$tl</td>";
246 $r .= '<td class="mw-enhanced-rc">' . $this->recentChangesFlags( array(
247 'newpage' => $isnew, # show, when one have this flag
248 'minor' => $allMinors, # show only, when all have this flag
249 'unpatrolled' => $unpatrolled, # show, when one have this flag
250 'bot' => $allBots, # show only, when all have this flag
254 $r .= ' ' . $block[0]->timestamp
. ' </td><td>';
258 $r .= ' <span class="history-deleted">' .
259 $this->msg( 'rev-deleted-event' )->escaped() . '</span>';
260 } elseif ( $allLogs ) {
261 $r .= $this->maybeWatchedLink( $block[0]->link
, $block[0]->watched
);
263 $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled
, $block[0]->watched
);
266 $r .= $this->getLanguage()->getDirMark();
268 $queryParams['curid'] = $curId;
271 static $nchanges = array();
272 static $sinceLastVisitMsg = array();
274 $n = count( $block );
275 if ( !isset( $nchanges[$n] ) ) {
276 $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
280 $unvisitedOldid = null;
281 /** @var $rcObj RCCacheEntry */
282 foreach ( $block as $rcObj ) {
283 // Same logic as below inside main foreach
284 if ( $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
) {
286 $unvisitedOldid = $rcObj->mAttribs
['rc_last_oldid'];
289 if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
290 $sinceLastVisitMsg[$sinceLast] =
291 $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
297 /** @var $block0 RecentChange */
300 if ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
301 $logtext .= $nchanges[$n];
302 } elseif ( $isnew ) {
303 $logtext .= $nchanges[$n];
305 $logtext .= Linker
::link(
309 $queryParams +
array(
310 'diff' => $currentRevision,
313 array( 'known', 'noclasses' )
315 if ( $sinceLast > 0 && $sinceLast < $n ) {
316 $logtext .= $this->message
['pipe-separator'] . Linker
::link(
318 $sinceLastVisitMsg[$sinceLast],
320 $queryParams +
array(
321 'diff' => $currentRevision,
322 'oldid' => $unvisitedOldid,
324 array( 'known', 'noclasses' )
332 // don't show history link for logs
333 } elseif ( $namehidden ||
!$block0->getTitle()->exists() ) {
334 $logtext .= $this->message
['pipe-separator'] . $this->message
['enhancedrc-history'];
336 $params = $queryParams;
337 $params['action'] = 'history';
339 $logtext .= $this->message
['pipe-separator'] .
342 $this->message
['enhancedrc-history'],
348 if ( $logtext !== '' ) {
349 $r .= $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
352 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
354 # Character difference (does not apply if only log items)
355 if ( $wgRCShowChangedSize && !$allLogs ) {
357 $first = count( $block ) - 1;
358 # Some events (like logs) have an "empty" size, so we need to skip those...
359 while ( $last < $first && $block[$last]->mAttribs
['rc_new_len'] === null ) {
362 while ( $first > $last && $block[$first]->mAttribs
['rc_old_len'] === null ) {
366 $chardiff = $this->formatCharacterDifference( $block[$first], $block[$last] );
368 if ( $chardiff == '' ) {
371 $r .= ' ' . $chardiff . ' <span class="mw-changeslist-separator">. .</span> ';
376 $r .= $this->numberofWatchingusers( $block0->numberofWatchingusers
);
380 foreach ( $block as $rcObj ) {
381 # Classes to apply -- TODO implement
383 $type = $rcObj->mAttribs
['rc_type'];
385 $trClass = $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
386 ?
' class="mw-enhanced-watched"' : '';
388 $r .= '<tr' . $trClass . '><td></td><td class="mw-enhanced-rc">';
389 $r .= $this->recentChangesFlags( array(
390 'newpage' => $type == RC_NEW
,
391 'minor' => $rcObj->mAttribs
['rc_minor'],
392 'unpatrolled' => $rcObj->unpatrolled
,
393 'bot' => $rcObj->mAttribs
['rc_bot'],
395 $r .= ' </td><td class="mw-enhanced-rc-nested"><span class="mw-enhanced-rc-time">';
397 $params = $queryParams;
399 if ( $rcObj->mAttribs
['rc_this_oldid'] != 0 ) {
400 $params['oldid'] = $rcObj->mAttribs
['rc_this_oldid'];
404 if ( $type == RC_LOG
) {
405 $link = $rcObj->timestamp
;
407 } elseif ( !ChangesList
::userCan( $rcObj, Revision
::DELETED_TEXT
, $this->getUser() ) ) {
408 $link = '<span class="history-deleted">' . $rcObj->timestamp
. '</span> ';
411 $link = Linker
::linkKnown(
417 if ( $this->isDeleted( $rcObj, Revision
::DELETED_TEXT
) ) {
418 $link = '<span class="history-deleted">' . $link . '</span> ';
421 $r .= $link . '</span>';
423 if ( !$type == RC_LOG ||
$type == RC_NEW
) {
424 $r .= ' ' . $this->msg( 'parentheses' )->rawParams(
426 $this->message
['pipe-separator'] .
430 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
433 if ( $wgRCShowChangedSize ) {
434 $cd = $this->formatCharacterDifference( $rcObj );
436 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
440 if ( $rcObj->mAttribs
['rc_type'] == RC_LOG
) {
441 $r .= $this->insertLogEntry( $rcObj );
444 $r .= $rcObj->userlink
;
445 $r .= $rcObj->usertalklink
;
446 $r .= $this->insertComment( $rcObj );
450 $this->insertRollback( $r, $rcObj );
452 $this->insertTags( $r, $rcObj, $classes );
454 $r .= "</td></tr>\n";
458 $this->rcCacheIndex++
;
460 wfProfileOut( __METHOD__
);
466 * Generate HTML for an arrow or placeholder graphic
467 * @param string $dir One of '', 'd', 'l', 'r'
469 * @param string $title
470 * @return string HTML "<img>" tag
472 protected function arrow( $dir, $alt = '', $title = '' ) {
474 $encUrl = htmlspecialchars( $wgStylePath . '/common/images/Arr_' . $dir . '.png' );
475 $encAlt = htmlspecialchars( $alt );
476 $encTitle = htmlspecialchars( $title );
478 return "<img src=\"$encUrl\" width=\"12\" height=\"12\" alt=\"$encAlt\" title=\"$encTitle\" />";
482 * Generate HTML for a right- or left-facing arrow,
483 * depending on language direction.
484 * @return string HTML "<img>" tag
486 protected function sideArrow() {
487 $dir = $this->getLanguage()->isRTL() ?
'l' : 'r';
489 return $this->arrow( $dir, '+', $this->msg( 'rc-enhanced-expand' )->text() );
493 * Generate HTML for a down-facing arrow
494 * depending on language direction.
495 * @return string HTML "<img>" tag
497 protected function downArrow() {
498 return $this->arrow( 'd', '-', $this->msg( 'rc-enhanced-hide' )->text() );
502 * Generate HTML for a spacer image
503 * @return string HTML "<img>" tag
505 protected function spacerArrow() {
506 return $this->arrow( '', codepointToUtf8( 0xa0 ) ); // non-breaking space
510 * Enhanced RC ungrouped line.
512 * @param RecentChange|RCCacheEntry $rcObj
513 * @return string A HTML formatted line (generated using $r)
515 protected function recentChangesBlockLine( $rcObj ) {
516 global $wgRCShowChangedSize;
518 wfProfileIn( __METHOD__
);
519 $query['curid'] = $rcObj->mAttribs
['rc_cur_id'];
521 $type = $rcObj->mAttribs
['rc_type'];
522 $logType = $rcObj->mAttribs
['rc_log_type'];
523 $classes = array( 'mw-enhanced-rc' );
526 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-log-' . $logType );
528 $classes[] = Sanitizer
::escapeClass( 'mw-changeslist-ns' .
529 $rcObj->mAttribs
['rc_namespace'] . '-' . $rcObj->mAttribs
['rc_title'] );
531 $classes[] = $rcObj->watched
&& $rcObj->mAttribs
['rc_timestamp'] >= $rcObj->watched
532 ?
'mw-changeslist-line-watched' : 'mw-changeslist-line-not-watched';
533 $r = Html
::openElement( 'table', array( 'class' => $classes ) ) .
534 Html
::openElement( 'tr' );
536 $r .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow-space"></span>';
538 if ( $type == RC_MOVE ||
$type == RC_MOVE_OVER_REDIRECT
) {
539 $r .= $this->recentChangesFlags( array() ); // no flags, but need the placeholders
541 $r .= $this->recentChangesFlags( array(
542 'newpage' => $type == RC_NEW
,
543 'minor' => $rcObj->mAttribs
['rc_minor'],
544 'unpatrolled' => $rcObj->unpatrolled
,
545 'bot' => $rcObj->mAttribs
['rc_bot'],
548 $r .= ' ' . $rcObj->timestamp
. ' </td><td>';
549 # Article or log link
551 $logPage = new LogPage( $logType );
552 $logTitle = SpecialPage
::getTitleFor( 'Log', $logType );
553 $logName = $logPage->getName()->escaped();
554 $r .= $this->msg( 'parentheses' )
555 ->rawParams( Linker
::linkKnown( $logTitle, $logName ) )->escaped();
557 $this->insertArticleLink( $r, $rcObj, $rcObj->unpatrolled
, $rcObj->watched
);
559 # Diff and hist links
560 if ( $type != RC_LOG
) {
561 $query['action'] = 'history';
562 $r .= ' ' . $this->msg( 'parentheses' )
563 ->rawParams( $rcObj->difflink
. $this->message
['pipe-separator'] . Linker
::linkKnown(
565 $this->message
['hist'],
570 $r .= ' <span class="mw-changeslist-separator">. .</span> ';
572 if ( $wgRCShowChangedSize ) {
573 $cd = $this->formatCharacterDifference( $rcObj );
575 $r .= $cd . ' <span class="mw-changeslist-separator">. .</span> ';
579 if ( $type == RC_LOG
) {
580 $r .= $this->insertLogEntry( $rcObj );
582 $r .= ' ' . $rcObj->userlink
. $rcObj->usertalklink
;
583 $r .= $this->insertComment( $rcObj );
584 $this->insertRollback( $r, $rcObj );
588 $this->insertTags( $r, $rcObj, $classes );
589 # Show how many people are watching this if enabled
590 $r .= $this->numberofWatchingusers( $rcObj->numberofWatchingusers
);
592 $r .= "</td></tr></table>\n";
594 wfProfileOut( __METHOD__
);
600 * If enhanced RC is in use, this function takes the previously cached
601 * RC lines, arranges them, and outputs the HTML
605 protected function recentChangesBlock() {
606 if ( count( $this->rc_cache
) == 0 ) {
610 wfProfileIn( __METHOD__
);
613 foreach ( $this->rc_cache
as $block ) {
614 if ( count( $block ) < 2 ) {
615 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
617 $blockOut .= $this->recentChangesBlockGroup( $block );
621 wfProfileOut( __METHOD__
);
623 return '<div>' . $blockOut . '</div>';
627 * Returns text for the end of RC
628 * If enhanced RC is in use, returns pretty much all the text
631 public function endRecentChangesList() {
632 return $this->recentChangesBlock() . '</div>';