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