documentation, a couple notes about code and some whitespaces adjustements to make...
[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 );
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 $r = '<table class="mw-enhanced-rc"><tr>';
740
741 # Collate list of users
742 $userlinks = array();
743 # Other properties
744 $unpatrolled = false;
745 $isnew = false;
746 $curId = $currentRevision = 0;
747 # Some catalyst variables...
748 $namehidden = true;
749 $allLogs = true;
750 foreach( $block as $rcObj ) {
751 $oldid = $rcObj->mAttribs['rc_last_oldid'];
752 if( $rcObj->mAttribs['rc_new'] ) {
753 $isnew = true;
754 }
755 // If all log actions to this page were hidden, then don't
756 // give the name of the affected page for this block!
757 if( !$this->isDeleted( $rcObj, LogPage::DELETED_ACTION ) ) {
758 $namehidden = false;
759 }
760 $u = $rcObj->userlink;
761 if( !isset( $userlinks[$u] ) ) {
762 $userlinks[$u] = 0;
763 }
764 if( $rcObj->unpatrolled ) {
765 $unpatrolled = true;
766 }
767 if( $rcObj->mAttribs['rc_type'] != RC_LOG ) {
768 $allLogs = false;
769 }
770 # Get the latest entry with a page_id and oldid
771 # since logs may not have these.
772 if( !$curId && $rcObj->mAttribs['rc_cur_id'] ) {
773 $curId = $rcObj->mAttribs['rc_cur_id'];
774 }
775 if( !$currentRevision && $rcObj->mAttribs['rc_this_oldid'] ) {
776 $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
777 }
778
779 $bot = $rcObj->mAttribs['rc_bot'];
780 $userlinks[$u]++;
781 }
782
783 # Sort the list and convert to text
784 krsort( $userlinks );
785 asort( $userlinks );
786 $users = array();
787 foreach( $userlinks as $userlink => $count) {
788 $text = $userlink;
789 $text .= $wgContLang->getDirMark();
790 if( $count > 1 ) {
791 $text .= ' (' . $wgLang->formatNum( $count ) . '×)';
792 }
793 array_push( $users, $text );
794 }
795
796 $users = ' <span class="changedby">[' .
797 implode( $this->message['semicolon-separator'], $users ) . ']</span>';
798
799 # ID for JS visibility toggle
800 $jsid = $this->rcCacheIndex;
801 # onclick handler to toggle hidden/expanded
802 $toggleLink = "onclick='toggleVisibility($jsid); return false'";
803 # Title for <a> tags
804 $expandTitle = htmlspecialchars( wfMsg( 'rc-enhanced-expand' ) );
805 $closeTitle = htmlspecialchars( wfMsg( 'rc-enhanced-hide' ) );
806
807 $tl = "<span id='mw-rc-openarrow-$jsid' class='mw-changeslist-expanded' style='visibility:hidden'><a href='#' $toggleLink title='$expandTitle'>" . $this->sideArrow() . "</a></span>";
808 $tl .= "<span id='mw-rc-closearrow-$jsid' class='mw-changeslist-hidden' style='display:none'><a href='#' $toggleLink title='$closeTitle'>" . $this->downArrow() . "</a></span>";
809 $r .= '<td class="mw-enhanced-rc">'.$tl.'&#160;';
810
811 # Main line
812 $r .= $this->recentChangesFlags( $isnew, false, $unpatrolled, '&#160;', $bot );
813
814 # Timestamp
815 $r .= '&#160;'.$block[0]->timestamp.'&#160;</td><td style="padding:0px;">';
816
817 # Article link
818 if( $namehidden ) {
819 $r .= ' <span class="history-deleted">' . wfMsgHtml( 'rev-deleted-event' ) . '</span>';
820 } else if( $allLogs ) {
821 $r .= $this->maybeWatchedLink( $block[0]->link, $block[0]->watched );
822 } else {
823 $this->insertArticleLink( $r, $block[0], $block[0]->unpatrolled, $block[0]->watched );
824 }
825
826 $r .= $wgContLang->getDirMark();
827
828 $queryParams['curid'] = $curId;
829 # Changes message
830 $n = count($block);
831 static $nchanges = array();
832 if ( !isset( $nchanges[$n] ) ) {
833 $nchanges[$n] = wfMsgExt( 'nchanges', array( 'parsemag', 'escape' ), $wgLang->formatNum( $n ) );
834 }
835 # Total change link
836 $r .= ' ';
837 if( !$allLogs ) {
838 $r .= '(';
839 if( !ChangesList::userCan( $rcObj, Revision::DELETED_TEXT ) ) {
840 $r .= $nchanges[$n];
841 } else if( $isnew ) {
842 $r .= $nchanges[$n];
843 } else {
844 $params = $queryParams;
845 $params['diff'] = $currentRevision;
846 $params['oldid'] = $oldid;
847
848 $r .= $this->skin->link(
849 $block[0]->getTitle(),
850 $nchanges[$n],
851 array(),
852 $params,
853 array( 'known', 'noclasses' )
854 );
855 }
856 }
857
858 # History
859 if( $allLogs ) {
860 // don't show history link for logs
861 } else if( $namehidden || !$block[0]->getTitle()->exists() ) {
862 $r .= $this->message['pipe-separator'] . $this->message['hist'] . ')';
863 } else {
864 $params = $queryParams;
865 $params['action'] = 'history';
866
867 $r .= $this->message['pipe-separator'] .
868 $this->skin->link(
869 $block[0]->getTitle(),
870 $this->message['hist'],
871 array(),
872 $params,
873 array( 'known', 'noclasses' )
874 ) . ')';
875 }
876 $r .= ' . . ';
877
878 # Character difference (does not apply if only log items)
879 if( $wgRCShowChangedSize && !$allLogs ) {
880 $last = 0;
881 $first = count($block) - 1;
882 # Some events (like logs) have an "empty" size, so we need to skip those...
883 while( $last < $first && $block[$last]->mAttribs['rc_new_len'] === null ) {
884 $last++;
885 }
886 while( $first > $last && $block[$first]->mAttribs['rc_old_len'] === null ) {
887 $first--;
888 }
889 # Get net change
890 $chardiff = $rcObj->getCharacterDifference( $block[$first]->mAttribs['rc_old_len'],
891 $block[$last]->mAttribs['rc_new_len'] );
892
893 if( $chardiff == '' ) {
894 $r .= ' ';
895 } else {
896 $r .= ' ' . $chardiff. ' . . ';
897 }
898 }
899
900 $r .= $users;
901 $r .= $this->numberofWatchingusers($block[0]->numberofWatchingusers);
902
903 $r .= "</td></tr></table>\n";
904
905 # Sub-entries
906 $r .= '<div id="mw-rc-subentries-'.$jsid.'" class="mw-changeslist-hidden">';
907 $r .= '<table class="mw-enhanced-rc">';
908 foreach( $block as $rcObj ) {
909 # Extract fields from DB into the function scope (rc_xxxx variables)
910 // FIXME: Would be good to replace this extract() call with something
911 // that explicitly initializes variables.
912 # Classes to apply -- TODO implement
913 $classes = array();
914 extract( $rcObj->mAttribs );
915
916 #$r .= '<tr><td valign="top">'.$this->spacerArrow();
917 $r .= '<tr><td style="vertical-align:top;font-family:monospace; padding:0px;">';
918 $r .= $this->spacerIndent() . $this->spacerIndent();
919 $r .= $this->recentChangesFlags( $rc_new, $rc_minor, $rcObj->unpatrolled, '&#160;', $rc_bot );
920 $r .= '&#160;</td><td style="vertical-align:top; padding:0px;"><span style="font-family:monospace">';
921
922 $params = $queryParams;
923
924 if( $rc_this_oldid != 0 ) {
925 $params['oldid'] = $rc_this_oldid;
926 }
927
928 # Log timestamp
929 if( $rc_type == RC_LOG ) {
930 $link = $rcObj->timestamp;
931 # Revision link
932 } else if( !ChangesList::userCan($rcObj,Revision::DELETED_TEXT) ) {
933 $link = '<span class="history-deleted">'.$rcObj->timestamp.'</span> ';
934 } else {
935 if ( $rcObj->unpatrolled && $rc_type == RC_NEW) {
936 $params['rcid'] = $rcObj->mAttribs['rc_id'];
937 }
938
939 $link = $this->skin->link(
940 $rcObj->getTitle(),
941 $rcObj->timestamp,
942 array(),
943 $params,
944 array( 'known', 'noclasses' )
945 );
946 if( $this->isDeleted($rcObj,Revision::DELETED_TEXT) )
947 $link = '<span class="history-deleted">'.$link.'</span> ';
948 }
949 $r .= $link . '</span>';
950
951 if ( !$rc_type == RC_LOG || $rc_type == RC_NEW ) {
952 $r .= ' (';
953 $r .= $rcObj->curlink;
954 $r .= $this->message['pipe-separator'];
955 $r .= $rcObj->lastlink;
956 $r .= ')';
957 }
958 $r .= ' . . ';
959
960 # Character diff
961 if( $wgRCShowChangedSize ) {
962 $r .= ( $rcObj->getCharacterDifference() == '' ? '' : $rcObj->getCharacterDifference() . ' . . ' ) ;
963 }
964 # User links
965 $r .= $rcObj->userlink;
966 $r .= $rcObj->usertalklink;
967 // log action
968 $this->insertAction( $r, $rcObj );
969 // log comment
970 $this->insertComment( $r, $rcObj );
971 # Rollback
972 $this->insertRollback( $r, $rcObj );
973 # Tags
974 $this->insertTags( $r, $rcObj, $classes );
975
976 $r .= "</td></tr>\n";
977 }
978 $r .= "</table></div>\n";
979
980 $this->rcCacheIndex++;
981
982 wfProfileOut( __METHOD__ );
983
984 return $r;
985 }
986
987 /**
988 * Generate HTML for an arrow or placeholder graphic
989 * @param $dir String: one of '', 'd', 'l', 'r'
990 * @param $alt String: text
991 * @param $title String: text
992 * @return String: HTML <img> tag
993 */
994 protected function arrow( $dir, $alt='', $title='' ) {
995 global $wgStylePath;
996 $encUrl = htmlspecialchars( $wgStylePath . '/common/images/Arr_' . $dir . '.png' );
997 $encAlt = htmlspecialchars( $alt );
998 $encTitle = htmlspecialchars( $title );
999 return "<img src=\"$encUrl\" width=\"12\" height=\"12\" alt=\"$encAlt\" title=\"$encTitle\" />";
1000 }
1001
1002 /**
1003 * Generate HTML for a right- or left-facing arrow,
1004 * depending on language direction.
1005 * @return String: HTML <img> tag
1006 */
1007 protected function sideArrow() {
1008 global $wgContLang;
1009 $dir = $wgContLang->isRTL() ? 'l' : 'r';
1010 return $this->arrow( $dir, '+', wfMsg( 'rc-enhanced-expand' ) );
1011 }
1012
1013 /**
1014 * Generate HTML for a down-facing arrow
1015 * depending on language direction.
1016 * @return String: HTML <img> tag
1017 */
1018 protected function downArrow() {
1019 return $this->arrow( 'd', '-', wfMsg( 'rc-enhanced-hide' ) );
1020 }
1021
1022 /**
1023 * Generate HTML for a spacer image
1024 * @return String: HTML <img> tag
1025 */
1026 protected function spacerArrow() {
1027 return $this->arrow( '', codepointToUtf8( 0xa0 ) ); // non-breaking space
1028 }
1029
1030 /**
1031 * Add a set of spaces
1032 * @return String: HTML <td> tag
1033 */
1034 protected function spacerIndent() {
1035 return '&#160;&#160;&#160;&#160;&#160;';
1036 }
1037
1038 /**
1039 * Enhanced RC ungrouped line.
1040 * @return String: a HTML formated line (generated using $r)
1041 */
1042 protected function recentChangesBlockLine( $rcObj ) {
1043 global $wgRCShowChangedSize;
1044
1045 wfProfileIn( __METHOD__ );
1046
1047 # Extract fields from DB into the function scope (rc_xxxx variables)
1048 // FIXME: Would be good to replace this extract() call with something
1049 // that explicitly initializes variables.
1050 $classes = array(); // TODO implement
1051 extract( $rcObj->mAttribs );
1052 $query['curid'] = $rc_cur_id;
1053
1054 $r = '<table class="mw-enhanced-rc"><tr>';
1055 $r .= '<td class="mw-enhanced-rc">' . $this->spacerArrow() . '&#160;';
1056 # Flag and Timestamp
1057 if( $rc_type == RC_MOVE || $rc_type == RC_MOVE_OVER_REDIRECT ) {
1058 $r .= '&#160;&#160;&#160;&#160;'; // 4 flags -> 4 spaces
1059 } else {
1060 $r .= $this->recentChangesFlags( $rc_type == RC_NEW, $rc_minor, $rcObj->unpatrolled, '&#160;', $rc_bot );
1061 }
1062 $r .= '&#160;'.$rcObj->timestamp.'&#160;</td><td style="padding:0px;">';
1063 # Article or log link
1064 if( $rc_log_type ) {
1065 $logtitle = Title::newFromText( "Log/$rc_log_type", NS_SPECIAL );
1066 $logname = LogPage::logName( $rc_log_type );
1067 $r .= '(' . $this->skin->link(
1068 $logtitle,
1069 $logname,
1070 array(),
1071 array(),
1072 array( 'known', 'noclasses' )
1073 ) . ')';
1074 } else {
1075 $this->insertArticleLink( $r, $rcObj, $rcObj->unpatrolled, $rcObj->watched );
1076 }
1077 # Diff and hist links
1078 if ( $rc_type != RC_LOG ) {
1079 $r .= ' ('. $rcObj->difflink . $this->message['pipe-separator'];
1080 $query['action'] = 'history';
1081 $r .= $this->skin->link(
1082 $rcObj->getTitle(),
1083 $this->message['hist'],
1084 array(),
1085 $query,
1086 array( 'known', 'noclasses' )
1087 ) . ')';
1088 }
1089 $r .= ' . . ';
1090 # Character diff
1091 if( $wgRCShowChangedSize && ($cd = $rcObj->getCharacterDifference()) ) {
1092 $r .= "$cd . . ";
1093 }
1094 # User/talk
1095 $r .= ' '.$rcObj->userlink . $rcObj->usertalklink;
1096 # Log action (if any)
1097 if( $rc_log_type ) {
1098 if( $this->isDeleted($rcObj,LogPage::DELETED_ACTION) ) {
1099 $r .= ' <span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
1100 } else {
1101 $r .= ' ' . LogPage::actionText( $rc_log_type, $rc_log_action, $rcObj->getTitle(),
1102 $this->skin, LogPage::extractParams($rc_params), true, true );
1103 }
1104 }
1105 $this->insertComment( $r, $rcObj );
1106 $this->insertRollback( $r, $rcObj );
1107 # Tags
1108 $this->insertTags( $r, $rcObj, $classes );
1109 # Show how many people are watching this if enabled
1110 $r .= $this->numberofWatchingusers($rcObj->numberofWatchingusers);
1111
1112 $r .= "</td></tr></table>\n";
1113
1114 wfProfileOut( __METHOD__ );
1115
1116 return $r;
1117 }
1118
1119 /**
1120 * If enhanced RC is in use, this function takes the previously cached
1121 * RC lines, arranges them, and outputs the HTML
1122 */
1123 protected function recentChangesBlock() {
1124 if( count ( $this->rc_cache ) == 0 ) {
1125 return '';
1126 }
1127
1128 wfProfileIn( __METHOD__ );
1129
1130 $blockOut = '';
1131 foreach( $this->rc_cache as $block ) {
1132 if( count( $block ) < 2 ) {
1133 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
1134 } else {
1135 $blockOut .= $this->recentChangesBlockGroup( $block );
1136 }
1137 }
1138
1139 wfProfileOut( __METHOD__ );
1140
1141 return '<div>'.$blockOut.'</div>';
1142 }
1143
1144 /**
1145 * Returns text for the end of RC
1146 * If enhanced RC is in use, returns pretty much all the text
1147 */
1148 public function endRecentChangesList() {
1149 return $this->recentChangesBlock() . parent::endRecentChangesList();
1150 }
1151
1152 }