ac4a8c5077fd42979ba8e3c9fb1c451befa81873
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;
22 $fname = 'wfSpecialRecentchanges';
24 # Get query parameters
25 $feedFormat = $wgRequest->getVal( 'feed' );
28 /* int */ 'days' => $wgUser->getDefaultOption('rcdays'),
29 /* int */ 'limit' => $wgUser->getDefaultOption('rclimit'),
30 /* bool */ 'hideminor' => false,
31 /* bool */ 'hidebots' => true,
32 /* bool */ 'hideliu' => false,
33 /* bool */ 'hidepatrolled' => false,
34 /* text */ 'from' => '',
35 /* text */ 'namespace' => null,
36 /* bool */ 'invert' => false,
42 $days = $wgUser->getOption( 'rcdays' );
43 if ( !$days ) { $days = $defaults['days']; }
44 $days = $wgRequest->getInt( 'days', $days );
46 $limit = $wgUser->getOption( 'rclimit' );
47 if ( !$limit ) { $limit = $defaults['limit']; }
49 # list( $limit, $offset ) = wfCheckLimits( 100, 'rclimit' );
50 $limit = $wgRequest->getInt( 'limit', $limit );
52 /* order of selection: url > preferences > default */
53 $hideminor = $wgRequest->getBool( 'hideminor', $wgUser->getOption( 'hideminor') ?
true : $defaults['hideminor'] );
56 # As a feed, use limited settings only
59 if( $limit > $wgFeedLimit ) {
60 $options['limit'] = $wgFeedLimit;
65 $namespace = $wgRequest->getIntOrNull( 'namespace' );
66 $invert = $wgRequest->getBool( 'invert', $defaults['invert'] );
67 $hidebots = $wgRequest->getBool( 'hidebots', $defaults['hidebots'] );
68 $hideliu = $wgRequest->getBool( 'hideliu', $defaults['hideliu'] );
69 $hidepatrolled = $wgRequest->getBool( 'hidepatrolled', $defaults['hidepatrolled'] );
70 $from = $wgRequest->getVal( 'from', $defaults['from'] );
72 # Get query parameters from path
74 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
75 foreach ( $bits as $bit ) {
76 if ( 'hidebots' == $bit ) $hidebots = 1;
77 if ( 'bots' == $bit ) $hidebots = 0;
78 if ( 'hideminor' == $bit ) $hideminor = 1;
79 if ( 'minor' == $bit ) $hideminor = 0;
80 if ( 'hideliu' == $bit ) $hideliu = 1;
81 if ( 'hidepatrolled' == $bit ) $hidepatrolled = 1;
83 if ( is_numeric( $bit ) ) {
87 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) {
91 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) {
98 if ( $limit < 0 ||
$limit > 5000 ) $limit = $defaults['limit'];
101 # Database connection and caching
102 $dbr =& wfGetDB( DB_SLAVE
);
103 extract( $dbr->tableNames( 'recentchanges', 'watchlist' ) );
106 $cutoff_unixtime = time() - ( $days * 86400 );
107 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
108 $cutoff = $dbr->timestamp( $cutoff_unixtime );
109 if(preg_match('/^[0-9]{14}$/', $from) and $from > wfTimestamp(TS_MW
,$cutoff)) {
110 $cutoff = $dbr->timestamp($from);
112 $from = $defaults['from'];
115 # 10 seconds server-side caching max
116 $wgOut->setSquidMaxage( 10 );
118 # Get last modified date, for client caching
119 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
120 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, $fname );
121 if ( $feedFormat ||
!$wgUseRCPatrol ) {
122 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
123 # Client cache fresh and headers sent, nothing more to do.
128 $hidem = $hideminor ?
'AND rc_minor=0' : '';
129 $hidem .= $hidebots ?
' AND rc_bot=0' : '';
130 $hidem .= $hideliu ?
' AND rc_user=0' : '';
131 $hidem .= $hidepatrolled ?
' AND rc_patrolled=0' : '';
132 $hidem .= is_null( $namespace ) ?
'' : ' AND rc_namespace' . ($invert ?
'!=' : '=') . $namespace;
134 // This is the big thing!
136 $uid = $wgUser->getID();
139 $sql2 = "SELECT * FROM $recentchanges FORCE INDEX (rc_timestamp) " .
140 ($uid ?
"LEFT OUTER JOIN $watchlist ON wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace " : "") .
141 "WHERE rc_timestamp > '{$cutoff}' {$hidem} " .
142 "ORDER BY rc_timestamp DESC";
143 $sql2 = $dbr->limitResult($sql2, $limit, 0);
144 $res = $dbr->query( $sql2, $fname );
146 // Fetch results, prepare a batch link existence check query
148 $batch = new LinkBatch
;
149 while( $row = $dbr->fetchObject( $res ) ){
151 if ( !$feedFormat ) {
153 $title = Title
::makeTitleSafe( NS_USER
, $row->rc_user_text
);
154 $batch->addObj( $title );
157 $title = Title
::makeTitleSafe( NS_USER_TALK
, $row->rc_user_text
);
158 $batch->addObj( $title );
162 $dbr->freeResult( $res );
165 rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod );
170 // Run existence checks
171 $batch->execute( $wgLinkCache );
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>'."\n");
195 // And now for the content
196 $sk = $wgUser->getSkin();
197 $wgOut->setSyndicated( true );
199 $list = ChangesList
::newFromUser( $wgUser );
201 $s = $list->beginRecentChangesList();
203 foreach( $rows as $obj ){
208 if ( ! ( $hideminor && $obj->rc_minor
) &&
209 ! ( $hidepatrolled && $obj->rc_patrolled
) ) {
210 $rc = RecentChange
::newFromRow( $obj );
211 $rc->counter
= $counter++
;
213 if ($wgShowUpdatedMarker
214 && !empty( $obj->wl_notificationtimestamp
)
215 && ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
)) {
216 $rc->notificationtimestamp
= true;
218 $rc->notificationtimestamp
= false;
221 if ($wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' )) {
222 $sql3 = "SELECT COUNT(*) AS n FROM $watchlist WHERE wl_title='" . $dbr->strencode($obj->rc_title
) ."' AND wl_namespace=$obj->rc_namespace" ;
223 $res3 = $dbr->query( $sql3, 'wfSpecialRecentChanges');
224 $x = $dbr->fetchObject( $res3 );
225 $rc->numberofWatchingusers
= $x->n
;
227 $rc->numberofWatchingusers
= 0;
229 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
233 $s .= $list->endRecentChangesList();
234 $wgOut->addHTML( $s );
238 function rcOutputFeed( $rows, $feedFormat, $limit, $hideminor, $lastmod ) {
239 global $messageMemc, $wgDBname, $wgFeedCacheTimeout;
240 global $wgFeedClasses, $wgTitle, $wgSitename, $wgContLanguageCode;
242 if( !isset( $wgFeedClasses[$feedFormat] ) ) {
243 wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
247 $timekey = "$wgDBname:rcfeed:$feedFormat:timestamp";
248 $key = "$wgDBname:rcfeed:$feedFormat:limit:$limit:minor:$hideminor";
250 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'recentchanges' ) .
251 ' [' . $wgContLanguageCode . ']';
252 $feed = new $wgFeedClasses[$feedFormat](
254 htmlspecialchars( wfMsgForContent( 'recentchangestext' ) ),
255 $wgTitle->getFullUrl() );
258 * Bumping around loading up diffs can be pretty slow, so where
259 * possible we want to cache the feed output so the next visitor
263 if( ( $wgFeedCacheTimeout > 0 ) && ( $feedLastmod = $messageMemc->get( $timekey ) ) ) {
265 * If the cached feed was rendered very recently, we may
266 * go ahead and use it even if there have been edits made
267 * since it was rendered. This keeps a swarm of requests
268 * from being too bad on a super-frequently edited wiki.
270 if( time() - wfTimestamp( TS_UNIX
, $feedLastmod )
271 < $wgFeedCacheTimeout
272 ||
wfTimestamp( TS_UNIX
, $feedLastmod )
273 > wfTimestamp( TS_UNIX
, $lastmod ) ) {
274 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
275 $cachedFeed = $messageMemc->get( $key );
277 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
280 if( is_string( $cachedFeed ) ) {
281 wfDebug( "RC: Outputting cached feed\n" );
282 $feed->httpHeaders();
285 wfDebug( "RC: rendering new feed and caching it\n" );
287 rcDoOutputFeed( $rows, $feed );
288 $cachedFeed = ob_get_contents();
291 $expire = 3600 * 24; # One day
292 $messageMemc->set( $key, $cachedFeed );
293 $messageMemc->set( $timekey, wfTimestamp( TS_MW
), $expire );
298 function rcDoOutputFeed( $rows, &$feed ) {
299 global $wgSitename, $wgFeedClasses, $wgContLanguageCode;
300 $fname = 'rcDoOutputFeed';
301 wfProfileIn( $fname );
305 # Merge adjacent edits by one user
308 foreach( $rows as $obj ) {
310 $obj->rc_namespace
>= 0 &&
311 $obj->rc_cur_id
== $sorted[$n-1]->rc_cur_id
&&
312 $obj->rc_user_text
== $sorted[$n-1]->rc_user_text
) {
313 $sorted[$n-1]->rc_last_oldid
= $obj->rc_last_oldid
;
321 foreach( $sorted as $obj ) {
322 $title = Title
::makeTitle( $obj->rc_namespace
, $obj->rc_title
);
323 $talkpage = $title->getTalkPage();
324 $item = new FeedItem(
325 $title->getPrefixedText(),
326 rcFormatDiff( $obj ),
327 $title->getFullURL(),
330 $talkpage->getFullURL()
332 $feed->outItem( $item );
335 wfProfileOut( $fname );
341 function rcCountLink( $lim, $d, $page='Recentchanges', $more='' ) {
342 global $wgUser, $wgLang, $wgContLang;
343 $sk = $wgUser->getSkin();
344 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
345 ($lim ?
$wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
346 ($d ?
"days={$d}&" : '') . 'limit='.$lim );
353 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='' ) {
354 global $wgUser, $wgLang, $wgContLang;
355 $sk = $wgUser->getSkin();
356 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
357 ($d ?
$wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
358 ($lim ?
'&limit='.$lim : '') );
363 * Used by Recentchangeslinked
365 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
366 $botLink = '', $liuLink = '', $patrLink = '' ) {
367 if ($more != '') $more .= '&';
368 $cl = rcCountLink( 50, $days, $page, $more ) . ' | ' .
369 rcCountLink( 100, $days, $page, $more ) . ' | ' .
370 rcCountLink( 250, $days, $page, $more ) . ' | ' .
371 rcCountLink( 500, $days, $page, $more ) .
372 ( $doall ?
( ' | ' . rcCountLink( 0, $days, $page, $more ) ) : '' );
373 $dl = rcDaysLink( $limit, 1, $page, $more ) . ' | ' .
374 rcDaysLink( $limit, 3, $page, $more ) . ' | ' .
375 rcDaysLink( $limit, 7, $page, $more ) . ' | ' .
376 rcDaysLink( $limit, 14, $page, $more ) . ' | ' .
377 rcDaysLink( $limit, 30, $page, $more ) .
378 ( $doall ?
( ' | ' . rcDaysLink( $limit, 0, $page, $more ) ) : '' );
379 $shm = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
380 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
386 * Makes change an option link which carries all the other options
388 function makeOptionsLink( $title, $override, $options ) {
389 global $wgUser, $wgLang, $wgContLang;
390 $sk = $wgUser->getSkin();
391 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
392 $title, wfArrayToCGI( $override, $options ) );
396 * Creates the options panel
398 function rcOptionsPanel( $defaults, $nondefaults ) {
401 $options = $nondefaults +
$defaults;
403 if( $options['from'] )
404 $note = wfMsg( 'rcnotefrom', $wgLang->formatNum( $options['limit'] ), $wgLang->timeanddate( $options['from'], true ) );
406 $note = wfMsg( 'rcnote', $wgLang->formatNum( $options['limit'] ), $wgLang->formatNum( $options['days'] ) );
410 $options_limit = array(50, 100, 250, 500);
412 while ( $i+
1 < count($options_limit) ) {
413 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $nondefaults) . ' | ' ;
416 $cl .= makeOptionsLink( $options_limit[$i], array( 'limit' => $options_limit[$i] ), $nondefaults) ;
418 // day links, reset 'from' to none
420 $options_days = array(1, 3, 7, 14, 30);
422 while ( $i+
1 < count($options_days) ) {
423 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $nondefaults) . ' | ' ;
426 $dl .= makeOptionsLink( $options_days[$i], array( 'days' => $options_days[$i], 'from' => '' ), $nondefaults) ;
429 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
430 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
431 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
432 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
433 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
434 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
435 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
436 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
437 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
439 $hl = wfMsg( 'showhideminor', $minorLink, $botLink, $liuLink, $patrLink );
441 // show from this onward link
442 $now = $wgLang->timeanddate( wfTimestampNow(), true );
443 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
445 $rclinks = wfMsg( 'rclinks', $cl, $dl, $hl );
446 $rclistfrom = wfMsg( 'rclistfrom', $tl );
447 return "$note<br />$rclinks<br />$rclistfrom";
452 * Creates the choose namespace selection
456 * @param mixed $namespace The key of the currently selected namespace, empty string
458 * @param bool $invert Whether to invert the namespace selection
459 * @param array $nondefaults An array of non default options to be remembered
463 function rcNamespaceForm ( $namespace, $invert, $nondefaults ) {
464 global $wgContLang, $wgScript;
465 $t = Title
::makeTitle( NS_SPECIAL
, 'Recentchanges' );
467 $namespaceselect = HTMLnamespaceselector($namespace, '');
468 $submitbutton = '<input type="submit" value="' . wfMsgHtml( 'allpagessubmit' ) . "\" />\n";
469 $invertbox = "<input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ?
' checked="checked"' : '' ) . ' />';
471 $out = "<div class='namespacesettings'><form method='get' action='{$wgScript}'>\n";
473 foreach ( $nondefaults as $key => $value ) {
474 if ($key != 'namespace' && $key != 'invert')
475 $out .= wfElement('input', array( 'type' => 'hidden', 'name' => $key, 'value' => $value));
478 $out .= '<input type="hidden" name="title" value="'.$t->getPrefixedText().'" />';
480 <div id='nsselect' class='recentchanges'>
481 <label for='namespace'>" . wfMsgHtml('namespace') . "</label>
482 {$namespaceselect}{$submitbutton}{$invertbox} <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>\n</div>";
483 $out .= '</form></div>';
489 * Format a diff for the newsfeed
491 function rcFormatDiff( $row ) {
492 global $wgFeedDiffCutoff, $wgContLang;
493 $fname = 'rcFormatDiff';
494 wfProfileIn( $fname );
496 require_once( 'DifferenceEngine.php' );
497 $completeText = '<p>' . htmlspecialchars( $row->rc_comment
) . "</p>\n";
499 if( $row->rc_namespace
>= 0 ) {
500 if( $row->rc_last_oldid
) {
501 wfProfileIn( "$fname-dodiff" );
503 $titleObj = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
504 $de = new DifferenceEngine( $titleObj, $row->rc_last_oldid
, $row->rc_this_oldid
);
505 $diffText = $de->getDiff( wfMsg( 'revisionasof', $wgContLang->timeanddate( $row->rc_timestamp
) ),
506 wfMsg( 'currentrev' ) );
508 if ( strlen( $diffText ) > $wgFeedDiffCutoff ) {
510 $diffLink = $titleObj->escapeFullUrl(
511 'diff=' . $row->rc_this_oldid
.
512 '&oldid=' . $row->rc_last_oldid
);
513 $diffText = '<a href="' .
516 htmlspecialchars( wfMsgForContent( 'difference' ) ) .
518 } elseif ( $diffText === false ) {
519 // Error in diff engine, probably a missing revision
520 $diffText = "<p>Can't load revision $row->rc_this_oldid</p>";
522 // Diff output fine, clean up any illegal UTF-8
523 $diffText = UtfNormal
::cleanUp( $diffText );
525 wfProfileOut( "$fname-dodiff" );
527 $rev = Revision
::newFromId( $row->rc_this_oldid
);
528 if( is_null( $rev ) ) {
531 $newtext = $rev->getText();
533 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
534 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
536 $completeText .= $diffText;
539 wfProfileOut( $fname );
540 return $completeText;