Eileen has uploaded a new change for review.

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

Change subject: CRM-18193 Make log_date optional for revert / retrieving 
changes where uniqueID is in play
......................................................................

CRM-18193 Make log_date optional for revert / retrieving changes where uniqueID 
is in play

Change-Id: I6307c088b62ec7037f27873a1a1681afc09a46f1
---
M CRM/Logging/Differ.php
M api/v3/Logging.php
M api/v3/examples/Logging/Get.php
M api/v3/examples/Logging/Revert.php
4 files changed, 114 insertions(+), 68 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/crm/civicrm 
refs/changes/88/282088/1

diff --git a/CRM/Logging/Differ.php b/CRM/Logging/Differ.php
index 3490af4..6dca866 100644
--- a/CRM/Logging/Differ.php
+++ b/CRM/Logging/Differ.php
@@ -80,7 +80,6 @@
 
     $params = array(
       1 => array($this->log_conn_id, 'String'),
-      2 => array($this->log_date, 'String'),
     );
 
     $logging = new CRM_Logging_Schema();
@@ -142,14 +141,21 @@
       }
     }
 
+    $logDateClause = '';
+    if ($this->log_date) {
+      $params[2] = array($this->log_date, 'String');
+      $logDateClause = "
+        AND lt.log_date BETWEEN DATE_SUB(%2, INTERVAL {$this->interval}) AND 
DATE_ADD(%2, INTERVAL {$this->interval})
+      ";
+    }
+
     // find ids in this table that were affected in the given connection 
(based on connection id and a ±10 s time period around the date)
     $sql = "
 SELECT DISTINCT lt.id FROM `{$this->db}`.`log_$table` lt
 {$join}
-WHERE lt.log_conn_id = %1 AND
-      lt.log_date BETWEEN DATE_SUB(%2, INTERVAL {$this->interval}) AND 
DATE_ADD(%2, INTERVAL {$this->interval})
-      {$contactIdClause}";
-
+WHERE lt.log_conn_id = %1
+    $logDateClause
+    {$contactIdClause}";
     $dao = CRM_Core_DAO::executeQuery($sql, $params);
     while ($dao->fetch()) {
       $diffs = array_merge($diffs, $this->diffsInTableForId($table, $dao->id));
@@ -169,15 +175,23 @@
 
     $params = array(
       1 => array($this->log_conn_id, 'String'),
-      2 => array($this->log_date, 'String'),
       3 => array($id, 'Integer'),
     );
 
-    // look for all the changes in the given connection that happended less 
than {$this->interval} s later than log_date to the given id to catch 
multi-query changes
-    $changedSQL = "SELECT * FROM `{$this->db}`.`log_$table` WHERE log_conn_id 
= %1 AND log_date >= %2 AND log_date < DATE_ADD(%2, INTERVAL {$this->interval}) 
AND id = %3 ORDER BY log_date DESC LIMIT 1";
+    // look for all the changes in the given connection that happened less 
than {$this->interval} s later than log_date to the given id to catch 
multi-query changes
+    $logDateClause = "";
+    if ($this->log_date) {
+      $logDateClause = " AND log_date >= %2 AND log_date < DATE_ADD(%2, 
INTERVAL {$this->interval})";
+      $params[2] = array($this->log_date, 'String');
+    }
+
+    $changedSQL = "SELECT * FROM `{$this->db}`.`log_$table` WHERE log_conn_id 
= %1 $logDateClause AND id = %3 ORDER BY log_date DESC LIMIT 1";
 
     $changedDAO = CRM_Core_DAO::executeQuery($changedSQL, $params);
     while ($changedDAO->fetch()) {
+      if (empty($this->log_date) && 
!$this->checkLogCanBeUsedWithNoLogDate($changedDAO->log_date)) {
+        throw new CRM_Core_Exception('The connection date must be passed in to 
disambiguate this logging entry per CRM-18193');
+      }
       $changed = $changedDAO->toArray();
 
       // return early if nothing found
@@ -201,6 +215,7 @@
           break;
 
         case 'Update':
+          $params[2] = array($changedDAO->log_date, 'String');
           // look for the previous state (different log_conn_id) of the given 
id
           $originalSQL = "SELECT * FROM `{$this->db}`.`log_$table` WHERE 
log_conn_id != %1 AND log_date < %2 AND id = %3 ORDER BY log_date DESC LIMIT 1";
           $original = $this->sqlToArray($originalSQL, $params);
@@ -420,4 +435,36 @@
     return $diffs;
   }
 
+  /**
+   * Check that the log record relates to a unique log id.
+   *
+   * If the record was recorded using the old non-unique style then the
+   * log_date
+   * MUST be set to get the (fairly accurate) list of changes. In this case the
+   * nasty 10 second interval rule is applied.
+   *
+   * See  CRM-18193 for a discussion of unique log id.
+   *
+   * @param string $change_date
+   *
+   * @return bool
+   * @throws \CiviCRM_API3_Exception
+   */
+  protected function checkLogCanBeUsedWithNoLogDate($change_date) {
+    if (civicrm_api3('Setting', 'getvalue', array('name' => 
'logging_all_tables_uniquid', 'group' => 'CiviCRM Preferences'))) {
+      return TRUE;
+    };
+    $uniqueDate = civicrm_api3('Setting', 'getvalue', array(
+      'name' => 'logging_uniqueid_date',
+      'group' => 'CiviCRM Preferences',
+    ));
+    if (strtotime($uniqueDate) < strtotime($change_date)) {
+      return FALSE;
+    }
+    else {
+      return TRUE;
+    }
+
+  }
+
 }
diff --git a/api/v3/Logging.php b/api/v3/Logging.php
index f3418a6..32bab67 100644
--- a/api/v3/Logging.php
+++ b/api/v3/Logging.php
@@ -43,7 +43,7 @@
  */
 function civicrm_api3_logging_revert($params) {
   $schema = new CRM_Logging_Schema();
-  $reverter = new CRM_Logging_Reverter($params['log_conn_id'], 
$params['log_date']);
+  $reverter = new CRM_Logging_Reverter($params['log_conn_id'], 
CRM_Utils_Array::value('log_date', $params));
   
$reverter->calculateDiffsFromLogConnAndDate($schema->getLogTablesForContact());
   $reverter->revert();
   return civicrm_api3_create_success(1);
diff --git a/api/v3/examples/Logging/Get.php b/api/v3/examples/Logging/Get.php
index 3e181ae..6e904de 100644
--- a/api/v3/examples/Logging/Get.php
+++ b/api/v3/examples/Logging/Get.php
@@ -7,8 +7,7 @@
  */
 function logging_get_example() {
   $params = array(
-    'log_conn_id' => 'wooty woot',
-    'log_date' => '2016-04-06 00:08:23',
+    'log_conn_id' => 'wooty wop wop',
   );
 
   try{
@@ -44,173 +43,173 @@
     'values' => array(
       '0' => array(
         'action' => 'Update',
-        'id' => '3',
+        'id' => '9',
         'field' => 'sort_name',
         'from' => 'Anderson, Anthony',
         'to' => 'Dwarf, Dopey',
         'table' => 'civicrm_contact',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '1' => array(
         'action' => 'Update',
-        'id' => '3',
+        'id' => '9',
         'field' => 'display_name',
         'from' => 'Mr. Anthony Anderson II',
         'to' => 'Mr. Dopey Dwarf II',
         'table' => 'civicrm_contact',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '2' => array(
         'action' => 'Update',
-        'id' => '3',
+        'id' => '9',
         'field' => 'first_name',
         'from' => 'Anthony',
         'to' => 'Dopey',
         'table' => 'civicrm_contact',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '3' => array(
         'action' => 'Update',
-        'id' => '3',
+        'id' => '9',
         'field' => 'last_name',
         'from' => 'Anderson',
         'to' => 'Dwarf',
         'table' => 'civicrm_contact',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '4' => array(
         'action' => 'Update',
-        'id' => '3',
+        'id' => '9',
         'field' => 'modified_date',
-        'from' => '2016-04-06 00:08:06',
-        'to' => '2016-04-06 00:08:23',
+        'from' => '2016-04-06 02:53:27',
+        'to' => '2016-04-06 02:53:44',
         'table' => 'civicrm_contact',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '5' => array(
         'action' => 'Insert',
-        'id' => '4',
+        'id' => '2',
         'field' => 'id',
         'from' => '',
-        'to' => '4',
+        'to' => '2',
         'table' => 'civicrm_email',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '6' => array(
         'action' => 'Insert',
-        'id' => '4',
+        'id' => '2',
         'field' => 'contact_id',
         'from' => '',
-        'to' => '3',
+        'to' => '9',
         'table' => 'civicrm_email',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '7' => array(
         'action' => 'Insert',
-        'id' => '4',
+        'id' => '2',
         'field' => 'location_type_id',
         'from' => '',
         'to' => '',
         'table' => 'civicrm_email',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '8' => array(
         'action' => 'Insert',
-        'id' => '4',
+        'id' => '2',
         'field' => 'email',
         'from' => '',
         'to' => '[email protected]',
         'table' => 'civicrm_email',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '9' => array(
         'action' => 'Insert',
-        'id' => '4',
+        'id' => '2',
         'field' => 'is_primary',
         'from' => '',
         'to' => 0,
         'table' => 'civicrm_email',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '10' => array(
         'action' => 'Insert',
-        'id' => '4',
+        'id' => '2',
         'field' => 'is_billing',
         'from' => '',
         'to' => 0,
         'table' => 'civicrm_email',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '11' => array(
         'action' => 'Insert',
-        'id' => '4',
+        'id' => '2',
         'field' => 'on_hold',
         'from' => '',
         'to' => 0,
         'table' => 'civicrm_email',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '12' => array(
         'action' => 'Insert',
-        'id' => '4',
+        'id' => '2',
         'field' => 'is_bulkmail',
         'from' => '',
         'to' => 0,
         'table' => 'civicrm_email',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '13' => array(
         'action' => 'Insert',
-        'id' => '4',
+        'id' => '2',
         'field' => 'hold_date',
         'from' => '',
         'to' => '',
         'table' => 'civicrm_email',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '14' => array(
         'action' => 'Insert',
-        'id' => '4',
+        'id' => '2',
         'field' => 'reset_date',
         'from' => '',
         'to' => '',
         'table' => 'civicrm_email',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '15' => array(
         'action' => 'Insert',
-        'id' => '4',
+        'id' => '2',
         'field' => 'signature_text',
         'from' => '',
         'to' => '',
         'table' => 'civicrm_email',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
       '16' => array(
         'action' => 'Insert',
-        'id' => '4',
+        'id' => '2',
         'field' => 'signature_html',
         'from' => '',
         'to' => '',
         'table' => 'civicrm_email',
-        'log_date' => '2016-04-06 00:08:23',
-        'log_conn_id' => 'wooty woot',
+        'log_date' => '2016-04-06 02:53:44',
+        'log_conn_id' => 'wooty wop wop',
       ),
     ),
   );
@@ -220,7 +219,7 @@
 
 /*
 * This example has been generated from the API test suite.
-* The test that created it is called "testGet"
+* The test that created it is called "testGetNoDate"
 * and can be found at:
 * 
https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/LoggingTest.php
 *
diff --git a/api/v3/examples/Logging/Revert.php 
b/api/v3/examples/Logging/Revert.php
index fcdf924..88a5514 100644
--- a/api/v3/examples/Logging/Revert.php
+++ b/api/v3/examples/Logging/Revert.php
@@ -8,7 +8,7 @@
 function logging_revert_example() {
   $params = array(
     'log_conn_id' => 'woot',
-    'log_date' => '2016-04-05 23:52:59',
+    'log_date' => '2016-04-06 02:52:07',
   );
 
   try{

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6307c088b62ec7037f27873a1a1681afc09a46f1
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/crm/civicrm
Gerrit-Branch: master
Gerrit-Owner: Eileen <[email protected]>

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

Reply via email to