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