Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
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 | ||
694e78fd DS |
114 | /** |
115 | * Return custom data tables for specified entity / extends. | |
116 | */ | |
117 | function entityCustomDataLogTables($extends) { | |
118 | $customGroupTables = array(); | |
119 | $customGroupDAO = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity($extends); | |
120 | $customGroupDAO->find(); | |
121 | while ($customGroupDAO->fetch()) { | |
122 | $customGroupTables[$customGroupDAO->table_name] = $this->logs[$customGroupDAO->table_name]; | |
123 | } | |
124 | return $customGroupTables; | |
125 | } | |
126 | ||
6a488035 TO |
127 | /** |
128 | * Disable logging by dropping the triggers (but keep the log tables intact). | |
129 | */ | |
130 | function disableLogging() { | |
131 | $config = CRM_Core_Config::singleton(); | |
132 | $config->logging = FALSE; | |
133 | ||
134 | $this->dropTriggers(); | |
135 | ||
136 | // invoke the meta trigger creation call | |
137 | CRM_Core_DAO::triggerRebuild(); | |
138 | ||
139 | $this->deleteReports(); | |
140 | } | |
141 | ||
142 | /** | |
143 | * Drop triggers for all logged tables. | |
144 | */ | |
145 | function dropTriggers($tableName = NULL) { | |
146 | $dao = new CRM_Core_DAO; | |
147 | ||
148 | if ($tableName) { | |
149 | $tableNames = array($tableName); | |
150 | } | |
151 | else { | |
152 | $tableNames = $this->tables; | |
153 | } | |
154 | ||
155 | foreach ($tableNames as $table) { | |
156 | // before triggers | |
157 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_before_insert"); | |
158 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_before_update"); | |
159 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_before_delete"); | |
160 | ||
161 | // after triggers | |
162 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_after_insert"); | |
163 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_after_update"); | |
164 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$table}_after_delete"); | |
165 | } | |
166 | } | |
167 | ||
168 | /** | |
169 | * Enable sitewide logging. | |
170 | * | |
171 | * @return void | |
172 | */ | |
173 | function enableLogging() { | |
174 | $this->fixSchemaDifferences(TRUE); | |
175 | $this->addReports(); | |
176 | } | |
177 | ||
178 | /** | |
179 | * Sync log tables and rebuild triggers. | |
180 | * | |
181 | * @param bool $enableLogging: Ensure logging is enabled | |
182 | * | |
183 | * @return void | |
184 | */ | |
185 | function fixSchemaDifferences($enableLogging = FALSE) { | |
186 | $config = CRM_Core_Config::singleton(); | |
187 | if ($enableLogging) { | |
188 | $config->logging = TRUE; | |
189 | } | |
190 | if ($config->logging) { | |
bfb723bb | 191 | $this->fixSchemaDifferencesForALL(); |
6a488035 TO |
192 | } |
193 | // invoke the meta trigger creation call | |
194 | CRM_Core_DAO::triggerRebuild(); | |
195 | } | |
196 | ||
197 | /** | |
198 | * Add missing (potentially specified) log table columns for the given table. | |
199 | * | |
200 | * @param $table string name of the relevant table | |
201 | * @param $cols mixed array of columns to add or null (to check for the missing columns) | |
202 | * @param $rebuildTrigger boolean should we rebuild the triggers | |
203 | * | |
204 | * @return void | |
205 | */ | |
bfb723bb DS |
206 | function fixSchemaDifferencesFor($table, $cols = array(), $rebuildTrigger = FALSE) { |
207 | if (empty($table)) { | |
208 | return FALSE; | |
209 | } | |
6a488035 TO |
210 | if (empty($this->logs[$table])) { |
211 | $this->createLogTableFor($table); | |
bfb723bb | 212 | return TRUE; |
6a488035 TO |
213 | } |
214 | ||
6a488035 | 215 | if (empty($cols)) { |
bfb723bb | 216 | $cols = $this->columnsWithDiffSpecs($table, "log_$table"); |
6a488035 TO |
217 | } |
218 | ||
219 | // use the relevant lines from CREATE TABLE to add colums to the log table | |
bfb723bb DS |
220 | $create = $this->_getCreateQuery($table); |
221 | foreach ((array('ADD', 'MODIFY')) as $alterType) { | |
12de149a DS |
222 | if (!empty($cols[$alterType])) { |
223 | foreach ($cols[$alterType] as $col) { | |
224 | $line = $this->_getColumnQuery($col, $create); | |
225 | CRM_Core_DAO::executeQuery("ALTER TABLE `{$this->db}`.log_$table {$alterType} {$line}"); | |
226 | } | |
bfb723bb DS |
227 | } |
228 | } | |
229 | ||
31c270e1 DS |
230 | // for any obsolete columns (not null) we just make the column nullable. |
231 | if (!empty($cols['OBSOLETE'])) { | |
bfb723bb | 232 | $create = $this->_getCreateQuery("log_{$table}"); |
31c270e1 | 233 | foreach ($cols['OBSOLETE'] as $col) { |
bfb723bb | 234 | $line = $this->_getColumnQuery($col, $create); |
31c270e1 DS |
235 | // This is just going to make a not null column to nullable |
236 | CRM_Core_DAO::executeQuery("ALTER TABLE `{$this->db}`.log_$table MODIFY {$line}"); | |
bfb723bb DS |
237 | } |
238 | } | |
239 | ||
240 | if ($rebuildTrigger) { | |
241 | // invoke the meta trigger creation call | |
242 | CRM_Core_DAO::triggerRebuild($table); | |
243 | } | |
244 | return TRUE; | |
245 | } | |
246 | ||
247 | private function _getCreateQuery($table) { | |
248 | $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE {$table}"); | |
6a488035 TO |
249 | $dao->fetch(); |
250 | $create = explode("\n", $dao->Create_Table); | |
bfb723bb DS |
251 | return $create; |
252 | } | |
6a488035 | 253 | |
bfb723bb DS |
254 | private function _getColumnQuery($col, $createQuery) { |
255 | $line = preg_grep("/^ `$col` /", $createQuery); | |
4a423b7a | 256 | $line = rtrim(array_pop($line), ','); |
bfb723bb | 257 | // CRM-11179 |
4a423b7a | 258 | $line = $this->fixTimeStampAndNotNullSQL($line); |
bfb723bb DS |
259 | return $line; |
260 | } | |
261 | ||
262 | function fixSchemaDifferencesForAll($rebuildTrigger = FALSE) { | |
263 | $diffs = array(); | |
bfb723bb DS |
264 | foreach ($this->tables as $table) { |
265 | if (empty($this->logs[$table])) { | |
266 | $this->createLogTableFor($table); | |
267 | } else { | |
268 | $diffs[$table] = $this->columnsWithDiffSpecs($table, "log_$table"); | |
269 | } | |
270 | } | |
6a488035 | 271 | |
bfb723bb DS |
272 | foreach ($diffs as $table => $cols) { |
273 | $this->fixSchemaDifferencesFor($table, $cols, FALSE); | |
6a488035 TO |
274 | } |
275 | ||
276 | if ($rebuildTrigger) { | |
277 | // invoke the meta trigger creation call | |
278 | CRM_Core_DAO::triggerRebuild($table); | |
279 | } | |
280 | } | |
281 | ||
bfb723bb DS |
282 | /* |
283 | * log_civicrm_contact.modified_date for example would always be copied from civicrm_contact.modified_date, | |
284 | * so there's no need for a default timestamp and therefore we remove such default timestamps | |
285 | * also eliminate the NOT NULL constraint, since we always copy and schema can change down the road) | |
286 | */ | |
c28241be | 287 | function fixTimeStampAndNotNullSQL($query) { |
6a488035 TO |
288 | $query = str_ireplace("TIMESTAMP NOT NULL", "TIMESTAMP NULL", $query); |
289 | $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", '', $query); | |
290 | $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP", '', $query); | |
c28241be | 291 | $query = str_ireplace("NOT NULL", '', $query); |
6a488035 TO |
292 | return $query; |
293 | } | |
294 | ||
6a488035 TO |
295 | private function addReports() { |
296 | $titles = array( | |
297 | 'logging/contact/detail' => ts('Logging Details'), | |
298 | 'logging/contact/summary' => ts('Contact Logging Report (Summary)'), | |
299 | 'logging/contribute/detail' => ts('Contribution Logging Report (Detail)'), | |
300 | 'logging/contribute/summary' => ts('Contribution Logging Report (Summary)'), | |
301 | ); | |
302 | // enable logging templates | |
303 | CRM_Core_DAO::executeQuery(" | |
304 | UPDATE civicrm_option_value | |
305 | SET is_active = 1 | |
306 | WHERE value IN ('" . implode("', '", $this->reports) . "') | |
307 | "); | |
308 | ||
309 | // add report instances | |
310 | $domain_id = CRM_Core_Config::domainID(); | |
311 | foreach ($this->reports as $report) { | |
0b25329b | 312 | $dao = new CRM_Report_DAO_ReportInstance; |
6a488035 TO |
313 | $dao->domain_id = $domain_id; |
314 | $dao->report_id = $report; | |
315 | $dao->title = $titles[$report]; | |
316 | $dao->permission = 'administer CiviCRM'; | |
317 | if ($report == 'logging/contact/summary') | |
318 | $dao->is_reserved = 1; | |
319 | $dao->insert(); | |
320 | } | |
321 | } | |
322 | ||
323 | /** | |
324 | * Get an array of column names of the given table. | |
325 | */ | |
326 | private function columnsOf($table) { | |
327 | static $columnsOf = array(); | |
328 | ||
329 | $from = (substr($table, 0, 4) == 'log_') ? "`{$this->db}`.$table" : $table; | |
330 | ||
331 | if (!isset($columnsOf[$table])) { | |
332 | CRM_Core_Error::ignoreException(); | |
333 | $dao = CRM_Core_DAO::executeQuery("SHOW COLUMNS FROM $from"); | |
334 | CRM_Core_Error::setCallback(); | |
335 | if (is_a($dao, 'DB_Error')) { | |
336 | return array(); | |
337 | } | |
338 | $columnsOf[$table] = array(); | |
339 | while ($dao->fetch()) { | |
340 | $columnsOf[$table][] = $dao->Field; | |
341 | } | |
342 | } | |
343 | ||
344 | return $columnsOf[$table]; | |
345 | } | |
346 | ||
bfb723bb DS |
347 | /** |
348 | * Get an array of columns and their details like DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT for the given table. | |
349 | */ | |
350 | private function columnSpecsOf($table) { | |
351 | static $columnSpecs = array(), $civiDB = NULL; | |
352 | ||
353 | if (empty($columnSpecs)) { | |
354 | if (!$civiDB) { | |
355 | $dao = new CRM_Contact_DAO_Contact(); | |
356 | $civiDB = $dao->_database; | |
357 | } | |
358 | CRM_Core_Error::ignoreException(); | |
359 | // NOTE: W.r.t Performance using one query to find all details and storing in static array is much faster | |
360 | // than firing query for every given table. | |
361 | $query = " | |
362 | SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT | |
363 | FROM INFORMATION_SCHEMA.COLUMNS | |
364 | WHERE table_schema IN ('{$this->db}', '{$civiDB}')"; | |
bfb723bb | 365 | $dao = CRM_Core_DAO::executeQuery($query); |
bfb723bb DS |
366 | CRM_Core_Error::setCallback(); |
367 | if (is_a($dao, 'DB_Error')) { | |
368 | return array(); | |
369 | } | |
370 | while ($dao->fetch()) { | |
bfb723bb DS |
371 | if (!array_key_exists($dao->TABLE_NAME, $columnSpecs)) { |
372 | $columnSpecs[$dao->TABLE_NAME] = array(); | |
373 | } | |
374 | $columnSpecs[$dao->TABLE_NAME][$dao->COLUMN_NAME] = | |
375 | array( | |
376 | 'COLUMN_NAME' => $dao->COLUMN_NAME, | |
377 | 'DATA_TYPE' => $dao->DATA_TYPE, | |
378 | 'IS_NULLABLE' => $dao->IS_NULLABLE, | |
379 | 'COLUMN_DEFAULT' => $dao->COLUMN_DEFAULT | |
380 | ); | |
381 | } | |
bfb723bb | 382 | } |
bfb723bb DS |
383 | return $columnSpecs[$table]; |
384 | } | |
385 | ||
4a423b7a DS |
386 | function columnsWithDiffSpecs($civiTable, $logTable) { |
387 | $civiTableSpecs = $this->columnSpecsOf($civiTable); | |
388 | $logTableSpecs = $this->columnSpecsOf($logTable); | |
bfb723bb | 389 | |
31c270e1 | 390 | $diff = array('ADD' => array(), 'MODIFY' => array(), 'OBSOLETE' => array()); |
4a423b7a DS |
391 | |
392 | // columns to be added | |
393 | $diff['ADD'] = array_diff(array_keys($civiTableSpecs), array_keys($logTableSpecs)); | |
394 | ||
395 | // columns to be modified | |
396 | // NOTE: we consider only those columns for modifications where there is a spec change, and that the column definition | |
397 | // wasn't deliberately modified by fixTimeStampAndNotNullSQL() method. | |
398 | foreach ($civiTableSpecs as $col => $colSpecs) { | |
cd71572a | 399 | $specDiff = array_diff($civiTableSpecs[$col], $logTableSpecs[$col]); |
12de149a | 400 | if (!empty($specDiff) && $col != 'id' && !array_key_exists($col, $diff['ADD'])) { |
4a423b7a DS |
401 | // ignore 'id' column for any spec changes, to avoid any auto-increment mysql errors |
402 | if ($civiTableSpecs[$col]['DATA_TYPE'] != $logTableSpecs[$col]['DATA_TYPE']) { | |
403 | // if data-type is different, surely consider the column | |
404 | $diff['MODIFY'][] = $col; | |
405 | } else if ($civiTableSpecs[$col]['IS_NULLABLE'] != $logTableSpecs[$col]['IS_NULLABLE'] && | |
406 | $logTableSpecs[$col]['IS_NULLABLE'] == 'NO') { | |
407 | // if is-null property is different, and log table's column is NOT-NULL, surely consider the column | |
408 | $diff['MODIFY'][] = $col; | |
409 | } else if ($civiTableSpecs[$col]['COLUMN_DEFAULT'] != $logTableSpecs[$col]['COLUMN_DEFAULT'] && | |
410 | !strstr($civiTableSpecs[$col]['COLUMN_DEFAULT'], 'TIMESTAMP')) { | |
411 | // if default property is different, and its not about a timestamp column, consider it | |
412 | $diff['MODIFY'][] = $col; | |
413 | } | |
bfb723bb DS |
414 | } |
415 | } | |
416 | ||
4a423b7a DS |
417 | // columns to made obsolete by turning into not-null |
418 | $oldCols = array_diff(array_keys($logTableSpecs), array_keys($civiTableSpecs)); | |
31c270e1 DS |
419 | foreach ($oldCols as $col) { |
420 | if (!in_array($col, array('log_date', 'log_conn_id', 'log_user_id', 'log_action')) && | |
4a423b7a DS |
421 | $logTableSpecs[$col]['IS_NULLABLE'] == 'NO') { |
422 | // if its a column present only in log table, not among those used by log tables for special purpose, and not-null | |
31c270e1 | 423 | $diff['OBSOLETE'][] = $col; |
bfb723bb DS |
424 | } |
425 | } | |
426 | ||
427 | return $diff; | |
428 | } | |
429 | ||
6a488035 TO |
430 | /** |
431 | * Create a log table with schema mirroring the given table’s structure and seeding it with the given table’s contents. | |
432 | */ | |
433 | private function createLogTableFor($table) { | |
434 | $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE $table"); | |
435 | $dao->fetch(); | |
436 | $query = $dao->Create_Table; | |
437 | ||
438 | // rewrite the queries into CREATE TABLE queries for log tables: | |
6a488035 TO |
439 | $cols = <<<COLS |
440 | log_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | |
441 | log_conn_id INTEGER, | |
442 | log_user_id INTEGER, | |
443 | log_action ENUM('Initialization', 'Insert', 'Update', 'Delete') | |
444 | COLS; | |
c28241be DL |
445 | |
446 | // - prepend the name with log_ | |
447 | // - drop AUTO_INCREMENT columns | |
448 | // - drop non-column rows of the query (keys, constraints, etc.) | |
449 | // - set the ENGINE to ARCHIVE | |
450 | // - add log-specific columns (at the end of the table) | |
6a488035 TO |
451 | $query = preg_replace("/^CREATE TABLE `$table`/i", "CREATE TABLE `{$this->db}`.log_$table", $query); |
452 | $query = preg_replace("/ AUTO_INCREMENT/i", '', $query); | |
453 | $query = preg_replace("/^ [^`].*$/m", '', $query); | |
454 | $query = preg_replace("/^\) ENGINE=[^ ]+ /im", ') ENGINE=ARCHIVE ', $query); | |
c28241be | 455 | |
6a488035 | 456 | // log_civicrm_contact.modified_date for example would always be copied from civicrm_contact.modified_date, |
c28241be DL |
457 | // so there's no need for a default timestamp and therefore we remove such default timestamps |
458 | // also eliminate the NOT NULL constraint, since we always copy and schema can change down the road) | |
459 | $query = self::fixTimeStampAndNotNullSQL($query); | |
6a488035 TO |
460 | $query = preg_replace("/^\) /m", "$cols\n) ", $query); |
461 | ||
462 | CRM_Core_DAO::executeQuery($query); | |
463 | ||
464 | $columns = implode(', ', $this->columnsOf($table)); | |
465 | 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}"); | |
466 | ||
467 | $this->tables[] = $table; | |
468 | $this->logs[$table] = "log_$table"; | |
469 | } | |
470 | ||
471 | private function deleteReports() { | |
472 | // disable logging templates | |
473 | CRM_Core_DAO::executeQuery(" | |
474 | UPDATE civicrm_option_value | |
475 | SET is_active = 0 | |
476 | WHERE value IN ('" . implode("', '", $this->reports) . "') | |
477 | "); | |
478 | ||
479 | // delete report instances | |
480 | $domain_id = CRM_Core_Config::domainID(); | |
481 | foreach ($this->reports as $report) { | |
0b25329b | 482 | $dao = new CRM_Report_DAO_ReportInstance; |
6a488035 TO |
483 | $dao->domain_id = $domain_id; |
484 | $dao->report_id = $report; | |
485 | $dao->delete(); | |
486 | } | |
487 | } | |
488 | ||
489 | /** | |
490 | * Predicate whether logging is enabled. | |
491 | */ | |
492 | public function isEnabled() { | |
493 | $config = CRM_Core_Config::singleton(); | |
494 | ||
495 | if ($config->logging) { | |
496 | return $this->tablesExist() and $this->triggersExist(); | |
497 | } | |
498 | return FALSE; | |
499 | } | |
500 | ||
501 | /** | |
502 | * Predicate whether any log tables exist. | |
503 | */ | |
504 | private function tablesExist() { | |
505 | return !empty($this->logs); | |
506 | } | |
507 | ||
508 | /** | |
509 | * Predicate whether the logging triggers are in place. | |
510 | */ | |
511 | private function triggersExist() { | |
512 | // FIXME: probably should be a bit more thorough… | |
513 | // note that the LIKE parameter is TABLE NAME | |
514 | return (bool) CRM_Core_DAO::singleValueQuery("SHOW TRIGGERS LIKE 'civicrm_contact'"); | |
515 | } | |
516 | ||
517 | function triggerInfo(&$info, $tableName = NULL) { | |
518 | // check if we have logging enabled | |
519 | $config =& CRM_Core_Config::singleton(); | |
520 | if (!$config->logging) { | |
521 | return; | |
522 | } | |
523 | ||
524 | $insert = array('INSERT'); | |
525 | $update = array('UPDATE'); | |
526 | $delete = array('DELETE'); | |
527 | ||
528 | if ($tableName) { | |
529 | $tableNames = array($tableName); | |
530 | } | |
531 | else { | |
532 | $tableNames = $this->tables; | |
533 | } | |
534 | ||
535 | // logging is enabled, so now lets create the trigger info tables | |
536 | foreach ($tableNames as $table) { | |
537 | $columns = $this->columnsOf($table); | |
538 | ||
539 | // only do the change if any data has changed | |
540 | $cond = array( ); | |
541 | foreach ($columns as $column) { | |
542 | // ignore modified_date changes | |
4d1040bb | 543 | if ($column != 'modified_date' && !in_array($column, CRM_Utils_Array::value($table, $this->exceptions, array()))) { |
6a488035 TO |
544 | $cond[] = "IFNULL(OLD.$column,'') <> IFNULL(NEW.$column,'')"; |
545 | } | |
546 | } | |
547 | $suppressLoggingCond = "@civicrm_disable_logging IS NULL OR @civicrm_disable_logging = 0"; | |
548 | $updateSQL = "IF ( (" . implode( ' OR ', $cond ) . ") AND ( $suppressLoggingCond ) ) THEN "; | |
549 | ||
340f6aa0 DL |
550 | if ($this->useDBPrefix) { |
551 | $sqlStmt = "INSERT INTO `{$this->db}`.log_{tableName} ("; | |
552 | } | |
553 | else { | |
554 | $sqlStmt = "INSERT INTO log_{tableName} ("; | |
555 | } | |
6a488035 TO |
556 | foreach ($columns as $column) { |
557 | $sqlStmt .= "$column, "; | |
558 | } | |
559 | $sqlStmt .= "log_conn_id, log_user_id, log_action) VALUES ("; | |
560 | ||
561 | $insertSQL = $deleteSQL = "IF ( $suppressLoggingCond ) THEN $sqlStmt "; | |
562 | $updateSQL .= $sqlStmt; | |
563 | ||
564 | $sqlStmt = ''; | |
565 | foreach ($columns as $column) { | |
566 | $sqlStmt .= "NEW.$column, "; | |
567 | $deleteSQL .= "OLD.$column, "; | |
568 | } | |
569 | $sqlStmt .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}');"; | |
570 | $deleteSQL .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}');"; | |
571 | ||
572 | $sqlStmt .= "END IF;"; | |
573 | $deleteSQL .= "END IF;"; | |
574 | ||
575 | $insertSQL .= $sqlStmt; | |
576 | $updateSQL .= $sqlStmt; | |
577 | ||
578 | $info[] = array( | |
579 | 'table' => array($table), | |
580 | 'when' => 'AFTER', | |
581 | 'event' => $insert, | |
582 | 'sql' => $insertSQL, | |
583 | ); | |
584 | ||
585 | $info[] = array( | |
586 | 'table' => array($table), | |
587 | 'when' => 'AFTER', | |
588 | 'event' => $update, | |
589 | 'sql' => $updateSQL, | |
590 | ); | |
591 | ||
592 | $info[] = array( | |
593 | 'table' => array($table), | |
594 | 'when' => 'AFTER', | |
595 | 'event' => $delete, | |
596 | 'sql' => $deleteSQL, | |
597 | ); | |
598 | } | |
599 | } | |
600 | ||
601 | /** | |
602 | * This allow logging to be temporarily disabled for certain cases | |
603 | * where we want to do a mass cleanup but dont want to bother with | |
604 | * an audit trail | |
605 | * | |
606 | * @static | |
607 | * @public | |
608 | */ | |
609 | static function disableLoggingForThisConnection( ) { | |
610 | // do this only if logging is enabled | |
611 | $config = CRM_Core_Config::singleton( ); | |
612 | if ( $config->logging ) { | |
613 | CRM_Core_DAO::executeQuery( 'SET @civicrm_disable_logging = 1' ); | |
614 | } | |
615 | } | |
616 | ||
617 | } | |
618 |