backport changes for RC-backed watchlist in recentchanges code from live site
[lhc/web/wiklou.git] / includes / ChangesList.php
1 <?php
2 /**
3 * @package MediaWiki
4 */
5
6 /**
7 * @package MediaWiki
8 */
9 class ChangesList {
10 # Called by history lists and recent changes
11 #
12
13 /** @todo document */
14 function ChangesList( &$skin ) {
15 $this->skin =& $skin;
16 }
17
18 /**
19 * Returns the appropiate flags for new page, minor change and patrolling
20 */
21 function recentChangesFlags( $new, $minor, $patrolled, $nothing = '&nbsp;' ) {
22 $f = $new ? '<span class="newpage">' . htmlspecialchars( wfMsg( 'newpageletter' ) ) . '</span>'
23 : $nothing;
24 $f .= $minor ? '<span class="minor">' . htmlspecialchars( wfMsg( 'minoreditletter' ) ) . '</span>'
25 : $nothing;
26 $f .= $patrolled ? '<span class="unpatrolled">!</span>' : $nothing;
27 return $f;
28
29 }
30
31 /**
32 * Returns text for the start of the tabular part of RC
33 */
34 function beginRecentChangesList() {
35 $this->rc_cache = array() ;
36 $this->rcMoveIndex = 0;
37 $this->rcCacheIndex = 0 ;
38 $this->lastdate = '';
39 $this->rclistOpen = false;
40 return '';
41 }
42
43 /**
44 * Returns text for the end of RC
45 * If enhanced RC is in use, returns pretty much all the text
46 */
47 function endRecentChangesList() {
48 $s = $this->recentChangesBlock() ;
49 if( $this->rclistOpen ) {
50 $s .= "</ul>\n";
51 }
52 return $s;
53 }
54
55 /**
56 * Enhanced RC ungrouped line
57 */
58 function recentChangesBlockLine ( $rcObj ) {
59 global $wgStylePath, $wgContLang ;
60
61 # Get rc_xxxx variables
62 extract( $rcObj->mAttribs ) ;
63 $curIdEq = 'curid='.$rc_cur_id;
64
65 # Spacer image
66 $r = '' ;
67
68 $r .= '<img src="'.$wgStylePath.'/common/images/Arr_.png" width="12" height="12" border="0" />' ;
69 $r .= '<tt>' ;
70
71 if ( $rc_type == RC_MOVE || $rc_type == RC_MOVE_OVER_REDIRECT ) {
72 $r .= '&nbsp;&nbsp;&nbsp;';
73 } else {
74 $r .= $this->recentChangesFlags( $rc_type == RC_NEW, $rc_minor, $rcObj->unpatrolled );
75 }
76
77 # Timestamp
78 $r .= ' '.$rcObj->timestamp.' ' ;
79 $r .= '</tt>' ;
80
81 # Article link
82 $link = $rcObj->link ;
83 if ( $rcObj->watched ) $link = '<strong>'.$link.'</strong>' ;
84 $r .= $link ;
85
86 # Diff
87 $r .= ' (' ;
88 $r .= $rcObj->difflink ;
89 $r .= '; ' ;
90
91 # Hist
92 $r .= $this->skin->makeKnownLinkObj( $rcObj->getTitle(), wfMsg( 'hist' ), $curIdEq.'&action=history' );
93
94 # User/talk
95 $r .= ') . . '.$rcObj->userlink ;
96 $r .= $rcObj->usertalklink ;
97
98 # Comment
99 if ( $rc_type != RC_MOVE && $rc_type != RC_MOVE_OVER_REDIRECT ) {
100 $r .= $this->skin->commentBlock( $rc_comment, $rcObj->getTitle() );
101 }
102
103 if ($rcObj->numberofWatchingusers > 0) {
104 $r .= wfMsg('number_of_watching_users_RCview', $wgContLang->formatNum($rcObj->numberofWatchingusers));
105 }
106
107 $r .= "<br />\n" ;
108 return $r ;
109 }
110
111 /**
112 * Enhanced RC group
113 */
114 function recentChangesBlockGroup ( $block ) {
115 global $wgStylePath, $wgContLang ;
116
117 $r = '';
118
119 # Collate list of users
120 $isnew = false ;
121 $unpatrolled = false;
122 $userlinks = array () ;
123 foreach ( $block AS $rcObj ) {
124 $oldid = $rcObj->mAttribs['rc_last_oldid'];
125 $newid = $rcObj->mAttribs['rc_this_oldid'];
126 if ( $rcObj->mAttribs['rc_new'] ) {
127 $isnew = true ;
128 }
129 $u = $rcObj->userlink ;
130 if ( !isset ( $userlinks[$u] ) ) {
131 $userlinks[$u] = 0 ;
132 }
133 if ( $rcObj->unpatrolled ) {
134 $unpatrolled = true;
135 }
136 $userlinks[$u]++ ;
137 }
138
139 # Sort the list and convert to text
140 krsort ( $userlinks ) ;
141 asort ( $userlinks ) ;
142 $users = array () ;
143 foreach ( $userlinks as $userlink => $count) {
144 $text = $userlink ;
145 if ( $count > 1 ) $text .= " ({$count}&times;)" ;
146 array_push ( $users , $text ) ;
147 }
148 $users = ' <span class="changedby">['.implode('; ',$users).']</span>';
149
150 # Arrow
151 $rci = 'RCI'.$this->rcCacheIndex ;
152 $rcl = 'RCL'.$this->rcCacheIndex ;
153 $rcm = 'RCM'.$this->rcCacheIndex ;
154 $toggleLink = "javascript:toggleVisibility('$rci','$rcm','$rcl')" ;
155 $arrowdir = $wgContLang->isRTL() ? 'l' : 'r';
156 $tl = '<span id="'.$rcm.'"><a href="'.$toggleLink.'"><img src="'.$wgStylePath.'/common/images/Arr_'.$arrowdir.'.png" width="12" height="12" alt="+" /></a></span>' ;
157 $tl .= '<span id="'.$rcl.'" style="display:none"><a href="'.$toggleLink.'"><img src="'.$wgStylePath.'/common/images/Arr_d.png" width="12" height="12" alt="-" /></a></span>' ;
158 $r .= $tl ;
159
160 # Main line
161
162 $r .= '<tt>' ;
163 $r .= $this->recentChangesFlags( $isnew, false, $unpatrolled );
164
165 # Timestamp
166 $r .= ' '.$block[0]->timestamp.' ' ;
167 $r .= '</tt>' ;
168
169 # Article link
170 $link = $block[0]->link ;
171 if ( $block[0]->watched ) $link = '<strong>'.$link.'</strong>' ;
172 $r .= $link ;
173
174 $curIdEq = 'curid=' . $block[0]->mAttribs['rc_cur_id'];
175 if ( $block[0]->mAttribs['rc_type'] != RC_LOG ) {
176 # Changes
177 $r .= ' ('.count($block).' ' ;
178 if ( $isnew ) $r .= wfMsg('changes');
179 else $r .= $this->skin->makeKnownLinkObj( $block[0]->getTitle() , wfMsg('changes') ,
180 $curIdEq."&diff=$oldid&oldid=$newid" ) ;
181 $r .= '; ' ;
182
183 # History
184 $r .= $this->skin->makeKnownLinkObj( $block[0]->getTitle(), wfMsg( 'history' ), $curIdEq.'&action=history' );
185 $r .= ')' ;
186 }
187
188 $r .= $users ;
189
190 if ($block[0]->numberofWatchingusers > 0) {
191 $r .= wfMsg('number_of_watching_users_RCview', $wgContLang->formatNum($block[0]->numberofWatchingusers));
192 }
193 $r .= "<br />\n" ;
194
195 # Sub-entries
196 $r .= '<div id="'.$rci.'" style="display:none">' ;
197 foreach ( $block AS $rcObj ) {
198 # Get rc_xxxx variables
199 extract( $rcObj->mAttribs );
200
201 $r .= '<img src="'.$wgStylePath.'/common/images/Arr_.png" width="12" height="12" />';
202 $r .= '<tt>&nbsp; &nbsp; &nbsp; &nbsp;' ;
203 $r .= $this->recentChangesFlags( $rc_new, $rc_minor, $rcObj->unpatrolled );
204 $r .= '&nbsp;</tt>' ;
205
206 $o = '' ;
207 if ( $rc_last_oldid != 0 ) {
208 $o = 'oldid='.$rc_last_oldid ;
209 }
210 if ( $rc_type == RC_LOG ) {
211 $link = $rcObj->timestamp ;
212 } else {
213 $link = $this->skin->makeKnownLinkObj( $rcObj->getTitle(), $rcObj->timestamp , "{$curIdEq}&$o" ) ;
214 }
215 $link = '<tt>'.$link.'</tt>' ;
216
217 $r .= $link ;
218 $r .= ' (' ;
219 $r .= $rcObj->curlink ;
220 $r .= '; ' ;
221 $r .= $rcObj->lastlink ;
222 $r .= ') . . '.$rcObj->userlink ;
223 $r .= $rcObj->usertalklink ;
224 $r .= $this->skin->commentBlock( $rc_comment, $rcObj->getTitle() );
225 $r .= "<br />\n" ;
226 }
227 $r .= "</div>\n" ;
228
229 $this->rcCacheIndex++ ;
230 return $r ;
231 }
232
233 /**
234 * If enhanced RC is in use, this function takes the previously cached
235 * RC lines, arranges them, and outputs the HTML
236 */
237 function recentChangesBlock () {
238 global $wgStylePath ;
239 if ( count ( $this->rc_cache ) == 0 ) return '' ;
240 $blockOut = '';
241 foreach ( $this->rc_cache AS $secureName => $block ) {
242 if ( count ( $block ) < 2 ) {
243 $blockOut .= $this->recentChangesBlockLine ( array_shift ( $block ) ) ;
244 } else {
245 $blockOut .= $this->recentChangesBlockGroup ( $block ) ;
246 }
247 }
248
249 return '<div>'.$blockOut.'</div>' ;
250 }
251
252 /**
253 * Called in a loop over all displayed RC entries
254 * Either returns the line, or caches it for later use
255 */
256 function recentChangesLine( &$rc, $watched = false ) {
257 global $wgUser;
258 $usenew = $wgUser->getOption( 'usenewrc' );
259 if ( $usenew )
260 $line = $this->recentChangesLineNew ( $rc, $watched ) ;
261 else
262 $line = $this->recentChangesLineOld ( $rc, $watched ) ;
263 return $line ;
264 }
265
266
267 function recentChangesLineOld( &$rc, $watched = false ) {
268 global $wgTitle, $wgLang, $wgContLang, $wgUser, $wgUseRCPatrol,
269 $wgOnlySysopsCanPatrol, $wgSysopUserBans;
270
271 $fname = 'Skin::recentChangesLineOld';
272 wfProfileIn( $fname );
273
274 static $message;
275 if( !isset( $message ) ) {
276 foreach( explode(' ', 'diff hist minoreditletter newpageletter blocklink' ) as $msg ) {
277 $message[$msg] = wfMsg( $msg );
278 }
279 }
280
281 # Extract DB fields into local scope
282 extract( $rc->mAttribs );
283 $curIdEq = 'curid=' . $rc_cur_id;
284
285 # Should patrol-related stuff be shown?
286 $unpatrolled = $wgUseRCPatrol && $wgUser->isLoggedIn() &&
287 ( !$wgOnlySysopsCanPatrol || $wgUser->isAllowed('patrol') ) && $rc_patrolled == 0;
288
289 # Make date header if necessary
290 $date = $wgLang->date( $rc_timestamp, true, true );
291 $s = '';
292 if ( $date != $this->lastdate ) {
293 if ( '' != $this->lastdate ) { $s .= "</ul>\n"; }
294 $s .= "<h4>{$date}</h4>\n<ul class=\"special\">";
295 $this->lastdate = $date;
296 $this->rclistOpen = true;
297 }
298
299 $s .= '<li>';
300
301 if ( $rc_type == RC_MOVE || $rc_type == RC_MOVE_OVER_REDIRECT ) {
302 # Diff
303 $s .= '(' . $message['diff'] . ') (';
304 # Hist
305 $s .= $this->skin->makeKnownLinkObj( $rc->getMovedToTitle(), $message['hist'], 'action=history' ) .
306 ') . . ';
307
308 # "[[x]] moved to [[y]]"
309 $msg = ( $rc_type == RC_MOVE ) ? '1movedto2' : '1movedto2_redir';
310 $s .= wfMsg( $msg, $this->skin->makeKnownLinkObj( $rc->getTitle(), '', 'redirect=no' ),
311 $this->skin->makeKnownLinkObj( $rc->getMovedToTitle(), '' ) );
312 } elseif( $rc_namespace == NS_SPECIAL && preg_match( '!^Log/(.*)$!', $rc_title, $matches ) ) {
313 # Log updates, etc
314 $logtype = $matches[1];
315 $logname = LogPage::logName( $logtype );
316 $s .= '(' . $this->skin->makeKnownLinkObj( $rc->getTitle(), $logname ) . ')';
317 } else {
318 wfProfileIn("$fname-page");
319 # Diff link
320 if ( $rc_type == RC_NEW || $rc_type == RC_LOG ) {
321 $diffLink = $message['diff'];
322 } else {
323 if ( $unpatrolled )
324 $rcidparam = "&rcid={$rc_id}";
325 else
326 $rcidparam = "";
327 $diffLink = $this->skin->makeKnownLinkObj( $rc->getTitle(), $message['diff'],
328 "{$curIdEq}&diff={$rc_this_oldid}&oldid={$rc_last_oldid}{$rcidparam}",
329 '', '', ' tabindex="'.$rc->counter.'"');
330 }
331 $s .= '('.$diffLink.') (';
332
333 # History link
334 $s .= $this->skin->makeKnownLinkObj( $rc->getTitle(), $message['hist'], $curIdEq.'&action=history' );
335 $s .= ') . . ';
336
337 # M, N and ! (minor, new and unpatrolled)
338 $s .= ' ' . $this->recentChangesFlags( $rc_type == RC_NEW, $rc_minor, $unpatrolled, '' );
339
340 # Article link
341 # If it's a new article, there is no diff link, but if it hasn't been
342 # patrolled yet, we need to give users a way to do so
343 if ( $unpatrolled && $rc_type == RC_NEW )
344 $articleLink = $this->skin->makeKnownLinkObj( $rc->getTitle(), '', "rcid={$rc_id}" );
345 else
346 $articleLink = $this->skin->makeKnownLinkObj( $rc->getTitle(), '' );
347
348 if ( $watched ) {
349 $articleLink = '<strong>'.$articleLink.'</strong>';
350 }
351
352 $s .= ' '.$articleLink;
353 wfProfileOut("$fname-page");
354 }
355
356 wfProfileIn( "$fname-rest" );
357 # Timestamp
358 $s .= '; ' . $wgLang->time( $rc_timestamp, true, true ) . ' . . ';
359
360 # User link (or contributions for unregistered users)
361 if ( 0 == $rc_user ) {
362 $contribsPage =& Title::makeTitle( NS_SPECIAL, 'Contributions' );
363 $userLink = $this->skin->makeKnownLinkObj( $contribsPage,
364 $rc_user_text, 'target=' . $rc_user_text );
365 } else {
366 $userPage =& Title::makeTitle( NS_USER, $rc_user_text );
367 $userLink = $this->skin->makeLinkObj( $userPage, $rc_user_text );
368 }
369 $s .= $userLink;
370
371 # User talk link
372 $talkname = $wgContLang->getNsText(NS_TALK); # use the shorter name
373 global $wgDisableAnonTalk;
374 if( 0 == $rc_user && $wgDisableAnonTalk ) {
375 $userTalkLink = '';
376 } else {
377 $userTalkPage =& Title::makeTitle( NS_USER_TALK, $rc_user_text );
378 $userTalkLink= $this->skin->makeLinkObj( $userTalkPage, $talkname );
379 }
380 # Block link
381 $blockLink='';
382 if ( ( $wgSysopUserBans || 0 == $rc_user ) && $wgUser->isAllowed('block') ) {
383 $blockLinkPage = Title::makeTitle( NS_SPECIAL, 'Blockip' );
384 $blockLink = $this->skin->makeKnownLinkObj( $blockLinkPage,
385 $message['blocklink'], 'ip='.$rc_user_text );
386
387 }
388 if($blockLink) {
389 if($userTalkLink) $userTalkLink .= ' | ';
390 $userTalkLink .= $blockLink;
391 }
392 if($userTalkLink) $s.=' ('.$userTalkLink.')';
393
394 # Add comment
395 if ( $rc_type != RC_MOVE && $rc_type != RC_MOVE_OVER_REDIRECT ) {
396 $s .= $this->skin->commentBlock( $rc_comment, $rc->getTitle() );
397 }
398
399 if ($rc->numberofWatchingusers > 0) {
400 $s .= ' ' . wfMsg('number_of_watching_users_RCview', $wgContLang->formatNum($rc->numberofWatchingusers));
401 }
402
403 $s .= "</li>\n";
404
405 wfProfileOut( "$fname-rest" );
406 wfProfileOut( $fname );
407 return $s;
408 }
409
410 function recentChangesLineNew( &$baseRC, $watched = false ) {
411 global $wgTitle, $wgLang, $wgContLang, $wgUser,
412 $wgUseRCPatrol, $wgOnlySysopsCanPatrol, $wgSysopUserBans;
413
414 static $message;
415 if( !isset( $message ) ) {
416 foreach( explode(' ', 'cur diff hist minoreditletter newpageletter last blocklink' ) as $msg ) {
417 $message[$msg] = wfMsg( $msg );
418 }
419 }
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 extract( $rc->mAttribs );
426 $curIdEq = 'curid=' . $rc_cur_id;
427
428 # If it's a new day, add the headline and flush the cache
429 $date = $wgLang->date( $rc_timestamp, true);
430 $ret = '';
431 if ( $date != $this->lastdate ) {
432 # Process current cache
433 $ret = $this->recentChangesBlock () ;
434 $this->rc_cache = array() ;
435 $ret .= "<h4>{$date}</h4>\n";
436 $this->lastdate = $date;
437 }
438
439 # Should patrol-related stuff be shown?
440 if ( $wgUseRCPatrol && $wgUser->isLoggedIn() &&
441 ( !$wgOnlySysopsCanPatrol || $wgUser->isAllowed('patrol') )) {
442 $rc->unpatrolled = !$rc_patrolled;
443 } else {
444 $rc->unpatrolled = false;
445 }
446
447 # Make article link
448 if ( $rc_type == RC_MOVE || $rc_type == RC_MOVE_OVER_REDIRECT ) {
449 $msg = ( $rc_type == RC_MOVE ) ? "1movedto2" : "1movedto2_redir";
450 $clink = wfMsg( $msg, $this->skin->makeKnownLinkObj( $rc->getTitle(), '', 'redirect=no' ),
451 $this->skin->makeKnownLinkObj( $rc->getMovedToTitle(), '' ) );
452 } elseif( $rc_namespace == NS_SPECIAL && preg_match( '!^Log/(.*)$!', $rc_title, $matches ) ) {
453 # Log updates, etc
454 $logtype = $matches[1];
455 $logname = LogPage::logName( $logtype );
456 $clink = '(' . $this->skin->makeKnownLinkObj( $rc->getTitle(), $logname ) . ')';
457 } elseif ( $rc->unpatrolled && $rc_type == RC_NEW ) {
458 # Unpatrolled new page, give rc_id in query
459 $clink = $this->skin->makeKnownLinkObj( $rc->getTitle(), '', "rcid={$rc_id}" );
460 } else {
461 $clink = $this->skin->makeKnownLinkObj( $rc->getTitle(), '' ) ;
462 }
463
464 $time = $wgContLang->time( $rc_timestamp, true, true );
465 $rc->watched = $watched ;
466 $rc->link = $clink ;
467 $rc->timestamp = $time;
468 $rc->numberofWatchingusers = $baseRC->numberofWatchingusers;
469
470 # Make "cur" and "diff" links
471 $titleObj = $rc->getTitle();
472 if ( $rc->unpatrolled ) {
473 $rcIdQuery = "&rcid={$rc_id}";
474 } else {
475 $rcIdQuery = '';
476 }
477 if ( ( $rc_type == RC_NEW && $rc_this_oldid == 0 ) || $rc_type == RC_LOG || $rc_type == RC_MOVE || $rc_type == RC_MOVE_OVER_REDIRECT ) {
478 $curLink = $message['cur'];
479 $diffLink = $message['diff'];
480 } else {
481 # $query = $curIdEq.'&diff=0&oldid='.$rc_this_oldid;
482 $query = $curIdEq."&diff=$rc_this_oldid&oldid=$rc_last_oldid";
483 $aprops = ' tabindex="'.$baseRC->counter.'"';
484 $curLink = $this->skin->makeKnownLinkObj( $rc->getTitle(), $message['cur'], $query, '' ,'' , $aprops );
485 $diffLink = $this->skin->makeKnownLinkObj( $rc->getTitle(), $message['diff'], $query . $rcIdQuery, '' ,'' , $aprops );
486 }
487
488 # Make "last" link
489 if ( $rc_last_oldid == 0 || $rc_type == RC_LOG || $rc_type == RC_MOVE || $rc_type == RC_MOVE_OVER_REDIRECT ) {
490 $lastLink = $message['last'];
491 } else {
492 $lastLink = $this->skin->makeKnownLinkObj( $rc->getTitle(), $message['last'],
493 $curIdEq.'&diff='.$rc_this_oldid.'&oldid='.$rc_last_oldid . $rcIdQuery );
494 }
495
496 # Make user link (or user contributions for unregistered users)
497 if ( $rc_user == 0 ) {
498 $contribsPage =& Title::makeTitle( NS_SPECIAL, 'Contributions' );
499 $userLink = $this->skin->makeKnownLinkObj( $contribsPage,
500 $rc_user_text, 'target=' . $rc_user_text );
501 } else {
502 $userPage =& Title::makeTitle( NS_USER, $rc_user_text );
503 $userLink = $this->skin->makeLinkObj( $userPage, $rc_user_text );
504 }
505
506 $rc->userlink = $userLink;
507 $rc->lastlink = $lastLink;
508 $rc->curlink = $curLink;
509 $rc->difflink = $diffLink;
510
511 # Make user talk link
512 $talkname = $wgContLang->getNsText( NS_TALK ); # use the shorter name
513 $userTalkPage =& Title::makeTitle( NS_USER_TALK, $rc_user_text );
514 $userTalkLink = $this->skin->makeLinkObj( $userTalkPage, $talkname );
515
516 global $wgDisableAnonTalk;
517 if ( ( $wgSysopUserBans || 0 == $rc_user ) && $wgUser->isAllowed('block') ) {
518 $blockPage =& Title::makeTitle( NS_SPECIAL, 'Blockip' );
519 $blockLink = $this->skin->makeKnownLinkObj( $blockPage,
520 $message['blocklink'], 'ip='.$rc_user_text );
521 if( $wgDisableAnonTalk )
522 $rc->usertalklink = ' ('.$blockLink.')';
523 else
524 $rc->usertalklink = ' ('.$userTalkLink.' | '.$blockLink.')';
525 } else {
526 if( $wgDisableAnonTalk && ($rc_user == 0) )
527 $rc->usertalklink = '';
528 else
529 $rc->usertalklink = ' ('.$userTalkLink.')';
530 }
531
532 # Put accumulated information into the cache, for later display
533 # Page moves go on their own line
534 $title = $rc->getTitle();
535 $secureName = $title->getPrefixedDBkey();
536 if ( $rc_type == RC_MOVE || $rc_type == RC_MOVE_OVER_REDIRECT ) {
537 # Use an @ character to prevent collision with page names
538 $this->rc_cache['@@' . ($this->rcMoveIndex++)] = array($rc);
539 } else {
540 if ( !isset ( $this->rc_cache[$secureName] ) ) $this->rc_cache[$secureName] = array() ;
541 array_push ( $this->rc_cache[$secureName] , $rc ) ;
542 }
543 return $ret;
544 }
545
546 }
547 ?>