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