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