From d25a944635d9447ae223fca320d41046cd8dfe17 Mon Sep 17 00:00:00 2001 From: Moriel Schottlender Date: Fri, 18 Aug 2017 14:30:17 -0700 Subject: [PATCH] RCFilters: Load default saved query if it exists Bug: T166908 Change-Id: Ic3931bc0d67b340db11ed100aad836e8d867fa56 --- .../specialpage/ChangesListSpecialPage.php | 93 ++++++++++++++++--- .../mw.rcfilters.Controller.js | 15 ++- 2 files changed, 85 insertions(+), 23 deletions(-) diff --git a/includes/specialpage/ChangesListSpecialPage.php b/includes/specialpage/ChangesListSpecialPage.php index dcd14e8098..df6a1c173c 100644 --- a/includes/specialpage/ChangesListSpecialPage.php +++ b/includes/specialpage/ChangesListSpecialPage.php @@ -540,6 +540,8 @@ abstract class ChangesListSpecialPage extends SpecialPage { public function execute( $subpage ) { $this->rcSubpage = $subpage; + $this->considerActionsForDefaultSavedQuery(); + $rows = $this->getRows(); $opts = $this->getOptions(); if ( $rows === false ) { @@ -591,6 +593,77 @@ abstract class ChangesListSpecialPage extends SpecialPage { $this->includeRcFiltersApp(); } + /** + * Check whether or not the page should load defaults, and if so, whether + * a default saved query is relevant to be redirected to. If it is relevant, + * redirect properly with all necessary query parameters. + */ + protected function considerActionsForDefaultSavedQuery() { + if ( !$this->isStructuredFilterUiEnabled() ) { + return; + } + + $knownParams = call_user_func_array( + [ $this->getRequest(), 'getValues' ], + array_keys( $this->getOptions()->getAllValues() ) + ); + + // HACK: Temporarily until we can properly define "sticky" filters and parameters, + // we need to exclude several parameters we know should not be counted towards preventing + // the loading of defaults. + $excludedParams = [ 'limit' => '', 'days' => '', 'enhanced' => '', 'from' => '' ]; + $knownParams = array_diff_key( $knownParams, $excludedParams ); + + if ( + // If there are NO known parameters in the URL request + // (that are not excluded) then we need to check into loading + // the default saved query + count( $knownParams ) === 0 + ) { + // Get the saved queries data and parse it + $savedQueries = FormatJson::decode( + $this->getUser()->getOption( static::$savedQueriesPreferenceName ), + true + ); + + if ( $savedQueries && isset( $savedQueries[ 'default' ] ) ) { + // Only load queries that are 'version' 2, since those + // have parameter representation + if ( $savedQueries[ 'version' ] === '2' ) { + $savedQueryDefaultID = $savedQueries[ 'default' ]; + $defaultQuery = $savedQueries[ 'queries' ][ $savedQueryDefaultID ][ 'data' ]; + + // Build the entire parameter list + $query = array_merge( + $defaultQuery[ 'params' ], + $defaultQuery[ 'highlights' ], + [ + 'urlversion' => '2', + ] + ); + // Add to the query any parameters that we may have ignored before + // but are still valid and requested in the URL + $query = array_merge( $this->getRequest()->getValues(), $query ); + unset( $query[ 'title' ] ); + $this->getOutput()->redirect( $this->getPageTitle()->getCanonicalURL( $query ) ); + } else { + // There's a default, but the version is not 2, and the server can't + // actually recognize the query itself. This happens if it is before + // the conversion, so we need to tell the UI to reload saved query as + // it does the conversion to version 2 + $this->getOutput()->addJsConfigVars( + 'wgStructuredChangeFiltersDefaultSavedQueryExists', + true + ); + + // Add the class that tells the frontend it is still loading + // another query + $this->getOutput()->addBodyClasses( 'mw-rcfilters-ui-loading' ); + } + } + } + } + /** * Include the modules and configuration for the RCFilters app. * Conditional on the user having the feature enabled. @@ -632,25 +705,15 @@ abstract class ChangesListSpecialPage extends SpecialPage { ] ); + $out->addJsConfigVars( + 'wgStructuredChangeFiltersSavedQueriesPreferenceName', + static::$savedQueriesPreferenceName + ); + $out->addJsConfigVars( 'StructuredChangeFiltersLiveUpdatePollingRate', $this->getConfig()->get( 'StructuredChangeFiltersLiveUpdatePollingRate' ) ); - - if ( static::$savedQueriesPreferenceName ) { - $savedQueries = FormatJson::decode( - $this->getUser()->getOption( static::$savedQueriesPreferenceName ) - ); - if ( $savedQueries && isset( $savedQueries->default ) ) { - // If there is a default saved query, show a loading spinner, - // since the frontend is going to reload the results - $out->addBodyClasses( 'mw-rcfilters-ui-loading' ); - } - $out->addJsConfigVars( - 'wgStructuredChangeFiltersSavedQueriesPreferenceName', - static::$savedQueriesPreferenceName - ); - } } else { $out->addBodyClasses( 'mw-rcfilters-disabled' ); } diff --git a/resources/src/mediawiki.rcfilters/mw.rcfilters.Controller.js b/resources/src/mediawiki.rcfilters/mw.rcfilters.Controller.js index 685adb6811..3e7172930a 100644 --- a/resources/src/mediawiki.rcfilters/mw.rcfilters.Controller.js +++ b/resources/src/mediawiki.rcfilters/mw.rcfilters.Controller.js @@ -43,6 +43,7 @@ mw.rcfilters.Controller.prototype.initialize = function ( filterStructure, namespaceStructure, tagList ) { var parsedSavedQueries, displayConfig = mw.config.get( 'StructuredChangeFiltersDisplayConfig' ), + defaultSavedQueryExists = mw.config.get( 'wgStructuredChangeFiltersDefaultSavedQueryExists' ), controller = this, views = {}, items = [], @@ -231,14 +232,12 @@ // Defaults should only be applied on load (if necessary) // or on request this.initializing = true; - if ( - !mw.user.isAnon() && this.savedQueriesModel.getDefault() && - !this.uriProcessor.doesQueryContainRecognizedParams( uri.query ) - ) { - // We have defaults from a saved query. - // We will load them straight-forward (as if - // they were clicked in the menu) so we trigger - // a full ajax request and change of URL + + if ( defaultSavedQueryExists ) { + // This came from the server, meaning that we have a default + // saved query, but the server could not load it, probably because + // it was pre-conversion to the new format. + // We need to load this query again this.applySavedQuery( this.savedQueriesModel.getDefault() ); } else { // There are either recognized parameters in the URL -- 2.20.1