Load limiting work for Recentchanges RSS/Atom feeds:
[lhc/web/wiklou.git] / includes / SpecialRecentchanges.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 *
10 */
11 require_once( 'Feed.php' );
12 require_once( 'ChangesList.php' );
13
14 /**
15 * Constructor
16 */
17 function wfSpecialRecentchanges( $par ) {
18 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgTitle, $wgMemc, $wgDBname;
19 global $wgRequest, $wgSitename, $wgLanguageCode, $wgContLanguageCode;
20 global $wgFeedClasses, $wgUseRCPatrol;
21 $fname = 'wfSpecialRecentchanges';
22
23 # Get query parameters
24 $feedFormat = $wgRequest->getVal( 'feed' );
25
26 $defaultDays = $wgUser->getOption( 'rcdays' );
27 if ( !$defaultDays ) { $defaultDays = 3; }
28
29 $days = $wgRequest->getInt( 'days', $defaultDays );
30 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor' ) ) ? 1 : 0;
31 list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
32
33 # As a feed, use limited settings only
34 if( $feedFormat ) {
35 $from = null;
36 $hidebots = 1;
37 $hideliu = 0;
38 $hidepatrolled = 0;
39 global $wgFeedLimit;
40 if( $limit > $wgFeedLimit ) {
41 $limit = $wgFeedLimit;
42 }
43 } else {
44 $from = $wgRequest->getText( 'from' );
45 $hidebots = $wgRequest->getBool( 'hidebots', true ) ? 1 : 0;
46 $hideliu = $wgRequest->getBool( 'hideliu', false ) ? 1 : 0;
47 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', false ) ? 1 : 0;
48
49 # Get query parameters from path
50 if( $par ) {
51 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
52 if( in_array( 'hidebots', $bits ) ) $hidebots = 1;
53 if( in_array( 'bots', $bits ) ) $hidebots = 0;
54 if( in_array( 'hideminor', $bits ) ) $hideminor = 1;
55 if( in_array( 'minor', $bits ) ) $hideminor = 0;
56 if( in_array( 'hideliu', $bits) ) $hideliu = 1;
57 if( in_array( 'hidepatrolled', $bits) ) $hidepatrolled = 1;
58 }
59 }
60
61
62 # Database connection and caching
63 $dbr =& wfGetDB( DB_SLAVE );
64 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
65
66
67 # 10 seconds server-side caching max
68 $wgOut->setSquidMaxage( 10 );
69
70 # Get last modified date, for client caching
71 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
72 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
73 if ( $feedFormat || !$wgUseRCPatrol ) {
74 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
75 # Client cache fresh and headers sent, nothing more to do.
76 return;
77 }
78 }
79
80 # Output header
81 $rctext = wfMsg( "recentchangestext" );
82 $wgOut->addWikiText( $rctext );
83
84
85 $now = wfTimestampNow();
86 $cutoff_unixtime = time() - ( $days * 86400 );
87 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime % 86400);
88 $cutoff = $dbr->timestamp( $cutoff_unixtime );
89 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW,$cutoff)) {
90 $cutoff = $dbr->timestamp($from);
91 } else {
92 unset($from);
93 }
94
95 $sk = $wgUser->getSkin();
96
97 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
98
99 $hidem = ( $hideminor ) ? 'AND rc_minor=0' : '';
100 $hidem .= ( $hidebots ) ? ' AND rc_bot=0' : '';
101 $hidem .= ( $hideliu ) ? ' AND rc_user=0' : '';
102 $hidem .= ( $hidepatrolled )? ' AND rc_patrolled=0' : '';
103
104 $urlparams = array( 'hideminor' => $hideminor, 'hideliu' => $hideliu,
105 'hidebots' => $hidebots, 'hidepatrolled' => $hidepatrolled,
106 'limit' => $limit );
107 $hideparams = wfArrayToCGI( $urlparams );
108
109 $minorLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
110 $showhide[1-$hideminor], wfArrayToCGI( array( 'hideminor' => 1-$hideminor ), $urlparams ) );
111 $botLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
112 $showhide[1-$hidebots], wfArrayToCGI( array( 'hidebots' => 1-$hidebots ), $urlparams ) );
113 $liuLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
114 $showhide[1-$hideliu], wfArrayToCGI( array( 'hideliu' => 1-$hideliu ), $urlparams ) );
115 $patrLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
116 $showhide[1-$hidepatrolled], wfArrayToCGI( array( 'hidepatrolled' => 1-$hidepatrolled ), $urlparams ) );
117
118 $uid = $wgUser->getID();
119 $sql2 = "SELECT $recentchanges.*" . ($uid ? ",wl_user" : "") . " FROM $recentchanges " .
120 ($uid ? "LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace & 65534 " : "") .
121 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
122 "ORDER BY rc_timestamp DESC LIMIT {$limit}";
123
124 $res = $dbr->query( $sql2, DB_SLAVE, $fname );
125 $rows = array();
126 while( $row = $dbr->fetchObject( $res ) ){
127 $rows[] = $row;
128 }
129 $dbr->freeResult( $res );
130
131 if(isset($from)) {
132 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $limit ),
133 $wgLang->timeanddate( $from, true ) );
134 } else {
135 $note = wfMsg( 'rcnote', $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
136 }
137 $wgOut->addHTML( "\n<hr />\n{$note}\n<br />" );
138
139 $note = rcDayLimitLinks( $days, $limit, 'Recentchanges', $hideparams, false, $minorLink, $botLink, $liuLink, $patrLink );
140
141 $note .= "<br />\n" . wfMsg( 'rclistfrom',
142 $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
143 $wgLang->timeanddate( $now, true ), $hideparams.'&from='.$now ) );
144
145 $wgOut->addHTML( $note."\n" );
146
147 if( $feedFormat ) {
148 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
149 } else {
150 # Web output...
151 $wgOut->setSyndicated( true );
152 $list =& new ChangesList( $sk );
153 $s = $list->beginRecentChangesList();
154 $counter = 1;
155 foreach( $rows as $obj ){
156 if( $limit == 0) {
157 break;
158 }
159
160 if ( ! ( $hideminor && $obj->rc_minor ) &&
161 ! ( $hidepatrolled && $obj->rc_patrolled ) ) {
162 $rc = RecentChange::newFromRow( $obj );
163 $rc->counter = $counter++;
164 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ) );
165 --$limit;
166 }
167 }
168 $s .= $list->endRecentChangesList();
169 $wgOut->addHTML( $s );
170 }
171 }
172
173 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
174 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
175 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
176
177 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
178 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
179 return false;
180 }
181
182 $timekey = "$wgDBname:rcfeed:timestamp";
183 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
184
185 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
186 ' [' . $wgContLanguageCode . ']';
187 $feed = new $wgFeedClasses[$feedFormat](
188 $feedTitle,
189 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
190 $wgTitle->getFullUrl() );
191
192 /**
193 * Bumping around loading up diffs can be pretty slow, so where
194 * possible we want to cache the feed output so the next visitor
195 * gets it quick too.
196 */
197 $cachedFeed = false;
198 if( $feedLastmod = $messageMemc->get( $timekey ) ) {
199 /**
200 * If the cached feed was rendered very recently, we may
201 * go ahead and use it even if there have been edits made
202 * since it was rendered. This keeps a swarm of requests
203 * from being too bad on a super-frequently edited wiki.
204 */
205 if( time() - wfTimestamp( TS_UNIX, $feedLastmod )
206 < $wgFeedCacheTimeout
207 || wfTimestamp( TS_UNIX, $feedLastmod )
208 > wfTimestamp( TS_UNIX, $lastmod ) ) {
209 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
210 $cachedFeed = $messageMemc->get( $key );
211 } else {
212 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
213 }
214 }
215 if( is_string( $cachedFeed ) ) {
216 wfDebug( "RC: Outputting cached feed\n" );
217 $feed->httpHeaders();
218 echo $cachedFeed;
219 } else {
220 wfDebug( "RC: rendering new feed and caching it\n" );
221 ob_start();
222 rcDoOutputFeed( $rows, $feed );
223 $cachedFeed = ob_get_contents();
224 ob_end_flush();
225
226 $expire = 3600 * 24; # One day
227 $messageMemc->set( $key, $cachedFeed );
228 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
229 }
230 return true;
231 }
232
233 function rcDoOutputFeed( $rows, &$feed ) {
234 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
235
236 $feed->outHeader();
237
238 # Merge adjacent edits by one user
239 $sorted = array();
240 $n = 0;
241 foreach( $rows as $obj ) {
242 if( $n > 0 &&
243 $obj->rc_namespace >= 0 &&
244 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
245 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
246 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
247 } else {
248 $sorted[$n] = $obj;
249 $n++;
250 }
251 $first = false;
252 }
253
254 foreach( $sorted as $obj ) {
255 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
256 $talkpage = $title->getTalkPage();
257 $item = new FeedItem(
258 $title->getPrefixedText(),
259 rcFormatDiff( $obj ),
260 $title->getFullURL(),
261 $obj->rc_timestamp,
262 $obj->rc_user_text,
263 $talkpage->getFullURL()
264 );
265 $feed->outItem( $item );
266 }
267 $feed->outFooter();
268 }
269
270 /**
271 *
272 */
273 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
274 global $wgUser, $wgLang, $wgContLang;
275 $sk = $wgUser->getSkin();
276 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
277 ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( 'all' ) ), "{$more}" .
278 ($d ? "days={$d}&" : '') . 'limit='.$lim );
279 return $s;
280 }
281
282 /**
283 *
284 */
285 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
286 global $wgUser, $wgLang, $wgContLang;
287 $sk = $wgUser->getSkin();
288 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
289 ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( "all" ) ), $more.'days='.$d .
290 ($lim ? '&limit='.$lim : '') );
291 return $s;
292 }
293
294 /**
295 * Used also by Recentchangeslinked
296 */
297 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
298 $botLink = '', $liuLink = '', $patrLink = '' ) {
299 if ($more != '') $more .= '&';
300 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
301 rcCountLink( 100, $days, $page, $more ) . ' | ' .
302 rcCountLink( 250, $days, $page, $more ) . ' | ' .
303 rcCountLink( 500, $days, $page, $more ) .
304 ( $doall ? ( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
305 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
306 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
307 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
308 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
309 rcDaysLink( $limit, 30, $page, $more ) .
310 ( $doall ? ( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
311 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
312 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
313 return $note;
314 }
315
316 /**
317 * Format a diff for the newsfeed
318 */
319 function rcFormatDiff( $row ) {
320 $fname = 'rcFormatDiff';
321 wfProfileIn( $fname );
322
323 require_once( 'DifferenceEngine.php' );
324 $comment = "<p>" . htmlspecialchars( $row->rc_comment ) . "</p>\n";
325
326 if( $row->rc_namespace >= 0 ) {
327 global $wgContLang;
328
329 #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
330 #$diff->showDiffPage();
331
332 $dbr =& wfGetDB( DB_SLAVE );
333 if( $row->rc_this_oldid ) {
334 $newrow = $dbr->selectRow( 'old',
335 array( 'old_flags', 'old_text' ),
336 array( 'old_id' => $row->rc_this_oldid ) );
337 $newtext = Article::getRevisionText( $newrow );
338 } else {
339 $newrow = $dbr->selectRow( 'cur',
340 array( 'cur_text' ),
341 array( 'cur_id' => $row->rc_cur_id ) );
342 $newtext = $newrow->cur_text;
343 }
344 if( $row->rc_last_oldid ) {
345 wfProfileIn( "$fname-dodiff" );
346 $oldrow = $dbr->selectRow( 'old',
347 array( 'old_flags', 'old_text' ),
348 array( 'old_id' => $row->rc_last_oldid ) );
349 $oldtext = Article::getRevisionText( $oldrow );
350
351 global $wgFeedDiffCutoff;
352 if( strlen( $newtext ) > $wgFeedDiffCutoff ||
353 strlen( $oldtext ) > $wgFeedDiffCutoff ) {
354 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
355 $diffLink = $titleObj->escapeFullUrl(
356 'diff=' . $row->rc_this_oldid .
357 '&oldid=' . $row->rc_last_oldid );
358 $diffText = '<a href="' .
359 $diffLink .
360 '">' .
361 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
362 '</a>';
363 } else {
364 $diffText = DifferenceEngine::getDiff( $oldtext, $newtext,
365 wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp ) ),
366 wfMsg( 'currentrev' ) );
367 }
368 wfProfileOut( "$fname-dodiff" );
369 } else {
370 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
371 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
372 }
373
374 wfProfileOut( $fname );
375 return $comment . $diffText;
376 }
377
378 wfProfileOut( $fname );
379 return $comment;
380 }
381
382 ?>