From ebc787dcdbc0fdc0c31d58377b38c18e9b96317e Mon Sep 17 00:00:00 2001
From: Kjartan <kjartan@drupal.org>
Date: Mon, 12 Dec 2011 21:41:34 +0100
Subject: [PATCH] Issue #1368862: Make installer unix socket aware.

---
 core/includes/database/mysql/database.inc |    1 +
 core/includes/database/mysql/install.inc  |   21 ++++++++++++++++++++
 core/includes/database/pgsql/database.inc |   19 ++++++++++++-----
 core/includes/database/pgsql/install.inc  |   30 +++++++++++++++++++++++++++++
 core/includes/database/sqlite/install.inc |    2 +-
 core/includes/install.inc                 |    9 ++++---
 6 files changed, 71 insertions(+), 11 deletions(-)

diff --git a/core/includes/database/mysql/database.inc b/core/includes/database/mysql/database.inc
index a57f7dd..f66c9ac 100644
--- a/core/includes/database/mysql/database.inc
+++ b/core/includes/database/mysql/database.inc
@@ -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..b2d62c3 100644
--- a/core/includes/database/mysql/install.inc
+++ b/core/includes/database/mysql/install.inc
@@ -29,5 +29,26 @@ class DatabaseTasks_mysql extends DatabaseTasks {
   public function minimumVersion() {
     return '5.0.15';
   }
+
+  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'])) {
+        // If we are using a unix socket unset host/port.
+        $database['unix_socket'] = $database['host'];
+        $database['host'] = NULL;
+        $database['port'] = NULL;
+      }
+      else {
+        $errors[$database['driver'] . '][host'] =  st('Unix socket must exist.');
+      }
+    }
+
+    return $errors;
+  }
+
 }
 
diff --git a/core/includes/database/pgsql/database.inc b/core/includes/database/pgsql/database.inc
index 68300e5..93c3dae 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,19 @@ 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 default connection (socket > localhost).
+      $dsn = 'pgsql:';
+    }
+    elseif ($connection_options['host'][0] == '/') {
+      // If unix_socket is set use that for the host.
+      $dsn = 'pgsql:host=' . $connection_options['host'];
+    }
+    else {
+      // 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..513d633 100644
--- a/core/includes/database/pgsql/install.inc
+++ b/core/includes/database/pgsql/install.inc
@@ -112,6 +112,36 @@ 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']['#required'] = FALSE;
+      $form['advanced_options']['host']['#default_value'] = isset($database['host']) ? $database['host'] : '';
+    }
+
+    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'])) {
+        // If we are using a unix socket unset port.
+        $database['port'] = NULL;
+      }
+      else {
+        $errors[$database['driver'] . '][host'] =  st('Unix socket must exist.');
+      }
+    }
+
+    return $errors;
+  }
+
   /**
    * Make PostgreSQL Drupal friendly.
    */
diff --git a/core/includes/database/sqlite/install.inc b/core/includes/database/sqlite/install.inc
index 62cbac3..5de3d3c 100644
--- a/core/includes/database/sqlite/install.inc
+++ b/core/includes/database/sqlite/install.inc
@@ -35,7 +35,7 @@ class DatabaseTasks_sqlite extends DatabaseTasks {
     return $form;
   }
 
-  public function validateDatabaseSettings($database) {
+  public function validateDatabaseSettings(&$database) {
     // Perform standard validation.
     $errors = parent::validateDatabaseSettings($database);
 
diff --git a/core/includes/install.inc b/core/includes/install.inc
index 8d7f22d..d493bb9 100644
--- a/core/includes/install.inc
+++ b/core/includes/install.inc
@@ -517,7 +517,8 @@ abstract class DatabaseTasks {
 
     $form['advanced_options']['host'] = array(
       '#type' => 'textfield',
-      '#title' => st('Database host'),
+      // Unix sockets are not availabel on Windows.
+      '#title' => substr(PHP_OS, 0, 3) != 'WIN' ? st('Database host or unix socket') : st('Database host'),
       '#default_value' => empty($database['host']) ? 'localhost' : $database['host'],
       '#size' => 45,
       // Hostnames can be 255 characters long.
@@ -551,17 +552,17 @@ abstract class DatabaseTasks {
    * @return
    *   An array of driver configuration errors, keyed by form element name.
    */
-  public function validateDatabaseSettings($database) {
+  public function validateDatabaseSettings(&$database) {
     $errors = array();
 
     // 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

