watchlistFilterGroupDefinition = [ 'name' => 'watchlist', 'title' => 'rcfilters-filtergroup-watchlist', 'class' => ChangesListStringOptionsFilterGroup::class, 'priority' => -9, 'isFullCoverage' => true, 'filters' => [ [ 'name' => 'watched', 'label' => 'rcfilters-filter-watchlist-watched-label', 'description' => 'rcfilters-filter-watchlist-watched-description', 'cssClassSuffix' => 'watched', 'isRowApplicableCallable' => function ( $ctx, $rc ) { return $rc->getAttribute( 'wl_user' ); } ], [ 'name' => 'watchednew', 'label' => 'rcfilters-filter-watchlist-watchednew-label', 'description' => 'rcfilters-filter-watchlist-watchednew-description', 'cssClassSuffix' => 'watchednew', 'isRowApplicableCallable' => function ( $ctx, $rc ) { return $rc->getAttribute( 'wl_user' ) && $rc->getAttribute( 'rc_timestamp' ) && $rc->getAttribute( 'wl_notificationtimestamp' ) && $rc->getAttribute( 'rc_timestamp' ) >= $rc->getAttribute( 'wl_notificationtimestamp' ); }, ], [ 'name' => 'notwatched', 'label' => 'rcfilters-filter-watchlist-notwatched-label', 'description' => 'rcfilters-filter-watchlist-notwatched-description', 'cssClassSuffix' => 'notwatched', 'isRowApplicableCallable' => function ( $ctx, $rc ) { return $rc->getAttribute( 'wl_user' ) === null; }, ] ], 'default' => ChangesListStringOptionsFilterGroup::NONE, 'queryCallable' => function ( $specialPageClassName, $context, $dbr, &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selectedValues ) { sort( $selectedValues ); $notwatchedCond = 'wl_user IS NULL'; $watchedCond = 'wl_user IS NOT NULL'; $newCond = 'rc_timestamp >= wl_notificationtimestamp'; if ( $selectedValues === [ 'notwatched' ] ) { $conds[] = $notwatchedCond; return; } if ( $selectedValues === [ 'watched' ] ) { $conds[] = $watchedCond; return; } if ( $selectedValues === [ 'watchednew' ] ) { $conds[] = $dbr->makeList( [ $watchedCond, $newCond ], LIST_AND ); return; } if ( $selectedValues === [ 'notwatched', 'watched' ] ) { // no filters return; } if ( $selectedValues === [ 'notwatched', 'watchednew' ] ) { $conds[] = $dbr->makeList( [ $notwatchedCond, $dbr->makeList( [ $watchedCond, $newCond ], LIST_AND ) ], LIST_OR ); return; } if ( $selectedValues === [ 'watched', 'watchednew' ] ) { $conds[] = $watchedCond; return; } if ( $selectedValues === [ 'notwatched', 'watched', 'watchednew' ] ) { // no filters return; } } ]; } // @codingStandardsIgnoreEnd /** * Main execution point * * @param string $subpage */ public function execute( $subpage ) { // Backwards-compatibility: redirect to new feed URLs $feedFormat = $this->getRequest()->getVal( 'feed' ); if ( !$this->including() && $feedFormat ) { $query = $this->getFeedQuery(); $query['feedformat'] = $feedFormat === 'atom' ? 'atom' : 'rss'; $this->getOutput()->redirect( wfAppendQuery( wfScript( 'api' ), $query ) ); return; } // 10 seconds server-side caching max $out = $this->getOutput(); $out->setCdnMaxage( 10 ); // Check if the client has a cached version $lastmod = $this->checkLastModified(); if ( $lastmod === false ) { return; } $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Recent_changes', true ); parent::execute( $subpage ); if ( $this->isStructuredFilterUiEnabled() ) { $jsData = $this->getStructuredFilterJsData(); $messages = []; foreach ( $jsData['messageKeys'] as $key ) { $messages[$key] = $this->msg( $key )->plain(); } $out->addHTML( ResourceLoader::makeInlineScript( ResourceLoader::makeMessageSetScript( $messages ) ) ); $experimentalStructuredChangeFilters = $this->getConfig()->get( 'StructuredChangeFiltersEnableExperimentalViews' ); $out->addJsConfigVars( 'wgStructuredChangeFilters', $jsData['groups'] ); $out->addJsConfigVars( 'wgStructuredChangeFiltersEnableSaving', $this->getConfig()->get( 'StructuredChangeFiltersEnableSaving' ) ); $out->addJsConfigVars( 'wgStructuredChangeFiltersEnableExperimentalViews', $experimentalStructuredChangeFilters ); $out->addJsConfigVars( 'wgStructuredChangeFiltersEnableLiveUpdate', $this->getConfig()->get( 'StructuredChangeFiltersEnableLiveUpdate' ) ); if ( $experimentalStructuredChangeFilters ) { $out->addJsConfigVars( 'wgRCFiltersChangeTags', $this->buildChangeTagList() ); } } } /** * Fetch the change tags list for the front end * * @return Array Tag data */ protected function buildChangeTagList() { $explicitlyDefinedTags = array_fill_keys( ChangeTags::listExplicitlyDefinedTags(), 0 ); $softwareActivatedTags = array_fill_keys( ChangeTags::listSoftwareActivatedTags(), 0 ); // Hit counts disabled for perf reasons, see T169997 /* $tagStats = ChangeTags::tagUsageStatistics(); $tagHitCounts = array_merge( $explicitlyDefinedTags, $softwareActivatedTags, $tagStats ); // Sort by hits arsort( $tagHitCounts ); */ $tagHitCounts = array_merge( $explicitlyDefinedTags, $softwareActivatedTags ); // Build the list and data $result = []; foreach ( $tagHitCounts as $tagName => $hits ) { if ( // Only get active tags isset( $explicitlyDefinedTags[ $tagName ] ) || isset( $softwareActivatedTags[ $tagName ] ) ) { // Parse description $desc = ChangeTags::tagLongDescriptionMessage( $tagName, $this->getContext() ); $result[] = [ 'name' => $tagName, 'label' => Sanitizer::stripAllTags( ChangeTags::tagDescription( $tagName, $this->getContext() ) ), 'description' => $desc ? Sanitizer::stripAllTags( $desc->parse() ) : '', 'cssClass' => Sanitizer::escapeClass( 'mw-tag-' . $tagName ), 'hits' => $hits, ]; } } // Instead of sorting by hit count (disabled, see above), sort by display name usort( $result, function ( $a, $b ) { return strcasecmp( $a['label'], $b['label'] ); } ); return $result; } /** * @inheritdoc */ protected function transformFilterDefinition( array $filterDefinition ) { if ( isset( $filterDefinition['showHideSuffix'] ) ) { $filterDefinition['showHide'] = 'rc' . $filterDefinition['showHideSuffix']; } return $filterDefinition; } /** * @inheritdoc */ protected function registerFilters() { parent::registerFilters(); if ( !$this->including() && $this->getUser()->isLoggedIn() && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) { $this->registerFiltersFromDefinitions( [ $this->watchlistFilterGroupDefinition ] ); $watchlistGroup = $this->getFilterGroup( 'watchlist' ); $watchlistGroup->getFilter( 'watched' )->setAsSupersetOf( $watchlistGroup->getFilter( 'watchednew' ) ); } $user = $this->getUser(); $significance = $this->getFilterGroup( 'significance' ); $hideMinor = $significance->getFilter( 'hideminor' ); $hideMinor->setDefault( $user->getBoolOption( 'hideminor' ) ); $automated = $this->getFilterGroup( 'automated' ); $hideBots = $automated->getFilter( 'hidebots' ); $hideBots->setDefault( true ); $reviewStatus = $this->getFilterGroup( 'reviewStatus' ); if ( $reviewStatus !== null ) { // Conditional on feature being available and rights $hidePatrolled = $reviewStatus->getFilter( 'hidepatrolled' ); $hidePatrolled->setDefault( $user->getBoolOption( 'hidepatrolled' ) ); } $changeType = $this->getFilterGroup( 'changeType' ); $hideCategorization = $changeType->getFilter( 'hidecategorization' ); if ( $hideCategorization !== null ) { // Conditional on feature being available $hideCategorization->setDefault( $user->getBoolOption( 'hidecategorization' ) ); } } /** * Get a FormOptions object containing the default options * * @return FormOptions */ public function getDefaultOptions() { $opts = parent::getDefaultOptions(); $user = $this->getUser(); $opts->add( 'days', $user->getIntOption( 'rcdays' ) ); $opts->add( 'limit', $user->getIntOption( 'rclimit' ) ); $opts->add( 'from', '' ); $opts->add( 'categories', '' ); $opts->add( 'categories_any', false ); $opts->add( 'tagfilter', '' ); return $opts; } /** * Get all custom filters * * @return array Map of filter URL param names to properties (msg/default) */ protected function getCustomFilters() { if ( $this->customFilters === null ) { $this->customFilters = parent::getCustomFilters(); Hooks::run( 'SpecialRecentChangesFilters', [ $this, &$this->customFilters ], '1.23' ); } return $this->customFilters; } /** * Process $par and put options found in $opts. Used when including the page. * * @param string $par * @param FormOptions $opts */ public function parseParameters( $par, FormOptions $opts ) { parent::parseParameters( $par, $opts ); $bits = preg_split( '/\s*,\s*/', trim( $par ) ); foreach ( $bits as $bit ) { if ( is_numeric( $bit ) ) { $opts['limit'] = $bit; } $m = []; if ( preg_match( '/^limit=(\d+)$/', $bit, $m ) ) { $opts['limit'] = $m[1]; } if ( preg_match( '/^days=(\d+)$/', $bit, $m ) ) { $opts['days'] = $m[1]; } if ( preg_match( '/^namespace=(.*)$/', $bit, $m ) ) { $opts['namespace'] = $m[1]; } if ( preg_match( '/^tagfilter=(.*)$/', $bit, $m ) ) { $opts['tagfilter'] = $m[1]; } } } public function validateOptions( FormOptions $opts ) { $opts->validateIntBounds( 'limit', 0, 5000 ); parent::validateOptions( $opts ); } /** * @inheritdoc */ protected function buildQuery( &$tables, &$fields, &$conds, &$query_options, &$join_conds, FormOptions $opts ) { $dbr = $this->getDB(); parent::buildQuery( $tables, $fields, $conds, $query_options, $join_conds, $opts ); // 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 ); } /** * @inheritdoc */ protected function doMainQuery( $tables, $fields, $conds, $query_options, $join_conds, FormOptions $opts ) { $dbr = $this->getDB(); $user = $this->getUser(); $tables[] = 'recentchanges'; $fields = array_merge( RecentChange::selectFields(), $fields ); // JOIN on watchlist for users if ( $user->isLoggedIn() && $user->isAllowed( 'viewmywatchlist' ) ) { $tables[] = 'watchlist'; $fields[] = 'wl_user'; $fields[] = 'wl_notificationtimestamp'; $join_conds['watchlist'] = [ 'LEFT JOIN', [ 'wl_user' => $user->getId(), 'wl_title=rc_title', 'wl_namespace=rc_namespace' ] ]; } // JOIN on page, used for 'last revision' filter highlight $tables[] = 'page'; $fields[] = 'page_latest'; $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ]; ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $query_options, $opts['tagfilter'] ); if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts ) ) { return false; } if ( $this->areFiltersInConflict() ) { return false; } // array_merge() is used intentionally here so that hooks can, should // they so desire, override the ORDER BY / LIMIT condition(s); prior to // MediaWiki 1.26 this used to use the plus operator instead, which meant // that extensions weren't able to change these conditions $query_options = array_merge( [ 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $opts['limit'] ], $query_options ); $rows = $dbr->select( $tables, $fields, // rc_new is not an ENUM, but adding a redundant rc_new IN (0,1) gives mysql enough // knowledge to use an index merge if it wants (it may use some other index though). $conds + [ 'rc_new' => [ 0, 1 ] ], __METHOD__, $query_options, $join_conds ); // Build the final data if ( $this->getConfig()->get( 'AllowCategorizedRecentChanges' ) ) { $this->filterByCategories( $rows, $opts ); } return $rows; } protected function runMainQueryHook( &$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts ) { return parent::runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds, $opts ) && Hooks::run( 'SpecialRecentChangesQuery', [ &$conds, &$tables, &$join_conds, $opts, &$query_options, &$fields ], '1.23' ); } protected function getDB() { return wfGetDB( DB_REPLICA, 'recentchanges' ); } public function outputFeedLinks() { $this->addFeedLinks( $this->getFeedQuery() ); } /** * Get URL query parameters for action=feedrecentchanges API feed of current recent changes view. * * @return array */ protected function getFeedQuery() { $query = array_filter( $this->getOptions()->getAllValues(), function ( $value ) { // API handles empty parameters in a different way return $value !== ''; } ); $query['action'] = 'feedrecentchanges'; $feedLimit = $this->getConfig()->get( 'FeedLimit' ); if ( $query['limit'] > $feedLimit ) { $query['limit'] = $feedLimit; } return $query; } /** * Build and output the actual changes list. * * @param ResultWrapper $rows Database rows * @param FormOptions $opts */ public function outputChangesList( $rows, $opts ) { $limit = $opts['limit']; $showWatcherCount = $this->getConfig()->get( 'RCShowWatchingUsers' ) && $this->getUser()->getOption( 'shownumberswatching' ); $watcherCache = []; $dbr = $this->getDB(); $counter = 1; $list = ChangesList::newFromContext( $this->getContext(), $this->filterGroups ); $list->initChangesListRows( $rows ); $userShowHiddenCats = $this->getUser()->getBoolOption( 'showhiddencats' ); $rclistOutput = $list->beginRecentChangesList(); if ( $this->isStructuredFilterUiEnabled() ) { $rclistOutput .= $this->makeLegend(); } foreach ( $rows as $obj ) { if ( $limit == 0 ) { break; } $rc = RecentChange::newFromRow( $obj ); # Skip CatWatch entries for hidden cats based on user preference if ( $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE && !$userShowHiddenCats && $rc->getParam( 'hidden-cat' ) ) { continue; } $rc->counter = $counter++; # Check if the page has been updated since the last visit if ( $this->getConfig()->get( 'ShowUpdatedMarker' ) && !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] = MediaWikiServices::getInstance()->getWatchedItemStore()->countWatchers( new TitleValue( (int)$obj->rc_namespace, $obj->rc_title ) ); } $rc->numberofWatchingusers = $watcherCache[$obj->rc_namespace][$obj->rc_title]; } $changeLine = $list->recentChangesLine( $rc, !empty( $obj->wl_user ), $counter ); if ( $changeLine !== false ) { $rclistOutput .= $changeLine; --$limit; } } $rclistOutput .= $list->endRecentChangesList(); if ( $rows->numRows() === 0 ) { $this->outputNoResults(); if ( !$this->including() ) { $this->getOutput()->setStatusCode( 404 ); } } else { $this->getOutput()->addHTML( $rclistOutput ); } } /** * Set the text to be displayed above the changes * * @param FormOptions $opts * @param int $numRows Number of rows in the result to show after this header */ public function doHeader( $opts, $numRows ) { $this->setTopText( $opts ); $defaults = $opts->getAllValues(); $nondefaults = $opts->getChangedValues(); $panel = []; if ( !$this->isStructuredFilterUiEnabled() ) { $panel[] = $this->makeLegend(); } $panel[] = $this->optionsPanel( $defaults, $nondefaults, $numRows ); $panel[] = '