Merge "RCFilters: Always replaceState the URL"
[lhc/web/wiklou.git] / includes / specialpage / ChangesListSpecialPage.php
index 1b92dc3..1b561ef 100644 (file)
@@ -255,79 +255,79 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                        // reviewStatus (conditional)
 
                        [
-                               'name' => 'lastRevision',
-                               'title' => 'rcfilters-filtergroup-lastRevision',
+                               'name' => 'significance',
+                               'title' => 'rcfilters-filtergroup-significance',
                                'class' => ChangesListBooleanFilterGroup::class,
-                               'priority' => -7,
+                               'priority' => -6,
                                'filters' => [
                                        [
-                                               'name' => 'hidelastrevision',
-                                               'label' => 'rcfilters-filter-lastrevision-label',
-                                               'description' => 'rcfilters-filter-lastrevision-description',
+                                               'name' => 'hideminor',
+                                               'label' => 'rcfilters-filter-minor-label',
+                                               'description' => 'rcfilters-filter-minor-description',
+                                               // rcshowhideminor-show, rcshowhideminor-hide,
+                                               // wlshowhideminor
+                                               'showHideSuffix' => 'showhideminor',
                                                'default' => false,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
                                                        &$query_options, &$join_conds ) {
-                                                       $conds[] = 'rc_this_oldid <> page_latest';
+
+                                                       $conds[] = 'rc_minor = 0';
                                                },
-                                               'cssClassSuffix' => 'last',
+                                               'cssClassSuffix' => 'minor',
                                                'isRowApplicableCallable' => function ( $ctx, $rc ) {
-                                                       return $rc->getAttribute( 'rc_this_oldid' ) === $rc->getAttribute( 'page_latest' );
+                                                       return $rc->getAttribute( 'rc_minor' );
                                                }
                                        ],
                                        [
-                                               'name' => 'hidepreviousrevisions',
-                                               'label' => 'rcfilters-filter-previousrevision-label',
-                                               'description' => 'rcfilters-filter-previousrevision-description',
+                                               'name' => 'hidemajor',
+                                               'label' => 'rcfilters-filter-major-label',
+                                               'description' => 'rcfilters-filter-major-description',
                                                'default' => false,
                                                'queryCallable' => function ( $specialClassName, $ctx, $dbr, &$tables, &$fields, &$conds,
                                                        &$query_options, &$join_conds ) {
-                                                       $conds[] = 'rc_this_oldid = page_latest';
+
+                                                       $conds[] = 'rc_minor = 1';
                                                },
-                                               'cssClassSuffix' => 'previous',
+                                               'cssClassSuffix' => 'major',
                                                'isRowApplicableCallable' => function ( $ctx, $rc ) {
-                                                       return $rc->getAttribute( 'rc_this_oldid' ) !== $rc->getAttribute( 'page_latest' );
+                                                       return !$rc->getAttribute( 'rc_minor' );
                                                }
                                        ]
                                ]
                        ],
 
                        [
-                               'name' => 'significance',
-                               'title' => 'rcfilters-filtergroup-significance',
+                               'name' => 'lastRevision',
+                               'title' => 'rcfilters-filtergroup-lastRevision',
                                'class' => ChangesListBooleanFilterGroup::class,
-                               'priority' => -6,
+                               'priority' => -7,
                                'filters' => [
                                        [
-                                               'name' => 'hideminor',
-                                               'label' => 'rcfilters-filter-minor-label',
-                                               'description' => 'rcfilters-filter-minor-description',
-                                               // rcshowhideminor-show, rcshowhideminor-hide,
-                                               // wlshowhideminor
-                                               'showHideSuffix' => 'showhideminor',
+                                               '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_minor = 0';
+                                                       $conds[] = 'rc_this_oldid <> page_latest';
                                                },
-                                               'cssClassSuffix' => 'minor',
+                                               'cssClassSuffix' => 'last',
                                                'isRowApplicableCallable' => function ( $ctx, $rc ) {
-                                                       return $rc->getAttribute( 'rc_minor' );
+                                                       return $rc->getAttribute( 'rc_this_oldid' ) === $rc->getAttribute( 'page_latest' );
                                                }
                                        ],
                                        [
-                                               'name' => 'hidemajor',
-                                               'label' => 'rcfilters-filter-major-label',
-                                               'description' => 'rcfilters-filter-major-description',
+                                               '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_minor = 1';
+                                                       $conds[] = 'rc_this_oldid = page_latest';
                                                },
-                                               'cssClassSuffix' => 'major',
+                                               'cssClassSuffix' => 'previous',
                                                'isRowApplicableCallable' => function ( $ctx, $rc ) {
-                                                       return !$rc->getAttribute( 'rc_minor' );
+                                                       return $rc->getAttribute( 'rc_this_oldid' ) !== $rc->getAttribute( 'page_latest' );
                                                }
                                        ]
                                ]
@@ -338,6 +338,7 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                'name' => 'changeType',
                                'title' => 'rcfilters-filtergroup-changetype',
                                'class' => ChangesListBooleanFilterGroup::class,
+                               'priority' => -8,
                                'filters' => [
                                        [
                                                'name' => 'hidepageedits',
@@ -393,94 +394,6 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                                ],
                        ],
 
-                       [
-                               'name' => 'watchlist',
-                               'title' => 'rcfilters-filtergroup-watchlist',
-                               'class' => ChangesListStringOptionsFilterGroup::class,
-                               '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' );
-                                               },
-                                       ],
-                                       [
-                                               '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;
-                                       }
-                               },
-                       ],
                ];
 
                $this->reviewStatusFilterGroupDefinition = [
@@ -698,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 );
                }
@@ -768,11 +681,6 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                        'rcfilters-hideminor-conflicts-typeofchange',
                        'rcfilters-typeofchange-conflicts-hideminor'
                );
-
-               $watchlistGroup = $this->getFilterGroup( 'watchlist' );
-               $watchlistGroup->getFilter( 'watched' )->setAsSupersetOf(
-                       $watchlistGroup->getFilter( 'watchednew' )
-               );
        }
 
        /**
@@ -822,10 +730,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',
@@ -883,23 +791,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;
        }
@@ -1052,7 +963,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;
        }
 
        /**
@@ -1090,25 +1084,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";
                }
        }
 
@@ -1138,15 +1135,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 )
                ) {
@@ -1340,9 +1328,11 @@ 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,
@@ -1361,7 +1351,9 @@ abstract class ChangesListSpecialPage extends SpecialPage {
                $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;
@@ -1369,7 +1361,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
                );
@@ -1377,7 +1369,8 @@ 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
                );