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