4 * Implements Special:Recentchanges
7 class SpecialRecentChanges
extends SpecialPage
{
8 public function __construct() {
9 parent
::__construct( 'Recentchanges' );
10 $this->includable( true );
14 * Get a FormOptions object containing the default options
18 public function getDefaultOptions() {
20 $opts = new FormOptions();
22 $opts->add( 'days', (int)$wgUser->getOption( 'rcdays' ) );
23 $opts->add( 'limit', (int)$wgUser->getOption( 'rclimit' ) );
24 $opts->add( 'from', '' );
26 $opts->add( 'hideminor', $wgUser->getBoolOption( 'hideminor' ) );
27 $opts->add( 'hidebots', true );
28 $opts->add( 'hideanons', false );
29 $opts->add( 'hideliu', false );
30 $opts->add( 'hidepatrolled', $wgUser->getBoolOption( 'hidepatrolled' ) );
31 $opts->add( 'hidemyself', false );
33 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
34 $opts->add( 'invert', false );
36 $opts->add( 'categories', '' );
37 $opts->add( 'categories_any', false );
42 * Get a FormOptions object with options as specified by the user
46 public function setup( $parameters ) {
49 $opts = $this->getDefaultOptions();
50 $opts->fetchValuesFromRequest( $wgRequest );
52 // Give precedence to subpage syntax
53 if( $parameters !== null ) {
54 $this->parseParameters( $parameters, $opts );
57 $opts->validateIntBounds( 'limit', 0, 5000 );
62 * Get a FormOptions object sepcific for feed requests
66 public function feedSetup() {
67 global $wgFeedLimit, $wgRequest;
68 $opts = $this->getDefaultOptions();
69 $opts->fetchValuesFromRequest( $wgRequest, array( 'days', 'limit', 'hideminor' ) );
70 $opts->validateIntBounds( 'limit', 0, $wgFeedLimit );
75 * Main execution point
77 * @param $parameters string
79 public function execute( $parameters ) {
80 global $wgRequest, $wgOut;
81 $feedFormat = $wgRequest->getVal( 'feed' );
83 # 10 seconds server-side caching max
84 $wgOut->setSquidMaxage( 10 );
85 # Check if the client has a cached version
86 $lastmod = $this->checkLastModified( $feedFormat );
87 if( $lastmod === false ) {
91 $opts = $feedFormat ?
$this->feedSetup() : $this->setup( $parameters );
93 $this->outputHeader();
95 // Fetch results, prepare a batch link existence check query
97 $conds = $this->buildMainQueryConds( $opts );
98 $rows = $this->doMainQuery( $conds, $opts );
99 if( $rows === false ){
100 if( !$this->including() ) {
101 $this->doHeader( $opts );
107 $batch = new LinkBatch
;
108 foreach( $rows as $row ) {
109 $batch->add( NS_USER
, $row->rc_user_text
);
110 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
116 list( $feed, $feedObj ) = $this->getFeedObject( $feedFormat );
117 $feed->execute( $feedObj, $rows, $opts['limit'], $opts['hideminor'], $lastmod );
119 $this->webOutput( $rows, $opts );
126 * Return an array with a ChangesFeed object and ChannelFeed object
130 public function getFeedObject( $feedFormat ){
131 $feed = new ChangesFeed( $feedFormat, 'rcfeed' );
132 $feedObj = $feed->getFeedObject(
133 wfMsgForContent( 'recentchanges' ),
134 wfMsgForContent( 'recentchanges-feed-description' )
136 return array( $feed, $feedObj );
140 * Process $par and put options found if $opts
141 * Mainly used when including the page
144 * @param $opts FormOptions
146 public function parseParameters( $par, FormOptions
$opts ) {
147 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
148 foreach( $bits as $bit ) {
149 if( 'hidebots' === $bit ) $opts['hidebots'] = true;
150 if( 'bots' === $bit ) $opts['hidebots'] = false;
151 if( 'hideminor' === $bit ) $opts['hideminor'] = true;
152 if( 'minor' === $bit ) $opts['hideminor'] = false;
153 if( 'hideliu' === $bit ) $opts['hideliu'] = true;
154 if( 'hidepatrolled' === $bit ) $opts['hidepatrolled'] = true;
155 if( 'hideanons' === $bit ) $opts['hideanons'] = true;
156 if( 'hidemyself' === $bit ) $opts['hidemyself'] = true;
158 if( is_numeric( $bit ) ) $opts['limit'] = $bit;
161 if( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) $opts['limit'] = $m[1];
162 if( preg_match( '/^days=(\d+)$/', $bit, $m ) ) $opts['days'] = $m[1];
167 * Get last modified date, for client caching
168 * Don't use this if we are using the patrol feature, patrol changes don't
169 * update the timestamp
171 * @param $feedFormat String
172 * @return string or false
174 public function checkLastModified( $feedFormat ) {
175 global $wgUseRCPatrol, $wgOut;
176 $dbr = wfGetDB( DB_SLAVE
);
177 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __FUNCTION__
);
178 if( $feedFormat ||
!$wgUseRCPatrol ) {
179 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ) {
180 # Client cache fresh and headers sent, nothing more to do.
188 * Return an array of conditions depending of options set in $opts
190 * @param $opts FormOptions
193 public function buildMainQueryConds( FormOptions
$opts ) {
196 $dbr = wfGetDB( DB_SLAVE
);
199 # It makes no sense to hide both anons and logged-in users
200 # Where this occurs, force anons to be shown
202 if( $opts['hideanons'] && $opts['hideliu'] ){
203 # Check if the user wants to show bots only
204 if( $opts['hidebots'] ){
205 $opts['hideanons'] = false;
208 $opts['hidebots'] = false;
213 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
214 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
215 $cutoff = $dbr->timestamp( $cutoff_unixtime );
217 $fromValid = preg_match('/^[0-9]{14}$/', $opts['from']);
218 if( $fromValid && $opts['from'] > wfTimestamp(TS_MW
,$cutoff) ) {
219 $cutoff = $dbr->timestamp($opts['from']);
221 $opts->reset( 'from' );
224 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
227 $hidePatrol = $wgUser->useRCPatrol() && $opts['hidepatrolled'];
228 $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
229 $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
231 if( $opts['hideminor'] ) $conds['rc_minor'] = 0;
232 if( $opts['hidebots'] ) $conds['rc_bot'] = 0;
233 if( $hidePatrol ) $conds['rc_patrolled'] = 0;
234 if( $forcebot ) $conds['rc_bot'] = 1;
235 if( $hideLoggedInUsers ) $conds[] = 'rc_user = 0';
236 if( $hideAnonymousUsers ) $conds[] = 'rc_user != 0';
238 if( $opts['hidemyself'] ) {
239 if( $wgUser->getId() ) {
240 $conds[] = 'rc_user != ' . $dbr->addQuotes( $wgUser->getId() );
242 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() );
246 # Namespace filtering
247 if( $opts['namespace'] !== '' ) {
248 if( !$opts['invert'] ) {
249 $conds[] = 'rc_namespace = ' . $dbr->addQuotes( $opts['namespace'] );
251 $conds[] = 'rc_namespace != ' . $dbr->addQuotes( $opts['namespace'] );
261 * @param $conds array
262 * @param $opts FormOptions
263 * @return database result or false (for Recentchangeslinked only)
265 public function doMainQuery( $conds, $opts ) {
268 $tables = array( 'recentchanges' );
269 $join_conds = array();
271 $uid = $wgUser->getId();
272 $dbr = wfGetDB( DB_SLAVE
);
273 $limit = $opts['limit'];
274 $namespace = $opts['namespace'];
275 $invert = $opts['invert'];
277 // JOIN on watchlist for users
279 $tables[] = 'watchlist';
280 $join_conds = array( 'watchlist' => array('LEFT JOIN',
281 "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace") );
284 wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
286 // Is there either one namespace selected or excluded?
287 // Also, if this is "all" or main namespace, just use timestamp index.
288 if( is_null($namespace) ||
$invert ||
$namespace == NS_MAIN
) {
289 $res = $dbr->select( $tables, '*', $conds, __METHOD__
,
290 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
291 'USE INDEX' => array('recentchanges' => 'rc_timestamp') ),
293 // We have a new_namespace_time index! UNION over new=(0,1) and sort result set!
296 $sqlNew = $dbr->selectSQLText( $tables, '*',
297 array( 'rc_new' => 1 ) +
$conds,
299 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
300 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
303 $sqlOld = $dbr->selectSQLText( $tables, '*',
304 array( 'rc_new' => 0 ) +
$conds,
306 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
307 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
309 # Join the two fast queries, and sort the result set
310 $sql = "($sqlNew) UNION ($sqlOld) ORDER BY rc_timestamp DESC LIMIT $limit";
311 $res = $dbr->query( $sql, __METHOD__
);
318 * Send output to $wgOut, only called if not used feeds
320 * @param $rows array of database rows
321 * @param $opts FormOptions
323 public function webOutput( $rows, $opts ) {
324 global $wgOut, $wgUser, $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
325 global $wgAllowCategorizedRecentChanges;
327 $limit = $opts['limit'];
329 if( !$this->including() ) {
330 // Output options box
331 $this->doHeader( $opts );
334 // And now for the content
335 $wgOut->setSyndicated( true );
337 if( $wgAllowCategorizedRecentChanges ) {
338 $this->filterByCategories( $rows, $opts );
341 $showWatcherCount = $wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' );
342 $watcherCache = array();
344 $dbr = wfGetDB( DB_SLAVE
);
347 $list = ChangesList
::newFromUser( $wgUser );
349 $s = $list->beginRecentChangesList();
350 foreach( $rows as $obj ) {
351 if( $limit == 0 ) break;
352 $rc = RecentChange
::newFromRow( $obj );
353 $rc->counter
= $counter++
;
354 # Check if the page has been updated since the last visit
355 if( $wgShowUpdatedMarker && !empty($obj->wl_notificationtimestamp
) ) {
356 $rc->notificationtimestamp
= ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
);
358 $rc->notificationtimestamp
= false; // Default
360 # Check the number of users watching the page
361 $rc->numberofWatchingusers
= 0; // Default
362 if( $showWatcherCount && $obj->rc_namespace
>= 0 ) {
363 if( !isset($watcherCache[$obj->rc_namespace
][$obj->rc_title
]) ) {
364 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
365 $dbr->selectField( 'watchlist',
368 'wl_namespace' => $obj->rc_namespace
,
369 'wl_title' => $obj->rc_title
,
371 __METHOD__
. '-watchers' );
373 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
375 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
378 $s .= $list->endRecentChangesList();
379 $wgOut->addHTML( $s );
383 * Return the text to be displayed above the changes
385 * @param $opts FormOptions
386 * @return String: XHTML
388 public function doHeader( $opts ) {
389 global $wgScript, $wgOut;
391 $this->setTopText( $wgOut, $opts );
393 $defaults = $opts->getAllValues();
394 $nondefaults = $opts->getChangedValues();
395 $opts->consumeValues( array( 'namespace', 'invert' ) );
398 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
401 $extraOpts = $this->getExtraOptions( $opts );
402 $extraOptsCount = count( $extraOpts );
404 $submit = ' ' . Xml
::submitbutton( wfMsg( 'allpagessubmit' ) );
406 $out = Xml
::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
407 foreach( $extraOpts as $optionRow ) {
408 # Add submit button to the last row only
410 $addSubmit = $count === $extraOptsCount ?
$submit : '';
412 $out .= Xml
::openElement( 'tr' );
413 if( is_array( $optionRow ) ) {
414 $out .= Xml
::tags( 'td', array( 'class' => 'mw-label' ), $optionRow[0] );
415 $out .= Xml
::tags( 'td', array( 'class' => 'mw-input' ), $optionRow[1] . $addSubmit );
417 $out .= Xml
::tags( 'td', array( 'class' => 'mw-input', 'colspan' => 2 ), $optionRow . $addSubmit );
419 $out .= Xml
::closeElement( 'tr' );
421 $out .= Xml
::closeElement( 'table' );
423 $unconsumed = $opts->getUnconsumedValues();
424 foreach( $unconsumed as $key => $value ) {
425 $out .= Xml
::hidden( $key, $value );
428 $t = $this->getTitle();
429 $out .= Xml
::hidden( 'title', $t->getPrefixedText() );
430 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
432 $panelString = implode( "\n", $panel );
435 Xml
::fieldset( wfMsg( 'recentchanges-legend' ), $panelString, array( 'class' => 'rcoptions' ) )
438 $this->setBottomText( $wgOut, $opts );
442 * Get options to be displayed in a form
444 * @param $opts FormOptions
447 function getExtraOptions( $opts ){
448 $extraOpts = array();
449 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
451 global $wgAllowCategorizedRecentChanges;
452 if( $wgAllowCategorizedRecentChanges ) {
453 $extraOpts['category'] = $this->categoryFilterForm( $opts );
456 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
461 * Send the text to be displayed above the options
463 * @param $out OutputPage
464 * @param $opts FormOptions
466 function setTopText( OutputPage
$out, FormOptions
$opts ){
467 $out->addWikiText( wfMsgForContentNoTrans( 'recentchangestext' ) );
471 * Send the text to be displayed after the options, for use in
472 * Recentchangeslinked
474 * @param $out OutputPage
475 * @param $opts FormOptions
477 function setBottomText( OutputPage
$out, FormOptions
$opts ){}
480 * Creates the choose namespace selection
482 * @param $opts FormOptions
485 protected function namespaceFilterForm( FormOptions
$opts ) {
486 $nsSelect = Xml
::namespaceSelector( $opts['namespace'], '' );
487 $nsLabel = Xml
::label( wfMsg('namespace'), 'namespace' );
488 $invert = Xml
::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $opts['invert'] );
489 return array( $nsLabel, "$nsSelect $invert" );
493 * Create a input to filter changes by categories
495 * @param $opts FormOptions
498 protected function categoryFilterForm( FormOptions
$opts ) {
499 list( $label, $input ) = Xml
::inputLabelSep( wfMsg('rc_categories'),
500 'categories', 'mw-categories', false, $opts['categories'] );
502 $input .= ' ' . Xml
::checkLabel( wfMsg('rc_categories_any'),
503 'categories_any', 'mw-categories_any', $opts['categories_any'] );
505 return array( $label, $input );
509 * Filter $rows by categories set in $opts
511 * @param $rows array of database rows
512 * @param $opts FormOptions
514 function filterByCategories( &$rows, FormOptions
$opts ) {
515 $categories = array_map( 'trim', explode( "|" , $opts['categories'] ) );
517 if( empty($categories) ) {
523 foreach( $categories as $cat ) {
525 if( $cat == "" ) continue;
532 foreach( $rows AS $k => $r ) {
533 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
534 $id = $nt->getArticleID();
535 if( $id == 0 ) continue; # Page might have been deleted...
536 if( !in_array($id, $articles) ) {
539 if( !isset($a2r[$id]) ) {
546 if( !count($articles) ||
!count($cats) )
550 $c = new Categoryfinder
;
551 $c->seed( $articles, $cats, $opts['categories_any'] ?
"OR" : "AND" ) ;
556 foreach( $match AS $id ) {
557 foreach( $a2r[$id] AS $rev ) {
559 $newrows[$k] = $rows[$k];
566 * Makes change an option link which carries all the other options
567 * @param $title see Title
571 function makeOptionsLink( $title, $override, $options, $active = false ) {
573 $sk = $wgUser->getSkin();
574 $params = $override +
$options;
575 return $sk->link( $this->getTitle(), htmlspecialchars( $title ),
576 ( $active ?
array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
580 * Creates the options panel.
581 * @param $defaults array
582 * @param $nondefaults array
584 function optionsPanel( $defaults, $nondefaults ) {
585 global $wgLang, $wgUser, $wgRCLinkLimits, $wgRCLinkDays;
587 $options = $nondefaults +
$defaults;
590 if( $options['from'] ) {
591 $note .= wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
592 $wgLang->formatNum( $options['limit'] ),
593 $wgLang->timeanddate( $options['from'], true ) ) . '<br />';
595 if( !wfEmptyMsg( 'rclegend', wfMsg('rclegend') ) ) {
596 $note .= wfMsgExt( 'rclegend', array('parseinline') ) . '<br />';
599 # Sort data for display and make sure it's unique after we've added user data.
600 $wgRCLinkLimits[] = $options['limit'];
601 $wgRCLinkDays[] = $options['days'];
602 sort( $wgRCLinkLimits );
603 sort( $wgRCLinkDays );
604 $wgRCLinkLimits = array_unique( $wgRCLinkLimits );
605 $wgRCLinkDays = array_unique( $wgRCLinkDays );
608 foreach( $wgRCLinkLimits as $value ) {
609 $cl[] = $this->makeOptionsLink( $wgLang->formatNum( $value ),
610 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] ) ;
612 $cl = implode( ' | ', $cl );
614 // day links, reset 'from' to none
615 foreach( $wgRCLinkDays as $value ) {
616 $dl[] = $this->makeOptionsLink( $wgLang->formatNum( $value ),
617 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] ) ;
619 $dl = implode( ' | ', $dl );
623 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
624 $minorLink = $this->makeOptionsLink( $showhide[1-$options['hideminor']],
625 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
626 $botLink = $this->makeOptionsLink( $showhide[1-$options['hidebots']],
627 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
628 $anonsLink = $this->makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
629 array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
630 $liuLink = $this->makeOptionsLink( $showhide[1-$options['hideliu']],
631 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
632 $patrLink = $this->makeOptionsLink( $showhide[1-$options['hidepatrolled']],
633 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
634 $myselfLink = $this->makeOptionsLink( $showhide[1-$options['hidemyself']],
635 array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
637 $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
638 $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
639 $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
640 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
641 if( $wgUser->useRCPatrol() )
642 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
643 $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
644 $hl = implode( ' | ', $links );
646 // show from this onward link
647 $now = $wgLang->timeanddate( wfTimestampNow(), true );
648 $tl = $this->makeOptionsLink( $now, array( 'from' => wfTimestampNow() ), $nondefaults );
650 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter' ),
652 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter' ), $tl );
653 return "{$note}$rclinks<br />$rclistfrom";