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