* Follow-up r84610: don't assume a Parser object is attached
[lhc/web/wiklou.git] / includes / ChangesFeed.php
1 <?php
2
3 /**
4 * Feed to Special:RecentChanges and Special:RecentChangesLiked
5 *
6 * @ingroup Feed
7 */
8 class ChangesFeed {
9 public $format, $type, $titleMsg, $descMsg;
10
11 /**
12 * Constructor
13 *
14 * @param $format String: feed's format (either 'rss' or 'atom')
15 * @param $type String: type of feed (for cache keys)
16 */
17 public function __construct( $format, $type ) {
18 $this->format = $format;
19 $this->type = $type;
20 }
21
22 /**
23 * Get a ChannelFeed subclass object to use
24 *
25 * @param $title String: feed's title
26 * @param $description String: feed's description
27 * @param $url String: url of origin page
28 * @return ChannelFeed subclass or false on failure
29 */
30 public function getFeedObject( $title, $description, $url ) {
31 global $wgSitename, $wgLanguageCode, $wgFeedClasses;
32
33 if ( !isset( $wgFeedClasses[$this->format] ) ) {
34 return false;
35 }
36
37 $feedTitle = "$wgSitename - {$title} [$wgLanguageCode]";
38 return new $wgFeedClasses[$this->format](
39 $feedTitle, htmlspecialchars( $description ), $url );
40 }
41
42 /**
43 * Generates feed's content
44 *
45 * @param $feed ChannelFeed subclass object (generally the one returned by getFeedObject())
46 * @param $rows ResultWrapper object with rows in recentchanges table
47 * @param $lastmod Integer: timestamp of the last item in the recentchanges table (only used for the cache key)
48 * @param $opts FormOptions as in SpecialRecentChanges::getDefaultOptions()
49 * @return null or true
50 */
51 public function execute( $feed, $rows, $lastmod, $opts ) {
52 global $wgLang, $wgRenderHashAppend;
53
54 if ( !FeedUtils::checkFeedOutput( $this->format ) ) {
55 return;
56 }
57
58 $optionsHash = md5( serialize( $opts->getAllValues() ) ) . $wgRenderHashAppend;
59 $timekey = wfMemcKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash, 'timestamp' );
60 $key = wfMemcKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash );
61
62 FeedUtils::checkPurge( $timekey, $key );
63
64 /*
65 * Bumping around loading up diffs can be pretty slow, so where
66 * possible we want to cache the feed output so the next visitor
67 * gets it quick too.
68 */
69 $cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
70 if( is_string( $cachedFeed ) ) {
71 wfDebug( "RC: Outputting cached feed\n" );
72 $feed->httpHeaders();
73 echo $cachedFeed;
74 } else {
75 wfDebug( "RC: rendering new feed and caching it\n" );
76 ob_start();
77 self::generateFeed( $rows, $feed );
78 $cachedFeed = ob_get_contents();
79 ob_end_flush();
80 $this->saveToCache( $cachedFeed, $timekey, $key );
81 }
82 return true;
83 }
84
85 /**
86 * Save to feed result to $messageMemc
87 *
88 * @param $feed String: feed's content
89 * @param $timekey String: memcached key of the last modification
90 * @param $key String: memcached key of the content
91 */
92 public function saveToCache( $feed, $timekey, $key ) {
93 global $messageMemc;
94 $expire = 3600 * 24; # One day
95 $messageMemc->set( $key, $feed, $expire );
96 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
97 }
98
99 /**
100 * Try to load the feed result from $messageMemc
101 *
102 * @param $lastmod Integer: timestamp of the last item in the recentchanges table
103 * @param $timekey String: memcached key of the last modification
104 * @param $key String: memcached key of the content
105 * @return feed's content on cache hit or false on cache miss
106 */
107 public function loadFromCache( $lastmod, $timekey, $key ) {
108 global $wgFeedCacheTimeout, $wgOut, $messageMemc;
109
110 $feedLastmod = $messageMemc->get( $timekey );
111
112 if( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
113 /*
114 * If the cached feed was rendered very recently, we may
115 * go ahead and use it even if there have been edits made
116 * since it was rendered. This keeps a swarm of requests
117 * from being too bad on a super-frequently edited wiki.
118 */
119
120 $feedAge = time() - wfTimestamp( TS_UNIX, $feedLastmod );
121 $feedLastmodUnix = wfTimestamp( TS_UNIX, $feedLastmod );
122 $lastmodUnix = wfTimestamp( TS_UNIX, $lastmod );
123
124 if( $feedAge < $wgFeedCacheTimeout || $feedLastmodUnix > $lastmodUnix) {
125 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
126 if ( $feedLastmodUnix < $lastmodUnix ) {
127 $wgOut->setLastModified( $feedLastmod ); // bug 21916
128 }
129 return $messageMemc->get( $key );
130 } else {
131 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
132 }
133 }
134 return false;
135 }
136
137 /**
138 * Generate the feed items given a row from the database.
139 * @param $rows DatabaseBase resource with recentchanges rows
140 * @param $feed Feed object
141 */
142 public static function generateFeed( $rows, &$feed ) {
143 wfProfileIn( __METHOD__ );
144
145 $feed->outHeader();
146
147 # Merge adjacent edits by one user
148 $sorted = array();
149 $n = 0;
150 foreach( $rows as $obj ) {
151 if( $n > 0 &&
152 $obj->rc_namespace >= 0 &&
153 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
154 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
155 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
156 } else {
157 $sorted[$n] = $obj;
158 $n++;
159 }
160 }
161
162 foreach( $sorted as $obj ) {
163 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
164 $talkpage = $title->getTalkPage();
165 // Skip items with deleted content (avoids partially complete/inconsistent output)
166 if( $obj->rc_deleted ) continue;
167 $item = new FeedItem(
168 $title->getPrefixedText(),
169 FeedUtils::formatDiff( $obj ),
170 $obj->rc_this_oldid ? $title->getFullURL( 'diff=' . $obj->rc_this_oldid . '&oldid=prev' ) : $title->getFullURL(),
171 $obj->rc_timestamp,
172 ($obj->rc_deleted & Revision::DELETED_USER) ? wfMsgHtml('rev-deleted-user') : $obj->rc_user_text,
173 $talkpage->getFullURL()
174 );
175 $feed->outItem( $item );
176 }
177 $feed->outFooter();
178 wfProfileOut( __METHOD__ );
179 }
180
181 }