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