* (bug 30245) Use the correct way to construct a log page title
[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 IContextSource
49 */
50 public function __construct( $obj ) {
51 if ( $obj instanceof IContextSource ) {
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 IContextSource to use
78 * @return ChangesList|EnhancedChangesList|OldChangesList derivative
79 */
80 public static function newFromContext( IContextSource $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 $page = new LogPage( $logtype );
243 $logname = $page->getName()->escaped();
244 $s .= '(' . Linker::linkKnown( $title, $logname ) . ')';
245 }
246
247 /**
248 * @param $s
249 * @param $rc RecentChange
250 * @param $unpatrolled
251 * @return void
252 */
253 public function insertDiffHist( &$s, &$rc, $unpatrolled ) {
254 # Diff link
255 if( $rc->mAttribs['rc_type'] == RC_NEW || $rc->mAttribs['rc_type'] == RC_LOG ) {
256 $diffLink = $this->message['diff'];
257 } elseif( !self::userCan($rc,Revision::DELETED_TEXT) ) {
258 $diffLink = $this->message['diff'];
259 } else {
260 $query = array(
261 'curid' => $rc->mAttribs['rc_cur_id'],
262 'diff' => $rc->mAttribs['rc_this_oldid'],
263 'oldid' => $rc->mAttribs['rc_last_oldid']
264 );
265
266 if( $unpatrolled ) {
267 $query['rcid'] = $rc->mAttribs['rc_id'];
268 };
269
270 $diffLink = Linker::linkKnown(
271 $rc->getTitle(),
272 $this->message['diff'],
273 array( 'tabindex' => $rc->counter ),
274 $query
275 );
276 }
277 $s .= '(' . $diffLink . $this->message['pipe-separator'];
278 # History link
279 $s .= Linker::linkKnown(
280 $rc->getTitle(),
281 $this->message['hist'],
282 array(),
283 array(
284 'curid' => $rc->mAttribs['rc_cur_id'],
285 'action' => 'history'
286 )
287 );
288 $s .= ') . . ';
289 }
290
291 /**
292 * @param $s
293 * @param $rc RecentChange
294 * @param $unpatrolled
295 * @param $watched
296 * @return void
297 */
298 public function insertArticleLink( &$s, &$rc, $unpatrolled, $watched ) {
299 # If it's a new article, there is no diff link, but if it hasn't been
300 # patrolled yet, we need to give users a way to do so
301 $params = array();
302
303 if ( $unpatrolled && $rc->mAttribs['rc_type'] == RC_NEW ) {
304 $params['rcid'] = $rc->mAttribs['rc_id'];
305 }
306
307 $articlelink = Linker::linkKnown(
308 $rc->getTitle(),
309 null,
310 array(),
311 $params
312 );
313 if( $this->isDeleted($rc,Revision::DELETED_TEXT) ) {
314 $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
315 }
316 # Bolden pages watched by this user
317 if( $watched ) {
318 $articlelink = "<strong class=\"mw-watched\">{$articlelink}</strong>";
319 }
320 # RTL/LTR marker
321 $articlelink .= $this->getLang()->getDirMark();
322
323 wfRunHooks( 'ChangesListInsertArticleLink',
324 array(&$this, &$articlelink, &$s, &$rc, $unpatrolled, $watched) );
325
326 $s .= " $articlelink";
327 }
328
329 /**
330 * @param $s
331 * @param $rc RecentChange
332 * @return void
333 */
334 public function insertTimestamp( &$s, $rc ) {
335 $s .= $this->message['semicolon-separator'] .
336 $this->getLang()->time( $rc->mAttribs['rc_timestamp'], true, true ) . ' . . ';
337 }
338
339 /** Insert links to user page, user talk page and eventually a blocking link
340 *
341 * @param $rc RecentChange
342 */
343 public function insertUserRelatedLinks( &$s, &$rc ) {
344 if( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
345 $s .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
346 } else {
347 $s .= Linker::userLink( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
348 $s .= Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
349 }
350 }
351
352 /** insert a formatted action
353 *
354 * @param $rc RecentChange
355 */
356 public function insertLogEntry( $rc ) {
357 $formatter = LogFormatter::newFromRow( $rc->mAttribs );
358 $formatter->setShowUserToolLinks( true );
359 $mark = $this->getLang()->getDirMark();
360 return $formatter->getActionText() . " $mark" . $formatter->getComment();
361 }
362
363 /**
364 * Insert a formatted comment
365 * @param $rc RecentChange
366 */
367 public function insertComment( $rc ) {
368 if( $rc->mAttribs['rc_type'] != RC_MOVE && $rc->mAttribs['rc_type'] != RC_MOVE_OVER_REDIRECT ) {
369 if( $this->isDeleted( $rc, Revision::DELETED_COMMENT ) ) {
370 return ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
371 } else {
372 return Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
373 }
374 }
375 }
376
377 /**
378 * Check whether to enable recent changes patrol features
379 * @return Boolean
380 */
381 public static function usePatrol() {
382 global $wgUser;
383 return $wgUser->useRCPatrol();
384 }
385
386 /**
387 * Returns the string which indicates the number of watching users
388 */
389 protected function numberofWatchingusers( $count ) {
390 static $cache = array();
391 if( $count > 0 ) {
392 if( !isset( $cache[$count] ) ) {
393 $cache[$count] = wfMsgExt( 'number_of_watching_users_RCview',
394 array('parsemag', 'escape' ), $this->getLang()->formatNum( $count ) );
395 }
396 return $cache[$count];
397 } else {
398 return '';
399 }
400 }
401
402 /**
403 * Determine if said field of a revision is hidden
404 * @param $rc RCCacheEntry
405 * @param $field Integer: one of DELETED_* bitfield constants
406 * @return Boolean
407 */
408 public static function isDeleted( $rc, $field ) {
409 return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
410 }
411
412 /**
413 * Determine if the current user is allowed to view a particular
414 * field of this revision, if it's marked as deleted.
415 * @param $rc RCCacheEntry
416 * @param $field Integer
417 * @return Boolean
418 */
419 public static function userCan( $rc, $field ) {
420 if( $rc->mAttribs['rc_type'] == RC_LOG ) {
421 return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field );
422 } else {
423 return Revision::userCanBitfield( $rc->mAttribs['rc_deleted'], $field );
424 }
425 }
426
427 protected function maybeWatchedLink( $link, $watched = false ) {
428 if( $watched ) {
429 return '<strong class="mw-watched">' . $link . '</strong>';
430 } else {
431 return '<span class="mw-rc-unwatched">' . $link . '</span>';
432 }
433 }
434
435 /** Inserts a rollback link
436 *
437 * @param $s
438 * @param $rc RecentChange
439 */
440 public function insertRollback( &$s, &$rc ) {
441 if( !$rc->mAttribs['rc_new'] && $rc->mAttribs['rc_this_oldid'] && $rc->mAttribs['rc_cur_id'] ) {
442 $page = $rc->getTitle();
443 /** Check for rollback and edit permissions, disallow special pages, and only
444 * show a link on the top-most revision */
445 if ( $this->getUser()->isAllowed('rollback') && $rc->mAttribs['page_latest'] == $rc->mAttribs['rc_this_oldid'] )
446 {
447 $rev = new Revision( array(
448 'id' => $rc->mAttribs['rc_this_oldid'],
449 'user' => $rc->mAttribs['rc_user'],
450 'user_text' => $rc->mAttribs['rc_user_text'],
451 'deleted' => $rc->mAttribs['rc_deleted']
452 ) );
453 $rev->setTitle( $page );
454 $s .= ' '.Linker::generateRollback( $rev, $this->getContext() );
455 }
456 }
457 }
458
459 /**
460 * @param $s
461 * @param $rc RecentChange
462 * @param $classes
463 * @return
464 */
465 public function insertTags( &$s, &$rc, &$classes ) {
466 if ( empty($rc->mAttribs['ts_tags']) )
467 return;
468
469 list($tagSummary, $newClasses) = ChangeTags::formatSummaryRow( $rc->mAttribs['ts_tags'], 'changeslist' );
470 $classes = array_merge( $classes, $newClasses );
471 $s .= ' ' . $tagSummary;
472 }
473
474 public function insertExtra( &$s, &$rc, &$classes ) {
475 ## Empty, used for subclassers to add anything special.
476 }
477 }
478
479
480 /**
481 * Generate a list of changes using the good old system (no javascript)
482 */
483 class OldChangesList extends ChangesList {
484 /**
485 * Format a line using the old system (aka without any javascript).
486 *
487 * @param $rc RecentChange
488 */
489 public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
490 global $wgRCShowChangedSize;
491 wfProfileIn( __METHOD__ );
492 # Should patrol-related stuff be shown?
493 $unpatrolled = $this->getUser()->useRCPatrol() && !$rc->mAttribs['rc_patrolled'];
494
495 $dateheader = ''; // $s now contains only <li>...</li>, for hooks' convenience.
496 $this->insertDateHeader( $dateheader, $rc->mAttribs['rc_timestamp'] );
497
498 $s = '';
499 $classes = array();
500 // use mw-line-even/mw-line-odd class only if linenumber is given (feature from bug 14468)
501 if( $linenumber ) {
502 if( $linenumber & 1 ) {
503 $classes[] = 'mw-line-odd';
504 }
505 else {
506 $classes[] = 'mw-line-even';
507 }
508 }
509
510 // Moved pages (very very old, not supported anymore)
511 if( $rc->mAttribs['rc_type'] == RC_MOVE || $rc->mAttribs['rc_type'] == RC_MOVE_OVER_REDIRECT ) {
512 // Log entries
513 } elseif( $rc->mAttribs['rc_log_type'] ) {
514 $logtitle = SpecialPage::getTitleFor( 'Log', $rc->mAttribs['rc_log_type'] );
515 $this->insertLog( $s, $logtitle, $rc->mAttribs['rc_log_type'] );
516 // Log entries (old format) or log targets, and special pages
517 } elseif( $rc->mAttribs['rc_namespace'] == NS_SPECIAL ) {
518 list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $rc->mAttribs['rc_title'] );
519 if( $name == 'Log' ) {
520 $this->insertLog( $s, $rc->getTitle(), $subpage );
521 }
522 // Regular entries
523 } else {
524 $this->insertDiffHist( $s, $rc, $unpatrolled );
525 # M, N, b and ! (minor, new, bot and unpatrolled)
526 $s .= $this->recentChangesFlags(
527 array(
528 'newpage' => $rc->mAttribs['rc_new'],
529 'minor' => $rc->mAttribs['rc_minor'],
530 'unpatrolled' => $unpatrolled,
531 'bot' => $rc->mAttribs['rc_bot']
532 ),
533 ''
534 );
535 $this->insertArticleLink( $s, $rc, $unpatrolled, $watched );
536 }
537 # Edit/log timestamp
538 $this->insertTimestamp( $s, $rc );
539 # Bytes added or removed
540 if( $wgRCShowChangedSize ) {
541 $cd = $rc->getCharacterDifference();
542 if( $cd != '' ) {
543 $s .= "$cd . . ";
544 }
545 }
546
547 if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
548 $s .= $this->insertLogEntry( $rc );
549 } else {
550 # User tool links
551 $this->insertUserRelatedLinks( $s, $rc );
552 # LTR/RTL direction mark
553 $s .= $this->getLang()->getDirMark();
554 $s .= $this->insertComment( $rc );
555 }
556
557 # Tags
558 $this->insertTags( $s, $rc, $classes );
559 # Rollback
560 $this->insertRollback( $s, $rc );
561 # For subclasses
562 $this->insertExtra( $s, $rc, $classes );
563
564 # How many users watch this page
565 if( $rc->numberofWatchingusers > 0 ) {
566 $s .= ' ' . wfMsgExt( 'number_of_watching_users_RCview',
567 array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $rc->numberofWatchingusers ) );
568 }
569
570 if( $this->watchlist ) {
571 $classes[] = Sanitizer::escapeClass( 'watchlist-'.$rc->mAttribs['rc_namespace'].'-'.$rc->mAttribs['rc_title'] );
572 }
573
574 wfRunHooks( 'OldChangesListRecentChangesLine', array(&$this, &$s, $rc) );
575
576 wfProfileOut( __METHOD__ );
577 return "$dateheader<li class=\"".implode( ' ', $classes )."\">".$s."</li>\n";
578 }
579 }
580
581
582 /**
583 * Generate a list of changes using an Enhanced system (uses javascript).
584 */
585 class EnhancedChangesList extends ChangesList {
586 /**
587 * Add the JavaScript file for enhanced changeslist
588 * @return String
589 */
590 public function beginRecentChangesList() {
591 $this->rc_cache = array();
592 $this->rcMoveIndex = 0;
593 $this->rcCacheIndex = 0;
594 $this->lastdate = '';
595 $this->rclistOpen = false;
596 $this->getOutput()->addModuleStyles( 'mediawiki.special.changeslist' );
597 return '';
598 }
599 /**
600 * Format a line for enhanced recentchange (aka with javascript and block of lines).
601 *
602 * @param $baseRC RecentChange
603 * @param $watched bool
604 *
605 * @return string
606 */
607 public function recentChangesLine( &$baseRC, $watched = false ) {
608 wfProfileIn( __METHOD__ );
609
610 # Create a specialised object
611 $rc = RCCacheEntry::newFromParent( $baseRC );
612
613 $curIdEq = array( 'curid' => $rc->mAttribs['rc_cur_id'] );
614
615 # If it's a new day, add the headline and flush the cache
616 $date = $this->getLang()->date( $rc->mAttribs['rc_timestamp'], true );
617 $ret = '';
618 if( $date != $this->lastdate ) {
619 # Process current cache
620 $ret = $this->recentChangesBlock();
621 $this->rc_cache = array();
622 $ret .= Xml::element( 'h4', null, $date ) . "\n";
623 $this->lastdate = $date;
624 }
625
626 # Should patrol-related stuff be shown?
627 if( $this->getUser()->useRCPatrol() ) {
628 $rc->unpatrolled = !$rc->mAttribs['rc_patrolled'];
629 } else {
630 $rc->unpatrolled = false;
631 }
632
633 $showdifflinks = true;
634 # Make article link
635 $type = $rc->mAttribs['rc_type'];
636 $logType = $rc->mAttribs['rc_log_type'];
637 // Page moves, very old style, not supported anymore
638 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
639 // New unpatrolled pages
640 } elseif( $rc->unpatrolled && $type == RC_NEW ) {
641 $clink = Linker::linkKnown( $rc->getTitle(), null, array(),
642 array( 'rcid' => $rc->mAttribs['rc_id'] ) );
643 // Log entries
644 } elseif( $type == RC_LOG ) {
645 if( $logType ) {
646 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
647 $clink = '(' . Linker::linkKnown( $logtitle,
648 LogPage::logName( $logType ) ) . ')';
649 } else {
650 $clink = Linker::link( $rc->getTitle() );
651 }
652 $watched = false;
653 // Log entries (old format) and special pages
654 } elseif( $rc->mAttribs['rc_namespace'] == NS_SPECIAL ) {
655 list( $specialName, $logtype ) = SpecialPageFactory::resolveAlias( $rc->mAttribs['rc_title'] );
656 if ( $specialName == 'Log' ) {
657 # Log updates, etc
658 $logname = LogPage::logName( $logtype );
659 $clink = '(' . Linker::linkKnown( $rc->getTitle(), $logname ) . ')';
660 } else {
661 wfDebug( "Unexpected special page in recentchanges\n" );
662 $clink = '';
663 }
664 // Edits
665 } else {
666 $clink = Linker::linkKnown( $rc->getTitle() );
667 }
668
669 # Don't show unusable diff links
670 if ( !ChangesList::userCan($rc,Revision::DELETED_TEXT) ) {
671 $showdifflinks = false;
672 }
673
674 $time = $this->getLang()->time( $rc->mAttribs['rc_timestamp'], true, true );
675 $rc->watched = $watched;
676 $rc->link = $clink;
677 $rc->timestamp = $time;
678 $rc->numberofWatchingusers = $baseRC->numberofWatchingusers;
679
680 # Make "cur" and "diff" links. Do not use link(), it is too slow if
681 # called too many times (50% of CPU time on RecentChanges!).
682 $thisOldid = $rc->mAttribs['rc_this_oldid'];
683 $lastOldid = $rc->mAttribs['rc_last_oldid'];
684 if( $rc->unpatrolled ) {
685 $rcIdQuery = array( 'rcid' => $rc->mAttribs['rc_id'] );
686 } else {
687 $rcIdQuery = array();
688 }
689 $querycur = $curIdEq + array( 'diff' => '0', 'oldid' => $thisOldid );
690 $querydiff = $curIdEq + array( 'diff' => $thisOldid, 'oldid' =>
691 $lastOldid ) + $rcIdQuery;
692
693 if( !$showdifflinks ) {
694 $curLink = $this->message['cur'];
695 $diffLink = $this->message['diff'];
696 } elseif( in_array( $type, array( RC_NEW, RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT ) ) ) {
697 if ( $type != RC_NEW ) {
698 $curLink = $this->message['cur'];
699 } else {
700 $curUrl = htmlspecialchars( $rc->getTitle()->getLinkUrl( $querycur ) );
701 $curLink = "<a href=\"$curUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['cur']}</a>";
702 }
703 $diffLink = $this->message['diff'];
704 } else {
705 $diffUrl = htmlspecialchars( $rc->getTitle()->getLinkUrl( $querydiff ) );
706 $curUrl = htmlspecialchars( $rc->getTitle()->getLinkUrl( $querycur ) );
707 $diffLink = "<a href=\"$diffUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['diff']}</a>";
708 $curLink = "<a href=\"$curUrl\" tabindex=\"{$baseRC->counter}\">{$this->message['cur']}</a>";
709 }
710
711 # Make "last" link
712 if( !$showdifflinks || !$lastOldid ) {
713 $lastLink = $this->message['last'];
714 } elseif( in_array( $type, array( RC_LOG, RC_MOVE, RC_MOVE_OVER_REDIRECT ) ) ) {
715 $lastLink = $this->message['last'];
716 } else {
717 $lastLink = Linker::linkKnown( $rc->getTitle(), $this->message['last'],
718 array(), $curIdEq + array('diff' => $thisOldid, 'oldid' => $lastOldid) + $rcIdQuery );
719 }
720
721 # Make user links
722 if( $this->isDeleted( $rc, Revision::DELETED_USER ) ) {
723 $rc->userlink = ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
724 } else {
725 $rc->userlink = Linker::userLink( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
726 $rc->usertalklink = Linker::userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
727 }
728
729 $rc->lastlink = $lastLink;
730 $rc->curlink = $curLink;
731 $rc->difflink = $diffLink;
732
733 # Put accumulated information into the cache, for later display
734 # Page moves go on their own line
735 $title = $rc->getTitle();
736 $secureName = $title->getPrefixedDBkey();
737 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
738 # Use an @ character to prevent collision with page names
739 $this->rc_cache['@@' . ($this->rcMoveIndex++)] = array($rc);
740 } else {
741 # Logs are grouped by type
742 if( $type == RC_LOG ){
743 $secureName = SpecialPage::getTitleFor( 'Log', $logType )->getPrefixedDBkey();
744 }
745 if( !isset( $this->rc_cache[$secureName] ) ) {
746 $this->rc_cache[$secureName] = array();
747 }
748
749 array_push( $this->rc_cache[$secureName], $rc );
750 }
751
752 wfProfileOut( __METHOD__ );
753
754 return $ret;
755 }
756
757 /**
758 * Enhanced RC group
759 */
760 protected function recentChangesBlockGroup( $block ) {
761 global $wgRCShowChangedSize;
762
763 wfProfileIn( __METHOD__ );
764
765 # Add the namespace and title of the block as part of the class
766 if ( $block[0]->mAttribs['rc_log_type'] ) {
767 # Log entry
768 $classes = 'mw-collapsible mw-collapsed mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-log-' . $block[0]->mAttribs['rc_log_type'] . '-' . $block[0]->mAttribs['rc_title'] );
769 } else {
770 $classes = 'mw-collapsible mw-collapsed mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-ns' . $block[0]->mAttribs['rc_namespace'] . '-' . $block[0]->mAttribs['rc_title'] );
771 }
772 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
773 Html::openElement( 'tr' );
774
775 # Collate list of users
776 $userlinks = array();
777 # Other properties
778 $unpatrolled = false;
779 $isnew = false;
780 $curId = $currentRevision = 0;
781 # Some catalyst variables...
782 $namehidden = true;
783 $allLogs = true;
784 foreach( $block as $rcObj ) {
785 $oldid = $rcObj->mAttribs['rc_last_oldid'];
786 if( $rcObj->mAttribs['rc_new'] ) {
787 $isnew = true;
788 }
789 // If all log actions to this page were hidden, then don't
790 // give the name of the affected page for this block!
791 if( !$this->isDeleted( $rcObj, LogPage::DELETED_ACTION ) ) {
792 $namehidden = false;
793 }
794 $u = $rcObj->userlink;
795 if( !isset( $userlinks[$u] ) ) {
796 $userlinks[$u] = 0;
797 }
798 if( $rcObj->unpatrolled ) {
799 $unpatrolled = true;
800 }
801 if( $rcObj->mAttribs['rc_type'] != RC_LOG ) {
802 $allLogs = false;
803 }
804 # Get the latest entry with a page_id and oldid
805 # since logs may not have these.
806 if( !$curId && $rcObj->mAttribs['rc_cur_id'] ) {
807 $curId = $rcObj->mAttribs['rc_cur_id'];
808 }
809 if( !$currentRevision && $rcObj->mAttribs['rc_this_oldid'] ) {
810 $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
811 }
812
813 $bot = $rcObj->mAttribs['rc_bot'];
814 $userlinks[$u]++;
815 }
816
817 # Sort the list and convert to text
818 krsort( $userlinks );
819 asort( $userlinks );
820 $users = array();
821 foreach( $userlinks as $userlink => $count) {
822 $text = $userlink;
823 $text .= $this->getLang()->getDirMark();
824 if( $count > 1 ) {
825 $text .= ' (' . $this->getLang()->formatNum( $count ) . '×)';
826 }
827 array_push( $users, $text );
828 }
829
830 $users = ' <span class="changedby">[' .
831 implode( $this->message['semicolon-separator'], $users ) . ']</span>';
832
833 # Title for <a> tags
834 $expandTitle = htmlspecialchars( wfMsg( 'rc-enhanced-expand' ) );
835 $closeTitle = htmlspecialchars( wfMsg( 'rc-enhanced-hide' ) );
836
837 $tl = "<span class='mw-collapsible-toggle'>"
838 . "<span class='mw-rc-openarrow'>"
839 . "<a href='#' title='$expandTitle'>{$this->sideArrow()}</a>"
840 . "</span><span class='mw-rc-closearrow'>"
841 . "<a href='#' title='$closeTitle'>{$this->downArrow()}</a>"
842 . "</span></span>";
843 $r .= "<td>$tl</td>";
844
845 # Main line
846 $r .= '<td class="mw-enhanced-rc">' . $this->recentChangesFlags( array(
847 'newpage' => $isnew,
848 'minor' => false,
849 'unpatrolled' => $unpatrolled,
850 'bot' => $bot ,
851 ) );
852
853 # Timestamp
854 $r .= '&#160;'.$block[0]->timestamp.'&#160;</td><td>';
855
856 # Article link
857 if( $namehidden ) {
858 $r .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-event' ) . '</span>';
859 } elseif( $allLogs ) {
860 $r .= $this->maybeWatchedLink( $block[0]->link, $block[0]->watched );
861 } else {
862 $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled, $block[0]->watched );
863 }
864
865 $r .= $this->getLang()->getDirMark();
866
867 $queryParams['curid'] = $curId;
868 # Changes message
869 $n = count($block);
870 static $nchanges = array();
871 if ( !isset( $nchanges[$n] ) ) {
872 $nchanges[$n] = wfMsgExt( 'nchanges', array( 'parsemag', 'escape' ), $this->getLang()->formatNum( $n ) );
873 }
874 # Total change link
875 $r .= ' ';
876 if( !$allLogs ) {
877 $r .= '(';
878 if( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT ) ) {
879 $r .= $nchanges[$n];
880 } elseif( $isnew ) {
881 $r .= $nchanges[$n];
882 } else {
883 $params = $queryParams;
884 $params['diff'] = $currentRevision;
885 $params['oldid'] = $oldid;
886
887 $r .= Linker::link(
888 $block[0]->getTitle(),
889 $nchanges[$n],
890 array(),
891 $params,
892 array( 'known', 'noclasses' )
893 );
894 }
895 }
896
897 # History
898 if( $allLogs ) {
899 // don't show history link for logs
900 } elseif( $namehidden || !$block[0]->getTitle()->exists() ) {
901 $r .= $this->message['pipe-separator'] . $this->message['hist'] . ')';
902 } else {
903 $params = $queryParams;
904 $params['action'] = 'history';
905
906 $r .= $this->message['pipe-separator'] .
907 Linker::linkKnown(
908 $block[0]->getTitle(),
909 $this->message['hist'],
910 array(),
911 $params
912 ) . ')';
913 }
914 $r .= ' . . ';
915
916 # Character difference (does not apply if only log items)
917 if( $wgRCShowChangedSize && !$allLogs ) {
918 $last = 0;
919 $first = count($block) - 1;
920 # Some events (like logs) have an "empty" size, so we need to skip those...
921 while( $last < $first && $block[$last]->mAttribs['rc_new_len'] === null ) {
922 $last++;
923 }
924 while( $first > $last && $block[$first]->mAttribs['rc_old_len'] === null ) {
925 $first--;
926 }
927 # Get net change
928 $chardiff = $rcObj->getCharacterDifference( $block[$first]->mAttribs['rc_old_len'],
929 $block[$last]->mAttribs['rc_new_len'] );
930
931 if( $chardiff == '' ) {
932 $r .= ' ';
933 } else {
934 $r .= ' ' . $chardiff. ' . . ';
935 }
936 }
937
938 $r .= $users;
939 $r .= $this->numberofWatchingusers($block[0]->numberofWatchingusers);
940
941 # Sub-entries
942 foreach( $block as $rcObj ) {
943 # Classes to apply -- TODO implement
944 $classes = array();
945 $type = $rcObj->mAttribs['rc_type'];
946
947 #$r .= '<tr><td valign="top">'.$this->spacerArrow();
948 $r .= '<tr><td></td><td class="mw-enhanced-rc">';
949 $r .= $this->recentChangesFlags( array(
950 'newpage' => $rcObj->mAttribs['rc_new'],
951 'minor' => $rcObj->mAttribs['rc_minor'],
952 'unpatrolled' => $rcObj->unpatrolled,
953 'bot' => $rcObj->mAttribs['rc_bot'],
954 ) );
955 $r .= '&#160;</td><td class="mw-enhanced-rc-nested"><span class="mw-enhanced-rc-time">';
956
957 $params = $queryParams;
958
959 if( $rcObj->mAttribs['rc_this_oldid'] != 0 ) {
960 $params['oldid'] = $rcObj->mAttribs['rc_this_oldid'];
961 }
962
963 # Log timestamp
964 if( $type == RC_LOG ) {
965 $link = $rcObj->timestamp;
966 # Revision link
967 } elseif( !ChangesList::userCan($rcObj,Revision::DELETED_TEXT) ) {
968 $link = '<span class="history-deleted">'.$rcObj->timestamp.'</span> ';
969 } else {
970 if ( $rcObj->unpatrolled && $type == RC_NEW) {
971 $params['rcid'] = $rcObj->mAttribs['rc_id'];
972 }
973
974 $link = Linker::linkKnown(
975 $rcObj->getTitle(),
976 $rcObj->timestamp,
977 array(),
978 $params
979 );
980 if( $this->isDeleted($rcObj,Revision::DELETED_TEXT) )
981 $link = '<span class="history-deleted">'.$link.'</span> ';
982 }
983 $r .= $link . '</span>';
984
985 if ( !$type == RC_LOG || $type == RC_NEW ) {
986 $r .= ' (';
987 $r .= $rcObj->curlink;
988 $r .= $this->message['pipe-separator'];
989 $r .= $rcObj->lastlink;
990 $r .= ')';
991 }
992 $r .= ' . . ';
993
994 # Character diff
995 if( $wgRCShowChangedSize && $rcObj->getCharacterDifference() ) {
996 $r .= $rcObj->getCharacterDifference() . ' . . ' ;
997 }
998
999 if ( $rcObj->mAttribs['rc_type'] == RC_LOG ) {
1000 $r .= $this->insertLogEntry( $rcObj );
1001 } else {
1002 # User links
1003 $r .= $rcObj->userlink;
1004 $r .= $rcObj->usertalklink;
1005 $r .= $this->insertComment( $rcObj );
1006 }
1007
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>\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 * Enhanced RC ungrouped line.
1069 *
1070 * @param $rcObj RecentChange
1071 * @return String: a HTML formated line (generated using $r)
1072 */
1073 protected function recentChangesBlockLine( $rcObj ) {
1074 global $wgRCShowChangedSize;
1075
1076 wfProfileIn( __METHOD__ );
1077 $query['curid'] = $rcObj->mAttribs['rc_cur_id'];
1078
1079 $type = $rcObj->mAttribs['rc_type'];
1080 $logType = $rcObj->mAttribs['rc_log_type'];
1081 if( $logType ) {
1082 # Log entry
1083 $classes = 'mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-log-' . $logType . '-' . $rcObj->mAttribs['rc_title'] );
1084 } else {
1085 $classes = 'mw-enhanced-rc ' . Sanitizer::escapeClass( 'mw-changeslist-ns' . $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
1086 }
1087 $r = Html::openElement( 'table', array( 'class' => $classes ) ) .
1088 Html::openElement( 'tr' );
1089
1090 $r .= '<td class="mw-enhanced-rc">' . $this->spacerArrow();
1091 # Flag and Timestamp
1092 if( $type == RC_MOVE || $type == RC_MOVE_OVER_REDIRECT ) {
1093 $r .= '&#160;&#160;&#160;&#160;'; // 4 flags -> 4 spaces
1094 } else {
1095 $r .= $this->recentChangesFlags( array(
1096 'newpage' => $type == RC_NEW,
1097 'mino' => $rcObj->mAttribs['rc_minor'],
1098 'unpatrolled' => $rcObj->unpatrolled,
1099 'bot' => $rcObj->mAttribs['rc_bot'],
1100 ) );
1101 }
1102 $r .= '&#160;'.$rcObj->timestamp.'&#160;</td><td>';
1103 # Article or log link
1104 if( $logType ) {
1105 $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
1106 $logname = LogPage::logName( $logType );
1107 $r .= '(' . Linker::linkKnown( $logtitle, htmlspecialchars( $logname ) ) . ')';
1108 } else {
1109 $this->insertArticleLink( $r, $rcObj, $rcObj->unpatrolled, $rcObj->watched );
1110 }
1111 # Diff and hist links
1112 if ( $type != RC_LOG ) {
1113 $r .= ' ('. $rcObj->difflink . $this->message['pipe-separator'];
1114 $query['action'] = 'history';
1115 $r .= Linker::linkKnown(
1116 $rcObj->getTitle(),
1117 $this->message['hist'],
1118 array(),
1119 $query
1120 ) . ')';
1121 }
1122 $r .= ' . . ';
1123 # Character diff
1124 if( $wgRCShowChangedSize && ($cd = $rcObj->getCharacterDifference()) ) {
1125 $r .= "$cd . . ";
1126 }
1127
1128 if ( $type == RC_LOG ) {
1129 $r .= $this->insertLogEntry( $rcObj );
1130 } else {
1131 $r .= ' '.$rcObj->userlink . $rcObj->usertalklink;
1132 $r .= $this->insertComment( $rcObj );
1133 $r .= $this->insertRollback( $r, $rcObj );
1134 }
1135
1136 # Tags
1137 $classes = explode( ' ', $classes );
1138 $this->insertTags( $r, $rcObj, $classes );
1139 # Show how many people are watching this if enabled
1140 $r .= $this->numberofWatchingusers($rcObj->numberofWatchingusers);
1141
1142 $r .= "</td></tr></table>\n";
1143
1144 wfProfileOut( __METHOD__ );
1145
1146 return $r;
1147 }
1148
1149 /**
1150 * If enhanced RC is in use, this function takes the previously cached
1151 * RC lines, arranges them, and outputs the HTML
1152 *
1153 * @return string
1154 */
1155 protected function recentChangesBlock() {
1156 if( count ( $this->rc_cache ) == 0 ) {
1157 return '';
1158 }
1159
1160 wfProfileIn( __METHOD__ );
1161
1162 $blockOut = '';
1163 foreach( $this->rc_cache as $block ) {
1164 if( count( $block ) < 2 ) {
1165 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
1166 } else {
1167 $blockOut .= $this->recentChangesBlockGroup( $block );
1168 }
1169 }
1170
1171 wfProfileOut( __METHOD__ );
1172
1173 return '<div>'.$blockOut.'</div>';
1174 }
1175
1176 /**
1177 * Returns text for the end of RC
1178 * If enhanced RC is in use, returns pretty much all the text
1179 */
1180 public function endRecentChangesList() {
1181 return $this->recentChangesBlock() . parent::endRecentChangesList();
1182 }
1183
1184 }