Bartosz Dziewoński has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/94288


Change subject: SpecialWatchlist: Use FormOptions for parameter handling
......................................................................

SpecialWatchlist: Use FormOptions for parameter handling

The SpecialWatchlistQuery hook now gets a FormOptions object as the last
parameter instead of an array (which is not bad as bad as it sounds, since
the class implements ArrayAccess).

Change-Id: Iec4aab870b6e9aa98d2b118010e78e25b1dbe437
---
M docs/hooks.txt
M includes/specials/SpecialWatchlist.php
2 files changed, 82 insertions(+), 64 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/88/94288/2

diff --git a/docs/hooks.txt b/docs/hooks.txt
index 7bd725a..3b6edb9 100644
--- a/docs/hooks.txt
+++ b/docs/hooks.txt
@@ -2374,7 +2374,7 @@
 &$tables: array of tables to be queried
 &$join_conds: join conditions for the tables
 &$fields: array of query fields
-$values: array of variables with watchlist options
+$opts: A FormOptions object with watchlist options for the current request
 
 'SpecialWatchlistGetNonRevisionTypes': Called when building sql query for
 SpecialWatchlist. Allows extensions to register custom values they have 
diff --git a/includes/specials/SpecialWatchlist.php 
b/includes/specials/SpecialWatchlist.php
index a3b83ec..30fa0ec 100644
--- a/includes/specials/SpecialWatchlist.php
+++ b/includes/specials/SpecialWatchlist.php
@@ -67,6 +67,65 @@
        }
 
        /**
+        * Get a FormOptions object containing the default options
+        *
+        * @return FormOptions
+        */
+       public function getDefaultOptions() {
+               $opts = parent::getDefaultOptions();
+               $user = $this->getUser();
+
+               // Overwrite RC options with Watchlist options
+               // (calling #add() again is okay)
+               $opts->add( 'days', $user->getOption( 'watchlistdays' ), 
FormOptions::FLOAT );
+               $opts->add( 'hideminor', $user->getBoolOption( 
'watchlisthideminor' ) );
+               $opts->add( 'hidebots', $user->getBoolOption( 
'watchlisthidebots' ) );
+               $opts->add( 'hideanons', $user->getBoolOption( 
'watchlisthideanons' ) );
+               $opts->add( 'hideliu', $user->getBoolOption( 'watchlisthideliu' 
) );
+               $opts->add( 'hidepatrolled', $user->getBoolOption( 
'watchlisthidepatrolled' ) );
+               $opts->add( 'hidemyself', $user->getBoolOption( 
'watchlisthideown' ) );
+
+               // Add new ones
+               $opts->add( 'extended', $user->getBoolOption( 'extendwatchlist' 
) );
+
+               return $opts;
+       }
+
+       /**
+        * Get custom show/hide filters
+        *
+        * @return array Map of filter URL param names to properties 
(msg/default)
+        */
+       protected function getCustomFilters() {
+               if ( $this->customFilters === null ) {
+                       $this->customFilters = array();
+                       wfRunHooks( 'SpecialWatchlistFilters', array( $this, 
&$this->customFilters ) );
+               }
+
+               return $this->customFilters;
+       }
+
+       /**
+        * Process $par and put options found if $opts. Not used for Watchlist.
+        *
+        * @param string $par
+        * @param FormOptions $opts
+        */
+       public function parseParameters( $par, FormOptions $opts ) {
+       }
+
+       /**
+        * Get the current FormOptions for this request
+        */
+       public function getOptions() {
+               if ( $this->rcOptions === null ) {
+                       $this->rcOptions = $this->setup( null );
+               }
+
+               return $this->rcOptions;
+       }
+
+       /**
         * Execute
         * @param $par Parameter passed to the page
         */
@@ -106,6 +165,7 @@
                        ) );
                }
 
+               $opts = $this->getOptions();
                $this->setHeaders();
                $this->outputHeader();
 
@@ -136,49 +196,14 @@
                        return;
                }
 
-               // @todo use FormOptions!
-               $defaults = array(
-               /* float */ 'days' => floatval( $user->getOption( 
'watchlistdays' ) ),
-               /* bool  */ 'hideminor' => (int)$user->getBoolOption( 
'watchlisthideminor' ),
-               /* bool  */ 'hidebots' => (int)$user->getBoolOption( 
'watchlisthidebots' ),
-               /* bool  */ 'hideanons' => (int)$user->getBoolOption( 
'watchlisthideanons' ),
-               /* bool  */ 'hideliu' => (int)$user->getBoolOption( 
'watchlisthideliu' ),
-               /* bool  */ 'hidepatrolled' => (int)$user->getBoolOption( 
'watchlisthidepatrolled' ),
-               /* bool  */ 'hidemyself' => (int)$user->getBoolOption( 
'watchlisthideown' ),
-               /* bool  */ 'extended' => (int)$user->getBoolOption( 
'extendwatchlist' ),
-               /* ?     */ 'namespace' => '', //means all
-               /* ?     */ 'invert' => false,
-               /* bool  */ 'associated' => false,
-               );
-               $this->customFilters = array();
-               wfRunHooks( 'SpecialWatchlistFilters', array( $this, 
&$this->customFilters ) );
-               foreach ( $this->customFilters as $key => $params ) {
-                       $defaults[$key] = $params['default'];
-               }
-
-               # Extract variables from the request, falling back to user 
preferences or
-               # other default values if these don't exist
-               $values = array();
-               $values['days'] = floatval( $request->getVal( 'days', 
$defaults['days'] ) );
-               $values['hideminor'] = (int)$request->getBool( 'hideminor', 
$defaults['hideminor'] );
-               $values['hidebots'] = (int)$request->getBool( 'hidebots', 
$defaults['hidebots'] );
-               $values['hideanons'] = (int)$request->getBool( 'hideanons', 
$defaults['hideanons'] );
-               $values['hideliu'] = (int)$request->getBool( 'hideliu', 
$defaults['hideliu'] );
-               $values['hidemyself'] = (int)$request->getBool( 'hidemyself', 
$defaults['hidemyself'] );
-               $values['hidepatrolled'] = (int)$request->getBool( 
'hidepatrolled', $defaults['hidepatrolled'] );
-               $values['extended'] = (int)$request->getBool( 'extended', 
$defaults['extended'] );
-               foreach ( $this->customFilters as $key => $params ) {
-                       $values[$key] = (int)$request->getBool( $key, 
$defaults[$key] );
-               }
-
                # Get namespace value, if supplied, and prepare a WHERE fragment
-               $nameSpace = $request->getIntOrNull( 'namespace' );
-               $invert = $request->getBool( 'invert' );
-               $associated = $request->getBool( 'associated' );
-               if ( !is_null( $nameSpace ) ) {
+               $nameSpace = $opts['namespace'];
+               $invert = $opts['invert'];
+               $associated = $opts['associated'];
+
+               if ( $nameSpace !== '' ) {
                        $eq_op = $invert ? '!=' : '=';
                        $bool_op = $invert ? 'AND' : 'OR';
-                       $nameSpace = intval( $nameSpace ); // paranioa
                        if ( !$associated ) {
                                $nameSpaceClause = "rc_namespace $eq_op 
$nameSpace";
                        } else {
@@ -189,18 +214,11 @@
                                        " rc_namespace $eq_op $associatedNS";
                        }
                } else {
-                       $nameSpace = '';
                        $nameSpaceClause = '';
                }
-               $values['namespace'] = $nameSpace;
-               $values['invert'] = $invert;
-               $values['associated'] = $associated;
 
                // Dump everything here
-               $nondefaults = array();
-               foreach ( $defaults as $name => $defValue ) {
-                       wfAppendToArrayIfNotDefault( $name, $values[$name], 
$defaults, $nondefaults );
-               }
+               $nondefaults = $opts->getChangedValues();
 
                if ( ( $wgEnotifWatchlist || $wgShowUpdatedMarker ) && 
$request->getVal( 'reset' ) &&
                        $request->wasPosted() )
@@ -213,27 +231,27 @@
                # Possible where conditions
                $conds = array();
 
-               if ( $values['days'] > 0 ) {
-                       $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( 
$dbr->timestamp( time() - intval( $values['days'] * 86400 ) ) );
+               if ( $opts['days'] > 0 ) {
+                       $conds[] = 'rc_timestamp > ' . $dbr->addQuotes( 
$dbr->timestamp( time() - intval( $opts['days'] * 86400 ) ) );
                }
 
                # Toggles
-               if ( $values['hidemyself'] ) {
+               if ( $opts['hidemyself'] ) {
                        $conds[] = 'rc_user != ' . $user->getId();
                }
-               if ( $values['hidebots'] ) {
+               if ( $opts['hidebots'] ) {
                        $conds[] = 'rc_bot = 0';
                }
-               if ( $values['hideminor'] ) {
+               if ( $opts['hideminor'] ) {
                        $conds[] = 'rc_minor = 0';
                }
-               if ( $values['hideliu'] ) {
+               if ( $opts['hideliu'] ) {
                        $conds[] = 'rc_user = 0';
                }
-               if ( $values['hideanons'] ) {
+               if ( $opts['hideanons'] ) {
                        $conds[] = 'rc_user != 0';
                }
-               if ( $user->useRCPatrol() && $values['hidepatrolled'] ) {
+               if ( $user->useRCPatrol() && $opts['hidepatrolled'] ) {
                        $conds[] = 'rc_patrolled != 1';
                }
                if ( $nameSpaceClause ) {
@@ -241,7 +259,7 @@
                }
 
                # Toggle watchlist content (all recent edits or just the latest)
-               if ( $values['extended'] ) {
+               if ( $opts['extended'] ) {
                        $limitWatchlist = $user->getIntOption( 'wllimit' );
                        $usePage = false;
                } else {
@@ -334,7 +352,7 @@
                }
 
                ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, 
$join_conds, $options, '' );
-               wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, 
&$join_conds, &$fields, $values ) );
+               wfRunHooks( 'SpecialWatchlistQuery', array( &$conds, &$tables, 
&$join_conds, &$fields, $opts ) );
 
                $res = $dbr->select( $tables, $fields, $conds, __METHOD__, 
$options, $join_conds );
                $numRows = $res->numRows();
@@ -343,13 +361,13 @@
 
                $lang = $this->getLanguage();
                $wlInfo = '';
-               if ( $values['days'] > 0 ) {
+               if ( $opts['days'] > 0 ) {
                        $timestamp = wfTimestampNow();
-                       $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, 
round( $values['days'] * 24 ) )->params(
+                       $wlInfo = $this->msg( 'wlnote' )->numParams( $numRows, 
round( $opts['days'] * 24 ) )->params(
                                $lang->userDate( $timestamp, $user ), 
$lang->userTime( $timestamp, $user ) )->parse() . "<br />\n";
                }
 
-               $cutofflinks = $this->cutoffLinks( $values['days'], 
$nondefaults ) . "<br />\n";
+               $cutofflinks = $this->cutoffLinks( $opts['days'], $nondefaults 
) . "<br />\n";
 
                # Spit out some control panel links
                $filters = array(
@@ -360,7 +378,7 @@
                        'hidemyself' => 'rcshowhidemine',
                        'hidepatrolled' => 'rcshowhidepatr'
                );
-               foreach ( $this->customFilters as $key => $params ) {
+               foreach ( $this->getCustomFilters() as $key => $params ) {
                        $filters[$key] = $params['msg'];
                }
                // Disable some if needed
@@ -370,7 +388,7 @@
 
                $links = array();
                foreach ( $filters as $name => $msg ) {
-                       $links[] = $this->showHideLink( $nondefaults, $msg, 
$name, $values[$name] );
+                       $links[] = $this->showHideLink( $nondefaults, $msg, 
$name, $opts[$name] );
                }
 
                $hiddenFields = $nondefaults;

-- 
To view, visit https://gerrit.wikimedia.org/r/94288
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iec4aab870b6e9aa98d2b118010e78e25b1dbe437
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <[email protected]>
Gerrit-Reviewer: Anomie <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to