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