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;
23 $fname = 'wfSpecialRecentchanges';
25 # Get query parameters
26 $feedFormat = $wgRequest->getVal( 'feed' );
28 $defaultDays = $wgUser->getOption( 'rcdays' );
29 if ( !$defaultDays ) { $defaultDays = 3; }
31 $days = $wgRequest->getInt( 'days', $defaultDays );
32 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor' ) ) ?
1 : 0;
33 list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
35 # As a feed, use limited settings only
42 if( $limit > $wgFeedLimit ) {
43 $limit = $wgFeedLimit;
46 $from = $wgRequest->getText( 'from' );
47 $hidebots = $wgRequest->getBool( 'hidebots', true ) ?
1 : 0;
48 $hideliu = $wgRequest->getBool( 'hideliu', false ) ?
1 : 0;
49 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', false ) ?
1 : 0;
51 # Get query parameters from path
53 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
54 if( in_array( 'hidebots', $bits ) ) $hidebots = 1;
55 if( in_array( 'bots', $bits ) ) $hidebots = 0;
56 if( in_array( 'hideminor', $bits ) ) $hideminor = 1;
57 if( in_array( 'minor', $bits ) ) $hideminor = 0;
58 if( in_array( 'hideliu', $bits) ) $hideliu = 1;
59 if( in_array( 'hidepatrolled', $bits) ) $hidepatrolled = 1;
64 # Database connection and caching
65 $dbr =& wfGetDB( DB_SLAVE
);
66 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
69 # 10 seconds server-side caching max
70 $wgOut->setSquidMaxage( 10 );
72 # Get last modified date, for client caching
73 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
74 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
75 if ( $feedFormat ||
!$wgUseRCPatrol ) {
76 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
77 # Client cache fresh and headers sent, nothing more to do.
83 $rctext = wfMsgForContent( "recentchangestext" );
84 $wgOut->addWikiText( $rctext );
87 $now = wfTimestampNow();
88 $cutoff_unixtime = time() - ( $days * 86400 );
89 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
90 $cutoff = $dbr->timestamp( $cutoff_unixtime );
91 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW
,$cutoff)) {
92 $cutoff = $dbr->timestamp($from);
97 $sk = $wgUser->getSkin();
99 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
101 $hidem = ( $hideminor ) ?
'AND rc_minor=0' : '';
102 $hidem .= ( $hidebots ) ?
' AND rc_bot=0' : '';
103 $hidem .= ( $hideliu ) ?
' AND rc_user=0' : '';
104 $hidem .= ( $hidepatrolled )?
' AND rc_patrolled=0' : '';
106 $urlparams = array( 'hideminor' => $hideminor, 'hideliu' => $hideliu,
107 '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();
121 # Patch for showing "updated since last visit" marker
122 $sql2 = "SELECT $recentchanges.*" . ($uid ?
",wl_user,wl_notificationtimestamp" : "") . " FROM $recentchanges " .
123 ($uid ?
"LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
124 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
125 "ORDER BY rc_timestamp DESC LIMIT {$limit}";
127 $res = $dbr->query( $sql2, DB_SLAVE
, $fname );
129 while( $row = $dbr->fetchObject( $res ) ){
132 $dbr->freeResult( $res );
135 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $limit ),
136 $wgLang->timeanddate( $from, true ) );
138 $note = wfMsg( 'rcnote', $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
140 $wgOut->addHTML( "\n<hr />\n{$note}\n<br />" );
142 $note = rcDayLimitLinks( $days, $limit, 'Recentchanges', $hideparams, false, $minorLink, $botLink, $liuLink, $patrLink );
144 $note .= "<br />\n" . wfMsg( 'rclistfrom',
145 $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
146 $wgLang->timeanddate( $now, true ), $hideparams.'&from='.$now ) );
148 $wgOut->addHTML( $note."\n" );
151 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
154 $wgOut->setSyndicated( true );
155 $list =& new ChangesList( $sk );
156 $s = $list->beginRecentChangesList();
158 foreach( $rows as $obj ){
163 if ( ! ( $hideminor && $obj->rc_minor
) &&
164 ! ( $hidepatrolled && $obj->rc_patrolled
) ) {
165 $rc = RecentChange
::newFromRow( $obj );
166 $rc->counter
= $counter++
;
168 if ($wgShowUpdatedMarker
169 && $wgUser->getOption( 'showupdated' )
170 && !empty( $obj->wl_notificationtimestamp
)
171 && ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
)) {
172 $rc->notificationtimestamp
= true;
174 $rc->notificationtimestamp
= false;
177 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
178 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title
) ."' AND wl_namespace=$obj->rc_namespace" ;
179 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
180 $x = $dbr->fetchObject( $res3 );
181 $rc->numberofWatchingusers
= $x->n
;
183 $rc->numberofWatchingusers
= 0;
185 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
189 $s .= $list->endRecentChangesList();
190 $wgOut->addHTML( $s );
194 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
195 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
196 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
198 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
199 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
203 $timekey = "$wgDBname:rcfeed:timestamp";
204 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
206 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
207 ' [' . $wgContLanguageCode . ']';
208 $feed = new $wgFeedClasses[$feedFormat](
210 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
211 $wgTitle->getFullUrl() );
214 * Bumping around loading up diffs can be pretty slow, so where
215 * possible we want to cache the feed output so the next visitor
219 if( $feedLastmod = $messageMemc->get( $timekey ) ) {
221 * If the cached feed was rendered very recently, we may
222 * go ahead and use it even if there have been edits made
223 * since it was rendered. This keeps a swarm of requests
224 * from being too bad on a super-frequently edited wiki.
226 if( time() - wfTimestamp( TS_UNIX
, $feedLastmod )
227 < $wgFeedCacheTimeout
228 ||
wfTimestamp( TS_UNIX
, $feedLastmod )
229 > wfTimestamp( TS_UNIX
, $lastmod ) ) {
230 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
231 $cachedFeed = $messageMemc->get( $key );
233 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
236 if( is_string( $cachedFeed ) ) {
237 wfDebug( "RC: Outputting cached feed\n" );
238 $feed->httpHeaders();
241 wfDebug( "RC: rendering new feed and caching it\n" );
243 rcDoOutputFeed( $rows, $feed );
244 $cachedFeed = ob_get_contents();
247 $expire = 3600 * 24; # One day
248 $messageMemc->set( $key, $cachedFeed );
249 $messageMemc->set( $timekey, wfTimestamp( TS_MW
), $expire );
254 function rcDoOutputFeed( $rows, &$feed ) {
255 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
259 # Merge adjacent edits by one user
262 foreach( $rows as $obj ) {
264 $obj->rc_namespace
>= 0 &&
265 $obj->rc_cur_id
== $sorted[$n-1]->rc_cur_id
&&
266 $obj->rc_user_text
== $sorted[$n-1]->rc_user_text
) {
267 $sorted[$n-1]->rc_last_oldid
= $obj->rc_last_oldid
;
275 foreach( $sorted as $obj ) {
276 $title = Title
::makeTitle( $obj->rc_namespace
, $obj->rc_title
);
277 $talkpage = $title->getTalkPage();
278 $item = new FeedItem(
279 $title->getPrefixedText(),
280 rcFormatDiff( $obj ),
281 $title->getFullURL(),
284 $talkpage->getFullURL()
286 $feed->outItem( $item );
294 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
295 global $wgUser, $wgLang, $wgContLang;
296 $sk = $wgUser->getSkin();
297 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
298 ($lim ?
$wgLang->formatNum( "{$lim}" ) : wfMsg( 'all' ) ), "{$more}" .
299 ($d ?
"days={$d}&" : '') . 'limit='.$lim );
306 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
307 global $wgUser, $wgLang, $wgContLang;
308 $sk = $wgUser->getSkin();
309 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
310 ($d ?
$wgLang->formatNum( "{$d}" ) : wfMsg( "all" ) ), $more.'days='.$d .
311 ($lim ?
'&limit='.$lim : '') );
316 * Used also by Recentchangeslinked
318 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
319 $botLink = '', $liuLink = '', $patrLink = '' ) {
320 if ($more != '') $more .= '&';
321 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
322 rcCountLink( 100, $days, $page, $more ) . ' | ' .
323 rcCountLink( 250, $days, $page, $more ) . ' | ' .
324 rcCountLink( 500, $days, $page, $more ) .
325 ( $doall ?
( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
326 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
327 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
328 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
329 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
330 rcDaysLink( $limit, 30, $page, $more ) .
331 ( $doall ?
( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
332 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
333 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
338 * Format a diff for the newsfeed
340 function rcFormatDiff( $row ) {
341 $fname = 'rcFormatDiff';
342 wfProfileIn( $fname );
344 require_once( 'DifferenceEngine.php' );
345 $comment = "<p>" . htmlspecialchars( $row->rc_comment
) . "</p>\n";
347 if( $row->rc_namespace
>= 0 ) {
350 #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
351 #$diff->showDiffPage();
353 $titleObj = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
354 $dbr =& wfGetDB( DB_SLAVE
);
355 $newrev =& Revision
::newFromTitle( $titleObj, $row->rc_this_oldid
);
357 $newtext = $newrev->getText();
359 $diffText = "<p>Can't load revision $row->rc_this_oldid</p>";
360 wfProfileOut( $fname );
361 return $comment . $diffText;
364 if( $row->rc_last_oldid
) {
365 wfProfileIn( "$fname-dodiff" );
366 $oldrev =& Revision
::newFromId( $row->rc_last_oldid
);
368 $diffText = "<p>Can't load old revision $row->rc_last_oldid</p>";
369 wfProfileOut( $fname );
370 return $comment . $diffText;
372 $oldtext = $oldrev->getText();
374 global $wgFeedDiffCutoff;
375 if( strlen( $newtext ) > $wgFeedDiffCutoff ||
376 strlen( $oldtext ) > $wgFeedDiffCutoff ) {
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 );