My changes in r96548 were too radical, stripping of edit commits (oops!) and rollback...
[lhc/web/wiklou.git] / includes / ChangesList.php
1 <?php
2 /**
3 * Classes to show various lists of changes:
4 * - watchlist
5 * - related changes
6 * - recent changes
7 *
8 * @file
9 */
10
11 /**
12 * @todo document
13 */
14 class RCCacheEntry extends RecentChange {
15 var $secureName, $link;
16 var $curlink , $difflink, $lastlink, $usertalklink, $versionlink;
17 var $userlink, $timestamp, $watched;
18
19 /**
20 * @param $rc RecentChange
21 * @return RCCacheEntry
22 */
23 static function newFromParent( $rc ) {
24 $rc2 = new RCCacheEntry;
25 $rc2->mAttribs = $rc->mAttribs;
26 $rc2->mExtra = $rc->mExtra;
27 return $rc2;
28 }
29 }
30
31 /**
32 * Base class for all changes lists
33 */
34 class ChangesList extends ContextSource {
35
36 /**
37 * @var Skin
38 */
39 public $skin;
40
41 protected $watchlist = false;
42
43 protected $message;
44
45 /**
46 * Changeslist contructor
47 *
48 * @param $obj Skin or IContextSource
49 */
50 public function __construct( $obj ) {
51 if ( $obj instanceof IContextSource ) {
52 $this->setContext( $obj );
53 $this->skin = $obj->getSkin();
54 } else {
55 $this->setContext( $obj->getContext() );
56 $this->skin = $obj;
57 }
58 $this->preCacheMessages();
59 }
60
61 /**
62 * Fetch an appropriate changes list class for the main context
63 * This first argument used to be an User object.
64 *
65 * @deprecated in 1.18; use newFromContext() instead
66 * @param $unused Unused
67 * @return ChangesList|EnhancedChangesList|OldChangesList derivative
68 */
69 public static function newFromUser( $unused ) {
70 return self::newFromContext( RequestContext::getMain() );
71 }
72
73 /**
74 * Fetch an appropriate changes list class for the specified context
75 * Some users might want to use an enhanced list format, for instance
76 *
77 * @param $context IContextSource to use
78 * @return ChangesList|EnhancedChangesList|OldChangesList derivative
79 */
80 public static function newFromContext( IContextSource $context ) {
81 $user = $context->getUser();
82 $sk = $context->getSkin();
83 $list = null;
84 if( wfRunHooks( 'FetchChangesList', array( $user, &$sk, &$list ) ) ) {
85 $new = $context->getRequest()->getBool( 'enhanced', $user->getOption( 'usenewrc' ) );
86 return $new ? new EnhancedChangesList( $context ) : new OldChangesList( $context );
87 } else {
88 return $list;
89 }
90 }
91
92 /**
93 * Sets the list to use a <li class="watchlist-(namespace)-(page)"> tag
94 * @param $value Boolean
95 */
96 public function setWatchlistDivs( $value = true ) {
97 $this->watchlist = $value;
98 }
99
100 /**
101 * As we use the same small set of messages in various methods and that
102 * they are called often, we call them once and save them in $this->message
103 */
104 private function preCacheMessages() {
105 if( !isset( $this->message ) ) {
106 foreach ( explode( ' ', 'cur diff hist last blocklink history ' .
107 'semicolon-separator pipe-separator' ) as $msg ) {
108 $this->message[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
109 }
110 }
111 }
112
113 /**
114 * Returns the appropriate flags for new page, minor change and patrolling
115 * @param $flags Array Associative array of 'flag' => Bool
116 * @param $nothing String to use for empty space
117 * @return String
118 */
119 protected function recentChangesFlags( $flags, $nothing = '&#160;' ) {
120 $f = '';
121 foreach( array( 'newpage', 'minor', 'bot', 'unpatrolled' ) as $flag ){
122 $f .= isset( $flags[$flag] ) && $flags[$flag]
123 ? self::flag( $flag )
124 : $nothing;
125 }
126 return $f;
127 }
128
129 /**
130 * Provide the <abbr> element appropriate to a given abbreviated flag,
131 * namely the flag indicating a new page, a minor edit, a bot edit, or an
132 * unpatrolled edit. By default in English it will contain "N", "m", "b",
133 * "!" respectively, plus it will have an appropriate title and class.
134 *
135 * @param $flag String: 'newpage', 'unpatrolled', 'minor', or 'bot'
136 * @return String: Raw HTML
137 */
138 public static function flag( $flag ) {
139 static $messages = null;
140 if ( is_null( $messages ) ) {
141 $messages = array(
142 'newpage' => array( 'newpageletter', 'recentchanges-label-newpage' ),
143 'minoredit' => array( 'minoreditletter', 'recentchanges-label-minor' ),
144 'botedit' => array( 'boteditletter', 'recentchanges-label-bot' ),
145 'unpatrolled' => array( 'unpatrolledletter', 'recentchanges-label-unpatrolled' ),
146 );
147 foreach( $messages as &$value ) {
148 $value[0] = wfMsgExt( $value[0], 'escapenoentities' );
149 $value[1] = wfMsgExt( $value[1], 'escapenoentities' );
150 }
151 }
152
153 # Inconsistent naming, bleh
154 $map = array(
155 'newpage' => 'newpage',
156 'minor' => 'minoredit',
157 'bot' => 'botedit',
158 'unpatrolled' => 'unpatrolled',
159 'minoredit' => 'minoredit',
160 'botedit' => 'botedit',
161 );
162 $flag = $map[$flag];
163
164 return "<abbr class='$flag' title='" . $messages[$flag][1] . "'>" . $messages[$flag][0] . '</abbr>';
165 }
166
167 /**
168 * Returns text for the start of the tabular part of RC
169 * @return String
170 */
171 public function beginRecentChangesList() {
172 $this->rc_cache = array();
173 $this->rcMoveIndex = 0;
174 $this->rcCacheIndex = 0;
175 $this->lastdate = '';
176 $this->rclistOpen = false;
177 return '';
178 }
179
180 /**
181 * Show formatted char difference
182 * @param $old Integer: bytes
183 * @param $new Integer: bytes
184 * @return String
185 */
186 public static function showCharacterDifference( $old, $new ) {
187 global $wgRCChangedSizeThreshold, $wgLang, $wgMiserMode;
188 $szdiff = $new - $old;
189
190 $code = $wgLang->getCode();
191 static $fastCharDiff = array();
192 if ( !isset($fastCharDiff[$code]) ) {
193 $fastCharDiff[$code] = $wgMiserMode || wfMsgNoTrans( 'rc-change-size' ) === '$1';
194 }
195
196 $formatedSize = $wgLang->formatNum($szdiff);
197
198 if ( !$fastCharDiff[$code] ) {
199 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape' ), $formatedSize );
200 }
201
202 if( abs( $szdiff ) > abs( $wgRCChangedSizeThreshold ) ) {
203 $tag = 'strong';
204 } else {
205 $tag = 'span';
206 }
207 if( $szdiff === 0 ) {
208 return "<$tag class='mw-plusminus-null'>($formatedSize)</$tag>";
209 } elseif( $szdiff > 0 ) {
210 return "<$tag class='mw-plusminus-pos'>(+$formatedSize)</$tag>";
211 } else {
212 return "<$tag class='mw-plusminus-neg'>($formatedSize)</$tag>";
213 }
214 }
215
216 /**
217 * Returns text for the end of RC
218 * @return String
219 */
220 public function endRecentChangesList() {
221 if( $this->rclistOpen ) {
222 return "</ul>\n";
223 } else {
224 return '';
225 }
226 }
227
228 public function insertDateHeader( &$s, $rc_timestamp ) {
229 # Make date header if necessary
230 $date = $this->getLang()->date( $rc_timestamp, true, true );
231 if( $date != $this->lastdate ) {
232 if( $this->lastdate != '' ) {
233 $s .= "</ul>\n";
234 }
235 $s .= Xml::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
236 $this->lastdate = $date;
237 $this->rclistOpen = true;
238 }
239 }
240
241 public function insertLog( &$s, $title, $logtype ) {
242 $page = new LogPage( $logtype );
243 $logname = $page->getName()->escaped();
244 $s .= '(' . Linker::linkKnown( $title, $logname ) . ')';
245 }
246
247 /**
248 * @param $s
249 * @param $rc RecentChange
250 * @param $unpatrolled
251 * @return void
252 */
253 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
254 # Diff link
255 if( $rc->mAttribs['rc_type'] == RC_NEW || $rc->mAttribs['rc_type'] == RC_LOG ) {
256 $diffLink = $this->message['diff'];
257 } elseif( !self::userCan($rc,Revision::DELETED_TEXT) ) {
258 $diffLink = $this->message['diff'];
259 } else {
260 $query = array(
261 'curid' => $rc->mAttribs['rc_cur_id'],
262 'diff' => $rc->mAttribs['rc_this_oldid'],
263 'oldid' => $rc->mAttribs['rc_last_oldid']
264 );
265
266 if( $unpatrolled ) {
267 $query['rcid'] = $rc->mAttribs['rc_id'];
268 };
269
270 $diffLink = Linker::linkKnown(
271 $rc->getTitle(),
272 $this->message['diff'],
273 array( 'tabindex' => $rc->counter ),
274 $query
275 );
276 }
277 $s .= '(' . $diffLink . $this->message['pipe-separator'];
278 # History link
279 $s .= Linker::linkKnown(
280 $rc->getTitle(),
281 $this->message['hist'],
282 array(),
283 array(
284 'curid' => $rc->mAttribs['rc_cur_id'],
285 'action' => 'history'
286 )
287 );
288 $s .= ') . . ';
289 }
290
291 /**
292 * @param $s
293 * @param $rc RecentChange
294 * @param $unpatrolled
295 * @param $watched
296 * @return void
297 */
298 public function insertArticleLink( &$s, &$rc, $unpatrolled, $watched ) {
299 # If it's a new article, there is no diff link, but if it hasn't been
300 # patrolled yet, we need to give users a way to do so
301 $params = array();
302
303 if ( $unpatrolled && $rc->mAttribs['rc_type'] == RC_NEW ) {
304 $params['rcid'] = $rc->mAttribs['rc_id'];
305 }
306
307 if( $this->isDeleted($rc,Revision::DELETED_TEXT) ) {
308 $articlelink = Linker::linkKnown(
309 $rc->getTitle(),
310 null,
311 array(),
312 $params
313 );
314 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
315 } else {
316 $articlelink = ' '. Linker::linkKnown(
317 $rc->getTitle(),
318 null,
319 array(),
320 $params
321 );
322 }
323 # Bolden pages watched by this user
324 if( $watched ) {
325 $articlelink = "<strong class=\"mw-watched\">{$articlelink}</strong>";
326 }
327 # RTL/LTR marker
328 $articlelink .= $this->getLang()->getDirMark();
329
330 wfRunHooks( 'ChangesListInsertArticleLink',
331 array(&$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched) );
332
333 $s .= " $articlelink";
334 }
335
336 /**
337 * @param $s
338 * @param $rc RecentChange
339 * @return void
340 */
341 public function insertTimestamp( &$s, $rc ) {
342 $s .= $this->message['semicolon-separator'] .
343 $this->getLang()->time( $rc->mAttribs['rc_timestamp'], true, true ) . ' . . ';
344 }
345
346 /** Insert links to user page, user talk page and eventually a blocking link
347 *
348 * @param $rc RecentChange
349 */
350 public function insertUserRelatedLinks( &$s, &$rc ) {
351 if( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
352 $s .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
353 } else {
354 $s .= Linker::userLink( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
355 $s .= Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
356 }
357 }
358
359 /** insert a formatted action
360 *
361 * @param $rc RecentChange
362 */
363 public function insertLogEntry( $rc ) {
364 $formatter = LogFormatter::newFromRow( $rc->mAttribs );
365 $formatter->setShowUserToolLinks( true );
366 $mark = $this->getLang()->getDirMark();
367 return $formatter->getActionText() . " $mark" . $formatter->getComment();
368 }
369
370 /**
371 * Insert a formatted comment
372 * @param $rc RecentChange
373 */
374 public function insertComment( $rc ) {
375 if( $rc->mAttribs['rc_type'] != RC_MOVE && $rc->mAttribs['rc_type'] != RC_MOVE_OVER_REDIRECT ) {
376 if( $this->isDeleted( $rc, Revision::DELETED_COMMENT ) ) {
377 return ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
378 } else {
379 return Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
380 }
381 }
382 }
383
384 /**
385 * Check whether to enable recent changes patrol features
386 * @return Boolean
387 */
388 public static function usePatrol() {
389 global $wgUser;
390 return $wgUser->useRCPatrol();
391 }
392
393 /**
394 * Returns the string which indicates the number of watching users
395 */
396 protected function numberofWatchingusers( $count ) {
397 static $cache = array();
398 if( $count > 0 ) {
399 if( !isset( $cache[$count] ) ) {
400 $cache[$count] = wfMsgExt( 'number_of_watching_users_RCview',
401 array('parsemag', 'escape' ), $this->getLang()->formatNum( $count ) );
402 }
403 return $cache[$count];
404 } else {
405 return '';
406 }
407 }
408
409 /**
410 * Determine if said field of a revision is hidden
411 * @param $rc RCCacheEntry
412 * @param $field Integer: one of DELETED_* bitfield constants
413 * @return Boolean
414 */
415 public static function isDeleted( $rc, $field ) {
416 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
417 }
418
419 /**
420 * Determine if the current user is allowed to view a particular
421 * field of this revision, if it's marked as deleted.
422 * @param $rc RCCacheEntry
423 * @param $field Integer
424 * @return Boolean
425 */
426 public static function userCan( $rc, $field ) {
427 if( $rc->mAttribs['rc_type'] == RC_LOG ) {
428 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field );
429 } else {
430 return Revision::userCanBitfield( $rc->mAttribs['rc_deleted'], $field );
431 }
432 }
433
434 protected function maybeWatchedLink( $link, $watched = false ) {
435 if( $watched ) {
436 return '<strong class="mw-watched">' . $link . '</strong>';
437 } else {
438 return '<span class="mw-rc-unwatched">' . $link . '</span>';
439 }
440 }
441
442 /** Inserts a rollback link
443 *
444 * @param $s
445 * @param $rc RecentChange
446 */
447 public function insertRollback( &$s, &$rc ) {
448 if( !$rc->mAttribs['rc_new'] && $rc->mAttribs['rc_this_oldid'] && $rc->mAttribs['rc_cur_id'] ) {
449 $page = $rc->getTitle();
450 /** Check for rollback and edit permissions, disallow special pages, and only
451 * show a link on the top-most revision */
452 if ( $this->getUser()->isAllowed('rollback') && $rc->mAttribs['page_latest'] == $rc->mAttribs['rc_this_oldid'] )
453 {
454 $rev = new Revision( array(
455 'id' => $rc->mAttribs['rc_this_oldid'],
456 'user' => $rc->mAttribs['rc_user'],
457 'user_text' => $rc->mAttribs['rc_user_text'],
458 'deleted' => $rc->mAttribs['rc_deleted']
459 ) );
460 $rev->setTitle( $page );
461 $s .= ' '.Linker::generateRollback( $rev, $this->getContext() );
462 }
463 }
464 }
465
466 /**
467 * @param $s
468 * @param $rc RecentChange
469 * @param $classes
470 * @return
471 */
472 public function insertTags( &$s, &$rc, &$classes ) {
473 if ( empty($rc->mAttribs['ts_tags']) )
474 return;
475
476 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $rc->mAttribs['ts_tags'], 'changeslist' );
477 $classes = array_merge( $classes, $newClasses );
478 $s .= ' ' . $tagSummary;
479 }
480
481 public function insertExtra( &$s, &$rc, &$classes ) {
482 ## Empty, used for subclassers to add anything special.
483 }
484 }
485
486
487 /**
488 * Generate a list of changes using the good old system (no javascript)
489 */
490 class OldChangesList extends ChangesList {
491 /**
492 * Format a line using the old system (aka without any javascript).
493 *
494 * @param $rc RecentChange
495 */
496 public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
497 global $wgRCShowChangedSize;
498 wfProfileIn( __METHOD__ );
499 # Should patrol-related stuff be shown?
500 $unpatrolled = $this->getUser()->useRCPatrol() && !$rc->mAttribs['rc_patrolled'];
501
502 $dateheader = ''; // $s now contains only <li>...</li>, for hooks' convenience.
503 $this->insertDateHeader( $dateheader, $rc->mAttribs['rc_timestamp'] );
504
505 $s = '';
506 $classes = array();
507 // use mw-line-even/mw-line-odd class only if linenumber is given (feature from bug 14468)
508 if( $linenumber ) {
509 if( $linenumber & 1 ) {
510 $classes[] = 'mw-line-odd';
511 }
512 else {
513 $classes[] = 'mw-line-even';
514 }
515 }
516
517 // Moved pages (very very old, not supported anymore)
518 if( $rc->mAttribs['rc_type'] == RC_MOVE || $rc->mAttribs['rc_type'] == RC_MOVE_OVER_REDIRECT ) {
519 // Log entries
520 } elseif( $rc->mAttribs['rc_log_type'] ) {
521 $logtitle = Title::newFromText( 'Log/'.$rc->mAttribs['rc_log_type'], NS_SPECIAL );
522 $this->insertLog( $s, $logtitle, $rc->mAttribs['rc_log_type'] );
523 // Log entries (old format) or log targets, and special pages
524 } elseif( $rc->mAttribs['rc_namespace'] == NS_SPECIAL ) {
525 list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $rc->mAttribs['rc_title'] );
526 if( $name == 'Log' ) {
527 $this->insertLog( $s, $rc->getTitle(), $subpage );
528 }
529 // Regular entries
530 } else {
531 $this->insertDiffHist( $s, $rc, $unpatrolled );
532 # M, N, b and ! (minor, new, bot and unpatrolled)
533 $s .= $this->recentChangesFlags(
534 array(
535 'newpage' => $rc->mAttribs['rc_new'],
536 'minor' => $rc->mAttribs['rc_minor'],
537 'unpatrolled' => $unpatrolled,
538 'bot' => $rc->mAttribs['rc_bot']
539 ),
540 ''
541 );
542 $this->insertArticleLink( $s, $rc, $unpatrolled, $watched );
543 }
544 # Edit/log timestamp
545 $this->insertTimestamp( $s, $rc );
546 # Bytes added or removed
547 if( $wgRCShowChangedSize ) {
548 $cd = $rc->getCharacterDifference();
549 if( $cd != '' ) {
550 $s .= "$cd . . ";
551 }
552 }
553
554 if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
555 $s .= $this->insertLogEntry( $rc );
556 } else {
557 # User tool links
558 $this->insertUserRelatedLinks( $s, $rc );
559 # LTR/RTL direction mark
560 $s .= $this->getLang()->getDirMark();
561 $s .= $this->insertComment( $rc );
562 }
563
564 # Tags
565 $this->insertTags( $s, $rc, $classes );
566 # Rollback
567 $this->insertRollback( $s, $rc );
568 # For subclasses
569 $this->insertExtra( $s, $rc, $classes );
570
571 # How many users watch this page
572 if( $rc->numberofWatchingusers > 0 ) {
573 $s .= ' ' . wfMsgExt( 'number_of_watching_users_RCview',
574 array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $rc->numberofWatchingusers ) );
575 }
576
577 if( $this->watchlist ) {
578 $classes[] = Sanitizer::escapeClass( 'watchlist-'.$rc->mAttribs['rc_namespace'].'-'.$rc->mAttribs['rc_title'] );
579 }
580
581 wfRunHooks( 'OldChangesListRecentChangesLine', array(&$this, &$s, $rc) );
582
583 wfProfileOut( __METHOD__ );
584 return "$dateheader<li class=\"".implode( ' ', $classes )."\">".$s."</li>\n";
585 }
586 }
587
588
589 /**
590 * Generate a list of changes using an Enhanced system (uses javascript).
591 */
592 class EnhancedChangesList extends ChangesList {
593 /**
594 * Add the JavaScript file for enhanced changeslist
595 * @return String
596 */
597 public function beginRecentChangesList() {
598 $this->rc_cache = array();
599 $this->rcMoveIndex = 0;
600 $this->rcCacheIndex = 0;
601 $this->lastdate = '';
602 $this->rclistOpen = false;
603 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
604 return '';
605 }
606 /**
607 * Format a line for enhanced recentchange (aka with javascript and block of lines).
608 *
609 * @param $baseRC RecentChange
610 * @param $watched bool
611 *
612 * @return string
613 */
614 public function recentChangesLine( &$baseRC, $watched = false ) {
615 wfProfileIn( __METHOD__ );
616
617 # Create a specialised object
618 $rc = RCCacheEntry::newFromParent( $baseRC );
619
620 $curIdEq = array( 'curid' => $rc->mAttribs['rc_cur_id'] );
621
622 # If it's a new day, add the headline and flush the cache
623 $date = $this->getLang()->date( $rc->mAttribs['rc_timestamp'], true );
624 $ret = '';
625 if( $date != $this->lastdate ) {
626 # Process current cache
627 $ret = $this->recentChangesBlock();
628 $this->rc_cache = array();
629 $ret .= Xml::element( 'h4', null, $date ) . "\n";
630 $this->lastdate = $date;
631 }
632
633 # Should patrol-related stuff be shown?
634 if( $this->getUser()->useRCPatrol() ) {
635 $rc->unpatrolled = !$rc->mAttribs['rc_patrolled'];
636 } else {
637 $rc->unpatrolled = false;
638 }
639
640 $showdifflinks = true;
641 # Make article link
642 $type = $rc->mAttribs['rc_type'];
643 $logType = $rc->mAttribs['rc_log_type'];
644 // Page moves, very old style, not supported anymore
645 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
646 // New unpatrolled pages
647 } elseif( $rc->unpatrolled && $type == RC_NEW ) {
648 $clink = Linker::linkKnown( $rc->getTitle(), null, array(),
649 array( 'rcid' => $rc->mAttribs['rc_id'] ) );
650 // Log entries
651 } elseif( $type == RC_LOG ) {
652 if( $logType ) {
653 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
654 $clink = '(' . Linker::linkKnown( $logtitle,
655 LogPage::logName( $logType ) ) . ')';
656 } else {
657 $clink = Linker::link( $rc->getTitle() );
658 }
659 $watched = false;
660 // Log entries (old format) and special pages
661 } elseif( $rc->mAttribs['rc_namespace'] == NS_SPECIAL ) {
662 list( $specialName, $logtype ) = SpecialPageFactory::resolveAlias( $rc->mAttribs['rc_title'] );
663 if ( $specialName == 'Log' ) {
664 # Log updates, etc
665 $logname = LogPage::logName( $logtype );
666 $clink = '(' . Linker::linkKnown( $rc->getTitle(), $logname ) . ')';
667 } else {
668 wfDebug( "Unexpected special page in recentchanges\n" );
669 $clink = '';
670 }
671 // Edits
672 } else {
673 $clink = Linker::linkKnown( $rc->getTitle() );
674 }
675
676 # Don't show unusable diff links
677 if ( !ChangesList::userCan($rc,Revision::DELETED_TEXT) ) {
678 $showdifflinks = false;
679 }
680
681 $time = $this->getLang()->time( $rc->mAttribs['rc_timestamp'], true, true );
682 $rc->watched = $watched;
683 $rc->link = $clink;
684 $rc->timestamp = $time;
685 $rc->numberofWatchingusers = $baseRC->numberofWatchingusers;
686
687 # Make "cur" and "diff" links. Do not use link(), it is too slow if
688 # called too many times (50% of CPU time on RecentChanges!).
689 $thisOldid = $rc->mAttribs['rc_this_oldid'];
690 $lastOldid = $rc->mAttribs['rc_last_oldid'];
691 if( $rc->unpatrolled ) {
692 $rcIdQuery = array( 'rcid' => $rc->mAttribs['rc_id'] );
693 } else {
694 $rcIdQuery = array();
695 }
696 $querycur = $curIdEq + array( 'diff' => '0', 'oldid' => $thisOldid );
697 $querydiff = $curIdEq + array( 'diff' => $thisOldid, 'oldid' =>
698 $lastOldid ) + $rcIdQuery;
699
700 if( !$showdifflinks ) {
701 $curLink = $this->message['cur'];
702 $diffLink = $this->message['diff'];
703 } elseif( in_array( $type, array( RC_NEW, RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT ) ) ) {
704 if ( $type != RC_NEW ) {
705 $curLink = $this->message['cur'];
706 } else {
707 $curUrl = htmlspecialchars( $rc->getTitle()->getLinkUrl( $querycur ) );
708 $curLink = "<a href=\"$curUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['cur']}</a>";
709 }
710 $diffLink = $this->message['diff'];
711 } else {
712 $diffUrl = htmlspecialchars( $rc->getTitle()->getLinkUrl( $querydiff ) );
713 $curUrl = htmlspecialchars( $rc->getTitle()->getLinkUrl( $querycur ) );
714 $diffLink = "<a href=\"$diffUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['diff']}</a>";
715 $curLink = "<a href=\"$curUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['cur']}</a>";
716 }
717
718 # Make "last" link
719 if( !$showdifflinks || !$lastOldid ) {
720 $lastLink = $this->message['last'];
721 } elseif( in_array( $type, array( RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT ) ) ) {
722 $lastLink = $this->message['last'];
723 } else {
724 $lastLink = Linker::linkKnown( $rc->getTitle(), $this->message['last'],
725 array(), $curIdEq + array('diff' => $thisOldid, 'oldid' => $lastOldid) + $rcIdQuery );
726 }
727
728 # Make user links
729 if( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
730 $rc->userlink = ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
731 } else {
732 $rc->userlink = Linker::userLink( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
733 $rc->usertalklink = Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
734 }
735
736 $rc->lastlink = $lastLink;
737 $rc->curlink = $curLink;
738 $rc->difflink = $diffLink;
739
740 # Put accumulated information into the cache, for later display
741 # Page moves go on their own line
742 $title = $rc->getTitle();
743 $secureName = $title->getPrefixedDBkey();
744 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
745 # Use an @ character to prevent collision with page names
746 $this->rc_cache['@@' . ($this->rcMoveIndex++)] = array($rc);
747 } else {
748 # Logs are grouped by type
749 if( $type == RC_LOG ){
750 $secureName = SpecialPage::getTitleFor( 'Log', $logType )->getPrefixedDBkey();
751 }
752 if( !isset( $this->rc_cache[$secureName] ) ) {
753 $this->rc_cache[$secureName] = array();
754 }
755
756 array_push( $this->rc_cache[$secureName], $rc );
757 }
758
759 wfProfileOut( __METHOD__ );
760
761 return $ret;
762 }
763
764 /**
765 * Enhanced RC group
766 */
767 protected function recentChangesBlockGroup( $block ) {
768 global $wgRCShowChangedSize;
769
770 wfProfileIn( __METHOD__ );
771
772 # Add the namespace and title of the block as part of the class
773 if ( $block[0]->mAttribs['rc_log_type'] ) {
774 # Log entry
775 $classes = 'mw-collapsible mw-collapsed mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-log-' . $block[0]->mAttribs['rc_log_type'] . '-' . $block[0]->mAttribs['rc_title'] );
776 } else {
777 $classes = 'mw-collapsible mw-collapsed mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-ns' . $block[0]->mAttribs['rc_namespace'] . '-' . $block[0]->mAttribs['rc_title'] );
778 }
779 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
780 Html::openElement( 'tr' );
781
782 # Collate list of users
783 $userlinks = array();
784 # Other properties
785 $unpatrolled = false;
786 $isnew = false;
787 $curId = $currentRevision = 0;
788 # Some catalyst variables...
789 $namehidden = true;
790 $allLogs = true;
791 foreach( $block as $rcObj ) {
792 $oldid = $rcObj->mAttribs['rc_last_oldid'];
793 if( $rcObj->mAttribs['rc_new'] ) {
794 $isnew = true;
795 }
796 // If all log actions to this page were hidden, then don't
797 // give the name of the affected page for this block!
798 if( !$this->isDeleted( $rcObj, LogPage::DELETED_ACTION ) ) {
799 $namehidden = false;
800 }
801 $u = $rcObj->userlink;
802 if( !isset( $userlinks[$u] ) ) {
803 $userlinks[$u] = 0;
804 }
805 if( $rcObj->unpatrolled ) {
806 $unpatrolled = true;
807 }
808 if( $rcObj->mAttribs['rc_type'] != RC_LOG ) {
809 $allLogs = false;
810 }
811 # Get the latest entry with a page_id and oldid
812 # since logs may not have these.
813 if( !$curId && $rcObj->mAttribs['rc_cur_id'] ) {
814 $curId = $rcObj->mAttribs['rc_cur_id'];
815 }
816 if( !$currentRevision && $rcObj->mAttribs['rc_this_oldid'] ) {
817 $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
818 }
819
820 $bot = $rcObj->mAttribs['rc_bot'];
821 $userlinks[$u]++;
822 }
823
824 # Sort the list and convert to text
825 krsort( $userlinks );
826 asort( $userlinks );
827 $users = array();
828 foreach( $userlinks as $userlink => $count) {
829 $text = $userlink;
830 $text .= $this->getLang()->getDirMark();
831 if( $count > 1 ) {
832 $text .= ' (' . $this->getLang()->formatNum( $count ) . '×)';
833 }
834 array_push( $users, $text );
835 }
836
837 $users = ' <span class="changedby">[' .
838 implode( $this->message['semicolon-separator'], $users ) . ']</span>';
839
840 # Title for <a> tags
841 $expandTitle = htmlspecialchars( wfMsg( 'rc-enhanced-expand' ) );
842 $closeTitle = htmlspecialchars( wfMsg( 'rc-enhanced-hide' ) );
843
844 $tl = "<span class='mw-collapsible-toggle'>"
845 . "<span class='mw-rc-openarrow'>"
846 . "<a href='#' title='$expandTitle'>{$this->sideArrow()}</a>"
847 . "</span><span class='mw-rc-closearrow'>"
848 . "<a href='#' title='$closeTitle'>{$this->downArrow()}</a>"
849 . "</span></span>";
850 $r .= "<td>$tl</td>";
851
852 # Main line
853 $r .= '<td class="mw-enhanced-rc">' . $this->recentChangesFlags( array(
854 'newpage' => $isnew,
855 'minor' => false,
856 'unpatrolled' => $unpatrolled,
857 'bot' => $bot ,
858 ) );
859
860 # Timestamp
861 $r .= '&#160;'.$block[0]->timestamp.'&#160;</td><td>';
862
863 # Article link
864 if( $namehidden ) {
865 $r .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-event' ) . '</span>';
866 } elseif( $allLogs ) {
867 $r .= $this->maybeWatchedLink( $block[0]->link, $block[0]->watched );
868 } else {
869 $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled, $block[0]->watched );
870 }
871
872 $r .= $this->getLang()->getDirMark();
873
874 $queryParams['curid'] = $curId;
875 # Changes message
876 $n = count($block);
877 static $nchanges = array();
878 if ( !isset( $nchanges[$n] ) ) {
879 $nchanges[$n] = wfMsgExt( 'nchanges', array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $n ) );
880 }
881 # Total change link
882 $r .= ' ';
883 if( !$allLogs ) {
884 $r .= '(';
885 if( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT ) ) {
886 $r .= $nchanges[$n];
887 } elseif( $isnew ) {
888 $r .= $nchanges[$n];
889 } else {
890 $params = $queryParams;
891 $params['diff'] = $currentRevision;
892 $params['oldid'] = $oldid;
893
894 $r .= Linker::link(
895 $block[0]->getTitle(),
896 $nchanges[$n],
897 array(),
898 $params,
899 array( 'known', 'noclasses' )
900 );
901 }
902 }
903
904 # History
905 if( $allLogs ) {
906 // don't show history link for logs
907 } elseif( $namehidden || !$block[0]->getTitle()->exists() ) {
908 $r .= $this->message['pipe-separator'] . $this->message['hist'] . ')';
909 } else {
910 $params = $queryParams;
911 $params['action'] = 'history';
912
913 $r .= $this->message['pipe-separator'] .
914 Linker::linkKnown(
915 $block[0]->getTitle(),
916 $this->message['hist'],
917 array(),
918 $params
919 ) . ')';
920 }
921 $r .= ' . . ';
922
923 # Character difference (does not apply if only log items)
924 if( $wgRCShowChangedSize && !$allLogs ) {
925 $last = 0;
926 $first = count($block) - 1;
927 # Some events (like logs) have an "empty" size, so we need to skip those...
928 while( $last < $first && $block[$last]->mAttribs['rc_new_len'] === null ) {
929 $last++;
930 }
931 while( $first > $last && $block[$first]->mAttribs['rc_old_len'] === null ) {
932 $first--;
933 }
934 # Get net change
935 $chardiff = $rcObj->getCharacterDifference( $block[$first]->mAttribs['rc_old_len'],
936 $block[$last]->mAttribs['rc_new_len'] );
937
938 if( $chardiff == '' ) {
939 $r .= ' ';
940 } else {
941 $r .= ' ' . $chardiff. ' . . ';
942 }
943 }
944
945 $r .= $users;
946 $r .= $this->numberofWatchingusers($block[0]->numberofWatchingusers);
947
948 # Sub-entries
949 foreach( $block as $rcObj ) {
950 # Classes to apply -- TODO implement
951 $classes = array();
952 $type = $rcObj->mAttribs['rc_type'];
953
954 #$r .= '<tr><td valign="top">'.$this->spacerArrow();
955 $r .= '<tr><td></td><td class="mw-enhanced-rc">';
956 $r .= $this->recentChangesFlags( array(
957 'newpage' => $rcObj->mAttribs['rc_new'],
958 'minor' => $rcObj->mAttribs['rc_minor'],
959 'unpatrolled' => $rcObj->unpatrolled,
960 'bot' => $rcObj->mAttribs['rc_bot'],
961 ) );
962 $r .= '&#160;</td><td class="mw-enhanced-rc-nested"><span class="mw-enhanced-rc-time">';
963
964 $params = $queryParams;
965
966 if( $rcObj->mAttribs['rc_this_oldid'] != 0 ) {
967 $params['oldid'] = $rcObj->mAttribs['rc_this_oldid'];
968 }
969
970 # Log timestamp
971 if( $type == RC_LOG ) {
972 $link = $rcObj->timestamp;
973 # Revision link
974 } elseif( !ChangesList::userCan($rcObj,Revision::DELETED_TEXT) ) {
975 $link = '<span class="history-deleted">'.$rcObj->timestamp.'</span> ';
976 } else {
977 if ( $rcObj->unpatrolled && $type == RC_NEW) {
978 $params['rcid'] = $rcObj->mAttribs['rc_id'];
979 }
980
981 $link = Linker::linkKnown(
982 $rcObj->getTitle(),
983 $rcObj->timestamp,
984 array(),
985 $params
986 );
987 if( $this->isDeleted($rcObj,Revision::DELETED_TEXT) )
988 $link = '<span class="history-deleted">'.$link.'</span> ';
989 }
990 $r .= $link . '</span>';
991
992 if ( !$type == RC_LOG || $type == RC_NEW ) {
993 $r .= ' (';
994 $r .= $rcObj->curlink;
995 $r .= $this->message['pipe-separator'];
996 $r .= $rcObj->lastlink;
997 $r .= ')';
998 }
999 $r .= ' . . ';
1000
1001 # Character diff
1002 if( $wgRCShowChangedSize && $rcObj->getCharacterDifference() ) {
1003 $r .= $rcObj->getCharacterDifference() . ' . . ' ;
1004 }
1005
1006 if ( $rcObj->mAttribs['rc_type'] == RC_LOG ) {
1007 $r .= $this->insertLogEntry( $rcObj );
1008 } else {
1009 # User links
1010 $r .= $rcObj->userlink;
1011 $r .= $rcObj->usertalklink;
1012 $r .= $this->insertComment( $rcObj );
1013 }
1014
1015 # Rollback
1016 $this->insertRollback( $r, $rcObj );
1017 # Tags
1018 $this->insertTags( $r, $rcObj, $classes );
1019
1020 $r .= "</td></tr>\n";
1021 }
1022 $r .= "</table>\n";
1023
1024 $this->rcCacheIndex++;
1025
1026 wfProfileOut( __METHOD__ );
1027
1028 return $r;
1029 }
1030
1031 /**
1032 * Generate HTML for an arrow or placeholder graphic
1033 * @param $dir String: one of '', 'd', 'l', 'r'
1034 * @param $alt String: text
1035 * @param $title String: text
1036 * @return String: HTML <img> tag
1037 */
1038 protected function arrow( $dir, $alt='', $title='' ) {
1039 global $wgStylePath;
1040 $encUrl = htmlspecialchars( $wgStylePath . '/common/images/Arr_' . $dir . '.png' );
1041 $encAlt = htmlspecialchars( $alt );
1042 $encTitle = htmlspecialchars( $title );
1043 return "<img src=\"$encUrl\" width=\"12\" height=\"12\" alt=\"$encAlt\" title=\"$encTitle\" />";
1044 }
1045
1046 /**
1047 * Generate HTML for a right- or left-facing arrow,
1048 * depending on language direction.
1049 * @return String: HTML <img> tag
1050 */
1051 protected function sideArrow() {
1052 global $wgContLang;
1053 $dir = $wgContLang->isRTL() ? 'l' : 'r';
1054 return $this->arrow( $dir, '+', wfMsg( 'rc-enhanced-expand' ) );
1055 }
1056
1057 /**
1058 * Generate HTML for a down-facing arrow
1059 * depending on language direction.
1060 * @return String: HTML <img> tag
1061 */
1062 protected function downArrow() {
1063 return $this->arrow( 'd', '-', wfMsg( 'rc-enhanced-hide' ) );
1064 }
1065
1066 /**
1067 * Generate HTML for a spacer image
1068 * @return String: HTML <img> tag
1069 */
1070 protected function spacerArrow() {
1071 return $this->arrow( '', codepointToUtf8( 0xa0 ) ); // non-breaking space
1072 }
1073
1074 /**
1075 * Enhanced RC ungrouped line.
1076 *
1077 * @param $rcObj RecentChange
1078 * @return String: a HTML formated line (generated using $r)
1079 */
1080 protected function recentChangesBlockLine( $rcObj ) {
1081 global $wgRCShowChangedSize;
1082
1083 wfProfileIn( __METHOD__ );
1084 $query['curid'] = $rcObj->mAttribs['rc_cur_id'];
1085
1086 $type = $rcObj->mAttribs['rc_type'];
1087 $logType = $rcObj->mAttribs['rc_log_type'];
1088 if( $logType ) {
1089 # Log entry
1090 $classes = 'mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-log-' . $logType . '-' . $rcObj->mAttribs['rc_title'] );
1091 } else {
1092 $classes = 'mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-ns' . $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
1093 }
1094 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
1095 Html::openElement( 'tr' );
1096
1097 $r .= '<td class="mw-enhanced-rc">' . $this->spacerArrow();
1098 # Flag and Timestamp
1099 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
1100 $r .= '&#160;&#160;&#160;&#160;'; // 4 flags -> 4 spaces
1101 } else {
1102 $r .= $this->recentChangesFlags( array(
1103 'newpage' => $type == RC_NEW,
1104 'mino' => $rcObj->mAttribs['rc_minor'],
1105 'unpatrolled' => $rcObj->unpatrolled,
1106 'bot' => $rcObj->mAttribs['rc_bot'],
1107 ) );
1108 }
1109 $r .= '&#160;'.$rcObj->timestamp.'&#160;</td><td>';
1110 # Article or log link
1111 if( $logType ) {
1112 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
1113 $logname = LogPage::logName( $logType );
1114 $r .= '(' . Linker::linkKnown( $logtitle, htmlspecialchars( $logname ) ) . ')';
1115 } else {
1116 $this->insertArticleLink( $r, $rcObj, $rcObj->unpatrolled, $rcObj->watched );
1117 }
1118 # Diff and hist links
1119 if ( $type != RC_LOG ) {
1120 $r .= ' ('. $rcObj->difflink . $this->message['pipe-separator'];
1121 $query['action'] = 'history';
1122 $r .= Linker::linkKnown(
1123 $rcObj->getTitle(),
1124 $this->message['hist'],
1125 array(),
1126 $query
1127 ) . ')';
1128 }
1129 $r .= ' . . ';
1130 # Character diff
1131 if( $wgRCShowChangedSize && ($cd = $rcObj->getCharacterDifference()) ) {
1132 $r .= "$cd . . ";
1133 }
1134
1135 if ( $type == RC_LOG ) {
1136 $r .= $this->insertLogEntry( $rcObj );
1137 } else {
1138 $r .= ' '.$rcObj->userlink . $rcObj->usertalklink;
1139 $r .= $this->insertComment( $rcObj );
1140 $r .= $this->insertRollback( $r, $rcObj );
1141 }
1142
1143 # Tags
1144 $classes = explode( ' ', $classes );
1145 $this->insertTags( $r, $rcObj, $classes );
1146 # Show how many people are watching this if enabled
1147 $r .= $this->numberofWatchingusers($rcObj->numberofWatchingusers);
1148
1149 $r .= "</td></tr></table>\n";
1150
1151 wfProfileOut( __METHOD__ );
1152
1153 return $r;
1154 }
1155
1156 /**
1157 * If enhanced RC is in use, this function takes the previously cached
1158 * RC lines, arranges them, and outputs the HTML
1159 *
1160 * @return string
1161 */
1162 protected function recentChangesBlock() {
1163 if( count ( $this->rc_cache ) == 0 ) {
1164 return '';
1165 }
1166
1167 wfProfileIn( __METHOD__ );
1168
1169 $blockOut = '';
1170 foreach( $this->rc_cache as $block ) {
1171 if( count( $block ) < 2 ) {
1172 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
1173 } else {
1174 $blockOut .= $this->recentChangesBlockGroup( $block );
1175 }
1176 }
1177
1178 wfProfileOut( __METHOD__ );
1179
1180 return '<div>'.$blockOut.'</div>';
1181 }
1182
1183 /**
1184 * Returns text for the end of RC
1185 * If enhanced RC is in use, returns pretty much all the text
1186 */
1187 public function endRecentChangesList() {
1188 return $this->recentChangesBlock() . parent::endRecentChangesList();
1189 }
1190
1191 }