Revert r38221, 38238 -- "Add new parser function {{apiurl}}. Also, add new global...
[lhc/web/wiklou.git] / includes / ChangesFeed.php
1 <?php
2
3 class ChangesFeed {
4
5 public $format, $type, $titleMsg, $descMsg;
6
7 public function __construct( $format, $type ) {
8 $this->format = $format;
9 $this->type = $type;
10 }
11
12 public function getFeedObject( $title, $description ) {
13 global $wgSitename, $wgContLanguageCode, $wgFeedClasses, $wgTitle;
14 $feedTitle = "$wgSitename - {$title} [$wgContLanguageCode]";
15
16 return new $wgFeedClasses[$this->format](
17 $feedTitle, htmlspecialchars( $description ), $wgTitle->getFullUrl() );
18 }
19
20 public function execute( $feed, $rows, $limit = 0 , $hideminor = false, $lastmod = false ) {
21 global $messageMemc, $wgFeedCacheTimeout;
22 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
23
24 if ( !FeedUtils::checkFeedOutput( $this->format ) ) {
25 return;
26 }
27
28 $timekey = wfMemcKey( $this->type, $this->format, 'timestamp' );
29 $key = wfMemcKey( $this->type, $this->format, 'limit', $limit, 'minor', $hideminor );
30
31 FeedUtils::checkPurge($timekey, $key);
32
33 /*
34 * Bumping around loading up diffs can be pretty slow, so where
35 * possible we want to cache the feed output so the next visitor
36 * gets it quick too.
37 */
38 $cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
39 if( is_string( $cachedFeed ) ) {
40 wfDebug( "RC: Outputting cached feed\n" );
41 $feed->httpHeaders();
42 echo $cachedFeed;
43 } else {
44 wfDebug( "RC: rendering new feed and caching it\n" );
45 ob_start();
46 self::generateFeed( $rows, $feed );
47 $cachedFeed = ob_get_contents();
48 ob_end_flush();
49 $this->saveToCache( $cachedFeed, $timekey, $key );
50 }
51 return true;
52 }
53
54 public function saveToCache( $feed, $timekey, $key ) {
55 global $messageMemc;
56 $expire = 3600 * 24; # One day
57 $messageMemc->set( $key, $feed );
58 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
59 }
60
61 public function loadFromCache( $lastmod, $timekey, $key ) {
62 global $wgFeedCacheTimeout, $messageMemc;
63 $feedLastmod = $messageMemc->get( $timekey );
64
65 if( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
66 /*
67 * If the cached feed was rendered very recently, we may
68 * go ahead and use it even if there have been edits made
69 * since it was rendered. This keeps a swarm of requests
70 * from being too bad on a super-frequently edited wiki.
71 */
72
73 $feedAge = time() - wfTimestamp( TS_UNIX, $feedLastmod );
74 $feedLastmodUnix = wfTimestamp( TS_UNIX, $feedLastmod );
75 $lastmodUnix = wfTimestamp( TS_UNIX, $lastmod );
76
77 if( $feedAge < $wgFeedCacheTimeout || $feedLastmodUnix > $lastmodUnix) {
78 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
79 return $messageMemc->get( $key );
80 } else {
81 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
82 }
83 }
84 return false;
85 }
86
87 /**
88 * @todo document
89 * @param $rows Database resource with recentchanges rows
90 * @param $feed Feed object
91 */
92 public static function generateFeed( $rows, &$feed ) {
93 wfProfileIn( __METHOD__ );
94
95 $feed->outHeader();
96
97 # Merge adjacent edits by one user
98 $sorted = array();
99 $n = 0;
100 foreach( $rows as $obj ) {
101 if( $n > 0 &&
102 $obj->rc_namespace >= 0 &&
103 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
104 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
105 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
106 } else {
107 $sorted[$n] = $obj;
108 $n++;
109 }
110 }
111
112 foreach( $sorted as $obj ) {
113 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
114 $talkpage = $title->getTalkPage();
115 $item = new FeedItem(
116 $title->getPrefixedText(),
117 FeedUtils::formatDiff( $obj ),
118 $title->getFullURL( 'diff=' . $obj->rc_this_oldid . '&oldid=prev' ),
119 $obj->rc_timestamp,
120 ($obj->rc_deleted & Revision::DELETED_USER) ? wfMsgHtml('rev-deleted-user') : $obj->rc_user_text,
121 $talkpage->getFullURL()
122 );
123 $feed->outItem( $item );
124 }
125 $feed->outFooter();
126 wfProfileOut( __METHOD__ );
127 }
128
129 }