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