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