ChangesFeed::getFeedObject()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 3
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Feed for list of changes.
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License along
16
 * with this program; if not, write to the Free Software Foundation, Inc.,
17
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
 * http://www.gnu.org/copyleft/gpl.html
19
 *
20
 * @file
21
 */
22
23
/**
24
 * Feed to Special:RecentChanges and Special:RecentChangesLiked
25
 *
26
 * @ingroup Feed
27
 */
28
class ChangesFeed {
29
	public $format, $type, $titleMsg, $descMsg;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

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