includable( true ); } /** * Get a FormOptions object containing the default options * * @return FormOptions */ public function getDefaultOptions() { global $wgUser; $opts = new FormOptions(); $opts->add( 'days', (int)$wgUser->getOption( 'rcdays' ) ); $opts->add( 'limit', (int)$wgUser->getOption( 'rclimit' ) ); $opts->add( 'from', '' ); $opts->add( 'hideminor', $wgUser->getBoolOption( 'hideminor' ) ); $opts->add( 'hidebots', true ); $opts->add( 'hideanons', false ); $opts->add( 'hideliu', false ); $opts->add( 'hidepatrolled', $wgUser->getBoolOption( 'hidepatrolled' ) ); $opts->add( 'hidemyself', false ); $opts->add( 'namespace', '', FormOptions::INTNULL ); $opts->add( 'invert', false ); $opts->add( 'categories', '' ); $opts->add( 'categories_any', false ); $opts->add( 'tagfilter', '' ); return $opts; } /** * Create a FormOptions object with options as specified by the user * * @return FormOptions */ public function setup( $parameters ) { global $wgRequest; $opts = $this->getDefaultOptions(); $opts->fetchValuesFromRequest( $wgRequest ); // Give precedence to subpage syntax if( $parameters !== null ) { $this->parseParameters( $parameters, $opts ); } $opts->validateIntBounds( 'limit', 0, 5000 ); return $opts; } /** * Create a FormOptions object specific for feed requests and return it * * @return FormOptions */ public function feedSetup() { global $wgFeedLimit, $wgRequest; $opts = $this->getDefaultOptions(); # Feed is cached on limit,hideminor,namespace; other params would randomly not work $opts->fetchValuesFromRequest( $wgRequest, array( 'limit', 'hideminor', 'namespace' ) ); $opts->validateIntBounds( 'limit', 0, $wgFeedLimit ); return $opts; } /** * Get the current FormOptions for this request */ public function getOptions() { if ( $this->rcOptions === null ) { global $wgRequest; $feedFormat = $wgRequest->getVal( 'feed' ); $this->rcOptions = $feedFormat ? $this->feedSetup() : $this->setup( $this->rcSubpage ); } return $this->rcOptions; } /** * Main execution point * * @param $subpage string */ public function execute( $subpage ) { global $wgRequest, $wgOut; $this->rcSubpage = $subpage; $feedFormat = $wgRequest->getVal( 'feed' ); # 10 seconds server-side caching max $wgOut->setSquidMaxage( 10 ); # Check if the client has a cached version $lastmod = $this->checkLastModified( $feedFormat ); if( $lastmod === false ) { return; } $opts = $this->getOptions(); $this->setHeaders(); $this->outputHeader(); // Fetch results, prepare a batch link existence check query $conds = $this->buildMainQueryConds( $opts ); $rows = $this->doMainQuery( $conds, $opts ); if( $rows === false ){ if( !$this->including() ) { $this->doHeader( $opts ); } return; } if( !$feedFormat ) { $batch = new LinkBatch; foreach( $rows as $row ) { $batch->add( NS_USER, $row->rc_user_text ); $batch->add( NS_USER_TALK, $row->rc_user_text ); $batch->add( $row->rc_namespace, $row->rc_title ); } $batch->execute(); } if( $feedFormat ) { list( $changesFeed, $formatter ) = $this->getFeedObject( $feedFormat ); $changesFeed->execute( $formatter, $rows, $lastmod, $opts ); } else { $this->webOutput( $rows, $opts ); } $rows->free(); } /** * Return an array with a ChangesFeed object and ChannelFeed object * * @return array */ public function getFeedObject( $feedFormat ){ $changesFeed = new ChangesFeed( $feedFormat, 'rcfeed' ); $formatter = $changesFeed->getFeedObject( wfMsgForContent( 'recentchanges' ), wfMsgForContent( 'recentchanges-feed-description' ) ); return array( $changesFeed, $formatter ); } /** * Process $par and put options found if $opts * Mainly used when including the page * * @param $par String * @param $opts FormOptions */ public function parseParameters( $par, FormOptions $opts ) { $bits = preg_split( '/\s*,\s*/', trim( $par ) ); foreach( $bits as $bit ) { if( 'hidebots' === $bit ) $opts['hidebots'] = true; if( 'bots' === $bit ) $opts['hidebots'] = false; if( 'hideminor' === $bit ) $opts['hideminor'] = true; if( 'minor' === $bit ) $opts['hideminor'] = false; if( 'hideliu' === $bit ) $opts['hideliu'] = true; if( 'hidepatrolled' === $bit ) $opts['hidepatrolled'] = true; if( 'hideanons' === $bit ) $opts['hideanons'] = true; if( 'hidemyself' === $bit ) $opts['hidemyself'] = true; if( is_numeric( $bit ) ) $opts['limit'] = $bit; $m = array(); if( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) $opts['limit'] = $m[1]; if( preg_match( '/^days=(\d+)$/', $bit, $m ) ) $opts['days'] = $m[1]; } } /** * Get last modified date, for client caching * Don't use this if we are using the patrol feature, patrol changes don't * update the timestamp * * @param $feedFormat String * @return string or false */ public function checkLastModified( $feedFormat ) { global $wgUseRCPatrol, $wgOut; $dbr = wfGetDB( DB_SLAVE ); $lastmod = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', false, __METHOD__ ); if( $feedFormat || !$wgUseRCPatrol ) { if( $lastmod && $wgOut->checkLastModified( $lastmod ) ) { # Client cache fresh and headers sent, nothing more to do. return false; } } return $lastmod; } /** * Return an array of conditions depending of options set in $opts * * @param $opts FormOptions * @return array */ public function buildMainQueryConds( FormOptions $opts ) { global $wgUser; $dbr = wfGetDB( DB_SLAVE ); $conds = array(); # It makes no sense to hide both anons and logged-in users # Where this occurs, force anons to be shown $forcebot = false; if( $opts['hideanons'] && $opts['hideliu'] ){ # Check if the user wants to show bots only if( $opts['hidebots'] ){ $opts['hideanons'] = false; } else { $forcebot = true; $opts['hidebots'] = false; } } // Calculate cutoff $cutoff_unixtime = time() - ( $opts['days'] * 86400 ); $cutoff_unixtime = $cutoff_unixtime - ($cutoff_unixtime % 86400); $cutoff = $dbr->timestamp( $cutoff_unixtime ); $fromValid = preg_match('/^[0-9]{14}$/', $opts['from']); if( $fromValid && $opts['from'] > wfTimestamp(TS_MW,$cutoff) ) { $cutoff = $dbr->timestamp($opts['from']); } else { $opts->reset( 'from' ); } $conds[] = 'rc_timestamp >= ' . $dbr->addQuotes( $cutoff ); $hidePatrol = $wgUser->useRCPatrol() && $opts['hidepatrolled']; $hideLoggedInUsers = $opts['hideliu'] && !$forcebot; $hideAnonymousUsers = $opts['hideanons'] && !$forcebot; if( $opts['hideminor'] ) $conds['rc_minor'] = 0; if( $opts['hidebots'] ) $conds['rc_bot'] = 0; if( $hidePatrol ) $conds['rc_patrolled'] = 0; if( $forcebot ) $conds['rc_bot'] = 1; if( $hideLoggedInUsers ) $conds[] = 'rc_user = 0'; if( $hideAnonymousUsers ) $conds[] = 'rc_user != 0'; if( $opts['hidemyself'] ) { if( $wgUser->getId() ) { $conds[] = 'rc_user != ' . $dbr->addQuotes( $wgUser->getId() ); } else { $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $wgUser->getName() ); } } # Namespace filtering if( $opts['namespace'] !== '' ) { if( !$opts['invert'] ) { $conds[] = 'rc_namespace = ' . $dbr->addQuotes( $opts['namespace'] ); } else { $conds[] = 'rc_namespace != ' . $dbr->addQuotes( $opts['namespace'] ); } } return $conds; } /** * Process the query * * @param $conds array * @param $opts FormOptions * @return database result or false (for Recentchangeslinked only) */ public function doMainQuery( $conds, $opts ) { global $wgUser; $tables = array( 'recentchanges' ); $join_conds = array(); $query_options = array( 'USE INDEX' => array('recentchanges' => 'rc_timestamp') ); $uid = $wgUser->getId(); $dbr = wfGetDB( DB_SLAVE ); $limit = $opts['limit']; $namespace = $opts['namespace']; $invert = $opts['invert']; // JOIN on watchlist for users if( $uid ) { $tables[] = 'watchlist'; $join_conds['watchlist'] = array('LEFT JOIN', "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace"); } if ($wgUser->isAllowed("rollback")) { $tables[] = 'page'; $join_conds['page'] = array('LEFT JOIN', 'rc_cur_id=page_id'); } // Tag stuff. $fields = array(); // Fields are * in this case, so let the function modify an empty array to keep it happy. ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $query_options, $opts['tagfilter'] ); if ( !wfRunHooks( 'SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts, &$query_options ) ) ) return false; // Don't use the new_namespace_time timestamp index if: // (a) "All namespaces" selected // (b) We want all pages NOT in a certain namespaces (inverted) // (c) There is a tag to filter on (use tag index instead) // (d) UNION + sort/limit is not an option for the DBMS if( is_null($namespace) || $invert || $opts['tagfilter'] != '' || !$dbr->unionSupportsOrderAndLimit() ) { $res = $dbr->select( $tables, '*', $conds, __METHOD__, array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) + $query_options, $join_conds ); // We have a new_namespace_time index! UNION over new=(0,1) and sort result set! } else { // New pages $sqlNew = $dbr->selectSQLText( $tables, '*', array( 'rc_new' => 1 ) + $conds, __METHOD__, array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit, 'USE INDEX' => array('recentchanges' => 'rc_timestamp') ), $join_conds ); // Old pages $sqlOld = $dbr->selectSQLText( $tables, '*', array( 'rc_new' => 0 ) + $conds, __METHOD__, array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit, 'USE INDEX' => array('recentchanges' => 'rc_timestamp') ), $join_conds ); # Join the two fast queries, and sort the result set $sql = $dbr->unionQueries(array($sqlNew, $sqlOld), false).' ORDER BY rc_timestamp DESC'; $sql = $dbr->limitResult($sql, $limit, false); $res = $dbr->query( $sql, __METHOD__ ); } return $res; } /** * Send output to $wgOut, only called if not used feeds * * @param $rows array of database rows * @param $opts FormOptions */ public function webOutput( $rows, $opts ) { global $wgOut, $wgUser, $wgRCShowWatchingUsers, $wgShowUpdatedMarker; global $wgAllowCategorizedRecentChanges; $limit = $opts['limit']; if( !$this->including() ) { // Output options box $this->doHeader( $opts ); } // And now for the content $wgOut->setFeedAppendQuery( $this->getFeedQuery() ); if( $wgAllowCategorizedRecentChanges ) { $this->filterByCategories( $rows, $opts ); } $showWatcherCount = $wgRCShowWatchingUsers && $wgUser->getOption( 'shownumberswatching' ); $watcherCache = array(); $dbr = wfGetDB( DB_SLAVE ); $counter = 1; $list = ChangesList::newFromUser( $wgUser ); $s = $list->beginRecentChangesList(); foreach( $rows as $obj ) { if( $limit == 0 ) break; $rc = RecentChange::newFromRow( $obj ); $rc->counter = $counter++; # Check if the page has been updated since the last visit if( $wgShowUpdatedMarker && !empty($obj->wl_notificationtimestamp) ) { $rc->notificationtimestamp = ($obj->rc_timestamp >= $obj->wl_notificationtimestamp); } else { $rc->notificationtimestamp = false; // Default } # Check the number of users watching the page $rc->numberofWatchingusers = 0; // Default if( $showWatcherCount && $obj->rc_namespace >= 0 ) { if( !isset($watcherCache[$obj->rc_namespace][$obj->rc_title]) ) { $watcherCache[$obj->rc_namespace][$obj->rc_title] = $dbr->selectField( 'watchlist', 'COUNT(*)', array( 'wl_namespace' => $obj->rc_namespace, 'wl_title' => $obj->rc_title, ), __METHOD__ . '-watchers' ); } $rc->numberofWatchingusers = $watcherCache[$obj->rc_namespace][$obj->rc_title]; } $s .= $list->recentChangesLine( $rc, !empty( $obj->wl_user ), $counter ); --$limit; } $s .= $list->endRecentChangesList(); $wgOut->addHTML( $s ); } /** * Get the query string to append to feed link URLs. * This is overridden by RCL to add the target parameter */ public function getFeedQuery() { return false; } /** * Return the text to be displayed above the changes * * @param $opts FormOptions * @return String: XHTML */ public function doHeader( $opts ) { global $wgScript, $wgOut; $this->setTopText( $wgOut, $opts ); $defaults = $opts->getAllValues(); $nondefaults = $opts->getChangedValues(); $opts->consumeValues( array( 'namespace', 'invert', 'tagfilter' ) ); $panel = array(); $panel[] = $this->optionsPanel( $defaults, $nondefaults ); $panel[] = '