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