From 738bc2069301b5ce712f4b94342ea8c6b084ee48 Mon Sep 17 00:00:00 2001
From: Horacio Degiorgi <horaciod@gmail.com>
Date: Fri, 28 Jul 2017 12:11:47 -0300
Subject: [PATCH] Allow port configuration for database adapter (#1008)

---
 config/vufind/config.ini                       |  8 ++++++++
 module/VuFind/src/VuFind/Db/AdapterFactory.php | 14 +++++++++++---
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/config/vufind/config.ini b/config/vufind/config.ini
index f1d9647de57..379aae86812 100644
--- a/config/vufind/config.ini
+++ b/config/vufind/config.ini
@@ -450,6 +450,14 @@ sms = enabled
 
 ; This section needs to be changed to match your database connection information
 [Database]
+; Connection string format is [platform]://[username]:[password]@[host]:[port]/[db]
+; where:
+; [platform] = database platform (mysql, oci8 or pgsql)
+; [username] = username for connection
+; [password] = password for connection (optional)
+; [host] = host of database server
+; [port] = port of database server (optional)
+; [db] = database name
 database          = mysql://root@localhost/vufind
 
 ; If your database (e.g. PostgreSQL) uses a schema, you can set it here:
diff --git a/module/VuFind/src/VuFind/Db/AdapterFactory.php b/module/VuFind/src/VuFind/Db/AdapterFactory.php
index edf0afd7626..22c07fd2ecb 100644
--- a/module/VuFind/src/VuFind/Db/AdapterFactory.php
+++ b/module/VuFind/src/VuFind/Db/AdapterFactory.php
@@ -146,7 +146,13 @@ class AdapterFactory
         list($type, $details) = explode('://', $connectionString);
         preg_match('/(.+)@([^@]+)\/(.+)/', $details, $matches);
         $credentials = isset($matches[1]) ? $matches[1] : null;
-        $host = isset($matches[2]) ? $matches[2] : null;
+        if (isset($matches[2])) {
+            if (strpos($matches[2], ':') !== false) {
+                list($host, $port) = explode(':', $matches[2]);
+            } else {
+                $host = $matches[2];
+            }
+        }
         $dbName = isset($matches[3]) ? $matches[3] : null;
         if (strstr($credentials, ':')) {
             list($username, $password) = explode(':', $credentials, 2);
@@ -160,12 +166,14 @@ class AdapterFactory
         // Set up default options:
         $options = [
             'driver' => $this->getDriverName($type),
-            'hostname' => $host,
+            'hostname' => isset($host) ? $host : null,
             'username' => $username,
             'password' => $password,
             'database' => $dbName
         ];
-
+        if (!empty($port)) {
+            $options['port'] = $port;
+        }
         return $this->getAdapterFromOptions($options);
     }
 }
-- 
GitLab