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