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