Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | class CRM_Logging_Schema { | |
34 | private $logs = array(); | |
35 | private $tables = array(); | |
36 | ||
37 | private $db; | |
340f6aa0 | 38 | private $useDBPrefix = TRUE; |
6a488035 TO |
39 | |
40 | private $reports = array( | |
41 | 'logging/contact/detail', | |
42 | 'logging/contact/summary', | |
43 | 'logging/contribute/detail', | |
44 | 'logging/contribute/summary', | |
45 | ); | |
46 | ||
74b348da | 47 | /** |
48 | * Columns that should never be subject to logging. | |
49 | * | |
50 | * CRM-13028 / NYSS-6933 - table => array (cols) - to be excluded from the update statement | |
51 | * | |
52 | * @var array | |
53 | */ | |
4d1040bb | 54 | private $exceptions = array( |
353ffa53 | 55 | 'civicrm_job' => array('last_run'), |
b5c82b2d | 56 | 'civicrm_group' => array('cache_date', 'refresh_date'), |
6842bb53 | 57 | ); |
4d1040bb | 58 | |
e299c1d0 | 59 | /** |
3d469574 | 60 | * Setting Callback - Validate. |
e299c1d0 TO |
61 | * |
62 | * @param mixed $value | |
63 | * @param array $fieldSpec | |
3d469574 | 64 | * |
e299c1d0 TO |
65 | * @return bool |
66 | * @throws API_Exception | |
67 | */ | |
68 | public static function checkLoggingSupport(&$value, $fieldSpec) { | |
69 | $domain = new CRM_Core_DAO_Domain(); | |
70 | $domain->find(TRUE); | |
f55f7133 SB |
71 | if (!(CRM_Core_DAO::checkTriggerViewPermission(FALSE)) && $value) { |
72 | 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."); | |
e299c1d0 TO |
73 | } |
74 | return TRUE; | |
75 | } | |
76 | ||
77 | /** | |
3d469574 | 78 | * Setting Callback - On Change. |
79 | * | |
e299c1d0 TO |
80 | * Respond to changes in the "logging" setting. Set up or destroy |
81 | * triggers, etal. | |
82 | * | |
83 | * @param array $oldValue | |
84 | * List of component names. | |
85 | * @param array $newValue | |
86 | * List of component names. | |
87 | * @param array $metadata | |
88 | * Specification of the setting (per *.settings.php). | |
89 | */ | |
90 | public static function onToggle($oldValue, $newValue, $metadata) { | |
91 | if ($oldValue == $newValue) { | |
92 | return; | |
93 | } | |
94 | ||
95 | $logging = new CRM_Logging_Schema(); | |
96 | if ($newValue) { | |
97 | $logging->enableLogging(); | |
98 | } | |
99 | else { | |
100 | $logging->disableLogging(); | |
101 | } | |
102 | } | |
103 | ||
6a488035 TO |
104 | /** |
105 | * Populate $this->tables and $this->logs with current db state. | |
106 | */ | |
00be9182 | 107 | public function __construct() { |
6a488035 TO |
108 | $dao = new CRM_Contact_DAO_Contact(); |
109 | $civiDBName = $dao->_database; | |
110 | ||
111 | $dao = CRM_Core_DAO::executeQuery(" | |
112 | SELECT TABLE_NAME | |
113 | FROM INFORMATION_SCHEMA.TABLES | |
114 | WHERE TABLE_SCHEMA = '{$civiDBName}' | |
115 | AND TABLE_TYPE = 'BASE TABLE' | |
116 | AND TABLE_NAME LIKE 'civicrm_%' | |
117 | "); | |
118 | while ($dao->fetch()) { | |
119 | $this->tables[] = $dao->TABLE_NAME; | |
120 | } | |
121 | ||
3292d079 | 122 | // do not log temp import, cache, menu and log tables |
6a488035 TO |
123 | $this->tables = preg_grep('/^civicrm_import_job_/', $this->tables, PREG_GREP_INVERT); |
124 | $this->tables = preg_grep('/_cache$/', $this->tables, PREG_GREP_INVERT); | |
125 | $this->tables = preg_grep('/_log/', $this->tables, PREG_GREP_INVERT); | |
6a488035 | 126 | $this->tables = preg_grep('/^civicrm_queue_/', $this->tables, PREG_GREP_INVERT); |
3d469574 | 127 | //CRM-14672 |
128 | $this->tables = preg_grep('/^civicrm_menu/', $this->tables, PREG_GREP_INVERT); | |
db70b323 | 129 | $this->tables = preg_grep('/_temp_/', $this->tables, PREG_GREP_INVERT); |
e719c24e | 130 | // CRM-18178 |
131 | $this->tables = preg_grep('/_bak$/', $this->tables, PREG_GREP_INVERT); | |
132 | $this->tables = preg_grep('/_backup$/', $this->tables, PREG_GREP_INVERT); | |
6a488035 | 133 | |
f9b793b0 DL |
134 | // do not log civicrm_mailing_event* tables, CRM-12300 |
135 | $this->tables = preg_grep('/^civicrm_mailing_event_/', $this->tables, PREG_GREP_INVERT); | |
136 | ||
d0cef561 | 137 | // do not log civicrm_mailing_recipients table, CRM-16193 |
138 | $this->tables = array_diff($this->tables, array('civicrm_mailing_recipients')); | |
139 | ||
340f6aa0 DL |
140 | if (defined('CIVICRM_LOGGING_DSN')) { |
141 | $dsn = DB::parseDSN(CIVICRM_LOGGING_DSN); | |
142 | $this->useDBPrefix = (CIVICRM_LOGGING_DSN != CIVICRM_DSN); | |
143 | } | |
144 | else { | |
145 | $dsn = DB::parseDSN(CIVICRM_DSN); | |
146 | $this->useDBPrefix = FALSE; | |
147 | } | |
6a488035 TO |
148 | $this->db = $dsn['database']; |
149 | ||
150 | $dao = CRM_Core_DAO::executeQuery(" | |
151 | SELECT TABLE_NAME | |
152 | FROM INFORMATION_SCHEMA.TABLES | |
153 | WHERE TABLE_SCHEMA = '{$this->db}' | |
154 | AND TABLE_TYPE = 'BASE TABLE' | |
155 | AND TABLE_NAME LIKE 'log_civicrm_%' | |
156 | "); | |
157 | while ($dao->fetch()) { | |
158 | $log = $dao->TABLE_NAME; | |
159 | $this->logs[substr($log, 4)] = $log; | |
160 | } | |
161 | } | |
162 | ||
163 | /** | |
164 | * Return logging custom data tables. | |
165 | */ | |
00be9182 | 166 | public function customDataLogTables() { |
6a488035 TO |
167 | return preg_grep('/^log_civicrm_value_/', $this->logs); |
168 | } | |
169 | ||
694e78fd DS |
170 | /** |
171 | * Return custom data tables for specified entity / extends. | |
ea3ddccf | 172 | * |
173 | * @param string $extends | |
174 | * | |
175 | * @return array | |
694e78fd | 176 | */ |
00be9182 | 177 | public function entityCustomDataLogTables($extends) { |
694e78fd DS |
178 | $customGroupTables = array(); |
179 | $customGroupDAO = CRM_Core_BAO_CustomGroup::getAllCustomGroupsByBaseEntity($extends); | |
180 | $customGroupDAO->find(); | |
181 | while ($customGroupDAO->fetch()) { | |
182 | $customGroupTables[$customGroupDAO->table_name] = $this->logs[$customGroupDAO->table_name]; | |
183 | } | |
184 | return $customGroupTables; | |
185 | } | |
186 | ||
6a488035 TO |
187 | /** |
188 | * Disable logging by dropping the triggers (but keep the log tables intact). | |
189 | */ | |
00be9182 | 190 | public function disableLogging() { |
6a488035 TO |
191 | $config = CRM_Core_Config::singleton(); |
192 | $config->logging = FALSE; | |
193 | ||
194 | $this->dropTriggers(); | |
195 | ||
196 | // invoke the meta trigger creation call | |
197 | CRM_Core_DAO::triggerRebuild(); | |
198 | ||
199 | $this->deleteReports(); | |
200 | } | |
201 | ||
202 | /** | |
203 | * Drop triggers for all logged tables. | |
ad37ac8e | 204 | * |
205 | * @param string $tableName | |
6a488035 | 206 | */ |
00be9182 | 207 | public function dropTriggers($tableName = NULL) { |
ae5ffbb7 | 208 | $dao = new CRM_Core_DAO(); |
6a488035 TO |
209 | |
210 | if ($tableName) { | |
211 | $tableNames = array($tableName); | |
212 | } | |
213 | else { | |
214 | $tableNames = $this->tables; | |
215 | } | |
216 | ||
217 | foreach ($tableNames as $table) { | |
6842bb53 DL |
218 | $validName = CRM_Core_DAO::shortenSQLName($table, 48, TRUE); |
219 | ||
6a488035 | 220 | // before triggers |
6842bb53 DL |
221 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$validName}_before_insert"); |
222 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$validName}_before_update"); | |
223 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$validName}_before_delete"); | |
6a488035 | 224 | |
353ffa53 | 225 | // after triggers |
6842bb53 DL |
226 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$validName}_after_insert"); |
227 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$validName}_after_update"); | |
228 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$validName}_after_delete"); | |
6a488035 | 229 | } |
a8dd306e DL |
230 | |
231 | // now lets also be safe and drop all triggers that start with | |
232 | // civicrm_ if we are dropping all triggers | |
233 | // we need to do this to capture all the leftover triggers since | |
234 | // we did the shortening trigger name for CRM-11794 | |
235 | if ($tableName === NULL) { | |
236 | $triggers = $dao->executeQuery("SHOW TRIGGERS LIKE 'civicrm_%'"); | |
237 | ||
238 | while ($triggers->fetch()) { | |
b44e3f84 | 239 | // note that drop trigger has a weird syntax and hence we do not |
a8dd306e DL |
240 | // send the trigger name as a string (i.e. its not quoted |
241 | $dao->executeQuery("DROP TRIGGER IF EXISTS {$triggers->Trigger}"); | |
242 | } | |
243 | } | |
6a488035 TO |
244 | } |
245 | ||
246 | /** | |
3d469574 | 247 | * Enable site-wide logging. |
6a488035 | 248 | */ |
00be9182 | 249 | public function enableLogging() { |
6a488035 TO |
250 | $this->fixSchemaDifferences(TRUE); |
251 | $this->addReports(); | |
252 | } | |
253 | ||
254 | /** | |
255 | * Sync log tables and rebuild triggers. | |
256 | * | |
353ffa53 | 257 | * @param bool $enableLogging : Ensure logging is enabled |
6a488035 | 258 | */ |
00be9182 | 259 | public function fixSchemaDifferences($enableLogging = FALSE) { |
6a488035 TO |
260 | $config = CRM_Core_Config::singleton(); |
261 | if ($enableLogging) { | |
262 | $config->logging = TRUE; | |
263 | } | |
264 | if ($config->logging) { | |
bfb723bb | 265 | $this->fixSchemaDifferencesForALL(); |
6a488035 TO |
266 | } |
267 | // invoke the meta trigger creation call | |
e53944ef | 268 | CRM_Core_DAO::triggerRebuild(NULL, TRUE); |
6a488035 TO |
269 | } |
270 | ||
271 | /** | |
272 | * Add missing (potentially specified) log table columns for the given table. | |
273 | * | |
5a4f6742 CW |
274 | * @param string $table |
275 | * name of the relevant table. | |
276 | * @param array $cols | |
4c922589 | 277 | * Mixed array of columns to add or null (to check for the missing columns). |
5a4f6742 CW |
278 | * @param bool $rebuildTrigger |
279 | * should we rebuild the triggers. | |
6a488035 | 280 | * |
3d469574 | 281 | * @return bool |
6a488035 | 282 | */ |
00be9182 | 283 | public function fixSchemaDifferencesFor($table, $cols = array(), $rebuildTrigger = FALSE) { |
bfb723bb DS |
284 | if (empty($table)) { |
285 | return FALSE; | |
286 | } | |
6a488035 TO |
287 | if (empty($this->logs[$table])) { |
288 | $this->createLogTableFor($table); | |
bfb723bb | 289 | return TRUE; |
6a488035 TO |
290 | } |
291 | ||
6a488035 | 292 | if (empty($cols)) { |
bfb723bb | 293 | $cols = $this->columnsWithDiffSpecs($table, "log_$table"); |
6a488035 TO |
294 | } |
295 | ||
296 | // use the relevant lines from CREATE TABLE to add colums to the log table | |
bfb723bb DS |
297 | $create = $this->_getCreateQuery($table); |
298 | foreach ((array('ADD', 'MODIFY')) as $alterType) { | |
12de149a DS |
299 | if (!empty($cols[$alterType])) { |
300 | foreach ($cols[$alterType] as $col) { | |
301 | $line = $this->_getColumnQuery($col, $create); | |
302 | CRM_Core_DAO::executeQuery("ALTER TABLE `{$this->db}`.log_$table {$alterType} {$line}"); | |
303 | } | |
bfb723bb DS |
304 | } |
305 | } | |
306 | ||
31c270e1 DS |
307 | // for any obsolete columns (not null) we just make the column nullable. |
308 | if (!empty($cols['OBSOLETE'])) { | |
eae2404c | 309 | $create = $this->_getCreateQuery("`{$this->db}`.log_{$table}"); |
31c270e1 | 310 | foreach ($cols['OBSOLETE'] as $col) { |
bfb723bb | 311 | $line = $this->_getColumnQuery($col, $create); |
31c270e1 DS |
312 | // This is just going to make a not null column to nullable |
313 | CRM_Core_DAO::executeQuery("ALTER TABLE `{$this->db}`.log_$table MODIFY {$line}"); | |
bfb723bb DS |
314 | } |
315 | } | |
316 | ||
317 | if ($rebuildTrigger) { | |
318 | // invoke the meta trigger creation call | |
319 | CRM_Core_DAO::triggerRebuild($table); | |
320 | } | |
321 | return TRUE; | |
322 | } | |
323 | ||
e0ef6999 | 324 | /** |
3d469574 | 325 | * Get query table. |
326 | * | |
327 | * @param string $table | |
e0ef6999 EM |
328 | * |
329 | * @return array | |
330 | */ | |
bfb723bb DS |
331 | private function _getCreateQuery($table) { |
332 | $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE {$table}"); | |
6a488035 TO |
333 | $dao->fetch(); |
334 | $create = explode("\n", $dao->Create_Table); | |
bfb723bb DS |
335 | return $create; |
336 | } | |
6a488035 | 337 | |
e0ef6999 | 338 | /** |
3d469574 | 339 | * Get column query. |
340 | * | |
341 | * @param string $col | |
342 | * @param bool $createQuery | |
e0ef6999 EM |
343 | * |
344 | * @return array|mixed|string | |
345 | */ | |
bfb723bb DS |
346 | private function _getColumnQuery($col, $createQuery) { |
347 | $line = preg_grep("/^ `$col` /", $createQuery); | |
4a423b7a | 348 | $line = rtrim(array_pop($line), ','); |
bfb723bb | 349 | // CRM-11179 |
4a423b7a | 350 | $line = $this->fixTimeStampAndNotNullSQL($line); |
bfb723bb DS |
351 | return $line; |
352 | } | |
353 | ||
e0ef6999 | 354 | /** |
3d469574 | 355 | * Fix schema differences. |
356 | * | |
e0ef6999 EM |
357 | * @param bool $rebuildTrigger |
358 | */ | |
00be9182 | 359 | public function fixSchemaDifferencesForAll($rebuildTrigger = FALSE) { |
bfb723bb | 360 | $diffs = array(); |
bfb723bb DS |
361 | foreach ($this->tables as $table) { |
362 | if (empty($this->logs[$table])) { | |
363 | $this->createLogTableFor($table); | |
e53944ef BS |
364 | } |
365 | else { | |
bfb723bb DS |
366 | $diffs[$table] = $this->columnsWithDiffSpecs($table, "log_$table"); |
367 | } | |
368 | } | |
6a488035 | 369 | |
bfb723bb DS |
370 | foreach ($diffs as $table => $cols) { |
371 | $this->fixSchemaDifferencesFor($table, $cols, FALSE); | |
6a488035 TO |
372 | } |
373 | ||
374 | if ($rebuildTrigger) { | |
375 | // invoke the meta trigger creation call | |
376 | CRM_Core_DAO::triggerRebuild($table); | |
377 | } | |
378 | } | |
379 | ||
d424ffde | 380 | /** |
3d469574 | 381 | * Fix timestamp. |
382 | * | |
d424ffde | 383 | * Log_civicrm_contact.modified_date for example would always be copied from civicrm_contact.modified_date, |
bfb723bb DS |
384 | * so there's no need for a default timestamp and therefore we remove such default timestamps |
385 | * also eliminate the NOT NULL constraint, since we always copy and schema can change down the road) | |
d424ffde | 386 | * |
3d469574 | 387 | * @param string $query |
e0ef6999 EM |
388 | * |
389 | * @return mixed | |
390 | */ | |
00be9182 | 391 | public function fixTimeStampAndNotNullSQL($query) { |
6a488035 TO |
392 | $query = str_ireplace("TIMESTAMP NOT NULL", "TIMESTAMP NULL", $query); |
393 | $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP", '', $query); | |
394 | $query = str_ireplace("DEFAULT CURRENT_TIMESTAMP", '', $query); | |
c28241be | 395 | $query = str_ireplace("NOT NULL", '', $query); |
6a488035 TO |
396 | return $query; |
397 | } | |
398 | ||
3d469574 | 399 | /** |
400 | * Add reports. | |
401 | */ | |
6a488035 TO |
402 | private function addReports() { |
403 | $titles = array( | |
404 | 'logging/contact/detail' => ts('Logging Details'), | |
405 | 'logging/contact/summary' => ts('Contact Logging Report (Summary)'), | |
406 | 'logging/contribute/detail' => ts('Contribution Logging Report (Detail)'), | |
407 | 'logging/contribute/summary' => ts('Contribution Logging Report (Summary)'), | |
408 | ); | |
409 | // enable logging templates | |
410 | CRM_Core_DAO::executeQuery(" | |
411 | UPDATE civicrm_option_value | |
412 | SET is_active = 1 | |
413 | WHERE value IN ('" . implode("', '", $this->reports) . "') | |
414 | "); | |
415 | ||
416 | // add report instances | |
417 | $domain_id = CRM_Core_Config::domainID(); | |
418 | foreach ($this->reports as $report) { | |
ae5ffbb7 | 419 | $dao = new CRM_Report_DAO_ReportInstance(); |
353ffa53 TO |
420 | $dao->domain_id = $domain_id; |
421 | $dao->report_id = $report; | |
422 | $dao->title = $titles[$report]; | |
6a488035 | 423 | $dao->permission = 'administer CiviCRM'; |
ae5ffbb7 | 424 | if ($report == 'logging/contact/summary') { |
6a488035 | 425 | $dao->is_reserved = 1; |
ae5ffbb7 | 426 | } |
6a488035 TO |
427 | $dao->insert(); |
428 | } | |
429 | } | |
430 | ||
431 | /** | |
432 | * Get an array of column names of the given table. | |
ea3ddccf | 433 | * |
3d469574 | 434 | * @param string $table |
ea3ddccf | 435 | * @param bool $force |
436 | * | |
437 | * @return array | |
6a488035 | 438 | */ |
e53944ef | 439 | private function columnsOf($table, $force = FALSE) { |
6a488035 TO |
440 | static $columnsOf = array(); |
441 | ||
442 | $from = (substr($table, 0, 4) == 'log_') ? "`{$this->db}`.$table" : $table; | |
443 | ||
e53944ef | 444 | if (!isset($columnsOf[$table]) || $force) { |
6a4257d4 | 445 | $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); |
f55f7133 | 446 | $dao = CRM_Core_DAO::executeQuery("SHOW COLUMNS FROM $from", CRM_Core_DAO::$_nullArray, TRUE, NULL, FALSE, FALSE); |
6a488035 TO |
447 | if (is_a($dao, 'DB_Error')) { |
448 | return array(); | |
449 | } | |
450 | $columnsOf[$table] = array(); | |
451 | while ($dao->fetch()) { | |
452 | $columnsOf[$table][] = $dao->Field; | |
453 | } | |
454 | } | |
455 | ||
456 | return $columnsOf[$table]; | |
457 | } | |
458 | ||
bfb723bb DS |
459 | /** |
460 | * Get an array of columns and their details like DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT for the given table. | |
ea3ddccf | 461 | * |
462 | * @param string $table | |
463 | * | |
464 | * @return array | |
bfb723bb DS |
465 | */ |
466 | private function columnSpecsOf($table) { | |
467 | static $columnSpecs = array(), $civiDB = NULL; | |
468 | ||
469 | if (empty($columnSpecs)) { | |
470 | if (!$civiDB) { | |
471 | $dao = new CRM_Contact_DAO_Contact(); | |
472 | $civiDB = $dao->_database; | |
473 | } | |
cf0bd1e1 | 474 | CRM_Core_TemporaryErrorScope::ignoreException(); |
6842bb53 | 475 | // NOTE: W.r.t Performance using one query to find all details and storing in static array is much faster |
bfb723bb DS |
476 | // than firing query for every given table. |
477 | $query = " | |
cf0bd1e1 | 478 | SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT, COLUMN_TYPE |
bfb723bb DS |
479 | FROM INFORMATION_SCHEMA.COLUMNS |
480 | WHERE table_schema IN ('{$this->db}', '{$civiDB}')"; | |
bfb723bb | 481 | $dao = CRM_Core_DAO::executeQuery($query); |
bfb723bb DS |
482 | if (is_a($dao, 'DB_Error')) { |
483 | return array(); | |
484 | } | |
485 | while ($dao->fetch()) { | |
bfb723bb DS |
486 | if (!array_key_exists($dao->TABLE_NAME, $columnSpecs)) { |
487 | $columnSpecs[$dao->TABLE_NAME] = array(); | |
488 | } | |
ae5ffbb7 TO |
489 | $columnSpecs[$dao->TABLE_NAME][$dao->COLUMN_NAME] = array( |
490 | 'COLUMN_NAME' => $dao->COLUMN_NAME, | |
491 | 'DATA_TYPE' => $dao->DATA_TYPE, | |
492 | 'IS_NULLABLE' => $dao->IS_NULLABLE, | |
493 | 'COLUMN_DEFAULT' => $dao->COLUMN_DEFAULT, | |
494 | ); | |
cf0bd1e1 EM |
495 | if (($first = strpos($dao->COLUMN_TYPE, '(')) != 0) { |
496 | $columnSpecs[$dao->TABLE_NAME][$dao->COLUMN_NAME]['LENGTH'] = substr($dao->COLUMN_TYPE, $first, strpos($dao->COLUMN_TYPE, ')')); | |
497 | } | |
bfb723bb | 498 | } |
bfb723bb | 499 | } |
bfb723bb DS |
500 | return $columnSpecs[$table]; |
501 | } | |
502 | ||
e0ef6999 | 503 | /** |
3d469574 | 504 | * Get columns that have changed. |
505 | * | |
506 | * @param string $civiTable | |
507 | * @param string $logTable | |
e0ef6999 EM |
508 | * |
509 | * @return array | |
510 | */ | |
00be9182 | 511 | public function columnsWithDiffSpecs($civiTable, $logTable) { |
4a423b7a | 512 | $civiTableSpecs = $this->columnSpecsOf($civiTable); |
353ffa53 | 513 | $logTableSpecs = $this->columnSpecsOf($logTable); |
6842bb53 | 514 | |
31c270e1 | 515 | $diff = array('ADD' => array(), 'MODIFY' => array(), 'OBSOLETE' => array()); |
6842bb53 | 516 | |
4a423b7a DS |
517 | // columns to be added |
518 | $diff['ADD'] = array_diff(array_keys($civiTableSpecs), array_keys($logTableSpecs)); | |
6842bb53 | 519 | |
4a423b7a | 520 | // columns to be modified |
6842bb53 | 521 | // NOTE: we consider only those columns for modifications where there is a spec change, and that the column definition |
4a423b7a DS |
522 | // wasn't deliberately modified by fixTimeStampAndNotNullSQL() method. |
523 | foreach ($civiTableSpecs as $col => $colSpecs) { | |
481a74f4 | 524 | if (!isset($logTableSpecs[$col]) || !is_array($logTableSpecs[$col])) { |
e53944ef BS |
525 | $logTableSpecs[$col] = array(); |
526 | } | |
527 | ||
cd71572a | 528 | $specDiff = array_diff($civiTableSpecs[$col], $logTableSpecs[$col]); |
12de149a | 529 | if (!empty($specDiff) && $col != 'id' && !array_key_exists($col, $diff['ADD'])) { |
4a423b7a | 530 | // ignore 'id' column for any spec changes, to avoid any auto-increment mysql errors |
cf0bd1e1 EM |
531 | if ($civiTableSpecs[$col]['DATA_TYPE'] != CRM_Utils_Array::value('DATA_TYPE', $logTableSpecs[$col]) |
532 | // We won't alter the log if the length is decreased in case some of the existing data won't fit. | |
533 | || CRM_Utils_Array::value('LENGTH', $civiTableSpecs[$col]) > CRM_Utils_Array::value('LENGTH', $logTableSpecs[$col]) | |
534 | ) { | |
6842bb53 | 535 | // if data-type is different, surely consider the column |
4a423b7a | 536 | $diff['MODIFY'][] = $col; |
0db6c3e1 | 537 | } |
4c9b6178 | 538 | elseif ($civiTableSpecs[$col]['IS_NULLABLE'] != CRM_Utils_Array::value('IS_NULLABLE', $logTableSpecs[$col]) && |
353ffa53 TO |
539 | $logTableSpecs[$col]['IS_NULLABLE'] == 'NO' |
540 | ) { | |
4a423b7a DS |
541 | // if is-null property is different, and log table's column is NOT-NULL, surely consider the column |
542 | $diff['MODIFY'][] = $col; | |
0db6c3e1 | 543 | } |
4c9b6178 | 544 | elseif ($civiTableSpecs[$col]['COLUMN_DEFAULT'] != CRM_Utils_Array::value('COLUMN_DEFAULT', $logTableSpecs[$col]) && |
353ffa53 TO |
545 | !strstr($civiTableSpecs[$col]['COLUMN_DEFAULT'], 'TIMESTAMP') |
546 | ) { | |
4a423b7a DS |
547 | // if default property is different, and its not about a timestamp column, consider it |
548 | $diff['MODIFY'][] = $col; | |
549 | } | |
6842bb53 | 550 | } |
bfb723bb DS |
551 | } |
552 | ||
4a423b7a DS |
553 | // columns to made obsolete by turning into not-null |
554 | $oldCols = array_diff(array_keys($logTableSpecs), array_keys($civiTableSpecs)); | |
31c270e1 | 555 | foreach ($oldCols as $col) { |
6842bb53 | 556 | if (!in_array($col, array('log_date', 'log_conn_id', 'log_user_id', 'log_action')) && |
353ffa53 TO |
557 | $logTableSpecs[$col]['IS_NULLABLE'] == 'NO' |
558 | ) { | |
4a423b7a | 559 | // if its a column present only in log table, not among those used by log tables for special purpose, and not-null |
31c270e1 | 560 | $diff['OBSOLETE'][] = $col; |
bfb723bb DS |
561 | } |
562 | } | |
563 | ||
564 | return $diff; | |
565 | } | |
566 | ||
6a488035 TO |
567 | /** |
568 | * Create a log table with schema mirroring the given table’s structure and seeding it with the given table’s contents. | |
ea3ddccf | 569 | * |
570 | * @param string $table | |
6a488035 TO |
571 | */ |
572 | private function createLogTableFor($table) { | |
20139237 | 573 | $dao = CRM_Core_DAO::executeQuery("SHOW CREATE TABLE $table", CRM_Core_DAO::$_nullArray, TRUE, NULL, FALSE, FALSE); |
6a488035 TO |
574 | $dao->fetch(); |
575 | $query = $dao->Create_Table; | |
576 | ||
577 | // rewrite the queries into CREATE TABLE queries for log tables: | |
6a488035 | 578 | $cols = <<<COLS |
1f4627e9 | 579 | , |
6a488035 TO |
580 | log_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
581 | log_conn_id INTEGER, | |
582 | log_user_id INTEGER, | |
583 | log_action ENUM('Initialization', 'Insert', 'Update', 'Delete') | |
584 | COLS; | |
c28241be DL |
585 | |
586 | // - prepend the name with log_ | |
587 | // - drop AUTO_INCREMENT columns | |
588 | // - drop non-column rows of the query (keys, constraints, etc.) | |
589 | // - set the ENGINE to ARCHIVE | |
590 | // - add log-specific columns (at the end of the table) | |
6a488035 TO |
591 | $query = preg_replace("/^CREATE TABLE `$table`/i", "CREATE TABLE `{$this->db}`.log_$table", $query); |
592 | $query = preg_replace("/ AUTO_INCREMENT/i", '', $query); | |
593 | $query = preg_replace("/^ [^`].*$/m", '', $query); | |
594 | $query = preg_replace("/^\) ENGINE=[^ ]+ /im", ') ENGINE=ARCHIVE ', $query); | |
c28241be | 595 | |
6a488035 | 596 | // log_civicrm_contact.modified_date for example would always be copied from civicrm_contact.modified_date, |
c28241be DL |
597 | // so there's no need for a default timestamp and therefore we remove such default timestamps |
598 | // also eliminate the NOT NULL constraint, since we always copy and schema can change down the road) | |
599 | $query = self::fixTimeStampAndNotNullSQL($query); | |
1f4627e9 | 600 | $query = preg_replace("/(,*\n*\) )ENGINE/m", "$cols\n) ENGINE", $query); |
6a488035 | 601 | |
f55f7133 | 602 | CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray, TRUE, NULL, FALSE, FALSE); |
6a488035 TO |
603 | |
604 | $columns = implode(', ', $this->columnsOf($table)); | |
f55f7133 | 605 | 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); |
6a488035 TO |
606 | |
607 | $this->tables[] = $table; | |
608 | $this->logs[$table] = "log_$table"; | |
609 | } | |
610 | ||
3d469574 | 611 | /** |
612 | * Delete reports. | |
613 | */ | |
6a488035 TO |
614 | private function deleteReports() { |
615 | // disable logging templates | |
616 | CRM_Core_DAO::executeQuery(" | |
617 | UPDATE civicrm_option_value | |
618 | SET is_active = 0 | |
619 | WHERE value IN ('" . implode("', '", $this->reports) . "') | |
620 | "); | |
621 | ||
622 | // delete report instances | |
623 | $domain_id = CRM_Core_Config::domainID(); | |
624 | foreach ($this->reports as $report) { | |
ae5ffbb7 | 625 | $dao = new CRM_Report_DAO_ReportInstance(); |
6a488035 TO |
626 | $dao->domain_id = $domain_id; |
627 | $dao->report_id = $report; | |
628 | $dao->delete(); | |
629 | } | |
630 | } | |
631 | ||
632 | /** | |
633 | * Predicate whether logging is enabled. | |
634 | */ | |
635 | public function isEnabled() { | |
636 | $config = CRM_Core_Config::singleton(); | |
637 | ||
638 | if ($config->logging) { | |
639 | return $this->tablesExist() and $this->triggersExist(); | |
640 | } | |
641 | return FALSE; | |
642 | } | |
643 | ||
644 | /** | |
645 | * Predicate whether any log tables exist. | |
646 | */ | |
647 | private function tablesExist() { | |
648 | return !empty($this->logs); | |
649 | } | |
650 | ||
651 | /** | |
652 | * Predicate whether the logging triggers are in place. | |
653 | */ | |
654 | private function triggersExist() { | |
655 | // FIXME: probably should be a bit more thorough… | |
656 | // note that the LIKE parameter is TABLE NAME | |
657 | return (bool) CRM_Core_DAO::singleValueQuery("SHOW TRIGGERS LIKE 'civicrm_contact'"); | |
658 | } | |
659 | ||
e0ef6999 | 660 | /** |
3d469574 | 661 | * Get trigger info. |
662 | * | |
663 | * @param array $info | |
e0ef6999 EM |
664 | * @param null $tableName |
665 | * @param bool $force | |
666 | */ | |
00be9182 | 667 | public function triggerInfo(&$info, $tableName = NULL, $force = FALSE) { |
74b348da | 668 | if (!CRM_Core_Config::singleton()->logging) { |
6a488035 TO |
669 | return; |
670 | } | |
671 | ||
672 | $insert = array('INSERT'); | |
673 | $update = array('UPDATE'); | |
674 | $delete = array('DELETE'); | |
675 | ||
676 | if ($tableName) { | |
677 | $tableNames = array($tableName); | |
678 | } | |
679 | else { | |
680 | $tableNames = $this->tables; | |
681 | } | |
682 | ||
683 | // logging is enabled, so now lets create the trigger info tables | |
684 | foreach ($tableNames as $table) { | |
e53944ef | 685 | $columns = $this->columnsOf($table, $force); |
6a488035 TO |
686 | |
687 | // only do the change if any data has changed | |
481a74f4 | 688 | $cond = array(); |
6a488035 TO |
689 | foreach ($columns as $column) { |
690 | // ignore modified_date changes | |
4d1040bb | 691 | if ($column != 'modified_date' && !in_array($column, CRM_Utils_Array::value($table, $this->exceptions, array()))) { |
6a488035 TO |
692 | $cond[] = "IFNULL(OLD.$column,'') <> IFNULL(NEW.$column,'')"; |
693 | } | |
694 | } | |
695 | $suppressLoggingCond = "@civicrm_disable_logging IS NULL OR @civicrm_disable_logging = 0"; | |
481a74f4 | 696 | $updateSQL = "IF ( (" . implode(' OR ', $cond) . ") AND ( $suppressLoggingCond ) ) THEN "; |
6a488035 | 697 | |
340f6aa0 DL |
698 | if ($this->useDBPrefix) { |
699 | $sqlStmt = "INSERT INTO `{$this->db}`.log_{tableName} ("; | |
700 | } | |
701 | else { | |
702 | $sqlStmt = "INSERT INTO log_{tableName} ("; | |
703 | } | |
6a488035 TO |
704 | foreach ($columns as $column) { |
705 | $sqlStmt .= "$column, "; | |
706 | } | |
707 | $sqlStmt .= "log_conn_id, log_user_id, log_action) VALUES ("; | |
708 | ||
709 | $insertSQL = $deleteSQL = "IF ( $suppressLoggingCond ) THEN $sqlStmt "; | |
710 | $updateSQL .= $sqlStmt; | |
711 | ||
712 | $sqlStmt = ''; | |
713 | foreach ($columns as $column) { | |
353ffa53 | 714 | $sqlStmt .= "NEW.$column, "; |
6a488035 TO |
715 | $deleteSQL .= "OLD.$column, "; |
716 | } | |
353ffa53 | 717 | $sqlStmt .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}');"; |
6a488035 TO |
718 | $deleteSQL .= "CONNECTION_ID(), @civicrm_user_id, '{eventName}');"; |
719 | ||
353ffa53 | 720 | $sqlStmt .= "END IF;"; |
6a488035 TO |
721 | $deleteSQL .= "END IF;"; |
722 | ||
723 | $insertSQL .= $sqlStmt; | |
724 | $updateSQL .= $sqlStmt; | |
725 | ||
726 | $info[] = array( | |
727 | 'table' => array($table), | |
728 | 'when' => 'AFTER', | |
729 | 'event' => $insert, | |
730 | 'sql' => $insertSQL, | |
731 | ); | |
732 | ||
733 | $info[] = array( | |
734 | 'table' => array($table), | |
735 | 'when' => 'AFTER', | |
736 | 'event' => $update, | |
737 | 'sql' => $updateSQL, | |
738 | ); | |
739 | ||
740 | $info[] = array( | |
741 | 'table' => array($table), | |
742 | 'when' => 'AFTER', | |
743 | 'event' => $delete, | |
744 | 'sql' => $deleteSQL, | |
745 | ); | |
746 | } | |
747 | } | |
748 | ||
749 | /** | |
3d469574 | 750 | * Disable logging temporarily. |
751 | * | |
6a488035 | 752 | * This allow logging to be temporarily disabled for certain cases |
74b348da | 753 | * where we want to do a mass cleanup but do not want to bother with |
754 | * an audit trail. | |
6a488035 | 755 | */ |
481a74f4 | 756 | public static function disableLoggingForThisConnection() { |
74b348da | 757 | if (CRM_Core_Config::singleton()->logging) { |
481a74f4 | 758 | CRM_Core_DAO::executeQuery('SET @civicrm_disable_logging = 1'); |
6a488035 TO |
759 | } |
760 | } | |
761 | ||
762 | } |