Use LinkBatch for faster link lookup
[mediawiki.git] / includes / SpecialRecentchanges.php
blob470489ab7bfe449ccd7d769734c3b9fe5b787f39
1 <?php
2 /**
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
8 /**
11 require_once( 'Feed.php' );
12 require_once( 'ChangesList.php' );
13 require_once( 'Revision.php' );
15 /**
16 * Constructor
18 function wfSpecialRecentchanges( $par ) {
19 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgTitle, $wgMemc, $wgDBname;
20 global $wgRequest, $wgSitename, $wgLanguageCode, $wgContLanguageCode;
21 global $wgFeedClasses, $wgUseRCPatrol;
22 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
23 global $wgLinkCache;
24 $fname = 'wfSpecialRecentchanges';
26 # Get query parameters
27 $feedFormat = $wgRequest->getVal( 'feed' );
29 $defaultDays = $wgUser->getOption( 'rcdays' );
30 if ( !$defaultDays ) { $defaultDays = 3; }
32 $days = $wgRequest->getInt( 'days', $defaultDays );
33 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor' ) ) ? 1 : 0;
34 list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
36 # As a feed, use limited settings only
37 if( $feedFormat ) {
38 $from = null;
39 $hidebots = 1;
40 $hideliu = 0;
41 $hidepatrolled = 0;
42 global $wgFeedLimit;
43 if( $limit > $wgFeedLimit ) {
44 $limit = $wgFeedLimit;
46 } else {
47 $from = $wgRequest->getText( 'from' );
48 $hidebots = $wgRequest->getBool( 'hidebots', true ) ? 1 : 0;
49 $hideliu = $wgRequest->getBool( 'hideliu', false ) ? 1 : 0;
50 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', false ) ? 1 : 0;
52 # Get query parameters from path
53 if( $par ) {
54 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
55 if( in_array( 'hidebots', $bits ) ) $hidebots = 1;
56 if( in_array( 'bots', $bits ) ) $hidebots = 0;
57 if( in_array( 'hideminor', $bits ) ) $hideminor = 1;
58 if( in_array( 'minor', $bits ) ) $hideminor = 0;
59 if( in_array( 'hideliu', $bits) ) $hideliu = 1;
60 if( in_array( 'hidepatrolled', $bits) ) $hidepatrolled = 1;
65 # Database connection and caching
66 $dbr =& wfGetDB( DB_SLAVE );
67 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
70 # 10 seconds server-side caching max
71 $wgOut->setSquidMaxage( 10 );
73 # Get last modified date, for client caching
74 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
75 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
76 if ( $feedFormat || !$wgUseRCPatrol ) {
77 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
78 # Client cache fresh and headers sent, nothing more to do.
79 return;
83 # Output header
84 $rctext = wfMsgForContent( "recentchangestext" );
85 $wgOut->addWikiText( $rctext );
88 $now = wfTimestampNow();
89 $cutoff_unixtime = time() - ( $days * 86400 );
90 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime % 86400);
91 $cutoff = $dbr->timestamp( $cutoff_unixtime );
92 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW,$cutoff)) {
93 $cutoff = $dbr->timestamp($from);
94 } else {
95 unset($from);
98 $sk = $wgUser->getSkin();
100 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
102 $hidem = ( $hideminor ) ? 'AND rc_minor=0' : '';
103 $hidem .= ( $hidebots ) ? ' AND rc_bot=0' : '';
104 $hidem .= ( $hideliu ) ? ' AND rc_user=0' : '';
105 $hidem .= ( $hidepatrolled )? ' AND rc_patrolled=0' : '';
107 $urlparams = array( 'hideminor' => $hideminor, 'hideliu' => $hideliu,
108 'hidebots' => $hidebots, 'hidepatrolled' => $hidepatrolled,
109 'limit' => $limit );
110 $hideparams = wfArrayToCGI( $urlparams );
112 $minorLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
113 $showhide[1-$hideminor], wfArrayToCGI( array( 'hideminor' => 1-$hideminor ), $urlparams ) );
114 $botLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
115 $showhide[1-$hidebots], wfArrayToCGI( array( 'hidebots' => 1-$hidebots ), $urlparams ) );
116 $liuLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
117 $showhide[1-$hideliu], wfArrayToCGI( array( 'hideliu' => 1-$hideliu ), $urlparams ) );
118 $patrLink = $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
119 $showhide[1-$hidepatrolled], wfArrayToCGI( array( 'hidepatrolled' => 1-$hidepatrolled ), $urlparams ) );
121 $uid = $wgUser->getID();
123 // Perform query
124 $sql2 = "SELECT $recentchanges.*" . ($uid ? ",wl_user,wl_notificationtimestamp" : "") . " FROM $recentchanges " .
125 ($uid ? "LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
126 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
127 "ORDER BY rc_timestamp DESC LIMIT {$limit}";
128 $res = $dbr->query( $sql2, $fname );
130 // Fetch results, prepare a batch link existence check query
131 $rows = array();
132 $batch = new LinkBatch;
133 while( $row = $dbr->fetchObject( $res ) ){
134 $rows[] = $row;
136 // Title
137 $batch->add( $row->rc_namespace, $row->rc_title );
139 // User page link
140 $title = Title::makeTitleSafe( NS_USER, $row->rc_user_text );
141 $batch->addObj( $title );
143 // User talk
144 $title = Title::makeTitleSafe( NS_USER_TALK, $row->rc_user_text );
145 $batch->addObj( $title );
148 $dbr->freeResult( $res );
150 // Run existence checks
151 $batch->execute( $wgLinkCache );
153 if(isset($from)) {
154 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $limit ),
155 $wgLang->timeanddate( $from, true ) );
156 } else {
157 $note = wfMsg( 'rcnote', $wgLang->formatNum( $limit ), $wgLang->formatNum( $days ) );
159 $wgOut->addHTML( "\n<hr />\n{$note}\n<br />" );
161 $note = rcDayLimitLinks( $days, $limit, 'Recentchanges', $hideparams, false, $minorLink, $botLink, $liuLink, $patrLink );
163 $note .= "<br />\n" . wfMsg( 'rclistfrom',
164 $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
165 $wgLang->timeanddate( $now, true ), $hideparams.'&from='.$now ) );
167 $wgOut->addHTML( $note."\n" );
169 if( $feedFormat ) {
170 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
171 } else {
172 # Web output...
173 $wgOut->setSyndicated( true );
174 $list =& new ChangesList( $sk );
175 $s = $list->beginRecentChangesList();
176 $counter = 1;
177 foreach( $rows as $obj ){
178 if( $limit == 0) {
179 break;
182 if ( ! ( $hideminor && $obj->rc_minor ) &&
183 ! ( $hidepatrolled && $obj->rc_patrolled ) ) {
184 $rc = RecentChange::newFromRow( $obj );
185 $rc->counter = $counter++;
187 if ($wgShowUpdatedMarker
188 && $wgUser->getOption( 'showupdated' )
189 && !empty( $obj->wl_notificationtimestamp )
190 && ($obj->rc_timestamp >= $obj->wl_notificationtimestamp)) {
191 $rc->notificationtimestamp = true;
192 } else {
193 $rc->notificationtimestamp = false;
196 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
197 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title) ."' AND wl_namespace=$obj->rc_namespace" ;
198 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
199 $x = $dbr->fetchObject( $res3 );
200 $rc->numberofWatchingusers = $x->n;
201 } else {
202 $rc->numberofWatchingusers = 0;
204 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ) );
205 --$limit;
208 $s .= $list->endRecentChangesList();
209 $wgOut->addHTML( $s );
213 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
214 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
215 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
217 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
218 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
219 return false;
222 $timekey = "$wgDBname:rcfeed:timestamp";
223 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
225 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
226 ' [' . $wgContLanguageCode . ']';
227 $feed = new $wgFeedClasses[$feedFormat](
228 $feedTitle,
229 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
230 $wgTitle->getFullUrl() );
233 * Bumping around loading up diffs can be pretty slow, so where
234 * possible we want to cache the feed output so the next visitor
235 * gets it quick too.
237 $cachedFeed = false;
238 if( $feedLastmod = $messageMemc->get( $timekey ) ) {
240 * If the cached feed was rendered very recently, we may
241 * go ahead and use it even if there have been edits made
242 * since it was rendered. This keeps a swarm of requests
243 * from being too bad on a super-frequently edited wiki.
245 if( time() - wfTimestamp( TS_UNIX, $feedLastmod )
246 < $wgFeedCacheTimeout
247 || wfTimestamp( TS_UNIX, $feedLastmod )
248 > wfTimestamp( TS_UNIX, $lastmod ) ) {
249 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
250 $cachedFeed = $messageMemc->get( $key );
251 } else {
252 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
255 if( is_string( $cachedFeed ) ) {
256 wfDebug( "RC: Outputting cached feed\n" );
257 $feed->httpHeaders();
258 echo $cachedFeed;
259 } else {
260 wfDebug( "RC: rendering new feed and caching it\n" );
261 ob_start();
262 rcDoOutputFeed( $rows, $feed );
263 $cachedFeed = ob_get_contents();
264 ob_end_flush();
266 $expire = 3600 * 24; # One day
267 $messageMemc->set( $key, $cachedFeed );
268 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
270 return true;
273 function rcDoOutputFeed( $rows, &$feed ) {
274 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
276 $feed->outHeader();
278 # Merge adjacent edits by one user
279 $sorted = array();
280 $n = 0;
281 foreach( $rows as $obj ) {
282 if( $n > 0 &&
283 $obj->rc_namespace >= 0 &&
284 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
285 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
286 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
287 } else {
288 $sorted[$n] = $obj;
289 $n++;
291 $first = false;
294 foreach( $sorted as $obj ) {
295 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
296 $talkpage = $title->getTalkPage();
297 $item = new FeedItem(
298 $title->getPrefixedText(),
299 rcFormatDiff( $obj ),
300 $title->getFullURL(),
301 $obj->rc_timestamp,
302 $obj->rc_user_text,
303 $talkpage->getFullURL()
305 $feed->outItem( $item );
307 $feed->outFooter();
313 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
314 global $wgUser, $wgLang, $wgContLang;
315 $sk = $wgUser->getSkin();
316 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
317 ($lim ? $wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
318 ($d ? "days={$d}&" : '') . 'limit='.$lim );
319 return $s;
325 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
326 global $wgUser, $wgLang, $wgContLang;
327 $sk = $wgUser->getSkin();
328 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
329 ($d ? $wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
330 ($lim ? '&limit='.$lim : '') );
331 return $s;
335 * Used also by Recentchangeslinked
337 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
338 $botLink = '', $liuLink = '', $patrLink = '' ) {
339 if ($more != '') $more .= '&';
340 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
341 rcCountLink( 100, $days, $page, $more ) . ' | ' .
342 rcCountLink( 250, $days, $page, $more ) . ' | ' .
343 rcCountLink( 500, $days, $page, $more ) .
344 ( $doall ? ( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
345 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
346 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
347 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
348 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
349 rcDaysLink( $limit, 30, $page, $more ) .
350 ( $doall ? ( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
351 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
352 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
353 return $note;
357 * Format a diff for the newsfeed
359 function rcFormatDiff( $row ) {
360 $fname = 'rcFormatDiff';
361 wfProfileIn( $fname );
363 require_once( 'DifferenceEngine.php' );
364 $comment = "<p>" . htmlspecialchars( $row->rc_comment ) . "</p>\n";
366 if( $row->rc_namespace >= 0 ) {
367 global $wgContLang;
369 #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
370 #$diff->showDiffPage();
372 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
373 $dbr =& wfGetDB( DB_SLAVE );
374 $newrev =& Revision::newFromTitle( $titleObj, $row->rc_this_oldid );
375 if( $newrev ) {
376 $newtext = $newrev->getText();
377 } else {
378 $diffText = "<p>Can't load revision $row->rc_this_oldid</p>";
379 wfProfileOut( $fname );
380 return $comment . $diffText;
383 if( $row->rc_last_oldid ) {
384 wfProfileIn( "$fname-dodiff" );
385 $oldrev =& Revision::newFromId( $row->rc_last_oldid );
386 if( !$oldrev ) {
387 $diffText = "<p>Can't load old revision $row->rc_last_oldid</p>";
388 wfProfileOut( $fname );
389 return $comment . $diffText;
391 $oldtext = $oldrev->getText();
393 # Old entries may contain illegal characters
394 # which will damage output
395 $oldtext = UtfNormal::cleanUp( $oldtext );
397 global $wgFeedDiffCutoff;
398 if( strlen( $newtext ) > $wgFeedDiffCutoff ||
399 strlen( $oldtext ) > $wgFeedDiffCutoff ) {
400 $diffLink = $titleObj->escapeFullUrl(
401 'diff=' . $row->rc_this_oldid .
402 '&oldid=' . $row->rc_last_oldid );
403 $diffText = '<a href="' .
404 $diffLink .
405 '">' .
406 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
407 '</a>';
408 } else {
409 $diffText = DifferenceEngine::getDiff( $oldtext, $newtext,
410 wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp ) ),
411 wfMsg( 'currentrev' ) );
413 wfProfileOut( "$fname-dodiff" );
414 } else {
415 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
416 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
419 wfProfileOut( $fname );
420 return $comment . $diffText;
423 wfProfileOut( $fname );
424 return $comment;