Commit | Line | Data |
---|---|---|
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 | */ |
17 | class 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') { |
58d1e21e SL |
31 | $dsn = defined('CIVICRM_LOGGING_DSN') ? CRM_Utils_SQL::autoSwitchDSN(CIVICRM_LOGGING_DSN) : CRM_Utils_SQL::autoSwitchDSN(CIVICRM_DSN); |
32 | $dsn = DB::parseDSN($dsn); | |
353ffa53 | 33 | $this->db = $dsn['database']; |
6a488035 | 34 | $this->log_conn_id = $log_conn_id; |
353ffa53 | 35 | $this->log_date = $log_date; |
d32d065d | 36 | $this->interval = self::filterInterval($interval); |
6a488035 TO |
37 | } |
38 | ||
e0ef6999 EM |
39 | /** |
40 | * @param $tables | |
41 | * | |
42 | * @return array | |
43 | */ | |
00be9182 | 44 | public function diffsInTables($tables) { |
be2fb01f | 45 | $diffs = []; |
6a488035 TO |
46 | foreach ($tables as $table) { |
47 | $diff = $this->diffsInTable($table); | |
48 | if (!empty($diff)) { | |
49 | $diffs[$table] = $diff; | |
50 | } | |
51 | } | |
52 | return $diffs; | |
53 | } | |
54 | ||
e0ef6999 EM |
55 | /** |
56 | * @param $table | |
100fef9d | 57 | * @param int $contactID |
e0ef6999 EM |
58 | * |
59 | * @return array | |
60 | */ | |
e60f24eb | 61 | public function diffsInTable($table, $contactID = NULL) { |
be2fb01f | 62 | $diffs = []; |
6a488035 | 63 | |
be2fb01f CW |
64 | $params = [ |
65 | 1 => [$this->log_conn_id, 'String'], | |
66 | ]; | |
6a488035 | 67 | |
87a890cc | 68 | $logging = new CRM_Logging_Schema(); |
694e78fd DS |
69 | $addressCustomTables = $logging->entityCustomDataLogTables('Address'); |
70 | ||
0b4c85c3 | 71 | $contactIdClause = $join = ''; |
481a74f4 | 72 | if ($contactID) { |
be2fb01f | 73 | $params[3] = [$contactID, 'Integer']; |
6a488035 | 74 | switch ($table) { |
353ffa53 TO |
75 | case 'civicrm_contact': |
76 | $contactIdClause = "AND id = %3"; | |
77 | break; | |
ea100cb5 | 78 | |
353ffa53 TO |
79 | case 'civicrm_note': |
80 | $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'))"; | |
81 | break; | |
ea100cb5 | 82 | |
353ffa53 TO |
83 | case 'civicrm_entity_tag': |
84 | $contactIdClause = "AND entity_id = %3 AND entity_table = 'civicrm_contact'"; | |
85 | break; | |
ea100cb5 | 86 | |
353ffa53 TO |
87 | case 'civicrm_relationship': |
88 | $contactIdClause = "AND (contact_id_a = %3 OR contact_id_b = %3)"; | |
89 | break; | |
ea100cb5 | 90 | |
353ffa53 | 91 | case 'civicrm_activity': |
44f817d4 | 92 | $activityContacts = CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'validate'); |
353ffa53 TO |
93 | $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); |
94 | $assigneeID = CRM_Utils_Array::key('Activity Assignees', $activityContacts); | |
95 | $targetID = CRM_Utils_Array::key('Activity Targets', $activityContacts); | |
9e74e3ce | 96 | |
353ffa53 | 97 | $join = " |
9e74e3ce | 98 | LEFT JOIN civicrm_activity_contact at ON at.activity_id = lt.id AND at.contact_id = %3 AND at.record_type_id = {$targetID} |
99 | LEFT JOIN civicrm_activity_contact aa ON aa.activity_id = lt.id AND aa.contact_id = %3 AND aa.record_type_id = {$assigneeID} | |
100 | LEFT JOIN civicrm_activity_contact source ON source.activity_id = lt.id AND source.contact_id = %3 AND source.record_type_id = {$sourceID} "; | |
353ffa53 TO |
101 | $contactIdClause = "AND (at.id IS NOT NULL OR aa.id IS NOT NULL OR source.id IS NOT NULL)"; |
102 | break; | |
ea100cb5 | 103 | |
353ffa53 TO |
104 | case 'civicrm_case': |
105 | $contactIdClause = "AND id = (select case_id FROM civicrm_case_contact WHERE contact_id = %3 LIMIT 1)"; | |
694e78fd | 106 | break; |
694e78fd | 107 | |
353ffa53 TO |
108 | default: |
109 | if (array_key_exists($table, $addressCustomTables)) { | |
110 | $join = "INNER JOIN `{$this->db}`.`log_civicrm_address` et ON et.id = lt.entity_id"; | |
111 | $contactIdClause = "AND contact_id = %3"; | |
112 | break; | |
113 | } | |
6b4b11c4 | 114 | |
353ffa53 TO |
115 | // allow tables to be extended by report hook query objects |
116 | list($contactIdClause, $join) = CRM_Report_BAO_Hook::singleton()->logDiffClause($this, $table); | |
117 | ||
118 | if (empty($contactIdClause)) { | |
119 | $contactIdClause = "AND contact_id = %3"; | |
120 | } | |
121 | if (strpos($table, 'civicrm_value') !== FALSE) { | |
122 | $contactIdClause = "AND entity_id = %3"; | |
123 | } | |
6a488035 TO |
124 | } |
125 | } | |
126 | ||
99008b08 | 127 | $logDateClause = ''; |
128 | if ($this->log_date) { | |
be2fb01f | 129 | $params[2] = [$this->log_date, 'String']; |
99008b08 | 130 | $logDateClause = " |
131 | AND lt.log_date BETWEEN DATE_SUB(%2, INTERVAL {$this->interval}) AND DATE_ADD(%2, INTERVAL {$this->interval}) | |
132 | "; | |
133 | } | |
134 | ||
6a488035 | 135 | // 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 | 136 | $sql = " |
8ef12e64 | 137 | SELECT DISTINCT lt.id FROM `{$this->db}`.`log_$table` lt |
138 | {$join} | |
99008b08 | 139 | WHERE lt.log_conn_id = %1 |
140 | $logDateClause | |
141 | {$contactIdClause}"; | |
6a488035 TO |
142 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
143 | while ($dao->fetch()) { | |
144 | $diffs = array_merge($diffs, $this->diffsInTableForId($table, $dao->id)); | |
145 | } | |
146 | ||
147 | return $diffs; | |
148 | } | |
149 | ||
e0ef6999 EM |
150 | /** |
151 | * @param $table | |
100fef9d | 152 | * @param int $id |
e0ef6999 EM |
153 | * |
154 | * @return array | |
aa00132e | 155 | * @throws \CRM_Core_Exception |
e0ef6999 | 156 | */ |
6a488035 | 157 | private function diffsInTableForId($table, $id) { |
be2fb01f | 158 | $diffs = []; |
6a488035 | 159 | |
be2fb01f CW |
160 | $params = [ |
161 | 1 => [$this->log_conn_id, 'String'], | |
162 | 3 => [$id, 'Integer'], | |
163 | ]; | |
6a488035 | 164 | |
b44e3f84 | 165 | // 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 | 166 | $logDateClause = ""; |
167 | if ($this->log_date && $this->interval) { | |
168 | $logDateClause = " AND log_date >= %2 AND log_date < DATE_ADD(%2, INTERVAL {$this->interval})"; | |
be2fb01f | 169 | $params[2] = [$this->log_date, 'String']; |
99008b08 | 170 | } |
171 | ||
172 | $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 |
173 | |
174 | $changedDAO = CRM_Core_DAO::executeQuery($changedSQL, $params); | |
481a74f4 | 175 | while ($changedDAO->fetch()) { |
9bf49a0e | 176 | if (empty($this->log_date) && !self::checkLogCanBeUsedWithNoLogDate($changedDAO->log_date)) { |
99008b08 | 177 | throw new CRM_Core_Exception('The connection date must be passed in to disambiguate this logging entry per CRM-18193'); |
178 | } | |
6a488035 TO |
179 | $changed = $changedDAO->toArray(); |
180 | ||
181 | // return early if nothing found | |
182 | if (empty($changed)) { | |
183 | continue; | |
184 | } | |
185 | ||
186 | switch ($changed['log_action']) { | |
187 | case 'Delete': | |
188 | // the previous state is kept in the current state, current should keep the keys and clear the values | |
189 | $original = $changed; | |
353ffa53 TO |
190 | foreach ($changed as & $val) { |
191 | $val = NULL; | |
192 | } | |
6a488035 TO |
193 | $changed['log_action'] = 'Delete'; |
194 | break; | |
195 | ||
196 | case 'Insert': | |
197 | // the previous state does not exist | |
be2fb01f | 198 | $original = []; |
6a488035 TO |
199 | break; |
200 | ||
201 | case 'Update': | |
be2fb01f | 202 | $params[2] = [$changedDAO->log_date, 'String']; |
6a488035 TO |
203 | // look for the previous state (different log_conn_id) of the given id |
204 | $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"; | |
205 | $original = $this->sqlToArray($originalSQL, $params); | |
206 | if (empty($original)) { | |
8ef12e64 | 207 | // A blank original array is not possible for Update action, otherwise we 'll end up displaying all information |
6a488035 TO |
208 | // in $changed variable as updated-info |
209 | $original = $changed; | |
210 | } | |
211 | ||
212 | break; | |
213 | } | |
214 | ||
215 | // populate $diffs with only the differences between $changed and $original | |
be2fb01f | 216 | $skipped = ['log_action', 'log_conn_id', 'log_date', 'log_user_id']; |
6a488035 TO |
217 | foreach (array_keys(array_diff_assoc($changed, $original)) as $diff) { |
218 | if (in_array($diff, $skipped)) { | |
219 | continue; | |
220 | } | |
221 | ||
222 | if (CRM_Utils_Array::value($diff, $original) === CRM_Utils_Array::value($diff, $changed)) { | |
223 | continue; | |
224 | } | |
8ef12e64 | 225 | |
226 | // hack: case_type_id column is a varchar with separator. For proper mapping to type labels, | |
6a488035 TO |
227 | // we need to make sure separators are trimmed |
228 | if ($diff == 'case_type_id') { | |
be2fb01f | 229 | foreach (['original', 'changed'] as $var) { |
6ef04c72 | 230 | if (!empty(${$var[$diff]})) { |
6a488035 TO |
231 | $holder =& $$var; |
232 | $val = explode(CRM_Case_BAO_Case::VALUE_SEPARATOR, $holder[$diff]); | |
9c1bc317 | 233 | $holder[$diff] = $val[1] ?? NULL; |
6a488035 TO |
234 | } |
235 | } | |
236 | } | |
237 | ||
be2fb01f | 238 | $diffs[] = [ |
6a488035 TO |
239 | 'action' => $changed['log_action'], |
240 | 'id' => $id, | |
241 | 'field' => $diff, | |
6b409353 CW |
242 | 'from' => $original[$diff] ?? NULL, |
243 | 'to' => $changed[$diff] ?? NULL, | |
29444295 | 244 | 'table' => $table, |
245 | 'log_date' => $changed['log_date'], | |
246 | 'log_conn_id' => $changed['log_conn_id'], | |
be2fb01f | 247 | ]; |
6a488035 TO |
248 | } |
249 | } | |
250 | ||
251 | return $diffs; | |
252 | } | |
253 | ||
e0ef6999 | 254 | /** |
10b32ed4 | 255 | * Get the titles & metadata option values for the table. |
256 | * | |
257 | * For custom fields the titles may change so we use the ones as at the reference date. | |
258 | * | |
259 | * @param string $table | |
260 | * @param string $referenceDate | |
e0ef6999 EM |
261 | * |
262 | * @return array | |
263 | */ | |
10b32ed4 | 264 | public function titlesAndValuesForTable($table, $referenceDate) { |
6a488035 | 265 | // static caches for subsequent calls with the same $table |
be2fb01f CW |
266 | static $titles = []; |
267 | static $values = []; | |
6a488035 | 268 | |
6a488035 | 269 | if (!isset($titles[$table]) or !isset($values[$table])) { |
8485f8e7 | 270 | if (($tableDAO = CRM_Core_DAO_AllCoreTables::getClassForTable($table)) != FALSE) { |
6a488035 TO |
271 | // FIXME: these should be populated with pseudo constants as they |
272 | // were at the time of logging rather than their current values | |
c0c9cd82 | 273 | // FIXME: Use *_BAO:buildOptions() method rather than pseudoconstants & fetch programmatically |
be2fb01f | 274 | $values[$table] = [ |
6a488035 | 275 | 'contribution_page_id' => CRM_Contribute_PseudoConstant::contributionPage(), |
c3b82060 | 276 | 'contribution_status_id' => CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'label'), |
353ffa53 | 277 | 'financial_type_id' => CRM_Contribute_PseudoConstant::financialType(), |
6a488035 | 278 | 'country_id' => CRM_Core_PseudoConstant::country(), |
26cf88b5 | 279 | 'gender_id' => CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'), |
b2b0530a | 280 | 'location_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'), |
6a488035 | 281 | 'payment_instrument_id' => CRM_Contribute_PseudoConstant::paymentInstrument(), |
b4f964d9 | 282 | 'phone_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'), |
c0c9cd82 CW |
283 | 'preferred_communication_method' => CRM_Contact_BAO_Contact::buildOptions('preferred_communication_method'), |
284 | 'preferred_language' => CRM_Contact_BAO_Contact::buildOptions('preferred_language'), | |
285 | 'prefix_id' => CRM_Contact_BAO_Contact::buildOptions('prefix_id'), | |
e7e657f0 | 286 | 'provider_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'), |
6a488035 | 287 | 'state_province_id' => CRM_Core_PseudoConstant::stateProvince(), |
c0c9cd82 | 288 | 'suffix_id' => CRM_Contact_BAO_Contact::buildOptions('suffix_id'), |
cbf48754 | 289 | 'website_type_id' => CRM_Core_PseudoConstant::get('CRM_Core_DAO_Website', 'website_type_id'), |
6a488035 | 290 | 'activity_type_id' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE), |
42047ce4 | 291 | 'case_type_id' => CRM_Case_PseudoConstant::caseType('title', FALSE), |
353ffa53 | 292 | 'priority_id' => CRM_Core_PseudoConstant::get('CRM_Activity_DAO_Activity', 'priority_id'), |
3c547c18 | 293 | 'record_type_id' => CRM_Activity_BAO_ActivityContact::buildOptions('record_type_id', 'get'), |
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 | 308 | foreach ($dao->fields() as $field) { |
9c1bc317 | 309 | $titles[$table][$field['name']] = $field['title'] ?? NULL; |
6a488035 TO |
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 = " |
363 | SELECT column_name, data_type, label, name, option_group_id | |
364 | FROM `{$this->db}`.log_civicrm_custom_field | |
365 | WHERE log_date <= %2 | |
366 | AND custom_group_id = %3 | |
367 | ORDER 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 = " |
384 | SELECT label, value | |
385 | FROM `{$this->db}`.log_civicrm_option_value | |
386 | WHERE log_date <= %2 | |
387 | AND option_group_id = %3 | |
388 | ORDER 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. | |
2f41db93 VP |
407 | * @param int $limit |
408 | * Limit result to x | |
409 | * @param int $offset | |
410 | * Offset result to y | |
93afbc3a | 411 | * |
412 | * @return array | |
413 | */ | |
2f41db93 VP |
414 | public function getAllChangesForConnection($tables, $limit = 0, $offset = 0) { |
415 | $params = [ | |
416 | 1 => [$this->log_conn_id, 'String'], | |
417 | 2 => [$limit, 'Integer'], | |
418 | 3 => [$offset, 'Integer'], | |
419 | ]; | |
420 | ||
93afbc3a | 421 | foreach ($tables as $table) { |
422 | if (empty($sql)) { | |
423 | $sql = " SELECT '{$table}' as table_name, id FROM {$this->db}.log_{$table} WHERE log_conn_id = %1"; | |
424 | } | |
425 | else { | |
426 | $sql .= " UNION SELECT '{$table}' as table_name, id FROM {$this->db}.log_{$table} WHERE log_conn_id = %1"; | |
427 | } | |
428 | } | |
2f41db93 VP |
429 | if ($limit) { |
430 | $sql .= " LIMIT %2"; | |
431 | } | |
432 | if ($offset) { | |
433 | $sql .= " OFFSET %3"; | |
434 | } | |
be2fb01f | 435 | $diffs = []; |
93afbc3a | 436 | $dao = CRM_Core_DAO::executeQuery($sql, $params); |
437 | while ($dao->fetch()) { | |
438 | if (empty($this->log_date)) { | |
89b183fa SP |
439 | // look for available table in above query instead of looking for last table. this will avoid multiple loops |
440 | $this->log_date = CRM_Core_DAO::singleValueQuery("SELECT log_date FROM {$this->db}.log_{$dao->table_name} WHERE log_conn_id = %1 LIMIT 1", $params); | |
93afbc3a | 441 | } |
442 | $diffs = array_merge($diffs, $this->diffsInTableForId($dao->table_name, $dao->id)); | |
443 | } | |
444 | return $diffs; | |
445 | } | |
446 | ||
2f41db93 VP |
447 | /** |
448 | * Get count of all changes made in the connection. | |
449 | * | |
450 | * @param array $tables | |
451 | * Array of tables to inspect. | |
452 | * | |
453 | * @return array | |
454 | */ | |
455 | public function getCountOfAllContactChangesForConnection($tables) { | |
456 | $count = 0; | |
457 | $params = [1 => [$this->log_conn_id, 'String']]; | |
458 | foreach ($tables as $table) { | |
459 | if (empty($sql)) { | |
460 | $sql = " SELECT '{$table}' as table_name, id FROM {$this->db}.log_{$table} WHERE log_conn_id = %1"; | |
461 | } | |
462 | else { | |
463 | $sql .= " UNION SELECT '{$table}' as table_name, id FROM {$this->db}.log_{$table} WHERE log_conn_id = %1"; | |
464 | } | |
465 | } | |
466 | $countSQL = " SELECT count(*) as countOfContacts FROM ({$sql}) count"; | |
467 | $count = CRM_Core_DAO::singleValueQuery($countSQL, $params); | |
468 | return $count; | |
469 | } | |
470 | ||
99008b08 | 471 | /** |
472 | * Check that the log record relates to a unique log id. | |
473 | * | |
474 | * If the record was recorded using the old non-unique style then the | |
475 | * log_date | |
476 | * MUST be set to get the (fairly accurate) list of changes. In this case the | |
477 | * nasty 10 second interval rule is applied. | |
478 | * | |
479 | * See CRM-18193 for a discussion of unique log id. | |
480 | * | |
481 | * @param string $change_date | |
482 | * | |
483 | * @return bool | |
484 | * @throws \CiviCRM_API3_Exception | |
485 | */ | |
9bf49a0e | 486 | public static function checkLogCanBeUsedWithNoLogDate($change_date) { |
487 | ||
be2fb01f | 488 | if (civicrm_api3('Setting', 'getvalue', ['name' => 'logging_all_tables_uniquid', 'group' => 'CiviCRM Preferences'])) { |
99008b08 | 489 | return TRUE; |
490 | }; | |
be2fb01f | 491 | $uniqueDate = civicrm_api3('Setting', 'getvalue', [ |
99008b08 | 492 | 'name' => 'logging_uniqueid_date', |
493 | 'group' => 'CiviCRM Preferences', | |
be2fb01f | 494 | ]); |
99008b08 | 495 | if (strtotime($uniqueDate) <= strtotime($change_date)) { |
496 | return TRUE; | |
497 | } | |
498 | else { | |
499 | return FALSE; | |
500 | } | |
501 | ||
502 | } | |
503 | ||
d32d065d TO |
504 | /** |
505 | * Filter a MySQL interval expression. | |
506 | * | |
507 | * @param string $interval | |
508 | * @return string | |
509 | * Normalized version of $interval | |
510 | * @throws \CRM_Core_Exception | |
511 | * If the expression is invalid. | |
512 | * @see https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add | |
513 | */ | |
514 | private static function filterInterval($interval) { | |
515 | if (empty($interval)) { | |
516 | return $interval; | |
517 | } | |
518 | ||
be2fb01f | 519 | $units = ['MICROSECOND', 'SECOND', 'MINUTE', 'HOUR', 'DAY', 'WEEK', 'MONTH', 'QUARTER', 'YEAR']; |
d32d065d TO |
520 | $interval = strtoupper($interval); |
521 | if (preg_match('/^([0-9]+) ([A-Z]+)$/', $interval, $matches)) { | |
522 | if (in_array($matches[2], $units)) { | |
523 | return $interval; | |
524 | } | |
525 | } | |
526 | if (preg_match('/^\'([0-9: \.\-]+)\' ([A-Z]+)_([A-Z]+)$/', $interval, $matches)) { | |
527 | if (in_array($matches[2], $units) && in_array($matches[3], $units)) { | |
528 | return $interval; | |
529 | } | |
530 | } | |
531 | throw new CRM_Core_Exception("Malformed interval"); | |
532 | } | |
533 | ||
6a488035 | 534 | } |