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' );
29 $defaultDays = $wgUser->getOption( 'rcdays' );
30 if ( !$defaultDays ) { $defaultDays = 3; }
32 $days = $wgRequest->getInt( 'days', $defaultDays );
33 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor' ) ) ?
1 : 0;
34 list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
36 # As a feed, use limited settings only
43 if( $limit > $wgFeedLimit ) {
44 $limit = $wgFeedLimit;
47 $from = $wgRequest->getText( 'from' );
48 $hidebots = $wgRequest->getBool( 'hidebots', true ) ?
1 : 0;
49 $hideliu = $wgRequest->getBool( 'hideliu', false ) ?
1 : 0;
50 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', false ) ?
1 : 0;
52 # Get query parameters from path
54 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
55 if( in_array( 'hidebots', $bits ) ) $hidebots = 1;
56 if( in_array( 'bots', $bits ) ) $hidebots = 0;
57 if( in_array( 'hideminor', $bits ) ) $hideminor = 1;
58 if( in_array( 'minor', $bits ) ) $hideminor = 0;
59 if( in_array( 'hideliu', $bits) ) $hideliu = 1;
60 if( in_array( 'hidepatrolled', $bits) ) $hidepatrolled = 1;
65 # Database connection and caching
66 $dbr =& wfGetDB( DB_SLAVE
);
67 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
70 # 10 seconds server-side caching max
71 $wgOut->setSquidMaxage( 10 );
73 # Get last modified date, for client caching
74 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
75 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
76 if ( $feedFormat ||
!$wgUseRCPatrol ) {
77 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
78 # Client cache fresh and headers sent, nothing more to do.
84 $rctext = wfMsgForContent( "recentchangestext" );
85 $wgOut->addWikiText( $rctext );
88 $now = wfTimestampNow();
89 $cutoff_unixtime = time() - ( $days * 86400 );
90 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
91 $cutoff = $dbr->timestamp( $cutoff_unixtime );
92 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW
,$cutoff)) {
93 $cutoff = $dbr->timestamp($from);
98 $sk = $wgUser->getSkin();
100 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
102 $hidem = ( $hideminor ) ?
'AND rc_minor=0' : '';
103 $hidem .= ( $hidebots ) ?
' AND rc_bot=0' : '';
104 $hidem .= ( $hideliu ) ?
' AND rc_user=0' : '';
105 $hidem .= ( $hidepatrolled )?
' AND rc_patrolled=0' : '';
107 $urlparams = array( 'hideminor' => $hideminor, 'hideliu' => $hideliu,
108 'hidebots' => $hidebots, 'hidepatrolled' => $hidepatrolled );
109 $hideparams = wfArrayToCGI( $urlparams );
111 $minorLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
112 $showhide[1-$hideminor], wfArrayToCGI( array( 'hideminor' => 1-$hideminor ), $urlparams ) );
113 $botLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
114 $showhide[1-$hidebots], wfArrayToCGI( array( 'hidebots' => 1-$hidebots ), $urlparams ) );
115 $liuLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
116 $showhide[1-$hideliu], wfArrayToCGI( array( 'hideliu' => 1-$hideliu ), $urlparams ) );
117 $patrLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
118 $showhide[1-$hidepatrolled], wfArrayToCGI( array( 'hidepatrolled' => 1-$hidepatrolled ), $urlparams ) );
120 $uid = $wgUser->getID();
123 $sql2 = "SELECT $recentchanges.*" . ($uid ?
",wl_user,wl_notificationtimestamp" : "") . " FROM $recentchanges " .
124 ($uid ?
"LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
125 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
126 "ORDER BY rc_timestamp DESC LIMIT {$limit}";
127 $res = $dbr->query( $sql2, $fname );
129 // Fetch results, prepare a batch link existence check query
131 $batch = new LinkBatch
;
132 while( $row = $dbr->fetchObject( $res ) ){
135 $title = Title
::makeTitleSafe( NS_USER
, $row->rc_user_text
);
136 $batch->addObj( $title );
139 $title = Title
::makeTitleSafe( NS_USER_TALK
, $row->rc_user_text
);
140 $batch->addObj( $title );
143 $dbr->freeResult( $res );
145 // Run existence checks
146 $batch->execute( $wgLinkCache );
149 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $limit ),
150 $wgLang->timeanddate( $from, true ) );
152 $note = wfMsg( 'rcnote', $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
154 $wgOut->addHTML( "\n<hr />\n{$note}\n<br />" );
156 $note = rcDayLimitLinks( $days, $limit, 'Recentchanges', $hideparams, false, $minorLink, $botLink, $liuLink, $patrLink );
158 $note .= "<br />\n" . wfMsg( 'rclistfrom',
159 $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
160 $wgLang->timeanddate( $now, true ), $hideparams.'&from='.$now ) );
162 $wgOut->addHTML( $note."\n" );
165 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
168 $wgOut->setSyndicated( true );
169 $list =& new ChangesList( $sk );
170 $s = $list->beginRecentChangesList();
172 foreach( $rows as $obj ){
177 if ( ! ( $hideminor && $obj->rc_minor
) &&
178 ! ( $hidepatrolled && $obj->rc_patrolled
) ) {
179 $rc = RecentChange
::newFromRow( $obj );
180 $rc->counter
= $counter++
;
182 if ($wgShowUpdatedMarker
183 && $wgUser->getOption( 'showupdated' )
184 && !empty( $obj->wl_notificationtimestamp
)
185 && ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
)) {
186 $rc->notificationtimestamp
= true;
188 $rc->notificationtimestamp
= false;
191 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
192 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title
) ."' AND wl_namespace=$obj->rc_namespace" ;
193 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
194 $x = $dbr->fetchObject( $res3 );
195 $rc->numberofWatchingusers
= $x->n
;
197 $rc->numberofWatchingusers
= 0;
199 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
203 $s .= $list->endRecentChangesList();
204 $wgOut->addHTML( $s );
208 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
209 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
210 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
212 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
213 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
217 $timekey = "$wgDBname:rcfeed:$feedFormat:timestamp";
218 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
220 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
221 ' [' . $wgContLanguageCode . ']';
222 $feed = new $wgFeedClasses[$feedFormat](
224 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
225 $wgTitle->getFullUrl() );
228 * Bumping around loading up diffs can be pretty slow, so where
229 * possible we want to cache the feed output so the next visitor
233 if( $feedLastmod = $messageMemc->get( $timekey ) ) {
235 * If the cached feed was rendered very recently, we may
236 * go ahead and use it even if there have been edits made
237 * since it was rendered. This keeps a swarm of requests
238 * from being too bad on a super-frequently edited wiki.
240 if( time() - wfTimestamp( TS_UNIX
, $feedLastmod )
241 < $wgFeedCacheTimeout
242 ||
wfTimestamp( TS_UNIX
, $feedLastmod )
243 > wfTimestamp( TS_UNIX
, $lastmod ) ) {
244 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
245 $cachedFeed = $messageMemc->get( $key );
247 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
250 if( is_string( $cachedFeed ) ) {
251 wfDebug( "RC: Outputting cached feed\n" );
252 $feed->httpHeaders();
255 wfDebug( "RC: rendering new feed and caching it\n" );
257 rcDoOutputFeed( $rows, $feed );
258 $cachedFeed = ob_get_contents();
261 $expire = 3600 * 24; # One day
262 $messageMemc->set( $key, $cachedFeed );
263 $messageMemc->set( $timekey, wfTimestamp( TS_MW
), $expire );
268 function rcDoOutputFeed( $rows, &$feed ) {
269 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
273 # Merge adjacent edits by one user
276 foreach( $rows as $obj ) {
278 $obj->rc_namespace
>= 0 &&
279 $obj->rc_cur_id
== $sorted[$n-1]->rc_cur_id
&&
280 $obj->rc_user_text
== $sorted[$n-1]->rc_user_text
) {
281 $sorted[$n-1]->rc_last_oldid
= $obj->rc_last_oldid
;
289 foreach( $sorted as $obj ) {
290 $title = Title
::makeTitle( $obj->rc_namespace
, $obj->rc_title
);
291 $talkpage = $title->getTalkPage();
292 $item = new FeedItem(
293 $title->getPrefixedText(),
294 rcFormatDiff( $obj ),
295 $title->getFullURL(),
298 $talkpage->getFullURL()
300 $feed->outItem( $item );
308 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
309 global $wgUser, $wgLang, $wgContLang;
310 $sk = $wgUser->getSkin();
311 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
312 ($lim ?
$wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
313 ($d ?
"days={$d}&" : '') . 'limit='.$lim );
320 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
321 global $wgUser, $wgLang, $wgContLang;
322 $sk = $wgUser->getSkin();
323 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
324 ($d ?
$wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
325 ($lim ?
'&limit='.$lim : '') );
330 * Used also by Recentchangeslinked
332 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
333 $botLink = '', $liuLink = '', $patrLink = '' ) {
334 if ($more != '') $more .= '&';
335 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
336 rcCountLink( 100, $days, $page, $more ) . ' | ' .
337 rcCountLink( 250, $days, $page, $more ) . ' | ' .
338 rcCountLink( 500, $days, $page, $more ) .
339 ( $doall ?
( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
340 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
341 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
342 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
343 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
344 rcDaysLink( $limit, 30, $page, $more ) .
345 ( $doall ?
( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
346 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
347 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
352 * Format a diff for the newsfeed
354 function rcFormatDiff( $row ) {
355 $fname = 'rcFormatDiff';
356 wfProfileIn( $fname );
358 require_once( 'DifferenceEngine.php' );
359 $comment = "<p>" . htmlspecialchars( $row->rc_comment
) . "</p>\n";
361 if( $row->rc_namespace
>= 0 ) {
364 #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
365 #$diff->showDiffPage();
367 $titleObj = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
368 $dbr =& wfGetDB( DB_SLAVE
);
369 $newrev =& Revision
::newFromTitle( $titleObj, $row->rc_this_oldid
);
371 $newtext = $newrev->getText();
373 $diffText = "<p>Can't load revision $row->rc_this_oldid</p>";
374 wfProfileOut( $fname );
375 return $comment . $diffText;
378 if( $row->rc_last_oldid
) {
379 wfProfileIn( "$fname-dodiff" );
380 $oldrev =& Revision
::newFromId( $row->rc_last_oldid
);
382 $diffText = "<p>Can't load old revision $row->rc_last_oldid</p>";
383 wfProfileOut( $fname );
384 return $comment . $diffText;
386 $oldtext = $oldrev->getText();
388 # Old entries may contain illegal characters
389 # which will damage output
390 $oldtext = UtfNormal
::cleanUp( $oldtext );
392 global $wgFeedDiffCutoff;
393 if( strlen( $newtext ) > $wgFeedDiffCutoff ||
394 strlen( $oldtext ) > $wgFeedDiffCutoff ) {
395 $diffLink = $titleObj->escapeFullUrl(
396 'diff=' . $row->rc_this_oldid
.
397 '&oldid=' . $row->rc_last_oldid
);
398 $diffText = '<a href="' .
401 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
404 $diffText = DifferenceEngine
::getDiff( $oldtext, $newtext,
405 wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp
) ),
406 wfMsg( 'currentrev' ) );
408 wfProfileOut( "$fname-dodiff" );
410 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
411 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
414 wfProfileOut( $fname );
415 return $comment . $diffText;
418 wfProfileOut( $fname );