Follow-up I5b02aa914916f64492c85ce6dcc3272b6406551a (#4335), also put a link in the...
[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 string|User 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 .= $this->msg( 'parentheses' )->rawParams( Linker::linkKnown( $title, $logname ) )->escaped();
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 $diffhist = $diffLink . $this->message['pipe-separator'];
288 # History link
289 $diffhist .= 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 .= $this->msg( 'parentheses' )->rawParams( $diffhist )->escaped() . ' . . ';
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 * @return string
368 */
369 public function insertLogEntry( $rc ) {
370 $formatter = LogFormatter::newFromRow( $rc->mAttribs );
371 $formatter->setShowUserToolLinks( true );
372 $mark = $this->getLanguage()->getDirMark();
373 return $formatter->getActionText() . " $mark" . $formatter->getComment();
374 }
375
376 /**
377 * Insert a formatted comment
378 * @param $rc RecentChange
379 * @return string
380 */
381 public function insertComment( $rc ) {
382 if( $rc->mAttribs['rc_type'] != RC_MOVE && $rc->mAttribs['rc_type'] != RC_MOVE_OVER_REDIRECT ) {
383 if( $this->isDeleted( $rc, Revision::DELETED_COMMENT ) ) {
384 return ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
385 } else {
386 return Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
387 }
388 }
389 }
390
391 /**
392 * Check whether to enable recent changes patrol features
393 * @return Boolean
394 */
395 public static function usePatrol() {
396 global $wgUser;
397 return $wgUser->useRCPatrol();
398 }
399
400 /**
401 * Returns the string which indicates the number of watching users
402 * @return string
403 */
404 protected function numberofWatchingusers( $count ) {
405 static $cache = array();
406 if( $count > 0 ) {
407 if( !isset( $cache[$count] ) ) {
408 $cache[$count] = wfMsgExt( 'number_of_watching_users_RCview',
409 array('parsemag', 'escape' ), $this->getLanguage()->formatNum( $count ) );
410 }
411 return $cache[$count];
412 } else {
413 return '';
414 }
415 }
416
417 /**
418 * Determine if said field of a revision is hidden
419 * @param $rc RCCacheEntry
420 * @param $field Integer: one of DELETED_* bitfield constants
421 * @return Boolean
422 */
423 public static function isDeleted( $rc, $field ) {
424 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
425 }
426
427 /**
428 * Determine if the current user is allowed to view a particular
429 * field of this revision, if it's marked as deleted.
430 * @param $rc RCCacheEntry
431 * @param $field Integer
432 * @param $user User object to check, or null to use $wgUser
433 * @return Boolean
434 */
435 public static function userCan( $rc, $field, User $user = null ) {
436 if( $rc->mAttribs['rc_type'] == RC_LOG ) {
437 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
438 } else {
439 return Revision::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
440 }
441 }
442
443 /**
444 * @param $link string
445 * @param $watched bool
446 * @return string
447 */
448 protected function maybeWatchedLink( $link, $watched = false ) {
449 if( $watched ) {
450 return '<strong class="mw-watched">' . $link . '</strong>';
451 } else {
452 return '<span class="mw-rc-unwatched">' . $link . '</span>';
453 }
454 }
455
456 /** Inserts a rollback link
457 *
458 * @param $s string
459 * @param $rc RecentChange
460 */
461 public function insertRollback( &$s, &$rc ) {
462 if( !$rc->mAttribs['rc_new'] && $rc->mAttribs['rc_this_oldid'] && $rc->mAttribs['rc_cur_id'] ) {
463 $page = $rc->getTitle();
464 /** Check for rollback and edit permissions, disallow special pages, and only
465 * show a link on the top-most revision */
466 if ( $this->getUser()->isAllowed('rollback') && $rc->mAttribs['page_latest'] == $rc->mAttribs['rc_this_oldid'] )
467 {
468 $rev = new Revision( array(
469 'id' => $rc->mAttribs['rc_this_oldid'],
470 'user' => $rc->mAttribs['rc_user'],
471 'user_text' => $rc->mAttribs['rc_user_text'],
472 'deleted' => $rc->mAttribs['rc_deleted']
473 ) );
474 $rev->setTitle( $page );
475 $s .= ' '.Linker::generateRollback( $rev, $this->getContext() );
476 }
477 }
478 }
479
480 /**
481 * @param $s string
482 * @param $rc RecentChange
483 * @param $classes
484 */
485 public function insertTags( &$s, &$rc, &$classes ) {
486 if ( empty($rc->mAttribs['ts_tags']) )
487 return;
488
489 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $rc->mAttribs['ts_tags'], 'changeslist' );
490 $classes = array_merge( $classes, $newClasses );
491 $s .= ' ' . $tagSummary;
492 }
493
494 public function insertExtra( &$s, &$rc, &$classes ) {
495 ## Empty, used for subclassers to add anything special.
496 }
497
498 protected function showAsUnpatrolled( RecentChange $rc ) {
499 $unpatrolled = false;
500 if ( !$rc->mAttribs['rc_patrolled'] ) {
501 if ( $this->getUser()->useRCPatrol() ) {
502 $unpatrolled = true;
503 } elseif ( $this->getUser()->useNPPatrol() && $rc->mAttribs['rc_new'] ) {
504 $unpatrolled = true;
505 }
506 }
507 return $unpatrolled;
508 }
509 }
510
511
512 /**
513 * Generate a list of changes using the good old system (no javascript)
514 */
515 class OldChangesList extends ChangesList {
516 /**
517 * Format a line using the old system (aka without any javascript).
518 *
519 * @param $rc RecentChange
520 * @return string
521 */
522 public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
523 global $wgRCShowChangedSize;
524 wfProfileIn( __METHOD__ );
525
526 # Should patrol-related stuff be shown?
527 $unpatrolled = $this->showAsUnpatrolled( $rc );
528
529 $dateheader = ''; // $s now contains only <li>...</li>, for hooks' convenience.
530 $this->insertDateHeader( $dateheader, $rc->mAttribs['rc_timestamp'] );
531
532 $s = '';
533 $classes = array();
534 // use mw-line-even/mw-line-odd class only if linenumber is given (feature from bug 14468)
535 if( $linenumber ) {
536 if( $linenumber & 1 ) {
537 $classes[] = 'mw-line-odd';
538 }
539 else {
540 $classes[] = 'mw-line-even';
541 }
542 }
543
544 // Moved pages (very very old, not supported anymore)
545 if( $rc->mAttribs['rc_type'] == RC_MOVE || $rc->mAttribs['rc_type'] == RC_MOVE_OVER_REDIRECT ) {
546 // Log entries
547 } elseif( $rc->mAttribs['rc_log_type'] ) {
548 $logtitle = SpecialPage::getTitleFor( 'Log', $rc->mAttribs['rc_log_type'] );
549 $this->insertLog( $s, $logtitle, $rc->mAttribs['rc_log_type'] );
550 // Log entries (old format) or log targets, and special pages
551 } elseif( $rc->mAttribs['rc_namespace'] == NS_SPECIAL ) {
552 list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $rc->mAttribs['rc_title'] );
553 if( $name == 'Log' ) {
554 $this->insertLog( $s, $rc->getTitle(), $subpage );
555 }
556 // Regular entries
557 } else {
558 $this->insertDiffHist( $s, $rc, $unpatrolled );
559 # M, N, b and ! (minor, new, bot and unpatrolled)
560 $s .= $this->recentChangesFlags(
561 array(
562 'newpage' => $rc->mAttribs['rc_new'],
563 'minor' => $rc->mAttribs['rc_minor'],
564 'unpatrolled' => $unpatrolled,
565 'bot' => $rc->mAttribs['rc_bot']
566 ),
567 ''
568 );
569 $this->insertArticleLink( $s, $rc, $unpatrolled, $watched );
570 }
571 # Edit/log timestamp
572 $this->insertTimestamp( $s, $rc );
573 # Bytes added or removed
574 if( $wgRCShowChangedSize ) {
575 $cd = $rc->getCharacterDifference();
576 if( $cd != '' ) {
577 $s .= "$cd . . ";
578 }
579 }
580
581 if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
582 $s .= $this->insertLogEntry( $rc );
583 } else {
584 # User tool links
585 $this->insertUserRelatedLinks( $s, $rc );
586 # LTR/RTL direction mark
587 $s .= $this->getLanguage()->getDirMark();
588 $s .= $this->insertComment( $rc );
589 }
590
591 # Tags
592 $this->insertTags( $s, $rc, $classes );
593 # Rollback
594 $this->insertRollback( $s, $rc );
595 # For subclasses
596 $this->insertExtra( $s, $rc, $classes );
597
598 # How many users watch this page
599 if( $rc->numberofWatchingusers > 0 ) {
600 $s .= ' ' . wfMsgExt( 'number_of_watching_users_RCview',
601 array( 'parsemag', 'escape' ), $this->getLanguage()->formatNum( $rc->numberofWatchingusers ) );
602 }
603
604 if( $this->watchlist ) {
605 $classes[] = Sanitizer::escapeClass( 'watchlist-'.$rc->mAttribs['rc_namespace'].'-'.$rc->mAttribs['rc_title'] );
606 }
607
608 wfRunHooks( 'OldChangesListRecentChangesLine', array(&$this, &$s, $rc) );
609
610 wfProfileOut( __METHOD__ );
611 return "$dateheader<li class=\"".implode( ' ', $classes )."\">".$s."</li>\n";
612 }
613 }
614
615
616 /**
617 * Generate a list of changes using an Enhanced system (uses javascript).
618 */
619 class EnhancedChangesList extends ChangesList {
620
621 protected $rc_cache;
622
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->getLanguage()->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 $rc->unpatrolled = $this->showAsUnpatrolled( $rc );
665
666 $showdifflinks = true;
667 # Make article link
668 $type = $rc->mAttribs['rc_type'];
669 $logType = $rc->mAttribs['rc_log_type'];
670 // Page moves, very old style, not supported anymore
671 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
672 // New unpatrolled pages
673 } elseif( $rc->unpatrolled && $type == RC_NEW ) {
674 $clink = Linker::linkKnown( $rc->getTitle(), null, array(),
675 array( 'rcid' => $rc->mAttribs['rc_id'] ) );
676 // Log entries
677 } elseif( $type == RC_LOG ) {
678 if( $logType ) {
679 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
680 $logpage = new LogPage( $logType );
681 $logname = $logpage->getName()->escaped();
682 $clink = $this->msg( 'parentheses' )->rawParams( Linker::linkKnown( $logtitle, $logname ) )->escaped();
683 } else {
684 $clink = Linker::link( $rc->getTitle() );
685 }
686 $watched = false;
687 // Log entries (old format) and special pages
688 } elseif( $rc->mAttribs['rc_namespace'] == NS_SPECIAL ) {
689 wfDebug( "Unexpected special page in recentchanges\n" );
690 $clink = '';
691 // Edits
692 } else {
693 $clink = Linker::linkKnown( $rc->getTitle() );
694 }
695
696 # Don't show unusable diff links
697 if ( !ChangesList::userCan( $rc, Revision::DELETED_TEXT, $this->getUser() ) ) {
698 $showdifflinks = false;
699 }
700
701 $time = $this->getLanguage()->time( $rc->mAttribs['rc_timestamp'], true, true );
702 $rc->watched = $watched;
703 $rc->link = $clink;
704 $rc->timestamp = $time;
705 $rc->numberofWatchingusers = $baseRC->numberofWatchingusers;
706
707 # Make "cur" and "diff" links. Do not use link(), it is too slow if
708 # called too many times (50% of CPU time on RecentChanges!).
709 $thisOldid = $rc->mAttribs['rc_this_oldid'];
710 $lastOldid = $rc->mAttribs['rc_last_oldid'];
711 if( $rc->unpatrolled ) {
712 $rcIdQuery = array( 'rcid' => $rc->mAttribs['rc_id'] );
713 } else {
714 $rcIdQuery = array();
715 }
716 $querycur = $curIdEq + array( 'diff' => '0', 'oldid' => $thisOldid );
717 $querydiff = $curIdEq + array( 'diff' => $thisOldid, 'oldid' =>
718 $lastOldid ) + $rcIdQuery;
719
720 if( !$showdifflinks ) {
721 $curLink = $this->message['cur'];
722 $diffLink = $this->message['diff'];
723 } elseif( in_array( $type, array( RC_NEW, RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT ) ) ) {
724 if ( $type != RC_NEW ) {
725 $curLink = $this->message['cur'];
726 } else {
727 $curUrl = htmlspecialchars( $rc->getTitle()->getLinkURL( $querycur ) );
728 $curLink = "<a href=\"$curUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['cur']}</a>";
729 }
730 $diffLink = $this->message['diff'];
731 } else {
732 $diffUrl = htmlspecialchars( $rc->getTitle()->getLinkURL( $querydiff ) );
733 $curUrl = htmlspecialchars( $rc->getTitle()->getLinkURL( $querycur ) );
734 $diffLink = "<a href=\"$diffUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['diff']}</a>";
735 $curLink = "<a href=\"$curUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['cur']}</a>";
736 }
737
738 # Make "last" link
739 if( !$showdifflinks || !$lastOldid ) {
740 $lastLink = $this->message['last'];
741 } elseif( in_array( $type, array( RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT ) ) ) {
742 $lastLink = $this->message['last'];
743 } else {
744 $lastLink = Linker::linkKnown( $rc->getTitle(), $this->message['last'],
745 array(), $curIdEq + array('diff' => $thisOldid, 'oldid' => $lastOldid) + $rcIdQuery );
746 }
747
748 # Make user links
749 if( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
750 $rc->userlink = ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
751 } else {
752 $rc->userlink = Linker::userLink( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
753 $rc->usertalklink = Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
754 }
755
756 $rc->lastlink = $lastLink;
757 $rc->curlink = $curLink;
758 $rc->difflink = $diffLink;
759
760 # Put accumulated information into the cache, for later display
761 # Page moves go on their own line
762 $title = $rc->getTitle();
763 $secureName = $title->getPrefixedDBkey();
764 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
765 # Use an @ character to prevent collision with page names
766 $this->rc_cache['@@' . ($this->rcMoveIndex++)] = array($rc);
767 } else {
768 # Logs are grouped by type
769 if( $type == RC_LOG ){
770 $secureName = SpecialPage::getTitleFor( 'Log', $logType )->getPrefixedDBkey();
771 }
772 if( !isset( $this->rc_cache[$secureName] ) ) {
773 $this->rc_cache[$secureName] = array();
774 }
775
776 array_push( $this->rc_cache[$secureName], $rc );
777 }
778
779 wfProfileOut( __METHOD__ );
780
781 return $ret;
782 }
783
784 /**
785 * Enhanced RC group
786 * @return string
787 */
788 protected function recentChangesBlockGroup( $block ) {
789 global $wgRCShowChangedSize;
790
791 wfProfileIn( __METHOD__ );
792
793 # Add the namespace and title of the block as part of the class
794 if ( $block[0]->mAttribs['rc_log_type'] ) {
795 # Log entry
796 $classes = 'mw-collapsible mw-collapsed mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-log-'
797 . $block[0]->mAttribs['rc_log_type'] . '-' . $block[0]->mAttribs['rc_title'] );
798 } else {
799 $classes = 'mw-collapsible mw-collapsed mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-ns'
800 . $block[0]->mAttribs['rc_namespace'] . '-' . $block[0]->mAttribs['rc_title'] );
801 }
802 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
803 Html::openElement( 'tr' );
804
805 # Collate list of users
806 $userlinks = array();
807 # Other properties
808 $unpatrolled = false;
809 $isnew = false;
810 $curId = $currentRevision = 0;
811 # Some catalyst variables...
812 $namehidden = true;
813 $allLogs = true;
814 foreach( $block as $rcObj ) {
815 $oldid = $rcObj->mAttribs['rc_last_oldid'];
816 if( $rcObj->mAttribs['rc_new'] ) {
817 $isnew = true;
818 }
819 // If all log actions to this page were hidden, then don't
820 // give the name of the affected page for this block!
821 if( !$this->isDeleted( $rcObj, LogPage::DELETED_ACTION ) ) {
822 $namehidden = false;
823 }
824 $u = $rcObj->userlink;
825 if( !isset( $userlinks[$u] ) ) {
826 $userlinks[$u] = 0;
827 }
828 if( $rcObj->unpatrolled ) {
829 $unpatrolled = true;
830 }
831 if( $rcObj->mAttribs['rc_type'] != RC_LOG ) {
832 $allLogs = false;
833 }
834 # Get the latest entry with a page_id and oldid
835 # since logs may not have these.
836 if( !$curId && $rcObj->mAttribs['rc_cur_id'] ) {
837 $curId = $rcObj->mAttribs['rc_cur_id'];
838 }
839 if( !$currentRevision && $rcObj->mAttribs['rc_this_oldid'] ) {
840 $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
841 }
842
843 $bot = $rcObj->mAttribs['rc_bot'];
844 $userlinks[$u]++;
845 }
846
847 # Sort the list and convert to text
848 krsort( $userlinks );
849 asort( $userlinks );
850 $users = array();
851 foreach( $userlinks as $userlink => $count) {
852 $text = $userlink;
853 $text .= $this->getLanguage()->getDirMark();
854 if( $count > 1 ) {
855 $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->formatNum( $count ) . '×' )->escaped();
856 }
857 array_push( $users, $text );
858 }
859
860 $users = ' <span class="changedby">[' .
861 implode( $this->message['semicolon-separator'], $users ) . ']</span>';
862
863 $tl = '<span class="mw-collapsible-toggle mw-enhancedchanges-arrow"></span>';
864 $r .= "<td>$tl</td>";
865
866 # Main line
867 $r .= '<td class="mw-enhanced-rc">' . $this->recentChangesFlags( array(
868 'newpage' => $isnew,
869 'minor' => false,
870 'unpatrolled' => $unpatrolled,
871 'bot' => $bot ,
872 ) );
873
874 # Timestamp
875 $r .= '&#160;'.$block[0]->timestamp.'&#160;</td><td>';
876
877 # Article link
878 if( $namehidden ) {
879 $r .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-event' ) . '</span>';
880 } elseif( $allLogs ) {
881 $r .= $this->maybeWatchedLink( $block[0]->link, $block[0]->watched );
882 } else {
883 $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled, $block[0]->watched );
884 }
885
886 $r .= $this->getLanguage()->getDirMark();
887
888 $queryParams['curid'] = $curId;
889 # Changes message
890 $n = count($block);
891 static $nchanges = array();
892 if ( !isset( $nchanges[$n] ) ) {
893 $nchanges[$n] = wfMsgExt( 'nchanges', array( 'parsemag', 'escape' ), $this->getLanguage()->formatNum( $n ) );
894 }
895 # Total change link
896 $r .= ' ';
897 $logtext = '';
898 if( !$allLogs ) {
899 if( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT, $this->getUser() ) ) {
900 $logtext .= $nchanges[$n];
901 } elseif( $isnew ) {
902 $logtext .= $nchanges[$n];
903 } else {
904 $params = $queryParams;
905 $params['diff'] = $currentRevision;
906 $params['oldid'] = $oldid;
907
908 $logtext .= Linker::link(
909 $block[0]->getTitle(),
910 $nchanges[$n],
911 array(),
912 $params,
913 array( 'known', 'noclasses' )
914 );
915 }
916 }
917
918 # History
919 if( $allLogs ) {
920 // don't show history link for logs
921 } elseif( $namehidden || !$block[0]->getTitle()->exists() ) {
922 $logtext .= $this->message['pipe-separator'] . $this->message['hist'];
923 } else {
924 $params = $queryParams;
925 $params['action'] = 'history';
926
927 $logtext .= $this->message['pipe-separator'] .
928 Linker::linkKnown(
929 $block[0]->getTitle(),
930 $this->message['hist'],
931 array(),
932 $params
933 );
934 }
935
936 if( $logtext !== '' ) {
937 $r .= $this->msg( 'parentheses' )->rawParams( $logtext )->escaped();
938 }
939
940 $r .= ' . . ';
941
942 # Character difference (does not apply if only log items)
943 if( $wgRCShowChangedSize && !$allLogs ) {
944 $last = 0;
945 $first = count($block) - 1;
946 # Some events (like logs) have an "empty" size, so we need to skip those...
947 while( $last < $first && $block[$last]->mAttribs['rc_new_len'] === null ) {
948 $last++;
949 }
950 while( $first > $last && $block[$first]->mAttribs['rc_old_len'] === null ) {
951 $first--;
952 }
953 # Get net change
954 $chardiff = $rcObj->getCharacterDifference( $block[$first]->mAttribs['rc_old_len'],
955 $block[$last]->mAttribs['rc_new_len'] );
956
957 if( $chardiff == '' ) {
958 $r .= ' ';
959 } else {
960 $r .= ' ' . $chardiff. ' . . ';
961 }
962 }
963
964 $r .= $users;
965 $r .= $this->numberofWatchingusers($block[0]->numberofWatchingusers);
966
967 # Sub-entries
968 foreach( $block as $rcObj ) {
969 # Classes to apply -- TODO implement
970 $classes = array();
971 $type = $rcObj->mAttribs['rc_type'];
972
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 .= ' ' . $this->msg( 'parentheses' )->rawParams( $rcObj->curlink . $this->message['pipe-separator'] . $rcObj->lastlink )->escaped();
1012 }
1013 $r .= ' . . ';
1014
1015 # Character diff
1016 if( $wgRCShowChangedSize && $rcObj->getCharacterDifference() ) {
1017 $r .= $rcObj->getCharacterDifference() . ' . . ' ;
1018 }
1019
1020 if ( $rcObj->mAttribs['rc_type'] == RC_LOG ) {
1021 $r .= $this->insertLogEntry( $rcObj );
1022 } else {
1023 # User links
1024 $r .= $rcObj->userlink;
1025 $r .= $rcObj->usertalklink;
1026 $r .= $this->insertComment( $rcObj );
1027 }
1028
1029 # Rollback
1030 $this->insertRollback( $r, $rcObj );
1031 # Tags
1032 $this->insertTags( $r, $rcObj, $classes );
1033
1034 $r .= "</td></tr>\n";
1035 }
1036 $r .= "</table>\n";
1037
1038 $this->rcCacheIndex++;
1039
1040 wfProfileOut( __METHOD__ );
1041
1042 return $r;
1043 }
1044
1045 /**
1046 * Generate HTML for an arrow or placeholder graphic
1047 * @param $dir String: one of '', 'd', 'l', 'r'
1048 * @param $alt String: text
1049 * @param $title String: text
1050 * @return String: HTML <img> tag
1051 */
1052 protected function arrow( $dir, $alt='', $title='' ) {
1053 global $wgStylePath;
1054 $encUrl = htmlspecialchars( $wgStylePath . '/common/images/Arr_' . $dir . '.png' );
1055 $encAlt = htmlspecialchars( $alt );
1056 $encTitle = htmlspecialchars( $title );
1057 return "<img src=\"$encUrl\" width=\"12\" height=\"12\" alt=\"$encAlt\" title=\"$encTitle\" />";
1058 }
1059
1060 /**
1061 * Generate HTML for a right- or left-facing arrow,
1062 * depending on language direction.
1063 * @return String: HTML <img> tag
1064 */
1065 protected function sideArrow() {
1066 global $wgLang;
1067 $dir = $wgLang->isRTL() ? 'l' : 'r';
1068 return $this->arrow( $dir, '+', wfMsg( 'rc-enhanced-expand' ) );
1069 }
1070
1071 /**
1072 * Generate HTML for a down-facing arrow
1073 * depending on language direction.
1074 * @return String: HTML <img> tag
1075 */
1076 protected function downArrow() {
1077 return $this->arrow( 'd', '-', wfMsg( 'rc-enhanced-hide' ) );
1078 }
1079
1080 /**
1081 * Generate HTML for a spacer image
1082 * @return String: HTML <img> tag
1083 */
1084 protected function spacerArrow() {
1085 return $this->arrow( '', codepointToUtf8( 0xa0 ) ); // non-breaking space
1086 }
1087
1088 /**
1089 * Enhanced RC ungrouped line.
1090 *
1091 * @param $rcObj RecentChange
1092 * @return String: a HTML formatted line (generated using $r)
1093 */
1094 protected function recentChangesBlockLine( $rcObj ) {
1095 global $wgRCShowChangedSize;
1096
1097 wfProfileIn( __METHOD__ );
1098 $query['curid'] = $rcObj->mAttribs['rc_cur_id'];
1099
1100 $type = $rcObj->mAttribs['rc_type'];
1101 $logType = $rcObj->mAttribs['rc_log_type'];
1102 if( $logType ) {
1103 # Log entry
1104 $classes = 'mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-log-'
1105 . $logType . '-' . $rcObj->mAttribs['rc_title'] );
1106 } else {
1107 $classes = 'mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-ns' .
1108 $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
1109 }
1110 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
1111 Html::openElement( 'tr' );
1112
1113 $r .= '<td class="mw-enhanced-rc"><span class="mw-enhancedchanges-arrow mw-enhancedchanges-arrow-space"></span>';
1114 # Flag and Timestamp
1115 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
1116 $r .= '&#160;&#160;&#160;&#160;'; // 4 flags -> 4 spaces
1117 } else {
1118 $r .= $this->recentChangesFlags( array(
1119 'newpage' => $type == RC_NEW,
1120 'minor' => $rcObj->mAttribs['rc_minor'],
1121 'unpatrolled' => $rcObj->unpatrolled,
1122 'bot' => $rcObj->mAttribs['rc_bot'],
1123 ) );
1124 }
1125 $r .= '&#160;'.$rcObj->timestamp.'&#160;</td><td>';
1126 # Article or log link
1127 if( $logType ) {
1128 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
1129 $logname = LogPage::logName( $logType );
1130 $r .= $this->msg( 'parentheses' )->rawParams( Linker::linkKnown( $logtitle, htmlspecialchars( $logname ) ) )->escaped();
1131 } else {
1132 $this->insertArticleLink( $r, $rcObj, $rcObj->unpatrolled, $rcObj->watched );
1133 }
1134 # Diff and hist links
1135 if ( $type != RC_LOG ) {
1136 $query['action'] = 'history';
1137 $r .= ' ' . $this->msg( 'parentheses' )->rawParams( $rcObj->difflink . $this->message['pipe-separator'] . Linker::linkKnown(
1138 $rcObj->getTitle(),
1139 $this->message['hist'],
1140 array(),
1141 $query
1142 ) )->escaped();
1143 }
1144 $r .= ' . . ';
1145 # Character diff
1146 if( $wgRCShowChangedSize && ($cd = $rcObj->getCharacterDifference()) ) {
1147 $r .= "$cd . . ";
1148 }
1149
1150 if ( $type == RC_LOG ) {
1151 $r .= $this->insertLogEntry( $rcObj );
1152 } else {
1153 $r .= ' '.$rcObj->userlink . $rcObj->usertalklink;
1154 $r .= $this->insertComment( $rcObj );
1155 $r .= $this->insertRollback( $r, $rcObj );
1156 }
1157
1158 # Tags
1159 $classes = explode( ' ', $classes );
1160 $this->insertTags( $r, $rcObj, $classes );
1161 # Show how many people are watching this if enabled
1162 $r .= $this->numberofWatchingusers($rcObj->numberofWatchingusers);
1163
1164 $r .= "</td></tr></table>\n";
1165
1166 wfProfileOut( __METHOD__ );
1167
1168 return $r;
1169 }
1170
1171 /**
1172 * If enhanced RC is in use, this function takes the previously cached
1173 * RC lines, arranges them, and outputs the HTML
1174 *
1175 * @return string
1176 */
1177 protected function recentChangesBlock() {
1178 if( count ( $this->rc_cache ) == 0 ) {
1179 return '';
1180 }
1181
1182 wfProfileIn( __METHOD__ );
1183
1184 $blockOut = '';
1185 foreach( $this->rc_cache as $block ) {
1186 if( count( $block ) < 2 ) {
1187 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
1188 } else {
1189 $blockOut .= $this->recentChangesBlockGroup( $block );
1190 }
1191 }
1192
1193 wfProfileOut( __METHOD__ );
1194
1195 return '<div>'.$blockOut.'</div>';
1196 }
1197
1198 /**
1199 * Returns text for the end of RC
1200 * If enhanced RC is in use, returns pretty much all the text
1201 * @return string
1202 */
1203 public function endRecentChangesList() {
1204 return $this->recentChangesBlock() . parent::endRecentChangesList();
1205 }
1206
1207 }