3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 class CRM_Logging_Schema
{
36 private $logs = array();
37 private $tables = array();
40 private $useDBPrefix = TRUE;
42 private $reports = array(
43 'logging/contact/detail',
44 'logging/contact/summary',
45 'logging/contribute/detail',
46 'logging/contribute/summary',
49 //CRM-13028 / NYSS-6933 - table => array (cols) - to be excluded from the update statement
50 private $exceptions = array(
51 'civicrm_job' => array('last_run'),
52 'civicrm_group' => array('cache_date', 'refresh_date'),
56 * (Setting Callback - Validate)
59 * @param array $fieldSpec
61 * @throws API_Exception
63 public static function checkLoggingSupport(&$value, $fieldSpec) {
64 $domain = new CRM_Core_DAO_Domain();
66 if (!(CRM_Core_DAO
::checkTriggerViewPermission(FALSE)) && $value) {
67 throw new API_Exception("In order to use this functionality, the installation's database user must have privileges to create triggers (in MySQL 5.0 – and in MySQL 5.1 if binary logging is enabled – this means the SUPER privilege). This install either does not seem to have the required privilege enabled.");
73 * (Setting Callback - On Change)
74 * Respond to changes in the "logging" setting. Set up or destroy
77 * @param array $oldValue
78 * List of component names.
79 * @param array $newValue
80 * List of component names.
81 * @param array $metadata
82 * Specification of the setting (per *.settings.php).
84 public static function onToggle($oldValue, $newValue, $metadata) {
85 if ($oldValue == $newValue) {
89 $logging = new CRM_Logging_Schema();
91 $logging->enableLogging();
94 $logging->disableLogging();
99 * Populate $this->tables and $this->logs with current db state.
101 public function __construct() {
102 $dao = new CRM_Contact_DAO_Contact();
103 $civiDBName = $dao->_database
;
105 $dao = CRM_Core_DAO
::executeQuery("
107 FROM INFORMATION_SCHEMA.TABLES
108 WHERE TABLE_SCHEMA = '{$civiDBName}'
109 AND TABLE_TYPE = 'BASE TABLE'
110 AND TABLE_NAME LIKE 'civicrm_%'
112 while ($dao->fetch()) {
113 $this->tables
[] = $dao->TABLE_NAME
;
116 // do not log temp import, cache, menu and log tables
117 $this->tables
= preg_grep('/^civicrm_import_job_/', $this->tables
, PREG_GREP_INVERT
);
118 $this->tables
= preg_grep('/_cache$/', $this->tables
, PREG_GREP_INVERT
);
119 $this->tables
= preg_grep('/_log/', $this->tables
, PREG_GREP_INVERT
);
120 $this->tables
= preg_grep('/^civicrm_queue_/', $this->tables
, PREG_GREP_INVERT
);
121 $this->tables
= preg_grep('/^civicrm_menu/', $this->tables
, PREG_GREP_INVERT
); //CRM-14672
122 $this->tables
= preg_grep('/_temp_/', $this->tables
, PREG_GREP_INVERT
);
124 // do not log civicrm_mailing_event* tables, CRM-12300
125 $this->tables
= preg_grep('/^civicrm_mailing_event_/', $this->tables
, PREG_GREP_INVERT
);
127 // do not log civicrm_mailing_recipients table, CRM-16193
128 $this->tables
= array_diff($this->tables
, array('civicrm_mailing_recipients'));
130 if (defined('CIVICRM_LOGGING_DSN')) {
131 $dsn = DB
::parseDSN(CIVICRM_LOGGING_DSN
);
132 $this->useDBPrefix
= (CIVICRM_LOGGING_DSN
!= CIVICRM_DSN
);
135 $dsn = DB
::parseDSN(CIVICRM_DSN
);
136 $this->useDBPrefix
= FALSE;
138 $this->db
= $dsn['database'];
140 $dao = CRM_Core_DAO
::executeQuery("
142 FROM INFORMATION_SCHEMA.TABLES
143 WHERE TABLE_SCHEMA = '{$this->db}'
144 AND TABLE_TYPE = 'BASE TABLE'
145 AND TABLE_NAME LIKE 'log_civicrm_%'
147 while ($dao->fetch()) {
148 $log = $dao->TABLE_NAME
;
149 $this->logs
[substr($log, 4)] = $log;
154 * Return logging custom data tables.
156 public function customDataLogTables() {
157 return preg_grep('/^log_civicrm_value_/', $this->logs
);
161 * Return custom data tables for specified entity / extends.
163 public function entityCustomDataLogTables($extends) {
164 $customGroupTables = array();
165 $customGroupDAO = CRM_Core_BAO_CustomGroup
::getAllCustomGroupsByBaseEntity($extends);
166 $customGroupDAO->find();
167 while ($customGroupDAO->fetch()) {
168 $customGroupTables[$customGroupDAO->table_name
] = $this->logs
[$customGroupDAO->table_name
];
170 return $customGroupTables;
174 * Disable logging by dropping the triggers (but keep the log tables intact).
176 public function disableLogging() {
177 $config = CRM_Core_Config
::singleton();
178 $config->logging
= FALSE;
180 $this->dropTriggers();
182 // invoke the meta trigger creation call
183 CRM_Core_DAO
::triggerRebuild();
185 $this->deleteReports();
189 * Drop triggers for all logged tables.
191 public function dropTriggers($tableName = NULL) {
192 $dao = new CRM_Core_DAO();
195 $tableNames = array($tableName);
198 $tableNames = $this->tables
;
201 foreach ($tableNames as $table) {
202 $validName = CRM_Core_DAO
::shortenSQLName($table, 48, TRUE);
205 $dao->executeQuery("DROP TRIGGER IF EXISTS {$validName}_before_insert");
206 $dao->executeQuery("DROP TRIGGER IF EXISTS {$validName}_before_update");
207 $dao->executeQuery("DROP TRIGGER IF EXISTS {$validName}_before_delete");
210 $dao->executeQuery("DROP TRIGGER IF EXISTS {$validName}_after_insert");
211 $dao->executeQuery("DROP TRIGGER IF EXISTS {$validName}_after_update");
212 $dao->executeQuery("DROP TRIGGER IF EXISTS {$validName}_after_delete");
215 // now lets also be safe and drop all triggers that start with
216 // civicrm_ if we are dropping all triggers
217 // we need to do this to capture all the leftover triggers since
218 // we did the shortening trigger name for CRM-11794
219 if ($tableName === NULL) {
220 $triggers = $dao->executeQuery("SHOW TRIGGERS LIKE 'civicrm_%'");
222 while ($triggers->fetch()) {
223 // note that drop trigger has a weird syntax and hence we do not
224 // send the trigger name as a string (i.e. its not quoted
225 $dao->executeQuery("DROP TRIGGER IF EXISTS {$triggers->Trigger}");
231 * Enable sitewide logging.
235 public function enableLogging() {
236 $this->fixSchemaDifferences(TRUE);
241 * Sync log tables and rebuild triggers.
243 * @param bool $enableLogging : Ensure logging is enabled
247 public function fixSchemaDifferences($enableLogging = FALSE) {
248 $config = CRM_Core_Config
::singleton();
249 if ($enableLogging) {
250 $config->logging
= TRUE;
252 if ($config->logging
) {
253 $this->fixSchemaDifferencesForALL();
255 // invoke the meta trigger creation call
256 CRM_Core_DAO
::triggerRebuild(NULL, TRUE);
260 * Add missing (potentially specified) log table columns for the given table.
262 * @param string $table
263 * name of the relevant table.
265 * Mixed array of columns to add or null (to check for the missing columns).
266 * @param bool $rebuildTrigger
267 * should we rebuild the triggers.
271 public function fixSchemaDifferencesFor($table, $cols = array(), $rebuildTrigger = FALSE) {
275 if (empty($this->logs
[$table])) {
276 $this->createLogTableFor($table);
281 $cols = $this->columnsWithDiffSpecs($table, "log_$table");
284 // use the relevant lines from CREATE TABLE to add colums to the log table
285 $create = $this->_getCreateQuery($table);
286 foreach ((array('ADD', 'MODIFY')) as $alterType) {
287 if (!empty($cols[$alterType])) {
288 foreach ($cols[$alterType] as $col) {
289 $line = $this->_getColumnQuery($col, $create);
290 CRM_Core_DAO
::executeQuery("ALTER TABLE `{$this->db}`.log_$table {$alterType} {$line}");
295 // for any obsolete columns (not null) we just make the column nullable.
296 if (!empty($cols['OBSOLETE'])) {
297 $create = $this->_getCreateQuery("`{$this->db}`.log_{$table}");
298 foreach ($cols['OBSOLETE'] as $col) {
299 $line = $this->_getColumnQuery($col, $create);
300 // This is just going to make a not null column to nullable
301 CRM_Core_DAO
::executeQuery("ALTER TABLE `{$this->db}`.log_$table MODIFY {$line}");
305 if ($rebuildTrigger) {
306 // invoke the meta trigger creation call
307 CRM_Core_DAO
::triggerRebuild($table);
317 private function _getCreateQuery($table) {
318 $dao = CRM_Core_DAO
::executeQuery("SHOW CREATE TABLE {$table}");
320 $create = explode("\n", $dao->Create_Table
);
326 * @param $createQuery
328 * @return array|mixed|string
330 private function _getColumnQuery($col, $createQuery) {
331 $line = preg_grep("/^ `$col` /", $createQuery);
332 $line = rtrim(array_pop($line), ',');
334 $line = $this->fixTimeStampAndNotNullSQL($line);
339 * @param bool $rebuildTrigger
341 public function fixSchemaDifferencesForAll($rebuildTrigger = FALSE) {
343 foreach ($this->tables
as $table) {
344 if (empty($this->logs
[$table])) {
345 $this->createLogTableFor($table);
348 $diffs[$table] = $this->columnsWithDiffSpecs($table, "log_$table");
352 foreach ($diffs as $table => $cols) {
353 $this->fixSchemaDifferencesFor($table, $cols, FALSE);
356 if ($rebuildTrigger) {
357 // invoke the meta trigger creation call
358 CRM_Core_DAO
::triggerRebuild($table);
363 * Log_civicrm_contact.modified_date for example would always be copied from civicrm_contact.modified_date,
364 * so there's no need for a default timestamp and therefore we remove such default timestamps
365 * also eliminate the NOT NULL constraint, since we always copy and schema can change down the road)
371 public function fixTimeStampAndNotNullSQL($query) {
372 $query = str_ireplace("TIMESTAMP NOT NULL", "TIMESTAMP NULL", $query);
373 $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", '', $query);
374 $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP", '', $query);
375 $query = str_ireplace("NOT NULL", '', $query);
379 private function addReports() {
381 'logging/contact/detail' => ts('Logging Details'),
382 'logging/contact/summary' => ts('Contact Logging Report (Summary)'),
383 'logging/contribute/detail' => ts('Contribution Logging Report (Detail)'),
384 'logging/contribute/summary' => ts('Contribution Logging Report (Summary)'),
386 // enable logging templates
387 CRM_Core_DAO
::executeQuery("
388 UPDATE civicrm_option_value
390 WHERE value IN ('" . implode("', '", $this->reports
) . "')
393 // add report instances
394 $domain_id = CRM_Core_Config
::domainID();
395 foreach ($this->reports
as $report) {
396 $dao = new CRM_Report_DAO_ReportInstance();
397 $dao->domain_id
= $domain_id;
398 $dao->report_id
= $report;
399 $dao->title
= $titles[$report];
400 $dao->permission
= 'administer CiviCRM';
401 if ($report == 'logging/contact/summary') {
402 $dao->is_reserved
= 1;
409 * Get an array of column names of the given table.
411 private function columnsOf($table, $force = FALSE) {
412 static $columnsOf = array();
414 $from = (substr($table, 0, 4) == 'log_') ?
"`{$this->db}`.$table" : $table;
416 if (!isset($columnsOf[$table]) ||
$force) {
417 $errorScope = CRM_Core_TemporaryErrorScope
::ignoreException();
418 $dao = CRM_Core_DAO
::executeQuery("SHOW COLUMNS FROM $from", CRM_Core_DAO
::$_nullArray, TRUE, NULL, FALSE, FALSE);
419 if (is_a($dao, 'DB_Error')) {
422 $columnsOf[$table] = array();
423 while ($dao->fetch()) {
424 $columnsOf[$table][] = $dao->Field
;
428 return $columnsOf[$table];
432 * Get an array of columns and their details like DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT for the given table.
434 private function columnSpecsOf($table) {
435 static $columnSpecs = array(), $civiDB = NULL;
437 if (empty($columnSpecs)) {
439 $dao = new CRM_Contact_DAO_Contact();
440 $civiDB = $dao->_database
;
442 CRM_Core_TemporaryErrorScope
::ignoreException();
443 // NOTE: W.r.t Performance using one query to find all details and storing in static array is much faster
444 // than firing query for every given table.
446 SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_TYPE
447 FROM INFORMATION_SCHEMA.COLUMNS
448 WHERE table_schema IN ('{$this->db}', '{$civiDB}')";
449 $dao = CRM_Core_DAO
::executeQuery($query);
450 if (is_a($dao, 'DB_Error')) {
453 while ($dao->fetch()) {
454 if (!array_key_exists($dao->TABLE_NAME
, $columnSpecs)) {
455 $columnSpecs[$dao->TABLE_NAME
] = array();
457 $columnSpecs[$dao->TABLE_NAME
][$dao->COLUMN_NAME
] = array(
458 'COLUMN_NAME' => $dao->COLUMN_NAME
,
459 'DATA_TYPE' => $dao->DATA_TYPE
,
460 'IS_NULLABLE' => $dao->IS_NULLABLE
,
461 'COLUMN_DEFAULT' => $dao->COLUMN_DEFAULT
,
463 if (($first = strpos($dao->COLUMN_TYPE
, '(')) != 0) {
464 $columnSpecs[$dao->TABLE_NAME
][$dao->COLUMN_NAME
]['LENGTH'] = substr($dao->COLUMN_TYPE
, $first, strpos($dao->COLUMN_TYPE
, ')'));
468 return $columnSpecs[$table];
477 public function columnsWithDiffSpecs($civiTable, $logTable) {
478 $civiTableSpecs = $this->columnSpecsOf($civiTable);
479 $logTableSpecs = $this->columnSpecsOf($logTable);
481 $diff = array('ADD' => array(), 'MODIFY' => array(), 'OBSOLETE' => array());
483 // columns to be added
484 $diff['ADD'] = array_diff(array_keys($civiTableSpecs), array_keys($logTableSpecs));
486 // columns to be modified
487 // NOTE: we consider only those columns for modifications where there is a spec change, and that the column definition
488 // wasn't deliberately modified by fixTimeStampAndNotNullSQL() method.
489 foreach ($civiTableSpecs as $col => $colSpecs) {
490 if (!isset($logTableSpecs[$col]) ||
!is_array($logTableSpecs[$col])) {
491 $logTableSpecs[$col] = array();
494 $specDiff = array_diff($civiTableSpecs[$col], $logTableSpecs[$col]);
495 if (!empty($specDiff) && $col != 'id' && !array_key_exists($col, $diff['ADD'])) {
496 // ignore 'id' column for any spec changes, to avoid any auto-increment mysql errors
497 if ($civiTableSpecs[$col]['DATA_TYPE'] != CRM_Utils_Array
::value('DATA_TYPE', $logTableSpecs[$col])
498 // We won't alter the log if the length is decreased in case some of the existing data won't fit.
499 || CRM_Utils_Array
::value('LENGTH', $civiTableSpecs[$col]) > CRM_Utils_Array
::value('LENGTH', $logTableSpecs[$col])
501 // if data-type is different, surely consider the column
502 $diff['MODIFY'][] = $col;
504 elseif ($civiTableSpecs[$col]['IS_NULLABLE'] != CRM_Utils_Array
::value('IS_NULLABLE', $logTableSpecs[$col]) &&
505 $logTableSpecs[$col]['IS_NULLABLE'] == 'NO'
507 // if is-null property is different, and log table's column is NOT-NULL, surely consider the column
508 $diff['MODIFY'][] = $col;
510 elseif ($civiTableSpecs[$col]['COLUMN_DEFAULT'] != CRM_Utils_Array
::value('COLUMN_DEFAULT', $logTableSpecs[$col]) &&
511 !strstr($civiTableSpecs[$col]['COLUMN_DEFAULT'], 'TIMESTAMP')
513 // if default property is different, and its not about a timestamp column, consider it
514 $diff['MODIFY'][] = $col;
519 // columns to made obsolete by turning into not-null
520 $oldCols = array_diff(array_keys($logTableSpecs), array_keys($civiTableSpecs));
521 foreach ($oldCols as $col) {
522 if (!in_array($col, array('log_date', 'log_conn_id', 'log_user_id', 'log_action')) &&
523 $logTableSpecs[$col]['IS_NULLABLE'] == 'NO'
525 // if its a column present only in log table, not among those used by log tables for special purpose, and not-null
526 $diff['OBSOLETE'][] = $col;
534 * Create a log table with schema mirroring the given table’s structure and seeding it with the given table’s contents.
536 private function createLogTableFor($table) {
537 $dao = CRM_Core_DAO
::executeQuery("SHOW CREATE TABLE $table", CRM_Core_DAO
::$_nullArray, TRUE, NULL, FALSE, FALSE);
539 $query = $dao->Create_Table
;
541 // rewrite the queries into CREATE TABLE queries for log tables:
543 log_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
546 log_action ENUM('Initialization', 'Insert', 'Update', 'Delete')
549 // - prepend the name with log_
550 // - drop AUTO_INCREMENT columns
551 // - drop non-column rows of the query (keys, constraints, etc.)
552 // - set the ENGINE to ARCHIVE
553 // - add log-specific columns (at the end of the table)
554 $query = preg_replace("/^CREATE TABLE `$table`/i", "CREATE TABLE `{$this->db}`.log_$table", $query);
555 $query = preg_replace("/ AUTO_INCREMENT/i", '', $query);
556 $query = preg_replace("/^ [^`].*$/m", '', $query);
557 $query = preg_replace("/^\) ENGINE=[^ ]+ /im", ') ENGINE=ARCHIVE ', $query);
559 // log_civicrm_contact.modified_date for example would always be copied from civicrm_contact.modified_date,
560 // so there's no need for a default timestamp and therefore we remove such default timestamps
561 // also eliminate the NOT NULL constraint, since we always copy and schema can change down the road)
562 $query = self
::fixTimeStampAndNotNullSQL($query);
563 $query = preg_replace("/^\) /m", "$cols\n) ", $query);
565 CRM_Core_DAO
::executeQuery($query, CRM_Core_DAO
::$_nullArray, TRUE, NULL, FALSE, FALSE);
567 $columns = implode(', ', $this->columnsOf($table));
568 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}", CRM_Core_DAO
::$_nullArray, TRUE, NULL, FALSE, FALSE);
570 $this->tables
[] = $table;
571 $this->logs
[$table] = "log_$table";
574 private function deleteReports() {
575 // disable logging templates
576 CRM_Core_DAO
::executeQuery("
577 UPDATE civicrm_option_value
579 WHERE value IN ('" . implode("', '", $this->reports
) . "')
582 // delete report instances
583 $domain_id = CRM_Core_Config
::domainID();
584 foreach ($this->reports
as $report) {
585 $dao = new CRM_Report_DAO_ReportInstance();
586 $dao->domain_id
= $domain_id;
587 $dao->report_id
= $report;
593 * Predicate whether logging is enabled.
595 public function isEnabled() {
596 $config = CRM_Core_Config
::singleton();
598 if ($config->logging
) {
599 return $this->tablesExist() and $this->triggersExist();
605 * Predicate whether any log tables exist.
607 private function tablesExist() {
608 return !empty($this->logs
);
612 * Predicate whether the logging triggers are in place.
614 private function triggersExist() {
615 // FIXME: probably should be a bit more thorough…
616 // note that the LIKE parameter is TABLE NAME
617 return (bool) CRM_Core_DAO
::singleValueQuery("SHOW TRIGGERS LIKE 'civicrm_contact'");
622 * @param null $tableName
625 public function triggerInfo(&$info, $tableName = NULL, $force = FALSE) {
626 // check if we have logging enabled
627 $config =& CRM_Core_Config
::singleton();
628 if (!$config->logging
) {
632 $insert = array('INSERT');
633 $update = array('UPDATE');
634 $delete = array('DELETE');
637 $tableNames = array($tableName);
640 $tableNames = $this->tables
;
643 // logging is enabled, so now lets create the trigger info tables
644 foreach ($tableNames as $table) {
645 $columns = $this->columnsOf($table, $force);
647 // only do the change if any data has changed
649 foreach ($columns as $column) {
650 // ignore modified_date changes
651 if ($column != 'modified_date' && !in_array($column, CRM_Utils_Array
::value($table, $this->exceptions
, array()))) {
652 $cond[] = "IFNULL(OLD.$column,'') <> IFNULL(NEW.$column,'')";
655 $suppressLoggingCond = "@civicrm_disable_logging IS NULL OR @civicrm_disable_logging = 0";
656 $updateSQL = "IF ( (" . implode(' OR ', $cond) . ") AND ( $suppressLoggingCond ) ) THEN ";
658 if ($this->useDBPrefix
) {
659 $sqlStmt = "INSERT INTO `{$this->db}`.log_{tableName} (";
662 $sqlStmt = "INSERT INTO log_{tableName} (";
664 foreach ($columns as $column) {
665 $sqlStmt .= "$column, ";
667 $sqlStmt .= "log_conn_id, log_user_id, log_action) VALUES (";
669 $insertSQL = $deleteSQL = "IF ( $suppressLoggingCond ) THEN $sqlStmt ";
670 $updateSQL .= $sqlStmt;
673 foreach ($columns as $column) {
674 $sqlStmt .= "NEW.$column, ";
675 $deleteSQL .= "OLD.$column, ";
677 $sqlStmt .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}');";
678 $deleteSQL .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}');";
680 $sqlStmt .= "END IF;";
681 $deleteSQL .= "END IF;";
683 $insertSQL .= $sqlStmt;
684 $updateSQL .= $sqlStmt;
687 'table' => array($table),
694 'table' => array($table),
701 'table' => array($table),
710 * This allow logging to be temporarily disabled for certain cases
711 * where we want to do a mass cleanup but dont want to bother with
715 public static function disableLoggingForThisConnection() {
716 // do this only if logging is enabled
717 $config = CRM_Core_Config
::singleton();
718 if ($config->logging
) {
719 CRM_Core_DAO
::executeQuery('SET @civicrm_disable_logging = 1');