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