5 * @subpackage SpecialPage
11 require_once( 'Feed.php' );
12 require_once( 'ChangesList.php' );
13 require_once( 'Revision.php' );
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;
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
45 if( $limit > $wgFeedLimit ) {
46 $limit = $wgFeedLimit;
49 $from = $wgRequest->getText( 'from' );
50 $hidebots = $wgRequest->getBool( 'hidebots', true );
51 $hideliu = $wgRequest->getBool( 'hideliu' );
52 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled' );
53 $namespace = $wgRequest->getVal( 'namespace', '' );
54 $namespace = $namespace === '' ?
NULL : $namespace;
55 $invert = $wgRequest->getBool( 'invert' );
57 # Get query parameters from path
59 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
60 if( in_array( 'hidebots', $bits ) ) $hidebots = 1;
61 if( in_array( 'bots', $bits ) ) $hidebots = 0;
62 if( in_array( 'hideminor', $bits ) ) $hideminor = 1;
63 if( in_array( 'minor', $bits ) ) $hideminor = 0;
64 if( in_array( 'hideliu', $bits) ) $hideliu = 1;
65 if( in_array( 'hidepatrolled', $bits) ) $hidepatrolled = 1;
70 # Database connection and caching
71 $dbr =& wfGetDB( DB_SLAVE
);
72 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
75 # 10 seconds server-side caching max
76 $wgOut->setSquidMaxage( 10 );
78 # Get last modified date, for client caching
79 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
80 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
81 if ( $feedFormat ||
!$wgUseRCPatrol ) {
82 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
83 # Client cache fresh and headers sent, nothing more to do.
89 $rctext = wfMsgForContent( "recentchangestext" );
90 $wgOut->addWikiText( $rctext );
93 $cutoff_unixtime = time() - ( $days * 86400 );
94 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
95 $cutoff = $dbr->timestamp( $cutoff_unixtime );
96 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW
,$cutoff)) {
97 $cutoff = $dbr->timestamp($from);
102 $sk = $wgUser->getSkin();
104 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
106 $hidem = $hideminor ?
'AND rc_minor=0' : '';
107 $hidem .= $hidebots ?
' AND rc_bot=0' : '';
108 $hidem .= $hideliu ?
' AND rc_user=0' : '';
109 $hidem .= $hidepatrolled ?
' AND rc_patrolled=0' : '';
110 $hidem .= is_null($namespace) ?
'' : ' AND rc_namespace' . ($invert ?
'!=' : '=') . $namespace;
113 'hideminor' => $hideminor,
114 'hideliu' => $hideliu,
115 'hidebots' => $hidebots,
116 'hidepatrolled' => $hidepatrolled,
120 'namespace' => $namespace,
124 $uid = $wgUser->getID();
127 $sql2 = "SELECT $recentchanges.*" . ($uid ?
",wl_user,wl_notificationtimestamp" : "") . " FROM $recentchanges " .
128 ($uid ?
"LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
129 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
130 "ORDER BY rc_timestamp DESC LIMIT {$limit}";
131 $res = $dbr->query( $sql2, $fname );
133 // Fetch results, prepare a batch link existence check query
135 $batch = new LinkBatch
;
136 while( $row = $dbr->fetchObject( $res ) ){
139 $title = Title
::makeTitleSafe( NS_USER
, $row->rc_user_text
);
140 $batch->addObj( $title );
143 $title = Title
::makeTitleSafe( NS_USER_TALK
, $row->rc_user_text
);
144 $batch->addObj( $title );
147 $dbr->freeResult( $res );
149 // Run existence checks
150 $batch->execute( $wgLinkCache );
152 $wgOut->addHTML( '<div class="rcoptions">' . rcOptionsPanel( $options ) . '</div>' );
153 $wgOut->addHTML( namespaceForm( $namespace, $invert) );
154 foreach ( $options as $key => $value ) {
155 if ($value != 'namespace' && $value != 'invert')
156 $wgOut->addHTML( "<input type='hidden' name='$key' value='$value' />" );
161 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
164 $wgOut->setSyndicated( true );
165 $list =& new ChangesList( $sk );
166 $s = $list->beginRecentChangesList();
168 foreach( $rows as $obj ){
173 if ( ! ( $hideminor && $obj->rc_minor
) &&
174 ! ( $hidepatrolled && $obj->rc_patrolled
) ) {
175 $rc = RecentChange
::newFromRow( $obj );
176 $rc->counter
= $counter++
;
178 if ($wgShowUpdatedMarker
179 && $wgUser->getOption( 'showupdated' )
180 && !empty( $obj->wl_notificationtimestamp
)
181 && ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
)) {
182 $rc->notificationtimestamp
= true;
184 $rc->notificationtimestamp
= false;
187 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
188 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title
) ."' AND wl_namespace=$obj->rc_namespace" ;
189 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
190 $x = $dbr->fetchObject( $res3 );
191 $rc->numberofWatchingusers
= $x->n
;
193 $rc->numberofWatchingusers
= 0;
195 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
199 $s .= $list->endRecentChangesList();
200 $wgOut->addHTML( $s );
204 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
205 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
206 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
208 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
209 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
213 $timekey = "$wgDBname:rcfeed:$feedFormat:timestamp";
214 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
216 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
217 ' [' . $wgContLanguageCode . ']';
218 $feed = new $wgFeedClasses[$feedFormat](
220 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
221 $wgTitle->getFullUrl() );
224 * Bumping around loading up diffs can be pretty slow, so where
225 * possible we want to cache the feed output so the next visitor
229 if( $feedLastmod = $messageMemc->get( $timekey ) ) {
231 * If the cached feed was rendered very recently, we may
232 * go ahead and use it even if there have been edits made
233 * since it was rendered. This keeps a swarm of requests
234 * from being too bad on a super-frequently edited wiki.
236 if( time() - wfTimestamp( TS_UNIX
, $feedLastmod )
237 < $wgFeedCacheTimeout
238 ||
wfTimestamp( TS_UNIX
, $feedLastmod )
239 > wfTimestamp( TS_UNIX
, $lastmod ) ) {
240 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
241 $cachedFeed = $messageMemc->get( $key );
243 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
246 if( is_string( $cachedFeed ) ) {
247 wfDebug( "RC: Outputting cached feed\n" );
248 $feed->httpHeaders();
251 wfDebug( "RC: rendering new feed and caching it\n" );
253 rcDoOutputFeed( $rows, $feed );
254 $cachedFeed = ob_get_contents();
257 $expire = 3600 * 24; # One day
258 $messageMemc->set( $key, $cachedFeed );
259 $messageMemc->set( $timekey, wfTimestamp( TS_MW
), $expire );
264 function rcDoOutputFeed( $rows, &$feed ) {
265 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
269 # Merge adjacent edits by one user
272 foreach( $rows as $obj ) {
274 $obj->rc_namespace
>= 0 &&
275 $obj->rc_cur_id
== $sorted[$n-1]->rc_cur_id
&&
276 $obj->rc_user_text
== $sorted[$n-1]->rc_user_text
) {
277 $sorted[$n-1]->rc_last_oldid
= $obj->rc_last_oldid
;
285 foreach( $sorted as $obj ) {
286 $title = Title
::makeTitle( $obj->rc_namespace
, $obj->rc_title
);
287 $talkpage = $title->getTalkPage();
288 $item = new FeedItem(
289 $title->getPrefixedText(),
290 rcFormatDiff( $obj ),
291 $title->getFullURL(),
294 $talkpage->getFullURL()
296 $feed->outItem( $item );
304 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
305 global $wgUser, $wgLang, $wgContLang;
306 $sk = $wgUser->getSkin();
307 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
308 ($lim ?
$wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
309 ($d ?
"days={$d}&" : '') . 'limit='.$lim );
316 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
317 global $wgUser, $wgLang, $wgContLang;
318 $sk = $wgUser->getSkin();
319 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
320 ($d ?
$wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
321 ($lim ?
'&limit='.$lim : '') );
326 * Used by Recentchangeslinked
328 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
329 $botLink = '', $liuLink = '', $patrLink = '' ) {
330 if ($more != '') $more .= '&';
331 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
332 rcCountLink( 100, $days, $page, $more ) . ' | ' .
333 rcCountLink( 250, $days, $page, $more ) . ' | ' .
334 rcCountLink( 500, $days, $page, $more ) .
335 ( $doall ?
( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
336 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
337 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
338 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
339 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
340 rcDaysLink( $limit, 30, $page, $more ) .
341 ( $doall ?
( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
342 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
343 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
349 * Makes change an option link which carries all the other options
351 function makeOptionsLink( $title, $override, $options ) {
352 global $wgUser, $wgLang, $wgContLang;
353 $sk = $wgUser->getSkin();
354 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
355 $title, wfArrayToCGI( $override, $options ) );
359 * Creates the options panel
361 function rcOptionsPanel( $options ) {
364 if( $options['from'] )
365 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $options['limit'] ), $wgLang->timeanddate( $options['from'], true ) );
367 $note = wfMsg( 'rcnote', $wgLang->formatNum( $options['limit'] ), $wgLang->formatNum( $options['days'] ) );
371 $options_limit = array(50, 100, 250, 500);
373 while ( $i+
1 < count($options_limit) ) {
374 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $options) . ' | ' ;
377 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $options) ;
379 // day links, reset 'from' to none
381 $options_days = array(1, 3, 7, 14, 30);
383 while ( $i+
1 < count($options_days) ) {
384 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $options) . ' | ' ;
387 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $options) ;
390 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
391 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
392 array( 'hideminor' => 1-$options['hideminor'] ), $options);
393 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
394 array( 'hidebots' => 1-$options['hidebots'] ), $options);
395 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
396 array( 'hideliu' => 1-$options['hideliu'] ), $options);
397 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
398 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $options);
400 $hl = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
402 // show from this onward link
403 $now = $wgLang->timeanddate( wfTimestampNow(), true );
404 $tl = makeOptionsLink( $now, array( 'from' => $now), $options );
406 $rclinks = wfMsg( 'rclinks', $cl, $dl, $hl );
407 $rclistfrom = wfMsg( 'rclistfrom', $tl );
408 return "$note<br />$rclinks<br />$rclistfrom";
413 * Creates the choose namespace selection
417 * @param mixed $namespace The key of the currently selected namespace, NULL
419 * @param bool $invert Whether to invert the namespace selection
423 function namespaceForm ( $namespace, $invert ) {
424 global $wgContLang, $wgScript;
425 $t = Title
::makeTitle( NS_SPECIAL
, 'Recentchanges' );
427 $namespaceselect = "<select name='namespace' id='nsselectbox'>";
428 $namespaceselect .= '<option value="" ' . (is_null($namespace) ?
' selected="selected"' : '') . '>' . wfMsg( 'contributionsall' ) . '</option>';
429 $arr = $wgContLang->getFormattedNamespaces();
430 foreach ( $arr as $ns => $name ) {
433 $n = $ns === NS_MAIN ?
wfMsg ( 'blanknamespace' ) : $name;
434 $sel = $namespace === (string)$ns ?
' selected="selected"' : '';
435 $namespaceselect .= "<option value='$ns'$sel>$n</option>";
437 $namespaceselect .= '</select>';
439 $submitbutton = '<input type="submit" value="' . wfMsg( 'allpagessubmit' ) . '" />';
440 $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ?
' checked="checked"' : '' ) . ' />';
442 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>\n";
443 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
445 <table id='nsselect' class='recentchanges'>
447 <td align='right'><label for='nsselectbox'>" . wfMsg('namespace') . "</label></td>
448 <td align='left'>$namespaceselect $invertbox <label for='nsinvert'>" . wfMsg('invert') . "</label> $submitbutton </td>
451 $out .= '</form></div>';
457 * Format a diff for the newsfeed
459 function rcFormatDiff( $row ) {
460 $fname = 'rcFormatDiff';
461 wfProfileIn( $fname );
463 require_once( 'DifferenceEngine.php' );
464 $comment = "<p>" . htmlspecialchars( $row->rc_comment
) . "</p>\n";
466 if( $row->rc_namespace
>= 0 ) {
469 #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
470 #$diff->showDiffPage();
472 $titleObj = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
473 $dbr =& wfGetDB( DB_SLAVE
);
474 $newrev =& Revision
::newFromTitle( $titleObj, $row->rc_this_oldid
);
476 $newtext = $newrev->getText();
478 $diffText = "<p>Can't load revision $row->rc_this_oldid</p>";
479 wfProfileOut( $fname );
480 return $comment . $diffText;
483 if( $row->rc_last_oldid
) {
484 wfProfileIn( "$fname-dodiff" );
485 $oldrev =& Revision
::newFromId( $row->rc_last_oldid
);
487 $diffText = "<p>Can't load old revision $row->rc_last_oldid</p>";
488 wfProfileOut( $fname );
489 return $comment . $diffText;
491 $oldtext = $oldrev->getText();
493 # Old entries may contain illegal characters
494 # which will damage output
495 $oldtext = UtfNormal
::cleanUp( $oldtext );
497 global $wgFeedDiffCutoff;
498 if( strlen( $newtext ) > $wgFeedDiffCutoff ||
499 strlen( $oldtext ) > $wgFeedDiffCutoff ) {
500 $diffLink = $titleObj->escapeFullUrl(
501 'diff=' . $row->rc_this_oldid
.
502 '&oldid=' . $row->rc_last_oldid
);
503 $diffText = '<a href="' .
506 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
509 $diffText = DifferenceEngine
::getDiff( $oldtext, $newtext,
510 wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp
) ),
511 wfMsg( 'currentrev' ) );
513 wfProfileOut( "$fname-dodiff" );
515 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
516 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
519 wfProfileOut( $fname );
520 return $comment . $diffText;
523 wfProfileOut( $fname );