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