5 * @subpackage SpecialPage
11 require_once( 'Feed.php' );
12 require_once( 'ChangesList.php' );
13 require_once( 'Revision.php' );
18 function wfSpecialRecentchanges( $par, $specialPage ) {
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' );
30 /* int */ 'days' => $wgUser->getDefaultOption('rcdays'),
31 /* int */ 'limit' => $wgUser->getDefaultOption('rclimit'),
32 /* bool */ 'hideminor' => false,
33 /* bool */ 'hidebots' => true,
34 /* bool */ 'hideliu' => false,
35 /* bool */ 'hidepatrolled' => false,
36 /* text */ 'from' => '',
37 /* text */ 'namespace' => null,
38 /* bool */ 'invert' => false,
44 $days = $wgUser->getOption( 'rcdays' );
45 if ( !$days ) { $days = $defaults['days']; }
46 $days = $wgRequest->getInt( 'days', $days );
48 $limit = $wgUser->getOption( 'rclimit' );
49 if ( !$limit ) { $limit = $defaults['limit']; }
51 # list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
52 $limit = $wgRequest->getInt( 'limit', $limit );
54 /* order of selection: url > preferences > default */
55 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor') ?
true : $defaults['hideminor'] );
58 # As a feed, use limited settings only
61 if( $limit > $wgFeedLimit ) {
62 $options['limit'] = $wgFeedLimit;
67 $namespace = $wgRequest->getIntOrNull( 'namespace' );
68 $invert = $wgRequest->getBool( 'invert', $defaults['invert'] );
69 $hidebots = $wgRequest->getBool( 'hidebots', $defaults['hidebots'] );
70 $hideliu = $wgRequest->getBool( 'hideliu', $defaults['hideliu'] );
71 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', $defaults['hidepatrolled'] );
72 $from = $wgRequest->getVal( 'from', $defaults['from'] );
74 # Get query parameters from path
76 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
77 foreach ( $bits as $bit ) {
78 if ( 'hidebots' == $bit ) $hidebots = 1;
79 if ( 'bots' == $bit ) $hidebots = 0;
80 if ( 'hideminor' == $bit ) $hideminor = 1;
81 if ( 'minor' == $bit ) $hideminor = 0;
82 if ( 'hideliu' == $bit ) $hideliu = 1;
83 if ( 'hidepatrolled' == $bit ) $hidepatrolled = 1;
85 if ( is_numeric( $bit ) ) {
89 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
93 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
100 if ( $limit < 0 ||
$limit > 5000 ) $limit = $defaults['limit'];
103 # Database connection and caching
104 $dbr =& wfGetDB( DB_SLAVE
);
105 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
108 $cutoff_unixtime = time() - ( $days * 86400 );
109 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
110 $cutoff = $dbr->timestamp( $cutoff_unixtime );
111 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW
,$cutoff)) {
112 $cutoff = $dbr->timestamp($from);
114 $from = $defaults['from'];
117 # 10 seconds server-side caching max
118 $wgOut->setSquidMaxage( 10 );
120 # Get last modified date, for client caching
121 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
122 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
123 if ( $feedFormat ||
!$wgUseRCPatrol ) {
124 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
125 # Client cache fresh and headers sent, nothing more to do.
130 $hidem = $hideminor ?
'AND rc_minor=0' : '';
131 $hidem .= $hidebots ?
' AND rc_bot=0' : '';
132 $hidem .= $hideliu ?
' AND rc_user=0' : '';
133 $hidem .= $hidepatrolled ?
' AND rc_patrolled=0' : '';
134 $hidem .= is_null( $namespace ) ?
'' : ' AND rc_namespace' . ($invert ?
'!=' : '=') . $namespace;
136 // This is the big thing!
138 $uid = $wgUser->getID();
141 $sql2 = "SELECT * FROM $recentchanges " .
142 ($uid ?
"LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
143 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
144 "ORDER BY rc_timestamp DESC";
145 $sql2 = $dbr->limitResult($sql2, $limit, 0);
146 $res = $dbr->query( $sql2, $fname );
148 // Fetch results, prepare a batch link existence check query
150 $batch = new LinkBatch
;
151 while( $row = $dbr->fetchObject( $res ) ){
154 $title = Title
::makeTitleSafe( NS_USER
, $row->rc_user_text
);
155 $batch->addObj( $title );
158 $title = Title
::makeTitleSafe( NS_USER_TALK
, $row->rc_user_text
);
159 $batch->addObj( $title );
162 $dbr->freeResult( $res );
164 // Run existence checks
165 $batch->execute( $wgLinkCache );
168 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
174 if ( !$specialPage->including() ) {
175 $wgOut->addWikiText( wfMsgForContent( "recentchangestext" ) );
177 // Dump everything here
178 $nondefaults = array();
180 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
181 wfAppendToArrayIfNotDefault( 'limit', $limit , $defaults, $nondefaults);
182 wfAppendToArrayIfNotDefault( 'hideminor', $hideminor, $defaults, $nondefaults);
183 wfAppendToArrayIfNotDefault( 'hidebots', $hidebots, $defaults, $nondefaults);
184 wfAppendToArrayIfNotDefault( 'hideliu', $hideliu, $defaults, $nondefaults);
185 wfAppendToArrayIfNotDefault( 'hidepatrolled', $hidepatrolled, $defaults, $nondefaults);
186 wfAppendToArrayIfNotDefault( 'from', $from, $defaults, $nondefaults);
187 wfAppendToArrayIfNotDefault( 'namespace', $namespace, $defaults, $nondefaults);
188 wfAppendToArrayIfNotDefault( 'invert', $invert, $defaults, $nondefaults);
190 // Add end of the texts
191 $wgOut->addHTML( '<div class="rcoptions">' . rcOptionsPanel( $defaults, $nondefaults ) . "\n" );
192 $wgOut->addHTML( rcNamespaceForm( $namespace, $invert, $nondefaults) . '</div>');
195 // And now for the content
196 $sk = $wgUser->getSkin();
197 $wgOut->setSyndicated( true );
198 $list =& new ChangesList( $sk );
199 $s = $list->beginRecentChangesList();
201 foreach( $rows as $obj ){
206 if ( ! ( $hideminor && $obj->rc_minor
) &&
207 ! ( $hidepatrolled && $obj->rc_patrolled
) ) {
208 $rc = RecentChange
::newFromRow( $obj );
209 $rc->counter
= $counter++
;
211 if ($wgShowUpdatedMarker
212 && !empty( $obj->wl_notificationtimestamp
)
213 && ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
)) {
214 $rc->notificationtimestamp
= true;
216 $rc->notificationtimestamp
= false;
219 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
220 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title
) ."' AND wl_namespace=$obj->rc_namespace" ;
221 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
222 $x = $dbr->fetchObject( $res3 );
223 $rc->numberofWatchingusers
= $x->n
;
225 $rc->numberofWatchingusers
= 0;
227 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
231 $s .= $list->endRecentChangesList();
232 $wgOut->addHTML( $s );
236 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
237 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
238 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
240 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
241 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
245 $timekey = "$wgDBname:rcfeed:$feedFormat:timestamp";
246 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
248 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
249 ' [' . $wgContLanguageCode . ']';
250 $feed = new $wgFeedClasses[$feedFormat](
252 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
253 $wgTitle->getFullUrl() );
256 * Bumping around loading up diffs can be pretty slow, so where
257 * possible we want to cache the feed output so the next visitor
261 if( $feedLastmod = $messageMemc->get( $timekey ) ) {
263 * If the cached feed was rendered very recently, we may
264 * go ahead and use it even if there have been edits made
265 * since it was rendered. This keeps a swarm of requests
266 * from being too bad on a super-frequently edited wiki.
268 if( time() - wfTimestamp( TS_UNIX
, $feedLastmod )
269 < $wgFeedCacheTimeout
270 ||
wfTimestamp( TS_UNIX
, $feedLastmod )
271 > wfTimestamp( TS_UNIX
, $lastmod ) ) {
272 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
273 $cachedFeed = $messageMemc->get( $key );
275 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
278 if( is_string( $cachedFeed ) ) {
279 wfDebug( "RC: Outputting cached feed\n" );
280 $feed->httpHeaders();
283 wfDebug( "RC: rendering new feed and caching it\n" );
285 rcDoOutputFeed( $rows, $feed );
286 $cachedFeed = ob_get_contents();
289 $expire = 3600 * 24; # One day
290 $messageMemc->set( $key, $cachedFeed );
291 $messageMemc->set( $timekey, wfTimestamp( TS_MW
), $expire );
296 function rcDoOutputFeed( $rows, &$feed ) {
297 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
301 # Merge adjacent edits by one user
304 foreach( $rows as $obj ) {
306 $obj->rc_namespace
>= 0 &&
307 $obj->rc_cur_id
== $sorted[$n-1]->rc_cur_id
&&
308 $obj->rc_user_text
== $sorted[$n-1]->rc_user_text
) {
309 $sorted[$n-1]->rc_last_oldid
= $obj->rc_last_oldid
;
317 foreach( $sorted as $obj ) {
318 $title = Title
::makeTitle( $obj->rc_namespace
, $obj->rc_title
);
319 $talkpage = $title->getTalkPage();
320 $item = new FeedItem(
321 $title->getPrefixedText(),
322 rcFormatDiff( $obj ),
323 $title->getFullURL(),
326 $talkpage->getFullURL()
328 $feed->outItem( $item );
336 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
337 global $wgUser, $wgLang, $wgContLang;
338 $sk = $wgUser->getSkin();
339 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
340 ($lim ?
$wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
341 ($d ?
"days={$d}&" : '') . 'limit='.$lim );
348 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
349 global $wgUser, $wgLang, $wgContLang;
350 $sk = $wgUser->getSkin();
351 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
352 ($d ?
$wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
353 ($lim ?
'&limit='.$lim : '') );
358 * Used by Recentchangeslinked
360 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
361 $botLink = '', $liuLink = '', $patrLink = '' ) {
362 if ($more != '') $more .= '&';
363 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
364 rcCountLink( 100, $days, $page, $more ) . ' | ' .
365 rcCountLink( 250, $days, $page, $more ) . ' | ' .
366 rcCountLink( 500, $days, $page, $more ) .
367 ( $doall ?
( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
368 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
369 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
370 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
371 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
372 rcDaysLink( $limit, 30, $page, $more ) .
373 ( $doall ?
( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
374 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
375 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
381 * Makes change an option link which carries all the other options
383 function makeOptionsLink( $title, $override, $options ) {
384 global $wgUser, $wgLang, $wgContLang;
385 $sk = $wgUser->getSkin();
386 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
387 $title, wfArrayToCGI( $override, $options ) );
391 * Creates the options panel
393 function rcOptionsPanel( $defaults, $nondefaults ) {
396 $options = $nondefaults +
$defaults;
398 if( $options['from'] )
399 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $options['limit'] ), $wgLang->timeanddate( $options['from'], true ) );
401 $note = wfMsg( 'rcnote', $wgLang->formatNum( $options['limit'] ), $wgLang->formatNum( $options['days'] ) );
405 $options_limit = array(50, 100, 250, 500);
407 while ( $i+
1 < count($options_limit) ) {
408 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $nondefaults) . ' | ' ;
411 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $nondefaults) ;
413 // day links, reset 'from' to none
415 $options_days = array(1, 3, 7, 14, 30);
417 while ( $i+
1 < count($options_days) ) {
418 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $nondefaults) . ' | ' ;
421 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $nondefaults) ;
424 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
425 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
426 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
427 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
428 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
429 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
430 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
431 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
432 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
434 $hl = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
436 // show from this onward link
437 $now = $wgLang->timeanddate( wfTimestampNow(), true );
438 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
440 $rclinks = wfMsg( 'rclinks', $cl, $dl, $hl );
441 $rclistfrom = wfMsg( 'rclistfrom', $tl );
442 return "$note<br />$rclinks<br />$rclistfrom";
447 * Creates the choose namespace selection
451 * @param mixed $namespace The key of the currently selected namespace, empty string
453 * @param bool $invert Whether to invert the namespace selection
454 * @param array $nondefaults An array of non default options to be remembered
458 function rcNamespaceForm ( $namespace, $invert, $nondefaults ) {
459 global $wgContLang, $wgScript;
460 $t = Title
::makeTitle( NS_SPECIAL
, 'Recentchanges' );
462 $namespaceselect = HTMLnamespaceselector($namespace, '');
463 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . '" />';
464 $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ?
' checked="checked"' : '' ) . ' />';
466 $out = "<div class='namespacesettings'><form method='get' action='{$wgScript}'>\n";
468 foreach ( $nondefaults as $key => $value ) {
469 if ($key != 'namespace' && $key != 'invert')
470 $out .= wfElement('input', array( 'type' => 'hidden', 'name' => $key, 'value' => $value));
473 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
475 <div id='nsselect' class='recentchanges'>
476 <label for='namespace'>" . wfMsgHtml('namespace') . "</label>
477 $namespaceselect $submitbutton $invertbox <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>
479 $out .= '</form></div>';
485 * Format a diff for the newsfeed
487 function rcFormatDiff( $row ) {
488 $fname = 'rcFormatDiff';
489 wfProfileIn( $fname );
491 require_once( 'DifferenceEngine.php' );
492 $comment = "<p>" . htmlspecialchars( $row->rc_comment
) . "</p>\n";
494 if( $row->rc_namespace
>= 0 ) {
497 #$diff =& new DifferenceEngine( $row->rc_this_oldid, $row->rc_last_oldid, $row->rc_id );
498 #$diff->showDiffPage();
500 $titleObj = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
501 $dbr =& wfGetDB( DB_SLAVE
);
502 $newrev = Revision
::newFromTitle( $titleObj, $row->rc_this_oldid
);
504 $newtext = $newrev->getText();
506 $diffText = "<p>Can't load revision $row->rc_this_oldid</p>";
507 wfProfileOut( $fname );
508 return $comment . $diffText;
511 if( $row->rc_last_oldid
) {
512 wfProfileIn( "$fname-dodiff" );
513 $oldrev =& Revision
::newFromId( $row->rc_last_oldid
);
515 $diffText = "<p>Can't load old revision $row->rc_last_oldid</p>";
516 wfProfileOut( $fname );
517 return $comment . $diffText;
519 $oldtext = $oldrev->getText();
521 # Old entries may contain illegal characters
522 # which will damage output
523 $oldtext = UtfNormal
::cleanUp( $oldtext );
525 global $wgFeedDiffCutoff;
526 if( strlen( $newtext ) > $wgFeedDiffCutoff ||
527 strlen( $oldtext ) > $wgFeedDiffCutoff ) {
528 $diffLink = $titleObj->escapeFullUrl(
529 'diff=' . $row->rc_this_oldid
.
530 '&oldid=' . $row->rc_last_oldid
);
531 $diffText = '<a href="' .
534 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
537 $diffText = DifferenceEngine
::getDiff( $oldtext, $newtext,
538 wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp
) ),
539 wfMsg( 'currentrev' ) );
541 wfProfileOut( "$fname-dodiff" );
543 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
544 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
547 wfProfileOut( $fname );
548 return $comment . $diffText;
551 wfProfileOut( $fname );