7 class SpecialRecentChanges
extends SpecialPage
{
8 public function __construct() {
9 SpecialPage
::SpecialPage( 'Recentchanges' );
10 $this->includable( true );
13 public function getDefaultOptions() {
14 $opts = new FormOptions();
16 $opts->add( 'days', (int)User
::getDefaultOption( 'rcdays' ) );
17 $opts->add( 'limit', (int)User
::getDefaultOption( 'rclimit' ) );
18 $opts->add( 'from', '' );
20 $opts->add( 'hideminor', false );
21 $opts->add( 'hidebots', true );
22 $opts->add( 'hideanons', false );
23 $opts->add( 'hideliu', false );
24 $opts->add( 'hidepatrolled', false );
25 $opts->add( 'hidemyself', false );
27 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
28 $opts->add( 'invert', false );
30 $opts->add( 'categories', '' );
31 $opts->add( 'categories_any', false );
36 public function setup( $parameters ) {
37 global $wgUser, $wgRequest;
39 $opts = $this->getDefaultOptions();
40 $opts['days'] = (int)$wgUser->getOption( 'rcdays', $opts['days'] );
41 $opts['limit'] = (int)$wgUser->getOption( 'rclimit', $opts['limit'] );
42 $opts['hideminor'] = $wgUser->getOption( 'hideminor', $opts['hideminor'] );
43 $opts->fetchValuesFromRequest( $wgRequest );
45 // Give precedence to subpage syntax
46 if ( $parameters !== null ) {
47 $this->parseParameters( $parameters, $opts );
50 $opts->validateIntBounds( 'limit', 0, 5000 );
54 public function feedSetup() {
55 global $wgFeedLimit, $wgRequest;
56 $opts = $this->getDefaultOptions();
57 $opts->fetchValuesFromRequest( $wgRequest, array( 'days', 'limit', 'hideminor' ) );
58 $opts->validateIntBounds( 'limit', 0, $wgFeedLimit );
62 public function execute( $parameters ) {
63 global $wgRequest, $wgOut;
64 $feedFormat = $wgRequest->getVal( 'feed' );
66 # 10 seconds server-side caching max
67 $wgOut->setSquidMaxage( 10 );
69 $lastmod = $this->checkLastModified( $feedFormat );
70 if( $lastmod === false ){
74 $opts = $feedFormat ?
$this->feedSetup() : $this->setup( $parameters );
77 // Fetch results, prepare a batch link existence check query
79 $batch = new LinkBatch
;
80 $conds = $this->buildMainQueryConds( $opts );
81 $res = $this->doMainQuery( $conds, $opts );
82 $dbr = wfGetDB( DB_SLAVE
);
83 while( $row = $dbr->fetchObject( $res ) ){
86 // User page and talk links
87 $batch->add( NS_USER
, $row->rc_user_text
);
88 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
92 $dbr->freeResult( $res );
95 $feed = new ChangesFeed( $feedFormat, 'rcfeed' );
96 $feedObj = $feed->getFeedObject(
97 wfMsgForContent( 'recentchanges' ),
98 wfMsgForContent( 'recentchanges-feed-description' )
100 $feed->execute( $feedObj, $rows, $opts['limit'], $opts['hideminor'], $lastmod );
103 $this->webOutput( $rows, $opts );
107 public function parseParameters( $par, FormOptions
$opts ) {
108 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
109 foreach ( $bits as $bit ) {
110 if ( 'hidebots' === $bit ) $opts['hidebots'] = true;
111 if ( 'bots' === $bit ) $opts['hidebots'] = false;
112 if ( 'hideminor' === $bit ) $opts['hideminor'] = true;
113 if ( 'minor' === $bit ) $opts['hideminor'] = false;
114 if ( 'hideliu' === $bit ) $opts['hideliu'] = true;
115 if ( 'hidepatrolled' === $bit ) $opts['hidepatrolled'] = true;
116 if ( 'hideanons' === $bit ) $opts['hideanons'] = true;
117 if ( 'hidemyself' === $bit ) $opts['hidemyself'] = true;
119 if ( is_numeric( $bit ) ) $opts['limit'] = $bit;
122 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) $opts['limit'] = $m[1];
123 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) $opts['days'] = $m[1];
127 # Get last modified date, for client caching
128 # Don't use this if we are using the patrol feature, patrol changes don't update the timestamp
129 public function checkLastModified( $feedFormat ) {
130 global $wgUseRCPatrol, $wgOut;
131 $dbr = wfGetDB( DB_SLAVE
);
132 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __FUNCTION__
);
133 if ( $feedFormat ||
!$wgUseRCPatrol ) {
134 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
135 # Client cache fresh and headers sent, nothing more to do.
142 public function buildMainQueryConds( FormOptions
$opts ) {
145 $dbr = wfGetDB( DB_SLAVE
);
148 # It makes no sense to hide both anons and logged-in users
149 # Where this occurs, force anons to be shown
151 if( $opts['hideanons'] && $opts['hideliu'] ){
152 # Check if the user wants to show bots only
153 if( $opts['hidebots'] ){
154 $opts['hideanons'] = false;
157 $opts['hidebots'] = false;
162 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
163 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
164 $cutoff = $dbr->timestamp( $cutoff_unixtime );
166 $fromValid = preg_match('/^[0-9]{14}$/', $opts['from']);
167 if( $fromValid && $opts['from'] > wfTimestamp(TS_MW
,$cutoff) ) {
168 $cutoff = $dbr->timestamp($opts['from']);
170 $opts->reset( 'from' );
173 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
176 $hidePatrol = $wgUser->useRCPatrol() && $opts['hidepatrolled'];
177 $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
178 $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
180 if ( $opts['hideminor'] ) $conds['rc_minor'] = 0;
181 if ( $opts['hidebots'] ) $conds['rc_bot'] = 0;
182 if ( $hidePatrol ) $conds['rc_patrolled'] = 0;
183 if ( $forcebot ) $conds['rc_bot'] = 1;
184 if ( $hideLoggedInUsers ) $conds[] = 'rc_user = 0';
185 if ( $hideAnonymousUsers ) $conds[] = 'rc_user != 0';
187 if( $opts['hidemyself'] ) {
188 if( $wgUser->getId() ) {
189 $conds[] = 'rc_user != ' . $dbr->addQuotes( $wgUser->getId() );
191 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() );
195 # Namespace filtering
196 if ( $opts['namespace'] !== '' ) {
197 if ( !$opts['invert'] ) {
198 $conds[] = 'rc_namespace = ' . $dbr->addQuotes( $opts['namespace'] );
200 $conds[] = 'rc_namespace != ' . $dbr->addQuotes( $opts['namespace'] );
207 public function doMainQuery( $conds, $opts ) {
210 $tables = array( 'recentchanges' );
211 $join_conds = array();
213 $uid = $wgUser->getId();
214 $dbr = wfGetDB( DB_SLAVE
);
215 $limit = $opts['limit'];
216 $namespace = $opts['namespace'];
217 $invert = $opts['invert'];
219 // JOIN on watchlist for users
220 if( $wgUser->getId() ) {
221 $tables[] = 'watchlist';
222 $join_conds = array( 'watchlist' => array('LEFT JOIN',"wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace") );
225 wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
227 // Is there either one namespace selected or excluded?
228 // Also, if this is "all" or main namespace, just use timestamp index.
229 if( is_null($namespace) ||
$invert ||
$namespace == NS_MAIN
) {
230 $res = $dbr->select( $tables, '*', $conds, __METHOD__
,
231 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
232 'USE INDEX' => array('recentchanges' => 'rc_timestamp') ),
234 // We have a new_namespace_time index! UNION over new=(0,1) and sort result set!
237 $sqlNew = $dbr->selectSQLText( $tables, '*',
238 array( 'rc_new' => 1 ) +
$conds,
240 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
241 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
244 $sqlOld = $dbr->selectSQLText( $tables, '*',
245 array( 'rc_new' => 0 ) +
$conds,
247 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
248 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
250 # Join the two fast queries, and sort the result set
251 $sql = "($sqlNew) UNION ($sqlOld) ORDER BY rc_timestamp DESC LIMIT $limit";
252 $res = $dbr->query( $sql, __METHOD__
);
258 public function webOutput( $rows, $opts ) {
259 global $wgOut, $wgUser, $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
260 global $wgAllowCategorizedRecentChanges;
262 $limit = $opts['limit'];
264 if ( !$this->including() ) {
265 // Output options box
266 $this->doHeader( $opts );
269 // And now for the content
270 $wgOut->setSyndicated( true );
272 $list = ChangesList
::newFromUser( $wgUser );
274 if ( $wgAllowCategorizedRecentChanges ) {
275 rcFilterByCategories( $rows, $opts );
278 $s = $list->beginRecentChangesList();
281 $showWatcherCount = $wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' );
282 $watcherCache = array();
284 $dbr = wfGetDB( DB_SLAVE
);
286 foreach( $rows as $obj ){
291 if ( ! ( $opts['hideminor'] && $obj->rc_minor
) &&
292 ! ( $opts['hidepatrolled'] && $obj->rc_patrolled
) ) {
293 $rc = RecentChange
::newFromRow( $obj );
294 $rc->counter
= $counter++
;
296 if ($wgShowUpdatedMarker
297 && !empty( $obj->wl_notificationtimestamp
)
298 && ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
)) {
299 $rc->notificationtimestamp
= true;
301 $rc->notificationtimestamp
= false;
304 $rc->numberofWatchingusers
= 0; // Default
305 if ($showWatcherCount && $obj->rc_namespace
>= 0) {
306 if (!isset($watcherCache[$obj->rc_namespace
][$obj->rc_title
])) {
307 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
308 $dbr->selectField( 'watchlist',
311 'wl_namespace' => $obj->rc_namespace
,
312 'wl_title' => $obj->rc_title
,
314 __METHOD__
. '-watchers' );
316 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
318 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
322 $s .= $list->endRecentChangesList();
323 $wgOut->addHTML( $s );
326 public function doHeader( $opts ) {
327 global $wgScript, $wgOut;
328 $wgOut->addWikiText( wfMsgForContentNoTrans( 'recentchangestext' ) );
330 $defaults = $opts->getAllValues();
331 $nondefaults = $opts->getChangedValues();
332 $opts->consumeValues( array( 'namespace', 'invert' ) );
335 $panel[] = rcOptionsPanel( $defaults, $nondefaults );
337 $extraOpts = array();
338 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
340 global $wgAllowCategorizedRecentChanges;
341 if ( $wgAllowCategorizedRecentChanges ) {
342 $extraOpts['category'] = $this->categoryFilterForm( $opts );
345 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
346 $extraOpts['submit'] = Xml
::submitbutton( wfMsg('allpagessubmit') );
348 $out = Xml
::openElement( 'table' );
349 foreach ( $extraOpts as $optionRow ) {
350 $out .= Xml
::openElement( 'tr' );
351 if ( is_array($optionRow) ) {
352 $out .= Xml
::tags( 'td', null, $optionRow[0] );
353 $out .= Xml
::tags( 'td', null, $optionRow[1] );
355 $out .= Xml
::tags( 'td', array( 'colspan' => 2 ), $optionRow );
357 $out .= Xml
::closeElement( 'tr' );
359 $out .= Xml
::closeElement( 'table' );
361 $unconsumed = $opts->getUnconsumedValues();
362 foreach ( $unconsumed as $key => $value ) {
363 $out .= Xml
::hidden( $key, $value );
366 $t = $this->getTitle();
367 $out .= Xml
::hidden( 'title', $t->getPrefixedText() );
368 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
370 $panelString = implode( "\n", $panel );
372 $wgOut->addHTML( '<div class="rcoptions">' . $panelString . '</div>' );
376 * Creates the choose namespace selection
380 protected function namespaceFilterForm( FormOptions
$opts ) {
381 $nsSelect = HTMLnamespaceselector( $opts['namespace'], '' );
382 $nsLabel = Xml
::label( wfMsg('namespace'), 'namespace' );
383 $invert = Xml
::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $opts['invert'] );
384 return array( $nsLabel, "$nsSelect $invert" );
387 protected function categoryFilterForm( FormOptions
$opts ) {
388 list( $label, $input ) = Xml
::inputLabelSep( wfMsg('rc_categories'),
389 'categories', 'mw-categories', false, $opts['categories'] );
391 $input .= ' ' . Xml
::checkLabel( wfMsg('rc_categories_any'),
392 'categories_any', 'mw-categories_any', $opts['categories_any'] );
394 return array( $label, $input );
399 function rcFilterByCategories ( &$rows, FormOptions
$opts ) {
400 $categories = array_map( 'trim', explode( "|" , $opts['categories'] ) );
402 if( empty($categories) ) {
408 foreach ( $categories as $cat ) {
410 if ( $cat == "" ) continue;
417 foreach ( $rows AS $k => $r ) {
418 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
419 $id = $nt->getArticleID();
420 if ( $id == 0 ) continue; # Page might have been deleted...
421 if ( !in_array($id, $articles) ) {
424 if ( !isset($a2r[$id]) ) {
431 if ( !count($articles) ||
!count($cats) )
435 $c = new Categoryfinder
;
436 $c->seed( $articles, $cats, $opts['categories_any'] ?
"OR" : "AND" ) ;
441 foreach ( $match AS $id ) {
442 foreach ( $a2r[$id] AS $rev ) {
444 $newrows[$k] = $rows[$k];
453 function rcCountLink( $lim, $d, $page='Recentchanges', $more='', $active = false ) {
454 global $wgUser, $wgLang, $wgContLang;
455 $sk = $wgUser->getSkin();
456 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
457 ($lim ?
$wgLang->formatNum( "{$lim}" ) : wfMsg( 'recentchangesall' ) ), "{$more}" .
458 ($d ?
"days={$d}&" : '') . 'limit='.$lim, '', '',
459 $active ?
'style="font-weight: bold;"' : '' );
466 function rcDaysLink( $lim, $d, $page='Recentchanges', $more='', $active = false ) {
467 global $wgUser, $wgLang, $wgContLang;
468 $sk = $wgUser->getSkin();
469 $s = $sk->makeKnownLink( $wgContLang->specialPage( $page ),
470 ($d ?
$wgLang->formatNum( "{$d}" ) : wfMsg( 'recentchangesall' ) ), $more.'days='.$d .
471 ($lim ?
'&limit='.$lim : ''), '', '',
472 $active ?
'style="font-weight: bold;"' : '' );
477 * Used by Recentchangeslinked
479 function rcDayLimitLinks( $days, $limit, $page='Recentchanges', $more='', $doall = false, $minorLink = '',
480 $botLink = '', $liuLink = '', $patrLink = '', $myselfLink = '' ) {
481 global $wgRCLinkLimits, $wgRCLinkDays;
482 if ($more != '') $more .= '&';
484 # Sort data for display and make sure it's unique after we've added user data.
485 # FIXME: why does this piss around with globals like this? Why is $limit added on globally?
486 $wgRCLinkLimits[] = $limit;
487 $wgRCLinkDays[] = $days;
488 sort($wgRCLinkLimits);
490 $wgRCLinkLimits = array_unique($wgRCLinkLimits);
491 $wgRCLinkDays = array_unique($wgRCLinkDays);
494 foreach( $wgRCLinkLimits as $countLink ) {
495 $cl[] = rcCountLink( $countLink, $days, $page, $more, $countLink == $limit );
497 if( $doall ) $cl[] = rcCountLink( 0, $days, $page, $more );
498 $cl = implode( ' | ', $cl);
501 foreach( $wgRCLinkDays as $daysLink ) {
502 $dl[] = rcDaysLink( $limit, $daysLink, $page, $more, $daysLink == $days );
504 if( $doall ) $dl[] = rcDaysLink( $limit, 0, $page, $more );
505 $dl = implode( ' | ', $dl);
507 $linkParts = array( 'minorLink' => 'minor', 'botLink' => 'bots', 'liuLink' => 'liu', 'patrLink' => 'patr', 'myselfLink' => 'mine' );
508 foreach( $linkParts as $linkVar => $linkMsg ) {
509 if( $
$linkVar != '' )
510 $links[] = wfMsgHtml( 'rcshowhide' . $linkMsg, $
$linkVar );
513 $shm = implode( ' | ', $links );
514 $note = wfMsg( 'rclinks', $cl, $dl, $shm );
520 * Makes change an option link which carries all the other options
521 * @param $title see Title
525 function makeOptionsLink( $title, $override, $options, $active = false ) {
526 global $wgUser, $wgContLang;
527 $sk = $wgUser->getSkin();
528 return $sk->makeKnownLink( $wgContLang->specialPage( 'Recentchanges' ),
529 htmlspecialchars( $title ), wfArrayToCGI( $override, $options ), '', '',
530 $active ?
'style="font-weight: bold;"' : '' );
534 * Creates the options panel.
536 * @param $nondefaults
538 function rcOptionsPanel( $defaults, $nondefaults ) {
539 global $wgLang, $wgUser, $wgRCLinkLimits, $wgRCLinkDays;
541 $options = $nondefaults +
$defaults;
543 if( $options['from'] )
544 $note = wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
545 $wgLang->formatNum( $options['limit'] ),
546 $wgLang->timeanddate( $options['from'], true ) );
548 $note = wfMsgExt( 'rcnote', array( 'parseinline' ),
549 $wgLang->formatNum( $options['limit'] ),
550 $wgLang->formatNum( $options['days'] ),
551 $wgLang->timeAndDate( wfTimestampNow(), true ) );
553 # Sort data for display and make sure it's unique after we've added user data.
554 $wgRCLinkLimits[] = $options['limit'];
555 $wgRCLinkDays[] = $options['days'];
556 sort($wgRCLinkLimits);
558 $wgRCLinkLimits = array_unique($wgRCLinkLimits);
559 $wgRCLinkDays = array_unique($wgRCLinkDays);
562 foreach( $wgRCLinkLimits as $value ) {
563 $cl[] = makeOptionsLink( $wgLang->formatNum( $value ),
564 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] ) ;
566 $cl = implode( ' | ', $cl);
568 // day links, reset 'from' to none
569 foreach( $wgRCLinkDays as $value ) {
570 $dl[] = makeOptionsLink( $wgLang->formatNum( $value ),
571 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] ) ;
573 $dl = implode( ' | ', $dl);
577 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
578 $minorLink = makeOptionsLink( $showhide[1-$options['hideminor']],
579 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
580 $botLink = makeOptionsLink( $showhide[1-$options['hidebots']],
581 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
582 $anonsLink = makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
583 array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
584 $liuLink = makeOptionsLink( $showhide[1-$options['hideliu']],
585 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
586 $patrLink = makeOptionsLink( $showhide[1-$options['hidepatrolled']],
587 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
588 $myselfLink = makeOptionsLink( $showhide[1-$options['hidemyself']],
589 array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
591 $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
592 $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
593 $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
594 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
595 if( $wgUser->useRCPatrol() )
596 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
597 $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
598 $hl = implode( ' | ', $links );
600 // show from this onward link
601 $now = $wgLang->timeanddate( wfTimestampNow(), true );
602 $tl = makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
604 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter'),
606 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter'), $tl );
607 return "$note<br />$rclinks<br />$rclistfrom";