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