00001 <?php
00002
00008 class ChangesFeed {
00009 public $format, $type, $titleMsg, $descMsg;
00010
00017 public function __construct( $format, $type ) {
00018 $this->format = $format;
00019 $this->type = $type;
00020 }
00021
00029 public function getFeedObject( $title, $description ) {
00030 global $wgSitename, $wgContLanguageCode, $wgFeedClasses, $wgTitle;
00031 $feedTitle = "$wgSitename - {$title} [$wgContLanguageCode]";
00032 if( !isset($wgFeedClasses[$this->format] ) )
00033 return false;
00034 return new $wgFeedClasses[$this->format](
00035 $feedTitle, htmlspecialchars( $description ), $wgTitle->getFullUrl() );
00036 }
00037
00047 public function execute( $feed, $rows, $lastmod, $opts ) {
00048 global $messageMemc, $wgFeedCacheTimeout;
00049 global $wgSitename, $wgLang;
00050
00051 if ( !FeedUtils::checkFeedOutput( $this->format ) ) {
00052 return;
00053 }
00054
00055 $timekey = wfMemcKey( $this->type, $this->format, 'timestamp' );
00056 $optionsHash = md5( serialize( $opts->getAllValues() ) );
00057 $key = wfMemcKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash );
00058
00059 FeedUtils::checkPurge($timekey, $key);
00060
00061
00062
00063
00064
00065
00066 $cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
00067 if( is_string( $cachedFeed ) ) {
00068 wfDebug( "RC: Outputting cached feed\n" );
00069 $feed->httpHeaders();
00070 echo $cachedFeed;
00071 } else {
00072 wfDebug( "RC: rendering new feed and caching it\n" );
00073 ob_start();
00074 self::generateFeed( $rows, $feed );
00075 $cachedFeed = ob_get_contents();
00076 ob_end_flush();
00077 $this->saveToCache( $cachedFeed, $timekey, $key );
00078 }
00079 return true;
00080 }
00081
00089 public function saveToCache( $feed, $timekey, $key ) {
00090 global $messageMemc;
00091 $expire = 3600 * 24; # One day
00092 $messageMemc->set( $key, $feed, $expire );
00093 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
00094 }
00095
00104 public function loadFromCache( $lastmod, $timekey, $key ) {
00105 global $wgFeedCacheTimeout, $messageMemc;
00106 $feedLastmod = $messageMemc->get( $timekey );
00107
00108 if( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
00109
00110
00111
00112
00113
00114
00115
00116 $feedAge = time() - wfTimestamp( TS_UNIX, $feedLastmod );
00117 $feedLastmodUnix = wfTimestamp( TS_UNIX, $feedLastmod );
00118 $lastmodUnix = wfTimestamp( TS_UNIX, $lastmod );
00119
00120 if( $feedAge < $wgFeedCacheTimeout || $feedLastmodUnix > $lastmodUnix) {
00121 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
00122 return $messageMemc->get( $key );
00123 } else {
00124 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
00125 }
00126 }
00127 return false;
00128 }
00129
00135 public static function generateFeed( $rows, &$feed ) {
00136 wfProfileIn( __METHOD__ );
00137
00138 $feed->outHeader();
00139
00140 # Merge adjacent edits by one user
00141 $sorted = array();
00142 $n = 0;
00143 foreach( $rows as $obj ) {
00144 if( $n > 0 &&
00145 $obj->rc_namespace >= 0 &&
00146 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
00147 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
00148 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
00149 } else {
00150 $sorted[$n] = $obj;
00151 $n++;
00152 }
00153 }
00154
00155 foreach( $sorted as $obj ) {
00156 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
00157 $talkpage = $title->getTalkPage();
00158
00159 if( $obj->rc_deleted ) continue;
00160 $item = new FeedItem(
00161 $title->getPrefixedText(),
00162 FeedUtils::formatDiff( $obj ),
00163 $obj->rc_this_oldid ? $title->getFullURL( 'diff=' . $obj->rc_this_oldid . '&oldid=prev' ) : $title->getFullURL(),
00164 $obj->rc_timestamp,
00165 ($obj->rc_deleted & Revision::DELETED_USER) ? wfMsgHtml('rev-deleted-user') : $obj->rc_user_text,
00166 $talkpage->getFullURL()
00167 );
00168 $feed->outItem( $item );
00169 }
00170 $feed->outFooter();
00171 wfProfileOut( __METHOD__ );
00172 }
00173
00174 }