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