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 global $wgRCUseModStyle, $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
22 $fname = 'wfSpecialRecentchanges';
24 # Get query parameters
25 $feedFormat = $wgRequest->getVal( 'feed' );
27 $defaultDays = $wgUser->getOption( 'rcdays' );
28 if ( !$defaultDays ) { $defaultDays = 3; }
30 $days = $wgRequest->getInt( 'days', $defaultDays );
31 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor' ) ) ?
1 : 0;
32 list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
34 # As a feed, use limited settings only
41 if( $limit > $wgFeedLimit ) {
42 $limit = $wgFeedLimit;
45 $from = $wgRequest->getText( 'from' );
46 $hidebots = $wgRequest->getBool( 'hidebots', true ) ?
1 : 0;
47 $hideliu = $wgRequest->getBool( 'hideliu', false ) ?
1 : 0;
48 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', false ) ?
1 : 0;
50 # Get query parameters from path
52 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
53 if( in_array( 'hidebots', $bits ) ) $hidebots = 1;
54 if( in_array( 'bots', $bits ) ) $hidebots = 0;
55 if( in_array( 'hideminor', $bits ) ) $hideminor = 1;
56 if( in_array( 'minor', $bits ) ) $hideminor = 0;
57 if( in_array( 'hideliu', $bits) ) $hideliu = 1;
58 if( in_array( 'hidepatrolled', $bits) ) $hidepatrolled = 1;
63 # Database connection and caching
64 $dbr =& wfGetDB( DB_SLAVE
);
65 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
68 # 10 seconds server-side caching max
69 $wgOut->setSquidMaxage( 10 );
71 # Get last modified date, for client caching
72 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
73 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
74 if ( $feedFormat ||
!$wgUseRCPatrol ) {
75 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
76 # Client cache fresh and headers sent, nothing more to do.
82 $rctext = wfMsg( "recentchangestext" );
83 $wgOut->addWikiText( $rctext );
86 $now = wfTimestampNow();
87 $cutoff_unixtime = time() - ( $days * 86400 );
88 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
89 $cutoff = $dbr->timestamp( $cutoff_unixtime );
90 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW
,$cutoff)) {
91 $cutoff = $dbr->timestamp($from);
96 $sk = $wgUser->getSkin();
98 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
100 $hidem = ( $hideminor ) ?
'AND rc_minor=0' : '';
101 $hidem .= ( $hidebots ) ?
' AND rc_bot=0' : '';
102 $hidem .= ( $hideliu ) ?
' AND rc_user=0' : '';
103 $hidem .= ( $hidepatrolled )?
' AND rc_patrolled=0' : '';
105 $urlparams = array( 'hideminor' => $hideminor, 'hideliu' => $hideliu,
106 'hidebots' => $hidebots, 'hidepatrolled' => $hidepatrolled,
108 $hideparams = wfArrayToCGI( $urlparams );
110 $minorLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
111 $showhide[1-$hideminor], wfArrayToCGI( array( 'hideminor' => 1-$hideminor ), $urlparams ) );
112 $botLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
113 $showhide[1-$hidebots], wfArrayToCGI( array( 'hidebots' => 1-$hidebots ), $urlparams ) );
114 $liuLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
115 $showhide[1-$hideliu], wfArrayToCGI( array( 'hideliu' => 1-$hideliu ), $urlparams ) );
116 $patrLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
117 $showhide[1-$hidepatrolled], wfArrayToCGI( array( 'hidepatrolled' => 1-$hidepatrolled ), $urlparams ) );
119 $RCUseModStyle = ($wgRCUseModStyle && $wgUser->getOption('rcusemodstyle')) ?
'AND rc_this_oldid=0 ' : '' ;
121 $uid = $wgUser->getID();
122 # Patch for showing "updated since last visit" marker
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} " . $RCUseModStyle .
126 "ORDER BY rc_timestamp DESC LIMIT {$limit}";
128 $res = $dbr->query( $sql2, DB_SLAVE
, $fname );
130 while( $row = $dbr->fetchObject( $res ) ){
133 $dbr->freeResult( $res );
136 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $limit ),
137 $wgLang->timeanddate( $from, true ) );
139 $note = wfMsg( 'rcnote', $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
141 $wgOut->addHTML( "\n<hr />\n{$note}\n<br />" );
143 $note = rcDayLimitLinks( $days, $limit, 'Recentchanges', $hideparams, false, $minorLink, $botLink, $liuLink, $patrLink );
145 $note .= "<br />\n" . wfMsg( 'rclistfrom',
146 $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
147 $wgLang->timeanddate( $now, true ), $hideparams.'&from='.$now ) );
149 $wgOut->addHTML( $note."\n" );
152 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
155 $wgOut->setSyndicated( true );
156 $list =& new ChangesList( $sk );
157 $s = $list->beginRecentChangesList();
159 foreach( $rows as $obj ){
164 if ( ! ( $hideminor && $obj->rc_minor
) &&
165 ! ( $hidepatrolled && $obj->rc_patrolled
) ) {
166 $rc = RecentChange
::newFromRow( $obj );
167 $rc->counter
= $counter++
;
169 if ($wgShowUpdatedMarker
170 && $wgUser->getOption( 'showupdated' )
171 && $obj->wl_notificationtimestamp
172 && ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
)) {
173 $rc->notificationtimestamp
= true;
175 $rc->notificationtimestamp
= false;
178 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
179 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title
) ."' AND wl_namespace=$obj->rc_namespace" ;
180 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
181 $x = $dbr->fetchObject( $res3 );
182 $rc->numberofWatchingusers
= $x->n
;
184 $rc->numberofWatchingusers
= 0;
186 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
190 $s .= $list->endRecentChangesList();
191 $wgOut->addHTML( $s );
195 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
196 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
197 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
199 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
200 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
204 $timekey = "$wgDBname:rcfeed:timestamp";
205 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
207 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
208 ' [' . $wgContLanguageCode . ']';
209 $feed = new $wgFeedClasses[$feedFormat](
211 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
212 $wgTitle->getFullUrl() );
215 * Bumping around loading up diffs can be pretty slow, so where
216 * possible we want to cache the feed output so the next visitor
220 if( $feedLastmod = $messageMemc->get( $timekey ) ) {
222 * If the cached feed was rendered very recently, we may
223 * go ahead and use it even if there have been edits made
224 * since it was rendered. This keeps a swarm of requests
225 * from being too bad on a super-frequently edited wiki.
227 if( time() - wfTimestamp( TS_UNIX
, $feedLastmod )
228 < $wgFeedCacheTimeout
229 ||
wfTimestamp( TS_UNIX
, $feedLastmod )
230 > wfTimestamp( TS_UNIX
, $lastmod ) ) {
231 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
232 $cachedFeed = $messageMemc->get( $key );
234 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
237 if( is_string( $cachedFeed ) ) {
238 wfDebug( "RC: Outputting cached feed\n" );
239 $feed->httpHeaders();
242 wfDebug( "RC: rendering new feed and caching it\n" );
244 rcDoOutputFeed( $rows, $feed );
245 $cachedFeed = ob_get_contents();
248 $expire = 3600 * 24; # One day
249 $messageMemc->set( $key, $cachedFeed );
250 $messageMemc->set( $timekey, wfTimestamp( TS_MW
), $expire );
255 function rcDoOutputFeed( $rows, &$feed ) {
256 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
260 # Merge adjacent edits by one user
263 foreach( $rows as $obj ) {
265 $obj->rc_namespace
>= 0 &&
266 $obj->rc_cur_id
== $sorted[$n-1]->rc_cur_id
&&
267 $obj->rc_user_text
== $sorted[$n-1]->rc_user_text
) {
268 $sorted[$n-1]->rc_last_oldid
= $obj->rc_last_oldid
;
276 foreach( $sorted as $obj ) {
277 $title = Title
::makeTitle( $obj->rc_namespace
, $obj->rc_title
);
278 $talkpage = $title->getTalkPage();
279 $item = new FeedItem(
280 $title->getPrefixedText(),
281 rcFormatDiff( $obj ),
282 $title->getFullURL(),
285 $talkpage->getFullURL()
287 $feed->outItem( $item );
295 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
296 global $wgUser, $wgLang, $wgContLang;
297 $sk = $wgUser->getSkin();
298 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
299 ($lim ?
$wgLang->formatNum( "{$lim}" ) : wfMsg( 'all' ) ), "{$more}" .
300 ($d ?
"days={$d}&" : '') . 'limit='.$lim );
307 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
308 global $wgUser, $wgLang, $wgContLang;
309 $sk = $wgUser->getSkin();
310 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
311 ($d ?
$wgLang->formatNum( "{$d}" ) : wfMsg( "all" ) ), $more.'days='.$d .
312 ($lim ?
'&limit='.$lim : '') );
317 * Used also by Recentchangeslinked
319 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
320 $botLink = '', $liuLink = '', $patrLink = '' ) {
321 if ($more != '') $more .= '&';
322 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
323 rcCountLink( 100, $days, $page, $more ) . ' | ' .
324 rcCountLink( 250, $days, $page, $more ) . ' | ' .
325 rcCountLink( 500, $days, $page, $more ) .
326 ( $doall ?
( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
327 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
328 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
329 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
330 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
331 rcDaysLink( $limit, 30, $page, $more ) .
332 ( $doall ?
( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
333 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
334 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
339 * Format a diff for the newsfeed
341 function rcFormatDiff( $row ) {
342 $fname = 'rcFormatDiff';
343 wfProfileIn( $fname );
345 require_once( 'DifferenceEngine.php' );
346 $comment = "<p>" . htmlspecialchars( $row->rc_comment
) . "</p>\n";
348 if( $row->rc_namespace
>= 0 ) {
351 #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
352 #$diff->showDiffPage();
354 $dbr =& wfGetDB( DB_SLAVE
);
355 if( $row->rc_this_oldid
) {
356 $newrow = $dbr->selectRow( 'old',
357 array( 'old_flags', 'old_text' ),
358 array( 'old_id' => $row->rc_this_oldid
) );
359 $newtext = Article
::getRevisionText( $newrow );
361 $newrow = $dbr->selectRow( 'cur',
363 array( 'cur_id' => $row->rc_cur_id
) );
364 $newtext = $newrow->cur_text
;
366 if( $row->rc_last_oldid
) {
367 wfProfileIn( "$fname-dodiff" );
368 $oldrow = $dbr->selectRow( 'old',
369 array( 'old_flags', 'old_text' ),
370 array( 'old_id' => $row->rc_last_oldid
) );
371 $oldtext = Article
::getRevisionText( $oldrow );
373 global $wgFeedDiffCutoff;
374 if( strlen( $newtext ) > $wgFeedDiffCutoff ||
375 strlen( $oldtext ) > $wgFeedDiffCutoff ) {
376 $titleObj = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
377 $diffLink = $titleObj->escapeFullUrl(
378 'diff=' . $row->rc_this_oldid
.
379 '&oldid=' . $row->rc_last_oldid
);
380 $diffText = '<a href="' .
383 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
386 $diffText = DifferenceEngine
::getDiff( $oldtext, $newtext,
387 wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp
) ),
388 wfMsg( 'currentrev' ) );
390 wfProfileOut( "$fname-dodiff" );
392 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
393 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
396 wfProfileOut( $fname );
397 return $comment . $diffText;
400 wfProfileOut( $fname );