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', (bool)$wgUser->getOption( 'hideminor' ) );
27 $opts->add( 'hidebots', true );
28 $opts->add( 'hideanons', false );
29 $opts->add( 'hideliu', false );
30 $opts->add( 'hidepatrolled', false );
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 );
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 $batch = new LinkBatch
;
98 $conds = $this->buildMainQueryConds( $opts );
99 $rows = $this->doMainQuery( $conds, $opts );
100 if( $rows === false ){
101 if( !$this->including() ) {
102 $this->doHeader( $opts );
107 foreach( $rows as $row ) {
108 if ( !$feedFormat ) {
109 // User page and talk links
110 $batch->add( NS_USER
, $row->rc_user_text
);
111 $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 );
120 $this->webOutput( $rows, $opts );
127 * Return an array with a ChangesFeed object and ChannelFeed object
131 public function getFeedObject( $feedFormat ){
132 $feed = new ChangesFeed( $feedFormat, 'rcfeed' );
133 $feedObj = $feed->getFeedObject(
134 wfMsgForContent( 'recentchanges' ),
135 wfMsgForContent( 'recentchanges-feed-description' )
137 return array( $feed, $feedObj );
141 * Process $par and put options found if $opts
142 * Mainly used when including the page
145 * @param $opts FormOptions
147 public function parseParameters( $par, FormOptions
$opts ) {
148 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
149 foreach ( $bits as $bit ) {
150 if ( 'hidebots' === $bit ) $opts['hidebots'] = true;
151 if ( 'bots' === $bit ) $opts['hidebots'] = false;
152 if ( 'hideminor' === $bit ) $opts['hideminor'] = true;
153 if ( 'minor' === $bit ) $opts['hideminor'] = false;
154 if ( 'hideliu' === $bit ) $opts['hideliu'] = true;
155 if ( 'hidepatrolled' === $bit ) $opts['hidepatrolled'] = true;
156 if ( 'hideanons' === $bit ) $opts['hideanons'] = true;
157 if ( 'hidemyself' === $bit ) $opts['hidemyself'] = true;
159 if ( is_numeric( $bit ) ) $opts['limit'] = $bit;
162 if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) $opts['limit'] = $m[1];
163 if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) $opts['days'] = $m[1];
168 * Get last modified date, for client caching
169 * Don't use this if we are using the patrol feature, patrol changes don't
170 * update the timestamp
172 * @param $feedFormat String
173 * @return int or false
175 public function checkLastModified( $feedFormat ) {
176 global $wgUseRCPatrol, $wgOut;
177 $dbr = wfGetDB( DB_SLAVE
);
178 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __FUNCTION__
);
179 if ( $feedFormat ||
!$wgUseRCPatrol ) {
180 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ){
181 # Client cache fresh and headers sent, nothing more to do.
189 * Return an array of conditions depending of options set in $opts
191 * @param $opts FormOptions
194 public function buildMainQueryConds( FormOptions
$opts ) {
197 $dbr = wfGetDB( DB_SLAVE
);
200 # It makes no sense to hide both anons and logged-in users
201 # Where this occurs, force anons to be shown
203 if( $opts['hideanons'] && $opts['hideliu'] ){
204 # Check if the user wants to show bots only
205 if( $opts['hidebots'] ){
206 $opts['hideanons'] = false;
209 $opts['hidebots'] = false;
214 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
215 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
216 $cutoff = $dbr->timestamp( $cutoff_unixtime );
218 $fromValid = preg_match('/^[0-9]{14}$/', $opts['from']);
219 if( $fromValid && $opts['from'] > wfTimestamp(TS_MW
,$cutoff) ) {
220 $cutoff = $dbr->timestamp($opts['from']);
222 $opts->reset( 'from' );
225 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
228 $hidePatrol = $wgUser->useRCPatrol() && $opts['hidepatrolled'];
229 $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
230 $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
232 if ( $opts['hideminor'] ) $conds['rc_minor'] = 0;
233 if ( $opts['hidebots'] ) $conds['rc_bot'] = 0;
234 if ( $hidePatrol ) $conds['rc_patrolled'] = 0;
235 if ( $forcebot ) $conds['rc_bot'] = 1;
236 if ( $hideLoggedInUsers ) $conds[] = 'rc_user = 0';
237 if ( $hideAnonymousUsers ) $conds[] = 'rc_user != 0';
239 if( $opts['hidemyself'] ) {
240 if( $wgUser->getId() ) {
241 $conds[] = 'rc_user != ' . $dbr->addQuotes( $wgUser->getId() );
243 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() );
247 # Namespace filtering
248 if ( $opts['namespace'] !== '' ) {
249 if ( !$opts['invert'] ) {
250 $conds[] = 'rc_namespace = ' . $dbr->addQuotes( $opts['namespace'] );
252 $conds[] = 'rc_namespace != ' . $dbr->addQuotes( $opts['namespace'] );
262 * @param $conds array
263 * @param $opts FormOptions
264 * @return database result or false (for Recentchangeslinked only)
266 public function doMainQuery( $conds, $opts ) {
269 $tables = array( 'recentchanges' );
270 $join_conds = array();
272 $uid = $wgUser->getId();
273 $dbr = wfGetDB( DB_SLAVE
);
274 $limit = $opts['limit'];
275 $namespace = $opts['namespace'];
276 $invert = $opts['invert'];
278 // JOIN on watchlist for users
280 $tables[] = 'watchlist';
281 $join_conds = array( 'watchlist' => array('LEFT JOIN',"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 $list = ChangesList
::newFromUser( $wgUser );
339 if ( $wgAllowCategorizedRecentChanges ) {
340 $this->filterByCategories( $rows, $opts );
343 $s = $list->beginRecentChangesList();
346 $showWatcherCount = $wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' );
347 $watcherCache = array();
349 $dbr = wfGetDB( DB_SLAVE
);
351 foreach( $rows as $obj ){
356 if ( ! ( $opts['hideminor'] && $obj->rc_minor
) &&
357 ! ( $opts['hidepatrolled'] && $obj->rc_patrolled
) ) {
358 $rc = RecentChange
::newFromRow( $obj );
359 $rc->counter
= $counter++
;
361 if ($wgShowUpdatedMarker
362 && !empty( $obj->wl_notificationtimestamp
)
363 && ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
)) {
364 $rc->notificationtimestamp
= true;
366 $rc->notificationtimestamp
= false;
369 $rc->numberofWatchingusers
= 0; // Default
370 if ($showWatcherCount && $obj->rc_namespace
>= 0) {
371 if (!isset($watcherCache[$obj->rc_namespace
][$obj->rc_title
])) {
372 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
373 $dbr->selectField( 'watchlist',
376 'wl_namespace' => $obj->rc_namespace
,
377 'wl_title' => $obj->rc_title
,
379 __METHOD__
. '-watchers' );
381 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
383 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
) );
387 $s .= $list->endRecentChangesList();
388 $wgOut->addHTML( $s );
392 * Return the text to be displayed above the changes
394 * @param $opts FormOptions
395 * @return String: XHTML
397 public function doHeader( $opts ) {
398 global $wgScript, $wgOut;
400 $this->setTopText( $wgOut, $opts );
402 $defaults = $opts->getAllValues();
403 $nondefaults = $opts->getChangedValues();
404 $opts->consumeValues( array( 'namespace', 'invert' ) );
407 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
410 $extraOpts = $this->getExtraOptions( $opts );
411 $extraOptsCount = count( $extraOpts );
413 $submit = ' ' . Xml
::submitbutton( wfMsg( 'allpagessubmit' ) );
415 $out = Xml
::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
416 foreach ( $extraOpts as $optionRow ) {
417 # Add submit button to the last row only
419 $addSubmit = $count === $extraOptsCount ?
$submit : '';
421 $out .= Xml
::openElement( 'tr' );
422 if ( is_array( $optionRow ) ) {
423 $out .= Xml
::tags( 'td', array( 'class' => 'mw-label' ), $optionRow[0] );
424 $out .= Xml
::tags( 'td', array( 'class' => 'mw-input' ), $optionRow[1] . $addSubmit );
426 $out .= Xml
::tags( 'td', array( 'class' => 'mw-input', 'colspan' => 2 ), $optionRow . $addSubmit );
428 $out .= Xml
::closeElement( 'tr' );
430 $out .= Xml
::closeElement( 'table' );
432 $unconsumed = $opts->getUnconsumedValues();
433 foreach ( $unconsumed as $key => $value ) {
434 $out .= Xml
::hidden( $key, $value );
437 $t = $this->getTitle();
438 $out .= Xml
::hidden( 'title', $t->getPrefixedText() );
439 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
441 $panelString = implode( "\n", $panel );
444 Xml
::fieldset( wfMsg( 'recentchanges-legend' ), $panelString, array( 'class' => 'rcoptions' ) )
447 $this->setBottomText( $wgOut, $opts );
451 * Get options to be displayed in a form
453 * @param $opts FormOptions
456 function getExtraOptions( $opts ){
457 $extraOpts = array();
458 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
460 global $wgAllowCategorizedRecentChanges;
461 if ( $wgAllowCategorizedRecentChanges ) {
462 $extraOpts['category'] = $this->categoryFilterForm( $opts );
465 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
470 * Send the text to be displayed above the options
472 * @param $out OutputPage
473 * @param $opts FormOptions
475 function setTopText( OutputPage
$out, FormOptions
$opts ){
476 $out->addWikiText( wfMsgForContentNoTrans( 'recentchangestext' ) );
480 * Send the text to be displayed after the options, for use in
481 * Recentchangeslinked
483 * @param $out OutputPage
484 * @param $opts FormOptions
486 function setBottomText( OutputPage
$out, FormOptions
$opts ){}
489 * Creates the choose namespace selection
491 * @param $opts FormOptions
494 protected function namespaceFilterForm( FormOptions
$opts ) {
495 $nsSelect = HTMLnamespaceselector( $opts['namespace'], '' );
496 $nsLabel = Xml
::label( wfMsg('namespace'), 'namespace' );
497 $invert = Xml
::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $opts['invert'] );
498 return array( $nsLabel, "$nsSelect $invert" );
502 * Create a input to filter changes by categories
504 * @param $opts FormOptions
507 protected function categoryFilterForm( FormOptions
$opts ) {
508 list( $label, $input ) = Xml
::inputLabelSep( wfMsg('rc_categories'),
509 'categories', 'mw-categories', false, $opts['categories'] );
511 $input .= ' ' . Xml
::checkLabel( wfMsg('rc_categories_any'),
512 'categories_any', 'mw-categories_any', $opts['categories_any'] );
514 return array( $label, $input );
518 * Filter $rows by categories set in $opts
520 * @param $rows array of database rows
521 * @param $opts FormOptions
523 function filterByCategories( &$rows, FormOptions
$opts ) {
524 $categories = array_map( 'trim', explode( "|" , $opts['categories'] ) );
526 if( empty($categories) ) {
532 foreach ( $categories as $cat ) {
534 if ( $cat == "" ) continue;
541 foreach ( $rows AS $k => $r ) {
542 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
543 $id = $nt->getArticleID();
544 if ( $id == 0 ) continue; # Page might have been deleted...
545 if ( !in_array($id, $articles) ) {
548 if ( !isset($a2r[$id]) ) {
555 if ( !count($articles) ||
!count($cats) )
559 $c = new Categoryfinder
;
560 $c->seed( $articles, $cats, $opts['categories_any'] ?
"OR" : "AND" ) ;
565 foreach ( $match AS $id ) {
566 foreach ( $a2r[$id] AS $rev ) {
568 $newrows[$k] = $rows[$k];
575 * Makes change an option link which carries all the other options
576 * @param $title see Title
580 function makeOptionsLink( $title, $override, $options, $active = false ) {
582 $sk = $wgUser->getSkin();
583 $params = wfArrayMerge( $options, $override );
584 return $sk->link( $this->getTitle(), htmlspecialchars( $title ),
585 ( $active ?
array( 'style'=>'font-weight: bold;' ) : array() ), $params, array( 'known' ) );
589 * Creates the options panel.
590 * @param $defaults array
591 * @param $nondefaults array
593 function optionsPanel( $defaults, $nondefaults ) {
594 global $wgLang, $wgUser, $wgRCLinkLimits, $wgRCLinkDays;
596 $options = $nondefaults +
$defaults;
598 if( $options['from'] )
599 $note = wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
600 $wgLang->formatNum( $options['limit'] ),
601 $wgLang->timeanddate( $options['from'], true ) );
603 $note = wfMsgExt( 'rcnote', array( 'parseinline' ),
604 $wgLang->formatNum( $options['limit'] ),
605 $wgLang->formatNum( $options['days'] ),
606 $wgLang->timeAndDate( wfTimestampNow(), true ),
607 $wgLang->date( wfTimestampNow(), true ),
608 $wgLang->time( wfTimestampNow(), true ) );
610 # Sort data for display and make sure it's unique after we've added user data.
611 $wgRCLinkLimits[] = $options['limit'];
612 $wgRCLinkDays[] = $options['days'];
613 sort( $wgRCLinkLimits );
614 sort( $wgRCLinkDays );
615 $wgRCLinkLimits = array_unique( $wgRCLinkLimits );
616 $wgRCLinkDays = array_unique( $wgRCLinkDays );
619 foreach( $wgRCLinkLimits as $value ) {
620 $cl[] = $this->makeOptionsLink( $wgLang->formatNum( $value ),
621 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] ) ;
623 $cl = implode( ' | ', $cl );
625 // day links, reset 'from' to none
626 foreach( $wgRCLinkDays as $value ) {
627 $dl[] = $this->makeOptionsLink( $wgLang->formatNum( $value ),
628 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] ) ;
630 $dl = implode( ' | ', $dl );
634 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
635 $minorLink = $this->makeOptionsLink( $showhide[1-$options['hideminor']],
636 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
637 $botLink = $this->makeOptionsLink( $showhide[1-$options['hidebots']],
638 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
639 $anonsLink = $this->makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
640 array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
641 $liuLink = $this->makeOptionsLink( $showhide[1-$options['hideliu']],
642 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
643 $patrLink = $this->makeOptionsLink( $showhide[1-$options['hidepatrolled']],
644 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
645 $myselfLink = $this->makeOptionsLink( $showhide[1-$options['hidemyself']],
646 array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
648 $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
649 $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
650 $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
651 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
652 if( $wgUser->useRCPatrol() )
653 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
654 $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
655 $hl = implode( ' | ', $links );
657 // show from this onward link
658 $now = $wgLang->timeanddate( wfTimestampNow(), true );
659 $tl = $this->makeOptionsLink( $now, array( 'from' => wfTimestampNow() ), $nondefaults );
661 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter' ),
663 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter' ), $tl );
664 return "$note<br />$rclinks<br />$rclistfrom";