SpecialWatchlist: Don't overwrite context now that we don't have to
authorBartosz Dziewoński <matma.rex@gmail.com>
Thu, 7 Nov 2013 23:42:47 +0000 (00:42 +0100)
committerOri.livneh <ori@wikimedia.org>
Sat, 11 Jan 2014 01:04:48 +0000 (01:04 +0000)
Abstracted away backwards-compatibility URL parameter handling to
a separate method called before the options are fetched.

Change-Id: I54fd5f35262d86c936deac4d8cec0d2aebad10cd

includes/specials/SpecialRecentchanges.php
includes/specials/SpecialWatchlist.php

index 8a119f5..a4e0b85 100644 (file)
@@ -87,12 +87,11 @@ class SpecialRecentChanges extends SpecialPage {
                global $wgFeedLimit;
 
                $opts = $this->getDefaultOptions();
-
                foreach ( $this->getCustomFilters() as $key => $params ) {
                        $opts->add( $key, $params['default'] );
                }
 
-               $opts->fetchValuesFromRequest( $this->getRequest() );
+               $opts = $this->fetchOptionsFromRequest( $opts );
 
                // Give precedence to subpage syntax
                if ( $parameters !== null ) {
@@ -104,6 +103,19 @@ class SpecialRecentChanges extends SpecialPage {
                return $opts;
        }
 
+       /**
+        * Fetch values for a FormOptions object from the WebRequest associated with this instance.
+        *
+        * Intended for subclassing, e.g. to add a backwards-compatibility layer.
+        *
+        * @param FormOptions $parameters
+        * @return FormOptions
+        */
+       protected function fetchOptionsFromRequest( $opts ) {
+               $opts->fetchValuesFromRequest( $this->getRequest() );
+               return $opts;
+       }
+
        /**
         * Get custom show/hide filters
         *
index 49a1494..afdf981 100644 (file)
@@ -34,38 +34,6 @@ class SpecialWatchlist extends SpecialRecentChanges {
                return false;
        }
 
-       /**
-        * Map old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
-        * to the current ones.
-        *
-        * This creates derivative context and request, pokes with request's parameters, and sets them as
-        * the context for this class instance, mapping old keys to new ones completely transparently (as
-        * long as nothing tries to access the globals instead of current context).
-        */
-       private function mapCompatibilityRequestParameters() {
-               static $map = array(
-                       'hideMinor' => 'hideminor',
-                       'hideBots' => 'hidebots',
-                       'hideAnons' => 'hideanons',
-                       'hideLiu' => 'hideliu',
-                       'hidePatrolled' => 'hidepatrolled',
-                       'hideOwn' => 'hidemyself',
-               );
-
-               $params = $this->getRequest()->getValues();
-               foreach ( $map as $from => $to ) {
-                       if ( isset( $params[$from] ) ) {
-                               $params[$to] = $params[$from];
-                               unset( $params[$from] );
-                       }
-               }
-
-               $context = new DerivativeContext( $this->getContext() );
-               $request = new DerivativeRequest( $context->getRequest(), $params );
-               $context->setRequest( $request );
-               $this->setContext( $context );
-       }
-
        /**
         * Get a FormOptions object containing the default options
         *
@@ -91,6 +59,40 @@ class SpecialWatchlist extends SpecialRecentChanges {
                return $opts;
        }
 
+       /**
+        * Fetch values for a FormOptions object from the WebRequest associated with this instance.
+        *
+        * Maps old pre-1.23 request parameters Watchlist used to use (different from Recentchanges' ones)
+        * to the current ones.
+        *
+        * @param FormOptions $parameters
+        * @return FormOptions
+        */
+       protected function fetchOptionsFromRequest( $opts ) {
+               static $compatibilityMap = array(
+                       'hideMinor' => 'hideminor',
+                       'hideBots' => 'hidebots',
+                       'hideAnons' => 'hideanons',
+                       'hideLiu' => 'hideliu',
+                       'hidePatrolled' => 'hidepatrolled',
+                       'hideOwn' => 'hidemyself',
+               );
+
+               $params = $this->getRequest()->getValues();
+               foreach ( $compatibilityMap as $from => $to ) {
+                       if ( isset( $params[$from] ) ) {
+                               $params[$to] = $params[$from];
+                               unset( $params[$from] );
+                       }
+               }
+
+               // Not the prettiest way to achieve this… FormOptions internally depends on data sanitization
+               // methods defined on WebRequest and removing this dependency would cause some code duplication.
+               $request = new DerivativeRequest( $this->getRequest(), $params );
+               $opts->fetchValuesFromRequest( $request );
+               return $opts;
+       }
+
        /**
         * Get custom show/hide filters
         *
@@ -132,8 +134,6 @@ class SpecialWatchlist extends SpecialRecentChanges {
        function execute( $par ) {
                global $wgRCShowWatchingUsers, $wgEnotifWatchlist, $wgShowUpdatedMarker;
 
-               $this->mapCompatibilityRequestParameters();
-
                $user = $this->getUser();
                $output = $this->getOutput();
                $output->addModuleStyles( 'mediawiki.special.changeslist' );