+function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
+ global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
+ global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
+
+ if( !isset( $wgFeedClasses[$feedFormat] ) ) {
+ wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
+ return false;
+ }
+
+ $timekey = "$wgDBname:rcfeed:timestamp";
+ $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
+
+ $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
+ ' [' . $wgContLanguageCode . ']';
+ $feed = new $wgFeedClasses[$feedFormat](
+ $feedTitle,
+ htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
+ $wgTitle->getFullUrl() );
+
+ /**
+ * Bumping around loading up diffs can be pretty slow, so where
+ * possible we want to cache the feed output so the next visitor
+ * gets it quick too.
+ */
+ $cachedFeed = false;
+ if( $feedLastmod = $messageMemc->get( $timekey ) ) {
+ /**
+ * If the cached feed was rendered very recently, we may
+ * go ahead and use it even if there have been edits made
+ * since it was rendered. This keeps a swarm of requests
+ * from being too bad on a super-frequently edited wiki.
+ */
+ if( time() - wfTimestamp( TS_UNIX, $feedLastmod )
+ < $wgFeedCacheTimeout
+ || wfTimestamp( TS_UNIX, $feedLastmod )
+ > wfTimestamp( TS_UNIX, $lastmod ) ) {
+ wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
+ $cachedFeed = $messageMemc->get( $key );
+ } else {
+ wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
+ }
+ }
+ if( is_string( $cachedFeed ) ) {
+ wfDebug( "RC: Outputting cached feed\n" );
+ $feed->httpHeaders();
+ echo $cachedFeed;
+ } else {
+ wfDebug( "RC: rendering new feed and caching it\n" );
+ ob_start();
+ rcDoOutputFeed( $rows, $feed );
+ $cachedFeed = ob_get_contents();
+ ob_end_flush();
+
+ $expire = 3600 * 24; # One day
+ $messageMemc->set( $key, $cachedFeed );
+ $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
+ }
+ return true;
+}
+
+function rcDoOutputFeed( $rows, &$feed ) {
+ global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
+
+ $feed->outHeader();
+
+ # Merge adjacent edits by one user
+ $sorted = array();
+ $n = 0;
+ foreach( $rows as $obj ) {
+ if( $n > 0 &&
+ $obj->rc_namespace >= 0 &&
+ $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
+ $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
+ $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
+ } else {
+ $sorted[$n] = $obj;
+ $n++;
+ }
+ $first = false;
+ }
+
+ foreach( $sorted as $obj ) {
+ $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
+ $talkpage = $title->getTalkPage();
+ $item = new FeedItem(
+ $title->getPrefixedText(),
+ rcFormatDiff( $obj ),
+ $title->getFullURL(),
+ $obj->rc_timestamp,
+ $obj->rc_user_text,
+ $talkpage->getFullURL()
+ );
+ $feed->outItem( $item );
+ }
+ $feed->outFooter();
+}
+