634aed93de02ca11ab415a50b82b5f58bb7b02a1
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, $wgRequest, $wgUseRCPatrol;
20 global $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
21 $fname = 'wfSpecialRecentchanges';
23 # Get query parameters
24 $feedFormat = $wgRequest->getVal( 'feed' );
27 /* int */ 'days' => $wgUser->getDefaultOption('rcdays'),
28 /* int */ 'limit' => $wgUser->getDefaultOption('rclimit'),
29 /* bool */ 'hideminor' => false,
30 /* bool */ 'hidebots' => true,
31 /* bool */ 'hideliu' => false,
32 /* bool */ 'hidepatrolled' => false,
33 /* text */ 'from' => '',
34 /* text */ 'namespace' => null,
35 /* bool */ 'invert' => false,
41 $days = $wgUser->getOption( 'rcdays' );
42 if ( !$days ) { $days = $defaults['days']; }
43 $days = $wgRequest->getInt( 'days', $days );
45 $limit = $wgUser->getOption( 'rclimit' );
46 if ( !$limit ) { $limit = $defaults['limit']; }
48 # list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
49 $limit = $wgRequest->getInt( 'limit', $limit );
51 /* order of selection: url > preferences > default */
52 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor') ?
true : $defaults['hideminor'] );
55 # As a feed, use limited settings only
58 if( $limit > $wgFeedLimit ) {
59 $options['limit'] = $wgFeedLimit;
64 $namespace = $wgRequest->getIntOrNull( 'namespace' );
65 $invert = $wgRequest->getBool( 'invert', $defaults['invert'] );
66 $hidebots = $wgRequest->getBool( 'hidebots', $defaults['hidebots'] );
67 $hideliu = $wgRequest->getBool( 'hideliu', $defaults['hideliu'] );
68 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', $defaults['hidepatrolled'] );
69 $from = $wgRequest->getVal( 'from', $defaults['from'] );
71 # Get query parameters from path
73 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
74 foreach ( $bits as $bit ) {
75 if ( 'hidebots' == $bit ) $hidebots = 1;
76 if ( 'bots' == $bit ) $hidebots = 0;
77 if ( 'hideminor' == $bit ) $hideminor = 1;
78 if ( 'minor' == $bit ) $hideminor = 0;
79 if ( 'hideliu' == $bit ) $hideliu = 1;
80 if ( 'hidepatrolled' == $bit ) $hidepatrolled = 1;
82 if ( is_numeric( $bit ) ) {
86 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
90 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
97 if ( $limit < 0 ||
$limit > 5000 ) $limit = $defaults['limit'];
100 # Database connection and caching
101 $dbr =& wfGetDB( DB_SLAVE
);
102 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
105 $cutoff_unixtime = time() - ( $days * 86400 );
106 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
107 $cutoff = $dbr->timestamp( $cutoff_unixtime );
108 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW
,$cutoff)) {
109 $cutoff = $dbr->timestamp($from);
111 $from = $defaults['from'];
114 # 10 seconds server-side caching max
115 $wgOut->setSquidMaxage( 10 );
117 # Get last modified date, for client caching
118 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
119 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
120 if ( $feedFormat ||
!$wgUseRCPatrol ) {
121 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
122 # Client cache fresh and headers sent, nothing more to do.
127 $hidem = $hideminor ?
'AND rc_minor=0' : '';
128 $hidem .= $hidebots ?
' AND rc_bot=0' : '';
129 $hidem .= $hideliu ?
' AND rc_user=0' : '';
130 $hidem .= $hidepatrolled ?
' AND rc_patrolled=0' : '';
131 $hidem .= is_null( $namespace ) ?
'' : ' AND rc_namespace' . ($invert ?
'!=' : '=') . $namespace;
133 // This is the big thing!
135 $uid = $wgUser->getID();
138 $sql2 = "SELECT * FROM $recentchanges FORCE INDEX (rc_timestamp) " .
139 ($uid ?
"LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
140 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
141 "ORDER BY rc_timestamp DESC";
142 $sql2 = $dbr->limitResult($sql2, $limit, 0);
143 $res = $dbr->query( $sql2, $fname );
145 // Fetch results, prepare a batch link existence check query
147 $batch = new LinkBatch
;
148 while( $row = $dbr->fetchObject( $res ) ){
150 if ( !$feedFormat ) {
152 $title = Title
::makeTitleSafe( NS_USER
, $row->rc_user_text
);
153 $batch->addObj( $title );
156 $title = Title
::makeTitleSafe( NS_USER_TALK
, $row->rc_user_text
);
157 $batch->addObj( $title );
161 $dbr->freeResult( $res );
164 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
169 // Run existence checks
173 if ( !$specialPage->including() ) {
174 $wgOut->addWikiText( wfMsgForContent( "recentchangestext" ) );
176 // Dump everything here
177 $nondefaults = array();
179 wfAppendToArrayIfNotDefault( 'days', $days, $defaults, $nondefaults);
180 wfAppendToArrayIfNotDefault( 'limit', $limit , $defaults, $nondefaults);
181 wfAppendToArrayIfNotDefault( 'hideminor', $hideminor, $defaults, $nondefaults);
182 wfAppendToArrayIfNotDefault( 'hidebots', $hidebots, $defaults, $nondefaults);
183 wfAppendToArrayIfNotDefault( 'hideliu', $hideliu, $defaults, $nondefaults);
184 wfAppendToArrayIfNotDefault( 'hidepatrolled', $hidepatrolled, $defaults, $nondefaults);
185 wfAppendToArrayIfNotDefault( 'from', $from, $defaults, $nondefaults);
186 wfAppendToArrayIfNotDefault( 'namespace', $namespace, $defaults, $nondefaults);
187 wfAppendToArrayIfNotDefault( 'invert', $invert, $defaults, $nondefaults);
189 // Add end of the texts
190 $wgOut->addHTML( '<div class="rcoptions">' . rcOptionsPanel( $defaults, $nondefaults ) . "\n" );
191 $wgOut->addHTML( rcNamespaceForm( $namespace, $invert, $nondefaults) . '</div>'."\n");
194 // And now for the content
195 $sk = $wgUser->getSkin();
196 $wgOut->setSyndicated( true );
198 $list = ChangesList
::newFromUser( $wgUser );
200 $s = $list->beginRecentChangesList();
202 foreach( $rows as $obj ){
207 if ( ! ( $hideminor && $obj->rc_minor
) &&
208 ! ( $hidepatrolled && $obj->rc_patrolled
) ) {
209 $rc = RecentChange
::newFromRow( $obj );
210 $rc->counter
= $counter++
;
212 if ($wgShowUpdatedMarker
213 && !empty( $obj->wl_notificationtimestamp
)
214 && ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
)) {
215 $rc->notificationtimestamp
= true;
217 $rc->notificationtimestamp
= false;
220 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
221 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title
) ."' AND wl_namespace=$obj->rc_namespace" ;
222 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
223 $x = $dbr->fetchObject( $res3 );
224 $rc->numberofWatchingusers
= $x->n
;
226 $rc->numberofWatchingusers
= 0;
228 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
232 $s .= $list->endRecentChangesList();
233 $wgOut->addHTML( $s );
237 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
238 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
239 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
241 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
242 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
246 $timekey = "$wgDBname:rcfeed:$feedFormat:timestamp";
247 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
249 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
250 ' [' . $wgContLanguageCode . ']';
251 $feed = new $wgFeedClasses[$feedFormat](
253 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
254 $wgTitle->getFullUrl() );
257 * Bumping around loading up diffs can be pretty slow, so where
258 * possible we want to cache the feed output so the next visitor
262 if( ( $wgFeedCacheTimeout > 0 ) && ( $feedLastmod = $messageMemc->get( $timekey ) ) ) {
264 * If the cached feed was rendered very recently, we may
265 * go ahead and use it even if there have been edits made
266 * since it was rendered. This keeps a swarm of requests
267 * from being too bad on a super-frequently edited wiki.
269 if( time() - wfTimestamp( TS_UNIX
, $feedLastmod )
270 < $wgFeedCacheTimeout
271 ||
wfTimestamp( TS_UNIX
, $feedLastmod )
272 > wfTimestamp( TS_UNIX
, $lastmod ) ) {
273 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
274 $cachedFeed = $messageMemc->get( $key );
276 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
279 if( is_string( $cachedFeed ) ) {
280 wfDebug( "RC: Outputting cached feed\n" );
281 $feed->httpHeaders();
284 wfDebug( "RC: rendering new feed and caching it\n" );
286 rcDoOutputFeed( $rows, $feed );
287 $cachedFeed = ob_get_contents();
290 $expire = 3600 * 24; # One day
291 $messageMemc->set( $key, $cachedFeed );
292 $messageMemc->set( $timekey, wfTimestamp( TS_MW
), $expire );
297 function rcDoOutputFeed( $rows, &$feed ) {
298 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
299 $fname = 'rcDoOutputFeed';
300 wfProfileIn( $fname );
304 # Merge adjacent edits by one user
307 foreach( $rows as $obj ) {
309 $obj->rc_namespace
>= 0 &&
310 $obj->rc_cur_id
== $sorted[$n-1]->rc_cur_id
&&
311 $obj->rc_user_text
== $sorted[$n-1]->rc_user_text
) {
312 $sorted[$n-1]->rc_last_oldid
= $obj->rc_last_oldid
;
320 foreach( $sorted as $obj ) {
321 $title = Title
::makeTitle( $obj->rc_namespace
, $obj->rc_title
);
322 $talkpage = $title->getTalkPage();
323 $item = new FeedItem(
324 $title->getPrefixedText(),
325 rcFormatDiff( $obj ),
326 $title->getFullURL(),
329 $talkpage->getFullURL()
331 $feed->outItem( $item );
334 wfProfileOut( $fname );
340 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
341 global $wgUser, $wgLang, $wgContLang;
342 $sk = $wgUser->getSkin();
343 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
344 ($lim ?
$wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
345 ($d ?
"days={$d}&" : '') . 'limit='.$lim );
352 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
353 global $wgUser, $wgLang, $wgContLang;
354 $sk = $wgUser->getSkin();
355 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
356 ($d ?
$wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
357 ($lim ?
'&limit='.$lim : '') );
362 * Used by Recentchangeslinked
364 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
365 $botLink = '', $liuLink = '', $patrLink = '' ) {
366 if ($more != '') $more .= '&';
367 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
368 rcCountLink( 100, $days, $page, $more ) . ' | ' .
369 rcCountLink( 250, $days, $page, $more ) . ' | ' .
370 rcCountLink( 500, $days, $page, $more ) .
371 ( $doall ?
( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
372 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
373 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
374 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
375 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
376 rcDaysLink( $limit, 30, $page, $more ) .
377 ( $doall ?
( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
378 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
379 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
385 * Makes change an option link which carries all the other options
387 function makeOptionsLink( $title, $override, $options ) {
388 global $wgUser, $wgLang, $wgContLang;
389 $sk = $wgUser->getSkin();
390 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
391 $title, wfArrayToCGI( $override, $options ) );
395 * Creates the options panel
397 function rcOptionsPanel( $defaults, $nondefaults ) {
400 $options = $nondefaults +
$defaults;
402 if( $options['from'] )
403 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $options['limit'] ), $wgLang->timeanddate( $options['from'], true ) );
405 $note = wfMsg( 'rcnote', $wgLang->formatNum( $options['limit'] ), $wgLang->formatNum( $options['days'] ) );
409 $options_limit = array(50, 100, 250, 500);
411 while ( $i+
1 < count($options_limit) ) {
412 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $nondefaults) . ' | ' ;
415 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $nondefaults) ;
417 // day links, reset 'from' to none
419 $options_days = array(1, 3, 7, 14, 30);
421 while ( $i+
1 < count($options_days) ) {
422 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $nondefaults) . ' | ' ;
425 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $nondefaults) ;
428 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
429 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
430 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
431 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
432 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
433 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
434 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
435 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
436 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
438 $hl = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
440 // show from this onward link
441 $now = $wgLang->timeanddate( wfTimestampNow(), true );
442 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
444 $rclinks = wfMsg( 'rclinks', $cl, $dl, $hl );
445 $rclistfrom = wfMsg( 'rclistfrom', $tl );
446 return "$note<br />$rclinks<br />$rclistfrom";
451 * Creates the choose namespace selection
455 * @param mixed $namespace The key of the currently selected namespace, empty string
457 * @param bool $invert Whether to invert the namespace selection
458 * @param array $nondefaults An array of non default options to be remembered
462 function rcNamespaceForm ( $namespace, $invert, $nondefaults ) {
463 global $wgContLang, $wgScript;
464 $t = Title
::makeTitle( NS_SPECIAL
, 'Recentchanges' );
466 $namespaceselect = HTMLnamespaceselector($namespace, '');
467 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
468 $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ?
' checked="checked"' : '' ) . ' />';
470 $out = "<div class='namespacesettings'><form method='get' action='{$wgScript}'>\n";
472 foreach ( $nondefaults as $key => $value ) {
473 if ($key != 'namespace' && $key != 'invert')
474 $out .= wfElement('input', array( 'type' => 'hidden', 'name' => $key, 'value' => $value));
477 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
479 <div id='nsselect' class='recentchanges'>
480 <label for='namespace'>" . wfMsgHtml('namespace') . "</label>
481 {$namespaceselect}{$submitbutton}{$invertbox} <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>\n</div>";
482 $out .= '</form></div>';
488 * Format a diff for the newsfeed
490 function rcFormatDiff( $row ) {
491 global $wgFeedDiffCutoff, $wgContLang;
492 $fname = 'rcFormatDiff';
493 wfProfileIn( $fname );
495 require_once( 'DifferenceEngine.php' );
496 $completeText = '<p>' . htmlspecialchars( $row->rc_comment
) . "</p>\n";
498 if( $row->rc_namespace
>= 0 ) {
499 if( $row->rc_last_oldid
) {
500 wfProfileIn( "$fname-dodiff" );
502 $titleObj = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
503 $de = new DifferenceEngine( $titleObj, $row->rc_last_oldid
, $row->rc_this_oldid
);
504 $diffText = $de->getDiff( wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp
) ),
505 wfMsg( 'currentrev' ) );
507 if ( strlen( $diffText ) > $wgFeedDiffCutoff ) {
509 $diffLink = $titleObj->escapeFullUrl(
510 'diff=' . $row->rc_this_oldid
.
511 '&oldid=' . $row->rc_last_oldid
);
512 $diffText = '<a href="' .
515 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
517 } elseif ( $diffText === false ) {
518 // Error in diff engine, probably a missing revision
519 $diffText = "<p>Can't load revision $row->rc_this_oldid</p>";
521 // Diff output fine, clean up any illegal UTF-8
522 $diffText = UtfNormal
::cleanUp( $diffText );
524 wfProfileOut( "$fname-dodiff" );
526 $rev = Revision
::newFromId( $row->rc_this_oldid
);
527 if( is_null( $rev ) ) {
530 $newtext = $rev->getText();
532 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
533 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
535 $completeText .= $diffText;
538 wfProfileOut( $fname );
539 return $completeText;