4 * Implements Special:Recentchanges
7 class SpecialRecentChanges
extends SpecialPage
{
8 public function __construct() {
9 SpecialPage
::SpecialPage( 'Recentchanges' );
10 $this->includable( true );
14 * Get a FormOptions object containing the default options
18 public function getDefaultOptions() {
19 $opts = new FormOptions();
21 $opts->add( 'days', (int)User
::getDefaultOption( 'rcdays' ) );
22 $opts->add( 'limit', (int)User
::getDefaultOption( 'rclimit' ) );
23 $opts->add( 'from', '' );
25 $opts->add( 'hideminor', false );
26 $opts->add( 'hidebots', true );
27 $opts->add( 'hideanons', false );
28 $opts->add( 'hideliu', false );
29 $opts->add( 'hidepatrolled', false );
30 $opts->add( 'hidemyself', false );
32 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
33 $opts->add( 'invert', false );
35 $opts->add( 'categories', '' );
36 $opts->add( 'categories_any', false );
42 * Get a FormOptions object with options as specified by the user
46 public function setup( $parameters ) {
47 global $wgUser, $wgRequest;
49 $opts = $this->getDefaultOptions();
50 $opts['days'] = (int)$wgUser->getOption( 'rcdays', $opts['days'] );
51 $opts['limit'] = (int)$wgUser->getOption( 'rclimit', $opts['limit'] );
52 $opts['hideminor'] = $wgUser->getOption( 'hideminor', $opts['hideminor'] );
53 $opts->fetchValuesFromRequest( $wgRequest );
55 // Give precedence to subpage syntax
56 if ( $parameters !== null ) {
57 $this->parseParameters( $parameters, $opts );
60 $opts->validateIntBounds( 'limit', 0, 5000 );
65 * Get a FormOptions object sepcific for feed requests
69 public function feedSetup() {
70 global $wgFeedLimit, $wgRequest;
71 $opts = $this->getDefaultOptions();
72 $opts->fetchValuesFromRequest( $wgRequest, array( 'days', 'limit', 'hideminor' ) );
73 $opts->validateIntBounds( 'limit', 0, $wgFeedLimit );
78 * Main execution point
80 * @param $parameters string
82 public function execute( $parameters ) {
83 global $wgRequest, $wgOut;
84 $feedFormat = $wgRequest->getVal( 'feed' );
86 # 10 seconds server-side caching max
87 $wgOut->setSquidMaxage( 10 );
89 $lastmod = $this->checkLastModified( $feedFormat );
90 if( $lastmod === false ){
94 $opts = $feedFormat ?
$this->feedSetup() : $this->setup( $parameters );
96 $this->outputHeader();
98 // Fetch results, prepare a batch link existence check query
100 $batch = new LinkBatch
;
101 $conds = $this->buildMainQueryConds( $opts );
102 $res = $this->doMainQuery( $conds, $opts );
103 if( $res === false ){
104 $this->doHeader( $opts );
107 $dbr = wfGetDB( DB_SLAVE
);
108 while( $row = $dbr->fetchObject( $res ) ){
110 if ( !$feedFormat ) {
111 // User page and talk links
112 $batch->add( NS_USER
, $row->rc_user_text
);
113 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
117 $dbr->freeResult( $res );
120 list( $feed, $feedObj ) = $this->getFeedObject( $feedFormat );
121 $feed->execute( $feedObj, $rows, $opts['limit'], $opts['hideminor'], $lastmod );
124 $this->webOutput( $rows, $opts );
130 * Return an array with a ChangesFeed object and ChannelFeed object
134 public function getFeedObject( $feedFormat ){
135 $feed = new ChangesFeed( $feedFormat, 'rcfeed' );
136 $feedObj = $feed->getFeedObject(
137 wfMsgForContent( 'recentchanges' ),
138 wfMsgForContent( 'recentchanges-feed-description' )
140 return array( $feed, $feedObj );
144 * Process $par and put options found if $opts
145 * Mainly used when including the page
148 * @param $opts FormOptions
150 public function parseParameters( $par, FormOptions
$opts ) {
151 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
152 foreach ( $bits as $bit ) {
153 if ( 'hidebots' === $bit ) $opts['hidebots'] = true;
154 if ( 'bots' === $bit ) $opts['hidebots'] = false;
155 if ( 'hideminor' === $bit ) $opts['hideminor'] = true;
156 if ( 'minor' === $bit ) $opts['hideminor'] = false;
157 if ( 'hideliu' === $bit ) $opts['hideliu'] = true;
158 if ( 'hidepatrolled' === $bit ) $opts['hidepatrolled'] = true;
159 if ( 'hideanons' === $bit ) $opts['hideanons'] = true;
160 if ( 'hidemyself' === $bit ) $opts['hidemyself'] = true;
162 if ( is_numeric( $bit ) ) $opts['limit'] = $bit;
165 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) $opts['limit'] = $m[1];
166 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) $opts['days'] = $m[1];
171 * Get last modified date, for client caching
172 * Don't use this if we are using the patrol feature, patrol changes don't
173 * update the timestamp
175 * @param $feedFormat String
176 * @return int or false
178 public function checkLastModified( $feedFormat ) {
179 global $wgUseRCPatrol, $wgOut;
180 $dbr = wfGetDB( DB_SLAVE
);
181 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __FUNCTION__
);
182 if ( $feedFormat ||
!$wgUseRCPatrol ) {
183 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
184 # Client cache fresh and headers sent, nothing more to do.
192 * Return an array of conditions depending of options set in $opts
194 * @param $opts FormOptions
197 public function buildMainQueryConds( FormOptions
$opts ) {
200 $dbr = wfGetDB( DB_SLAVE
);
203 # It makes no sense to hide both anons and logged-in users
204 # Where this occurs, force anons to be shown
206 if( $opts['hideanons'] && $opts['hideliu'] ){
207 # Check if the user wants to show bots only
208 if( $opts['hidebots'] ){
209 $opts['hideanons'] = false;
212 $opts['hidebots'] = false;
217 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
218 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
219 $cutoff = $dbr->timestamp( $cutoff_unixtime );
221 $fromValid = preg_match('/^[0-9]{14}$/', $opts['from']);
222 if( $fromValid && $opts['from'] > wfTimestamp(TS_MW
,$cutoff) ) {
223 $cutoff = $dbr->timestamp($opts['from']);
225 $opts->reset( 'from' );
228 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
231 $hidePatrol = $wgUser->useRCPatrol() && $opts['hidepatrolled'];
232 $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
233 $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
235 if ( $opts['hideminor'] ) $conds['rc_minor'] = 0;
236 if ( $opts['hidebots'] ) $conds['rc_bot'] = 0;
237 if ( $hidePatrol ) $conds['rc_patrolled'] = 0;
238 if ( $forcebot ) $conds['rc_bot'] = 1;
239 if ( $hideLoggedInUsers ) $conds[] = 'rc_user = 0';
240 if ( $hideAnonymousUsers ) $conds[] = 'rc_user != 0';
242 if( $opts['hidemyself'] ) {
243 if( $wgUser->getId() ) {
244 $conds[] = 'rc_user != ' . $dbr->addQuotes( $wgUser->getId() );
246 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() );
250 # Namespace filtering
251 if ( $opts['namespace'] !== '' ) {
252 if ( !$opts['invert'] ) {
253 $conds[] = 'rc_namespace = ' . $dbr->addQuotes( $opts['namespace'] );
255 $conds[] = 'rc_namespace != ' . $dbr->addQuotes( $opts['namespace'] );
265 * @param $conds array
266 * @param $opts FormOptions
267 * @return database result or false (for Recentchangeslinked only)
269 public function doMainQuery( $conds, $opts ) {
272 $tables = array( 'recentchanges' );
273 $join_conds = array();
275 $uid = $wgUser->getId();
276 $dbr = wfGetDB( DB_SLAVE
);
277 $limit = $opts['limit'];
278 $namespace = $opts['namespace'];
279 $invert = $opts['invert'];
281 // JOIN on watchlist for users
283 $tables[] = 'watchlist';
284 $join_conds = array( 'watchlist' => array('LEFT JOIN',"wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace") );
287 wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
289 // Is there either one namespace selected or excluded?
290 // Also, if this is "all" or main namespace, just use timestamp index.
291 if( is_null($namespace) ||
$invert ||
$namespace == NS_MAIN
) {
292 $res = $dbr->select( $tables, '*', $conds, __METHOD__
,
293 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
294 'USE INDEX' => array('recentchanges' => 'rc_timestamp') ),
296 // We have a new_namespace_time index! UNION over new=(0,1) and sort result set!
299 $sqlNew = $dbr->selectSQLText( $tables, '*',
300 array( 'rc_new' => 1 ) +
$conds,
302 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
303 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
306 $sqlOld = $dbr->selectSQLText( $tables, '*',
307 array( 'rc_new' => 0 ) +
$conds,
309 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
310 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
312 # Join the two fast queries, and sort the result set
313 $sql = "($sqlNew) UNION ($sqlOld) ORDER BY rc_timestamp DESC LIMIT $limit";
314 $res = $dbr->query( $sql, __METHOD__
);
321 * Send output to $wgOut, only called if not used feeds
323 * @param $rows array of database rows
324 * @param $opts FormOptions
326 public function webOutput( $rows, $opts ) {
327 global $wgOut, $wgUser, $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
328 global $wgAllowCategorizedRecentChanges;
330 $limit = $opts['limit'];
332 if ( !$this->including() ) {
333 // Output options box
334 $this->doHeader( $opts );
337 // And now for the content
338 $wgOut->setSyndicated( true );
340 $list = ChangesList
::newFromUser( $wgUser );
342 if ( $wgAllowCategorizedRecentChanges ) {
343 $this->filterByCategories( $rows, $opts );
346 $s = $list->beginRecentChangesList();
349 $showWatcherCount = $wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' );
350 $watcherCache = array();
352 $dbr = wfGetDB( DB_SLAVE
);
354 foreach( $rows as $obj ){
359 if ( ! ( $opts['hideminor'] && $obj->rc_minor
) &&
360 ! ( $opts['hidepatrolled'] && $obj->rc_patrolled
) ) {
361 $rc = RecentChange
::newFromRow( $obj );
362 $rc->counter
= $counter++
;
364 if ($wgShowUpdatedMarker
365 && !empty( $obj->wl_notificationtimestamp
)
366 && ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
)) {
367 $rc->notificationtimestamp
= true;
369 $rc->notificationtimestamp
= false;
372 $rc->numberofWatchingusers
= 0; // Default
373 if ($showWatcherCount && $obj->rc_namespace
>= 0) {
374 if (!isset($watcherCache[$obj->rc_namespace
][$obj->rc_title
])) {
375 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
376 $dbr->selectField( 'watchlist',
379 'wl_namespace' => $obj->rc_namespace
,
380 'wl_title' => $obj->rc_title
,
382 __METHOD__
. '-watchers' );
384 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
386 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
390 $s .= $list->endRecentChangesList();
391 $wgOut->addHTML( $s );
395 * Return the text to be displayed above the changes
397 * @param $opts FormOptions
398 * @return String: XHTML
400 public function doHeader( $opts ) {
401 global $wgScript, $wgOut;
403 $this->setTopText( $wgOut, $opts );
405 $defaults = $opts->getAllValues();
406 $nondefaults = $opts->getChangedValues();
407 $opts->consumeValues( array( 'namespace', 'invert' ) );
410 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
413 $extraOpts = $this->getExtraOptions( $opts );
415 $out = Xml
::openElement( 'table' );
416 foreach ( $extraOpts as $optionRow ) {
417 $out .= Xml
::openElement( 'tr' );
418 if ( is_array($optionRow) ) {
419 $out .= Xml
::tags( 'td', null, $optionRow[0] );
420 $out .= Xml
::tags( 'td', null, $optionRow[1] );
422 $out .= Xml
::tags( 'td', array( 'colspan' => 2 ), $optionRow );
424 $out .= Xml
::closeElement( 'tr' );
426 $out .= Xml
::closeElement( 'table' );
428 $unconsumed = $opts->getUnconsumedValues();
429 foreach ( $unconsumed as $key => $value ) {
430 $out .= Xml
::hidden( $key, $value );
433 $t = $this->getTitle();
434 $out .= Xml
::hidden( 'title', $t->getPrefixedText() );
435 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
437 $panelString = implode( "\n", $panel );
440 Xml
::fieldset( wfMsg( strtolower( $this->mName
) ), $panelString, array( 'class' => 'rcoptions' ) )
443 $this->setBottomText( $wgOut, $opts );
447 * Get options to be displayed in a form
449 * @param $opts FormOptions
452 function getExtraOptions( $opts ){
453 $extraOpts = array();
454 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
456 global $wgAllowCategorizedRecentChanges;
457 if ( $wgAllowCategorizedRecentChanges ) {
458 $extraOpts['category'] = $this->categoryFilterForm( $opts );
461 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
462 $extraOpts['submit'] = Xml
::submitbutton( wfMsg('allpagessubmit') );
467 * Send the text to be displayed above the options
469 * @param $out OutputPage
470 * @param $opts FormOptions
472 function setTopText( &$out, $opts ){
473 $out->addWikiText( wfMsgForContentNoTrans( 'recentchangestext' ) );
477 * Send the text to be displayed after the options, for use in
478 * Recentchangeslinked
480 * @param $out OutputPage
481 * @param $opts FormOptions
483 function setBottomText( &$out, $opts ){}
486 * Creates the choose namespace selection
488 * @param $opts FormOptions
491 protected function namespaceFilterForm( FormOptions
$opts ) {
492 $nsSelect = HTMLnamespaceselector( $opts['namespace'], '' );
493 $nsLabel = Xml
::label( wfMsg('namespace'), 'namespace' );
494 $invert = Xml
::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $opts['invert'] );
495 return array( $nsLabel, "$nsSelect $invert" );
499 * Create a input to filter changes by categories
501 * @param $opts FormOptions
504 protected function categoryFilterForm( FormOptions
$opts ) {
505 list( $label, $input ) = Xml
::inputLabelSep( wfMsg('rc_categories'),
506 'categories', 'mw-categories', false, $opts['categories'] );
508 $input .= ' ' . Xml
::checkLabel( wfMsg('rc_categories_any'),
509 'categories_any', 'mw-categories_any', $opts['categories_any'] );
511 return array( $label, $input );
515 * Filter $rows by categories set in $opts
517 * @param $rows array of database rows
518 * @param $opts FormOptions
520 function filterByCategories( &$rows, FormOptions
$opts ) {
521 $categories = array_map( 'trim', explode( "|" , $opts['categories'] ) );
523 if( empty($categories) ) {
529 foreach ( $categories as $cat ) {
531 if ( $cat == "" ) continue;
538 foreach ( $rows AS $k => $r ) {
539 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
540 $id = $nt->getArticleID();
541 if ( $id == 0 ) continue; # Page might have been deleted...
542 if ( !in_array($id, $articles) ) {
545 if ( !isset($a2r[$id]) ) {
552 if ( !count($articles) ||
!count($cats) )
556 $c = new Categoryfinder
;
557 $c->seed( $articles, $cats, $opts['categories_any'] ?
"OR" : "AND" ) ;
562 foreach ( $match AS $id ) {
563 foreach ( $a2r[$id] AS $rev ) {
565 $newrows[$k] = $rows[$k];
572 * Makes change an option link which carries all the other options
573 * @param $title see Title
577 function makeOptionsLink( $title, $override, $options, $active = false ) {
579 $sk = $wgUser->getSkin();
580 return $sk->makeKnownLinkObj( $this->getTitle(), htmlspecialchars( $title ),
581 wfArrayToCGI( $override, $options ), '', '', $active ?
'style="font-weight: bold;"' : '' );
585 * Creates the options panel.
586 * @param $defaults array
587 * @param $nondefaults array
589 function optionsPanel( $defaults, $nondefaults ) {
590 global $wgLang, $wgUser, $wgRCLinkLimits, $wgRCLinkDays;
592 $options = $nondefaults +
$defaults;
594 if( $options['from'] )
595 $note = wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
596 $wgLang->formatNum( $options['limit'] ),
597 $wgLang->timeanddate( $options['from'], true ) );
599 $note = wfMsgExt( 'rcnote', array( 'parseinline' ),
600 $wgLang->formatNum( $options['limit'] ),
601 $wgLang->formatNum( $options['days'] ),
602 $wgLang->timeAndDate( wfTimestampNow(), true ) );
604 # Sort data for display and make sure it's unique after we've added user data.
605 $wgRCLinkLimits[] = $options['limit'];
606 $wgRCLinkDays[] = $options['days'];
607 sort($wgRCLinkLimits);
609 $wgRCLinkLimits = array_unique($wgRCLinkLimits);
610 $wgRCLinkDays = array_unique($wgRCLinkDays);
613 foreach( $wgRCLinkLimits as $value ) {
614 $cl[] = $this->makeOptionsLink( $wgLang->formatNum( $value ),
615 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] ) ;
617 $cl = implode( ' | ', $cl);
619 // day links, reset 'from' to none
620 foreach( $wgRCLinkDays as $value ) {
621 $dl[] = $this->makeOptionsLink( $wgLang->formatNum( $value ),
622 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] ) ;
624 $dl = implode( ' | ', $dl);
628 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ));
629 $minorLink = $this->makeOptionsLink( $showhide[1-$options['hideminor']],
630 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
631 $botLink = $this->makeOptionsLink( $showhide[1-$options['hidebots']],
632 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
633 $anonsLink = $this->makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
634 array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
635 $liuLink = $this->makeOptionsLink( $showhide[1-$options['hideliu']],
636 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
637 $patrLink = $this->makeOptionsLink( $showhide[1-$options['hidepatrolled']],
638 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
639 $myselfLink = $this->makeOptionsLink( $showhide[1-$options['hidemyself']],
640 array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
642 $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
643 $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
644 $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
645 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
646 if( $wgUser->useRCPatrol() )
647 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
648 $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
649 $hl = implode( ' | ', $links );
651 // show from this onward link
652 $now = $wgLang->timeanddate( wfTimestampNow(), true );
653 $tl = $this->makeOptionsLink( $now, array( 'from' => wfTimestampNow()), $nondefaults );
655 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter'),
657 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter'), $tl );
658 return "$note<br />$rclinks<br />$rclistfrom";