5 * @subpackage SpecialPage
11 require_once( 'Feed.php' );
12 require_once( 'ChangesList.php' );
17 function wfSpecialRecentchanges( $par ) {
18 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgTitle, $wgMemc, $wgDBname;
19 global $wgRequest, $wgSitename, $wgLanguageCode, $wgContLanguageCode;
20 global $wgFeedClasses, $wgUseRCPatrol;
21 $fname = 'wfSpecialRecentchanges';
23 # Get query parameters
24 $feedFormat = $wgRequest->getVal( 'feed' );
26 $defaultDays = $wgUser->getOption( 'rcdays' );
27 if ( !$defaultDays ) { $defaultDays = 3; }
29 $days = $wgRequest->getInt( 'days', $defaultDays );
30 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor' ) ) ?
1 : 0;
31 list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
33 # As a feed, use limited settings only
40 if( $limit > $wgFeedLimit ) {
41 $limit = $wgFeedLimit;
44 $from = $wgRequest->getText( 'from' );
45 $hidebots = $wgRequest->getBool( 'hidebots', true ) ?
1 : 0;
46 $hideliu = $wgRequest->getBool( 'hideliu', false ) ?
1 : 0;
47 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', false ) ?
1 : 0;
49 # Get query parameters from path
51 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
52 if( in_array( 'hidebots', $bits ) ) $hidebots = 1;
53 if( in_array( 'bots', $bits ) ) $hidebots = 0;
54 if( in_array( 'hideminor', $bits ) ) $hideminor = 1;
55 if( in_array( 'minor', $bits ) ) $hideminor = 0;
56 if( in_array( 'hideliu', $bits) ) $hideliu = 1;
57 if( in_array( 'hidepatrolled', $bits) ) $hidepatrolled = 1;
62 # Database connection and caching
63 $dbr =& wfGetDB( DB_SLAVE
);
64 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
67 # 10 seconds server-side caching max
68 $wgOut->setSquidMaxage( 10 );
70 # Get last modified date, for client caching
71 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
72 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
73 if ( $feedFormat ||
!$wgUseRCPatrol ) {
74 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
75 # Client cache fresh and headers sent, nothing more to do.
81 $rctext = wfMsg( "recentchangestext" );
82 $wgOut->addWikiText( $rctext );
85 $now = wfTimestampNow();
86 $cutoff_unixtime = time() - ( $days * 86400 );
87 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
88 $cutoff = $dbr->timestamp( $cutoff_unixtime );
89 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW
,$cutoff)) {
90 $cutoff = $dbr->timestamp($from);
95 $sk = $wgUser->getSkin();
97 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
99 $hidem = ( $hideminor ) ?
'AND rc_minor=0' : '';
100 $hidem .= ( $hidebots ) ?
' AND rc_bot=0' : '';
101 $hidem .= ( $hideliu ) ?
' AND rc_user=0' : '';
102 $hidem .= ( $hidepatrolled )?
' AND rc_patrolled=0' : '';
104 $urlparams = array( 'hideminor' => $hideminor, 'hideliu' => $hideliu,
105 'hidebots' => $hidebots, 'hidepatrolled' => $hidepatrolled,
107 $hideparams = wfArrayToCGI( $urlparams );
109 $minorLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
110 $showhide[1-$hideminor], wfArrayToCGI( array( 'hideminor' => 1-$hideminor ), $urlparams ) );
111 $botLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
112 $showhide[1-$hidebots], wfArrayToCGI( array( 'hidebots' => 1-$hidebots ), $urlparams ) );
113 $liuLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
114 $showhide[1-$hideliu], wfArrayToCGI( array( 'hideliu' => 1-$hideliu ), $urlparams ) );
115 $patrLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
116 $showhide[1-$hidepatrolled], wfArrayToCGI( array( 'hidepatrolled' => 1-$hidepatrolled ), $urlparams ) );
118 $uid = $wgUser->getID();
119 $sql2 = "SELECT $recentchanges.*" . ($uid ?
",wl_user" : "") . " FROM $recentchanges " .
120 ($uid ?
"LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace & 65534 " : "") .
121 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
122 "ORDER BY rc_timestamp DESC LIMIT {$limit}";
124 $res = $dbr->query( $sql2, DB_SLAVE
, $fname );
126 while( $row = $dbr->fetchObject( $res ) ){
129 $dbr->freeResult( $res );
132 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $limit ),
133 $wgLang->timeanddate( $from, true ) );
135 $note = wfMsg( 'rcnote', $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
137 $wgOut->addHTML( "\n<hr />\n{$note}\n<br />" );
139 $note = rcDayLimitLinks( $days, $limit, 'Recentchanges', $hideparams, false, $minorLink, $botLink, $liuLink, $patrLink );
141 $note .= "<br />\n" . wfMsg( 'rclistfrom',
142 $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
143 $wgLang->timeanddate( $now, true ), $hideparams.'&from='.$now ) );
145 $wgOut->addHTML( $note."\n" );
148 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
151 $wgOut->setSyndicated( true );
152 $list =& new ChangesList( $sk );
153 $s = $list->beginRecentChangesList();
155 foreach( $rows as $obj ){
160 if ( ! ( $hideminor && $obj->rc_minor
) &&
161 ! ( $hidepatrolled && $obj->rc_patrolled
) ) {
162 $rc = RecentChange
::newFromRow( $obj );
163 $rc->counter
= $counter++
;
164 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
168 $s .= $list->endRecentChangesList();
169 $wgOut->addHTML( $s );
173 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
174 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
175 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
177 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
178 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
182 $timekey = "$wgDBname:rcfeed:timestamp";
183 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
185 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
186 ' [' . $wgContLanguageCode . ']';
187 $feed = new $wgFeedClasses[$feedFormat](
189 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
190 $wgTitle->getFullUrl() );
193 * Bumping around loading up diffs can be pretty slow, so where
194 * possible we want to cache the feed output so the next visitor
198 if( $feedLastmod = $messageMemc->get( $timekey ) ) {
200 * If the cached feed was rendered very recently, we may
201 * go ahead and use it even if there have been edits made
202 * since it was rendered. This keeps a swarm of requests
203 * from being too bad on a super-frequently edited wiki.
205 if( time() - wfTimestamp( TS_UNIX
, $feedLastmod )
206 < $wgFeedCacheTimeout
207 ||
wfTimestamp( TS_UNIX
, $feedLastmod )
208 > wfTimestamp( TS_UNIX
, $lastmod ) ) {
209 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
210 $cachedFeed = $messageMemc->get( $key );
212 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
215 if( is_string( $cachedFeed ) ) {
216 wfDebug( "RC: Outputting cached feed\n" );
217 $feed->httpHeaders();
220 wfDebug( "RC: rendering new feed and caching it\n" );
222 rcDoOutputFeed( $rows, $feed );
223 $cachedFeed = ob_get_contents();
226 $expire = 3600 * 24; # One day
227 $messageMemc->set( $key, $cachedFeed );
228 $messageMemc->set( $timekey, wfTimestamp( TS_MW
), $expire );
233 function rcDoOutputFeed( $rows, &$feed ) {
234 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
238 # Merge adjacent edits by one user
241 foreach( $rows as $obj ) {
243 $obj->rc_namespace
>= 0 &&
244 $obj->rc_cur_id
== $sorted[$n-1]->rc_cur_id
&&
245 $obj->rc_user_text
== $sorted[$n-1]->rc_user_text
) {
246 $sorted[$n-1]->rc_last_oldid
= $obj->rc_last_oldid
;
254 foreach( $sorted as $obj ) {
255 $title = Title
::makeTitle( $obj->rc_namespace
, $obj->rc_title
);
256 $talkpage = $title->getTalkPage();
257 $item = new FeedItem(
258 $title->getPrefixedText(),
259 rcFormatDiff( $obj ),
260 $title->getFullURL(),
263 $talkpage->getFullURL()
265 $feed->outItem( $item );
273 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
274 global $wgUser, $wgLang, $wgContLang;
275 $sk = $wgUser->getSkin();
276 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
277 ($lim ?
$wgLang->formatNum( "{$lim}" ) : wfMsg( 'all' ) ), "{$more}" .
278 ($d ?
"days={$d}&" : '') . 'limit='.$lim );
285 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
286 global $wgUser, $wgLang, $wgContLang;
287 $sk = $wgUser->getSkin();
288 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
289 ($d ?
$wgLang->formatNum( "{$d}" ) : wfMsg( "all" ) ), $more.'days='.$d .
290 ($lim ?
'&limit='.$lim : '') );
295 * Used also by Recentchangeslinked
297 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
298 $botLink = '', $liuLink = '', $patrLink = '' ) {
299 if ($more != '') $more .= '&';
300 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
301 rcCountLink( 100, $days, $page, $more ) . ' | ' .
302 rcCountLink( 250, $days, $page, $more ) . ' | ' .
303 rcCountLink( 500, $days, $page, $more ) .
304 ( $doall ?
( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
305 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
306 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
307 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
308 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
309 rcDaysLink( $limit, 30, $page, $more ) .
310 ( $doall ?
( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
311 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
312 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
317 * Format a diff for the newsfeed
319 function rcFormatDiff( $row ) {
320 $fname = 'rcFormatDiff';
321 wfProfileIn( $fname );
323 require_once( 'DifferenceEngine.php' );
324 $comment = "<p>" . htmlspecialchars( $row->rc_comment
) . "</p>\n";
326 if( $row->rc_namespace
>= 0 ) {
329 #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
330 #$diff->showDiffPage();
332 $dbr =& wfGetDB( DB_SLAVE
);
333 if( $row->rc_this_oldid
) {
334 $newrow = $dbr->selectRow( 'old',
335 array( 'old_flags', 'old_text' ),
336 array( 'old_id' => $row->rc_this_oldid
) );
337 $newtext = Article
::getRevisionText( $newrow );
339 $newrow = $dbr->selectRow( 'cur',
341 array( 'cur_id' => $row->rc_cur_id
) );
342 $newtext = $newrow->cur_text
;
344 if( $row->rc_last_oldid
) {
345 wfProfileIn( "$fname-dodiff" );
346 $oldrow = $dbr->selectRow( 'old',
347 array( 'old_flags', 'old_text' ),
348 array( 'old_id' => $row->rc_last_oldid
) );
349 $oldtext = Article
::getRevisionText( $oldrow );
351 global $wgFeedDiffCutoff;
352 if( strlen( $newtext ) > $wgFeedDiffCutoff ||
353 strlen( $oldtext ) > $wgFeedDiffCutoff ) {
354 $titleObj = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
355 $diffLink = $titleObj->escapeFullUrl(
356 'diff=' . $row->rc_this_oldid
.
357 '&oldid=' . $row->rc_last_oldid
);
358 $diffText = '<a href="' .
361 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
364 $diffText = DifferenceEngine
::getDiff( $oldtext, $newtext,
365 wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp
) ),
366 wfMsg( 'currentrev' ) );
368 wfProfileOut( "$fname-dodiff" );
370 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
371 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
374 wfProfileOut( $fname );
375 return $comment . $diffText;
378 wfProfileOut( $fname );