[BUGFIX] Support mysql socket connection 33/20933/8
authorPhilipp Gampe <philipp.gampe@typo3.org>
Mon, 20 May 2013 14:58:20 +0000 (16:58 +0200)
committerMarkus Klein <klein.t3@mfc-linz.at>
Fri, 5 Jul 2013 19:54:38 +0000 (21:54 +0200)
With the switch to mysqli in 6.1, the connection to mysql server via
socket broke.

With mysqli, the definition is now:
* If hostname is "localhost", a socket is used and the socket file
can be given optionally if it is different from the default.
* If hostname is not "localhost" but an IP address, a port can be
given that may be different from the default mysql 3306.

The patch now reflects this in the database connect step and adds a
parameter for the optional socket location. The patch will be adapted
for 6.1 to include only the additional optional socket parameter.

Resolves: #48117
Related: #49162
Releases: 6.2, 6.1
Change-Id: I968a72dbaf90b3f319c03e697137630534d325c1
Reviewed-on: https://review.typo3.org/20933
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
Reviewed-by: Markus Klein
Tested-by: Markus Klein
typo3/sysext/core/Classes/Core/Bootstrap.php
typo3/sysext/core/Classes/Database/DatabaseConnection.php
typo3/sysext/install/Classes/Controller/Action/AbstractAction.php
typo3/sysext/install/Classes/Controller/Action/Step/DatabaseConnect.php
typo3/sysext/install/Classes/Controller/Action/Step/DatabaseSelect.php
typo3/sysext/install/Classes/Controller/Action/Tool/ImportantActions.php
typo3/sysext/install/Resources/Private/Partials/Action/Step/DatabaseConnect/ConnectDetails.html
typo3/sysext/install/Resources/Private/Partials/Action/Tool/ImportantActions/SystemInformation.html
typo3/sysext/install/Resources/Public/Javascript/Install.js

index 1af33bb..fec4c07 100644 (file)
@@ -678,7 +678,6 @@ class Bootstrap {
        /**
         * Initialize database connection in $GLOBALS and connect if requested
         *
-        * @param boolean $connect Whether db should be connected
         * @return \TYPO3\CMS\Core\Core\Bootstrap
         * @internal This is not a public API method, do not use in own extensions
         */
@@ -697,6 +696,9 @@ class Bootstrap {
                        list($databaseHost, $databasePort) = explode(':', $databaseHost);
                        $databaseConnection->setDatabasePort($databasePort);
                }
+               if (isset($GLOBALS['TYPO3_CONF_VARS']['DB']['socket'])) {
+                       $databaseConnection->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
+               }
                $databaseConnection->setDatabaseHost($databaseHost);
 
                $databaseConnection->debugOutput = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sqlDebug'];
index 31a6650..326084a 100644 (file)
@@ -106,6 +106,11 @@ class DatabaseConnection {
        protected $databasePort = 3306;
 
        /**
+        * @var string|NULL Database socket to connect to
+        */
+       protected $databaseSocket = NULL;
+
+       /**
         * @var string Database name to connect to
         */
        protected $databaseName = '';
@@ -1184,7 +1189,7 @@ class DatabaseConnection {
                        $this->databaseUserPassword,
                        NULL,
                        (int)$this->databasePort,
-                       NULL,
+                       $this->databaseSocket,
                        $this->connectionCompression ? MYSQLI_CLIENT_COMPRESS : 0
                );
 
@@ -1438,6 +1443,16 @@ class DatabaseConnection {
        }
 
        /**
+        * Set database socket
+        *
+        * @param string|NULL $socket
+        */
+       public function setDatabaseSocket($socket = NULL) {
+               $this->disconnectIfConnected();
+               $this->databaseSocket = $socket;
+       }
+
+       /**
         * Set database name
         *
         * @param string $name
@@ -1814,6 +1829,7 @@ class DatabaseConnection {
                        'explainOutput',
                        'databaseHost',
                        'databasePort',
+                       'databaseSocket',
                        'databaseName',
                        'databaseUsername',
                        'databaseUserPassword',
@@ -1824,4 +1840,4 @@ class DatabaseConnection {
                );
        }
 }
-?>
+?>
\ No newline at end of file
index df894b2..cbd6764 100644 (file)
@@ -185,6 +185,7 @@ abstract class AbstractAction {
                        $database->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
                        $database->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
                        $database->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
+                       $database->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
                        $database->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
                        $database->connectDB();
                }
@@ -208,4 +209,4 @@ abstract class AbstractAction {
                        ->loadExtensionTables(FALSE);
        }
 }
-?>
+?>
\ No newline at end of file
index b43d60d..a8d32c9 100644 (file)
@@ -134,7 +134,7 @@ class DatabaseConnect extends Action\AbstractAction implements StepInterface {
                                }
                        }
 
-                       if (isset($postValues['port'])) {
+                       if (isset($postValues['port']) && $postValues['host'] !== 'localhost') {
                                $value = $postValues['port'];
                                if (preg_match('/^[0-9]+(:.+)?$/', $value) && $value > 0 && $value <= 65535) {
                                        $localConfigurationPathValuePairs['DB/port'] = (int)$value;
@@ -142,7 +142,19 @@ class DatabaseConnect extends Action\AbstractAction implements StepInterface {
                                        /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
                                        $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
                                        $errorStatus->setTitle('Database port not valid');
-                                       $errorStatus->setMessage('Given port is not numeric or within range 1 to 65535');
+                                       $errorStatus->setMessage('Given port is not numeric or within range 1 to 65535.');
+                                       $result[] = $errorStatus;
+                               }
+                       }
+
+                       if (isset($postValues['socket']) && $postValues['socket'] !== '') {
+                               if (@file_exists($postValues['socket'])) {
+                                       $localConfigurationPathValuePairs['DB/socket'] = $postValues['socket'];
+                               } else {
+                                       /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
+                                       $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
+                                       $errorStatus->setTitle('Socket does not exist');
+                                       $errorStatus->setMessage('Given socket location does not exist on server.');
                                        $result[] = $errorStatus;
                                }
                        }
@@ -196,7 +208,7 @@ class DatabaseConnect extends Action\AbstractAction implements StepInterface {
                        return TRUE;
                }
                if (!isset($GLOBALS['TYPO3_CONF_VARS']['DB']['host'])
-                       || !isset($GLOBALS['TYPO3_CONF_VARS']['DB']['port'])
+                       || (!isset($GLOBALS['TYPO3_CONF_VARS']['DB']['port']) && $GLOBALS['TYPO3_CONF_VARS']['DB']['host'] !== 'localhost')
                ) {
                        return TRUE;
                }
@@ -218,7 +230,8 @@ class DatabaseConnect extends Action\AbstractAction implements StepInterface {
                        ->assign('password', $GLOBALS['TYPO3_CONF_VARS']['DB']['password'] ?: '')
                        ->assign('host', $this->getConfiguredHost() ?: '127.0.0.1')
                        ->assign('port', $this->getConfiguredOrDefaultPort())
-                       ->assign('database', $GLOBALS['TYPO3_CONF_VARS']['DB']['database'] ?: '');
+                       ->assign('database', $GLOBALS['TYPO3_CONF_VARS']['DB']['database'] ?: '')
+                       ->assign('socket', $GLOBALS['TYPO3_CONF_VARS']['DB']['socket'] ?: '');
 
                if ($isDbalEnabled) {
                        $this->view->assign('selectedDbalDriver', $this->getSelectedDbalDriver());
@@ -229,7 +242,8 @@ class DatabaseConnect extends Action\AbstractAction implements StepInterface {
                                ->assign('renderConnectDetailsUsername', TRUE)
                                ->assign('renderConnectDetailsPassword', TRUE)
                                ->assign('renderConnectDetailsHost', TRUE)
-                               ->assign('renderConnectDetailsPort', TRUE);
+                               ->assign('renderConnectDetailsPort', TRUE)
+                               ->assign('renderConnectDetailsSocket', TRUE);
                }
 
                return $this->view->render();
@@ -329,6 +343,7 @@ class DatabaseConnect extends Action\AbstractAction implements StepInterface {
                $databaseConnection->setDatabasePassword($password);
                $databaseConnection->setDatabaseHost($this->getConfiguredHost());
                $databaseConnection->setDatabasePort($this->getConfiguredPort());
+               $databaseConnection->setDatabaseSocket($this->getConfiguredSocket());
 
                $result = FALSE;
                if (@$databaseConnection->sql_pconnect()) {
@@ -487,5 +502,15 @@ class DatabaseConnect extends Action\AbstractAction implements StepInterface {
                }
                return (int)$port;
        }
+
+       /**
+        * Returns configured socket, if set
+        *
+        * @return string|NULL
+        */
+       protected function getConfiguredSocket() {
+               $socket = isset($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']) ? $GLOBALS['TYPO3_CONF_VARS']['DB']['socket'] : NULL;
+               return $socket;
+       }
 }
-?>
+?>
\ No newline at end of file
index da4213c..3a76d8b 100644 (file)
@@ -153,6 +153,7 @@ class DatabaseSelect extends Action\AbstractAction implements StepInterface {
                $this->databaseConnection->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
                $this->databaseConnection->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
                $this->databaseConnection->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
+               $this->databaseConnection->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
                $this->databaseConnection->sql_pconnect();
        }
 }
index 2f5df93..198ba86 100644 (file)
@@ -82,6 +82,7 @@ class ImportantActions extends Action\AbstractAction implements Action\ActionInt
                        ->assign('databaseUsername', $GLOBALS['TYPO3_CONF_VARS']['DB']['username'])
                        ->assign('databaseHost', $GLOBALS['TYPO3_CONF_VARS']['DB']['host'])
                        ->assign('databasePort', $GLOBALS['TYPO3_CONF_VARS']['DB']['port'])
+                       ->assign('databaseSocket', $GLOBALS['TYPO3_CONF_VARS']['DB']['socket'])
                        ->assign('databaseNumberOfTables', count($this->getDatabase()->admin_get_tables()));
 
                return $this->view->render();
index aeadac0..83a2224 100644 (file)
@@ -7,35 +7,49 @@
                        <f:if condition="{renderConnectDetailsUsername}">
                                <li>
                                        <label for="t3-install-step-username">Username</label>
-                                       <input id="t3-install-step-username" class="t3-install-form-input-text" type="text" value="{username}" name="install[values][username]">
+                                       <input id="t3-install-step-username" class="t3-install-form-input-text" type="text" value="{username}" name="install[values][username]" />
                                </li>
                        </f:if>
 
                        <f:if condition="{renderConnectDetailsPassword}">
                                <li>
                                        <label for="t3-install-step-password">Password</label>
-                                       <input id="t3-install-step-password" class="t3-install-form-input-text" type="password" value="{password}" name="install[values][password]">
+                                       <input id="t3-install-step-password" class="t3-install-form-input-text" type="password" value="{password}" name="install[values][password]" />
                                </li>
                        </f:if>
 
                        <f:if condition="{renderConnectDetailsHost}">
                                <li>
+                                       <label for="t3-install-step-type">Type</label>
+                                       <select id="t3-install-step-type">
+                                               <option value="tcp">TCP/IP based connection</option>
+                                               <option value="socket" {f:if(condition:'{host} == \'localhost\'',then:'selected="selected"')}>Socket based connection</option>
+                                       </select>
+                               </li>
+                               <li>
                                        <label for="t3-install-step-host">Host</label>
-                                       <input id="t3-install-step-host" class="t3-install-form-input-text" type="text" value="{host}" name="install[values][host]">
+                                       <input id="t3-install-step-host" class="t3-install-form-input-text" type="text" value="{host}" name="install[values][host]" />
                                </li>
                        </f:if>
 
                        <f:if condition="{renderConnectDetailsPort}">
                                <li>
                                        <label for="t3-install-step-port">Port</label>
-                                       <input id="t3-install-step-port" class="t3-install-form-input-text" type="text" value="{port}" name="install[values][port]">
+                                       <input id="t3-install-step-port" class="t3-install-form-input-text" type="text" value="{port}" name="install[values][port]" />
+                               </li>
+                       </f:if>
+
+                       <f:if condition="{renderConnectDetailsSocket}">
+                               <li>
+                                       <label for="t3-install-step-socket">Socket</label>
+                                       <input id="t3-install-step-socket" class="t3-install-form-input-text" type="text" value="{socket}" name="install[values][socket]" placeholder="Default socket or enter name" />
                                </li>
                        </f:if>
 
                        <f:if condition="{renderConnectDetailsDatabase}">
                                <li>
                                        <label for="t3-install-step-database">Database</label>
-                                       <input id="t3-install-step-database" class="t3-install-form-input-text" type="text" value="{database}" name="install[values][database]">
+                                       <input id="t3-install-step-database" class="t3-install-form-input-text" type="text" value="{database}" name="install[values][database]" />
                                </li>
                        </f:if>
 
index 58fd11a..efbf3dd 100644 (file)
@@ -17,6 +17,8 @@
        <dd>{databaseHost} </dd>
        <dt>Database port:</dt>
        <dd>{databasePort} </dd>
+       <dt>Database socket:</dt>
+       <dd>{databaseSocket} </dd>
        <dt>Database tables:</dt>
        <dd>{databaseNumberOfTables} </dd>
-</dl>
\ No newline at end of file
+</dl>
index 23ae3c2..2750b95 100644 (file)
@@ -57,4 +57,24 @@ $(document).ready(function() {
                }
        });
 
+       $('#t3-install-step-type').change(function() {
+               var connectionType = $(this).val(),
+                       hostField = $('#t3-install-step-host'),
+                       portField = $('#t3-install-step-port'),
+                       socketField = $('#t3-install-step-socket');
+
+               if (connectionType === 'socket') {
+                       hostField.parent().fadeOut();
+                       hostField.val('localhost');
+                       portField.parent().fadeOut();
+                       socketField.parent().fadeIn();
+               } else {
+                       hostField.parent().fadeIn();
+                       if (hostField.val() === 'localhost') {
+                               hostField.val('127.0.0.1');
+                       }
+                       portField.parent().fadeIn();
+                       socketField.parent().fadeOut();
+               }
+       }).trigger('change');
 });
\ No newline at end of file