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