Fix for r92740: removed debugging code
[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 RequestContext
49 */
50 public function __construct( $obj ) {
51 if ( $obj instanceof RequestContext ) {
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 RequestContext to use
78 * @return ChangesList|EnhancedChangesList|OldChangesList derivative
79 */
80 public static function newFromContext( RequestContext $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 /**
229 * @param $s
230 * @param $rc RecentChange
231 * @return void
232 */
233 public function insertMove( &$s, $rc ) {
234 # Diff
235 $s .= '(' . $this->message['diff'] . ') (';
236 # Hist
237 $s .= Linker::linkKnown(
238 $rc->getMovedToTitle(),
239 $this->message['hist'],
240 array(),
241 array( 'action' => 'history' )
242 ) . ') . . ';
243 # "[[x]] moved to [[y]]"
244 $msg = ( $rc->mAttribs['rc_type'] == RC_MOVE ) ? '1movedto2' : '1movedto2_redir';
245 $s .= wfMsgHtml(
246 $msg,
247 Linker::linkKnown(
248 $rc->getTitle(),
249 null,
250 array(),
251 array( 'redirect' => 'no' )
252 ),
253 Linker::linkKnown( $rc->getMovedToTitle() )
254 );
255 }
256
257 public function insertDateHeader( &$s, $rc_timestamp ) {
258 # Make date header if necessary
259 $date = $this->getLang()->date( $rc_timestamp, true, true );
260 if( $date != $this->lastdate ) {
261 if( $this->lastdate != '' ) {
262 $s .= "</ul>\n";
263 }
264 $s .= Xml::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
265 $this->lastdate = $date;
266 $this->rclistOpen = true;
267 }
268 }
269
270 public function insertLog( &$s, $title, $logtype ) {
271 $logname = LogPage::logName( $logtype );
272 $s .= '(' . Linker::linkKnown( $title, htmlspecialchars( $logname ) ) . ')';
273 }
274
275 /**
276 * @param $s
277 * @param $rc RecentChange
278 * @param $unpatrolled
279 * @return void
280 */
281 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
282 # Diff link
283 if( $rc->mAttribs['rc_type'] == RC_NEW || $rc->mAttribs['rc_type'] == RC_LOG ) {
284 $diffLink = $this->message['diff'];
285 } elseif( !self::userCan($rc,Revision::DELETED_TEXT) ) {
286 $diffLink = $this->message['diff'];
287 } else {
288 $query = array(
289 'curid' => $rc->mAttribs['rc_cur_id'],
290 'diff' => $rc->mAttribs['rc_this_oldid'],
291 'oldid' => $rc->mAttribs['rc_last_oldid']
292 );
293
294 if( $unpatrolled ) {
295 $query['rcid'] = $rc->mAttribs['rc_id'];
296 };
297
298 $diffLink = Linker::linkKnown(
299 $rc->getTitle(),
300 $this->message['diff'],
301 array( 'tabindex' => $rc->counter ),
302 $query
303 );
304 }
305 $s .= '(' . $diffLink . $this->message['pipe-separator'];
306 # History link
307 $s .= Linker::linkKnown(
308 $rc->getTitle(),
309 $this->message['hist'],
310 array(),
311 array(
312 'curid' => $rc->mAttribs['rc_cur_id'],
313 'action' => 'history'
314 )
315 );
316 $s .= ') . . ';
317 }
318
319 /**
320 * @param $s
321 * @param $rc RecentChange
322 * @param $unpatrolled
323 * @param $watched
324 * @return void
325 */
326 public function insertArticleLink( &$s, &$rc, $unpatrolled, $watched ) {
327 # If it's a new article, there is no diff link, but if it hasn't been
328 # patrolled yet, we need to give users a way to do so
329 $params = array();
330
331 if ( $unpatrolled && $rc->mAttribs['rc_type'] == RC_NEW ) {
332 $params['rcid'] = $rc->mAttribs['rc_id'];
333 }
334
335 if( $this->isDeleted($rc,Revision::DELETED_TEXT) ) {
336 $articlelink = Linker::linkKnown(
337 $rc->getTitle(),
338 null,
339 array(),
340 $params
341 );
342 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
343 } else {
344 $articlelink = ' '. Linker::linkKnown(
345 $rc->getTitle(),
346 null,
347 array(),
348 $params
349 );
350 }
351 # Bolden pages watched by this user
352 if( $watched ) {
353 $articlelink = "<strong class=\"mw-watched\">{$articlelink}</strong>";
354 }
355 # RTL/LTR marker
356 $articlelink .= $this->getLang()->getDirMark();
357
358 wfRunHooks( 'ChangesListInsertArticleLink',
359 array(&$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched) );
360
361 $s .= " $articlelink";
362 }
363
364 /**
365 * @param $s
366 * @param $rc RecentChange
367 * @return void
368 */
369 public function insertTimestamp( &$s, $rc ) {
370 $s .= $this->message['semicolon-separator'] .
371 $this->getLang()->time( $rc->mAttribs['rc_timestamp'], true, true ) . ' . . ';
372 }
373
374 /** Insert links to user page, user talk page and eventually a blocking link
375 *
376 * @param $rc RecentChange
377 */
378 public function insertUserRelatedLinks( &$s, &$rc ) {
379 if( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
380 $s .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
381 } else {
382 $s .= Linker::userLink( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
383 $s .= Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
384 }
385 }
386
387 /** insert a formatted action
388 *
389 * @param $rc RecentChange
390 */
391 public function insertAction( &$s, &$rc ) {
392 if( $rc->mAttribs['rc_type'] == RC_LOG ) {
393 if( $this->isDeleted( $rc, LogPage::DELETED_ACTION ) ) {
394 $s .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-event' ) . '</span>';
395 } else {
396 $s .= ' '.LogPage::actionText( $rc->mAttribs['rc_log_type'], $rc->mAttribs['rc_log_action'],
397 $rc->getTitle(), $this->getSkin(), LogPage::extractParams( $rc->mAttribs['rc_params'] ), true, true );
398 }
399 }
400 }
401
402 /** insert a formatted comment
403 *
404 * @param $rc RecentChange
405 */
406 public function insertComment( &$s, &$rc ) {
407 if( $rc->mAttribs['rc_type'] != RC_MOVE && $rc->mAttribs['rc_type'] != RC_MOVE_OVER_REDIRECT ) {
408 if( $this->isDeleted( $rc, Revision::DELETED_COMMENT ) ) {
409 $s .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
410 } else {
411 $s .= Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
412 }
413 }
414 }
415
416 /**
417 * Check whether to enable recent changes patrol features
418 * @return Boolean
419 */
420 public static function usePatrol() {
421 global $wgUser;
422 return $wgUser->useRCPatrol();
423 }
424
425 /**
426 * Returns the string which indicates the number of watching users
427 */
428 protected function numberofWatchingusers( $count ) {
429 static $cache = array();
430 if( $count > 0 ) {
431 if( !isset( $cache[$count] ) ) {
432 $cache[$count] = wfMsgExt( 'number_of_watching_users_RCview',
433 array('parsemag', 'escape' ), $this->getLang()->formatNum( $count ) );
434 }
435 return $cache[$count];
436 } else {
437 return '';
438 }
439 }
440
441 /**
442 * Determine if said field of a revision is hidden
443 * @param $rc RCCacheEntry
444 * @param $field Integer: one of DELETED_* bitfield constants
445 * @return Boolean
446 */
447 public static function isDeleted( $rc, $field ) {
448 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
449 }
450
451 /**
452 * Determine if the current user is allowed to view a particular
453 * field of this revision, if it's marked as deleted.
454 * @param $rc RCCacheEntry
455 * @param $field Integer
456 * @return Boolean
457 */
458 public static function userCan( $rc, $field ) {
459 if( $rc->mAttribs['rc_type'] == RC_LOG ) {
460 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field );
461 } else {
462 return Revision::userCanBitfield( $rc->mAttribs['rc_deleted'], $field );
463 }
464 }
465
466 protected function maybeWatchedLink( $link, $watched = false ) {
467 if( $watched ) {
468 return '<strong class="mw-watched">' . $link . '</strong>';
469 } else {
470 return '<span class="mw-rc-unwatched">' . $link . '</span>';
471 }
472 }
473
474 /** Inserts a rollback link
475 *
476 * @param $s
477 * @param $rc RecentChange
478 */
479 public function insertRollback( &$s, &$rc ) {
480 if( !$rc->mAttribs['rc_new'] && $rc->mAttribs['rc_this_oldid'] && $rc->mAttribs['rc_cur_id'] ) {
481 $page = $rc->getTitle();
482 /** Check for rollback and edit permissions, disallow special pages, and only
483 * show a link on the top-most revision */
484 if ( $this->getUser()->isAllowed('rollback') && $rc->mAttribs['page_latest'] == $rc->mAttribs['rc_this_oldid'] )
485 {
486 $rev = new Revision( array(
487 'id' => $rc->mAttribs['rc_this_oldid'],
488 'user' => $rc->mAttribs['rc_user'],
489 'user_text' => $rc->mAttribs['rc_user_text'],
490 'deleted' => $rc->mAttribs['rc_deleted']
491 ) );
492 $rev->setTitle( $page );
493 $s .= ' '.Linker::generateRollback( $rev, $this->getContext() );
494 }
495 }
496 }
497
498 /**
499 * @param $s
500 * @param $rc RecentChange
501 * @param $classes
502 * @return
503 */
504 public function insertTags( &$s, &$rc, &$classes ) {
505 if ( empty($rc->mAttribs['ts_tags']) )
506 return;
507
508 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $rc->mAttribs['ts_tags'], 'changeslist' );
509 $classes = array_merge( $classes, $newClasses );
510 $s .= ' ' . $tagSummary;
511 }
512
513 public function insertExtra( &$s, &$rc, &$classes ) {
514 ## Empty, used for subclassers to add anything special.
515 }
516 }
517
518
519 /**
520 * Generate a list of changes using the good old system (no javascript)
521 */
522 class OldChangesList extends ChangesList {
523 /**
524 * Format a line using the old system (aka without any javascript).
525 *
526 * @param $rc RecentChange
527 */
528 public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
529 global $wgRCShowChangedSize;
530 wfProfileIn( __METHOD__ );
531 # Should patrol-related stuff be shown?
532 $unpatrolled = $this->getUser()->useRCPatrol() && !$rc->mAttribs['rc_patrolled'];
533
534 $dateheader = ''; // $s now contains only <li>...</li>, for hooks' convenience.
535 $this->insertDateHeader( $dateheader, $rc->mAttribs['rc_timestamp'] );
536
537 $s = '';
538 $classes = array();
539 // use mw-line-even/mw-line-odd class only if linenumber is given (feature from bug 14468)
540 if( $linenumber ) {
541 if( $linenumber & 1 ) {
542 $classes[] = 'mw-line-odd';
543 }
544 else {
545 $classes[] = 'mw-line-even';
546 }
547 }
548
549 // Moved pages
550 if( $rc->mAttribs['rc_type'] == RC_MOVE || $rc->mAttribs['rc_type'] == RC_MOVE_OVER_REDIRECT ) {
551 $this->insertMove( $s, $rc );
552 // Log entries
553 } elseif( $rc->mAttribs['rc_log_type'] ) {
554 $logtitle = Title::newFromText( 'Log/'.$rc->mAttribs['rc_log_type'], NS_SPECIAL );
555 $this->insertLog( $s, $logtitle, $rc->mAttribs['rc_log_type'] );
556 // Log entries (old format) or log targets, and special pages
557 } elseif( $rc->mAttribs['rc_namespace'] == NS_SPECIAL ) {
558 list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $rc->mAttribs['rc_title'] );
559 if( $name == 'Log' ) {
560 $this->insertLog( $s, $rc->getTitle(), $subpage );
561 }
562 // Regular entries
563 } else {
564 $this->insertDiffHist( $s, $rc, $unpatrolled );
565 # M, N, b and ! (minor, new, bot and unpatrolled)
566 $s .= $this->recentChangesFlags(
567 array(
568 'newpage' => $rc->mAttribs['rc_new'],
569 'minor' => $rc->mAttribs['rc_minor'],
570 'unpatrolled' => $unpatrolled,
571 'bot' => $rc->mAttribs['rc_bot']
572 ),
573 ''
574 );
575 $this->insertArticleLink( $s, $rc, $unpatrolled, $watched );
576 }
577 # Edit/log timestamp
578 $this->insertTimestamp( $s, $rc );
579 # Bytes added or removed
580 if( $wgRCShowChangedSize ) {
581 $cd = $rc->getCharacterDifference();
582 if( $cd != '' ) {
583 $s .= "$cd . . ";
584 }
585 }
586 # User tool links
587 $this->insertUserRelatedLinks( $s, $rc );
588 # LTR/RTL direction mark
589 $s .= $this->getLang()->getDirMark();
590 # Log action text (if any)
591 $this->insertAction( $s, $rc );
592 # Edit or log comment
593 $this->insertComment( $s, $rc );
594 # Tags
595 $this->insertTags( $s, $rc, $classes );
596 # Rollback
597 $this->insertRollback( $s, $rc );
598 # For subclasses
599 $this->insertExtra( $s, $rc, $classes );
600
601 # How many users watch this page
602 if( $rc->numberofWatchingusers > 0 ) {
603 $s .= ' ' . wfMsgExt( 'number_of_watching_users_RCview',
604 array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $rc->numberofWatchingusers ) );
605 }
606
607 if( $this->watchlist ) {
608 $classes[] = Sanitizer::escapeClass( 'watchlist-'.$rc->mAttribs['rc_namespace'].'-'.$rc->mAttribs['rc_title'] );
609 }
610
611 wfRunHooks( 'OldChangesListRecentChangesLine', array(&$this, &$s, $rc) );
612
613 wfProfileOut( __METHOD__ );
614 return "$dateheader<li class=\"".implode( ' ', $classes )."\">".$s."</li>\n";
615 }
616 }
617
618
619 /**
620 * Generate a list of changes using an Enhanced system (uses javascript).
621 */
622 class EnhancedChangesList extends ChangesList {
623 /**
624 * Add the JavaScript file for enhanced changeslist
625 * @return String
626 */
627 public function beginRecentChangesList() {
628 $this->rc_cache = array();
629 $this->rcMoveIndex = 0;
630 $this->rcCacheIndex = 0;
631 $this->lastdate = '';
632 $this->rclistOpen = false;
633 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
634 return '';
635 }
636 /**
637 * Format a line for enhanced recentchange (aka with javascript and block of lines).
638 *
639 * @param $baseRC RecentChange
640 * @param $watched bool
641 *
642 * @return string
643 */
644 public function recentChangesLine( &$baseRC, $watched = false ) {
645 wfProfileIn( __METHOD__ );
646
647 # Create a specialised object
648 $rc = RCCacheEntry::newFromParent( $baseRC );
649
650 $curIdEq = array( 'curid' => $rc->mAttribs['rc_cur_id'] );
651
652 # If it's a new day, add the headline and flush the cache
653 $date = $this->getLang()->date( $rc->mAttribs['rc_timestamp'], true );
654 $ret = '';
655 if( $date != $this->lastdate ) {
656 # Process current cache
657 $ret = $this->recentChangesBlock();
658 $this->rc_cache = array();
659 $ret .= Xml::element( 'h4', null, $date ) . "\n";
660 $this->lastdate = $date;
661 }
662
663 # Should patrol-related stuff be shown?
664 if( $this->getUser()->useRCPatrol() ) {
665 $rc->unpatrolled = !$rc->mAttribs['rc_patrolled'];
666 } else {
667 $rc->unpatrolled = false;
668 }
669
670 $showdifflinks = true;
671 # Make article link
672 $type = $rc->mAttribs['rc_type'];
673 $logType = $rc->mAttribs['rc_log_type'];
674 // Page moves
675 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
676 $msg = ( $type == RC_MOVE ) ? "1movedto2" : "1movedto2_redir";
677 $clink = wfMsg( $msg, Linker::linkKnown( $rc->getTitle(), null,
678 array(), array( 'redirect' => 'no' ) ),
679 Linker::linkKnown( $rc->getMovedToTitle() ) );
680 // New unpatrolled pages
681 } elseif( $rc->unpatrolled && $type == RC_NEW ) {
682 $clink = Linker::linkKnown( $rc->getTitle(), null, array(),
683 array( 'rcid' => $rc->mAttribs['rc_id'] ) );
684 // Log entries
685 } elseif( $type == RC_LOG ) {
686 if( $logType ) {
687 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
688 $clink = '(' . Linker::linkKnown( $logtitle,
689 LogPage::logName( $logType ) ) . ')';
690 } else {
691 $clink = Linker::link( $rc->getTitle() );
692 }
693 $watched = false;
694 // Log entries (old format) and special pages
695 } elseif( $rc->mAttribs['rc_namespace'] == NS_SPECIAL ) {
696 list( $specialName, $logtype ) = SpecialPageFactory::resolveAlias( $rc->mAttribs['rc_title'] );
697 if ( $specialName == 'Log' ) {
698 # Log updates, etc
699 $logname = LogPage::logName( $logtype );
700 $clink = '(' . Linker::linkKnown( $rc->getTitle(), $logname ) . ')';
701 } else {
702 wfDebug( "Unexpected special page in recentchanges\n" );
703 $clink = '';
704 }
705 // Edits
706 } else {
707 $clink = Linker::linkKnown( $rc->getTitle() );
708 }
709
710 # Don't show unusable diff links
711 if ( !ChangesList::userCan($rc,Revision::DELETED_TEXT) ) {
712 $showdifflinks = false;
713 }
714
715 $time = $this->getLang()->time( $rc->mAttribs['rc_timestamp'], true, true );
716 $rc->watched = $watched;
717 $rc->link = $clink;
718 $rc->timestamp = $time;
719 $rc->numberofWatchingusers = $baseRC->numberofWatchingusers;
720
721 # Make "cur" and "diff" links. Do not use link(), it is too slow if
722 # called too many times (50% of CPU time on RecentChanges!).
723 $thisOldid = $rc->mAttribs['rc_this_oldid'];
724 $lastOldid = $rc->mAttribs['rc_last_oldid'];
725 if( $rc->unpatrolled ) {
726 $rcIdQuery = array( 'rcid' => $rc->mAttribs['rc_id'] );
727 } else {
728 $rcIdQuery = array();
729 }
730 $querycur = $curIdEq + array( 'diff' => '0', 'oldid' => $thisOldid );
731 $querydiff = $curIdEq + array( 'diff' => $thisOldid, 'oldid' =>
732 $lastOldid ) + $rcIdQuery;
733
734 if( !$showdifflinks ) {
735 $curLink = $this->message['cur'];
736 $diffLink = $this->message['diff'];
737 } elseif( in_array( $type, array( RC_NEW, RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT ) ) ) {
738 if ( $type != RC_NEW ) {
739 $curLink = $this->message['cur'];
740 } else {
741 $curUrl = htmlspecialchars( $rc->getTitle()->getLinkUrl( $querycur ) );
742 $curLink = "<a href=\"$curUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['cur']}</a>";
743 }
744 $diffLink = $this->message['diff'];
745 } else {
746 $diffUrl = htmlspecialchars( $rc->getTitle()->getLinkUrl( $querydiff ) );
747 $curUrl = htmlspecialchars( $rc->getTitle()->getLinkUrl( $querycur ) );
748 $diffLink = "<a href=\"$diffUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['diff']}</a>";
749 $curLink = "<a href=\"$curUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['cur']}</a>";
750 }
751
752 # Make "last" link
753 if( !$showdifflinks || !$lastOldid ) {
754 $lastLink = $this->message['last'];
755 } elseif( in_array( $type, array( RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT ) ) ) {
756 $lastLink = $this->message['last'];
757 } else {
758 $lastLink = Linker::linkKnown( $rc->getTitle(), $this->message['last'],
759 array(), $curIdEq + array('diff' => $thisOldid, 'oldid' => $lastOldid) + $rcIdQuery );
760 }
761
762 # Make user links
763 if( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
764 $rc->userlink = ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
765 } else {
766 $rc->userlink = Linker::userLink( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
767 $rc->usertalklink = Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
768 }
769
770 $rc->lastlink = $lastLink;
771 $rc->curlink = $curLink;
772 $rc->difflink = $diffLink;
773
774 # Put accumulated information into the cache, for later display
775 # Page moves go on their own line
776 $title = $rc->getTitle();
777 $secureName = $title->getPrefixedDBkey();
778 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
779 # Use an @ character to prevent collision with page names
780 $this->rc_cache['@@' . ($this->rcMoveIndex++)] = array($rc);
781 } else {
782 # Logs are grouped by type
783 if( $type == RC_LOG ){
784 $secureName = SpecialPage::getTitleFor( 'Log', $logType )->getPrefixedDBkey();
785 }
786 if( !isset( $this->rc_cache[$secureName] ) ) {
787 $this->rc_cache[$secureName] = array();
788 }
789
790 array_push( $this->rc_cache[$secureName], $rc );
791 }
792
793 wfProfileOut( __METHOD__ );
794
795 return $ret;
796 }
797
798 /**
799 * Enhanced RC group
800 */
801 protected function recentChangesBlockGroup( $block ) {
802 global $wgRCShowChangedSize;
803
804 wfProfileIn( __METHOD__ );
805
806 # Add the namespace and title of the block as part of the class
807 if ( $block[0]->mAttribs['rc_log_type'] ) {
808 # Log entry
809 $classes = 'mw-collapsible mw-collapsed mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-log-' . $block[0]->mAttribs['rc_log_type'] . '-' . $block[0]->mAttribs['rc_title'] );
810 } else {
811 $classes = 'mw-collapsible mw-collapsed mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-ns' . $block[0]->mAttribs['rc_namespace'] . '-' . $block[0]->mAttribs['rc_title'] );
812 }
813 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
814 Html::openElement( 'tr' );
815
816 # Collate list of users
817 $userlinks = array();
818 # Other properties
819 $unpatrolled = false;
820 $isnew = false;
821 $curId = $currentRevision = 0;
822 # Some catalyst variables...
823 $namehidden = true;
824 $allLogs = true;
825 foreach( $block as $rcObj ) {
826 $oldid = $rcObj->mAttribs['rc_last_oldid'];
827 if( $rcObj->mAttribs['rc_new'] ) {
828 $isnew = true;
829 }
830 // If all log actions to this page were hidden, then don't
831 // give the name of the affected page for this block!
832 if( !$this->isDeleted( $rcObj, LogPage::DELETED_ACTION ) ) {
833 $namehidden = false;
834 }
835 $u = $rcObj->userlink;
836 if( !isset( $userlinks[$u] ) ) {
837 $userlinks[$u] = 0;
838 }
839 if( $rcObj->unpatrolled ) {
840 $unpatrolled = true;
841 }
842 if( $rcObj->mAttribs['rc_type'] != RC_LOG ) {
843 $allLogs = false;
844 }
845 # Get the latest entry with a page_id and oldid
846 # since logs may not have these.
847 if( !$curId && $rcObj->mAttribs['rc_cur_id'] ) {
848 $curId = $rcObj->mAttribs['rc_cur_id'];
849 }
850 if( !$currentRevision && $rcObj->mAttribs['rc_this_oldid'] ) {
851 $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
852 }
853
854 $bot = $rcObj->mAttribs['rc_bot'];
855 $userlinks[$u]++;
856 }
857
858 # Sort the list and convert to text
859 krsort( $userlinks );
860 asort( $userlinks );
861 $users = array();
862 foreach( $userlinks as $userlink => $count) {
863 $text = $userlink;
864 $text .= $this->getLang()->getDirMark();
865 if( $count > 1 ) {
866 $text .= ' (' . $this->getLang()->formatNum( $count ) . '×)';
867 }
868 array_push( $users, $text );
869 }
870
871 $users = ' <span class="changedby">[' .
872 implode( $this->message['semicolon-separator'], $users ) . ']</span>';
873
874 # Title for <a> tags
875 $expandTitle = htmlspecialchars( wfMsg( 'rc-enhanced-expand' ) );
876 $closeTitle = htmlspecialchars( wfMsg( 'rc-enhanced-hide' ) );
877
878 $tl = "<span class='mw-collapsible-toggle'>"
879 . "<span class='mw-rc-openarrow'>"
880 . "<a href='#' title='$expandTitle'>{$this->sideArrow()}</a>"
881 . "</span><span class='mw-rc-closearrow'>"
882 . "<a href='#' title='$closeTitle'>{$this->downArrow()}</a>"
883 . "</span></span>";
884 $r .= "<td>$tl</td>";
885
886 # Main line
887 $r .= '<td class="mw-enhanced-rc">' . $this->recentChangesFlags( array(
888 'newpage' => $isnew,
889 'minor' => false,
890 'unpatrolled' => $unpatrolled,
891 'bot' => $bot ,
892 ) );
893
894 # Timestamp
895 $r .= '&#160;'.$block[0]->timestamp.'&#160;</td><td>';
896
897 # Article link
898 if( $namehidden ) {
899 $r .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-event' ) . '</span>';
900 } elseif( $allLogs ) {
901 $r .= $this->maybeWatchedLink( $block[0]->link, $block[0]->watched );
902 } else {
903 $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled, $block[0]->watched );
904 }
905
906 $r .= $this->getLang()->getDirMark();
907
908 $queryParams['curid'] = $curId;
909 # Changes message
910 $n = count($block);
911 static $nchanges = array();
912 if ( !isset( $nchanges[$n] ) ) {
913 $nchanges[$n] = wfMsgExt( 'nchanges', array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $n ) );
914 }
915 # Total change link
916 $r .= ' ';
917 if( !$allLogs ) {
918 $r .= '(';
919 if( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT ) ) {
920 $r .= $nchanges[$n];
921 } elseif( $isnew ) {
922 $r .= $nchanges[$n];
923 } else {
924 $params = $queryParams;
925 $params['diff'] = $currentRevision;
926 $params['oldid'] = $oldid;
927
928 $r .= Linker::link(
929 $block[0]->getTitle(),
930 $nchanges[$n],
931 array(),
932 $params,
933 array( 'known', 'noclasses' )
934 );
935 }
936 }
937
938 # History
939 if( $allLogs ) {
940 // don't show history link for logs
941 } elseif( $namehidden || !$block[0]->getTitle()->exists() ) {
942 $r .= $this->message['pipe-separator'] . $this->message['hist'] . ')';
943 } else {
944 $params = $queryParams;
945 $params['action'] = 'history';
946
947 $r .= $this->message['pipe-separator'] .
948 Linker::linkKnown(
949 $block[0]->getTitle(),
950 $this->message['hist'],
951 array(),
952 $params
953 ) . ')';
954 }
955 $r .= ' . . ';
956
957 # Character difference (does not apply if only log items)
958 if( $wgRCShowChangedSize && !$allLogs ) {
959 $last = 0;
960 $first = count($block) - 1;
961 # Some events (like logs) have an "empty" size, so we need to skip those...
962 while( $last < $first && $block[$last]->mAttribs['rc_new_len'] === null ) {
963 $last++;
964 }
965 while( $first > $last && $block[$first]->mAttribs['rc_old_len'] === null ) {
966 $first--;
967 }
968 # Get net change
969 $chardiff = $rcObj->getCharacterDifference( $block[$first]->mAttribs['rc_old_len'],
970 $block[$last]->mAttribs['rc_new_len'] );
971
972 if( $chardiff == '' ) {
973 $r .= ' ';
974 } else {
975 $r .= ' ' . $chardiff. ' . . ';
976 }
977 }
978
979 $r .= $users;
980 $r .= $this->numberofWatchingusers($block[0]->numberofWatchingusers);
981
982 # Sub-entries
983 foreach( $block as $rcObj ) {
984 # Classes to apply -- TODO implement
985 $classes = array();
986 $type = $rcObj->mAttribs['rc_type'];
987
988 #$r .= '<tr><td valign="top">'.$this->spacerArrow();
989 $r .= '<tr><td></td><td class="mw-enhanced-rc">';
990 $r .= $this->recentChangesFlags( array(
991 'newpage' => $rcObj->mAttribs['rc_new'],
992 'minor' => $rcObj->mAttribs['rc_minor'],
993 'unpatrolled' => $rcObj->unpatrolled,
994 'bot' => $rcObj->mAttribs['rc_bot'],
995 ) );
996 $r .= '&#160;</td><td class="mw-enhanced-rc-nested"><span class="mw-enhanced-rc-time">';
997
998 $params = $queryParams;
999
1000 if( $rcObj->mAttribs['rc_this_oldid'] != 0 ) {
1001 $params['oldid'] = $rcObj->mAttribs['rc_this_oldid'];
1002 }
1003
1004 # Log timestamp
1005 if( $type == RC_LOG ) {
1006 $link = $rcObj->timestamp;
1007 # Revision link
1008 } elseif( !ChangesList::userCan($rcObj,Revision::DELETED_TEXT) ) {
1009 $link = '<span class="history-deleted">'.$rcObj->timestamp.'</span> ';
1010 } else {
1011 if ( $rcObj->unpatrolled && $type == RC_NEW) {
1012 $params['rcid'] = $rcObj->mAttribs['rc_id'];
1013 }
1014
1015 $link = Linker::linkKnown(
1016 $rcObj->getTitle(),
1017 $rcObj->timestamp,
1018 array(),
1019 $params
1020 );
1021 if( $this->isDeleted($rcObj,Revision::DELETED_TEXT) )
1022 $link = '<span class="history-deleted">'.$link.'</span> ';
1023 }
1024 $r .= $link . '</span>';
1025
1026 if ( !$type == RC_LOG || $type == RC_NEW ) {
1027 $r .= ' (';
1028 $r .= $rcObj->curlink;
1029 $r .= $this->message['pipe-separator'];
1030 $r .= $rcObj->lastlink;
1031 $r .= ')';
1032 }
1033 $r .= ' . . ';
1034
1035 # Character diff
1036 if( $wgRCShowChangedSize && $rcObj->getCharacterDifference() ) {
1037 $r .= $rcObj->getCharacterDifference() . ' . . ' ;
1038 }
1039
1040 # User links
1041 $r .= $rcObj->userlink;
1042 $r .= $rcObj->usertalklink;
1043 // log action
1044 $this->insertAction( $r, $rcObj );
1045 // log comment
1046 $this->insertComment( $r, $rcObj );
1047 # Rollback
1048 $this->insertRollback( $r, $rcObj );
1049 # Tags
1050 $this->insertTags( $r, $rcObj, $classes );
1051
1052 $r .= "</td></tr>\n";
1053 }
1054 $r .= "</table>\n";
1055
1056 $this->rcCacheIndex++;
1057
1058 wfProfileOut( __METHOD__ );
1059
1060 return $r;
1061 }
1062
1063 /**
1064 * Generate HTML for an arrow or placeholder graphic
1065 * @param $dir String: one of '', 'd', 'l', 'r'
1066 * @param $alt String: text
1067 * @param $title String: text
1068 * @return String: HTML <img> tag
1069 */
1070 protected function arrow( $dir, $alt='', $title='' ) {
1071 global $wgStylePath;
1072 $encUrl = htmlspecialchars( $wgStylePath . '/common/images/Arr_' . $dir . '.png' );
1073 $encAlt = htmlspecialchars( $alt );
1074 $encTitle = htmlspecialchars( $title );
1075 return "<img src=\"$encUrl\" width=\"12\" height=\"12\" alt=\"$encAlt\" title=\"$encTitle\" />";
1076 }
1077
1078 /**
1079 * Generate HTML for a right- or left-facing arrow,
1080 * depending on language direction.
1081 * @return String: HTML <img> tag
1082 */
1083 protected function sideArrow() {
1084 global $wgContLang;
1085 $dir = $wgContLang->isRTL() ? 'l' : 'r';
1086 return $this->arrow( $dir, '+', wfMsg( 'rc-enhanced-expand' ) );
1087 }
1088
1089 /**
1090 * Generate HTML for a down-facing arrow
1091 * depending on language direction.
1092 * @return String: HTML <img> tag
1093 */
1094 protected function downArrow() {
1095 return $this->arrow( 'd', '-', wfMsg( 'rc-enhanced-hide' ) );
1096 }
1097
1098 /**
1099 * Generate HTML for a spacer image
1100 * @return String: HTML <img> tag
1101 */
1102 protected function spacerArrow() {
1103 return $this->arrow( '', codepointToUtf8( 0xa0 ) ); // non-breaking space
1104 }
1105
1106 /**
1107 * Enhanced RC ungrouped line.
1108 *
1109 * @param $rcObj RecentChange
1110 * @return String: a HTML formated line (generated using $r)
1111 */
1112 protected function recentChangesBlockLine( $rcObj ) {
1113 global $wgRCShowChangedSize;
1114
1115 wfProfileIn( __METHOD__ );
1116 $query['curid'] = $rcObj->mAttribs['rc_cur_id'];
1117
1118 $type = $rcObj->mAttribs['rc_type'];
1119 $logType = $rcObj->mAttribs['rc_log_type'];
1120 if( $logType ) {
1121 # Log entry
1122 $classes = 'mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-log-' . $logType . '-' . $rcObj->mAttribs['rc_title'] );
1123 } else {
1124 $classes = 'mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-ns' . $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
1125 }
1126 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
1127 Html::openElement( 'tr' );
1128
1129 $r .= '<td class="mw-enhanced-rc">' . $this->spacerArrow();
1130 # Flag and Timestamp
1131 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
1132 $r .= '&#160;&#160;&#160;&#160;'; // 4 flags -> 4 spaces
1133 } else {
1134 $r .= $this->recentChangesFlags( array(
1135 'newpage' => $type == RC_NEW,
1136 'mino' => $rcObj->mAttribs['rc_minor'],
1137 'unpatrolled' => $rcObj->unpatrolled,
1138 'bot' => $rcObj->mAttribs['rc_bot'],
1139 ) );
1140 }
1141 $r .= '&#160;'.$rcObj->timestamp.'&#160;</td><td>';
1142 # Article or log link
1143 if( $logType ) {
1144 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
1145 $logname = LogPage::logName( $logType );
1146 $r .= '(' . Linker::linkKnown( $logtitle, htmlspecialchars( $logname ) ) . ')';
1147 } else {
1148 $this->insertArticleLink( $r, $rcObj, $rcObj->unpatrolled, $rcObj->watched );
1149 }
1150 # Diff and hist links
1151 if ( $type != RC_LOG ) {
1152 $r .= ' ('. $rcObj->difflink . $this->message['pipe-separator'];
1153 $query['action'] = 'history';
1154 $r .= Linker::linkKnown(
1155 $rcObj->getTitle(),
1156 $this->message['hist'],
1157 array(),
1158 $query
1159 ) . ')';
1160 }
1161 $r .= ' . . ';
1162 # Character diff
1163 if( $wgRCShowChangedSize && ($cd = $rcObj->getCharacterDifference()) ) {
1164 $r .= "$cd . . ";
1165 }
1166 # User/talk
1167 $r .= ' '.$rcObj->userlink . $rcObj->usertalklink;
1168 # Log action (if any)
1169 if( $logType ) {
1170 if( $this->isDeleted($rcObj,LogPage::DELETED_ACTION) ) {
1171 $r .= ' <span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
1172 } else {
1173 $r .= ' ' . LogPage::actionText( $logType, $rcObj->mAttribs['rc_log_action'], $rcObj->getTitle(),
1174 $this->getSkin(), LogPage::extractParams( $rcObj->mAttribs['rc_params'] ), true, true );
1175 }
1176 }
1177 $this->insertComment( $r, $rcObj );
1178 $this->insertRollback( $r, $rcObj );
1179 # Tags
1180 $classes = explode( ' ', $classes );
1181 $this->insertTags( $r, $rcObj, $classes );
1182 # Show how many people are watching this if enabled
1183 $r .= $this->numberofWatchingusers($rcObj->numberofWatchingusers);
1184
1185 $r .= "</td></tr></table>\n";
1186
1187 wfProfileOut( __METHOD__ );
1188
1189 return $r;
1190 }
1191
1192 /**
1193 * If enhanced RC is in use, this function takes the previously cached
1194 * RC lines, arranges them, and outputs the HTML
1195 *
1196 * @return string
1197 */
1198 protected function recentChangesBlock() {
1199 if( count ( $this->rc_cache ) == 0 ) {
1200 return '';
1201 }
1202
1203 wfProfileIn( __METHOD__ );
1204
1205 $blockOut = '';
1206 foreach( $this->rc_cache as $block ) {
1207 if( count( $block ) < 2 ) {
1208 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
1209 } else {
1210 $blockOut .= $this->recentChangesBlockGroup( $block );
1211 }
1212 }
1213
1214 wfProfileOut( __METHOD__ );
1215
1216 return '<div>'.$blockOut.'</div>';
1217 }
1218
1219 /**
1220 * Returns text for the end of RC
1221 * If enhanced RC is in use, returns pretty much all the text
1222 */
1223 public function endRecentChangesList() {
1224 return $this->recentChangesBlock() . parent::endRecentChangesList();
1225 }
1226
1227 }