Merge pull request #18093 from colemanw/expFix
[civicrm-core.git] / CRM / Logging / Differ.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Logging_Differ {
18 private $db;
19 private $log_conn_id;
20 private $log_date;
21 private $interval;
22
e0ef6999 23 /**
3b45d110 24 * Class constructor.
25 *
26 * @param string $log_conn_id
bef3accd 27 * @param string $log_date
e0ef6999
EM
28 * @param string $interval
29 */
00be9182 30 public function __construct($log_conn_id, $log_date, $interval = '10 SECOND') {
353ffa53
TO
31 $dsn = defined('CIVICRM_LOGGING_DSN') ? DB::parseDSN(CIVICRM_LOGGING_DSN) : DB::parseDSN(CIVICRM_DSN);
32 $this->db = $dsn['database'];
6a488035 33 $this->log_conn_id = $log_conn_id;
353ffa53 34 $this->log_date = $log_date;
d32d065d 35 $this->interval = self::filterInterval($interval);
6a488035
TO
36 }
37
e0ef6999
EM
38 /**
39 * @param $tables
40 *
41 * @return array
42 */
00be9182 43 public function diffsInTables($tables) {
be2fb01f 44 $diffs = [];
6a488035
TO
45 foreach ($tables as $table) {
46 $diff = $this->diffsInTable($table);
47 if (!empty($diff)) {
48 $diffs[$table] = $diff;
49 }
50 }
51 return $diffs;
52 }
53
e0ef6999
EM
54 /**
55 * @param $table
100fef9d 56 * @param int $contactID
e0ef6999
EM
57 *
58 * @return array
59 */
e60f24eb 60 public function diffsInTable($table, $contactID = NULL) {
be2fb01f 61 $diffs = [];
6a488035 62
be2fb01f
CW
63 $params = [
64 1 => [$this->log_conn_id, 'String'],
65 ];
6a488035 66
87a890cc 67 $logging = new CRM_Logging_Schema();
694e78fd
DS
68 $addressCustomTables = $logging->entityCustomDataLogTables('Address');
69
0b4c85c3 70 $contactIdClause = $join = '';
481a74f4 71 if ($contactID) {
be2fb01f 72 $params[3] = [$contactID, 'Integer'];
6a488035 73 switch ($table) {
353ffa53
TO
74 case 'civicrm_contact':
75 $contactIdClause = "AND id = %3";
76 break;
ea100cb5 77
353ffa53
TO
78 case 'civicrm_note':
79 $contactIdClause = "AND (( entity_id = %3 AND entity_table = 'civicrm_contact' ) OR (entity_id IN (SELECT note.id FROM `{$this->db}`.log_civicrm_note note WHERE note.entity_id = %3 AND note.entity_table = 'civicrm_contact') AND entity_table = 'civicrm_note'))";
80 break;
ea100cb5 81
353ffa53
TO
82 case 'civicrm_entity_tag':
83 $contactIdClause = "AND entity_id = %3 AND entity_table = 'civicrm_contact'";
84 break;
ea100cb5 85
353ffa53
TO
86 case 'civicrm_relationship':
87 $contactIdClause = "AND (contact_id_a = %3 OR contact_id_b = %3)";
88 break;
ea100cb5 89
353ffa53 90 case 'civicrm_activity':
44f817d4 91 $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate');
353ffa53
TO
92 $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts);
93 $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts);
94 $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts);
9e74e3ce 95
353ffa53 96 $join = "
9e74e3ce 97LEFT JOIN civicrm_activity_contact at ON at.activity_id = lt.id AND at.contact_id = %3 AND at.record_type_id = {$targetID}
98LEFT JOIN civicrm_activity_contact aa ON aa.activity_id = lt.id AND aa.contact_id = %3 AND aa.record_type_id = {$assigneeID}
99LEFT JOIN civicrm_activity_contact source ON source.activity_id = lt.id AND source.contact_id = %3 AND source.record_type_id = {$sourceID} ";
353ffa53
TO
100 $contactIdClause = "AND (at.id IS NOT NULL OR aa.id IS NOT NULL OR source.id IS NOT NULL)";
101 break;
ea100cb5 102
353ffa53
TO
103 case 'civicrm_case':
104 $contactIdClause = "AND id = (select case_id FROM civicrm_case_contact WHERE contact_id = %3 LIMIT 1)";
694e78fd 105 break;
694e78fd 106
353ffa53
TO
107 default:
108 if (array_key_exists($table, $addressCustomTables)) {
109 $join = "INNER JOIN `{$this->db}`.`log_civicrm_address` et ON et.id = lt.entity_id";
110 $contactIdClause = "AND contact_id = %3";
111 break;
112 }
6b4b11c4 113
353ffa53
TO
114 // allow tables to be extended by report hook query objects
115 list($contactIdClause, $join) = CRM_Report_BAO_Hook::singleton()->logDiffClause($this, $table);
116
117 if (empty($contactIdClause)) {
118 $contactIdClause = "AND contact_id = %3";
119 }
120 if (strpos($table, 'civicrm_value') !== FALSE) {
121 $contactIdClause = "AND entity_id = %3";
122 }
6a488035
TO
123 }
124 }
125
99008b08 126 $logDateClause = '';
127 if ($this->log_date) {
be2fb01f 128 $params[2] = [$this->log_date, 'String'];
99008b08 129 $logDateClause = "
130 AND lt.log_date BETWEEN DATE_SUB(%2, INTERVAL {$this->interval}) AND DATE_ADD(%2, INTERVAL {$this->interval})
131 ";
132 }
133
6a488035 134 // 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)
0b4c85c3 135 $sql = "
8ef12e64 136SELECT DISTINCT lt.id FROM `{$this->db}`.`log_$table` lt
137{$join}
99008b08 138WHERE lt.log_conn_id = %1
139 $logDateClause
140 {$contactIdClause}";
6a488035
TO
141 $dao = CRM_Core_DAO::executeQuery($sql, $params);
142 while ($dao->fetch()) {
143 $diffs = array_merge($diffs, $this->diffsInTableForId($table, $dao->id));
144 }
145
146 return $diffs;
147 }
148
e0ef6999
EM
149 /**
150 * @param $table
100fef9d 151 * @param int $id
e0ef6999
EM
152 *
153 * @return array
aa00132e 154 * @throws \CRM_Core_Exception
e0ef6999 155 */
6a488035 156 private function diffsInTableForId($table, $id) {
be2fb01f 157 $diffs = [];
6a488035 158
be2fb01f
CW
159 $params = [
160 1 => [$this->log_conn_id, 'String'],
161 3 => [$id, 'Integer'],
162 ];
6a488035 163
b44e3f84 164 // 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
99008b08 165 $logDateClause = "";
166 if ($this->log_date && $this->interval) {
167 $logDateClause = " AND log_date >= %2 AND log_date < DATE_ADD(%2, INTERVAL {$this->interval})";
be2fb01f 168 $params[2] = [$this->log_date, 'String'];
99008b08 169 }
170
171 $changedSQL = "SELECT * FROM `{$this->db}`.`log_$table` WHERE log_conn_id = %1 $logDateClause AND id = %3 ORDER BY log_date DESC LIMIT 1";
6a488035
TO
172
173 $changedDAO = CRM_Core_DAO::executeQuery($changedSQL, $params);
481a74f4 174 while ($changedDAO->fetch()) {
9bf49a0e 175 if (empty($this->log_date) && !self::checkLogCanBeUsedWithNoLogDate($changedDAO->log_date)) {
99008b08 176 throw new CRM_Core_Exception('The connection date must be passed in to disambiguate this logging entry per CRM-18193');
177 }
6a488035
TO
178 $changed = $changedDAO->toArray();
179
180 // return early if nothing found
181 if (empty($changed)) {
182 continue;
183 }
184
185 switch ($changed['log_action']) {
186 case 'Delete':
187 // the previous state is kept in the current state, current should keep the keys and clear the values
188 $original = $changed;
353ffa53
TO
189 foreach ($changed as & $val) {
190 $val = NULL;
191 }
6a488035
TO
192 $changed['log_action'] = 'Delete';
193 break;
194
195 case 'Insert':
196 // the previous state does not exist
be2fb01f 197 $original = [];
6a488035
TO
198 break;
199
200 case 'Update':
be2fb01f 201 $params[2] = [$changedDAO->log_date, 'String'];
6a488035
TO
202 // look for the previous state (different log_conn_id) of the given id
203 $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";
204 $original = $this->sqlToArray($originalSQL, $params);
205 if (empty($original)) {
8ef12e64 206 // A blank original array is not possible for Update action, otherwise we 'll end up displaying all information
6a488035
TO
207 // in $changed variable as updated-info
208 $original = $changed;
209 }
210
211 break;
212 }
213
214 // populate $diffs with only the differences between $changed and $original
be2fb01f 215 $skipped = ['log_action', 'log_conn_id', 'log_date', 'log_user_id'];
6a488035
TO
216 foreach (array_keys(array_diff_assoc($changed, $original)) as $diff) {
217 if (in_array($diff, $skipped)) {
218 continue;
219 }
220
221 if (CRM_Utils_Array::value($diff, $original) === CRM_Utils_Array::value($diff, $changed)) {
222 continue;
223 }
8ef12e64 224
225 // hack: case_type_id column is a varchar with separator. For proper mapping to type labels,
6a488035
TO
226 // we need to make sure separators are trimmed
227 if ($diff == 'case_type_id') {
be2fb01f 228 foreach (['original', 'changed'] as $var) {
6ef04c72 229 if (!empty(${$var[$diff]})) {
6a488035
TO
230 $holder =& $$var;
231 $val = explode(CRM_Case_BAO_Case::VALUE_SEPARATOR, $holder[$diff]);
9c1bc317 232 $holder[$diff] = $val[1] ?? NULL;
6a488035
TO
233 }
234 }
235 }
236
be2fb01f 237 $diffs[] = [
6a488035
TO
238 'action' => $changed['log_action'],
239 'id' => $id,
240 'field' => $diff,
6b409353
CW
241 'from' => $original[$diff] ?? NULL,
242 'to' => $changed[$diff] ?? NULL,
29444295 243 'table' => $table,
244 'log_date' => $changed['log_date'],
245 'log_conn_id' => $changed['log_conn_id'],
be2fb01f 246 ];
6a488035
TO
247 }
248 }
249
250 return $diffs;
251 }
252
e0ef6999 253 /**
10b32ed4 254 * Get the titles & metadata option values for the table.
255 *
256 * For custom fields the titles may change so we use the ones as at the reference date.
257 *
258 * @param string $table
259 * @param string $referenceDate
e0ef6999
EM
260 *
261 * @return array
262 */
10b32ed4 263 public function titlesAndValuesForTable($table, $referenceDate) {
6a488035 264 // static caches for subsequent calls with the same $table
be2fb01f
CW
265 static $titles = [];
266 static $values = [];
6a488035 267
6a488035 268 if (!isset($titles[$table]) or !isset($values[$table])) {
8485f8e7 269 if (($tableDAO = CRM_Core_DAO_AllCoreTables::getClassForTable($table)) != FALSE) {
6a488035
TO
270 // FIXME: these should be populated with pseudo constants as they
271 // were at the time of logging rather than their current values
c0c9cd82 272 // FIXME: Use *_BAO:buildOptions() method rather than pseudoconstants & fetch programmatically
be2fb01f 273 $values[$table] = [
6a488035 274 'contribution_page_id' => CRM_Contribute_PseudoConstant::contributionPage(),
c3b82060 275 'contribution_status_id' => CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label'),
353ffa53 276 'financial_type_id' => CRM_Contribute_PseudoConstant::financialType(),
6a488035 277 'country_id' => CRM_Core_PseudoConstant::country(),
26cf88b5 278 'gender_id' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'),
b2b0530a 279 'location_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'),
6a488035 280 'payment_instrument_id' => CRM_Contribute_PseudoConstant::paymentInstrument(),
b4f964d9 281 'phone_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'),
c0c9cd82
CW
282 'preferred_communication_method' => CRM_Contact_BAO_Contact::buildOptions('preferred_communication_method'),
283 'preferred_language' => CRM_Contact_BAO_Contact::buildOptions('preferred_language'),
284 'prefix_id' => CRM_Contact_BAO_Contact::buildOptions('prefix_id'),
e7e657f0 285 'provider_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'),
6a488035 286 'state_province_id' => CRM_Core_PseudoConstant::stateProvince(),
c0c9cd82 287 'suffix_id' => CRM_Contact_BAO_Contact::buildOptions('suffix_id'),
cbf48754 288 'website_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'),
6a488035 289 'activity_type_id' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE),
42047ce4 290 'case_type_id' => CRM_Case_PseudoConstant::caseType('title', FALSE),
353ffa53 291 'priority_id' => CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'),
be2fb01f 292 ];
6a488035
TO
293
294 // for columns that appear in more than 1 table
295 switch ($table) {
296 case 'civicrm_case':
297 $values[$table]['status_id'] = CRM_Case_PseudoConstant::caseStatus('label', FALSE);
298 break;
ea100cb5 299
6a488035 300 case 'civicrm_activity':
481a74f4 301 $values[$table]['status_id'] = CRM_Core_PseudoConstant::activityStatus();
6a488035
TO
302 break;
303 }
304
8485f8e7 305 $dao = new $tableDAO();
6a488035 306 foreach ($dao->fields() as $field) {
9c1bc317 307 $titles[$table][$field['name']] = $field['title'] ?? NULL;
6a488035
TO
308
309 if ($field['type'] == CRM_Utils_Type::T_BOOLEAN) {
be2fb01f 310 $values[$table][$field['name']] = ['0' => ts('false'), '1' => ts('true')];
6a488035
TO
311 }
312 }
313 }
314 elseif (substr($table, 0, 14) == 'civicrm_value_') {
10b32ed4 315 list($titles[$table], $values[$table]) = $this->titlesAndValuesForCustomDataTable($table, $referenceDate);
0db6c3e1
TO
316 }
317 else {
be2fb01f 318 $titles[$table] = $values[$table] = [];
6a488035
TO
319 }
320 }
321
be2fb01f 322 return [$titles[$table], $values[$table]];
6a488035
TO
323 }
324
e0ef6999
EM
325 /**
326 * @param $sql
c490a46a 327 * @param array $params
e0ef6999
EM
328 *
329 * @return mixed
330 */
6a488035
TO
331 private function sqlToArray($sql, $params) {
332 $dao = CRM_Core_DAO::executeQuery($sql, $params);
333 $dao->fetch();
334 return $dao->toArray();
335 }
336
e0ef6999 337 /**
10b32ed4 338 * Get the field titles & option group values for the custom table as at the reference date.
339 *
340 * @param string $table
341 * @param string $referenceDate
e0ef6999
EM
342 *
343 * @return array
344 */
10b32ed4 345 private function titlesAndValuesForCustomDataTable($table, $referenceDate) {
be2fb01f
CW
346 $titles = [];
347 $values = [];
6a488035 348
be2fb01f
CW
349 $params = [
350 1 => [$this->log_conn_id, 'String'],
351 2 => [$referenceDate, 'String'],
352 3 => [$table, 'String'],
353 ];
6a488035
TO
354
355 $sql = "SELECT id, title FROM `{$this->db}`.log_civicrm_custom_group WHERE log_date <= %2 AND table_name = %3 ORDER BY log_date DESC LIMIT 1";
356 $cgDao = CRM_Core_DAO::executeQuery($sql, $params);
357 $cgDao->fetch();
358
be2fb01f 359 $params[3] = [$cgDao->id, 'Integer'];
6a488035
TO
360 $sql = "
361SELECT column_name, data_type, label, name, option_group_id
362FROM `{$this->db}`.log_civicrm_custom_field
363WHERE log_date <= %2
364AND custom_group_id = %3
365ORDER BY log_date
366";
367 $cfDao = CRM_Core_DAO::executeQuery($sql, $params);
368
369 while ($cfDao->fetch()) {
370 $titles[$cfDao->column_name] = "{$cgDao->title}: {$cfDao->label}";
371
372 switch ($cfDao->data_type) {
373 case 'Boolean':
be2fb01f 374 $values[$cfDao->column_name] = ['0' => ts('false'), '1' => ts('true')];
6a488035
TO
375 break;
376
377 case 'String':
be2fb01f 378 $values[$cfDao->column_name] = [];
6a488035 379 if (!empty($cfDao->option_group_id)) {
be2fb01f 380 $params[3] = [$cfDao->option_group_id, 'Integer'];
6a488035
TO
381 $sql = "
382SELECT label, value
383FROM `{$this->db}`.log_civicrm_option_value
384WHERE log_date <= %2
385AND option_group_id = %3
386ORDER BY log_date
387";
388 $ovDao = CRM_Core_DAO::executeQuery($sql, $params);
389 while ($ovDao->fetch()) {
390 $values[$cfDao->column_name][$ovDao->value] = $ovDao->label;
391 }
392 }
393 break;
394 }
395 }
396
be2fb01f 397 return [$titles, $values];
6a488035 398 }
96025800 399
93afbc3a 400 /**
401 * Get all changes made in the connection.
402 *
403 * @param array $tables
404 * Array of tables to inspect.
405 *
406 * @return array
407 */
408 public function getAllChangesForConnection($tables) {
be2fb01f 409 $params = [1 => [$this->log_conn_id, 'String']];
93afbc3a 410 foreach ($tables as $table) {
411 if (empty($sql)) {
412 $sql = " SELECT '{$table}' as table_name, id FROM {$this->db}.log_{$table} WHERE log_conn_id = %1";
413 }
414 else {
415 $sql .= " UNION SELECT '{$table}' as table_name, id FROM {$this->db}.log_{$table} WHERE log_conn_id = %1";
416 }
417 }
be2fb01f 418 $diffs = [];
93afbc3a 419 $dao = CRM_Core_DAO::executeQuery($sql, $params);
420 while ($dao->fetch()) {
421 if (empty($this->log_date)) {
422 $this->log_date = CRM_Core_DAO::singleValueQuery("SELECT log_date FROM {$this->db}.log_{$table} WHERE log_conn_id = %1 LIMIT 1", $params);
423 }
424 $diffs = array_merge($diffs, $this->diffsInTableForId($dao->table_name, $dao->id));
425 }
426 return $diffs;
427 }
428
99008b08 429 /**
430 * Check that the log record relates to a unique log id.
431 *
432 * If the record was recorded using the old non-unique style then the
433 * log_date
434 * MUST be set to get the (fairly accurate) list of changes. In this case the
435 * nasty 10 second interval rule is applied.
436 *
437 * See CRM-18193 for a discussion of unique log id.
438 *
439 * @param string $change_date
440 *
441 * @return bool
442 * @throws \CiviCRM_API3_Exception
443 */
9bf49a0e 444 public static function checkLogCanBeUsedWithNoLogDate($change_date) {
445
be2fb01f 446 if (civicrm_api3('Setting', 'getvalue', ['name' => 'logging_all_tables_uniquid', 'group' => 'CiviCRM Preferences'])) {
99008b08 447 return TRUE;
448 };
be2fb01f 449 $uniqueDate = civicrm_api3('Setting', 'getvalue', [
99008b08 450 'name' => 'logging_uniqueid_date',
451 'group' => 'CiviCRM Preferences',
be2fb01f 452 ]);
99008b08 453 if (strtotime($uniqueDate) <= strtotime($change_date)) {
454 return TRUE;
455 }
456 else {
457 return FALSE;
458 }
459
460 }
461
d32d065d
TO
462 /**
463 * Filter a MySQL interval expression.
464 *
465 * @param string $interval
466 * @return string
467 * Normalized version of $interval
468 * @throws \CRM_Core_Exception
469 * If the expression is invalid.
470 * @see https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
471 */
472 private static function filterInterval($interval) {
473 if (empty($interval)) {
474 return $interval;
475 }
476
be2fb01f 477 $units = ['MICROSECOND', 'SECOND', 'MINUTE', 'HOUR', 'DAY', 'WEEK', 'MONTH', 'QUARTER', 'YEAR'];
d32d065d
TO
478 $interval = strtoupper($interval);
479 if (preg_match('/^([0-9]+) ([A-Z]+)$/', $interval, $matches)) {
480 if (in_array($matches[2], $units)) {
481 return $interval;
482 }
483 }
484 if (preg_match('/^\'([0-9: \.\-]+)\' ([A-Z]+)_([A-Z]+)$/', $interval, $matches)) {
485 if (in_array($matches[2], $units) && in_array($matches[3], $units)) {
486 return $interval;
487 }
488 }
489 throw new CRM_Core_Exception("Malformed interval");
490 }
491
6a488035 492}