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', $wgUser->getBoolOption( 'rc_hidebots' ) );
28 $opts->add( 'hideanons', false );
29 $opts->add( 'hideliu', false );
30 $opts->add( 'hidepatrolled', $wgUser->getBoolOption( 'hidepatrolled' ) );
31 $opts->add( 'hidemyself', $wgUser->getBoolOption( 'rc_hideown' ) );
33 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
34 $opts->add( 'invert', false );
36 $opts->add( 'categories', '' );
37 $opts->add( 'categories_any', false );
38 $opts->add( 'tagfilter', '' );
43 * Get a FormOptions object with options as specified by the user
47 public function setup( $parameters ) {
50 $opts = $this->getDefaultOptions();
51 $opts->fetchValuesFromRequest( $wgRequest );
53 // Give precedence to subpage syntax
54 if( $parameters !== null ) {
55 $this->parseParameters( $parameters, $opts );
58 $opts->validateIntBounds( 'limit', 0, 500 );
63 * Get a FormOptions object sepcific for feed requests
67 public function feedSetup() {
68 global $wgFeedLimit, $wgRequest;
69 $opts = $this->getDefaultOptions();
70 # Feed is cached on limit,hideminor; other params would randomly not work
71 $opts->fetchValuesFromRequest( $wgRequest, array( 'limit', 'hideminor' ) );
72 $opts->validateIntBounds( 'limit', 0, $wgFeedLimit );
77 * Main execution point
79 * @param $parameters string
81 public function execute( $parameters ) {
82 global $wgRequest, $wgOut;
83 $feedFormat = $wgRequest->getVal( 'feed' );
85 # 10 seconds server-side caching max
86 $wgOut->setSquidMaxage( 10 );
87 # Check if the client has a cached version
88 $lastmod = $this->checkLastModified( $feedFormat );
89 if( $lastmod === false ) {
93 $opts = $feedFormat ?
$this->feedSetup() : $this->setup( $parameters );
95 $this->outputHeader();
97 // Fetch results, prepare a batch link existence check query
99 $conds = $this->buildMainQueryConds( $opts );
100 $rows = $this->doMainQuery( $conds, $opts );
101 if( $rows === false ){
102 if( !$this->including() ) {
103 $this->doHeader( $opts );
109 $batch = new LinkBatch
;
110 foreach( $rows as $row ) {
111 $batch->add( NS_USER
, $row->rc_user_text
);
112 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
113 $batch->add( $row->rc_namespace
, $row->rc_title
);
117 $target = isset($opts['target']) ?
$opts['target'] : ''; // RCL has targets
119 list( $feed, $feedObj ) = $this->getFeedObject( $feedFormat );
120 $feed->execute( $feedObj, $rows, $opts['limit'], $opts['hideminor'], $lastmod, $target );
122 $this->webOutput( $rows, $opts );
129 * Return an array with a ChangesFeed object and ChannelFeed object
133 public function getFeedObject( $feedFormat ){
134 $feed = new ChangesFeed( $feedFormat, 'rcfeed' );
135 $feedObj = $feed->getFeedObject(
136 wfMsgForContent( 'recentchanges' ),
137 wfMsgForContent( 'recentchanges-feed-description' )
139 return array( $feed, $feedObj );
143 * Process $par and put options found if $opts
144 * Mainly used when including the page
147 * @param $opts FormOptions
149 public function parseParameters( $par, FormOptions
$opts ) {
150 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
151 foreach( $bits as $bit ) {
152 if( 'hidebots' === $bit ) $opts['hidebots'] = true;
153 if( 'bots' === $bit ) $opts['hidebots'] = false;
154 if( 'hideminor' === $bit ) $opts['hideminor'] = true;
155 if( 'minor' === $bit ) $opts['hideminor'] = false;
156 if( 'hideliu' === $bit ) $opts['hideliu'] = true;
157 if( 'hidepatrolled' === $bit ) $opts['hidepatrolled'] = true;
158 if( 'hideanons' === $bit ) $opts['hideanons'] = true;
159 if( 'hidemyself' === $bit ) $opts['hidemyself'] = true;
161 if( is_numeric( $bit ) ) $opts['limit'] = $bit;
164 if( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) $opts['limit'] = $m[1];
165 if( preg_match( '/^days=(\d+)$/', $bit, $m ) ) $opts['days'] = $m[1];
170 * Get last modified date, for client caching
171 * Don't use this if we are using the patrol feature, patrol changes don't
172 * update the timestamp
174 * @param $feedFormat String
175 * @return string or false
177 public function checkLastModified( $feedFormat ) {
178 global $wgUseRCPatrol, $wgOut;
179 $dbr = wfGetDB( DB_SLAVE
);
180 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __FUNCTION__
);
181 if( $feedFormat ||
!$wgUseRCPatrol ) {
182 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ) {
183 # Client cache fresh and headers sent, nothing more to do.
191 * Return an array of conditions depending of options set in $opts
193 * @param $opts FormOptions
196 public function buildMainQueryConds( FormOptions
$opts ) {
199 $dbr = wfGetDB( DB_SLAVE
);
202 # It makes no sense to hide both anons and logged-in users
203 # Where this occurs, force anons to be shown
205 if( $opts['hideanons'] && $opts['hideliu'] ){
206 # Check if the user wants to show bots only
207 if( $opts['hidebots'] ){
208 $opts['hideanons'] = false;
211 $opts['hidebots'] = false;
216 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
217 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
218 $cutoff = $dbr->timestamp( $cutoff_unixtime );
220 $fromValid = preg_match('/^[0-9]{14}$/', $opts['from']);
221 if( $fromValid && $opts['from'] > wfTimestamp(TS_MW
,$cutoff) ) {
222 $cutoff = $dbr->timestamp($opts['from']);
224 $opts->reset( 'from' );
227 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
230 $hidePatrol = $wgUser->useRCPatrol() && $opts['hidepatrolled'];
231 $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
232 $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
234 if( $opts['hideminor'] ) $conds['rc_minor'] = 0;
235 if( $opts['hidebots'] ) $conds['rc_bot'] = 0;
236 if( $hidePatrol ) $conds['rc_patrolled'] = 0;
237 if( $forcebot ) $conds['rc_bot'] = 1;
238 if( $hideLoggedInUsers ) $conds[] = 'rc_user = 0';
239 if( $hideAnonymousUsers ) $conds[] = 'rc_user != 0';
241 if( $opts['hidemyself'] ) {
242 if( $wgUser->getId() ) {
243 $conds[] = 'rc_user != ' . $dbr->addQuotes( $wgUser->getId() );
245 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() );
249 # Namespace filtering
250 if( $opts['namespace'] !== '' ) {
251 if( !$opts['invert'] ) {
252 $conds[] = 'rc_namespace = ' . $dbr->addQuotes( $opts['namespace'] );
254 $conds[] = 'rc_namespace != ' . $dbr->addQuotes( $opts['namespace'] );
264 * @param $conds array
265 * @param $opts FormOptions
266 * @return database result or false (for Recentchangeslinked only)
268 public function doMainQuery( $conds, $opts ) {
271 $tables = array( 'recentchanges' );
272 $join_conds = array();
273 $query_options = array( 'USE INDEX' => array('recentchanges' => 'rc_timestamp') );
275 $uid = $wgUser->getId();
276 $dbr = wfGetDB( DB_SLAVE
);
277 $limit = $opts['limit'];
278 $namespace = $opts['namespace'];
279 $invert = $opts['invert'];
281 $join_conds = array();
283 // JOIN on watchlist for users
285 $tables[] = 'watchlist';
286 $join_conds['watchlist'] = array('LEFT JOIN',
287 "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace");
289 if ($wgUser->isAllowed("rollback")) {
291 $join_conds['page'] = array('LEFT JOIN', 'rc_cur_id=page_id');
295 // Fields are * in this case, so let the function modify an empty array to keep it happy.
296 ChangeTags
::modifyDisplayQuery( $tables,
304 wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
306 // Is there either one namespace selected or excluded?
307 // Tag filtering also has a better index.
308 // Also, if this is "all" or main namespace, just use timestamp index.
309 if( is_null($namespace) ||
$invert ||
$opts['tagfilter'] ) {
310 $res = $dbr->select( $tables, '*', $conds, __METHOD__
,
311 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) +
314 // We have a new_namespace_time index! UNION over new=(0,1) and sort result set!
317 $sqlNew = $dbr->selectSQLText( $tables, '*',
318 array( 'rc_new' => 1 ) +
$conds,
320 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
321 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
324 $sqlOld = $dbr->selectSQLText( $tables, '*',
325 array( 'rc_new' => 0 ) +
$conds,
327 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
328 'USE INDEX' => array('recentchanges' => 'new_name_timestamp') ),
330 # Join the two fast queries, and sort the result set
331 $sql = "($sqlNew) UNION ($sqlOld) ORDER BY rc_timestamp DESC LIMIT $limit";
332 $res = $dbr->query( $sql, __METHOD__
);
339 * Send output to $wgOut, only called if not used feeds
341 * @param $rows array of database rows
342 * @param $opts FormOptions
344 public function webOutput( $rows, $opts ) {
345 global $wgOut, $wgUser, $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
346 global $wgAllowCategorizedRecentChanges;
348 $limit = $opts['limit'];
350 if( !$this->including() ) {
351 // Output options box
352 $this->doHeader( $opts );
355 // And now for the content
356 $wgOut->setSyndicated( true );
358 if( $wgAllowCategorizedRecentChanges ) {
359 $this->filterByCategories( $rows, $opts );
362 $showWatcherCount = $wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' );
363 $watcherCache = array();
365 $dbr = wfGetDB( DB_SLAVE
);
368 $list = ChangesList
::newFromUser( $wgUser );
370 $s = $list->beginRecentChangesList();
371 foreach( $rows as $obj ) {
372 if( $limit == 0 ) break;
373 $rc = RecentChange
::newFromRow( $obj );
374 $rc->counter
= $counter++
;
375 # Check if the page has been updated since the last visit
376 if( $wgShowUpdatedMarker && !empty($obj->wl_notificationtimestamp
) ) {
377 $rc->notificationtimestamp
= ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
);
379 $rc->notificationtimestamp
= false; // Default
381 # Check the number of users watching the page
382 $rc->numberofWatchingusers
= 0; // Default
383 if( $showWatcherCount && $obj->rc_namespace
>= 0 ) {
384 if( !isset($watcherCache[$obj->rc_namespace
][$obj->rc_title
]) ) {
385 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
386 $dbr->selectField( 'watchlist',
389 'wl_namespace' => $obj->rc_namespace
,
390 'wl_title' => $obj->rc_title
,
392 __METHOD__
. '-watchers' );
394 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
396 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
), $counter );
399 $s .= $list->endRecentChangesList();
400 $wgOut->addHTML( $s );
404 * Return the text to be displayed above the changes
406 * @param $opts FormOptions
407 * @return String: XHTML
409 public function doHeader( $opts ) {
410 global $wgScript, $wgOut;
412 $this->setTopText( $wgOut, $opts );
414 $defaults = $opts->getAllValues();
415 $nondefaults = $opts->getChangedValues();
416 $opts->consumeValues( array( 'namespace', 'invert' ) );
419 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
422 $extraOpts = $this->getExtraOptions( $opts );
423 $extraOptsCount = count( $extraOpts );
425 $submit = ' ' . Xml
::submitbutton( wfMsg( 'allpagessubmit' ) );
427 $out = Xml
::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
428 foreach( $extraOpts as $optionRow ) {
429 # Add submit button to the last row only
431 $addSubmit = $count === $extraOptsCount ?
$submit : '';
433 $out .= Xml
::openElement( 'tr' );
434 if( is_array( $optionRow ) ) {
435 $out .= Xml
::tags( 'td', array( 'class' => 'mw-label' ), $optionRow[0] );
436 $out .= Xml
::tags( 'td', array( 'class' => 'mw-input' ), $optionRow[1] . $addSubmit );
438 $out .= Xml
::tags( 'td', array( 'class' => 'mw-input', 'colspan' => 2 ), $optionRow . $addSubmit );
440 $out .= Xml
::closeElement( 'tr' );
442 $out .= Xml
::closeElement( 'table' );
444 $unconsumed = $opts->getUnconsumedValues();
445 foreach( $unconsumed as $key => $value ) {
446 $out .= Xml
::hidden( $key, $value );
449 $t = $this->getTitle();
450 $out .= Xml
::hidden( 'title', $t->getPrefixedText() );
451 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
453 $panelString = implode( "\n", $panel );
456 Xml
::fieldset( wfMsg( 'recentchanges-legend' ), $panelString, array( 'class' => 'rcoptions' ) )
459 $this->setBottomText( $wgOut, $opts );
463 * Get options to be displayed in a form
465 * @param $opts FormOptions
468 function getExtraOptions( $opts ){
469 $extraOpts = array();
470 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
472 global $wgAllowCategorizedRecentChanges;
473 if( $wgAllowCategorizedRecentChanges ) {
474 $extraOpts['category'] = $this->categoryFilterForm( $opts );
477 $tagFilter = ChangeTags
::buildTagFilterSelector( $opts['tagfilter'] );
478 if ( count($tagFilter) )
479 $extraOpts['tagfilter'] = $tagFilter;
481 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
486 * Send the text to be displayed above the options
488 * @param $out OutputPage
489 * @param $opts FormOptions
491 function setTopText( OutputPage
$out, FormOptions
$opts ){
492 $out->addWikiText( wfMsgForContentNoTrans( 'recentchangestext' ) );
496 * Send the text to be displayed after the options, for use in
497 * Recentchangeslinked
499 * @param $out OutputPage
500 * @param $opts FormOptions
502 function setBottomText( OutputPage
$out, FormOptions
$opts ){}
505 * Creates the choose namespace selection
507 * @param $opts FormOptions
510 protected function namespaceFilterForm( FormOptions
$opts ) {
511 $nsSelect = Xml
::namespaceSelector( $opts['namespace'], '' );
512 $nsLabel = Xml
::label( wfMsg('namespace'), 'namespace' );
513 $invert = Xml
::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $opts['invert'] );
514 return array( $nsLabel, "$nsSelect $invert" );
518 * Create a input to filter changes by categories
520 * @param $opts FormOptions
523 protected function categoryFilterForm( FormOptions
$opts ) {
524 list( $label, $input ) = Xml
::inputLabelSep( wfMsg('rc_categories'),
525 'categories', 'mw-categories', false, $opts['categories'] );
527 $input .= ' ' . Xml
::checkLabel( wfMsg('rc_categories_any'),
528 'categories_any', 'mw-categories_any', $opts['categories_any'] );
530 return array( $label, $input );
534 * Filter $rows by categories set in $opts
536 * @param $rows array of database rows
537 * @param $opts FormOptions
539 function filterByCategories( &$rows, FormOptions
$opts ) {
540 $categories = array_map( 'trim', explode( "|" , $opts['categories'] ) );
542 if( empty($categories) ) {
548 foreach( $categories as $cat ) {
550 if( $cat == "" ) continue;
557 foreach( $rows AS $k => $r ) {
558 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
559 $id = $nt->getArticleID();
560 if( $id == 0 ) continue; # Page might have been deleted...
561 if( !in_array($id, $articles) ) {
564 if( !isset($a2r[$id]) ) {
571 if( !count($articles) ||
!count($cats) )
575 $c = new Categoryfinder
;
576 $c->seed( $articles, $cats, $opts['categories_any'] ?
"OR" : "AND" ) ;
581 foreach( $match AS $id ) {
582 foreach( $a2r[$id] AS $rev ) {
584 $newrows[$k] = $rows[$k];
591 * Makes change an option link which carries all the other options
592 * @param $title see Title
596 function makeOptionsLink( $title, $override, $options, $active = false ) {
598 $sk = $wgUser->getSkin();
599 $params = $override +
$options;
601 return $sk->link( $this->getTitle(), '<strong>' . htmlspecialchars( $title ) . '</strong>',
602 array(), $params, array( 'known' ) );
604 return $sk->link( $this->getTitle(), htmlspecialchars( $title ), array() , $params, array( 'known' ) );
609 * Creates the options panel.
610 * @param $defaults array
611 * @param $nondefaults array
613 function optionsPanel( $defaults, $nondefaults ) {
614 global $wgLang, $wgUser, $wgRCLinkLimits, $wgRCLinkDays;
616 $options = $nondefaults +
$defaults;
619 if( !wfEmptyMsg( 'rclegend', wfMsg('rclegend') ) ) {
620 $note .= '<div class="mw-rclegend">' . wfMsgExt( 'rclegend', array('parseinline') ) . "</div>\n";
622 if( $options['from'] ) {
623 $note .= wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
624 $wgLang->formatNum( $options['limit'] ),
625 $wgLang->timeanddate( $options['from'], true ) ) . '<br />';
628 # Sort data for display and make sure it's unique after we've added user data.
629 $wgRCLinkLimits[] = $options['limit'];
630 $wgRCLinkDays[] = $options['days'];
631 sort( $wgRCLinkLimits );
632 sort( $wgRCLinkDays );
633 $wgRCLinkLimits = array_unique( $wgRCLinkLimits );
634 $wgRCLinkDays = array_unique( $wgRCLinkDays );
637 foreach( $wgRCLinkLimits as $value ) {
638 $cl[] = $this->makeOptionsLink( $wgLang->formatNum( $value ),
639 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] ) ;
641 $cl = $wgLang->pipeList( $cl );
643 // day links, reset 'from' to none
644 foreach( $wgRCLinkDays as $value ) {
645 $dl[] = $this->makeOptionsLink( $wgLang->formatNum( $value ),
646 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] ) ;
648 $dl = $wgLang->pipeList( $dl );
652 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
653 $minorLink = $this->makeOptionsLink( $showhide[1-$options['hideminor']],
654 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
655 $botLink = $this->makeOptionsLink( $showhide[1-$options['hidebots']],
656 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
657 $anonsLink = $this->makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
658 array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
659 $liuLink = $this->makeOptionsLink( $showhide[1-$options['hideliu']],
660 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
661 $patrLink = $this->makeOptionsLink( $showhide[1-$options['hidepatrolled']],
662 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
663 $myselfLink = $this->makeOptionsLink( $showhide[1-$options['hidemyself']],
664 array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
666 $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
667 $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
668 $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
669 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
670 if( $wgUser->useRCPatrol() )
671 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
672 $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
673 $hl = $wgLang->pipeList( $links );
675 // show from this onward link
676 $now = $wgLang->timeanddate( wfTimestampNow(), true );
677 $tl = $this->makeOptionsLink( $now, array( 'from' => wfTimestampNow() ), $nondefaults );
679 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter' ),
681 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter' ), $tl );
682 return "{$note}$rclinks<br />$rclistfrom";