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