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