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