jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/349495 )

Change subject: Backfill missing contribution_tracking links
......................................................................


Backfill missing contribution_tracking links

Little bit of reusable log parsing logic

Bug: T163443
Change-Id: I3847eea178330897cb0cea702bb1a3a4401ddc21
---
M sites/all/modules/wmf_civicrm/tests/phpunit/HelperFunctionsTest.php
M sites/all/modules/wmf_civicrm/wmf_civicrm.install
M sites/all/modules/wmf_civicrm/wmf_civicrm.module
3 files changed, 111 insertions(+), 0 deletions(-)

Approvals:
  XenoRyet: Looks good to me, approved
  jenkins-bot: Verified



diff --git 
a/sites/all/modules/wmf_civicrm/tests/phpunit/HelperFunctionsTest.php 
b/sites/all/modules/wmf_civicrm/tests/phpunit/HelperFunctionsTest.php
index 9d9557d..cb9bc15 100644
--- a/sites/all/modules/wmf_civicrm/tests/phpunit/HelperFunctionsTest.php
+++ b/sites/all/modules/wmf_civicrm/tests/phpunit/HelperFunctionsTest.php
@@ -2,6 +2,7 @@
 
 /**
  * @group WmfCivicrm
+ * @group WmfCivicrmHelpers
  */
 class HelperFunctionsTest extends BaseWmfDrupalPhpUnitTestCase {
 
@@ -77,4 +78,37 @@
         return $tag['id'];
     }
 
+    public function testParseWatchdogLog() {
+        $logLine = "Apr 21 17:00:02 mach1001 drupal: 
RecurringQueueConsumer|1492791234|127.0.0.1|https://example.wikimedia.org/index.php||1||Array#012(#012
    [date] => 1492791234#012    [txn_type] => subscr_payment#012    
[gateway_txn_id] => 1X123456TJ0987654#012    [currency] => EUR#012    
[contribution_tracking_id] => 47012345#012    [email] => [email protected]#012 
   [first_name] => DONNY#012    [last_name] => DONOR#012    [street_address] => 
123 GOLDFISH POND RD#012    [city] => LONDON#012    [state_province] => #012    
[country] => GB#012    [postal_code] => 1000#012    [gross] => 10.00#012    
[fee] => 0.62#012    [order_id] => 47012345#012    [recurring] => 1#012    
[subscr_id] => S-9TN12345BR9987654#012    [middle_name] => #012    [gateway] => 
paypal#012    [source_name] => SmashPig#012    [source_type] => listener#012    
[source_host] => mach1001#012    [source_run_id] => 113106#012    
[source_version] => 200f63eedb05f5e6665d9837bba97e7e7237a41d#012    
[source_enqueued_time] => 1492793225#012)";
+        $parsed = wmf_civicrm_parse_watchdog_array( $logLine );
+        $expected = array(
+            'date' => '1492791234',
+            'txn_type' => 'subscr_payment',
+            'gateway_txn_id' => '1X123456TJ0987654',
+            'currency' => 'EUR',
+            'contribution_tracking_id' => '47012345',
+            'email' => '[email protected]',
+            'first_name' => 'DONNY',
+            'last_name' => 'DONOR',
+            'street_address' => '123 GOLDFISH POND RD',
+            'city' => 'LONDON',
+            'state_province' => '',
+            'country' => 'GB',
+            'postal_code' => '1000',
+            'gross' => '10.00',
+            'fee' => '0.62',
+            'order_id' => '47012345',
+            'recurring' => '1',
+            'subscr_id' => 'S-9TN12345BR9987654',
+            'middle_name' => '',
+            'gateway' => 'paypal',
+            'source_name' => 'SmashPig',
+            'source_type' => 'listener',
+            'source_host' => 'mach1001',
+            'source_run_id' => '113106',
+            'source_version' => '200f63eedb05f5e6665d9837bba97e7e7237a41d',
+            'source_enqueued_time' => '1492793225',
+        );
+        $this->assertEquals( $expected, $parsed );
+    }
 }
diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.install 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.install
index 300d292..be49ad0 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.install
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.install
@@ -2905,3 +2905,64 @@
   }
 
 }
+/**
+ * Backfill contribution_tracking associations for recurring donations
+ *
+ * Bug: T163443
+ */
+function wmf_civicrm_update_7520() {
+  $affected = CRM_Core_DAO::executeQuery("
+    SELECT cont.id, ext.gateway_txn_id
+    FROM civicrm_contribution cont
+    INNER JOIN wmf_contribution_extra ext on ext.entity_id = cont.id
+    LEFT JOIN drupal.contribution_tracking ct
+    ON ct.contribution_id = cont.id
+    WHERE cont.trxn_id LIKE 'RECURRING PAYPAL%'
+    AND cont.receive_date > '2017-04-09'
+    AND ct.id IS NULL
+  ");
+  $found = [];
+  $logDir = wmf_audit_get_log_archive_dir();
+  $lines = array();
+  // These lines will have the gateway_trxn_id and the ct_id
+  $files = implode( ' ', glob( "$logDir/fundraising-drupal-201704*" ) );
+  $cmd = "/bin/zgrep subscr_payment $files";
+  exec( $cmd, $lines );
+  foreach ( $lines as $line ) {
+    $parsed = wmf_civicrm_parse_watchdog_array( $line );
+    if (
+      !empty( $parsed['gateway_txn_id'] ) &&
+      !empty( $parsed['contribution_tracking_id'] )
+    ) {
+      // Normalized lines
+      $found[$parsed['gateway_txn_id']] = $parsed['contribution_tracking_id'];
+    } else if ( !empty( $parsed['txn_id'] ) ) {
+      // Raw PayPal IPNs
+      if (
+        !empty( $parsed['transaction_subject'] ) &&
+        is_numeric( $parsed['transaction_subject'] )
+      ) {
+        $found[$parsed['txn_id']] = $parsed['transaction_subject'];
+      } else if (
+        !empty( $parsed['custom'] ) &&
+               is_numeric( $parsed['custom'] )
+      ) {
+        $found[$parsed['txn_id']] = $parsed['custom'];
+      }
+    }
+  }
+  $fixed = 0;
+  $missing = 0;
+  while( $affected->fetch() ) {
+    $gatewayTxnId = $affected->gateway_txn_id;
+    if ( empty( $found[$gatewayTxnId] ) ) {
+      $missing++;
+    } else {
+      wmf_civicrm_message_update_contribution_tracking(
+        array( 'contribution_tracking_id' => $found[$gatewayTxnId] ),
+        array( 'id' => $affected->id )
+      );
+      $fixed++;
+    }
+  }
+}
diff --git a/sites/all/modules/wmf_civicrm/wmf_civicrm.module 
b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
index 6d740e7..0bc895a 100644
--- a/sites/all/modules/wmf_civicrm/wmf_civicrm.module
+++ b/sites/all/modules/wmf_civicrm/wmf_civicrm.module
@@ -3544,3 +3544,19 @@
     }
     return FALSE;
   }
+
+  /**
+   * Given a line from a drupal watchdog log with an array, reconstruct
+   * the keys and values from the mangled print_r variant
+   * e.g. [gateway] => paypal#012    [source_name] => SmashPig#012
+   */
+  function wmf_civicrm_parse_watchdog_array( $logLine ) {
+    $logLine = str_replace( '=>', '=>', $logLine );
+    $output = [];
+    preg_match_all( '/\[([^\]]+)\] => (.*?)#012/', $logLine, $matches);
+    foreach( $matches[1] as $index => $key ) {
+      $output[$key] = $matches[2][$index];
+    }
+    return $output;
+  }
+

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I3847eea178330897cb0cea702bb1a3a4401ddc21
Gerrit-PatchSet: 3
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Ejegg <[email protected]>
Gerrit-Reviewer: XenoRyet <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to