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