New hook RecentChangesListRecentChangesLine which let you customize an
[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 wfRunHooks( 'EnhancedChangesListRecentChangesLine', array(&$this, &$s, $rc) );
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 }