Merge "Use local context to get messages"
[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 if( !array_key_exists( $this->format, $wgFeedClasses ) ) {
38 // falling back to atom
39 $this->format = 'atom';
40 }
41
42 $feedTitle = "$wgSitename - {$title} [$wgLanguageCode]";
43 return new $wgFeedClasses[$this->format](
44 $feedTitle, htmlspecialchars( $description ), $url );
45 }
46
47 /**
48 * Generates feed's content
49 *
50 * @param $feed ChannelFeed subclass object (generally the one returned by getFeedObject())
51 * @param $rows ResultWrapper object with rows in recentchanges table
52 * @param $lastmod Integer: timestamp of the last item in the recentchanges table (only used for the cache key)
53 * @param $opts FormOptions as in SpecialRecentChanges::getDefaultOptions()
54 * @return null|bool True or null
55 */
56 public function execute( $feed, $rows, $lastmod, $opts ) {
57 global $wgLang, $wgRenderHashAppend;
58
59 if ( !FeedUtils::checkFeedOutput( $this->format ) ) {
60 return null;
61 }
62
63 $optionsHash = md5( serialize( $opts->getAllValues() ) ) . $wgRenderHashAppend;
64 $timekey = wfMemcKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash, 'timestamp' );
65 $key = wfMemcKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash );
66
67 FeedUtils::checkPurge( $timekey, $key );
68
69 /**
70 * Bumping around loading up diffs can be pretty slow, so where
71 * possible we want to cache the feed output so the next visitor
72 * gets it quick too.
73 */
74 $cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
75 if( is_string( $cachedFeed ) ) {
76 wfDebug( "RC: Outputting cached feed\n" );
77 $feed->httpHeaders();
78 echo $cachedFeed;
79 } else {
80 wfDebug( "RC: rendering new feed and caching it\n" );
81 ob_start();
82 self::generateFeed( $rows, $feed );
83 $cachedFeed = ob_get_contents();
84 ob_end_flush();
85 $this->saveToCache( $cachedFeed, $timekey, $key );
86 }
87 return true;
88 }
89
90 /**
91 * Save to feed result to $messageMemc
92 *
93 * @param $feed String: feed's content
94 * @param $timekey String: memcached key of the last modification
95 * @param $key String: memcached key of the content
96 */
97 public function saveToCache( $feed, $timekey, $key ) {
98 global $messageMemc;
99 $expire = 3600 * 24; # One day
100 $messageMemc->set( $key, $feed, $expire );
101 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
102 }
103
104 /**
105 * Try to load the feed result from $messageMemc
106 *
107 * @param $lastmod Integer: timestamp of the last item in the recentchanges table
108 * @param $timekey String: memcached key of the last modification
109 * @param $key String: memcached key of the content
110 * @return string|bool feed's content on cache hit or false on cache miss
111 */
112 public function loadFromCache( $lastmod, $timekey, $key ) {
113 global $wgFeedCacheTimeout, $wgOut, $messageMemc;
114
115 $feedLastmod = $messageMemc->get( $timekey );
116
117 if( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
118 /**
119 * If the cached feed was rendered very recently, we may
120 * go ahead and use it even if there have been edits made
121 * since it was rendered. This keeps a swarm of requests
122 * from being too bad on a super-frequently edited wiki.
123 */
124
125 $feedAge = time() - wfTimestamp( TS_UNIX, $feedLastmod );
126 $feedLastmodUnix = wfTimestamp( TS_UNIX, $feedLastmod );
127 $lastmodUnix = wfTimestamp( TS_UNIX, $lastmod );
128
129 if( $feedAge < $wgFeedCacheTimeout || $feedLastmodUnix > $lastmodUnix) {
130 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
131 if ( $feedLastmodUnix < $lastmodUnix ) {
132 $wgOut->setLastModified( $feedLastmod ); // bug 21916
133 }
134 return $messageMemc->get( $key );
135 } else {
136 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
137 }
138 }
139 return false;
140 }
141
142 /**
143 * Generate the feed items given a row from the database.
144 * @param $rows DatabaseBase resource with recentchanges rows
145 * @param $feed Feed object
146 */
147 public static function generateFeed( $rows, &$feed ) {
148 wfProfileIn( __METHOD__ );
149
150 $feed->outHeader();
151
152 # Merge adjacent edits by one user
153 $sorted = array();
154 $n = 0;
155 foreach( $rows as $obj ) {
156 if( $n > 0 &&
157 $obj->rc_type == RC_EDIT &&
158 $obj->rc_namespace >= 0 &&
159 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
160 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
161 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
162 } else {
163 $sorted[$n] = $obj;
164 $n++;
165 }
166 }
167
168 foreach( $sorted as $obj ) {
169 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
170 $talkpage = MWNamespace::canTalk( $obj->rc_namespace ) ? $title->getTalkPage()->getFullUrl() : '';
171 // Skip items with deleted content (avoids partially complete/inconsistent output)
172 if( $obj->rc_deleted ) continue;
173
174 if ( $obj->rc_this_oldid ) {
175 $url = $title->getFullURL(
176 'diff=' . $obj->rc_this_oldid .
177 '&oldid=' . $obj->rc_last_oldid
178 );
179 } else {
180 // log entry or something like that.
181 $url = $title->getFullURL();
182 }
183
184 $item = new FeedItem(
185 $title->getPrefixedText(),
186 FeedUtils::formatDiff( $obj ),
187 $url,
188 $obj->rc_timestamp,
189 ($obj->rc_deleted & Revision::DELETED_USER) ? wfMsgHtml('rev-deleted-user') : $obj->rc_user_text,
190 $talkpage
191 );
192 $feed->outItem( $item );
193 }
194 $feed->outFooter();
195 wfProfileOut( __METHOD__ );
196 }
197
198 }