Merge pull request #20862 from eileenmcnaughton/leg_setting
[civicrm-core.git] / CRM / Logging / Schema.php
index 6fc36509a4b968db3727143e462775f55d98c714..ad09e3a32c20431bbc7266fee8c543f441424377 100644 (file)
@@ -26,7 +26,13 @@ class CRM_Logging_Schema {
   private $logs = [];
   private $tables = [];
 
+  /**
+   * Name of the database where the logging data is stored.
+   *
+   * @var string
+   */
   private $db;
+
   private $useDBPrefix = TRUE;
 
   private $reports = [
@@ -113,8 +119,7 @@ class CRM_Logging_Schema {
    * 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
@@ -126,6 +131,15 @@ AND    TABLE_NAME LIKE 'civicrm_%'
     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);
@@ -157,17 +171,8 @@ AND    TABLE_NAME LIKE 'civicrm_%'
     $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
@@ -230,7 +235,7 @@ AND    (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString )
    *
    * @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();
@@ -242,6 +247,9 @@ AND    (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString )
       $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);
 
@@ -518,10 +526,8 @@ AND    (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString )
 
   /**
    * Fix schema differences.
-   *
-   * @param bool $rebuildTrigger
    */
-  public function fixSchemaDifferencesForAll($rebuildTrigger = FALSE) {
+  public function fixSchemaDifferencesForAll(): void {
     $diffs = [];
     $this->resetTableColumnsCache();
 
@@ -537,10 +543,6 @@ AND    (TABLE_NAME LIKE 'log_civicrm_%' $nonStandardTableNameString )
     foreach ($diffs as $table => $cols) {
       $this->fixSchemaDifferencesFor($table, $cols);
     }
-    if ($rebuildTrigger) {
-      // invoke the meta trigger creation call
-      CRM_Core_DAO::triggerRebuild(NULL, TRUE);
-    }
   }
 
   /**
@@ -1070,4 +1072,31 @@ COLS;
     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;
+  }
+
 }