private $logs = [];
private $tables = [];
+ /**
+ * Name of the database where the logging data is stored.
+ *
+ * @var string
+ */
private $db;
+
private $useDBPrefix = TRUE;
private $reports = [
* Populate $this->tables and $this->logs with current db state.
*/
public function __construct() {
- $dao = new CRM_Contact_DAO_Contact();
- $civiDBName = $dao->_database;
+ $civiDBName = $this->getCiviCRMDatabaseName();
$dao = CRM_Core_DAO::executeQuery("
SELECT TABLE_NAME
while ($dao->fetch()) {
$this->tables[] = $dao->TABLE_NAME;
}
+ // Get any non standard table names used for custom groups.
+ // include these BEFORE the hook is called.
+ $customFieldDAO = CRM_Core_DAO::executeQuery("
+ SELECT DISTINCT table_name as TABLE_NAME FROM civicrm_custom_group
+ where table_name NOT LIKE 'civicrm_%';
+ ");
+ while ($customFieldDAO->fetch()) {
+ $this->tables[] = $customFieldDAO->TABLE_NAME;
+ }
// do not log temp import, cache, menu and log tables
$this->tables = preg_grep('/^civicrm_import_job_/', $this->tables, PREG_GREP_INVERT);
$this->tables = array_keys($this->logTableSpec);
$nonStandardTableNameString = $this->getNonStandardTableNameFilterString();
- if (defined('CIVICRM_LOGGING_DSN')) {
- $dsn = CRM_Utils_SQL::autoSwitchDSN(CIVICRM_LOGGING_DSN);
- $dsn = DB::parseDSN($dsn);
- $this->useDBPrefix = (CIVICRM_LOGGING_DSN != CIVICRM_DSN);
- }
- else {
- $dsn = CRM_Utils_SQL::autoSwitchDSN(CIVICRM_DSN);
- $dsn = DB::parseDSN($dsn);
- $this->useDBPrefix = FALSE;
- }
- $this->db = $dsn['database'];
+ $this->db = $this->getDatabaseNameFromDSN(defined('CIVICRM_LOGGING_DSN') ? CIVICRM_LOGGING_DSN : CIVICRM_DSN);
+ $this->useDBPrefix = $this->db !== $civiDBName;
$dao = CRM_Core_DAO::executeQuery("
SELECT TABLE_NAME
*
* @param string $tableName
*/
- public function dropTriggers($tableName = NULL) {
+ public function dropTriggers($tableName = NULL): void {
/** @var \Civi\Core\SqlTriggers $sqlTriggers */
$sqlTriggers = Civi::service('sql_triggers');
$dao = new CRM_Core_DAO();
$tableNames = $this->tables;
}
+ // Sort the table names so the sql output is consistent for those sites
+ // loading it asynchronously (using the setting 'logging_no_trigger_permission')
+ asort($tableNames);
foreach ($tableNames as $table) {
$validName = CRM_Core_DAO::shortenSQLName($table, 48, TRUE);
return [];
}
+ /**
+ * Get the name of the database from the dsn string.
+ *
+ * @param string $dsnString
+ *
+ * @return string
+ */
+ protected function getDatabaseNameFromDSN($dsnString): string {
+ $dsn = CRM_Utils_SQL::autoSwitchDSN($dsnString);
+ $dsn = DB::parseDSN($dsn);
+ return $dsn['database'];
+ }
+
+ /**
+ * Get the database name for the CiviCRM connection.
+ *
+ * Note that we want to get it from the database connection,
+ * not the dsn, because there is at least one extension
+ * (https://github.com/totten/rpow) that 'meddles' with
+ * the DSN string.
+ *
+ * @return string
+ */
+ protected function getCiviCRMDatabaseName(): string {
+ return (new CRM_Contact_DAO_Contact())->_database;
+ }
+
}