Merge "Special:Newpages feed now shows first revision instead of latest revision"
[lhc/web/wiklou.git] / includes / specialpage / ChangesListSpecialPage.php
index 3aafc94..645fbb2 100644 (file)
@@ -22,6 +22,7 @@
  */
 use MediaWiki\Logger\LoggerFactory;
 use Wikimedia\Rdbms\ResultWrapper;
+use Wikimedia\Rdbms\FakeResultWrapper;
 use Wikimedia\Rdbms\IDatabase;
 
 /**
@@ -85,40 +86,30 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                'filters' => [
                                        [
                                                'name' => 'hideliu',
-                                               'label' => 'rcfilters-filter-registered-label',
-                                               'description' => 'rcfilters-filter-registered-description',
                                                // rcshowhideliu-show, rcshowhideliu-hide,
                                                // wlshowhideliu
                                                'showHideSuffix' => 'showhideliu',
                                                'default' => false,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
-                                                       &$query_options, &$join_conds ) {
-
+                                                       &$query_options, &$join_conds
+                                               ) {
                                                        $conds[] = 'rc_user = 0';
                                                },
-                                               'cssClassSuffix' => 'liu',
-                                               'isRowApplicableCallable' => function ( $ctx, $rc ) {
-                                                       return $rc->getAttribute( 'rc_user' );
-                                               },
+                                               'isReplacedInStructuredUi' => true,
 
                                        ],
                                        [
                                                'name' => 'hideanons',
-                                               'label' => 'rcfilters-filter-unregistered-label',
-                                               'description' => 'rcfilters-filter-unregistered-description',
                                                // rcshowhideanons-show, rcshowhideanons-hide,
                                                // wlshowhideanons
                                                'showHideSuffix' => 'showhideanons',
                                                'default' => false,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
-                                                       &$query_options, &$join_conds ) {
-
+                                                       &$query_options, &$join_conds
+                                               ) {
                                                        $conds[] = 'rc_user != 0';
                                                },
-                                               'cssClassSuffix' => 'anon',
-                                               'isRowApplicableCallable' => function ( $ctx, $rc ) {
-                                                       return !$rc->getAttribute( 'rc_user' );
-                                               },
+                                               'isReplacedInStructuredUi' => true,
                                        ]
                                ],
                        ],
@@ -127,9 +118,26 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                'name' => 'userExpLevel',
                                'title' => 'rcfilters-filtergroup-userExpLevel',
                                'class' => ChangesListStringOptionsFilterGroup::class,
-                               // Excludes unregistered users
-                               'isFullCoverage' => false,
+                               'isFullCoverage' => true,
                                'filters' => [
+                                       [
+                                               'name' => 'unregistered',
+                                               'label' => 'rcfilters-filter-user-experience-level-unregistered-label',
+                                               'description' => 'rcfilters-filter-user-experience-level-unregistered-description',
+                                               'cssClassSuffix' => 'user-unregistered',
+                                               'isRowApplicableCallable' => function ( $ctx, $rc ) {
+                                                       return !$rc->getAttribute( 'rc_user' );
+                                               }
+                                       ],
+                                       [
+                                               'name' => 'registered',
+                                               'label' => 'rcfilters-filter-user-experience-level-registered-label',
+                                               'description' => 'rcfilters-filter-user-experience-level-registered-description',
+                                               'cssClassSuffix' => 'user-registered',
+                                               'isRowApplicableCallable' => function ( $ctx, $rc ) {
+                                                       return $rc->getAttribute( 'rc_user' );
+                                               }
+                                       ],
                                        [
                                                'name' => 'newcomer',
                                                'label' => 'rcfilters-filter-user-experience-level-newcomer-label',
@@ -182,8 +190,8 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                                'showHideSuffix' => 'showhidemine',
                                                'default' => false,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
-                                                       &$query_options, &$join_conds ) {
-
+                                                       &$query_options, &$join_conds
+                                               ) {
                                                        $user = $ctx->getUser();
                                                        $conds[] = 'rc_user_text != ' . $dbr->addQuotes( $user->getName() );
                                                },
@@ -198,8 +206,8 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                                'description' => 'rcfilters-filter-editsbyother-description',
                                                'default' => false,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
-                                                       &$query_options, &$join_conds ) {
-
+                                                       &$query_options, &$join_conds
+                                               ) {
                                                        $user = $ctx->getUser();
                                                        $conds[] = 'rc_user_text = ' . $dbr->addQuotes( $user->getName() );
                                                },
@@ -225,8 +233,8 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                                'showHideSuffix' => 'showhidebots',
                                                'default' => false,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
-                                                       &$query_options, &$join_conds ) {
-
+                                                       &$query_options, &$join_conds
+                                               ) {
                                                        $conds[] = 'rc_bot = 0';
                                                },
                                                'cssClassSuffix' => 'bot',
@@ -240,8 +248,8 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                                'description' => 'rcfilters-filter-humans-description',
                                                'default' => false,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
-                                                       &$query_options, &$join_conds ) {
-
+                                                       &$query_options, &$join_conds
+                                               ) {
                                                        $conds[] = 'rc_bot = 1';
                                                },
                                                'cssClassSuffix' => 'human',
@@ -269,8 +277,8 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                                'showHideSuffix' => 'showhideminor',
                                                'default' => false,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
-                                                       &$query_options, &$join_conds ) {
-
+                                                       &$query_options, &$join_conds
+                                               ) {
                                                        $conds[] = 'rc_minor = 0';
                                                },
                                                'cssClassSuffix' => 'minor',
@@ -284,8 +292,8 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                                'description' => 'rcfilters-filter-major-description',
                                                'default' => false,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
-                                                       &$query_options, &$join_conds ) {
-
+                                                       &$query_options, &$join_conds
+                                               ) {
                                                        $conds[] = 'rc_minor = 1';
                                                },
                                                'cssClassSuffix' => 'major',
@@ -296,11 +304,49 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                ]
                        ],
 
+                       [
+                               'name' => 'lastRevision',
+                               'title' => 'rcfilters-filtergroup-lastRevision',
+                               'class' => ChangesListBooleanFilterGroup::class,
+                               'priority' => -7,
+                               'filters' => [
+                                       [
+                                               'name' => 'hidelastrevision',
+                                               'label' => 'rcfilters-filter-lastrevision-label',
+                                               'description' => 'rcfilters-filter-lastrevision-description',
+                                               'default' => false,
+                                               'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
+                                                       &$query_options, &$join_conds ) {
+                                                       $conds[] = 'rc_this_oldid <> page_latest';
+                                               },
+                                               'cssClassSuffix' => 'last',
+                                               'isRowApplicableCallable' => function ( $ctx, $rc ) {
+                                                       return $rc->getAttribute( 'rc_this_oldid' ) === $rc->getAttribute( 'page_latest' );
+                                               }
+                                       ],
+                                       [
+                                               'name' => 'hidepreviousrevisions',
+                                               'label' => 'rcfilters-filter-previousrevision-label',
+                                               'description' => 'rcfilters-filter-previousrevision-description',
+                                               'default' => false,
+                                               'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
+                                                       &$query_options, &$join_conds ) {
+                                                       $conds[] = 'rc_this_oldid = page_latest';
+                                               },
+                                               'cssClassSuffix' => 'previous',
+                                               'isRowApplicableCallable' => function ( $ctx, $rc ) {
+                                                       return $rc->getAttribute( 'rc_this_oldid' ) !== $rc->getAttribute( 'page_latest' );
+                                               }
+                                       ]
+                               ]
+                       ],
+
                        // With extensions, there can be change types that will not be hidden by any of these.
                        [
                                'name' => 'changeType',
                                'title' => 'rcfilters-filtergroup-changetype',
                                'class' => ChangesListBooleanFilterGroup::class,
+                               'priority' => -8,
                                'filters' => [
                                        [
                                                'name' => 'hidepageedits',
@@ -309,8 +355,8 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                                'default' => false,
                                                'priority' => -2,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
-                                                       &$query_options, &$join_conds ) {
-
+                                                       &$query_options, &$join_conds
+                                               ) {
                                                        $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_EDIT );
                                                },
                                                'cssClassSuffix' => 'src-mw-edit',
@@ -325,8 +371,8 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                                'default' => false,
                                                'priority' => -3,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
-                                                       &$query_options, &$join_conds ) {
-
+                                                       &$query_options, &$join_conds
+                                               ) {
                                                        $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_NEW );
                                                },
                                                'cssClassSuffix' => 'src-mw-new',
@@ -344,8 +390,8 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                                'default' => false,
                                                'priority' => -5,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
-                                                       &$query_options, &$join_conds ) {
-
+                                                       &$query_options, &$join_conds
+                                               ) {
                                                        $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_LOG );
                                                },
                                                'cssClassSuffix' => 'src-mw-log',
@@ -355,6 +401,7 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                        ],
                                ],
                        ],
+
                ];
 
                $this->reviewStatusFilterGroupDefinition = [
@@ -373,8 +420,8 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                                'showHideSuffix' => 'showhidepatr',
                                                'default' => false,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
-                                                       &$query_options, &$join_conds ) {
-
+                                                       &$query_options, &$join_conds
+                                               ) {
                                                        $conds[] = 'rc_patrolled = 0';
                                                },
                                                'cssClassSuffix' => 'patrolled',
@@ -388,8 +435,8 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                                'description' => 'rcfilters-filter-unpatrolled-description',
                                                'default' => false,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
-                                                       &$query_options, &$join_conds ) {
-
+                                                       &$query_options, &$join_conds
+                                               ) {
                                                        $conds[] = 'rc_patrolled = 1';
                                                },
                                                'cssClassSuffix' => 'unpatrolled',
@@ -411,8 +458,8 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                        'default' => false,
                        'priority' => -4,
                        'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
-                               &$query_options, &$join_conds ) {
-
+                               &$query_options, &$join_conds
+                       ) {
                                $conds[] = 'rc_type != ' . $dbr->addQuotes( RC_CATEGORIZE );
                        },
                        'cssClassSuffix' => 'src-mw-categorize',
@@ -431,7 +478,6 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                $opts = $this->getOptions();
                /** @var ChangesListFilterGroup $group */
                foreach ( $this->getFilterGroups() as $group ) {
-
                        if ( $group->getConflictingGroups() ) {
                                wfLogWarning(
                                        $group->getName() .
@@ -448,7 +494,6 @@ abstract class ChangesListSpecialPage extends SpecialPage {
 
                        /** @var ChangesListFilter $filter */
                        foreach ( $group->getFilters() as $filter ) {
-
                                /** @var ChangesListFilter $conflictingFilter */
                                foreach ( $filter->getConflictingFilters() as $conflictingFilter ) {
                                        if (
@@ -481,13 +526,7 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                $rows = $this->getRows();
                $opts = $this->getOptions();
                if ( $rows === false ) {
-                       if ( !$this->including() ) {
-                               $this->doHeader( $opts, 0 );
-                               $this->outputNoResults();
-                               $this->getOutput()->setStatusCode( 404 );
-                       }
-
-                       return;
+                       $rows = new FakeResultWrapper( [] );
                }
 
                $batch = new LinkBatch;
@@ -572,7 +611,7 @@ abstract class ChangesListSpecialPage extends SpecialPage {
 
                // Make sure this is not being transcluded (we don't want to show this
                // information to all users just because the user that saves the edit can
-               // patrol)
+               // patrol or is logged in)
                if ( !$this->including() && $this->getUser()->useRCPatrol() ) {
                        $this->registerFiltersFromDefinitions( $this->reviewStatusFilterGroupDefinition );
                }
@@ -600,19 +639,10 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                $this->registerFiltersFromDefinitions( [ $unstructuredGroupDefinition ] );
 
                $userExperienceLevel = $this->getFilterGroup( 'userExpLevel' );
-
-               $registration = $this->getFilterGroup( 'registration' );
-               $anons = $registration->getFilter( 'hideanons' );
-
-               // This means there is a conflict between any item in user experience level
-               // being checked and only anons being *shown* (hideliu=1&hideanons=0 in the
-               // URL, or equivalent).
-               $userExperienceLevel->conflictsWith(
-                       $anons,
-                       'rcfilters-filtergroup-user-experience-level-conflicts-unregistered-global',
-                       'rcfilters-filtergroup-user-experience-level-conflicts-unregistered',
-                       'rcfilters-filter-unregistered-conflicts-user-experience-level'
-               );
+               $registered = $userExperienceLevel->getFilter( 'registered' );
+               $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'newcomer' ) );
+               $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'learner' ) );
+               $registered->setAsSupersetOf( $userExperienceLevel->getFilter( 'experienced' ) );
 
                $categoryFilter = $changeTypeGroup->getFilter( 'hidecategorization' );
                $logactionsFilter = $changeTypeGroup->getFilter( 'hidelog' );
@@ -691,10 +721,10 @@ abstract class ChangesListSpecialPage extends SpecialPage {
        /**
         * Get filter group definition from legacy custom filters
         *
-        * @param array Custom filters from legacy hooks
+        * @param array $customFilters Custom filters from legacy hooks
         * @return array Group definition
         */
-       protected function getFilterGroupDefinitionFromLegacyCustomFilters( $customFilters ) {
+       protected function getFilterGroupDefinitionFromLegacyCustomFilters( array $customFilters ) {
                // Special internal unstructured group
                $unstructuredGroupDefinition = [
                        'name' => 'unstructured',
@@ -752,23 +782,26 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                $config = $this->getConfig();
                $opts = new FormOptions();
                $structuredUI = $this->getUser()->getOption( 'rcenhancedfilters' );
+               // If urlversion=2 is set, ignore the filter defaults and set them all to false/empty
+               $useDefaults = $this->getRequest()->getInt( 'urlversion' ) !== 2;
 
                // Add all filters
                foreach ( $this->filterGroups as $filterGroup ) {
                        // URL parameters can be per-group, like 'userExpLevel',
                        // or per-filter, like 'hideminor'.
                        if ( $filterGroup->isPerGroupRequestParameter() ) {
-                               $opts->add( $filterGroup->getName(), $filterGroup->getDefault() );
+                               $opts->add( $filterGroup->getName(), $useDefaults ? $filterGroup->getDefault() : '' );
                        } else {
                                foreach ( $filterGroup->getFilters() as $filter ) {
-                                       $opts->add( $filter->getName(), $filter->getDefault( $structuredUI ) );
+                                       $opts->add( $filter->getName(), $useDefaults ? $filter->getDefault( $structuredUI ) : false );
                                }
                        }
                }
 
-               $opts->add( 'namespace', '', FormOptions::INTNULL );
+               $opts->add( 'namespace', '', FormOptions::STRING );
                $opts->add( 'invert', false );
                $opts->add( 'associated', false );
+               $opts->add( 'urlversion', 1 );
 
                return $opts;
        }
@@ -921,7 +954,90 @@ abstract class ChangesListSpecialPage extends SpecialPage {
         * @param FormOptions $opts
         */
        public function validateOptions( FormOptions $opts ) {
-               // nothing by default
+               if ( $this->fixContradictoryOptions( $opts ) ) {
+                       $query = wfArrayToCgi( $this->convertParamsForLink( $opts->getChangedValues() ) );
+                       $this->getOutput()->redirect( $this->getPageTitle()->getCanonicalURL( $query ) );
+               }
+       }
+
+       /**
+        * Fix invalid options by resetting pairs that should never appear together.
+        *
+        * @param FormOptions $opts
+        * @return bool True if any option was reset
+        */
+       private function fixContradictoryOptions( FormOptions $opts ) {
+               $fixed = $this->fixBackwardsCompatibilityOptions( $opts );
+
+               foreach ( $this->filterGroups as $filterGroup ) {
+                       if ( $filterGroup instanceof ChangesListBooleanFilterGroup ) {
+                               $filters = $filterGroup->getFilters();
+
+                               if ( count( $filters ) === 1 ) {
+                                       // legacy boolean filters should not be considered
+                                       continue;
+                               }
+
+                               $allInGroupEnabled = array_reduce(
+                                       $filters,
+                                       function ( $carry, $filter ) use ( $opts ) {
+                                               return $carry && $opts[ $filter->getName() ];
+                                       },
+                                       /* initialValue */ count( $filters ) > 0
+                               );
+
+                               if ( $allInGroupEnabled ) {
+                                       foreach ( $filters as $filter ) {
+                                               $opts[ $filter->getName() ] = false;
+                                       }
+
+                                       $fixed = true;
+                               }
+                       }
+               }
+
+               return $fixed;
+       }
+
+       /**
+        * Fix a special case (hideanons=1 and hideliu=1) in a special way, for backwards
+        * compatibility.
+        *
+        * This is deprecated and may be removed.
+        *
+        * @param FormOptions $opts
+        * @return bool True if this change was mode
+        */
+       private function fixBackwardsCompatibilityOptions( FormOptions $opts ) {
+               if ( $opts['hideanons'] && $opts['hideliu'] ) {
+                       $opts->reset( 'hideanons' );
+                       if ( !$opts['hidebots'] ) {
+                               $opts->reset( 'hideliu' );
+                               $opts['hidehumans'] = 1;
+                       }
+
+                       return true;
+               }
+
+               return false;
+       }
+
+       /**
+        * Convert parameters values from true/false to 1/0
+        * so they are not omitted by wfArrayToCgi()
+        * Bug 36524
+        *
+        * @param array $params
+        * @return array
+        */
+       protected function convertParamsForLink( $params ) {
+               foreach ( $params as &$value ) {
+                       if ( $value === false ) {
+                               $value = '0';
+                       }
+               }
+               unset( $value );
+               return $params;
        }
 
        /**
@@ -936,8 +1052,8 @@ abstract class ChangesListSpecialPage extends SpecialPage {
         * @param FormOptions $opts
         */
        protected function buildQuery( &$tables, &$fields, &$conds, &$query_options,
-               &$join_conds, FormOptions $opts ) {
-
+               &$join_conds, FormOptions $opts
+       ) {
                $dbr = $this->getDB();
                $user = $this->getUser();
 
@@ -959,25 +1075,28 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                }
 
                // Namespace filtering
-               if ( $opts['namespace'] !== '' ) {
-                       $selectedNS = $dbr->addQuotes( $opts['namespace'] );
-                       $operator = $opts['invert'] ? '!=' : '=';
-                       $boolean = $opts['invert'] ? 'AND' : 'OR';
-
-                       // Namespace association (T4429)
-                       if ( !$opts['associated'] ) {
-                               $condition = "rc_namespace $operator $selectedNS";
-                       } else {
-                               // Also add the associated namespace
-                               $associatedNS = $dbr->addQuotes(
-                                       MWNamespace::getAssociated( $opts['namespace'] )
+               if ( $opts[ 'namespace' ] !== '' ) {
+                       $namespaces = explode( ';', $opts[ 'namespace' ] );
+
+                       if ( $opts[ 'associated' ] ) {
+                               $associatedNamespaces = array_map(
+                                       function ( $ns ) {
+                                               return MWNamespace::getAssociated( $ns );
+                                       },
+                                       $namespaces
                                );
-                               $condition = "(rc_namespace $operator $selectedNS "
-                                       . $boolean
-                                       . " rc_namespace $operator $associatedNS)";
+                               $namespaces = array_unique( array_merge( $namespaces, $associatedNamespaces ) );
                        }
 
-                       $conds[] = $condition;
+                       if ( count( $namespaces ) === 1 ) {
+                               $operator = $opts[ 'invert' ] ? '!=' : '=';
+                               $value = $dbr->addQuotes( reset( $namespaces ) );
+                       } else {
+                               $operator = $opts[ 'invert' ] ? 'NOT IN' : 'IN';
+                               sort( $namespaces );
+                               $value = '(' . $dbr->makeList( $namespaces ) . ')';
+                       }
+                       $conds[] = "rc_namespace $operator $value";
                }
        }
 
@@ -993,8 +1112,8 @@ abstract class ChangesListSpecialPage extends SpecialPage {
         * @return bool|ResultWrapper Result or false
         */
        protected function doMainQuery( $tables, $fields, $conds,
-               $query_options, $join_conds, FormOptions $opts ) {
-
+               $query_options, $join_conds, FormOptions $opts
+       ) {
                $tables[] = 'recentchanges';
                $fields = array_merge( RecentChange::selectFields(), $fields );
 
@@ -1007,15 +1126,6 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                        ''
                );
 
-               // It makes no sense to hide both anons and logged-in users. When this occurs, try a guess on
-               // what the user meant and either show only bots or force anons to be shown.
-
-               // -------
-
-               // XXX: We're no longer doing this handling.  To preserve back-compat, we need to complete
-               // T151873 (particularly the hideanons/hideliu/hidebots/hidehumans part) in conjunction
-               // with merging this.
-
                if ( !$this->runMainQueryHook( $tables, $fields, $conds, $query_options, $join_conds,
                        $opts )
                ) {
@@ -1170,9 +1280,14 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                $legend .= Html::closeElement( 'dl' ) . "\n";
 
                # Collapsibility
+               $legendHeading = $this->getUser()->getOption(
+                       'rcenhancedfilters'
+               ) ?
+                       $context->msg( 'rcfilters-legend-heading' )->parse() :
+                       $context->msg( 'recentchanges-legend-heading' )->parse();
                $legend =
                        '<div class="mw-changeslist-legend">' .
-                               $context->msg( 'recentchanges-legend-heading' )->parse() .
+                               $legendHeading .
                                '<div class="mw-collapsible-content">' . $legend . '</div>' .
                        '</div>';
 
@@ -1209,28 +1324,52 @@ abstract class ChangesListSpecialPage extends SpecialPage {
         * @param array &$query_options Array of query options; see IDatabase::select $options
         * @param array &$join_conds Array of join conditions; see IDatabase::select $join_conds
         * @param array $selectedExpLevels The allowed active values, sorted
+        * @param int $now Number of seconds since the UNIX epoch, or 0 if not given
+        *   (optional)
         */
        public function filterOnUserExperienceLevel( $specialPageClassName, $context, $dbr,
-               &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selectedExpLevels ) {
-
+               &$tables, &$fields, &$conds, &$query_options, &$join_conds, $selectedExpLevels, $now = 0
+       ) {
                global $wgLearnerEdits,
                        $wgExperiencedUserEdits,
                        $wgLearnerMemberSince,
                        $wgExperiencedUserMemberSince;
 
-               $LEVEL_COUNT = 3;
+               $LEVEL_COUNT = 5;
 
-               // If all levels are selected, all logged-in users are included (but no
-               // anons), so we can short-circuit.
+               // If all levels are selected, don't filter
                if ( count( $selectedExpLevels ) === $LEVEL_COUNT ) {
+                       return;
+               }
+
+               // both 'registered' and 'unregistered', experience levels, if any, are included in 'registered'
+               if (
+                       in_array( 'registered', $selectedExpLevels ) &&
+                       in_array( 'unregistered', $selectedExpLevels )
+               ) {
+                       return;
+               }
+
+               // 'registered' but not 'unregistered', experience levels, if any, are included in 'registered'
+               if (
+                       in_array( 'registered', $selectedExpLevels ) &&
+                       !in_array( 'unregistered', $selectedExpLevels )
+               ) {
                        $conds[] = 'rc_user != 0';
                        return;
                }
 
+               if ( $selectedExpLevels === [ 'unregistered' ] ) {
+                       $conds[] = 'rc_user = 0';
+                       return;
+               }
+
                $tables[] = 'user';
                $join_conds['user'] = [ 'LEFT JOIN', 'rc_user = user_id' ];
 
-               $now = time();
+               if ( $now === 0 ) {
+                       $now = time();
+               }
                $secondsPerDay = 86400;
                $learnerCutoff = $now - $wgLearnerMemberSince * $secondsPerDay;
                $experiencedUserCutoff = $now - $wgExperiencedUserMemberSince * $secondsPerDay;
@@ -1238,7 +1377,7 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                $aboveNewcomer = $dbr->makeList(
                        [
                                'user_editcount >= ' . intval( $wgLearnerEdits ),
-                               'user_registration <= ' . $dbr->timestamp( $learnerCutoff ),
+                               'user_registration <= ' . $dbr->addQuotes( $dbr->timestamp( $learnerCutoff ) ),
                        ],
                        IDatabase::LIST_AND
                );
@@ -1246,29 +1385,45 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                $aboveLearner = $dbr->makeList(
                        [
                                'user_editcount >= ' . intval( $wgExperiencedUserEdits ),
-                               'user_registration <= ' . $dbr->timestamp( $experiencedUserCutoff ),
+                               'user_registration <= ' .
+                                       $dbr->addQuotes( $dbr->timestamp( $experiencedUserCutoff ) ),
                        ],
                        IDatabase::LIST_AND
                );
 
+               $conditions = [];
+
+               if ( in_array( 'unregistered', $selectedExpLevels ) ) {
+                       $selectedExpLevels = array_diff( $selectedExpLevels, [ 'unregistered' ] );
+                       $conditions[] = 'rc_user = 0';
+               }
+
                if ( $selectedExpLevels === [ 'newcomer' ] ) {
-                       $conds[] = "NOT ( $aboveNewcomer )";
+                       $conditions[] = "NOT ( $aboveNewcomer )";
                } elseif ( $selectedExpLevels === [ 'learner' ] ) {
-                       $conds[] = $dbr->makeList(
+                       $conditions[] = $dbr->makeList(
                                [ $aboveNewcomer, "NOT ( $aboveLearner )" ],
                                IDatabase::LIST_AND
                        );
                } elseif ( $selectedExpLevels === [ 'experienced' ] ) {
-                       $conds[] = $aboveLearner;
+                       $conditions[] = $aboveLearner;
                } elseif ( $selectedExpLevels === [ 'learner', 'newcomer' ] ) {
-                       $conds[] = "NOT ( $aboveLearner )";
+                       $conditions[] = "NOT ( $aboveLearner )";
                } elseif ( $selectedExpLevels === [ 'experienced', 'newcomer' ] ) {
-                       $conds[] = $dbr->makeList(
+                       $conditions[] = $dbr->makeList(
                                [ "NOT ( $aboveNewcomer )", $aboveLearner ],
                                IDatabase::LIST_OR
                        );
                } elseif ( $selectedExpLevels === [ 'experienced', 'learner' ] ) {
-                       $conds[] = $aboveNewcomer;
+                       $conditions[] = $aboveNewcomer;
+               } elseif ( $selectedExpLevels === [ 'experienced', 'learner', 'newcomer' ] ) {
+                       $conditions[] = 'rc_user != 0';
+               }
+
+               if ( count( $conditions ) > 1 ) {
+                       $conds[] = $dbr->makeList( $conditions, IDatabase::LIST_OR );
+               } elseif ( count( $conditions ) === 1 ) {
+                       $conds[] = reset( $conditions );
                }
        }
 }