5 * @subpackage SpecialPage
11 require_once( 'Feed.php' );
12 require_once( 'ChangesList.php' );
13 require_once( 'Revision.php' );
18 function wfSpecialRecentchanges( $par ) {
19 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgTitle, $wgMemc, $wgDBname;
20 global $wgRequest, $wgSitename, $wgLanguageCode, $wgContLanguageCode;
21 global $wgFeedClasses, $wgUseRCPatrol;
22 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
24 $fname = 'wfSpecialRecentchanges';
26 # Get query parameters
27 $feedFormat = $wgRequest->getVal( 'feed' );
30 /* int */ 'days' => $wgUser->getDefaultOption('rcdays'),
31 /* int */ 'limit' => $wgUser->getDefaultOption('rclimit'),
32 /* bool */ 'hideminor' => false,
33 /* bool */ 'hidebots' => true,
34 /* bool */ 'hideliu' => false,
35 /* bool */ 'hidepatrolled' => false,
36 /* text */ 'from' => '',
37 /* text */ 'namespace' => '',
38 /* bool */ 'invert' => false,
44 $days = $wgUser->getOption( 'rcdays' );
45 if ( !$days ) { $days = $defaults['days']; }
47 $limit = $wgUser->getOption( 'rclimit' );
48 if ( !$limit ) { $limit = $defaults['limit']; }
51 $days = $wgRequest->getInt( 'days', $defaults['days'] );
53 # list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
54 $limit = $wgRequest->getInt( 'limit', $defaults['limit'] );
55 if ( $limit < 0 ||
$limit > 5000 ) $limit = $defaults['limit'];
57 /* order of selection: url > preferences > default */
58 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor') ?
true : $defaults['hideminor'] );
61 # As a feed, use limited settings only
64 if( $limit > $wgFeedLimit ) {
65 $options['limit'] = $wgFeedLimit;
70 $namespace = $wgRequest->getVal( 'namespace', $defaults['namespace'] );
71 $invert = $wgRequest->getBool( 'invert', $defaults['invert'] );
72 $hidebots = $wgRequest->getBool( 'hidebots', $defaults['hidebots'] );
73 $hideliu = $wgRequest->getBool( 'hideliu', $defaults['hideliu'] );
74 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', $defaults['hidepatrolled'] );
75 $from = $wgRequest->getVal( 'from', $defaults['from'] );
77 # Get query parameters from path
79 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
80 if( in_array( 'hidebots', $bits ) ) $hidebots = 1;
81 if( in_array( 'bots', $bits ) ) $hidebots = 0;
82 if( in_array( 'hideminor', $bits ) ) $hideminor = 1;
83 if( in_array( 'minor', $bits ) ) $hideminor = 0;
84 if( in_array( 'hideliu', $bits) ) $hideliu = 1;
85 if( in_array( 'hidepatrolled', $bits) ) $hidepatrolled = 1;
90 # Database connection and caching
91 $dbr =& wfGetDB( DB_SLAVE
);
92 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
95 $cutoff_unixtime = time() - ( $days * 86400 );
96 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
97 $cutoff = $dbr->timestamp( $cutoff_unixtime );
98 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW
,$cutoff)) {
99 $cutoff = $dbr->timestamp($from);
101 $from = $defaults['from'];
104 # 10 seconds server-side caching max
105 $wgOut->setSquidMaxage( 10 );
107 # Get last modified date, for client caching
108 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
109 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
110 if ( $feedFormat ||
!$wgUseRCPatrol ) {
111 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
112 # Client cache fresh and headers sent, nothing more to do.
117 $hidem = $hideminor ?
'AND rc_minor=0' : '';
118 $hidem .= $hidebots ?
' AND rc_bot=0' : '';
119 $hidem .= $hideliu ?
' AND rc_user=0' : '';
120 $hidem .= $hidepatrolled ?
' AND rc_patrolled=0' : '';
121 $hidem .= $namespace === '' ?
'' :' AND rc_namespace' . ($invert ?
'!=' : '=') . $namespace;
123 // This is the big thing!
125 $uid = $wgUser->getID();
128 $sql2 = "SELECT *" . ($uid ?
",wl_user,wl_notificationtimestamp" : "") . " FROM $recentchanges " .
129 ($uid ?
"LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
130 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
131 "ORDER BY rc_timestamp DESC LIMIT {$limit}";
132 $res = $dbr->query( $sql2, $fname );
134 // Fetch results, prepare a batch link existence check query
136 $batch = new LinkBatch
;
137 while( $row = $dbr->fetchObject( $res ) ){
140 $title = Title
::makeTitleSafe( NS_USER
, $row->rc_user_text
);
141 $batch->addObj( $title );
144 $title = Title
::makeTitleSafe( NS_USER_TALK
, $row->rc_user_text
);
145 $batch->addObj( $title );
148 $dbr->freeResult( $res );
150 // Run existence checks
151 $batch->execute( $wgLinkCache );
154 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
160 $wgOut->addWikiText( wfMsgForContent( "recentchangestext" ) );
162 // Dump everything here
163 $nondefaults = array();
165 appendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
166 appendToArrayIfNotDefault( 'limit', $limit , $defaults, $nondefaults);
167 appendToArrayIfNotDefault( 'hideminor', $hideminor, $defaults, $nondefaults);
168 appendToArrayIfNotDefault( 'hidebots', $hidebots, $defaults, $nondefaults);
169 appendToArrayIfNotDefault( 'hideliu', $hideliu, $defaults, $nondefaults);
170 appendToArrayIfNotDefault( 'hidepatrolled', $hidepatrolled, $defaults, $nondefaults);
171 appendToArrayIfNotDefault( 'from', $from, $defaults, $nondefaults);
172 appendToArrayIfNotDefault( 'namespace', $namespace, $defaults, $nondefaults);
173 appendToArrayIfNotDefault( 'invert', $invert, $defaults, $nondefaults);
175 // Add end of the texts
176 $wgOut->addHTML( '<div class="rcoptions">' . rcOptionsPanel( $defaults, $nondefaults ) );
177 $wgOut->addHTML( namespaceForm( $namespace, $invert, $nondefaults) . '</div>');
179 // And now for the content
180 $sk = $wgUser->getSkin();
181 $wgOut->setSyndicated( true );
182 $list =& new ChangesList( $sk );
183 $s = $list->beginRecentChangesList();
185 foreach( $rows as $obj ){
190 if ( ! ( $hideminor && $obj->rc_minor
) &&
191 ! ( $hidepatrolled && $obj->rc_patrolled
) ) {
192 $rc = RecentChange
::newFromRow( $obj );
193 $rc->counter
= $counter++
;
195 if ($wgShowUpdatedMarker
196 && $wgUser->getOption( 'showupdated' )
197 && !empty( $obj->wl_notificationtimestamp
)
198 && ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
)) {
199 $rc->notificationtimestamp
= true;
201 $rc->notificationtimestamp
= false;
204 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
205 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title
) ."' AND wl_namespace=$obj->rc_namespace" ;
206 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
207 $x = $dbr->fetchObject( $res3 );
208 $rc->numberofWatchingusers
= $x->n
;
210 $rc->numberofWatchingusers
= 0;
212 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
216 $s .= $list->endRecentChangesList();
217 $wgOut->addHTML( $s );
221 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
222 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
223 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
225 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
226 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
230 $timekey = "$wgDBname:rcfeed:$feedFormat:timestamp";
231 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
233 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
234 ' [' . $wgContLanguageCode . ']';
235 $feed = new $wgFeedClasses[$feedFormat](
237 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
238 $wgTitle->getFullUrl() );
241 * Bumping around loading up diffs can be pretty slow, so where
242 * possible we want to cache the feed output so the next visitor
246 if( $feedLastmod = $messageMemc->get( $timekey ) ) {
248 * If the cached feed was rendered very recently, we may
249 * go ahead and use it even if there have been edits made
250 * since it was rendered. This keeps a swarm of requests
251 * from being too bad on a super-frequently edited wiki.
253 if( time() - wfTimestamp( TS_UNIX
, $feedLastmod )
254 < $wgFeedCacheTimeout
255 ||
wfTimestamp( TS_UNIX
, $feedLastmod )
256 > wfTimestamp( TS_UNIX
, $lastmod ) ) {
257 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
258 $cachedFeed = $messageMemc->get( $key );
260 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
263 if( is_string( $cachedFeed ) ) {
264 wfDebug( "RC: Outputting cached feed\n" );
265 $feed->httpHeaders();
268 wfDebug( "RC: rendering new feed and caching it\n" );
270 rcDoOutputFeed( $rows, $feed );
271 $cachedFeed = ob_get_contents();
274 $expire = 3600 * 24; # One day
275 $messageMemc->set( $key, $cachedFeed );
276 $messageMemc->set( $timekey, wfTimestamp( TS_MW
), $expire );
281 function rcDoOutputFeed( $rows, &$feed ) {
282 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
286 # Merge adjacent edits by one user
289 foreach( $rows as $obj ) {
291 $obj->rc_namespace
>= 0 &&
292 $obj->rc_cur_id
== $sorted[$n-1]->rc_cur_id
&&
293 $obj->rc_user_text
== $sorted[$n-1]->rc_user_text
) {
294 $sorted[$n-1]->rc_last_oldid
= $obj->rc_last_oldid
;
302 foreach( $sorted as $obj ) {
303 $title = Title
::makeTitle( $obj->rc_namespace
, $obj->rc_title
);
304 $talkpage = $title->getTalkPage();
305 $item = new FeedItem(
306 $title->getPrefixedText(),
307 rcFormatDiff( $obj ),
308 $title->getFullURL(),
311 $talkpage->getFullURL()
313 $feed->outItem( $item );
321 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
322 global $wgUser, $wgLang, $wgContLang;
323 $sk = $wgUser->getSkin();
324 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
325 ($lim ?
$wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
326 ($d ?
"days={$d}&" : '') . 'limit='.$lim );
333 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
334 global $wgUser, $wgLang, $wgContLang;
335 $sk = $wgUser->getSkin();
336 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
337 ($d ?
$wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
338 ($lim ?
'&limit='.$lim : '') );
343 * Used by Recentchangeslinked
345 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
346 $botLink = '', $liuLink = '', $patrLink = '' ) {
347 if ($more != '') $more .= '&';
348 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
349 rcCountLink( 100, $days, $page, $more ) . ' | ' .
350 rcCountLink( 250, $days, $page, $more ) . ' | ' .
351 rcCountLink( 500, $days, $page, $more ) .
352 ( $doall ?
( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
353 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
354 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
355 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
356 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
357 rcDaysLink( $limit, 30, $page, $more ) .
358 ( $doall ?
( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
359 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
360 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
366 * Makes change an option link which carries all the other options
368 function makeOptionsLink( $title, $override, $options ) {
369 global $wgUser, $wgLang, $wgContLang;
370 $sk = $wgUser->getSkin();
371 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
372 $title, wfArrayToCGI( $override, $options ) );
376 * Creates the options panel
378 function rcOptionsPanel( $defaults, $nondefaults ) {
381 $options = $nondefaults +
$defaults;
383 if( $options['from'] )
384 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $options['limit'] ), $wgLang->timeanddate( $options['from'], true ) );
386 $note = wfMsg( 'rcnote', $wgLang->formatNum( $options['limit'] ), $wgLang->formatNum( $options['days'] ) );
390 $options_limit = array(50, 100, 250, 500);
392 while ( $i+
1 < count($options_limit) ) {
393 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $nondefaults) . ' | ' ;
396 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $nondefaults) ;
398 // day links, reset 'from' to none
400 $options_days = array(1, 3, 7, 14, 30);
402 while ( $i+
1 < count($options_days) ) {
403 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $nondefaults) . ' | ' ;
406 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $nondefaults) ;
409 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
410 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
411 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
412 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
413 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
414 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
415 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
416 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
417 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
419 $hl = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
421 // show from this onward link
422 $now = $wgLang->timeanddate( wfTimestampNow(), true );
423 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
425 $rclinks = wfMsg( 'rclinks', $cl, $dl, $hl );
426 $rclistfrom = wfMsg( 'rclistfrom', $tl );
427 return "$note<br />$rclinks<br />$rclistfrom";
432 * Creates the choose namespace selection
436 * @param mixed $namespace The key of the currently selected namespace, empty string
438 * @param bool $invert Whether to invert the namespace selection
439 * @param array $nondefaults An array of non default options to be remembered
443 function namespaceForm ( $namespace, $invert, $nondefaults ) {
444 global $wgContLang, $wgScript;
445 $t = Title
::makeTitle( NS_SPECIAL
, 'Recentchanges' );
447 $namespaceselect = "<select name='namespace' id='nsselectbox'>";
448 $namespaceselect .= '<option value="" ' . ($namespace === '' ?
' selected="selected"' : '') . '>' . wfMsg( 'contributionsall' ) . '</option>';
449 $arr = $wgContLang->getFormattedNamespaces();
450 foreach ( $arr as $ns => $name ) {
453 $n = $ns === NS_MAIN ?
wfMsg ( 'blanknamespace' ) : $name;
454 $sel = $namespace === (string) $ns ?
' selected="selected"' : '';
455 $namespaceselect .= "<option value='$ns'$sel>$n</option>";
457 $namespaceselect .= '</select>';
460 $out .= "<div class='namespaceselector'><form method='get' action='{$wgScript}'>\n";
461 foreach ( $nondefaults as $key => $value ) {
462 if ($key != 'namespace' && $key != 'invert')
463 $out .= "<input type='hidden' name='$key' value='$value' />";
466 $submitbutton = '<input type="submit" value="' . wfMsg( 'allpagessubmit' ) . '" />';
467 $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ?
' checked="checked"' : '' ) . ' />';
470 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
472 <table id='nsselect' class='recentchanges'>
474 <td align='right'><label for='nsselectbox'>" . wfMsg('namespace') . "</label></td>
475 <td align='left'>$namespaceselect $submitbutton $invertbox <label for='nsinvert'>" . wfMsg('invert') . "</label></td>
478 $out .= '</form></div>';
484 * Format a diff for the newsfeed
486 function rcFormatDiff( $row ) {
487 $fname = 'rcFormatDiff';
488 wfProfileIn( $fname );
490 require_once( 'DifferenceEngine.php' );
491 $comment = "<p>" . htmlspecialchars( $row->rc_comment
) . "</p>\n";
493 if( $row->rc_namespace
>= 0 ) {
496 #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
497 #$diff->showDiffPage();
499 $titleObj = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
500 $dbr =& wfGetDB( DB_SLAVE
);
501 $newrev =& Revision
::newFromTitle( $titleObj, $row->rc_this_oldid
);
503 $newtext = $newrev->getText();
505 $diffText = "<p>Can't load revision $row->rc_this_oldid</p>";
506 wfProfileOut( $fname );
507 return $comment . $diffText;
510 if( $row->rc_last_oldid
) {
511 wfProfileIn( "$fname-dodiff" );
512 $oldrev =& Revision
::newFromId( $row->rc_last_oldid
);
514 $diffText = "<p>Can't load old revision $row->rc_last_oldid</p>";
515 wfProfileOut( $fname );
516 return $comment . $diffText;
518 $oldtext = $oldrev->getText();
520 # Old entries may contain illegal characters
521 # which will damage output
522 $oldtext = UtfNormal
::cleanUp( $oldtext );
524 global $wgFeedDiffCutoff;
525 if( strlen( $newtext ) > $wgFeedDiffCutoff ||
526 strlen( $oldtext ) > $wgFeedDiffCutoff ) {
527 $diffLink = $titleObj->escapeFullUrl(
528 'diff=' . $row->rc_this_oldid
.
529 '&oldid=' . $row->rc_last_oldid
);
530 $diffText = '<a href="' .
533 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
536 $diffText = DifferenceEngine
::getDiff( $oldtext, $newtext,
537 wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp
) ),
538 wfMsg( 'currentrev' ) );
540 wfProfileOut( "$fname-dodiff" );
542 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
543 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
546 wfProfileOut( $fname );
547 return $comment . $diffText;
550 wfProfileOut( $fname );
556 * Appends to second array if $value differs from that in $default
558 function appendToArrayIfNotDefault( $key, $value, $default, &$changed )
560 if ( is_null( $changed ) ) {
563 if ( $default[$key] !== $value ) {
564 $changed[$key] = $value;