Ejegg has uploaded a new change for review.

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

Change subject: Quit flooding antifraud queue
......................................................................

Quit flooding antifraud queue

Oops, we're sending a ton of useless messages.  Only send 'em
when the total changes, and we've actually done something.

Bug: T136381
Change-Id: Ic8fbd071d7bf4f6bd94059bab27cfb51ce0828b3
---
M extras/custom_filters/custom_filters.body.php
1 file changed, 51 insertions(+), 12 deletions(-)


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

diff --git a/extras/custom_filters/custom_filters.body.php 
b/extras/custom_filters/custom_filters.body.php
index 146043e..db8f985 100644
--- a/extras/custom_filters/custom_filters.body.php
+++ b/extras/custom_filters/custom_filters.body.php
@@ -76,26 +76,33 @@
                $this->fraud_logger->info( '"addRiskScore" ' . $log_message );
                $this->risk_score[$source] = $score;
        }
-       
+
 
        /**
-        * @throws InvalidArgumentException
+        * Add up the risk scores in an array, by default $this->risk_score
+        * @param array|null $scoreArray
+        * @return float total risk score
         */
-       public function getRiskScore() {
+       public function getRiskScore( $scoreArray = null ) {
+               if ( is_null( $scoreArray ) ) {
+                       $scoreArray = $this->risk_score;
+               }
 
-               if ( is_numeric( $this->risk_score ) ) {
-                       return $this->risk_score;
-
-               } elseif ( is_array( $this->risk_score) ) {
+               if ( is_numeric( $scoreArray ) ) {
+                       return $scoreArray;
+               } elseif ( is_array( $scoreArray ) ) {
                        $total = 0;
-                       foreach ( $this->risk_score as $score ){
+                       foreach ( $scoreArray as $score ){
                                $total += $score;
                        }
                        return $total;
 
                } else {
                        // TODO: We should catch this during setRiskScore.
-                       throw new InvalidArgumentException( __FUNCTION__ . " 
risk_score is neither numeric, nor an array." . print_r( $this->risk_score, 
true ) );
+                       throw new InvalidArgumentException(
+                               __FUNCTION__ . " risk_score is neither numeric, 
nor an array."
+                               . print_r( $scoreArray, true )
+                       );
                }
        }
 
@@ -125,15 +132,47 @@
                $log_message = '"' . addslashes( json_encode( $utm ) ) . '"';
                $this->fraud_logger->info( '"utm" ' . $log_message );
 
-               $storedScores = 
$this->gateway_adapter->getRequest()->getSessionData( 'risk_scores' );
-               if ( $storedScores != $this->risk_score ) {
-                       $this->gateway_adapter->getRequest()->setSessionData( 
'risk_scores', $this->risk_score );
+
+               if ( $this->shouldSendMessage( $score ) ) {
                        $this->sendAntifraudMessage( $localAction, 
$this->getRiskScore(), $this->risk_score );
                }
 
+               // Always keep the stored scores up to date
+               $this->gateway_adapter->getRequest()->setSessionData( 
'risk_scores', $this->risk_score );
+
                return TRUE;
        }
 
+       /**
+        * Determine if we should send an antifraud message
+        * @param float $score total risk score for this run
+        * @return bool true if a queue message is warranted
+        */
+       protected function shouldSendMessage( $score ) {
+               // We only send a message when the total changes
+               $storedScores = 
$this->gateway_adapter->getRequest()->getSessionData( 'risk_scores' );
+               if ( is_array( $storedScores ) ) {
+                       $storedTotal = $this->getRiskScore( $storedScores );
+               } else {
+                       // Nothing stored? Set this to an impossible value so we
+                       // send a message even if our new total is zero.
+                       $storedTotal = -1;
+               }
+
+               // We don't want to send a message if we didn't run anything,
+               // i.e., if $this->risk_score looks like this:
+               $shirked = array(
+                       'initial' => $this->gateway_adapter->getGlobal( 
'CustomFiltersRiskScore' )
+               );
+
+               if ( $this->risk_score != $shirked && $storedTotal !== $score ) 
{
+                       // We ran something, and the total changed
+                       return true;
+               }
+
+               return false;
+       }
+
        static function onValidate( GatewayType $gateway_adapter ) {
                if ( !$gateway_adapter->getGlobal( 'EnableCustomFilters' ) ){
                        return true;

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic8fbd071d7bf4f6bd94059bab27cfb51ce0828b3
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DonationInterface
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>

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

Reply via email to