| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.3 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 32 | * $Id$ |
| 33 | * |
| 34 | */ |
| 35 | class CRM_Logging_Schema { |
| 36 | private $logs = array(); |
| 37 | private $tables = array(); |
| 38 | |
| 39 | private $db; |
| 40 | private $useDBPrefix = TRUE; |
| 41 | |
| 42 | private $reports = array( |
| 43 | 'logging/contact/detail', |
| 44 | 'logging/contact/summary', |
| 45 | 'logging/contribute/detail', |
| 46 | 'logging/contribute/summary', |
| 47 | ); |
| 48 | |
| 49 | /** |
| 50 | * Populate $this->tables and $this->logs with current db state. |
| 51 | */ |
| 52 | function __construct() { |
| 53 | $dao = new CRM_Contact_DAO_Contact(); |
| 54 | $civiDBName = $dao->_database; |
| 55 | |
| 56 | $dao = CRM_Core_DAO::executeQuery(" |
| 57 | SELECT TABLE_NAME |
| 58 | FROM INFORMATION_SCHEMA.TABLES |
| 59 | WHERE TABLE_SCHEMA = '{$civiDBName}' |
| 60 | AND TABLE_TYPE = 'BASE TABLE' |
| 61 | AND TABLE_NAME LIKE 'civicrm_%' |
| 62 | "); |
| 63 | while ($dao->fetch()) { |
| 64 | $this->tables[] = $dao->TABLE_NAME; |
| 65 | } |
| 66 | |
| 67 | // do not log temp import, cache and log tables |
| 68 | $this->tables = preg_grep('/^civicrm_import_job_/', $this->tables, PREG_GREP_INVERT); |
| 69 | $this->tables = preg_grep('/_cache$/', $this->tables, PREG_GREP_INVERT); |
| 70 | $this->tables = preg_grep('/_log/', $this->tables, PREG_GREP_INVERT); |
| 71 | $this->tables = preg_grep('/^civicrm_task_action_temp_/', $this->tables, PREG_GREP_INVERT); |
| 72 | $this->tables = preg_grep('/^civicrm_export_temp_/', $this->tables, PREG_GREP_INVERT); |
| 73 | $this->tables = preg_grep('/^civicrm_queue_/', $this->tables, PREG_GREP_INVERT); |
| 74 | |
| 75 | // do not log civicrm_mailing_event* tables, CRM-12300 |
| 76 | $this->tables = preg_grep('/^civicrm_mailing_event_/', $this->tables, PREG_GREP_INVERT); |
| 77 | |
| 78 | if (defined('CIVICRM_LOGGING_DSN')) { |
| 79 | $dsn = DB::parseDSN(CIVICRM_LOGGING_DSN); |
| 80 | $this->useDBPrefix = (CIVICRM_LOGGING_DSN != CIVICRM_DSN); |
| 81 | } |
| 82 | else { |
| 83 | $dsn = DB::parseDSN(CIVICRM_DSN); |
| 84 | $this->useDBPrefix = FALSE; |
| 85 | } |
| 86 | $this->db = $dsn['database']; |
| 87 | |
| 88 | $dao = CRM_Core_DAO::executeQuery(" |
| 89 | SELECT TABLE_NAME |
| 90 | FROM INFORMATION_SCHEMA.TABLES |
| 91 | WHERE TABLE_SCHEMA = '{$this->db}' |
| 92 | AND TABLE_TYPE = 'BASE TABLE' |
| 93 | AND TABLE_NAME LIKE 'log_civicrm_%' |
| 94 | "); |
| 95 | while ($dao->fetch()) { |
| 96 | $log = $dao->TABLE_NAME; |
| 97 | $this->logs[substr($log, 4)] = $log; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Return logging custom data tables. |
| 103 | */ |
| 104 | function customDataLogTables() { |
| 105 | return preg_grep('/^log_civicrm_value_/', $this->logs); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Disable logging by dropping the triggers (but keep the log tables intact). |
| 110 | */ |
| 111 | function disableLogging() { |
| 112 | $config = CRM_Core_Config::singleton(); |
| 113 | $config->logging = FALSE; |
| 114 | |
| 115 | $this->dropTriggers(); |
| 116 | |
| 117 | // invoke the meta trigger creation call |
| 118 | CRM_Core_DAO::triggerRebuild(); |
| 119 | |
| 120 | $this->deleteReports(); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Drop triggers for all logged tables. |
| 125 | */ |
| 126 | function dropTriggers($tableName = NULL) { |
| 127 | $dao = new CRM_Core_DAO; |
| 128 | |
| 129 | if ($tableName) { |
| 130 | $tableNames = array($tableName); |
| 131 | } |
| 132 | else { |
| 133 | $tableNames = $this->tables; |
| 134 | } |
| 135 | |
| 136 | foreach ($tableNames as $table) { |
| 137 | // before triggers |
| 138 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_before_insert"); |
| 139 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_before_update"); |
| 140 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_before_delete"); |
| 141 | |
| 142 | // after triggers |
| 143 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_after_insert"); |
| 144 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_after_update"); |
| 145 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_after_delete"); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Enable sitewide logging. |
| 151 | * |
| 152 | * @return void |
| 153 | */ |
| 154 | function enableLogging() { |
| 155 | $this->fixSchemaDifferences(TRUE); |
| 156 | $this->addReports(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Sync log tables and rebuild triggers. |
| 161 | * |
| 162 | * @param bool $enableLogging: Ensure logging is enabled |
| 163 | * |
| 164 | * @return void |
| 165 | */ |
| 166 | function fixSchemaDifferences($enableLogging = FALSE) { |
| 167 | $config = CRM_Core_Config::singleton(); |
| 168 | if ($enableLogging) { |
| 169 | $config->logging = TRUE; |
| 170 | } |
| 171 | if ($config->logging) { |
| 172 | $this->fixSchemaDifferencesForALL(); |
| 173 | } |
| 174 | // invoke the meta trigger creation call |
| 175 | CRM_Core_DAO::triggerRebuild(); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Add missing (potentially specified) log table columns for the given table. |
| 180 | * |
| 181 | * @param $table string name of the relevant table |
| 182 | * @param $cols mixed array of columns to add or null (to check for the missing columns) |
| 183 | * @param $rebuildTrigger boolean should we rebuild the triggers |
| 184 | * |
| 185 | * @return void |
| 186 | */ |
| 187 | function fixSchemaDifferencesFor($table, $cols = array(), $rebuildTrigger = FALSE) { |
| 188 | if (empty($table)) { |
| 189 | return FALSE; |
| 190 | } |
| 191 | if (empty($this->logs[$table])) { |
| 192 | $this->createLogTableFor($table); |
| 193 | return TRUE; |
| 194 | } |
| 195 | |
| 196 | if (empty($cols)) { |
| 197 | $cols = $this->columnsWithDiffSpecs($table, "log_$table"); |
| 198 | } |
| 199 | |
| 200 | // use the relevant lines from CREATE TABLE to add colums to the log table |
| 201 | $create = $this->_getCreateQuery($table); |
| 202 | foreach ((array('ADD', 'MODIFY')) as $alterType) { |
| 203 | foreach ($cols[$alterType] as $col) { |
| 204 | $line = $this->_getColumnQuery($col, $create); |
| 205 | CRM_Core_DAO::executeQuery("ALTER TABLE `{$this->db}`.log_$table {$alterType} {$line}"); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // for any obsolete columns (not null) we just make the column nullable. |
| 210 | if (!empty($cols['OBSOLETE'])) { |
| 211 | $create = $this->_getCreateQuery("log_{$table}"); |
| 212 | foreach ($cols['OBSOLETE'] as $col) { |
| 213 | $line = $this->_getColumnQuery($col, $create); |
| 214 | // This is just going to make a not null column to nullable |
| 215 | CRM_Core_DAO::executeQuery("ALTER TABLE `{$this->db}`.log_$table MODIFY {$line}"); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if ($rebuildTrigger) { |
| 220 | // invoke the meta trigger creation call |
| 221 | CRM_Core_DAO::triggerRebuild($table); |
| 222 | } |
| 223 | return TRUE; |
| 224 | } |
| 225 | |
| 226 | private function _getCreateQuery($table) { |
| 227 | $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE {$table}"); |
| 228 | $dao->fetch(); |
| 229 | $create = explode("\n", $dao->Create_Table); |
| 230 | return $create; |
| 231 | } |
| 232 | |
| 233 | private function _getColumnQuery($col, $createQuery) { |
| 234 | $line = preg_grep("/^ `$col` /", $createQuery); |
| 235 | $line = rtrim(array_pop($line), ','); |
| 236 | // CRM-11179 |
| 237 | $line = $this->fixTimeStampAndNotNullSQL($line); |
| 238 | return $line; |
| 239 | } |
| 240 | |
| 241 | function fixSchemaDifferencesForAll($rebuildTrigger = FALSE) { |
| 242 | $diffs = array(); |
| 243 | foreach ($this->tables as $table) { |
| 244 | if (empty($this->logs[$table])) { |
| 245 | $this->createLogTableFor($table); |
| 246 | } else { |
| 247 | $diffs[$table] = $this->columnsWithDiffSpecs($table, "log_$table"); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | foreach ($diffs as $table => $cols) { |
| 252 | $this->fixSchemaDifferencesFor($table, $cols, FALSE); |
| 253 | } |
| 254 | |
| 255 | if ($rebuildTrigger) { |
| 256 | // invoke the meta trigger creation call |
| 257 | CRM_Core_DAO::triggerRebuild($table); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * log_civicrm_contact.modified_date for example would always be copied from civicrm_contact.modified_date, |
| 263 | * so there's no need for a default timestamp and therefore we remove such default timestamps |
| 264 | * also eliminate the NOT NULL constraint, since we always copy and schema can change down the road) |
| 265 | */ |
| 266 | function fixTimeStampAndNotNullSQL($query) { |
| 267 | $query = str_ireplace("TIMESTAMP NOT NULL", "TIMESTAMP NULL", $query); |
| 268 | $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", '', $query); |
| 269 | $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP", '', $query); |
| 270 | $query = str_ireplace("NOT NULL", '', $query); |
| 271 | return $query; |
| 272 | } |
| 273 | |
| 274 | private function addReports() { |
| 275 | $titles = array( |
| 276 | 'logging/contact/detail' => ts('Logging Details'), |
| 277 | 'logging/contact/summary' => ts('Contact Logging Report (Summary)'), |
| 278 | 'logging/contribute/detail' => ts('Contribution Logging Report (Detail)'), |
| 279 | 'logging/contribute/summary' => ts('Contribution Logging Report (Summary)'), |
| 280 | ); |
| 281 | // enable logging templates |
| 282 | CRM_Core_DAO::executeQuery(" |
| 283 | UPDATE civicrm_option_value |
| 284 | SET is_active = 1 |
| 285 | WHERE value IN ('" . implode("', '", $this->reports) . "') |
| 286 | "); |
| 287 | |
| 288 | // add report instances |
| 289 | $domain_id = CRM_Core_Config::domainID(); |
| 290 | foreach ($this->reports as $report) { |
| 291 | $dao = new CRM_Report_DAO_Instance; |
| 292 | $dao->domain_id = $domain_id; |
| 293 | $dao->report_id = $report; |
| 294 | $dao->title = $titles[$report]; |
| 295 | $dao->permission = 'administer CiviCRM'; |
| 296 | if ($report == 'logging/contact/summary') |
| 297 | $dao->is_reserved = 1; |
| 298 | $dao->insert(); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Get an array of column names of the given table. |
| 304 | */ |
| 305 | private function columnsOf($table) { |
| 306 | static $columnsOf = array(); |
| 307 | |
| 308 | $from = (substr($table, 0, 4) == 'log_') ? "`{$this->db}`.$table" : $table; |
| 309 | |
| 310 | if (!isset($columnsOf[$table])) { |
| 311 | CRM_Core_Error::ignoreException(); |
| 312 | $dao = CRM_Core_DAO::executeQuery("SHOW COLUMNS FROM $from"); |
| 313 | CRM_Core_Error::setCallback(); |
| 314 | if (is_a($dao, 'DB_Error')) { |
| 315 | return array(); |
| 316 | } |
| 317 | $columnsOf[$table] = array(); |
| 318 | while ($dao->fetch()) { |
| 319 | $columnsOf[$table][] = $dao->Field; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return $columnsOf[$table]; |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Get an array of columns and their details like DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT for the given table. |
| 328 | */ |
| 329 | private function columnSpecsOf($table) { |
| 330 | static $columnSpecs = array(), $civiDB = NULL; |
| 331 | |
| 332 | if (empty($columnSpecs)) { |
| 333 | if (!$civiDB) { |
| 334 | $dao = new CRM_Contact_DAO_Contact(); |
| 335 | $civiDB = $dao->_database; |
| 336 | } |
| 337 | CRM_Core_Error::ignoreException(); |
| 338 | // NOTE: W.r.t Performance using one query to find all details and storing in static array is much faster |
| 339 | // than firing query for every given table. |
| 340 | $query = " |
| 341 | SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT |
| 342 | FROM INFORMATION_SCHEMA.COLUMNS |
| 343 | WHERE table_schema IN ('{$this->db}', '{$civiDB}')"; |
| 344 | $dao = CRM_Core_DAO::executeQuery($query); |
| 345 | CRM_Core_Error::setCallback(); |
| 346 | if (is_a($dao, 'DB_Error')) { |
| 347 | return array(); |
| 348 | } |
| 349 | while ($dao->fetch()) { |
| 350 | if (!array_key_exists($dao->TABLE_NAME, $columnSpecs)) { |
| 351 | $columnSpecs[$dao->TABLE_NAME] = array(); |
| 352 | } |
| 353 | $columnSpecs[$dao->TABLE_NAME][$dao->COLUMN_NAME] = |
| 354 | array( |
| 355 | 'COLUMN_NAME' => $dao->COLUMN_NAME, |
| 356 | 'DATA_TYPE' => $dao->DATA_TYPE, |
| 357 | 'IS_NULLABLE' => $dao->IS_NULLABLE, |
| 358 | 'COLUMN_DEFAULT' => $dao->COLUMN_DEFAULT |
| 359 | ); |
| 360 | } |
| 361 | } |
| 362 | return $columnSpecs[$table]; |
| 363 | } |
| 364 | |
| 365 | function columnsWithDiffSpecs($civiTable, $logTable) { |
| 366 | $civiTableSpecs = $this->columnSpecsOf($civiTable); |
| 367 | $logTableSpecs = $this->columnSpecsOf($logTable); |
| 368 | |
| 369 | $diff = array('ADD' => array(), 'MODIFY' => array(), 'OBSOLETE' => array()); |
| 370 | |
| 371 | // columns to be added |
| 372 | $diff['ADD'] = array_diff(array_keys($civiTableSpecs), array_keys($logTableSpecs)); |
| 373 | |
| 374 | // columns to be modified |
| 375 | // NOTE: we consider only those columns for modifications where there is a spec change, and that the column definition |
| 376 | // wasn't deliberately modified by fixTimeStampAndNotNullSQL() method. |
| 377 | foreach ($civiTableSpecs as $col => $colSpecs) { |
| 378 | $specDiff = array_diff($civiTableSpecs[$col], $logTableSpecs[$col]); |
| 379 | if (!empty($specDiff) && $col != 'id') { |
| 380 | // ignore 'id' column for any spec changes, to avoid any auto-increment mysql errors |
| 381 | if ($civiTableSpecs[$col]['DATA_TYPE'] != $logTableSpecs[$col]['DATA_TYPE']) { |
| 382 | // if data-type is different, surely consider the column |
| 383 | $diff['MODIFY'][] = $col; |
| 384 | } else if ($civiTableSpecs[$col]['IS_NULLABLE'] != $logTableSpecs[$col]['IS_NULLABLE'] && |
| 385 | $logTableSpecs[$col]['IS_NULLABLE'] == 'NO') { |
| 386 | // if is-null property is different, and log table's column is NOT-NULL, surely consider the column |
| 387 | $diff['MODIFY'][] = $col; |
| 388 | } else if ($civiTableSpecs[$col]['COLUMN_DEFAULT'] != $logTableSpecs[$col]['COLUMN_DEFAULT'] && |
| 389 | !strstr($civiTableSpecs[$col]['COLUMN_DEFAULT'], 'TIMESTAMP')) { |
| 390 | // if default property is different, and its not about a timestamp column, consider it |
| 391 | $diff['MODIFY'][] = $col; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // columns to made obsolete by turning into not-null |
| 397 | $oldCols = array_diff(array_keys($logTableSpecs), array_keys($civiTableSpecs)); |
| 398 | foreach ($oldCols as $col) { |
| 399 | if (!in_array($col, array('log_date', 'log_conn_id', 'log_user_id', 'log_action')) && |
| 400 | $logTableSpecs[$col]['IS_NULLABLE'] == 'NO') { |
| 401 | // if its a column present only in log table, not among those used by log tables for special purpose, and not-null |
| 402 | $diff['OBSOLETE'][] = $col; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | return $diff; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Create a log table with schema mirroring the given table’s structure and seeding it with the given table’s contents. |
| 411 | */ |
| 412 | private function createLogTableFor($table) { |
| 413 | $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE $table"); |
| 414 | $dao->fetch(); |
| 415 | $query = $dao->Create_Table; |
| 416 | |
| 417 | // rewrite the queries into CREATE TABLE queries for log tables: |
| 418 | $cols = <<<COLS |
| 419 | log_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 420 | log_conn_id INTEGER, |
| 421 | log_user_id INTEGER, |
| 422 | log_action ENUM('Initialization', 'Insert', 'Update', 'Delete') |
| 423 | COLS; |
| 424 | |
| 425 | // - prepend the name with log_ |
| 426 | // - drop AUTO_INCREMENT columns |
| 427 | // - drop non-column rows of the query (keys, constraints, etc.) |
| 428 | // - set the ENGINE to ARCHIVE |
| 429 | // - add log-specific columns (at the end of the table) |
| 430 | $query = preg_replace("/^CREATE TABLE `$table`/i", "CREATE TABLE `{$this->db}`.log_$table", $query); |
| 431 | $query = preg_replace("/ AUTO_INCREMENT/i", '', $query); |
| 432 | $query = preg_replace("/^ [^`].*$/m", '', $query); |
| 433 | $query = preg_replace("/^\) ENGINE=[^ ]+ /im", ') ENGINE=ARCHIVE ', $query); |
| 434 | |
| 435 | // log_civicrm_contact.modified_date for example would always be copied from civicrm_contact.modified_date, |
| 436 | // so there's no need for a default timestamp and therefore we remove such default timestamps |
| 437 | // also eliminate the NOT NULL constraint, since we always copy and schema can change down the road) |
| 438 | $query = self::fixTimeStampAndNotNullSQL($query); |
| 439 | $query = preg_replace("/^\) /m", "$cols\n) ", $query); |
| 440 | |
| 441 | CRM_Core_DAO::executeQuery($query); |
| 442 | |
| 443 | $columns = implode(', ', $this->columnsOf($table)); |
| 444 | CRM_Core_DAO::executeQuery("INSERT INTO `{$this->db}`.log_$table ($columns, log_conn_id, log_user_id, log_action) SELECT $columns, CONNECTION_ID(), @civicrm_user_id, 'Initialization' FROM {$table}"); |
| 445 | |
| 446 | $this->tables[] = $table; |
| 447 | $this->logs[$table] = "log_$table"; |
| 448 | } |
| 449 | |
| 450 | private function deleteReports() { |
| 451 | // disable logging templates |
| 452 | CRM_Core_DAO::executeQuery(" |
| 453 | UPDATE civicrm_option_value |
| 454 | SET is_active = 0 |
| 455 | WHERE value IN ('" . implode("', '", $this->reports) . "') |
| 456 | "); |
| 457 | |
| 458 | // delete report instances |
| 459 | $domain_id = CRM_Core_Config::domainID(); |
| 460 | foreach ($this->reports as $report) { |
| 461 | $dao = new CRM_Report_DAO_Instance; |
| 462 | $dao->domain_id = $domain_id; |
| 463 | $dao->report_id = $report; |
| 464 | $dao->delete(); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Predicate whether logging is enabled. |
| 470 | */ |
| 471 | public function isEnabled() { |
| 472 | $config = CRM_Core_Config::singleton(); |
| 473 | |
| 474 | if ($config->logging) { |
| 475 | return $this->tablesExist() and $this->triggersExist(); |
| 476 | } |
| 477 | return FALSE; |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Predicate whether any log tables exist. |
| 482 | */ |
| 483 | private function tablesExist() { |
| 484 | return !empty($this->logs); |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Predicate whether the logging triggers are in place. |
| 489 | */ |
| 490 | private function triggersExist() { |
| 491 | // FIXME: probably should be a bit more thorough… |
| 492 | // note that the LIKE parameter is TABLE NAME |
| 493 | return (bool) CRM_Core_DAO::singleValueQuery("SHOW TRIGGERS LIKE 'civicrm_contact'"); |
| 494 | } |
| 495 | |
| 496 | function triggerInfo(&$info, $tableName = NULL) { |
| 497 | // check if we have logging enabled |
| 498 | $config =& CRM_Core_Config::singleton(); |
| 499 | if (!$config->logging) { |
| 500 | return; |
| 501 | } |
| 502 | |
| 503 | $insert = array('INSERT'); |
| 504 | $update = array('UPDATE'); |
| 505 | $delete = array('DELETE'); |
| 506 | |
| 507 | if ($tableName) { |
| 508 | $tableNames = array($tableName); |
| 509 | } |
| 510 | else { |
| 511 | $tableNames = $this->tables; |
| 512 | } |
| 513 | |
| 514 | // logging is enabled, so now lets create the trigger info tables |
| 515 | foreach ($tableNames as $table) { |
| 516 | $columns = $this->columnsOf($table); |
| 517 | |
| 518 | // only do the change if any data has changed |
| 519 | $cond = array( ); |
| 520 | foreach ($columns as $column) { |
| 521 | // ignore modified_date changes |
| 522 | if ($column != 'modified_date') { |
| 523 | $cond[] = "IFNULL(OLD.$column,'') <> IFNULL(NEW.$column,'')"; |
| 524 | } |
| 525 | } |
| 526 | $suppressLoggingCond = "@civicrm_disable_logging IS NULL OR @civicrm_disable_logging = 0"; |
| 527 | $updateSQL = "IF ( (" . implode( ' OR ', $cond ) . ") AND ( $suppressLoggingCond ) ) THEN "; |
| 528 | |
| 529 | if ($this->useDBPrefix) { |
| 530 | $sqlStmt = "INSERT INTO `{$this->db}`.log_{tableName} ("; |
| 531 | } |
| 532 | else { |
| 533 | $sqlStmt = "INSERT INTO log_{tableName} ("; |
| 534 | } |
| 535 | foreach ($columns as $column) { |
| 536 | $sqlStmt .= "$column, "; |
| 537 | } |
| 538 | $sqlStmt .= "log_conn_id, log_user_id, log_action) VALUES ("; |
| 539 | |
| 540 | $insertSQL = $deleteSQL = "IF ( $suppressLoggingCond ) THEN $sqlStmt "; |
| 541 | $updateSQL .= $sqlStmt; |
| 542 | |
| 543 | $sqlStmt = ''; |
| 544 | foreach ($columns as $column) { |
| 545 | $sqlStmt .= "NEW.$column, "; |
| 546 | $deleteSQL .= "OLD.$column, "; |
| 547 | } |
| 548 | $sqlStmt .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}');"; |
| 549 | $deleteSQL .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}');"; |
| 550 | |
| 551 | $sqlStmt .= "END IF;"; |
| 552 | $deleteSQL .= "END IF;"; |
| 553 | |
| 554 | $insertSQL .= $sqlStmt; |
| 555 | $updateSQL .= $sqlStmt; |
| 556 | |
| 557 | $info[] = array( |
| 558 | 'table' => array($table), |
| 559 | 'when' => 'AFTER', |
| 560 | 'event' => $insert, |
| 561 | 'sql' => $insertSQL, |
| 562 | ); |
| 563 | |
| 564 | $info[] = array( |
| 565 | 'table' => array($table), |
| 566 | 'when' => 'AFTER', |
| 567 | 'event' => $update, |
| 568 | 'sql' => $updateSQL, |
| 569 | ); |
| 570 | |
| 571 | $info[] = array( |
| 572 | 'table' => array($table), |
| 573 | 'when' => 'AFTER', |
| 574 | 'event' => $delete, |
| 575 | 'sql' => $deleteSQL, |
| 576 | ); |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * This allow logging to be temporarily disabled for certain cases |
| 582 | * where we want to do a mass cleanup but dont want to bother with |
| 583 | * an audit trail |
| 584 | * |
| 585 | * @static |
| 586 | * @public |
| 587 | */ |
| 588 | static function disableLoggingForThisConnection( ) { |
| 589 | // do this only if logging is enabled |
| 590 | $config = CRM_Core_Config::singleton( ); |
| 591 | if ( $config->logging ) { |
| 592 | CRM_Core_DAO::executeQuery( 'SET @civicrm_disable_logging = 1' ); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | } |
| 597 | |