From e3bd82cdfb7e869472593c1c81fe7bd5a5e4b0df Mon Sep 17 00:00:00 2001
From: Kjartan <kjartan@drupal.org>
Date: Fri, 6 Jan 2012 23:13:28 +0100
Subject: [PATCH] Issue #793200: Allow unix sockets during install.

---
 core/includes/database/mysql/database.inc |    3 +-
 core/includes/database/mysql/install.inc  |   42 +++++++++++++++++++++++++++++
 core/includes/database/pgsql/database.inc |   20 +++++++++----
 core/includes/database/pgsql/install.inc  |   32 ++++++++++++++++++++++
 core/includes/install.inc                 |    5 +--
 5 files changed, 92 insertions(+), 10 deletions(-)

diff --git a/core/includes/database/mysql/database.inc b/core/includes/database/mysql/database.inc
index e024a7f..580ee1e 100644
--- a/core/includes/database/mysql/database.inc
+++ b/core/includes/database/mysql/database.inc
@@ -29,7 +29,7 @@ class DatabaseConnection_mysql extends DatabaseConnection {
     $this->connectionOptions = $connection_options;
 
     // The DSN should use either a socket or a host/port.
-    if (isset($connection_options['unix_socket'])) {
+    if (!empty($connection_options['unix_socket'])) {
       $dsn = 'mysql:unix_socket=' . $connection_options['unix_socket'];
     }
     else {
@@ -37,6 +37,7 @@ class DatabaseConnection_mysql extends DatabaseConnection {
       $dsn = 'mysql:host=' . $connection_options['host'] . ';port=' . (empty($connection_options['port']) ? 3306 : $connection_options['port']);
     }
     $dsn .= ';dbname=' . $connection_options['database'];
+
     // Allow PDO options to be overridden.
     $connection_options += array(
       'pdo' => array(),
diff --git a/core/includes/database/mysql/install.inc b/core/includes/database/mysql/install.inc
index 75f2ae3..a6ebfa5 100644
--- a/core/includes/database/mysql/install.inc
+++ b/core/includes/database/mysql/install.inc
@@ -29,5 +29,47 @@ class DatabaseTasks_mysql extends DatabaseTasks {
   public function minimumVersion() {
     return '5.0.15';
   }
+
+  public function getFormOptions($database) {
+    $form = parent::getFormOptions($database);
+
+    // Alter options to use default unix socket connection when not on Windows.
+    if (substr(PHP_OS, 0, 3) != 'WIN') {
+      $form['advanced_options']['unix_socket'] = array(
+        '#type' => 'textfield',
+        '#title' => st('Unix socket'),
+        '#default_value' => empty($database['unix_socket']) ? '' : $database['unix_socket'],
+        '#size' => 45,
+        // Paths can be 255 characters long.
+        '#maxlength' => 255,
+        '#description' => st('If your database server is using a non-standard socket, enter it here (e.g. /tmp/mysql.sock).'),
+      );
+    }
+
+    return $form;
+  }
+
+  public function validateDatabaseSettings($database) {
+    // Perform standard validation.
+    $errors = parent::validateDatabaseSettings($database);
+
+    // Verify socket.
+    if (!empty($database['unix_socket'])) {
+      if (!file_exists($database['unix_socket'])) {
+        $errors[$database['driver'] . '][unix_socket'] = st('Unix socket must exist.');
+      }
+
+      // Host and port should be empty when using a unix socket.
+      if (!empty($database['host'])) {
+        $errors[$database['driver'] . '][host'] = st('Host should be blank when using a unix socket.');
+      }
+      if (!empty($database['port'])) {
+        $errors[$database['driver'] . '][port'] = st('Port should be blank when using a unix socket.');
+      }
+    }
+
+    return $errors;
+  }
+
 }
 
diff --git a/core/includes/database/pgsql/database.inc b/core/includes/database/pgsql/database.inc
index 68300e5..22db23b 100644
--- a/core/includes/database/pgsql/database.inc
+++ b/core/includes/database/pgsql/database.inc
@@ -25,11 +25,6 @@ class DatabaseConnection_pgsql extends DatabaseConnection {
     // but we'll only enable it if standard transactions are.
     $this->transactionalDDLSupport = $this->transactionSupport;
 
-    // Default to TCP connection on port 5432.
-    if (empty($connection_options['port'])) {
-      $connection_options['port'] = 5432;
-    }
-
     // PostgreSQL in trust mode doesn't require a password to be supplied.
     if (empty($connection_options['password'])) {
       $connection_options['password'] = NULL;
@@ -46,7 +41,20 @@ class DatabaseConnection_pgsql extends DatabaseConnection {
 
     $this->connectionOptions = $connection_options;
 
-    $dsn = 'pgsql:host=' . $connection_options['host'] . ' dbname=' . $connection_options['database'] . ' port=' . $connection_options['port'];
+    if (empty($connection_options['host'])) {
+      // If no host nor unix_socket use the default connection options defined
+      // by php or pgsql settings.
+      $dsn = 'pgsql:';
+    }
+    elseif ($connection_options['host'][0] == '/') {
+      // If unix_socket is set use that for the host.
+      $dsn = 'pgsql:host=' . $connection_options['host'];
+    }
+    else {
+      // Otherwise default to TCP connection on port 5432.
+      $dsn = 'pgsql:host=' . $connection_options['host'] . ' port=' . (empty($connection_options['port']) ? 5432 : $connection_options['port']);
+    }
+    $dsn .= ' dbname='. $connection_options['database'];
 
     // Allow PDO options to be overridden.
     $connection_options += array(
diff --git a/core/includes/database/pgsql/install.inc b/core/includes/database/pgsql/install.inc
index c350634..ba62afc 100644
--- a/core/includes/database/pgsql/install.inc
+++ b/core/includes/database/pgsql/install.inc
@@ -112,6 +112,38 @@ class DatabaseTasks_pgsql extends DatabaseTasks {
     return ($bytea_output == 'encoding');
   }
 
+  public function getFormOptions($database) {
+    $form = parent::getFormOptions($database);
+
+    // Alter options to use default unix socket connection when not on Windows.
+    if (substr(PHP_OS, 0, 3) != 'WIN') {
+      $form['advanced_options']['host']['#title'] = st('Database host or unix socket');
+      $form['advanced_options']['host']['#default_value'] = isset($database['host']) ? $database['host'] : '';
+      $form['advanced_options']['host']['#description'] = st('If your database server is using a non-standard host or socket, enter it here.');
+    }
+
+    return $form;
+  }
+
+  public function validateDatabaseSettings($database) {
+    // Perform standard validation.
+    $errors = parent::validateDatabaseSettings($database);
+
+    // Verify host/socket.
+    if (!empty($database['host']) && $database['host'][0] == '/') {
+      if (!file_exists($database['host'])) {
+        $errors[$database['driver'] . '][host'] = st('Unix socket must exist.');
+      }
+
+      // Port should be empty when using a unix socket.
+      if (!empty($database['port'])) {
+        $errors[$database['driver'] . '][port'] = st('Port should be blank when using a unix socket.');
+      }
+    }
+
+    return $errors;
+  }
+
   /**
    * Make PostgreSQL Drupal friendly.
    */
diff --git a/core/includes/install.inc b/core/includes/install.inc
index 8f9d007..0542f9b 100644
--- a/core/includes/install.inc
+++ b/core/includes/install.inc
@@ -522,7 +522,6 @@ abstract class DatabaseTasks {
       '#size' => 45,
       // Hostnames can be 255 characters long.
       '#maxlength' => 255,
-      '#required' => TRUE,
       '#description' => st('If your database is located on a different server, change this.'),
     );
 
@@ -556,12 +555,12 @@ abstract class DatabaseTasks {
 
     // Verify the table prefix.
     if (!empty($database['prefix']) && is_string($database['prefix']) && !preg_match('/^[A-Za-z0-9_.]+$/', $database['prefix'])) {
-      $errors[$database['driver'] . '][advanced_options][db_prefix'] = st('The database table prefix you have entered, %prefix, is invalid. The table prefix can only contain alphanumeric characters, periods, or underscores.', array('%prefix' => $database['prefix']));
+      $errors[$database['driver'] . '][db_prefix'] = st('The database table prefix you have entered, %prefix, is invalid. The table prefix can only contain alphanumeric characters, periods, or underscores.', array('%prefix' => $database['prefix']));
     }
 
     // Verify the database port.
     if (!empty($database['port']) && !is_numeric($database['port'])) {
-      $errors[$database['driver'] . '][advanced_options][port'] =  st('Database port must be a number.');
+      $errors[$database['driver'] . '][port'] = st('Database port must be a number.');
     }
 
     return $errors;
-- 
1.7.2.5

