Eileen has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/343804 )

Change subject: Further attempt to recover blank addresses.
......................................................................

Further attempt to recover blank addresses.

If they have been through more than one merge, gather & assess deleted from all

Bug: T159408
Change-Id: I4769a827a0479592340bb692cf12355ed7d97a84
---
M sites/all/modules/wmf_civicrm/update_restore_addresses.php
1 file changed, 21 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm 
refs/changes/04/343804/1

diff --git a/sites/all/modules/wmf_civicrm/update_restore_addresses.php 
b/sites/all/modules/wmf_civicrm/update_restore_addresses.php
index 7c8a77e..1b3e0bf 100644
--- a/sites/all/modules/wmf_civicrm/update_restore_addresses.php
+++ b/sites/all/modules/wmf_civicrm/update_restore_addresses.php
@@ -48,6 +48,7 @@
   while ($result->fetch()) {
     $logEntries[] = (array) $result;
   }
+  $highestLogID = count($logEntries) - 1;
 
   $dataFields = array(
     'street_address',
@@ -71,18 +72,23 @@
       return;
     }
   }
-  // Fetch all the address changes that happened during this merge action.
-  $logs = civicrm_api3('Logging', 'get', array(
-    'tables' => array('civicrm_address'),
-    'log_conn_id' => $logEntries[1]['log_conn_id']
-  ));
-  $updates = array();
+
   $deletedAddresses = array();
-  foreach ($logs['values'] as $log) {
-    if ($log['action'] == 'Delete') {
-      $deletedAddresses[$log['id']][$log['field']] = $log['from'];
+  foreach ($logEntries as $logEntryID => $logEntry) {
+    if ($logEntryID > 0);
+    // Fetch all the address changes that happened during the most recent 
merge action.
+    $logs = civicrm_api3('Logging', 'get', array(
+      'tables' => array('civicrm_address'),
+      'log_conn_id' => $logEntries[$logEntryID]['log_conn_id']
+    ));
+    foreach ($logs['values'] as $log) {
+      if ($log['action'] == 'Delete') {
+        $deletedAddresses[$log['id']][$log['field']] = $log['from'];
+      }
     }
   }
+  $updates = array();
+
   // Q. What do we do if more than one address was deleted during the merged?
   // A. Chicken out.
   if (count($deletedAddresses) > 1) {
@@ -94,19 +100,18 @@
    * - how many times has this address been changed?
    * - only 2? we are just dealing with a single address moved from one
    *   contact to another (double check that because we like belts & braces, 
red ones).
-   * - more than 2? ug complications. hide.
+   * - more than 2? ug complications. - give it a go working with the most 
recent one.
   */
   if (
-    count($logEntries) > 2
-    ||  !in_array($logEntries[0]['log_action'], array('Insert', 
'Initialization'))
-    || $logEntries[1]['log_action'] !== 'Update'
+    !in_array($logEntries[0]['log_action'], array('Insert', 'Initialization'))
+    || $logEntries[$highestLogID]['log_action'] !== 'Update'
     ) {
     return;
   }
 
   // We are specifically trying to handle merged data at this stage.
   // May extend if we identify other patterns.
-  if ($logEntries[0]['contact_id'] == $logEntries[1]['contact_id']) {
+  if ($logEntries[0]['contact_id'] == 
$logEntries[$highestLogID]['contact_id']) {
     return;
   }
 
@@ -114,7 +119,7 @@
   // was transferred to another contact. We have the log_conn_id & the new 
contact id.
   // let's make sure the new contact has not since been deleted (merged)
   $keptContact = civicrm_api3('Contact', 'get', array(
-    'id' => $logEntries[1]['contact_id'],
+    'id' => $logEntries[$highestLogID]['contact_id'],
     'sequential' => 1
   ));
   if ($keptContact['count'] == 0 || 
!empty($keptContact['values'][0]['is_deleted'])) {
@@ -124,7 +129,7 @@
 
   // If our address is NOT the primary
   $addresses = civicrm_api3('Address', 'get', array(
-    'contact_id' => $logEntries[1]['contact_id'],
+    'contact_id' => $logEntries[$highestLogID]['contact_id'],
   ));
   $emptyAddress = $addresses['values'][$addressID];
   $isPrimary = $emptyAddress['is_primary'];

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4769a827a0479592340bb692cf12355ed7d97a84
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm
Gerrit-Branch: master
Gerrit-Owner: Eileen <emcnaugh...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to