Commit | Line | Data |
---|---|---|
6a488035 TO |
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; | |
340f6aa0 | 40 | private $useDBPrefix = TRUE; |
6a488035 TO |
41 | |
42 | private $reports = array( | |
43 | 'logging/contact/detail', | |
44 | 'logging/contact/summary', | |
45 | 'logging/contribute/detail', | |
46 | 'logging/contribute/summary', | |
47 | ); | |
48 | ||
4d1040bb DS |
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'), | |
53 | ); | |
54 | ||
6a488035 TO |
55 | /** |
56 | * Populate $this->tables and $this->logs with current db state. | |
57 | */ | |
58 | function __construct() { | |
59 | $dao = new CRM_Contact_DAO_Contact(); | |
60 | $civiDBName = $dao->_database; | |
61 | ||
62 | $dao = CRM_Core_DAO::executeQuery(" | |
63 | SELECT TABLE_NAME | |
64 | FROM INFORMATION_SCHEMA.TABLES | |
65 | WHERE TABLE_SCHEMA = '{$civiDBName}' | |
66 | AND TABLE_TYPE = 'BASE TABLE' | |
67 | AND TABLE_NAME LIKE 'civicrm_%' | |
68 | "); | |
69 | while ($dao->fetch()) { | |
70 | $this->tables[] = $dao->TABLE_NAME; | |
71 | } | |
72 | ||
73 | // do not log temp import, cache and log tables | |
74 | $this->tables = preg_grep('/^civicrm_import_job_/', $this->tables, PREG_GREP_INVERT); | |
75 | $this->tables = preg_grep('/_cache$/', $this->tables, PREG_GREP_INVERT); | |
76 | $this->tables = preg_grep('/_log/', $this->tables, PREG_GREP_INVERT); | |
77 | $this->tables = preg_grep('/^civicrm_task_action_temp_/', $this->tables, PREG_GREP_INVERT); | |
78 | $this->tables = preg_grep('/^civicrm_export_temp_/', $this->tables, PREG_GREP_INVERT); | |
79 | $this->tables = preg_grep('/^civicrm_queue_/', $this->tables, PREG_GREP_INVERT); | |
80 | ||
f9b793b0 DL |
81 | // do not log civicrm_mailing_event* tables, CRM-12300 |
82 | $this->tables = preg_grep('/^civicrm_mailing_event_/', $this->tables, PREG_GREP_INVERT); | |
83 | ||
340f6aa0 DL |
84 | if (defined('CIVICRM_LOGGING_DSN')) { |
85 | $dsn = DB::parseDSN(CIVICRM_LOGGING_DSN); | |
86 | $this->useDBPrefix = (CIVICRM_LOGGING_DSN != CIVICRM_DSN); | |
87 | } | |
88 | else { | |
89 | $dsn = DB::parseDSN(CIVICRM_DSN); | |
90 | $this->useDBPrefix = FALSE; | |
91 | } | |
6a488035 TO |
92 | $this->db = $dsn['database']; |
93 | ||
94 | $dao = CRM_Core_DAO::executeQuery(" | |
95 | SELECT TABLE_NAME | |
96 | FROM INFORMATION_SCHEMA.TABLES | |
97 | WHERE TABLE_SCHEMA = '{$this->db}' | |
98 | AND TABLE_TYPE = 'BASE TABLE' | |
99 | AND TABLE_NAME LIKE 'log_civicrm_%' | |
100 | "); | |
101 | while ($dao->fetch()) { | |
102 | $log = $dao->TABLE_NAME; | |
103 | $this->logs[substr($log, 4)] = $log; | |
104 | } | |
105 | } | |
106 | ||
107 | /** | |
108 | * Return logging custom data tables. | |
109 | */ | |
110 | function customDataLogTables() { | |
111 | return preg_grep('/^log_civicrm_value_/', $this->logs); | |
112 | } | |
113 | ||
114 | /** | |
115 | * Disable logging by dropping the triggers (but keep the log tables intact). | |
116 | */ | |
117 | function disableLogging() { | |
118 | $config = CRM_Core_Config::singleton(); | |
119 | $config->logging = FALSE; | |
120 | ||
121 | $this->dropTriggers(); | |
122 | ||
123 | // invoke the meta trigger creation call | |
124 | CRM_Core_DAO::triggerRebuild(); | |
125 | ||
126 | $this->deleteReports(); | |
127 | } | |
128 | ||
129 | /** | |
130 | * Drop triggers for all logged tables. | |
131 | */ | |
132 | function dropTriggers($tableName = NULL) { | |
133 | $dao = new CRM_Core_DAO; | |
134 | ||
135 | if ($tableName) { | |
136 | $tableNames = array($tableName); | |
137 | } | |
138 | else { | |
139 | $tableNames = $this->tables; | |
140 | } | |
141 | ||
142 | foreach ($tableNames as $table) { | |
143 | // before triggers | |
144 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_before_insert"); | |
145 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_before_update"); | |
146 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_before_delete"); | |
147 | ||
148 | // after triggers | |
149 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_after_insert"); | |
150 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_after_update"); | |
151 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_after_delete"); | |
152 | } | |
153 | } | |
154 | ||
155 | /** | |
156 | * Enable sitewide logging. | |
157 | * | |
158 | * @return void | |
159 | */ | |
160 | function enableLogging() { | |
161 | $this->fixSchemaDifferences(TRUE); | |
162 | $this->addReports(); | |
163 | } | |
164 | ||
165 | /** | |
166 | * Sync log tables and rebuild triggers. | |
167 | * | |
168 | * @param bool $enableLogging: Ensure logging is enabled | |
169 | * | |
170 | * @return void | |
171 | */ | |
172 | function fixSchemaDifferences($enableLogging = FALSE) { | |
173 | $config = CRM_Core_Config::singleton(); | |
174 | if ($enableLogging) { | |
175 | $config->logging = TRUE; | |
176 | } | |
177 | if ($config->logging) { | |
178 | foreach ($this->schemaDifferences() as $table => $cols) { | |
179 | $this->fixSchemaDifferencesFor($table, $cols, FALSE); | |
180 | } | |
181 | } | |
182 | // invoke the meta trigger creation call | |
183 | CRM_Core_DAO::triggerRebuild(); | |
184 | } | |
185 | ||
186 | /** | |
187 | * Add missing (potentially specified) log table columns for the given table. | |
188 | * | |
189 | * @param $table string name of the relevant table | |
190 | * @param $cols mixed array of columns to add or null (to check for the missing columns) | |
191 | * @param $rebuildTrigger boolean should we rebuild the triggers | |
192 | * | |
193 | * @return void | |
194 | */ | |
195 | function fixSchemaDifferencesFor($table, $cols = NULL, $rebuildTrigger = TRUE) { | |
196 | if (empty($this->logs[$table])) { | |
197 | $this->createLogTableFor($table); | |
198 | return; | |
199 | } | |
200 | ||
201 | if (is_null($cols)) { | |
202 | $cols = array_diff($this->columnsOf($table), $this->columnsOf("log_$table")); | |
203 | } | |
204 | if (empty($cols)) { | |
205 | return; | |
206 | } | |
207 | ||
208 | // use the relevant lines from CREATE TABLE to add colums to the log table | |
209 | $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE $table"); | |
210 | $dao->fetch(); | |
211 | $create = explode("\n", $dao->Create_Table); | |
212 | foreach ($cols as $col) { | |
213 | $line = preg_grep("/^ `$col` /", $create); | |
214 | $line = substr(array_pop($line), 0, -1); | |
215 | // CRM-11179 | |
c28241be | 216 | $line = self::fixTimeStampAndNotNullSQL($line); |
6a488035 TO |
217 | |
218 | CRM_Core_DAO::executeQuery("ALTER TABLE `{$this->db}`.log_$table ADD $line"); | |
219 | } | |
220 | ||
221 | if ($rebuildTrigger) { | |
222 | // invoke the meta trigger creation call | |
223 | CRM_Core_DAO::triggerRebuild($table); | |
224 | } | |
225 | } | |
226 | ||
c28241be | 227 | function fixTimeStampAndNotNullSQL($query) { |
6a488035 TO |
228 | $query = str_ireplace("TIMESTAMP NOT NULL", "TIMESTAMP NULL", $query); |
229 | $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", '', $query); | |
230 | $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP", '', $query); | |
c28241be | 231 | $query = str_ireplace("NOT NULL", '', $query); |
6a488035 TO |
232 | return $query; |
233 | } | |
234 | ||
235 | /** | |
236 | * Find missing log table columns by comparing columns of the relevant tables. | |
237 | * Returns table-name-keyed array of arrays of missing columns, e.g. array('civicrm_value_foo_1' => array('bar_1', 'baz_2')) | |
238 | */ | |
239 | function schemaDifferences() { | |
240 | $diffs = array(); | |
241 | foreach ($this->tables as $table) { | |
242 | $diffs[$table] = array_diff($this->columnsOf($table), $this->columnsOf("log_$table")); | |
243 | } | |
244 | return array_filter($diffs); | |
245 | } | |
246 | ||
247 | private function addReports() { | |
248 | $titles = array( | |
249 | 'logging/contact/detail' => ts('Logging Details'), | |
250 | 'logging/contact/summary' => ts('Contact Logging Report (Summary)'), | |
251 | 'logging/contribute/detail' => ts('Contribution Logging Report (Detail)'), | |
252 | 'logging/contribute/summary' => ts('Contribution Logging Report (Summary)'), | |
253 | ); | |
254 | // enable logging templates | |
255 | CRM_Core_DAO::executeQuery(" | |
256 | UPDATE civicrm_option_value | |
257 | SET is_active = 1 | |
258 | WHERE value IN ('" . implode("', '", $this->reports) . "') | |
259 | "); | |
260 | ||
261 | // add report instances | |
262 | $domain_id = CRM_Core_Config::domainID(); | |
263 | foreach ($this->reports as $report) { | |
0b25329b | 264 | $dao = new CRM_Report_DAO_ReportInstance; |
6a488035 TO |
265 | $dao->domain_id = $domain_id; |
266 | $dao->report_id = $report; | |
267 | $dao->title = $titles[$report]; | |
268 | $dao->permission = 'administer CiviCRM'; | |
269 | if ($report == 'logging/contact/summary') | |
270 | $dao->is_reserved = 1; | |
271 | $dao->insert(); | |
272 | } | |
273 | } | |
274 | ||
275 | /** | |
276 | * Get an array of column names of the given table. | |
277 | */ | |
278 | private function columnsOf($table) { | |
279 | static $columnsOf = array(); | |
280 | ||
281 | $from = (substr($table, 0, 4) == 'log_') ? "`{$this->db}`.$table" : $table; | |
282 | ||
283 | if (!isset($columnsOf[$table])) { | |
284 | CRM_Core_Error::ignoreException(); | |
285 | $dao = CRM_Core_DAO::executeQuery("SHOW COLUMNS FROM $from"); | |
286 | CRM_Core_Error::setCallback(); | |
287 | if (is_a($dao, 'DB_Error')) { | |
288 | return array(); | |
289 | } | |
290 | $columnsOf[$table] = array(); | |
291 | while ($dao->fetch()) { | |
292 | $columnsOf[$table][] = $dao->Field; | |
293 | } | |
294 | } | |
295 | ||
296 | return $columnsOf[$table]; | |
297 | } | |
298 | ||
299 | /** | |
300 | * Create a log table with schema mirroring the given table’s structure and seeding it with the given table’s contents. | |
301 | */ | |
302 | private function createLogTableFor($table) { | |
303 | $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE $table"); | |
304 | $dao->fetch(); | |
305 | $query = $dao->Create_Table; | |
306 | ||
307 | // rewrite the queries into CREATE TABLE queries for log tables: | |
6a488035 TO |
308 | $cols = <<<COLS |
309 | log_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | |
310 | log_conn_id INTEGER, | |
311 | log_user_id INTEGER, | |
312 | log_action ENUM('Initialization', 'Insert', 'Update', 'Delete') | |
313 | COLS; | |
c28241be DL |
314 | |
315 | // - prepend the name with log_ | |
316 | // - drop AUTO_INCREMENT columns | |
317 | // - drop non-column rows of the query (keys, constraints, etc.) | |
318 | // - set the ENGINE to ARCHIVE | |
319 | // - add log-specific columns (at the end of the table) | |
6a488035 TO |
320 | $query = preg_replace("/^CREATE TABLE `$table`/i", "CREATE TABLE `{$this->db}`.log_$table", $query); |
321 | $query = preg_replace("/ AUTO_INCREMENT/i", '', $query); | |
322 | $query = preg_replace("/^ [^`].*$/m", '', $query); | |
323 | $query = preg_replace("/^\) ENGINE=[^ ]+ /im", ') ENGINE=ARCHIVE ', $query); | |
c28241be | 324 | |
6a488035 | 325 | // log_civicrm_contact.modified_date for example would always be copied from civicrm_contact.modified_date, |
c28241be DL |
326 | // so there's no need for a default timestamp and therefore we remove such default timestamps |
327 | // also eliminate the NOT NULL constraint, since we always copy and schema can change down the road) | |
328 | $query = self::fixTimeStampAndNotNullSQL($query); | |
6a488035 TO |
329 | $query = preg_replace("/^\) /m", "$cols\n) ", $query); |
330 | ||
331 | CRM_Core_DAO::executeQuery($query); | |
332 | ||
333 | $columns = implode(', ', $this->columnsOf($table)); | |
334 | 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}"); | |
335 | ||
336 | $this->tables[] = $table; | |
337 | $this->logs[$table] = "log_$table"; | |
338 | } | |
339 | ||
340 | private function deleteReports() { | |
341 | // disable logging templates | |
342 | CRM_Core_DAO::executeQuery(" | |
343 | UPDATE civicrm_option_value | |
344 | SET is_active = 0 | |
345 | WHERE value IN ('" . implode("', '", $this->reports) . "') | |
346 | "); | |
347 | ||
348 | // delete report instances | |
349 | $domain_id = CRM_Core_Config::domainID(); | |
350 | foreach ($this->reports as $report) { | |
0b25329b | 351 | $dao = new CRM_Report_DAO_ReportInstance; |
6a488035 TO |
352 | $dao->domain_id = $domain_id; |
353 | $dao->report_id = $report; | |
354 | $dao->delete(); | |
355 | } | |
356 | } | |
357 | ||
358 | /** | |
359 | * Predicate whether logging is enabled. | |
360 | */ | |
361 | public function isEnabled() { | |
362 | $config = CRM_Core_Config::singleton(); | |
363 | ||
364 | if ($config->logging) { | |
365 | return $this->tablesExist() and $this->triggersExist(); | |
366 | } | |
367 | return FALSE; | |
368 | } | |
369 | ||
370 | /** | |
371 | * Predicate whether any log tables exist. | |
372 | */ | |
373 | private function tablesExist() { | |
374 | return !empty($this->logs); | |
375 | } | |
376 | ||
377 | /** | |
378 | * Predicate whether the logging triggers are in place. | |
379 | */ | |
380 | private function triggersExist() { | |
381 | // FIXME: probably should be a bit more thorough… | |
382 | // note that the LIKE parameter is TABLE NAME | |
383 | return (bool) CRM_Core_DAO::singleValueQuery("SHOW TRIGGERS LIKE 'civicrm_contact'"); | |
384 | } | |
385 | ||
386 | function triggerInfo(&$info, $tableName = NULL) { | |
387 | // check if we have logging enabled | |
388 | $config =& CRM_Core_Config::singleton(); | |
389 | if (!$config->logging) { | |
390 | return; | |
391 | } | |
392 | ||
393 | $insert = array('INSERT'); | |
394 | $update = array('UPDATE'); | |
395 | $delete = array('DELETE'); | |
396 | ||
397 | if ($tableName) { | |
398 | $tableNames = array($tableName); | |
399 | } | |
400 | else { | |
401 | $tableNames = $this->tables; | |
402 | } | |
403 | ||
404 | // logging is enabled, so now lets create the trigger info tables | |
405 | foreach ($tableNames as $table) { | |
406 | $columns = $this->columnsOf($table); | |
407 | ||
408 | // only do the change if any data has changed | |
409 | $cond = array( ); | |
410 | foreach ($columns as $column) { | |
411 | // ignore modified_date changes | |
4d1040bb | 412 | if ($column != 'modified_date' && !in_array($column, CRM_Utils_Array::value($table, $this->exceptions, array()))) { |
6a488035 TO |
413 | $cond[] = "IFNULL(OLD.$column,'') <> IFNULL(NEW.$column,'')"; |
414 | } | |
415 | } | |
416 | $suppressLoggingCond = "@civicrm_disable_logging IS NULL OR @civicrm_disable_logging = 0"; | |
417 | $updateSQL = "IF ( (" . implode( ' OR ', $cond ) . ") AND ( $suppressLoggingCond ) ) THEN "; | |
418 | ||
340f6aa0 DL |
419 | if ($this->useDBPrefix) { |
420 | $sqlStmt = "INSERT INTO `{$this->db}`.log_{tableName} ("; | |
421 | } | |
422 | else { | |
423 | $sqlStmt = "INSERT INTO log_{tableName} ("; | |
424 | } | |
6a488035 TO |
425 | foreach ($columns as $column) { |
426 | $sqlStmt .= "$column, "; | |
427 | } | |
428 | $sqlStmt .= "log_conn_id, log_user_id, log_action) VALUES ("; | |
429 | ||
430 | $insertSQL = $deleteSQL = "IF ( $suppressLoggingCond ) THEN $sqlStmt "; | |
431 | $updateSQL .= $sqlStmt; | |
432 | ||
433 | $sqlStmt = ''; | |
434 | foreach ($columns as $column) { | |
435 | $sqlStmt .= "NEW.$column, "; | |
436 | $deleteSQL .= "OLD.$column, "; | |
437 | } | |
438 | $sqlStmt .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}');"; | |
439 | $deleteSQL .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}');"; | |
440 | ||
441 | $sqlStmt .= "END IF;"; | |
442 | $deleteSQL .= "END IF;"; | |
443 | ||
444 | $insertSQL .= $sqlStmt; | |
445 | $updateSQL .= $sqlStmt; | |
446 | ||
447 | $info[] = array( | |
448 | 'table' => array($table), | |
449 | 'when' => 'AFTER', | |
450 | 'event' => $insert, | |
451 | 'sql' => $insertSQL, | |
452 | ); | |
453 | ||
454 | $info[] = array( | |
455 | 'table' => array($table), | |
456 | 'when' => 'AFTER', | |
457 | 'event' => $update, | |
458 | 'sql' => $updateSQL, | |
459 | ); | |
460 | ||
461 | $info[] = array( | |
462 | 'table' => array($table), | |
463 | 'when' => 'AFTER', | |
464 | 'event' => $delete, | |
465 | 'sql' => $deleteSQL, | |
466 | ); | |
467 | } | |
468 | } | |
469 | ||
470 | /** | |
471 | * This allow logging to be temporarily disabled for certain cases | |
472 | * where we want to do a mass cleanup but dont want to bother with | |
473 | * an audit trail | |
474 | * | |
475 | * @static | |
476 | * @public | |
477 | */ | |
478 | static function disableLoggingForThisConnection( ) { | |
479 | // do this only if logging is enabled | |
480 | $config = CRM_Core_Config::singleton( ); | |
481 | if ( $config->logging ) { | |
482 | CRM_Core_DAO::executeQuery( 'SET @civicrm_disable_logging = 1' ); | |
483 | } | |
484 | } | |
485 | ||
486 | } | |
487 |