1 backup.database.inc protected BackupDatabase::getDatabaseConnection()

Get the db connection for the specified db.

File

core/includes/backup/backup.database.inc, line 328
Functions to handle the direct to/from database backup source.

Class

BackupDatabase
A destination type for saving to a database server.

Code

protected function getDatabaseConnection() {
  if (!$this->connection) {
    $parts = explode(':', $this->getTarget());
    // One of the predefined databases (set in settings.php)
    if ($parts[0] == 'db') {
      $key = empty($parts[1]) ? 'default' : $parts[1];
      $target = empty($parts[2]) ? 'default' : $parts[2];
    }
    // An arbitrary database URL such as mysql://user:pass@localhost:3306/db.
    // This ability is not used directly by core, which only provides UIs for
    // backing up and restoring existing database connections.
    else {
      $url_parts = parse_url($this->getTarget());
      // If the url is specified build it into a connection info array.
      if ($url_parts) {
        $info = array(
          'driver' => empty($url_parts['scheme']) ? NULL : $url_parts['scheme'],
          'host' => empty($url_parts['host']) ? NULL : $url_parts['host'],
          'port' => empty($url_parts['port']) ? 3306 : $url_parts['port'],
          'username' => empty($url_parts['user']) ? NULL : $url_parts['user'],
          'password' => empty($url_parts['pass']) ? NULL : $url_parts['pass'],
          'database' => empty($url_parts['path']) ? NULL : $url_parts['path'],
        );
        $key = uniqid('backdrop_backup_tmp_');
        $target = 'default';
        Database::addConnectionInfo($key, $target, $info);
      }
      // No database selected. Assume the default.
      else {
        $key = $target = 'default';
      }
    }
    $this->connection = Database::getConnection($target, $key);
  }
  return $this->connection;
}