3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
35 class CRM_Logging_Schema
{
36 private $logs = array();
37 private $tables = array();
41 private $reports = array(
42 'logging/contact/detail',
43 'logging/contact/summary',
44 'logging/contribute/detail',
45 'logging/contribute/summary',
49 * Populate $this->tables and $this->logs with current db state.
51 function __construct() {
52 $dao = new CRM_Contact_DAO_Contact();
53 $civiDBName = $dao->_database
;
55 $dao = CRM_Core_DAO
::executeQuery("
57 FROM INFORMATION_SCHEMA.TABLES
58 WHERE TABLE_SCHEMA = '{$civiDBName}'
59 AND TABLE_TYPE = 'BASE TABLE'
60 AND TABLE_NAME LIKE 'civicrm_%'
62 while ($dao->fetch()) {
63 $this->tables
[] = $dao->TABLE_NAME
;
66 // do not log temp import, cache and log tables
67 $this->tables
= preg_grep('/^civicrm_import_job_/', $this->tables
, PREG_GREP_INVERT
);
68 $this->tables
= preg_grep('/_cache$/', $this->tables
, PREG_GREP_INVERT
);
69 $this->tables
= preg_grep('/_log/', $this->tables
, PREG_GREP_INVERT
);
70 $this->tables
= preg_grep('/^civicrm_task_action_temp_/', $this->tables
, PREG_GREP_INVERT
);
71 $this->tables
= preg_grep('/^civicrm_export_temp_/', $this->tables
, PREG_GREP_INVERT
);
72 $this->tables
= preg_grep('/^civicrm_queue_/', $this->tables
, PREG_GREP_INVERT
);
74 $dsn = defined('CIVICRM_LOGGING_DSN') ? DB
::parseDSN(CIVICRM_LOGGING_DSN
) : DB
::parseDSN(CIVICRM_DSN
);
75 $this->db
= $dsn['database'];
77 $dao = CRM_Core_DAO
::executeQuery("
79 FROM INFORMATION_SCHEMA.TABLES
80 WHERE TABLE_SCHEMA = '{$this->db}'
81 AND TABLE_TYPE = 'BASE TABLE'
82 AND TABLE_NAME LIKE 'log_civicrm_%'
84 while ($dao->fetch()) {
85 $log = $dao->TABLE_NAME
;
86 $this->logs
[substr($log, 4)] = $log;
91 * Return logging custom data tables.
93 function customDataLogTables() {
94 return preg_grep('/^log_civicrm_value_/', $this->logs
);
98 * Disable logging by dropping the triggers (but keep the log tables intact).
100 function disableLogging() {
101 $config = CRM_Core_Config
::singleton();
102 $config->logging
= FALSE;
104 $this->dropTriggers();
106 // invoke the meta trigger creation call
107 CRM_Core_DAO
::triggerRebuild();
109 $this->deleteReports();
113 * Drop triggers for all logged tables.
115 function dropTriggers($tableName = NULL) {
116 $dao = new CRM_Core_DAO
;
119 $tableNames = array($tableName);
122 $tableNames = $this->tables
;
125 foreach ($tableNames as $table) {
127 $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_before_insert");
128 $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_before_update");
129 $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_before_delete");
132 $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_after_insert");
133 $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_after_update");
134 $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_after_delete");
139 * Enable sitewide logging.
143 function enableLogging() {
144 $this->fixSchemaDifferences(TRUE);
149 * Sync log tables and rebuild triggers.
151 * @param bool $enableLogging: Ensure logging is enabled
155 function fixSchemaDifferences($enableLogging = FALSE) {
156 $config = CRM_Core_Config
::singleton();
157 if ($enableLogging) {
158 $config->logging
= TRUE;
160 if ($config->logging
) {
161 foreach ($this->schemaDifferences() as $table => $cols) {
162 $this->fixSchemaDifferencesFor($table, $cols, FALSE);
165 // invoke the meta trigger creation call
166 CRM_Core_DAO
::triggerRebuild();
170 * Add missing (potentially specified) log table columns for the given table.
172 * @param $table string name of the relevant table
173 * @param $cols mixed array of columns to add or null (to check for the missing columns)
174 * @param $rebuildTrigger boolean should we rebuild the triggers
178 function fixSchemaDifferencesFor($table, $cols = NULL, $rebuildTrigger = TRUE) {
179 if (empty($this->logs
[$table])) {
180 $this->createLogTableFor($table);
184 if (is_null($cols)) {
185 $cols = array_diff($this->columnsOf($table), $this->columnsOf("log_$table"));
191 // use the relevant lines from CREATE TABLE to add colums to the log table
192 $dao = CRM_Core_DAO
::executeQuery("SHOW CREATE TABLE $table");
194 $create = explode("\n", $dao->Create_Table
);
195 foreach ($cols as $col) {
196 $line = preg_grep("/^ `$col` /", $create);
197 $line = substr(array_pop($line), 0, -1);
199 $line = self
::fixTimeStampAndNotNullSQL($line);
201 CRM_Core_DAO
::executeQuery("ALTER TABLE `{$this->db}`.log_$table ADD $line");
204 if ($rebuildTrigger) {
205 // invoke the meta trigger creation call
206 CRM_Core_DAO
::triggerRebuild($table);
210 function fixTimeStampAndNotNullSQL($query) {
211 $query = str_ireplace("TIMESTAMP NOT NULL", "TIMESTAMP NULL", $query);
212 $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", '', $query);
213 $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP", '', $query);
214 $query = str_ireplace("NOT NULL", '', $query);
219 * Find missing log table columns by comparing columns of the relevant tables.
220 * Returns table-name-keyed array of arrays of missing columns, e.g. array('civicrm_value_foo_1' => array('bar_1', 'baz_2'))
222 function schemaDifferences() {
224 foreach ($this->tables
as $table) {
225 $diffs[$table] = array_diff($this->columnsOf($table), $this->columnsOf("log_$table"));
227 return array_filter($diffs);
230 private function addReports() {
232 'logging/contact/detail' => ts('Logging Details'),
233 'logging/contact/summary' => ts('Contact Logging Report (Summary)'),
234 'logging/contribute/detail' => ts('Contribution Logging Report (Detail)'),
235 'logging/contribute/summary' => ts('Contribution Logging Report (Summary)'),
237 // enable logging templates
238 CRM_Core_DAO
::executeQuery("
239 UPDATE civicrm_option_value
241 WHERE value IN ('" . implode("', '", $this->reports
) . "')
244 // add report instances
245 $domain_id = CRM_Core_Config
::domainID();
246 foreach ($this->reports
as $report) {
247 $dao = new CRM_Report_DAO_Instance
;
248 $dao->domain_id
= $domain_id;
249 $dao->report_id
= $report;
250 $dao->title
= $titles[$report];
251 $dao->permission
= 'administer CiviCRM';
252 if ($report == 'logging/contact/summary')
253 $dao->is_reserved
= 1;
259 * Get an array of column names of the given table.
261 private function columnsOf($table) {
262 static $columnsOf = array();
264 $from = (substr($table, 0, 4) == 'log_') ?
"`{$this->db}`.$table" : $table;
266 if (!isset($columnsOf[$table])) {
267 CRM_Core_Error
::ignoreException();
268 $dao = CRM_Core_DAO
::executeQuery("SHOW COLUMNS FROM $from");
269 CRM_Core_Error
::setCallback();
270 if (is_a($dao, 'DB_Error')) {
273 $columnsOf[$table] = array();
274 while ($dao->fetch()) {
275 $columnsOf[$table][] = $dao->Field
;
279 return $columnsOf[$table];
283 * Create a log table with schema mirroring the given table’s structure and seeding it with the given table’s contents.
285 private function createLogTableFor($table) {
286 $dao = CRM_Core_DAO
::executeQuery("SHOW CREATE TABLE $table");
288 $query = $dao->Create_Table
;
290 // rewrite the queries into CREATE TABLE queries for log tables:
292 log_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
295 log_action ENUM('Initialization', 'Insert', 'Update', 'Delete')
298 // - prepend the name with log_
299 // - drop AUTO_INCREMENT columns
300 // - drop non-column rows of the query (keys, constraints, etc.)
301 // - set the ENGINE to ARCHIVE
302 // - add log-specific columns (at the end of the table)
303 $query = preg_replace("/^CREATE TABLE `$table`/i", "CREATE TABLE `{$this->db}`.log_$table", $query);
304 $query = preg_replace("/ AUTO_INCREMENT/i", '', $query);
305 $query = preg_replace("/^ [^`].*$/m", '', $query);
306 $query = preg_replace("/^\) ENGINE=[^ ]+ /im", ') ENGINE=ARCHIVE ', $query);
308 // log_civicrm_contact.modified_date for example would always be copied from civicrm_contact.modified_date,
309 // so there's no need for a default timestamp and therefore we remove such default timestamps
310 // also eliminate the NOT NULL constraint, since we always copy and schema can change down the road)
311 $query = self
::fixTimeStampAndNotNullSQL($query);
312 $query = preg_replace("/^\) /m", "$cols\n) ", $query);
314 CRM_Core_DAO
::executeQuery($query);
316 $columns = implode(', ', $this->columnsOf($table));
317 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}");
319 $this->tables
[] = $table;
320 $this->logs
[$table] = "log_$table";
323 private function deleteReports() {
324 // disable logging templates
325 CRM_Core_DAO
::executeQuery("
326 UPDATE civicrm_option_value
328 WHERE value IN ('" . implode("', '", $this->reports
) . "')
331 // delete report instances
332 $domain_id = CRM_Core_Config
::domainID();
333 foreach ($this->reports
as $report) {
334 $dao = new CRM_Report_DAO_Instance
;
335 $dao->domain_id
= $domain_id;
336 $dao->report_id
= $report;
342 * Predicate whether logging is enabled.
344 public function isEnabled() {
345 $config = CRM_Core_Config
::singleton();
347 if ($config->logging
) {
348 return $this->tablesExist() and $this->triggersExist();
354 * Predicate whether any log tables exist.
356 private function tablesExist() {
357 return !empty($this->logs
);
361 * Predicate whether the logging triggers are in place.
363 private function triggersExist() {
364 // FIXME: probably should be a bit more thorough…
365 // note that the LIKE parameter is TABLE NAME
366 return (bool) CRM_Core_DAO
::singleValueQuery("SHOW TRIGGERS LIKE 'civicrm_contact'");
369 function triggerInfo(&$info, $tableName = NULL) {
370 // check if we have logging enabled
371 $config =& CRM_Core_Config
::singleton();
372 if (!$config->logging
) {
376 $insert = array('INSERT');
377 $update = array('UPDATE');
378 $delete = array('DELETE');
381 $tableNames = array($tableName);
384 $tableNames = $this->tables
;
387 // logging is enabled, so now lets create the trigger info tables
388 foreach ($tableNames as $table) {
389 $columns = $this->columnsOf($table);
391 // only do the change if any data has changed
393 foreach ($columns as $column) {
394 // ignore modified_date changes
395 if ($column != 'modified_date') {
396 $cond[] = "IFNULL(OLD.$column,'') <> IFNULL(NEW.$column,'')";
399 $suppressLoggingCond = "@civicrm_disable_logging IS NULL OR @civicrm_disable_logging = 0";
400 $updateSQL = "IF ( (" . implode( ' OR ', $cond ) . ") AND ( $suppressLoggingCond ) ) THEN ";
402 $sqlStmt = "INSERT INTO `{$this->db}`.log_{tableName} (";
403 foreach ($columns as $column) {
404 $sqlStmt .= "$column, ";
406 $sqlStmt .= "log_conn_id, log_user_id, log_action) VALUES (";
408 $insertSQL = $deleteSQL = "IF ( $suppressLoggingCond ) THEN $sqlStmt ";
409 $updateSQL .= $sqlStmt;
412 foreach ($columns as $column) {
413 $sqlStmt .= "NEW.$column, ";
414 $deleteSQL .= "OLD.$column, ";
416 $sqlStmt .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}');";
417 $deleteSQL .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}');";
419 $sqlStmt .= "END IF;";
420 $deleteSQL .= "END IF;";
422 $insertSQL .= $sqlStmt;
423 $updateSQL .= $sqlStmt;
426 'table' => array($table),
433 'table' => array($table),
440 'table' => array($table),
449 * This allow logging to be temporarily disabled for certain cases
450 * where we want to do a mass cleanup but dont want to bother with
456 static function disableLoggingForThisConnection( ) {
457 // do this only if logging is enabled
458 $config = CRM_Core_Config
::singleton( );
459 if ( $config->logging
) {
460 CRM_Core_DAO
::executeQuery( 'SET @civicrm_disable_logging = 1' );