Fixing the most annoying problem in Bug 5724: adding dir mark after the recent change...
[lhc/web/wiklou.git] / includes / ChangesList.php
1 <?php
2 /**
3 * @package MediaWiki
4 * Contain class to show various lists of change:
5 * - what's link here
6 * - related changes
7 * - recent changes
8 */
9
10 /**
11 * @todo document
12 * @package MediaWiki
13 */
14 class RCCacheEntry extends RecentChange
15 {
16 var $secureName, $link;
17 var $curlink , $difflink, $lastlink , $usertalklink , $versionlink ;
18 var $userlink, $timestamp, $watched;
19
20 function newFromParent( $rc )
21 {
22 $rc2 = new RCCacheEntry;
23 $rc2->mAttribs = $rc->mAttribs;
24 $rc2->mExtra = $rc->mExtra;
25 return $rc2;
26 }
27 } ;
28
29 /**
30 * @package MediaWiki
31 */
32 class ChangesList {
33 # Called by history lists and recent changes
34 #
35
36 /** @todo document */
37 function ChangesList( &$skin ) {
38 $this->skin =& $skin;
39 $this->preCacheMessages();
40 }
41
42 function newFromUser( &$user ) {
43 $sk =& $user->getSkin();
44 if( $user->getOption('usenewrc') ) {
45 return new EnhancedChangesList( $sk );
46 } else {
47 return new OldChangesList( $sk );
48 }
49 }
50
51 /**
52 * As we use the same small set of messages in various methods and that
53 * they are called often, we call them once and save them in $this->message
54 */
55 function preCacheMessages() {
56 // Precache various messages
57 if( !isset( $this->message ) ) {
58 foreach( explode(' ', 'cur diff hist minoreditletter newpageletter last '.
59 'blocklink changes history boteditletter' ) as $msg ) {
60 $this->message[$msg] = wfMsgExt( $msg, array( 'escape') );
61 }
62 }
63 }
64
65
66 /**
67 * Returns the appropriate flags for new page, minor change and patrolling
68 */
69 function recentChangesFlags( $new, $minor, $patrolled, $nothing = '&nbsp;', $bot = false ) {
70 $f = $new ? '<span class="newpage">' . $this->message['newpageletter'] . '</span>'
71 : $nothing;
72 $f .= $minor ? '<span class="minor">' . $this->message['minoreditletter'] . '</span>'
73 : $nothing;
74 $f .= $bot ? '<span class="bot">' . $this->message['boteditletter'] . '</span>' : $nothing;
75 $f .= $patrolled ? '<span class="unpatrolled">!</span>' : $nothing;
76 return $f;
77 }
78
79 /**
80 * Returns text for the start of the tabular part of RC
81 */
82 function beginRecentChangesList() {
83 $this->rc_cache = array();
84 $this->rcMoveIndex = 0;
85 $this->rcCacheIndex = 0;
86 $this->lastdate = '';
87 $this->rclistOpen = false;
88 return '';
89 }
90
91 /**
92 * Returns text for the end of RC
93 */
94 function endRecentChangesList() {
95 if( $this->rclistOpen ) {
96 return "</ul>\n";
97 } else {
98 return '';
99 }
100 }
101
102
103 function insertMove( &$s, $rc ) {
104 # Diff
105 $s .= '(' . $this->message['diff'] . ') (';
106 # Hist
107 $s .= $this->skin->makeKnownLinkObj( $rc->getMovedToTitle(), $this->message['hist'], 'action=history' ) .
108 ') . . ';
109
110 # "[[x]] moved to [[y]]"
111 $msg = ( $rc->mAttribs['rc_type'] == RC_MOVE ) ? '1movedto2' : '1movedto2_redir';
112 $s .= wfMsg( $msg, $this->skin->makeKnownLinkObj( $rc->getTitle(), '', 'redirect=no' ),
113 $this->skin->makeKnownLinkObj( $rc->getMovedToTitle(), '' ) );
114 }
115
116 function insertDateHeader(&$s, $rc_timestamp) {
117 global $wgLang;
118
119 # Make date header if necessary
120 $date = $wgLang->date( $rc_timestamp, true, true );
121 $s = '';
122 if( $date != $this->lastdate ) {
123 if( '' != $this->lastdate ) {
124 $s .= "</ul>\n";
125 }
126 $s .= '<h4>'.$date."</h4>\n<ul class=\"special\">";
127 $this->lastdate = $date;
128 $this->rclistOpen = true;
129 }
130 }
131
132 function insertLog(&$s, $title, $logtype) {
133 $logname = LogPage::logName( $logtype );
134 $s .= '(' . $this->skin->makeKnownLinkObj($title, $logname ) . ')';
135 }
136
137
138 function insertDiffHist(&$s, &$rc, $unpatrolled) {
139 # Diff link
140 if( $rc->mAttribs['rc_type'] == RC_NEW || $rc->mAttribs['rc_type'] == RC_LOG ) {
141 $diffLink = $this->message['diff'];
142 } else {
143 $rcidparam = $unpatrolled
144 ? array( 'rcid' => $rc->mAttribs['rc_id'] )
145 : array();
146 $diffLink = $this->skin->makeKnownLinkObj( $rc->getTitle(), $this->message['diff'],
147 wfArrayToCGI( array(
148 'curid' => $rc->mAttribs['rc_cur_id'],
149 'diff' => $rc->mAttribs['rc_this_oldid'],
150 'oldid' => $rc->mAttribs['rc_last_oldid'] ),
151 $rcidparam ),
152 '', '', ' tabindex="'.$rc->counter.'"');
153 }
154 $s .= '('.$diffLink.') (';
155
156 # History link
157 $s .= $this->skin->makeKnownLinkObj( $rc->getTitle(), $this->message['hist'],
158 wfArrayToCGI( array(
159 'curid' => $rc->mAttribs['rc_cur_id'],
160 'action' => 'history' ) ) );
161 $s .= ') . . ';
162 }
163
164 function insertArticleLink(&$s, &$rc, $unpatrolled, $watched) {
165 # Article link
166 # If it's a new article, there is no diff link, but if it hasn't been
167 # patrolled yet, we need to give users a way to do so
168 $params = ( $unpatrolled && $rc->mAttribs['rc_type'] == RC_NEW )
169 ? 'rcid='.$rc->mAttribs['rc_id']
170 : '';
171 $articlelink = ' '. $this->skin->makeKnownLinkObj( $rc->getTitle(), '', $params );
172 if($watched) $articlelink = '<strong>'.$articlelink.'</strong>';
173 global $wgContLang;
174 $articlelink .= $wgContLang->getDirMark();
175
176 $s .= ' '.$articlelink;
177 }
178
179 function insertTimestamp(&$s, &$rc) {
180 global $wgLang;
181 # Timestamp
182 $s .= '; ' . $wgLang->time( $rc->mAttribs['rc_timestamp'], true, true ) . ' . . ';
183 }
184
185 /** Insert links to user page, user talk page and eventually a blocking link */
186 function insertUserRelatedLinks(&$s, &$rc) {
187 $s .= $this->skin->userLink( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
188 $s .= $this->skin->userToolLinks( $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'] );
189 }
190
191 /** insert a formatted comment */
192 function insertComment(&$s, &$rc) {
193 # Add comment
194 if( $rc->mAttribs['rc_type'] != RC_MOVE && $rc->mAttribs['rc_type'] != RC_MOVE_OVER_REDIRECT ) {
195 $s .= $this->skin->commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle() );
196 }
197 }
198
199 /**
200 * Check whether to enable recent changes patrol features
201 * @return bool
202 */
203 function usePatrol() {
204 global $wgUseRCPatrol, $wgUser;
205 return( $wgUseRCPatrol && $wgUser->isAllowed( 'patrol' ) );
206 }
207
208
209 }
210
211
212 /**
213 * Generate a list of changes using the good old system (no javascript)
214 */
215 class OldChangesList extends ChangesList {
216 /**
217 * Format a line using the old system (aka without any javascript).
218 */
219 function recentChangesLine( &$rc, $watched = false ) {
220 global $wgContLang;
221
222 $fname = 'ChangesList::recentChangesLineOld';
223 wfProfileIn( $fname );
224
225
226 # Extract DB fields into local scope
227 extract( $rc->mAttribs );
228 $curIdEq = 'curid=' . $rc_cur_id;
229
230 # Should patrol-related stuff be shown?
231 $unpatrolled = $this->usePatrol() && $rc_patrolled == 0;
232
233 $this->insertDateHeader($s,$rc_timestamp);
234
235 $s .= '<li>';
236
237 // moved pages
238 if( $rc_type == RC_MOVE || $rc_type == RC_MOVE_OVER_REDIRECT ) {
239 $this->insertMove( $s, $rc );
240 // log entries
241 } elseif( $rc_namespace == NS_SPECIAL && preg_match( '!^Log/(.*)$!', $rc_title, $matches ) ) {
242 $this->insertLog($s, $rc->getTitle(), $matches[1]);
243 // all other stuff
244 } else {
245 wfProfileIn($fname.'-page');
246
247 $this->insertDiffHist($s, $rc, $unpatrolled);
248
249 # M, N, b and ! (minor, new, bot and unpatrolled)
250 $s .= ' ' . $this->recentChangesFlags( $rc_type == RC_NEW, $rc_minor, $unpatrolled, '', $rc_bot );
251 $this->insertArticleLink($s, $rc, $unpatrolled, $watched);
252
253 wfProfileOut($fname.'-page');
254 }
255
256 wfProfileIn( $fname.'-rest' );
257
258 $this->insertTimestamp($s,$rc);
259 $this->insertUserRelatedLinks($s,$rc);
260 $this->insertComment($s, $rc);
261
262 if($rc->numberofWatchingusers > 0) {
263 $s .= ' ' . wfMsg('number_of_watching_users_RCview', $wgContLang->formatNum($rc->numberofWatchingusers));
264 }
265
266 $s .= "</li>\n";
267
268 wfProfileOut( $fname.'-rest' );
269
270 wfProfileOut( $fname );
271 return $s;
272 }
273 }
274
275
276 /**
277 * Generate a list of changes using an Enhanced system (use javascript).
278 */
279 class EnhancedChangesList extends ChangesList {
280 /**
281 * Format a line for enhanced recentchange (aka with javascript and block of lines).
282 */
283 function recentChangesLine( &$baseRC, $watched = false ) {
284 global $wgLang, $wgContLang;
285
286 # Create a specialised object
287 $rc = RCCacheEntry::newFromParent( $baseRC );
288
289 # Extract fields from DB into the function scope (rc_xxxx variables)
290 extract( $rc->mAttribs );
291 $curIdEq = 'curid=' . $rc_cur_id;
292
293 # If it's a new day, add the headline and flush the cache
294 $date = $wgLang->date( $rc_timestamp, true);
295 $ret = '';
296 if( $date != $this->lastdate ) {
297 # Process current cache
298 $ret = $this->recentChangesBlock();
299 $this->rc_cache = array();
300 $ret .= "<h4>{$date}</h4>\n";
301 $this->lastdate = $date;
302 }
303
304 # Should patrol-related stuff be shown?
305 if( $this->usePatrol() ) {
306 $rc->unpatrolled = !$rc_patrolled;
307 } else {
308 $rc->unpatrolled = false;
309 }
310
311 # Make article link
312 if( $rc_type == RC_MOVE || $rc_type == RC_MOVE_OVER_REDIRECT ) {
313 $msg = ( $rc_type == RC_MOVE ) ? "1movedto2" : "1movedto2_redir";
314 $clink = wfMsg( $msg, $this->skin->makeKnownLinkObj( $rc->getTitle(), '', 'redirect=no' ),
315 $this->skin->makeKnownLinkObj( $rc->getMovedToTitle(), '' ) );
316 } elseif( $rc_namespace == NS_SPECIAL && preg_match( '!^Log/(.*)$!', $rc_title, $matches ) ) {
317 # Log updates, etc
318 $logtype = $matches[1];
319 $logname = LogPage::logName( $logtype );
320 $clink = '(' . $this->skin->makeKnownLinkObj( $rc->getTitle(), $logname ) . ')';
321 } elseif( $rc->unpatrolled && $rc_type == RC_NEW ) {
322 # Unpatrolled new page, give rc_id in query
323 $clink = $this->skin->makeKnownLinkObj( $rc->getTitle(), '', "rcid={$rc_id}" );
324 } else {
325 $clink = $this->skin->makeKnownLinkObj( $rc->getTitle(), '' );
326 }
327
328 $time = $wgContLang->time( $rc_timestamp, true, true );
329 $rc->watched = $watched;
330 $rc->link = $clink;
331 $rc->timestamp = $time;
332 $rc->numberofWatchingusers = $baseRC->numberofWatchingusers;
333
334 # Make "cur" and "diff" links
335 if( $rc->unpatrolled ) {
336 $rcIdQuery = "&rcid={$rc_id}";
337 } else {
338 $rcIdQuery = '';
339 }
340 $querycur = $curIdEq."&diff=0&oldid=$rc_this_oldid";
341 $querydiff = $curIdEq."&diff=$rc_this_oldid&oldid=$rc_last_oldid$rcIdQuery";
342 $aprops = ' tabindex="'.$baseRC->counter.'"';
343 $curLink = $this->skin->makeKnownLinkObj( $rc->getTitle(), $this->message['cur'], $querycur, '' ,'', $aprops );
344 if( $rc_type == RC_NEW || $rc_type == RC_LOG || $rc_type == RC_MOVE || $rc_type == RC_MOVE_OVER_REDIRECT ) {
345 if( $rc_type != RC_NEW ) {
346 $curLink = $this->message['cur'];
347 }
348 $diffLink = $this->message['diff'];
349 } else {
350 $diffLink = $this->skin->makeKnownLinkObj( $rc->getTitle(), $this->message['diff'], $querydiff, '' ,'', $aprops );
351 }
352
353 # Make "last" link
354 if( $rc_last_oldid == 0 || $rc_type == RC_LOG || $rc_type == RC_MOVE || $rc_type == RC_MOVE_OVER_REDIRECT ) {
355 $lastLink = $this->message['last'];
356 } else {
357 $lastLink = $this->skin->makeKnownLinkObj( $rc->getTitle(), $this->message['last'],
358 $curIdEq.'&diff='.$rc_this_oldid.'&oldid='.$rc_last_oldid . $rcIdQuery );
359 }
360
361 $rc->userlink = $this->skin->userLink( $rc_user, $rc_user_text );
362
363 $rc->lastlink = $lastLink;
364 $rc->curlink = $curLink;
365 $rc->difflink = $diffLink;
366
367 $rc->usertalklink = $this->skin->userToolLinks( $rc_user, $rc_user_text );
368
369 # Put accumulated information into the cache, for later display
370 # Page moves go on their own line
371 $title = $rc->getTitle();
372 $secureName = $title->getPrefixedDBkey();
373 if( $rc_type == RC_MOVE || $rc_type == RC_MOVE_OVER_REDIRECT ) {
374 # Use an @ character to prevent collision with page names
375 $this->rc_cache['@@' . ($this->rcMoveIndex++)] = array($rc);
376 } else {
377 if( !isset ( $this->rc_cache[$secureName] ) ) {
378 $this->rc_cache[$secureName] = array();
379 }
380 array_push( $this->rc_cache[$secureName], $rc );
381 }
382 return $ret;
383 }
384
385 /**
386 * Enhanced RC group
387 */
388 function recentChangesBlockGroup( $block ) {
389 $r = '';
390
391 # Collate list of users
392 $isnew = false;
393 $unpatrolled = false;
394 $userlinks = array();
395 foreach( $block as $rcObj ) {
396 $oldid = $rcObj->mAttribs['rc_last_oldid'];
397 $newid = $rcObj->mAttribs['rc_this_oldid'];
398 if( $rcObj->mAttribs['rc_new'] ) {
399 $isnew = true;
400 }
401 $u = $rcObj->userlink;
402 if( !isset( $userlinks[$u] ) ) {
403 $userlinks[$u] = 0;
404 }
405 if( $rcObj->unpatrolled ) {
406 $unpatrolled = true;
407 }
408 $bot = $rcObj->mAttribs['rc_bot'];
409 $userlinks[$u]++;
410 }
411
412 # Sort the list and convert to text
413 krsort( $userlinks );
414 asort( $userlinks );
415 $users = array();
416 foreach( $userlinks as $userlink => $count) {
417 $text = $userlink;
418 if( $count > 1 ) {
419 $text .= ' ('.$count.'&times;)';
420 }
421 array_push( $users, $text );
422 }
423
424 $users = ' <span class="changedby">['.implode('; ',$users).']</span>';
425
426 # Arrow
427 $rci = 'RCI'.$this->rcCacheIndex;
428 $rcl = 'RCL'.$this->rcCacheIndex;
429 $rcm = 'RCM'.$this->rcCacheIndex;
430 $toggleLink = "javascript:toggleVisibility('$rci','$rcm','$rcl')";
431 $tl = '<span id="'.$rcm.'"><a href="'.$toggleLink.'">' . $this->sideArrow() . '</a></span>';
432 $tl .= '<span id="'.$rcl.'" style="display:none"><a href="'.$toggleLink.'">' . $this->downArrow() . '</a></span>';
433 $r .= $tl;
434
435 # Main line
436 $r .= '<tt>';
437 $r .= $this->recentChangesFlags( $isnew, false, $unpatrolled, '&nbsp;', $bot );
438
439 # Timestamp
440 $r .= ' '.$block[0]->timestamp.' ';
441 $r .= '</tt>';
442
443 # Article link
444 $r .= $this->maybeWatchedLink( $block[0]->link, $block[0]->watched );
445
446 $curIdEq = 'curid=' . $block[0]->mAttribs['rc_cur_id'];
447 $currentRevision = $block[0]->mAttribs['rc_this_oldid'];
448 if( $block[0]->mAttribs['rc_type'] != RC_LOG ) {
449 # Changes
450 $r .= ' ('.count($block).' ';
451 if( $isnew ) {
452 $r .= $this->message['changes'];
453 } else {
454 $r .= $this->skin->makeKnownLinkObj( $block[0]->getTitle(),
455 $this->message['changes'], $curIdEq."&diff=$currentRevision&oldid=$oldid" );
456 }
457 $r .= '; ';
458
459 # History
460 $r .= $this->skin->makeKnownLinkObj( $block[0]->getTitle(),
461 $this->message['history'], $curIdEq.'&action=history' );
462 $r .= ')';
463 }
464
465 $r .= $users;
466
467 if($block[0]->numberofWatchingusers > 0) {
468 global $wgContLang;
469 $r .= wfMsg('number_of_watching_users_RCview', $wgContLang->formatNum($block[0]->numberofWatchingusers));
470 }
471 $r .= "<br />\n";
472
473 # Sub-entries
474 $r .= '<div id="'.$rci.'" style="display:none">';
475 foreach( $block as $rcObj ) {
476 # Get rc_xxxx variables
477 extract( $rcObj->mAttribs );
478
479 $r .= $this->spacerArrow();
480 $r .= '<tt>&nbsp; &nbsp; &nbsp; &nbsp;';
481 $r .= $this->recentChangesFlags( $rc_new, $rc_minor, $rcObj->unpatrolled, '&nbsp;', $rc_bot );
482 $r .= '&nbsp;</tt>';
483
484 $o = '';
485 if( $rc_this_oldid != 0 ) {
486 $o = 'oldid='.$rc_this_oldid;
487 }
488 if( $rc_type == RC_LOG ) {
489 $link = $rcObj->timestamp;
490 } else {
491 $link = $this->skin->makeKnownLinkObj( $rcObj->getTitle(), $rcObj->timestamp, $curIdEq.'&'.$o );
492 }
493 $link = '<tt>'.$link.'</tt>';
494
495 $r .= $link;
496 $r .= ' (';
497 $r .= $rcObj->curlink;
498 $r .= '; ';
499 $r .= $rcObj->lastlink;
500 $r .= ') . . '.$rcObj->userlink;
501 $r .= $rcObj->usertalklink;
502 $r .= $this->skin->commentBlock( $rc_comment, $rcObj->getTitle() );
503 $r .= "<br />\n";
504 }
505 $r .= "</div>\n";
506
507 $this->rcCacheIndex++;
508 return $r;
509 }
510
511 function maybeWatchedLink( $link, $watched=false ) {
512 if( $watched ) {
513 // FIXME: css style might be more appropriate
514 return '<strong>' . $link . '</strong>';
515 } else {
516 return $link;
517 }
518 }
519
520 /**
521 * Generate HTML for an arrow or placeholder graphic
522 * @param string $dir one of '', 'd', 'l', 'r'
523 * @param string $alt text
524 * @return string HTML <img> tag
525 * @access private
526 */
527 function arrow( $dir, $alt='' ) {
528 global $wgStylePath;
529 $encUrl = htmlspecialchars( $wgStylePath . '/common/images/Arr_' . $dir . '.png' );
530 $encAlt = htmlspecialchars( $alt );
531 return "<img src=\"$encUrl\" width=\"12\" height=\"12\" alt=\"$encAlt\" />";
532 }
533
534 /**
535 * Generate HTML for a right- or left-facing arrow,
536 * depending on language direction.
537 * @return string HTML <img> tag
538 * @access private
539 */
540 function sideArrow() {
541 global $wgContLang;
542 $dir = $wgContLang->isRTL() ? 'l' : 'r';
543 return $this->arrow( $dir, '+' );
544 }
545
546 /**
547 * Generate HTML for a down-facing arrow
548 * depending on language direction.
549 * @return string HTML <img> tag
550 * @access private
551 */
552 function downArrow() {
553 return $this->arrow( 'd', '-' );
554 }
555
556 /**
557 * Generate HTML for a spacer image
558 * @return string HTML <img> tag
559 * @access private
560 */
561 function spacerArrow() {
562 return $this->arrow( '', ' ' );
563 }
564
565 /**
566 * Enhanced RC ungrouped line.
567 * @return string a HTML formated line (generated using $r)
568 */
569 function recentChangesBlockLine( $rcObj ) {
570 global $wgContLang;
571
572 # Get rc_xxxx variables
573 extract( $rcObj->mAttribs );
574 $curIdEq = 'curid='.$rc_cur_id;
575
576 $r = '';
577
578 # Spacer image
579 $r .= $this->spacerArrow();
580
581 # Flag and Timestamp
582 $r .= '<tt>';
583
584 if( $rc_type == RC_MOVE || $rc_type == RC_MOVE_OVER_REDIRECT ) {
585 $r .= '&nbsp;&nbsp;&nbsp;';
586 } else {
587 $r .= $this->recentChangesFlags( $rc_type == RC_NEW, $rc_minor, $rcObj->unpatrolled, '&nbsp;', $rc_bot );
588 }
589 $r .= ' '.$rcObj->timestamp.' </tt>';
590
591 # Article link
592 $r .= $this->maybeWatchedLink( $rcObj->link, $rcObj->watched );
593
594 # Diff
595 $r .= ' ('. $rcObj->difflink .'; ';
596
597 # Hist
598 $r .= $this->skin->makeKnownLinkObj( $rcObj->getTitle(), wfMsg( 'hist' ), $curIdEq.'&action=history' );
599
600 # User/talk
601 $r .= ') . . '.$rcObj->userlink . $rcObj->usertalklink;
602
603 # Comment
604 if( $rc_type != RC_MOVE && $rc_type != RC_MOVE_OVER_REDIRECT ) {
605 $r .= $this->skin->commentBlock( $rc_comment, $rcObj->getTitle() );
606 }
607
608 if( $rcObj->numberofWatchingusers > 0 ) {
609 $r .= wfMsg('number_of_watching_users_RCview', $wgContLang->formatNum($rcObj->numberofWatchingusers));
610 }
611
612 $r .= "<br />\n";
613 return $r;
614 }
615
616 /**
617 * If enhanced RC is in use, this function takes the previously cached
618 * RC lines, arranges them, and outputs the HTML
619 */
620 function recentChangesBlock() {
621 if( count ( $this->rc_cache ) == 0 ) {
622 return '';
623 }
624 $blockOut = '';
625 foreach( $this->rc_cache as $secureName => $block ) {
626 if( count( $block ) < 2 ) {
627 $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
628 } else {
629 $blockOut .= $this->recentChangesBlockGroup( $block );
630 }
631 }
632
633 return '<div>'.$blockOut.'</div>';
634 }
635
636 /**
637 * Returns text for the end of RC
638 * If enhanced RC is in use, returns pretty much all the text
639 */
640 function endRecentChangesList() {
641 return $this->recentChangesBlock() . parent::endRecentChangesList();
642 }
643
644 }
645 ?>