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_Schema { | |
204aa6fb PF |
18 | |
19 | /** | |
20 | * Default storage engine for log tables | |
21 | * | |
22 | * @var string | |
23 | */ | |
24 | const ENGINE = 'InnoDB'; | |
25 | ||
be2fb01f CW |
26 | private $logs = []; |
27 | private $tables = []; | |
6a488035 | 28 | |
d0b5c3f6 EM |
29 | /** |
30 | * Name of the database where the logging data is stored. | |
31 | * | |
32 | * @var string | |
33 | */ | |
6a488035 | 34 | private $db; |
d0b5c3f6 | 35 | |
340f6aa0 | 36 | private $useDBPrefix = TRUE; |
6a488035 | 37 | |
be2fb01f | 38 | private $reports = [ |
6a488035 TO |
39 | 'logging/contact/detail', |
40 | 'logging/contact/summary', | |
41 | 'logging/contribute/detail', | |
42 | 'logging/contribute/summary', | |
be2fb01f | 43 | ]; |
6a488035 | 44 | |
74b348da | 45 | /** |
46 | * Columns that should never be subject to logging. | |
47 | * | |
48 | * CRM-13028 / NYSS-6933 - table => array (cols) - to be excluded from the update statement | |
49 | * | |
50 | * @var array | |
51 | */ | |
be2fb01f CW |
52 | private $exceptions = [ |
53 | 'civicrm_job' => ['last_run'], | |
54 | 'civicrm_group' => ['cache_date', 'refresh_date'], | |
55 | ]; | |
4d1040bb | 56 | |
ef587f9c | 57 | /** |
58 | * Specifications of all log table including | |
204aa6fb | 59 | * - engine (default is InnoDB, if not set.) |
d7ea7150 | 60 | * - engine_config, a string appended to the engine type. |
61 | * For INNODB space can be saved with 'ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4' | |
ef587f9c | 62 | * - indexes (default is none and they cannot be added unless engine is innodb. If they are added and |
63 | * engine is not set to innodb an exception will be thrown since quiet acquiescence is easier to miss). | |
64 | * - exceptions (by default those stored in $this->exceptions are included). These are | |
65 | * excluded from the triggers. | |
66 | * | |
67 | * @var array | |
68 | */ | |
be2fb01f | 69 | private $logTableSpec = []; |
ef587f9c | 70 | |
e299c1d0 | 71 | /** |
3d469574 | 72 | * Setting Callback - Validate. |
e299c1d0 TO |
73 | * |
74 | * @param mixed $value | |
75 | * @param array $fieldSpec | |
3d469574 | 76 | * |
e299c1d0 TO |
77 | * @return bool |
78 | * @throws API_Exception | |
79 | */ | |
80 | public static function checkLoggingSupport(&$value, $fieldSpec) { | |
f55f7133 | 81 | if (!(CRM_Core_DAO::checkTriggerViewPermission(FALSE)) && $value) { |
c49c7067 | 82 | throw new API_Exception(ts("In order to use this functionality, the installation's database user must have privileges to create triggers and views (if binary logging is enabled – this means the SUPER privilege). This install does not have the required privilege(s) enabled.")); |
e299c1d0 | 83 | } |
a99a6696 SP |
84 | // dev/core#1812 Disable logging in a multilingual environment. |
85 | if (CRM_Core_I18n::isMultilingual() && $value) { | |
86 | throw new API_Exception(ts("Logging is not supported in a multilingual environment!")); | |
87 | } | |
e299c1d0 TO |
88 | return TRUE; |
89 | } | |
90 | ||
91 | /** | |
3d469574 | 92 | * Setting Callback - On Change. |
93 | * | |
e299c1d0 TO |
94 | * Respond to changes in the "logging" setting. Set up or destroy |
95 | * triggers, etal. | |
96 | * | |
97 | * @param array $oldValue | |
98 | * List of component names. | |
99 | * @param array $newValue | |
100 | * List of component names. | |
101 | * @param array $metadata | |
102 | * Specification of the setting (per *.settings.php). | |
103 | */ | |
104 | public static function onToggle($oldValue, $newValue, $metadata) { | |
105 | if ($oldValue == $newValue) { | |
106 | return; | |
107 | } | |
108 | ||
109 | $logging = new CRM_Logging_Schema(); | |
110 | if ($newValue) { | |
111 | $logging->enableLogging(); | |
112 | } | |
113 | else { | |
114 | $logging->disableLogging(); | |
115 | } | |
116 | } | |
117 | ||
6a488035 TO |
118 | /** |
119 | * Populate $this->tables and $this->logs with current db state. | |
120 | */ | |
00be9182 | 121 | public function __construct() { |
d0b5c3f6 | 122 | $civiDBName = $this->getCiviCRMDatabaseName(); |
6a488035 TO |
123 | |
124 | $dao = CRM_Core_DAO::executeQuery(" | |
125 | SELECT TABLE_NAME | |
126 | FROM INFORMATION_SCHEMA.TABLES | |
127 | WHERE TABLE_SCHEMA = '{$civiDBName}' | |
128 | AND TABLE_TYPE = 'BASE TABLE' | |
129 | AND TABLE_NAME LIKE 'civicrm_%' | |
130 | "); | |
131 | while ($dao->fetch()) { | |
132 | $this->tables[] = $dao->TABLE_NAME; | |
133 | } | |
8a475c3d EM |
134 | // Get any non standard table names used for custom groups. |
135 | // include these BEFORE the hook is called. | |
136 | $customFieldDAO = CRM_Core_DAO::executeQuery(" | |
137 | SELECT DISTINCT table_name as TABLE_NAME FROM civicrm_custom_group | |
138 | where table_name NOT LIKE 'civicrm_%'; | |
139 | "); | |
140 | while ($customFieldDAO->fetch()) { | |
141 | $this->tables[] = $customFieldDAO->TABLE_NAME; | |
142 | } | |
6a488035 | 143 | |
3292d079 | 144 | // do not log temp import, cache, menu and log tables |
6a488035 TO |
145 | $this->tables = preg_grep('/^civicrm_import_job_/', $this->tables, PREG_GREP_INVERT); |
146 | $this->tables = preg_grep('/_cache$/', $this->tables, PREG_GREP_INVERT); | |
147 | $this->tables = preg_grep('/_log/', $this->tables, PREG_GREP_INVERT); | |
6a488035 | 148 | $this->tables = preg_grep('/^civicrm_queue_/', $this->tables, PREG_GREP_INVERT); |
3d469574 | 149 | //CRM-14672 |
150 | $this->tables = preg_grep('/^civicrm_menu/', $this->tables, PREG_GREP_INVERT); | |
db70b323 | 151 | $this->tables = preg_grep('/_temp_/', $this->tables, PREG_GREP_INVERT); |
e719c24e | 152 | // CRM-18178 |
153 | $this->tables = preg_grep('/_bak$/', $this->tables, PREG_GREP_INVERT); | |
154 | $this->tables = preg_grep('/_backup$/', $this->tables, PREG_GREP_INVERT); | |
f7643f6b ML |
155 | // dev/core#462 |
156 | $this->tables = preg_grep('/^civicrm_tmp_/', $this->tables, PREG_GREP_INVERT); | |
6a488035 | 157 | |
f9b793b0 DL |
158 | // do not log civicrm_mailing_event* tables, CRM-12300 |
159 | $this->tables = preg_grep('/^civicrm_mailing_event_/', $this->tables, PREG_GREP_INVERT); | |
160 | ||
45e181f1 JK |
161 | // dev/core#1762 Don't log subscription_history |
162 | $this->tables = preg_grep('/^civicrm_subscription_history/', $this->tables, PREG_GREP_INVERT); | |
163 | ||
d0cef561 | 164 | // do not log civicrm_mailing_recipients table, CRM-16193 |
be2fb01f CW |
165 | $this->tables = array_diff($this->tables, ['civicrm_mailing_recipients']); |
166 | $this->logTableSpec = array_fill_keys($this->tables, []); | |
ef587f9c | 167 | foreach ($this->exceptions as $tableName => $fields) { |
168 | $this->logTableSpec[$tableName]['exceptions'] = $fields; | |
169 | } | |
170 | CRM_Utils_Hook::alterLogTables($this->logTableSpec); | |
171 | $this->tables = array_keys($this->logTableSpec); | |
f8d28563 | 172 | $nonStandardTableNameString = $this->getNonStandardTableNameFilterString(); |
d0cef561 | 173 | |
d0b5c3f6 EM |
174 | $this->db = $this->getDatabaseNameFromDSN(defined('CIVICRM_LOGGING_DSN') ? CIVICRM_LOGGING_DSN : CIVICRM_DSN); |
175 | $this->useDBPrefix = $this->db !== $civiDBName; | |
6a488035 TO |
176 | |
177 | $dao = CRM_Core_DAO::executeQuery(" | |
178 | SELECT TABLE_NAME | |
179 | FROM INFORMATION_SCHEMA.TABLES | |
180 | WHERE TABLE_SCHEMA = '{$this->db}' | |
181 | AND TABLE_TYPE = 'BASE TABLE' | |
f8d28563 | 182 | AND (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString ) |
6a488035 TO |
183 | "); |
184 | while ($dao->fetch()) { | |
185 | $log = $dao->TABLE_NAME; | |
186 | $this->logs[substr($log, 4)] = $log; | |
187 | } | |
188 | } | |
189 | ||
190 | /** | |
191 | * Return logging custom data tables. | |
192 | */ | |
00be9182 | 193 | public function customDataLogTables() { |
6a488035 TO |
194 | return preg_grep('/^log_civicrm_value_/', $this->logs); |
195 | } | |
196 | ||
694e78fd DS |
197 | /** |
198 | * Return custom data tables for specified entity / extends. | |
ea3ddccf | 199 | * |
200 | * @param string $extends | |
201 | * | |
202 | * @return array | |
694e78fd | 203 | */ |
00be9182 | 204 | public function entityCustomDataLogTables($extends) { |
be2fb01f | 205 | $customGroupTables = []; |
694e78fd DS |
206 | $customGroupDAO = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity($extends); |
207 | $customGroupDAO->find(); | |
208 | while ($customGroupDAO->fetch()) { | |
f2f65d33 | 209 | // logging is disabled for the table (e.g by hook) then $this->logs[$customGroupDAO->table_name] |
210 | // will be empty. | |
211 | if (!empty($this->logs[$customGroupDAO->table_name])) { | |
212 | $customGroupTables[$customGroupDAO->table_name] = $this->logs[$customGroupDAO->table_name]; | |
213 | } | |
694e78fd DS |
214 | } |
215 | return $customGroupTables; | |
216 | } | |
217 | ||
6a488035 TO |
218 | /** |
219 | * Disable logging by dropping the triggers (but keep the log tables intact). | |
220 | */ | |
00be9182 | 221 | public function disableLogging() { |
6a488035 TO |
222 | $config = CRM_Core_Config::singleton(); |
223 | $config->logging = FALSE; | |
224 | ||
225 | $this->dropTriggers(); | |
226 | ||
227 | // invoke the meta trigger creation call | |
228 | CRM_Core_DAO::triggerRebuild(); | |
229 | ||
230 | $this->deleteReports(); | |
231 | } | |
232 | ||
233 | /** | |
234 | * Drop triggers for all logged tables. | |
ad37ac8e | 235 | * |
236 | * @param string $tableName | |
6a488035 | 237 | */ |
0a089620 | 238 | public function dropTriggers($tableName = NULL): void { |
7a29e455 TO |
239 | /** @var \Civi\Core\SqlTriggers $sqlTriggers */ |
240 | $sqlTriggers = Civi::service('sql_triggers'); | |
ae5ffbb7 | 241 | $dao = new CRM_Core_DAO(); |
6a488035 TO |
242 | |
243 | if ($tableName) { | |
be2fb01f | 244 | $tableNames = [$tableName]; |
6a488035 TO |
245 | } |
246 | else { | |
247 | $tableNames = $this->tables; | |
248 | } | |
249 | ||
9b0ff7e2 EM |
250 | // Sort the table names so the sql output is consistent for those sites |
251 | // loading it asynchronously (using the setting 'logging_no_trigger_permission') | |
f44f9df8 | 252 | asort($tableNames); |
6a488035 | 253 | foreach ($tableNames as $table) { |
6842bb53 DL |
254 | $validName = CRM_Core_DAO::shortenSQLName($table, 48, TRUE); |
255 | ||
6a488035 | 256 | // before triggers |
7a29e455 TO |
257 | $sqlTriggers->enqueueQuery("DROP TRIGGER IF EXISTS {$validName}_before_insert"); |
258 | $sqlTriggers->enqueueQuery("DROP TRIGGER IF EXISTS {$validName}_before_update"); | |
259 | $sqlTriggers->enqueueQuery("DROP TRIGGER IF EXISTS {$validName}_before_delete"); | |
6a488035 | 260 | |
353ffa53 | 261 | // after triggers |
7a29e455 TO |
262 | $sqlTriggers->enqueueQuery("DROP TRIGGER IF EXISTS {$validName}_after_insert"); |
263 | $sqlTriggers->enqueueQuery("DROP TRIGGER IF EXISTS {$validName}_after_update"); | |
264 | $sqlTriggers->enqueueQuery("DROP TRIGGER IF EXISTS {$validName}_after_delete"); | |
6a488035 | 265 | } |
a8dd306e DL |
266 | |
267 | // now lets also be safe and drop all triggers that start with | |
268 | // civicrm_ if we are dropping all triggers | |
269 | // we need to do this to capture all the leftover triggers since | |
270 | // we did the shortening trigger name for CRM-11794 | |
271 | if ($tableName === NULL) { | |
272 | $triggers = $dao->executeQuery("SHOW TRIGGERS LIKE 'civicrm_%'"); | |
273 | ||
274 | while ($triggers->fetch()) { | |
7a29e455 | 275 | $sqlTriggers->enqueueQuery("DROP TRIGGER IF EXISTS {$triggers->Trigger}"); |
a8dd306e DL |
276 | } |
277 | } | |
6a488035 TO |
278 | } |
279 | ||
280 | /** | |
3d469574 | 281 | * Enable site-wide logging. |
6a488035 | 282 | */ |
00be9182 | 283 | public function enableLogging() { |
6a488035 TO |
284 | $this->fixSchemaDifferences(TRUE); |
285 | $this->addReports(); | |
286 | } | |
287 | ||
288 | /** | |
289 | * Sync log tables and rebuild triggers. | |
290 | * | |
353ffa53 | 291 | * @param bool $enableLogging : Ensure logging is enabled |
6a488035 | 292 | */ |
00be9182 | 293 | public function fixSchemaDifferences($enableLogging = FALSE) { |
6a488035 TO |
294 | $config = CRM_Core_Config::singleton(); |
295 | if ($enableLogging) { | |
296 | $config->logging = TRUE; | |
297 | } | |
298 | if ($config->logging) { | |
1b3947b3 | 299 | $this->fixSchemaDifferencesForAll(); |
6a488035 TO |
300 | } |
301 | // invoke the meta trigger creation call | |
e53944ef | 302 | CRM_Core_DAO::triggerRebuild(NULL, TRUE); |
6a488035 TO |
303 | } |
304 | ||
d7ea7150 | 305 | /** |
306 | * Update log tables structure. | |
307 | * | |
308 | * This function updates log tables to have the log_conn_id type of varchar | |
8d369f66 | 309 | * and also implements the engine change defined by the hook (i.e. INNODB). |
d7ea7150 | 310 | * |
311 | * Note changing engine & adding hook-defined indexes, but not changing back | |
204aa6fb PF |
312 | * to INNODB if engine has not been deliberately set (by hook) and not |
313 | * dropping indexes. Sysadmin will need to manually intervene to revert to | |
314 | * defaults. | |
dd05020d MWMC |
315 | * |
316 | * @param array $params | |
e26df891 PF |
317 | * 'updateChangedEngineConfig' - update if the engine config changes? |
318 | * 'forceEngineMigration' - force engine upgrade from ARCHIVE to InnoDB? | |
dd05020d MWMC |
319 | * |
320 | * @return int $updateTablesCount | |
204aa6fb | 321 | * @throws \CiviCRM_API3_Exception |
d7ea7150 | 322 | */ |
e26df891 | 323 | public function updateLogTableSchema($params) { |
d7ea7150 | 324 | $updateLogConn = FALSE; |
dd05020d | 325 | $updatedTablesCount = 0; |
d7ea7150 | 326 | foreach ($this->logs as $mainTable => $logTable) { |
be2fb01f | 327 | $alterSql = []; |
d7ea7150 | 328 | $tableSpec = $this->logTableSpec[$mainTable]; |
204aa6fb PF |
329 | $currentEngine = strtoupper($this->getEngineForLogTable($logTable)); |
330 | if (!isset($tableSpec['engine']) && $currentEngine == 'ARCHIVE' && $params['forceEngineMigration']) { | |
331 | // table uses ARCHIVE engine (the previous default) and no one set an | |
332 | // alternative engine via hook_civicrm_alterLogTables => force change to | |
333 | // new default | |
334 | $tableSpec['engine'] = self::ENGINE; | |
335 | } | |
336 | $engineChanged = isset($tableSpec['engine']) && (strtoupper($tableSpec['engine']) != $currentEngine); | |
dd05020d MWMC |
337 | $engineConfigChanged = isset($tableSpec['engine_config']) && (strtoupper($tableSpec['engine_config']) != $this->getEngineConfigForLogTable($logTable)); |
338 | if ($engineChanged || ($engineConfigChanged && $params['updateChangedEngineConfig'])) { | |
d7ea7150 | 339 | $alterSql[] = "ENGINE=" . $tableSpec['engine'] . " " . CRM_Utils_Array::value('engine_config', $tableSpec); |
8d369f66 PF |
340 | } |
341 | if (!empty($tableSpec['indexes'])) { | |
342 | $indexes = $this->getIndexesForTable($logTable); | |
343 | foreach ($tableSpec['indexes'] as $indexName => $indexSpec) { | |
344 | if (!in_array($indexName, $indexes)) { | |
345 | if (is_array($indexSpec)) { | |
346 | $indexSpec = implode(" , ", $indexSpec); | |
d7ea7150 | 347 | } |
8d369f66 | 348 | $alterSql[] = "ADD INDEX {$indexName}($indexSpec)"; |
d7ea7150 | 349 | } |
350 | } | |
351 | } | |
352 | $columns = $this->columnSpecsOf($logTable); | |
353 | if (empty($columns['log_conn_id'])) { | |
354 | throw new Exception($logTable . print_r($columns, TRUE)); | |
355 | } | |
356 | if ($columns['log_conn_id']['DATA_TYPE'] != 'varchar' || $columns['log_conn_id']['LENGTH'] != 17) { | |
357 | $alterSql[] = "MODIFY log_conn_id VARCHAR(17)"; | |
358 | $updateLogConn = TRUE; | |
359 | } | |
360 | if (!empty($alterSql)) { | |
e714094a | 361 | CRM_Core_DAO::executeQuery("ALTER TABLE {$this->db}.{$logTable} " . implode(', ', $alterSql), [], TRUE, NULL, FALSE, FALSE); |
dd05020d | 362 | $updatedTablesCount++; |
d7ea7150 | 363 | } |
364 | } | |
365 | if ($updateLogConn) { | |
be2fb01f | 366 | civicrm_api3('Setting', 'create', ['logging_uniqueid_date' => date('Y-m-d H:i:s')]); |
d7ea7150 | 367 | } |
dd05020d | 368 | return $updatedTablesCount; |
d7ea7150 | 369 | } |
370 | ||
371 | /** | |
372 | * Get the engine for the given table. | |
373 | * | |
374 | * @param string $table | |
375 | * | |
376 | * @return string | |
377 | */ | |
378 | public function getEngineForLogTable($table) { | |
379 | return strtoupper(CRM_Core_DAO::singleValueQuery(" | |
380 | SELECT ENGINE FROM information_schema.tables WHERE TABLE_NAME = %1 | |
381 | AND table_schema = %2 | |
be2fb01f | 382 | ", [1 => [$table, 'String'], 2 => [$this->db, 'String']])); |
d7ea7150 | 383 | } |
384 | ||
dd05020d MWMC |
385 | /** |
386 | * Get the engine config for the given table. | |
387 | * | |
388 | * @param string $table | |
389 | * | |
390 | * @return string | |
391 | */ | |
392 | public function getEngineConfigForLogTable($table) { | |
393 | return strtoupper(CRM_Core_DAO::singleValueQuery(" | |
394 | SELECT CREATE_OPTIONS FROM information_schema.tables WHERE TABLE_NAME = %1 | |
395 | AND table_schema = %2 | |
396 | ", [1 => [$table, 'String'], 2 => [$this->db, 'String']])); | |
397 | } | |
398 | ||
d7ea7150 | 399 | /** |
400 | * Get all the indexes in the table. | |
401 | * | |
402 | * @param string $table | |
403 | * | |
404 | * @return array | |
405 | */ | |
406 | public function getIndexesForTable($table) { | |
9c8a3025 PF |
407 | $indexes = []; |
408 | $result = CRM_Core_DAO::executeQuery(" | |
409 | SELECT constraint_name AS index_name | |
410 | FROM information_schema.key_column_usage | |
411 | WHERE table_schema = %2 AND table_name = %1 | |
412 | UNION | |
413 | SELECT index_name AS index_name | |
414 | FROM information_schema.statistics | |
415 | WHERE table_schema = %2 AND table_name = %1 | |
416 | ", | |
be2fb01f | 417 | [1 => [$table, 'String'], 2 => [$this->db, 'String']] |
9c8a3025 PF |
418 | ); |
419 | while ($result->fetch()) { | |
420 | $indexes[] = $result->index_name; | |
421 | } | |
422 | return $indexes; | |
d7ea7150 | 423 | } |
424 | ||
6a488035 TO |
425 | /** |
426 | * Add missing (potentially specified) log table columns for the given table. | |
427 | * | |
5a4f6742 CW |
428 | * @param string $table |
429 | * name of the relevant table. | |
430 | * @param array $cols | |
4c922589 | 431 | * Mixed array of columns to add or null (to check for the missing columns). |
6a488035 | 432 | * |
3d469574 | 433 | * @return bool |
6a488035 | 434 | */ |
fcc20cec | 435 | public function fixSchemaDifferencesFor($table, $cols = []) { |
bfb723bb DS |
436 | if (empty($table)) { |
437 | return FALSE; | |
438 | } | |
6a488035 TO |
439 | if (empty($this->logs[$table])) { |
440 | $this->createLogTableFor($table); | |
bfb723bb | 441 | return TRUE; |
6a488035 TO |
442 | } |
443 | ||
6a488035 | 444 | if (empty($cols)) { |
bfb723bb | 445 | $cols = $this->columnsWithDiffSpecs($table, "log_$table"); |
6a488035 TO |
446 | } |
447 | ||
363c450b CR |
448 | // If a column that already exists on logging table is being added, we |
449 | // should treat it as a modification. | |
450 | $this->resetSchemaCacheForTable("log_$table"); | |
451 | $logTableSchema = $this->columnSpecsOf("log_$table"); | |
b3dd4cb0 | 452 | if (!empty($cols['ADD'])) { |
453 | foreach ($cols['ADD'] as $colKey => $col) { | |
454 | if (array_key_exists($col, $logTableSchema)) { | |
455 | $cols['MODIFY'][] = $col; | |
456 | unset($cols['ADD'][$colKey]); | |
457 | } | |
363c450b CR |
458 | } |
459 | } | |
460 | ||
6a488035 | 461 | // use the relevant lines from CREATE TABLE to add colums to the log table |
bfb723bb | 462 | $create = $this->_getCreateQuery($table); |
be2fb01f | 463 | foreach ((['ADD', 'MODIFY']) as $alterType) { |
12de149a DS |
464 | if (!empty($cols[$alterType])) { |
465 | foreach ($cols[$alterType] as $col) { | |
466 | $line = $this->_getColumnQuery($col, $create); | |
be2fb01f | 467 | CRM_Core_DAO::executeQuery("ALTER TABLE `{$this->db}`.log_$table {$alterType} {$line}", [], TRUE, NULL, FALSE, FALSE); |
12de149a | 468 | } |
bfb723bb DS |
469 | } |
470 | } | |
471 | ||
31c270e1 DS |
472 | // for any obsolete columns (not null) we just make the column nullable. |
473 | if (!empty($cols['OBSOLETE'])) { | |
eae2404c | 474 | $create = $this->_getCreateQuery("`{$this->db}`.log_{$table}"); |
31c270e1 | 475 | foreach ($cols['OBSOLETE'] as $col) { |
bfb723bb | 476 | $line = $this->_getColumnQuery($col, $create); |
31c270e1 | 477 | // This is just going to make a not null column to nullable |
be2fb01f | 478 | CRM_Core_DAO::executeQuery("ALTER TABLE `{$this->db}`.log_$table MODIFY {$line}", [], TRUE, NULL, FALSE, FALSE); |
bfb723bb DS |
479 | } |
480 | } | |
481 | ||
363c450b CR |
482 | $this->resetSchemaCacheForTable("log_$table"); |
483 | ||
bfb723bb DS |
484 | return TRUE; |
485 | } | |
486 | ||
363c450b CR |
487 | /** |
488 | * Resets schema cache for the given table. | |
489 | * | |
490 | * @param string $table | |
491 | * Name of the table. | |
492 | */ | |
493 | private function resetSchemaCacheForTable($table) { | |
494 | unset(\Civi::$statics[__CLASS__]['columnSpecs'][$table]); | |
495 | } | |
496 | ||
e0ef6999 | 497 | /** |
3d469574 | 498 | * Get query table. |
499 | * | |
500 | * @param string $table | |
e0ef6999 EM |
501 | * |
502 | * @return array | |
503 | */ | |
bfb723bb | 504 | private function _getCreateQuery($table) { |
be2fb01f | 505 | $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE {$table}", [], TRUE, NULL, FALSE, FALSE); |
6a488035 TO |
506 | $dao->fetch(); |
507 | $create = explode("\n", $dao->Create_Table); | |
bfb723bb DS |
508 | return $create; |
509 | } | |
6a488035 | 510 | |
e0ef6999 | 511 | /** |
3d469574 | 512 | * Get column query. |
513 | * | |
514 | * @param string $col | |
515 | * @param bool $createQuery | |
e0ef6999 EM |
516 | * |
517 | * @return array|mixed|string | |
518 | */ | |
bfb723bb DS |
519 | private function _getColumnQuery($col, $createQuery) { |
520 | $line = preg_grep("/^ `$col` /", $createQuery); | |
4a423b7a | 521 | $line = rtrim(array_pop($line), ','); |
bfb723bb | 522 | // CRM-11179 |
91bd5c14 | 523 | $line = self::fixTimeStampAndNotNullSQL($line); |
bfb723bb DS |
524 | return $line; |
525 | } | |
526 | ||
e0ef6999 | 527 | /** |
3d469574 | 528 | * Fix schema differences. |
e0ef6999 | 529 | */ |
753bc831 | 530 | public function fixSchemaDifferencesForAll(): void { |
be2fb01f | 531 | $diffs = []; |
a40a5a48 CR |
532 | $this->resetTableColumnsCache(); |
533 | ||
bfb723bb DS |
534 | foreach ($this->tables as $table) { |
535 | if (empty($this->logs[$table])) { | |
536 | $this->createLogTableFor($table); | |
e53944ef BS |
537 | } |
538 | else { | |
bfb723bb DS |
539 | $diffs[$table] = $this->columnsWithDiffSpecs($table, "log_$table"); |
540 | } | |
541 | } | |
6a488035 | 542 | |
bfb723bb | 543 | foreach ($diffs as $table => $cols) { |
fcc20cec | 544 | $this->fixSchemaDifferencesFor($table, $cols); |
6a488035 | 545 | } |
6a488035 TO |
546 | } |
547 | ||
a40a5a48 CR |
548 | /** |
549 | * Resets columnSpecs. | |
550 | * | |
551 | * Resets columnSpecs static array in Civi's $statics to make sure we use the | |
552 | * real state of the schema to perform sync operations between core and | |
553 | * logging tables. | |
554 | */ | |
555 | private function resetTableColumnsCache() { | |
556 | unset(\Civi::$statics[__CLASS__]['columnSpecs']); | |
557 | } | |
558 | ||
d424ffde | 559 | /** |
3d469574 | 560 | * Fix timestamp. |
561 | * | |
d424ffde | 562 | * Log_civicrm_contact.modified_date for example would always be copied from civicrm_contact.modified_date, |
bfb723bb DS |
563 | * so there's no need for a default timestamp and therefore we remove such default timestamps |
564 | * also eliminate the NOT NULL constraint, since we always copy and schema can change down the road) | |
d424ffde | 565 | * |
3d469574 | 566 | * @param string $query |
e0ef6999 EM |
567 | * |
568 | * @return mixed | |
569 | */ | |
91bd5c14 SL |
570 | public static function fixTimeStampAndNotNullSQL($query) { |
571 | $query = str_ireplace("TIMESTAMP() NOT NULL", "TIMESTAMP NULL", $query); | |
6a488035 | 572 | $query = str_ireplace("TIMESTAMP NOT NULL", "TIMESTAMP NULL", $query); |
91bd5c14 | 573 | $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP()", '', $query); |
6a488035 | 574 | $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", '', $query); |
91bd5c14 | 575 | $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP()", '', $query); |
6a488035 | 576 | $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP", '', $query); |
c28241be | 577 | $query = str_ireplace("NOT NULL", '', $query); |
6a488035 TO |
578 | return $query; |
579 | } | |
580 | ||
3d469574 | 581 | /** |
582 | * Add reports. | |
583 | */ | |
6a488035 | 584 | private function addReports() { |
be2fb01f | 585 | $titles = [ |
6a488035 TO |
586 | 'logging/contact/detail' => ts('Logging Details'), |
587 | 'logging/contact/summary' => ts('Contact Logging Report (Summary)'), | |
588 | 'logging/contribute/detail' => ts('Contribution Logging Report (Detail)'), | |
589 | 'logging/contribute/summary' => ts('Contribution Logging Report (Summary)'), | |
be2fb01f | 590 | ]; |
6a488035 TO |
591 | // enable logging templates |
592 | CRM_Core_DAO::executeQuery(" | |
593 | UPDATE civicrm_option_value | |
594 | SET is_active = 1 | |
595 | WHERE value IN ('" . implode("', '", $this->reports) . "') | |
596 | "); | |
597 | ||
598 | // add report instances | |
599 | $domain_id = CRM_Core_Config::domainID(); | |
600 | foreach ($this->reports as $report) { | |
ae5ffbb7 | 601 | $dao = new CRM_Report_DAO_ReportInstance(); |
353ffa53 TO |
602 | $dao->domain_id = $domain_id; |
603 | $dao->report_id = $report; | |
604 | $dao->title = $titles[$report]; | |
6a488035 | 605 | $dao->permission = 'administer CiviCRM'; |
ae5ffbb7 | 606 | if ($report == 'logging/contact/summary') { |
6a488035 | 607 | $dao->is_reserved = 1; |
ae5ffbb7 | 608 | } |
6a488035 TO |
609 | $dao->insert(); |
610 | } | |
611 | } | |
612 | ||
613 | /** | |
614 | * Get an array of column names of the given table. | |
ea3ddccf | 615 | * |
3d469574 | 616 | * @param string $table |
ea3ddccf | 617 | * @param bool $force |
618 | * | |
619 | * @return array | |
6a488035 | 620 | */ |
e53944ef | 621 | private function columnsOf($table, $force = FALSE) { |
003b4269 | 622 | if ($force || !isset(\Civi::$statics[__CLASS__]['columnsOf'][$table])) { |
623 | $from = (substr($table, 0, 4) == 'log_') ? "`{$this->db}`.$table" : $table; | |
e03e1641 | 624 | $dao = CRM_Core_DAO::executeQuery("SHOW COLUMNS FROM $from", [], TRUE, NULL, FALSE, FALSE); |
6a488035 | 625 | if (is_a($dao, 'DB_Error')) { |
be2fb01f | 626 | return []; |
6a488035 | 627 | } |
be2fb01f | 628 | \Civi::$statics[__CLASS__]['columnsOf'][$table] = []; |
6a488035 | 629 | while ($dao->fetch()) { |
c33f1df1 | 630 | \Civi::$statics[__CLASS__]['columnsOf'][$table][] = CRM_Utils_Type::escape($dao->Field, 'MysqlColumnNameOrAlias'); |
6a488035 TO |
631 | } |
632 | } | |
003b4269 | 633 | return \Civi::$statics[__CLASS__]['columnsOf'][$table]; |
6a488035 TO |
634 | } |
635 | ||
bfb723bb DS |
636 | /** |
637 | * Get an array of columns and their details like DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT for the given table. | |
ea3ddccf | 638 | * |
639 | * @param string $table | |
640 | * | |
641 | * @return array | |
bfb723bb DS |
642 | */ |
643 | private function columnSpecsOf($table) { | |
87a52027 | 644 | static $civiDB = NULL; |
645 | if (empty(\Civi::$statics[__CLASS__]['columnSpecs'])) { | |
be2fb01f | 646 | \Civi::$statics[__CLASS__]['columnSpecs'] = []; |
87a52027 | 647 | } |
648 | if (empty(\Civi::$statics[__CLASS__]['columnSpecs']) || !isset(\Civi::$statics[__CLASS__]['columnSpecs'][$table])) { | |
bfb723bb DS |
649 | if (!$civiDB) { |
650 | $dao = new CRM_Contact_DAO_Contact(); | |
651 | $civiDB = $dao->_database; | |
652 | } | |
3c155656 | 653 | |
6842bb53 | 654 | // NOTE: W.r.t Performance using one query to find all details and storing in static array is much faster |
bfb723bb DS |
655 | // than firing query for every given table. |
656 | $query = " | |
16142ce2 | 657 | SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_TYPE, EXTRA |
bfb723bb DS |
658 | FROM INFORMATION_SCHEMA.COLUMNS |
659 | WHERE table_schema IN ('{$this->db}', '{$civiDB}')"; | |
bfb723bb | 660 | $dao = CRM_Core_DAO::executeQuery($query); |
bfb723bb | 661 | if (is_a($dao, 'DB_Error')) { |
be2fb01f | 662 | return []; |
bfb723bb DS |
663 | } |
664 | while ($dao->fetch()) { | |
87a52027 | 665 | if (!array_key_exists($dao->TABLE_NAME, \Civi::$statics[__CLASS__]['columnSpecs'])) { |
be2fb01f | 666 | \Civi::$statics[__CLASS__]['columnSpecs'][$dao->TABLE_NAME] = []; |
bfb723bb | 667 | } |
be2fb01f | 668 | \Civi::$statics[__CLASS__]['columnSpecs'][$dao->TABLE_NAME][$dao->COLUMN_NAME] = [ |
ae5ffbb7 TO |
669 | 'COLUMN_NAME' => $dao->COLUMN_NAME, |
670 | 'DATA_TYPE' => $dao->DATA_TYPE, | |
671 | 'IS_NULLABLE' => $dao->IS_NULLABLE, | |
672 | 'COLUMN_DEFAULT' => $dao->COLUMN_DEFAULT, | |
16142ce2 | 673 | 'EXTRA' => $dao->EXTRA, |
be2fb01f | 674 | ]; |
cf0bd1e1 | 675 | if (($first = strpos($dao->COLUMN_TYPE, '(')) != 0) { |
f34f504a PF |
676 | // this extracts the value between parentheses after the column type. |
677 | // it could be the column length, i.e. "int(8)", "decimal(20,2)") | |
678 | // or the permitted values of an enum (e.g. "enum('A','B')") | |
679 | $parValue = substr( | |
9c8a3025 | 680 | $dao->COLUMN_TYPE, $first + 1, strpos($dao->COLUMN_TYPE, ')') - $first - 1 |
87a52027 | 681 | ); |
f34f504a PF |
682 | if (strpos($parValue, "'") === FALSE) { |
683 | // no quote in value means column length | |
684 | \Civi::$statics[__CLASS__]['columnSpecs'][$dao->TABLE_NAME][$dao->COLUMN_NAME]['LENGTH'] = $parValue; | |
685 | } | |
686 | else { | |
687 | // single quote means enum permitted values | |
688 | \Civi::$statics[__CLASS__]['columnSpecs'][$dao->TABLE_NAME][$dao->COLUMN_NAME]['ENUM_VALUES'] = $parValue; | |
689 | } | |
cf0bd1e1 | 690 | } |
bfb723bb | 691 | } |
bfb723bb | 692 | } |
87a52027 | 693 | return \Civi::$statics[__CLASS__]['columnSpecs'][$table]; |
bfb723bb DS |
694 | } |
695 | ||
e0ef6999 | 696 | /** |
3d469574 | 697 | * Get columns that have changed. |
698 | * | |
699 | * @param string $civiTable | |
700 | * @param string $logTable | |
e0ef6999 EM |
701 | * |
702 | * @return array | |
703 | */ | |
00be9182 | 704 | public function columnsWithDiffSpecs($civiTable, $logTable) { |
4a423b7a | 705 | $civiTableSpecs = $this->columnSpecsOf($civiTable); |
353ffa53 | 706 | $logTableSpecs = $this->columnSpecsOf($logTable); |
6842bb53 | 707 | |
be2fb01f | 708 | $diff = ['ADD' => [], 'MODIFY' => [], 'OBSOLETE' => []]; |
585a6418 JK |
709 | |
710 | // Columns to be added | |
4a423b7a | 711 | $diff['ADD'] = array_diff(array_keys($civiTableSpecs), array_keys($logTableSpecs)); |
585a6418 JK |
712 | |
713 | // Columns to be modified | |
714 | // Only pick columns where there is a spec change and the column definition was not deliberately modified by | |
715 | // fixTimeStampAndNotNullSQL() method, also accounting for differences in db version. | |
4a423b7a | 716 | foreach ($civiTableSpecs as $col => $colSpecs) { |
481a74f4 | 717 | if (!isset($logTableSpecs[$col]) || !is_array($logTableSpecs[$col])) { |
be2fb01f | 718 | $logTableSpecs[$col] = []; |
e53944ef | 719 | } |
cd71572a | 720 | $specDiff = array_diff($civiTableSpecs[$col], $logTableSpecs[$col]); |
235592e0 | 721 | if (!empty($specDiff) && $col !== 'id' && !in_array($col, $diff['ADD'])) { |
16142ce2 SL |
722 | if (empty($colSpecs['EXTRA']) || (!empty($colSpecs['EXTRA']) && $colSpecs['EXTRA'] !== 'auto_increment')) { |
723 | // ignore 'id' column for any spec changes, to avoid any auto-increment mysql errors | |
724 | if ($civiTableSpecs[$col]['DATA_TYPE'] != CRM_Utils_Array::value('DATA_TYPE', $logTableSpecs[$col]) | |
4c8d6fd2 JK |
725 | // We won't alter the log if the length is decreased in case some of the existing data won't fit. |
726 | || CRM_Utils_Array::value('LENGTH', $civiTableSpecs[$col]) > CRM_Utils_Array::value('LENGTH', $logTableSpecs[$col]) | |
16142ce2 SL |
727 | ) { |
728 | // if data-type is different, surely consider the column | |
729 | $diff['MODIFY'][] = $col; | |
730 | } | |
235592e0 | 731 | elseif ($civiTableSpecs[$col]['DATA_TYPE'] === 'enum' && |
f34f504a PF |
732 | CRM_Utils_Array::value('ENUM_VALUES', $civiTableSpecs[$col]) != CRM_Utils_Array::value('ENUM_VALUES', $logTableSpecs[$col]) |
733 | ) { | |
734 | // column is enum and the permitted values have changed | |
735 | $diff['MODIFY'][] = $col; | |
736 | } | |
16142ce2 | 737 | elseif ($civiTableSpecs[$col]['IS_NULLABLE'] != CRM_Utils_Array::value('IS_NULLABLE', $logTableSpecs[$col]) && |
235592e0 | 738 | $logTableSpecs[$col]['IS_NULLABLE'] === 'NO' |
16142ce2 SL |
739 | ) { |
740 | // if is-null property is different, and log table's column is NOT-NULL, surely consider the column | |
741 | $diff['MODIFY'][] = $col; | |
742 | } | |
585a6418 JK |
743 | elseif ( |
744 | $civiTableSpecs[$col]['COLUMN_DEFAULT'] != ($logTableSpecs[$col]['COLUMN_DEFAULT'] ?? NULL) | |
745 | && !stristr($civiTableSpecs[$col]['COLUMN_DEFAULT'], 'timestamp') | |
746 | && !($civiTableSpecs[$col]['COLUMN_DEFAULT'] === NULL && ($logTableSpecs[$col]['COLUMN_DEFAULT'] ?? NULL) === 'NULL') | |
16142ce2 SL |
747 | ) { |
748 | // if default property is different, and its not about a timestamp column, consider it | |
749 | $diff['MODIFY'][] = $col; | |
750 | } | |
4a423b7a | 751 | } |
6842bb53 | 752 | } |
bfb723bb DS |
753 | } |
754 | ||
4a423b7a DS |
755 | // columns to made obsolete by turning into not-null |
756 | $oldCols = array_diff(array_keys($logTableSpecs), array_keys($civiTableSpecs)); | |
31c270e1 | 757 | foreach ($oldCols as $col) { |
be2fb01f | 758 | if (!in_array($col, ['log_date', 'log_conn_id', 'log_user_id', 'log_action']) && |
235592e0 | 759 | $logTableSpecs[$col]['IS_NULLABLE'] === 'NO' |
760 | // This could be to support replication - https://lab.civicrm.org/dev/core/-/issues/2120 | |
761 | && $logTableSpecs[$col]['EXTRA'] !== 'auto_increment' | |
353ffa53 | 762 | ) { |
4a423b7a | 763 | // if its a column present only in log table, not among those used by log tables for special purpose, and not-null |
31c270e1 | 764 | $diff['OBSOLETE'][] = $col; |
bfb723bb DS |
765 | } |
766 | } | |
767 | ||
768 | return $diff; | |
769 | } | |
770 | ||
d7ea7150 | 771 | /** |
772 | * Getter for logTableSpec. | |
773 | * | |
774 | * @return array | |
775 | */ | |
776 | public function getLogTableSpec() { | |
777 | return $this->logTableSpec; | |
778 | } | |
779 | ||
6a488035 TO |
780 | /** |
781 | * Create a log table with schema mirroring the given table’s structure and seeding it with the given table’s contents. | |
ea3ddccf | 782 | * |
783 | * @param string $table | |
6a488035 TO |
784 | */ |
785 | private function createLogTableFor($table) { | |
e03e1641 | 786 | $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE $table", [], TRUE, NULL, FALSE, FALSE); |
6a488035 TO |
787 | $dao->fetch(); |
788 | $query = $dao->Create_Table; | |
789 | ||
790 | // rewrite the queries into CREATE TABLE queries for log tables: | |
6a488035 | 791 | $cols = <<<COLS |
1f4627e9 | 792 | , |
848cfde7 | 793 | log_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
8dd883ca | 794 | log_conn_id VARCHAR(17), |
6a488035 TO |
795 | log_user_id INTEGER, |
796 | log_action ENUM('Initialization', 'Insert', 'Update', 'Delete') | |
797 | COLS; | |
c28241be | 798 | |
ef587f9c | 799 | if (!empty($this->logTableSpec[$table]['indexes'])) { |
800 | foreach ($this->logTableSpec[$table]['indexes'] as $indexName => $indexSpec) { | |
801 | if (is_array($indexSpec)) { | |
802 | $indexSpec = implode(" , ", $indexSpec); | |
803 | } | |
804 | $cols .= ", INDEX {$indexName}($indexSpec)"; | |
805 | } | |
806 | } | |
807 | ||
c28241be DL |
808 | // - prepend the name with log_ |
809 | // - drop AUTO_INCREMENT columns | |
810 | // - drop non-column rows of the query (keys, constraints, etc.) | |
204aa6fb | 811 | // - set the ENGINE to the specified engine (default is INNODB) |
c28241be | 812 | // - add log-specific columns (at the end of the table) |
6a488035 TO |
813 | $query = preg_replace("/^CREATE TABLE `$table`/i", "CREATE TABLE `{$this->db}`.log_$table", $query); |
814 | $query = preg_replace("/ AUTO_INCREMENT/i", '', $query); | |
815 | $query = preg_replace("/^ [^`].*$/m", '', $query); | |
c3eff32d | 816 | $engine = strtoupper(empty($this->logTableSpec[$table]['engine']) ? self::ENGINE : $this->logTableSpec[$table]['engine']); |
817 | $engine .= " " . ($this->logTableSpec[$table]['engine_config'] ?? ''); | |
828c6915 SL |
818 | if (strpos($engine, 'ROW_FORMAT') !== FALSE) { |
819 | $query = preg_replace("/ROW_FORMAT=\w+/m", '', $query); | |
820 | } | |
ef587f9c | 821 | $query = preg_replace("/^\) ENGINE=[^ ]+ /im", ') ENGINE=' . $engine . ' ', $query); |
c28241be | 822 | |
6a488035 | 823 | // log_civicrm_contact.modified_date for example would always be copied from civicrm_contact.modified_date, |
c28241be DL |
824 | // so there's no need for a default timestamp and therefore we remove such default timestamps |
825 | // also eliminate the NOT NULL constraint, since we always copy and schema can change down the road) | |
826 | $query = self::fixTimeStampAndNotNullSQL($query); | |
1f4627e9 | 827 | $query = preg_replace("/(,*\n*\) )ENGINE/m", "$cols\n) ENGINE", $query); |
6a488035 | 828 | |
e03e1641 | 829 | CRM_Core_DAO::executeQuery($query, [], TRUE, NULL, FALSE, FALSE); |
6a488035 TO |
830 | |
831 | $columns = implode(', ', $this->columnsOf($table)); | |
e03e1641 | 832 | CRM_Core_DAO::executeQuery("INSERT INTO `{$this->db}`.log_$table ($columns, log_conn_id, log_user_id, log_action) SELECT $columns, @uniqueID, @civicrm_user_id, 'Initialization' FROM {$table}", [], TRUE, NULL, FALSE, FALSE); |
6a488035 TO |
833 | |
834 | $this->tables[] = $table; | |
d7ea7150 | 835 | if (empty($this->logs)) { |
be2fb01f CW |
836 | civicrm_api3('Setting', 'create', ['logging_uniqueid_date' => date('Y-m-d H:i:s')]); |
837 | civicrm_api3('Setting', 'create', ['logging_all_tables_uniquid' => 1]); | |
8dd883ca | 838 | } |
6a488035 TO |
839 | $this->logs[$table] = "log_$table"; |
840 | } | |
841 | ||
3d469574 | 842 | /** |
843 | * Delete reports. | |
844 | */ | |
6a488035 TO |
845 | private function deleteReports() { |
846 | // disable logging templates | |
847 | CRM_Core_DAO::executeQuery(" | |
848 | UPDATE civicrm_option_value | |
849 | SET is_active = 0 | |
850 | WHERE value IN ('" . implode("', '", $this->reports) . "') | |
851 | "); | |
852 | ||
853 | // delete report instances | |
854 | $domain_id = CRM_Core_Config::domainID(); | |
855 | foreach ($this->reports as $report) { | |
ae5ffbb7 | 856 | $dao = new CRM_Report_DAO_ReportInstance(); |
6a488035 TO |
857 | $dao->domain_id = $domain_id; |
858 | $dao->report_id = $report; | |
859 | $dao->delete(); | |
860 | } | |
861 | } | |
862 | ||
863 | /** | |
864 | * Predicate whether logging is enabled. | |
865 | */ | |
866 | public function isEnabled() { | |
509e50b7 | 867 | if (\Civi::settings()->get('logging')) { |
868 | return ($this->tablesExist() && (\Civi::settings()->get('logging_no_trigger_permission') || $this->triggersExist())); | |
6a488035 TO |
869 | } |
870 | return FALSE; | |
871 | } | |
872 | ||
873 | /** | |
874 | * Predicate whether any log tables exist. | |
875 | */ | |
876 | private function tablesExist() { | |
877 | return !empty($this->logs); | |
878 | } | |
879 | ||
1f43f9e2 | 880 | /** |
881 | * Drop all log tables. | |
882 | * | |
883 | * This does not currently have a usage outside the tests. | |
884 | */ | |
885 | public function dropAllLogTables() { | |
886 | if ($this->tablesExist()) { | |
887 | foreach ($this->logs as $log_table) { | |
888 | CRM_Core_DAO::executeQuery("DROP TABLE $log_table"); | |
889 | } | |
890 | } | |
891 | } | |
892 | ||
f8d28563 | 893 | /** |
894 | * Get an sql clause to find the names of any log tables that do not match the normal pattern. | |
895 | * | |
896 | * Most tables are civicrm_xxx with the log table being log_civicrm_xxx | |
897 | * However, they don't have to match this pattern (e.g when defined by hook) so find the | |
898 | * anomalies and return a filter string to include them. | |
899 | * | |
900 | * @return string | |
901 | */ | |
902 | public function getNonStandardTableNameFilterString() { | |
903 | $nonStandardTableNames = preg_grep('/^civicrm_/', $this->tables, PREG_GREP_INVERT); | |
904 | if (empty($nonStandardTableNames)) { | |
905 | return ''; | |
906 | } | |
be2fb01f | 907 | $nonStandardTableLogs = []; |
f8d28563 | 908 | foreach ($nonStandardTableNames as $nonStandardTableName) { |
909 | $nonStandardTableLogs[] = "'log_{$nonStandardTableName}'"; | |
910 | } | |
911 | return " OR TABLE_NAME IN (" . implode(',', $nonStandardTableLogs) . ")"; | |
912 | } | |
913 | ||
6a488035 TO |
914 | /** |
915 | * Predicate whether the logging triggers are in place. | |
916 | */ | |
917 | private function triggersExist() { | |
918 | // FIXME: probably should be a bit more thorough… | |
919 | // note that the LIKE parameter is TABLE NAME | |
920 | return (bool) CRM_Core_DAO::singleValueQuery("SHOW TRIGGERS LIKE 'civicrm_contact'"); | |
921 | } | |
922 | ||
e0ef6999 | 923 | /** |
3d469574 | 924 | * Get trigger info. |
925 | * | |
926 | * @param array $info | |
e0ef6999 EM |
927 | * @param null $tableName |
928 | * @param bool $force | |
929 | */ | |
00be9182 | 930 | public function triggerInfo(&$info, $tableName = NULL, $force = FALSE) { |
74b348da | 931 | if (!CRM_Core_Config::singleton()->logging) { |
6a488035 TO |
932 | return; |
933 | } | |
934 | ||
be2fb01f CW |
935 | $insert = ['INSERT']; |
936 | $update = ['UPDATE']; | |
937 | $delete = ['DELETE']; | |
6a488035 TO |
938 | |
939 | if ($tableName) { | |
be2fb01f | 940 | $tableNames = [$tableName]; |
6a488035 TO |
941 | } |
942 | else { | |
943 | $tableNames = $this->tables; | |
944 | } | |
945 | ||
946 | // logging is enabled, so now lets create the trigger info tables | |
947 | foreach ($tableNames as $table) { | |
89d7d62e | 948 | if (!isset($this->logTableSpec[$table])) { |
949 | // Per testIgnoreCustomTableByHook this would be unset if a hook had | |
950 | // intervened to prevent logging / triggers on this table. | |
951 | // This could go to the extent of blocking the updates to 'modified_date' | |
952 | // which makes sense, in particular, for calculated fields. | |
953 | continue; | |
954 | } | |
e53944ef | 955 | $columns = $this->columnsOf($table, $force); |
6a488035 TO |
956 | |
957 | // only do the change if any data has changed | |
be2fb01f | 958 | $cond = []; |
6a488035 | 959 | foreach ($columns as $column) { |
be2fb01f | 960 | $tableExceptions = array_key_exists('exceptions', $this->logTableSpec[$table]) ? $this->logTableSpec[$table]['exceptions'] : []; |
6a488035 | 961 | // ignore modified_date changes |
9c3c84d1 PF |
962 | $tableExceptions[] = 'modified_date'; |
963 | // exceptions may be provided with or without backticks | |
964 | $excludeColumn = in_array($column, $tableExceptions) || | |
965 | in_array(str_replace('`', '', $column), $tableExceptions); | |
966 | if (!$excludeColumn) { | |
49d2e8ee | 967 | $cond[] = "IFNULL(OLD.$column,'') <> IFNULL(NEW.$column,'')"; |
6a488035 TO |
968 | } |
969 | } | |
970 | $suppressLoggingCond = "@civicrm_disable_logging IS NULL OR @civicrm_disable_logging = 0"; | |
481a74f4 | 971 | $updateSQL = "IF ( (" . implode(' OR ', $cond) . ") AND ( $suppressLoggingCond ) ) THEN "; |
6a488035 | 972 | |
340f6aa0 DL |
973 | if ($this->useDBPrefix) { |
974 | $sqlStmt = "INSERT INTO `{$this->db}`.log_{tableName} ("; | |
975 | } | |
976 | else { | |
977 | $sqlStmt = "INSERT INTO log_{tableName} ("; | |
978 | } | |
6a488035 TO |
979 | foreach ($columns as $column) { |
980 | $sqlStmt .= "$column, "; | |
981 | } | |
982 | $sqlStmt .= "log_conn_id, log_user_id, log_action) VALUES ("; | |
983 | ||
984 | $insertSQL = $deleteSQL = "IF ( $suppressLoggingCond ) THEN $sqlStmt "; | |
985 | $updateSQL .= $sqlStmt; | |
986 | ||
987 | $sqlStmt = ''; | |
988 | foreach ($columns as $column) { | |
353ffa53 | 989 | $sqlStmt .= "NEW.$column, "; |
6a488035 TO |
990 | $deleteSQL .= "OLD.$column, "; |
991 | } | |
be2fb01f | 992 | if (civicrm_api3('Setting', 'getvalue', ['name' => 'logging_uniqueid_date'])) { |
8d2c1ae5 E |
993 | // Note that when connecting directly via mysql @uniqueID may not be set so a fallback is |
994 | // 'c_' to identify a non-CRM connection + timestamp to the hour + connection_id | |
995 | // If the connection_id is longer than 6 chars it will be truncated. | |
996 | // We tried setting the @uniqueID in the trigger but it was unreliable. | |
997 | // An external interaction could split over 2 connections & it seems worth blocking the revert on | |
998 | // these reports & adding extra permissioning to the api for this. | |
08a50e27 | 999 | $connectionSQLString = "COALESCE(@uniqueID, LEFT(CONCAT('c_', unix_timestamp()/3600, CONNECTION_ID()), 17))"; |
8dd883ca | 1000 | } |
1001 | else { | |
1002 | // The log tables have not yet been converted to have varchar(17) fields for log_conn_id. | |
1003 | // Continue to use the less reliable connection_id for al tables for now. | |
8d2c1ae5 | 1004 | $connectionSQLString = "CONNECTION_ID()"; |
8dd883ca | 1005 | } |
8d2c1ae5 E |
1006 | $sqlStmt .= $connectionSQLString . ", @civicrm_user_id, '{eventName}'); END IF;"; |
1007 | $deleteSQL .= $connectionSQLString . ", @civicrm_user_id, '{eventName}'); END IF;"; | |
6a488035 TO |
1008 | |
1009 | $insertSQL .= $sqlStmt; | |
1010 | $updateSQL .= $sqlStmt; | |
1011 | ||
be2fb01f CW |
1012 | $info[] = [ |
1013 | 'table' => [$table], | |
6a488035 TO |
1014 | 'when' => 'AFTER', |
1015 | 'event' => $insert, | |
1016 | 'sql' => $insertSQL, | |
be2fb01f | 1017 | ]; |
6a488035 | 1018 | |
be2fb01f CW |
1019 | $info[] = [ |
1020 | 'table' => [$table], | |
6a488035 TO |
1021 | 'when' => 'AFTER', |
1022 | 'event' => $update, | |
1023 | 'sql' => $updateSQL, | |
be2fb01f | 1024 | ]; |
6a488035 | 1025 | |
be2fb01f CW |
1026 | $info[] = [ |
1027 | 'table' => [$table], | |
6a488035 TO |
1028 | 'when' => 'AFTER', |
1029 | 'event' => $delete, | |
1030 | 'sql' => $deleteSQL, | |
be2fb01f | 1031 | ]; |
6a488035 TO |
1032 | } |
1033 | } | |
1034 | ||
1035 | /** | |
3d469574 | 1036 | * Disable logging temporarily. |
1037 | * | |
6a488035 | 1038 | * This allow logging to be temporarily disabled for certain cases |
74b348da | 1039 | * where we want to do a mass cleanup but do not want to bother with |
1040 | * an audit trail. | |
6a488035 | 1041 | */ |
481a74f4 | 1042 | public static function disableLoggingForThisConnection() { |
74b348da | 1043 | if (CRM_Core_Config::singleton()->logging) { |
481a74f4 | 1044 | CRM_Core_DAO::executeQuery('SET @civicrm_disable_logging = 1'); |
6a488035 TO |
1045 | } |
1046 | } | |
1047 | ||
93afbc3a | 1048 | /** |
1049 | * Get all the log tables that reference civicrm_contact. | |
1050 | * | |
1051 | * Note that it might make sense to wrap this in a getLogTablesForEntity | |
1052 | * but this is the only entity currently available... | |
1053 | */ | |
1054 | public function getLogTablesForContact() { | |
e3e87c73 | 1055 | $tables = array_keys(CRM_Core_DAO::getReferencesToContactTable()); |
eca28463 | 1056 | // This additional hardcoding has been moved from getReferencesToContactTable |
1057 | // to here as it is not needed in the other place where the function is called. | |
1058 | // It may not be needed here either... | |
1059 | $tables[] = 'civicrm_entity_tag'; | |
93afbc3a | 1060 | return array_intersect($tables, $this->tables); |
1061 | } | |
1062 | ||
74044663 JP |
1063 | /** |
1064 | * Retrieve missing log tables. | |
1065 | * | |
1066 | * @return array | |
1067 | */ | |
1068 | public function getMissingLogTables() { | |
1069 | if ($this->tablesExist()) { | |
1070 | return array_diff($this->tables, array_keys($this->logs)); | |
1071 | } | |
be2fb01f | 1072 | return []; |
74044663 JP |
1073 | } |
1074 | ||
d0b5c3f6 EM |
1075 | /** |
1076 | * Get the name of the database from the dsn string. | |
1077 | * | |
1078 | * @param string $dsnString | |
1079 | * | |
1080 | * @return string | |
1081 | */ | |
1082 | protected function getDatabaseNameFromDSN($dsnString): string { | |
1083 | $dsn = CRM_Utils_SQL::autoSwitchDSN($dsnString); | |
1084 | $dsn = DB::parseDSN($dsn); | |
1085 | return $dsn['database']; | |
1086 | } | |
1087 | ||
1088 | /** | |
1089 | * Get the database name for the CiviCRM connection. | |
1090 | * | |
1091 | * Note that we want to get it from the database connection, | |
1092 | * not the dsn, because there is at least one extension | |
1093 | * (https://github.com/totten/rpow) that 'meddles' with | |
1094 | * the DSN string. | |
1095 | * | |
1096 | * @return string | |
1097 | */ | |
1098 | protected function getCiviCRMDatabaseName(): string { | |
1099 | return (new CRM_Contact_DAO_Contact())->_database; | |
1100 | } | |
1101 | ||
6a488035 | 1102 | } |