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