4 * Implements Special:Recentchanges
7 class SpecialRecentChanges
extends SpecialPage
{
8 var $rcOptions, $rcSubpage;
10 public function __construct() {
11 parent
::__construct( 'Recentchanges' );
12 $this->includable( true );
16 * Get a FormOptions object containing the default options
20 public function getDefaultOptions() {
22 $opts = new FormOptions();
24 $opts->add( 'days', (int)$wgUser->getOption( 'rcdays' ) );
25 $opts->add( 'limit', (int)$wgUser->getOption( 'rclimit' ) );
26 $opts->add( 'from', '' );
28 $opts->add( 'hideminor', $wgUser->getBoolOption( 'hideminor' ) );
29 $opts->add( 'hidebots', true );
30 $opts->add( 'hideanons', false );
31 $opts->add( 'hideliu', false );
32 $opts->add( 'hidepatrolled', $wgUser->getBoolOption( 'hidepatrolled' ) );
33 $opts->add( 'hidemyself', false );
35 $opts->add( 'namespace', '', FormOptions
::INTNULL
);
36 $opts->add( 'invert', false );
38 $opts->add( 'categories', '' );
39 $opts->add( 'categories_any', false );
40 $opts->add( 'tagfilter', '' );
45 * Create a FormOptions object with options as specified by the user
49 public function setup( $parameters ) {
52 $opts = $this->getDefaultOptions();
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 * Create a FormOptions object specific for feed requests and return it
69 public function feedSetup() {
70 global $wgFeedLimit, $wgRequest;
71 $opts = $this->getDefaultOptions();
72 # Feed is cached on limit,hideminor,namespace; other params would randomly not work
73 $opts->fetchValuesFromRequest( $wgRequest, array( 'limit', 'hideminor', 'namespace' ) );
74 $opts->validateIntBounds( 'limit', 0, $wgFeedLimit );
79 * Get the current FormOptions for this request
81 public function getOptions() {
82 if ( $this->rcOptions
=== null ) {
84 $feedFormat = $wgRequest->getVal( 'feed' );
85 $this->rcOptions
= $feedFormat ?
$this->feedSetup() : $this->setup( $this->rcSubpage
);
87 return $this->rcOptions
;
92 * Main execution point
94 * @param $subpage string
96 public function execute( $subpage ) {
97 global $wgRequest, $wgOut;
98 $this->rcSubpage
= $subpage;
99 $feedFormat = $wgRequest->getVal( 'feed' );
101 # 10 seconds server-side caching max
102 $wgOut->setSquidMaxage( 10 );
103 # Check if the client has a cached version
104 $lastmod = $this->checkLastModified( $feedFormat );
105 if( $lastmod === false ) {
109 $opts = $this->getOptions();
111 $this->outputHeader();
113 // Fetch results, prepare a batch link existence check query
114 $conds = $this->buildMainQueryConds( $opts );
115 $rows = $this->doMainQuery( $conds, $opts );
116 if( $rows === false ){
117 if( !$this->including() ) {
118 $this->doHeader( $opts );
124 $batch = new LinkBatch
;
125 foreach( $rows as $row ) {
126 $batch->add( NS_USER
, $row->rc_user_text
);
127 $batch->add( NS_USER_TALK
, $row->rc_user_text
);
128 $batch->add( $row->rc_namespace
, $row->rc_title
);
133 list( $changesFeed, $formatter ) = $this->getFeedObject( $feedFormat );
134 $changesFeed->execute( $formatter, $rows, $lastmod, $opts );
136 $this->webOutput( $rows, $opts );
143 * Return an array with a ChangesFeed object and ChannelFeed object
147 public function getFeedObject( $feedFormat ){
148 $changesFeed = new ChangesFeed( $feedFormat, 'rcfeed' );
149 $formatter = $changesFeed->getFeedObject(
150 wfMsgForContent( 'recentchanges' ),
151 wfMsgForContent( 'recentchanges-feed-description' )
153 return array( $changesFeed, $formatter );
157 * Process $par and put options found if $opts
158 * Mainly used when including the page
161 * @param $opts FormOptions
163 public function parseParameters( $par, FormOptions
$opts ) {
164 $bits = preg_split( '/\s*,\s*/', trim( $par ) );
165 foreach( $bits as $bit ) {
166 if( 'hidebots' === $bit ) $opts['hidebots'] = true;
167 if( 'bots' === $bit ) $opts['hidebots'] = false;
168 if( 'hideminor' === $bit ) $opts['hideminor'] = true;
169 if( 'minor' === $bit ) $opts['hideminor'] = false;
170 if( 'hideliu' === $bit ) $opts['hideliu'] = true;
171 if( 'hidepatrolled' === $bit ) $opts['hidepatrolled'] = true;
172 if( 'hideanons' === $bit ) $opts['hideanons'] = true;
173 if( 'hidemyself' === $bit ) $opts['hidemyself'] = true;
175 if( is_numeric( $bit ) ) $opts['limit'] = $bit;
178 if( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) $opts['limit'] = $m[1];
179 if( preg_match( '/^days=(\d+)$/', $bit, $m ) ) $opts['days'] = $m[1];
184 * Get last modified date, for client caching
185 * Don't use this if we are using the patrol feature, patrol changes don't
186 * update the timestamp
188 * @param $feedFormat String
189 * @return string or false
191 public function checkLastModified( $feedFormat ) {
192 global $wgUseRCPatrol, $wgOut;
193 $dbr = wfGetDB( DB_SLAVE
);
194 $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__
);
195 if( $feedFormat ||
!$wgUseRCPatrol ) {
196 if( $lastmod && $wgOut->checkLastModified( $lastmod ) ) {
197 # Client cache fresh and headers sent, nothing more to do.
205 * Return an array of conditions depending of options set in $opts
207 * @param $opts FormOptions
210 public function buildMainQueryConds( FormOptions
$opts ) {
213 $dbr = wfGetDB( DB_SLAVE
);
216 # It makes no sense to hide both anons and logged-in users
217 # Where this occurs, force anons to be shown
219 if( $opts['hideanons'] && $opts['hideliu'] ){
220 # Check if the user wants to show bots only
221 if( $opts['hidebots'] ){
222 $opts['hideanons'] = false;
225 $opts['hidebots'] = false;
230 $cutoff_unixtime = time() - ( $opts['days'] * 86400 );
231 $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime %
86400);
232 $cutoff = $dbr->timestamp( $cutoff_unixtime );
234 $fromValid = preg_match('/^[0-9]{14}$/', $opts['from']);
235 if( $fromValid && $opts['from'] > wfTimestamp(TS_MW
,$cutoff) ) {
236 $cutoff = $dbr->timestamp($opts['from']);
238 $opts->reset( 'from' );
241 $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff );
244 $hidePatrol = $wgUser->useRCPatrol() && $opts['hidepatrolled'];
245 $hideLoggedInUsers = $opts['hideliu'] && !$forcebot;
246 $hideAnonymousUsers = $opts['hideanons'] && !$forcebot;
248 if( $opts['hideminor'] ) $conds['rc_minor'] = 0;
249 if( $opts['hidebots'] ) $conds['rc_bot'] = 0;
250 if( $hidePatrol ) $conds['rc_patrolled'] = 0;
251 if( $forcebot ) $conds['rc_bot'] = 1;
252 if( $hideLoggedInUsers ) $conds[] = 'rc_user = 0';
253 if( $hideAnonymousUsers ) $conds[] = 'rc_user != 0';
255 if( $opts['hidemyself'] ) {
256 if( $wgUser->getId() ) {
257 $conds[] = 'rc_user != ' . $dbr->addQuotes( $wgUser->getId() );
259 $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() );
263 # Namespace filtering
264 if( $opts['namespace'] !== '' ) {
265 if( !$opts['invert'] ) {
266 $conds[] = 'rc_namespace = ' . $dbr->addQuotes( $opts['namespace'] );
268 $conds[] = 'rc_namespace != ' . $dbr->addQuotes( $opts['namespace'] );
278 * @param $conds array
279 * @param $opts FormOptions
280 * @return database result or false (for Recentchangeslinked only)
282 public function doMainQuery( $conds, $opts ) {
285 $tables = array( 'recentchanges' );
286 $join_conds = array();
287 $query_options = array( 'USE INDEX' => array('recentchanges' => 'rc_timestamp') );
289 $uid = $wgUser->getId();
290 $dbr = wfGetDB( DB_SLAVE
);
291 $limit = $opts['limit'];
292 $namespace = $opts['namespace'];
293 $invert = $opts['invert'];
295 // JOIN on watchlist for users
297 $tables[] = 'watchlist';
298 $join_conds['watchlist'] = array('LEFT JOIN',
299 "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace");
301 if ($wgUser->isAllowed("rollback")) {
303 $join_conds['page'] = array('LEFT JOIN', 'rc_cur_id=page_id');
307 // Fields are * in this case, so let the function modify an empty array to keep it happy.
308 ChangeTags
::modifyDisplayQuery(
309 $tables, $fields, $conds, $join_conds, $query_options, $opts['tagfilter']
312 if ( !wfRunHooks( 'SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts, &$query_options ) ) )
315 // Don't use the new_namespace_time timestamp index if:
316 // (a) "All namespaces" selected
317 // (b) We want all pages NOT in a certain namespaces (inverted)
318 // (c) There is a tag to filter on (use tag index instead)
319 // (d) UNION + sort/limit is not an option for the DBMS
320 if( is_null($namespace)
322 ||
$opts['tagfilter'] != ''
323 ||
!$dbr->unionSupportsOrderAndLimit() )
325 $res = $dbr->select( $tables, '*', $conds, __METHOD__
,
326 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) +
329 // We have a new_namespace_time index! UNION over new=(0,1) and sort result set!
332 $sqlNew = $dbr->selectSQLText( $tables, '*',
333 array( 'rc_new' => 1 ) +
$conds,
335 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
336 'USE INDEX' => array('recentchanges' => 'rc_timestamp') ),
339 $sqlOld = $dbr->selectSQLText( $tables, '*',
340 array( 'rc_new' => 0 ) +
$conds,
342 array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
343 'USE INDEX' => array('recentchanges' => 'rc_timestamp') ),
345 # Join the two fast queries, and sort the result set
346 $sql = $dbr->unionQueries(array($sqlNew, $sqlOld), false).' ORDER BY rc_timestamp DESC';
347 $sql = $dbr->limitResult($sql, $limit, false);
348 $res = $dbr->query( $sql, __METHOD__
);
355 * Send output to $wgOut, only called if not used feeds
357 * @param $rows array of database rows
358 * @param $opts FormOptions
360 public function webOutput( $rows, $opts ) {
361 global $wgOut, $wgUser, $wgRCShowWatchingUsers, $wgShowUpdatedMarker;
362 global $wgAllowCategorizedRecentChanges;
364 $limit = $opts['limit'];
366 if( !$this->including() ) {
367 // Output options box
368 $this->doHeader( $opts );
371 // And now for the content
372 $wgOut->setFeedAppendQuery( $this->getFeedQuery() );
374 if( $wgAllowCategorizedRecentChanges ) {
375 $this->filterByCategories( $rows, $opts );
378 $showWatcherCount = $wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' );
379 $watcherCache = array();
381 $dbr = wfGetDB( DB_SLAVE
);
384 $list = ChangesList
::newFromUser( $wgUser );
386 $s = $list->beginRecentChangesList();
387 foreach( $rows as $obj ) {
388 if( $limit == 0 ) break;
389 $rc = RecentChange
::newFromRow( $obj );
390 $rc->counter
= $counter++
;
391 # Check if the page has been updated since the last visit
392 if( $wgShowUpdatedMarker && !empty($obj->wl_notificationtimestamp
) ) {
393 $rc->notificationtimestamp
= ($obj->rc_timestamp
>= $obj->wl_notificationtimestamp
);
395 $rc->notificationtimestamp
= false; // Default
397 # Check the number of users watching the page
398 $rc->numberofWatchingusers
= 0; // Default
399 if( $showWatcherCount && $obj->rc_namespace
>= 0 ) {
400 if( !isset($watcherCache[$obj->rc_namespace
][$obj->rc_title
]) ) {
401 $watcherCache[$obj->rc_namespace
][$obj->rc_title
] =
402 $dbr->selectField( 'watchlist',
405 'wl_namespace' => $obj->rc_namespace
,
406 'wl_title' => $obj->rc_title
,
408 __METHOD__
. '-watchers' );
410 $rc->numberofWatchingusers
= $watcherCache[$obj->rc_namespace
][$obj->rc_title
];
412 $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user
), $counter );
415 $s .= $list->endRecentChangesList();
416 $wgOut->addHTML( $s );
420 * Get the query string to append to feed link URLs.
421 * This is overridden by RCL to add the target parameter
423 public function getFeedQuery() {
428 * Return the text to be displayed above the changes
430 * @param $opts FormOptions
431 * @return String: XHTML
433 public function doHeader( $opts ) {
434 global $wgScript, $wgOut;
436 $this->setTopText( $wgOut, $opts );
438 $defaults = $opts->getAllValues();
439 $nondefaults = $opts->getChangedValues();
440 $opts->consumeValues( array( 'namespace', 'invert', 'tagfilter',
441 'categories', 'categories_any' ) );
444 $panel[] = $this->optionsPanel( $defaults, $nondefaults );
447 $extraOpts = $this->getExtraOptions( $opts );
448 $extraOptsCount = count( $extraOpts );
450 $submit = ' ' . Xml
::submitbutton( wfMsg( 'allpagessubmit' ) );
452 $out = Xml
::openElement( 'table', array( 'class' => 'mw-recentchanges-table' ) );
453 foreach( $extraOpts as $optionRow ) {
454 # Add submit button to the last row only
456 $addSubmit = $count === $extraOptsCount ?
$submit : '';
458 $out .= Xml
::openElement( 'tr' );
459 if( is_array( $optionRow ) ) {
460 $out .= Xml
::tags( 'td', array( 'class' => 'mw-label' ), $optionRow[0] );
461 $out .= Xml
::tags( 'td', array( 'class' => 'mw-input' ), $optionRow[1] . $addSubmit );
463 $out .= Xml
::tags( 'td', array( 'class' => 'mw-input', 'colspan' => 2 ), $optionRow . $addSubmit );
465 $out .= Xml
::closeElement( 'tr' );
467 $out .= Xml
::closeElement( 'table' );
469 $unconsumed = $opts->getUnconsumedValues();
470 foreach( $unconsumed as $key => $value ) {
471 $out .= Xml
::hidden( $key, $value );
474 $t = $this->getTitle();
475 $out .= Xml
::hidden( 'title', $t->getPrefixedText() );
476 $form = Xml
::tags( 'form', array( 'action' => $wgScript ), $out );
478 $panelString = implode( "\n", $panel );
481 Xml
::fieldset( wfMsg( 'recentchanges-legend' ), $panelString, array( 'class' => 'rcoptions' ) )
484 $wgOut->addHTML( ChangesList
::flagLegend() );
486 $this->setBottomText( $wgOut, $opts );
490 * Get options to be displayed in a form
492 * @param $opts FormOptions
495 function getExtraOptions( $opts ){
496 $extraOpts = array();
497 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
499 global $wgAllowCategorizedRecentChanges;
500 if( $wgAllowCategorizedRecentChanges ) {
501 $extraOpts['category'] = $this->categoryFilterForm( $opts );
504 $tagFilter = ChangeTags
::buildTagFilterSelector( $opts['tagfilter'] );
505 if ( count($tagFilter) )
506 $extraOpts['tagfilter'] = $tagFilter;
508 wfRunHooks( 'SpecialRecentChangesPanel', array( &$extraOpts, $opts ) );
513 * Send the text to be displayed above the options
515 * @param $out OutputPage
516 * @param $opts FormOptions
518 function setTopText( OutputPage
$out, FormOptions
$opts ){
519 $out->addWikiText( wfMsgForContentNoTrans( 'recentchangestext' ) );
523 * Send the text to be displayed after the options, for use in
524 * Recentchangeslinked
526 * @param $out OutputPage
527 * @param $opts FormOptions
529 function setBottomText( OutputPage
$out, FormOptions
$opts ){}
532 * Creates the choose namespace selection
534 * @param $opts FormOptions
537 protected function namespaceFilterForm( FormOptions
$opts ) {
538 $nsSelect = Xml
::namespaceSelector( $opts['namespace'], '' );
539 $nsLabel = Xml
::label( wfMsg('namespace'), 'namespace' );
540 $invert = Xml
::checkLabel( wfMsg('invert'), 'invert', 'nsinvert', $opts['invert'] );
541 return array( $nsLabel, "$nsSelect $invert" );
545 * Create a input to filter changes by categories
547 * @param $opts FormOptions
550 protected function categoryFilterForm( FormOptions
$opts ) {
551 list( $label, $input ) = Xml
::inputLabelSep( wfMsg('rc_categories'),
552 'categories', 'mw-categories', false, $opts['categories'] );
554 $input .= ' ' . Xml
::checkLabel( wfMsg('rc_categories_any'),
555 'categories_any', 'mw-categories_any', $opts['categories_any'] );
557 return array( $label, $input );
561 * Filter $rows by categories set in $opts
563 * @param $rows array of database rows
564 * @param $opts FormOptions
566 function filterByCategories( &$rows, FormOptions
$opts ) {
567 $categories = array_map( 'trim', explode( '|' , $opts['categories'] ) );
569 if( !count( $categories ) ) {
575 foreach( $categories as $cat ) {
577 if( $cat == '' ) continue;
585 foreach( $rows AS $k => $r ) {
586 $nt = Title
::makeTitle( $r->rc_namespace
, $r->rc_title
);
587 $id = $nt->getArticleID();
588 if( $id == 0 ) continue; # Page might have been deleted...
589 if( !in_array( $id, $articles ) ) {
592 if( !isset( $a2r[$id] ) ) {
600 if( !count( $articles ) ||
!count( $cats ) )
604 $c = new Categoryfinder
;
605 $c->seed( $articles, $cats, $opts['categories_any'] ?
"OR" : "AND" ) ;
610 foreach( $match AS $id ) {
611 foreach( $a2r[$id] AS $rev ) {
613 $newrows[$k] = $rowsarr[$k];
620 * Makes change an option link which carries all the other options
621 * @param $title see Title
625 function makeOptionsLink( $title, $override, $options, $active = false ) {
627 $sk = $wgUser->getSkin();
628 $params = $override +
$options;
630 return $sk->link( $this->getTitle(), '<strong>' . htmlspecialchars( $title ) . '</strong>',
631 array(), $params, array( 'known' ) );
633 return $sk->link( $this->getTitle(), htmlspecialchars( $title ), array() , $params, array( 'known' ) );
638 * Creates the options panel.
639 * @param $defaults array
640 * @param $nondefaults array
642 function optionsPanel( $defaults, $nondefaults ) {
643 global $wgLang, $wgUser, $wgRCLinkLimits, $wgRCLinkDays;
645 $options = $nondefaults +
$defaults;
648 if( !wfEmptyMsg( 'rclegend', wfMsg('rclegend') ) ) {
649 $note .= '<div class="mw-rclegend">' . wfMsgExt( 'rclegend', array('parseinline') ) . "</div>\n";
651 if( $options['from'] ) {
652 $note .= wfMsgExt( 'rcnotefrom', array( 'parseinline' ),
653 $wgLang->formatNum( $options['limit'] ),
654 $wgLang->timeanddate( $options['from'], true ),
655 $wgLang->date( $options['from'], true ),
656 $wgLang->time( $options['from'], true ) ) . '<br />';
659 # Sort data for display and make sure it's unique after we've added user data.
660 $wgRCLinkLimits[] = $options['limit'];
661 $wgRCLinkDays[] = $options['days'];
662 sort( $wgRCLinkLimits );
663 sort( $wgRCLinkDays );
664 $wgRCLinkLimits = array_unique( $wgRCLinkLimits );
665 $wgRCLinkDays = array_unique( $wgRCLinkDays );
668 foreach( $wgRCLinkLimits as $value ) {
669 $cl[] = $this->makeOptionsLink( $wgLang->formatNum( $value ),
670 array( 'limit' => $value ), $nondefaults, $value == $options['limit'] ) ;
672 $cl = $wgLang->pipeList( $cl );
674 // day links, reset 'from' to none
675 foreach( $wgRCLinkDays as $value ) {
676 $dl[] = $this->makeOptionsLink( $wgLang->formatNum( $value ),
677 array( 'days' => $value, 'from' => '' ), $nondefaults, $value == $options['days'] ) ;
679 $dl = $wgLang->pipeList( $dl );
683 $showhide = array( wfMsg( 'show' ), wfMsg( 'hide' ) );
684 $minorLink = $this->makeOptionsLink( $showhide[1-$options['hideminor']],
685 array( 'hideminor' => 1-$options['hideminor'] ), $nondefaults);
686 $botLink = $this->makeOptionsLink( $showhide[1-$options['hidebots']],
687 array( 'hidebots' => 1-$options['hidebots'] ), $nondefaults);
688 $anonsLink = $this->makeOptionsLink( $showhide[ 1 - $options['hideanons'] ],
689 array( 'hideanons' => 1 - $options['hideanons'] ), $nondefaults );
690 $liuLink = $this->makeOptionsLink( $showhide[1-$options['hideliu']],
691 array( 'hideliu' => 1-$options['hideliu'] ), $nondefaults);
692 $patrLink = $this->makeOptionsLink( $showhide[1-$options['hidepatrolled']],
693 array( 'hidepatrolled' => 1-$options['hidepatrolled'] ), $nondefaults);
694 $myselfLink = $this->makeOptionsLink( $showhide[1-$options['hidemyself']],
695 array( 'hidemyself' => 1-$options['hidemyself'] ), $nondefaults);
697 $links[] = wfMsgHtml( 'rcshowhideminor', $minorLink );
698 $links[] = wfMsgHtml( 'rcshowhidebots', $botLink );
699 $links[] = wfMsgHtml( 'rcshowhideanons', $anonsLink );
700 $links[] = wfMsgHtml( 'rcshowhideliu', $liuLink );
701 if( $wgUser->useRCPatrol() )
702 $links[] = wfMsgHtml( 'rcshowhidepatr', $patrLink );
703 $links[] = wfMsgHtml( 'rcshowhidemine', $myselfLink );
704 $hl = $wgLang->pipeList( $links );
706 // show from this onward link
707 $now = $wgLang->timeanddate( wfTimestampNow(), true );
708 $tl = $this->makeOptionsLink( $now, array( 'from' => wfTimestampNow() ), $nondefaults );
710 $rclinks = wfMsgExt( 'rclinks', array( 'parseinline', 'replaceafter' ),
712 $rclistfrom = wfMsgExt( 'rclistfrom', array( 'parseinline', 'replaceafter' ), $tl );
713 return "{$note}$rclinks<br />$rclistfrom";