Mwalker has uploaded a new change for review.

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


Change subject: Matt 'Cleaned up' GatewayFormChooser aka Went Crazy
......................................................................

Matt 'Cleaned up' GatewayFormChooser aka Went Crazy

Uhh... so ya... only needed to allow gateway filtering and then
decided things looked ugly... Not sure if this ended up any better
though.

Change-Id: Ib61d1486806b9cce4e39cadfadf8e83c22fd4692
---
M gateway_common/DataValidator.php
M special/GatewayFormChooser.php
2 files changed, 93 insertions(+), 92 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DonationInterface 
refs/changes/56/73356/1

diff --git a/gateway_common/DataValidator.php b/gateway_common/DataValidator.php
index 30e7649..f97420c 100644
--- a/gateway_common/DataValidator.php
+++ b/gateway_common/DataValidator.php
@@ -1042,21 +1042,46 @@
        /**
         * Test to determine if a value either appears in the haystack in the 
case
         * of an array, or that the needle IS the haystack 
-        * @param mixed $needle The value to match on
-        * @param mixed $haystack Either an array, or a single value
+        * @param mixed $needle Value, or array of values, to match
+        * @param mixed $haystack Value, or array of values, that are acceptable
         * @return bool
         */
-       public static function value_appears_in( $needle, $haystack ){
-               if ( !is_array( $haystack ) ){
-                       if ( $needle === $haystack ){
+       public static function value_appears_in( $needle, $haystack ) {
+               $needle = ( is_array( $needle) ) ? $needle : array( $needle );
+               $haystack = ( is_array( $haystack) ) ? $haystack : array( 
$haystack );
+
+               $plusCheck = array_key_exists( '+', $haystack );
+               $minusCheck = array_key_exists( '-', $haystack );
+
+               if ( $plusCheck || $minusCheck ) {
+                       // With +/- checks we will first explicitly deny 
anything in '-'
+                       // Then if '+' is defined accept anything there
+                       //    but if '+' is not defined we just let everything 
that wasn't denied by '-' through
+                       // Otherwise we assume both were defined and deny 
everything :)
+
+                       if ( $minusCheck && DataValidator::value_appears_in( 
$needle, $haystack['-'] ) ) {
+                               return false;
+                       }
+                       if ( $plusCheck && DataValidator::value_appears_in( 
$needle, $haystack['+'] ) ) {
+                               return true;
+                       } elseif ( !$plusCheck ) {
+                               // Implicit acceptance
                                return true;
                        }
-               } else {
-                       if ( in_array( $needle, $haystack ) ) {
-                               return true;
-                       }
+                       return false;
                }
-               return false;
+
+               if ( in_array( 'ALL', $haystack ) ) {
+                       // If the haystack can accept anything, then whoo!
+                       return true;
+               }
+
+               $result = array_intersect( $haystack, $needle );
+               if ( !empty( $result ) ) {
+                       return true;
+               } else {
+                       return false;
+               }
        }
        
        /**
diff --git a/special/GatewayFormChooser.php b/special/GatewayFormChooser.php
index b14cb93..2fa089a 100644
--- a/special/GatewayFormChooser.php
+++ b/special/GatewayFormChooser.php
@@ -23,11 +23,12 @@
                $paymentMethod = $this->getRequest()->getVal( 'paymentmethod', 
null );
                $paymentSubMethod = $this->getRequest()->getVal( 'submethod', 
null );
                $recurring = $this->getRequest()->getVal( 'recurring', false );
+               $gateway = $this->getRequest()->getVal( 'gateway', null );
                
                //This is clearly going to go away before we deploy this 
bizniss. 
                $testNewGetAll = $this->getRequest()->getVal( 'testGetAll', 
false );
                if ( $testNewGetAll ){
-                       $forms = self::getAllValidForms( $country, $currency, 
$paymentMethod, $paymentSubMethod, $recurring );
+                       $forms = self::getAllValidForms( $country, $currency, 
$paymentMethod, $paymentSubMethod, $recurring, $gateway );
                        echo "<pre>" . print_r( $forms, true ) . "</pre>";
                        $form = self::pickOneForm( $forms, $currency, $country 
);
                        echo "<pre>I choose you, " . print_r( $form, true) . 
"!</pre>";
@@ -35,7 +36,7 @@
                        die();
                }
 
-               $forms = self::getAllValidForms( $country, $currency, 
$paymentMethod, $paymentSubMethod, $recurring );
+               $forms = self::getAllValidForms( $country, $currency, 
$paymentMethod, $paymentSubMethod, $recurring, $gateway );
                $form = self::pickOneForm( $forms, $currency, $country );
 
                // And... construct the URL
@@ -79,113 +80,88 @@
         * @param string $payment_method Optional payment method filter
         * @param string $payment_submethod Optional payment submethod filter. 
THIS WILL ONLY WORK IF YOU ALSO SEND THE PAYMENT METHOD.
         * @param boolean $recurring Whether or not we should return recurring 
forms. Default = false.
-        * @return type 
+        * @param string $gateway Optional gateway to force.
+        * @return array
         */
-       static function getAllValidForms( $country = null, $currency = null, 
$payment_method = null, $payment_submethod = null, $recurring = false ){
+       static function getAllValidForms( $country = null, $currency = null, 
$payment_method = null,
+               $payment_submethod = null, $recurring = false, $gateway = null
+       ) {
                global $wgDonationInterfaceAllowedHtmlForms;
                $forms = $wgDonationInterfaceAllowedHtmlForms;
+
+               // The following metadata are required for a form; miss any of 
these and we will drop it immediately
+               $required = array( 'gateway', 'countries', 'currencies', 
'payment_methods' );
                
-               //then remove the ones that we don't want. 
+               // First get all the valid and enabled gateways capable of 
processing shtuff
                $valid_gateways = self::getAllEnabledGateways();
-               foreach ( $forms as $name => $meta ){
-                       $unset = false;
-                       
-                       //filter on enabled gateways
-                       if ( !array_key_exists( 'gateway', $meta ) ){
-                               unset ( $forms[$name] );
-                               continue; 
-                       }
-                       //if it's an array, any one will do. 
-                       if ( is_array( $meta['gateway'] ) ){
-                               $found = false;
-                               foreach ( $meta['gateway'] as $index => $value 
){
-                                       if ( DataValidator::value_appears_in( 
$value, $valid_gateways ) ){
-                                               $found = true;
-                                               break;
-                                       }
-                               }
-                               if ( !$found ){
-                                       $unset = true;
-                               }
+               if ( $gateway !== null ) {
+                       if ( in_array( $gateway, $valid_gateways ) ) {
+                               $valid_gateways = array( $gateway );
                        } else {
-                               if ( !DataValidator::value_appears_in( 
$meta['gateway'], $valid_gateways ) ){
-                                       $unset = true;
+                               // Aaah; the requested gateway is not valid :'( 
Nothing to do but return nothing
+                               return array();
+                       }
+               }
+
+               // then remove the forms that we don't want.
+               foreach ( $forms as $name => $meta ) {
+                       // Prefilter for sillyness
+                       foreach ( $required as $paramName ) {
+                               if ( !array_key_exists( $paramName, $meta ) ) {
+                                       unset ( $forms[$name] );
+                                       continue 2;
                                }
                        }
-                       
-                       if ( $unset ){
+
+                       // filter on enabled gateways
+                       if ( !DataValidator::value_appears_in( 
$meta['gateway'], $valid_gateways ) ) {
                                unset( $forms[$name] );
                                continue;
                        }
                        
                        //filter on country
-                       if ( !is_null( $country ) ){
-                               if ( array_key_exists( 'countries', $meta ) ){ 
//totally okay if it doesn't.
-                                       if ( array_key_exists( '+', 
$meta['countries'] ) ){
-                                               if ( 
!DataValidator::value_appears_in( $country, $meta['countries']['+'] ) ){
-                                                       unset( $forms[$name] );
-                                                       continue;
-                                               }
-                                       }
-                                       if ( array_key_exists( '-', 
$meta['countries'] ) ){
-                                               if ( 
DataValidator::value_appears_in( $country, $meta['countries']['-'] ) ){
-                                                       unset( $forms[$name] );
-                                                       continue;
-                                               }
-                                       }
-                               }
-                       }
-
-                       //filter on currency
-                       if ( !is_null( $currency ) ){
-                               if ( array_key_exists( 'currencies', $meta ) ){ 
//totally okay if it doesn't.
-                                       if ( array_key_exists( '+', 
$meta['currencies'] ) ){
-                                               if ( 
!DataValidator::value_appears_in( $currency, $meta['currencies']['+'] ) ){
-                                                       unset( $forms[$name] );
-                                                       continue;
-                                               }
-                                       }
-                                       if ( array_key_exists( '-', 
$meta['currencies'] ) ){
-                                               if ( 
DataValidator::value_appears_in( $currency, $meta['currencies']['-'] ) ){
-                                                       unset( $forms[$name] );
-                                                       continue;
-                                               }
-                                       }
-                               }
-                       }
-                       
-                       //filter on payment method
-                       if ( !array_key_exists( 'payment_methods', $meta ) ){ 
+                       if ( !is_null( $country ) && 
!DataValidator::value_appears_in( $country, $meta['countries'] ) ) {
                                unset( $forms[$name] );
                                continue;
                        }
-                       if ( !is_null( $payment_method ) ){
-                               if ( !array_key_exists( $payment_method, 
$meta['payment_methods'] ) ){
+
+                       //filter on currency
+                       if ( !is_null( $currency ) && 
!DataValidator::value_appears_in( $currency, $meta['currencies'] ) ) {
+                               unset( $forms[$name] );
+                               continue;
+                       }
+                       
+                       //filter on payment method
+                       if ( !is_null( $payment_method ) ) {
+                               if ( !array_key_exists( $payment_method, 
$meta['payment_methods'] ) ) {
+                                       // Well, the root payment method is 
invalid, so... die!
                                        unset( $forms[$name] );
                                        continue;
                                }
                                
                                //filter on payment submethod
                                //CURSES! I didn't want this to be buried down 
in here, but I guess it's sort of reasonable. Ish.
-                               if ( !is_null( $payment_submethod ) ){
-                                       if ( !DataValidator::value_appears_in( 
$payment_submethod, $meta['payment_methods'][$payment_method] )
-                                               && 
!DataValidator::value_appears_in( 'ALL', 
$meta['payment_methods'][$payment_method] ) ){
-                                               unset( $forms[$name] );
-                                               continue;
-                                       }
+                               if (
+                                       !is_null( $payment_submethod ) &&
+                                       !DataValidator::value_appears_in( 
$payment_submethod, $meta['payment_methods'][$payment_method] )
+                               ) {
+                                       unset( $forms[$name] );
+                                       continue;
                                }
                        }
                        
                        //filter on recurring
-                       if ( $recurring && !DataValidator::value_appears_in( 
'recurring', $meta ) ){
-                               unset( $forms[$name] );
-                               continue;
+                       if ( in_array( 'recurring', $meta ) ) {
+                               if ( !$recurring ) {
+                                       unset( $forms[$name] );
+                                       continue;
+                               }
+                       } else {
+                               if ( $recurring ) {
+                                       unset( $forms[$name] );
+                                       continue;
+                               }
                        }
-                       if ( !$recurring && DataValidator::value_appears_in( 
'recurring', $meta ) ){
-                               unset( $forms[$name] );
-                               continue;
-                       }
-                       
                }
                return $forms;
        }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib61d1486806b9cce4e39cadfadf8e83c22fd4692
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: deploy-payments_1.22
Gerrit-Owner: Mwalker <[email protected]>

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

Reply via email to