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