[TASK] Change usage of t3lib_DB->sql_pconnect to t3lib_DB->connectDB
authorThomas Maroschik <tmaroschik@dfau.de>
Wed, 6 Jul 2011 16:35:44 +0000 (18:35 +0200)
committerSusanne Moog <typo3@susannemoog.de>
Thu, 7 Jul 2011 16:27:45 +0000 (18:27 +0200)
Currently many entry points that connect to the database use sql_pconnect
for that. This method is a low level sql method that skips hook initialization.
Also duplicate code was spread all over the entry points and are rewritten
to use the exceptions from t3lib_DB->connectDB.

Change-Id: I5b3cdecfed018be43236d1d54feac47a29333213
Resolves: #27954
Reviewed-on: http://review.typo3.org/3059
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
Reviewed-by: Björn Pedersen
Reviewed-by: Thomas Maroschik
Tested-by: Thomas Maroschik
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
typo3/init.php
typo3/sysext/cms/tslib/class.tslib_eidtools.php
typo3/sysext/cms/tslib/class.tslib_fe.php
typo3/sysext/cms/tslib/showpic.php

index 7d33a5a..d679047 100644 (file)
@@ -356,14 +356,8 @@ if (TYPO3_REQUESTTYPE & TYPO3_REQUESTTYPE_INSTALL) {
        // Redirect to install tool if database host and database are not defined
 if (!TYPO3_db_host && !TYPO3_db) {
        t3lib_utility_Http::redirect('install/index.php?mode=123&step=1&password=joh316');
-} elseif ($TYPO3_DB->sql_pconnect(TYPO3_db_host, TYPO3_db_username, TYPO3_db_password)) {
-       if (!TYPO3_db)  {
-               throw new RuntimeException('Database Error: No database selected', 1294587021);
-       } elseif (!$TYPO3_DB->sql_select_db(TYPO3_db))  {
-               throw new RuntimeException('Database Error: Cannot connect to the current database, "' . TYPO3_db . '"', 1294587022);
-       }
 } else {
-       throw new RuntimeException('Database Error: The current username, password or host was not accepted when the connection to the database was attempted to be established!', time());
+       $TYPO3_DB->connectDB();
 }
 
 
index 1260441..b360b54 100644 (file)
@@ -90,16 +90,13 @@ final class tslib_eidtools {
         * @return      boolean         TRUE if connection was successful
         */
        public static function connectDB()      {
-               static $dbConnected = FALSE;
-
-               if (!$dbConnected) {
-                       // Attempt to connect to the database
-                       if ($GLOBALS['TYPO3_DB']->sql_pconnect(TYPO3_db_host, TYPO3_db_username, TYPO3_db_password) &&
-                                       $GLOBALS['TYPO3_DB']->sql_select_db(TYPO3_db)) {
-                               $dbConnected = TRUE;
-                       }
+               if (!$GLOBALS['TYPO3_DB']->isConnected()) {
+                               // Attempt to connect to the database
+                       $GLOBALS['TYPO3_DB']->connectDB();
                }
-               return $dbConnected;
+                       // connectDB() throws exceptions if something went wrong,
+                       // so we are sure that connect was successful here.
+               return TRUE;
        }
 
        /**
index c292462..19b48e4 100644 (file)
        }
 
        /**
-        * Connect to SQL database
-        * May exit after outputting an error message or some JavaScript redirecting to the install tool.
+        * Connect to SQL database. May exit after outputting an error message
+        * or some JavaScript redirecting to the install tool.
         *
-        * @return      void
+        * @return void
         */
-       function connectToDB()  {
-               if (!TYPO3_db) {
-                               // jump into Install Tool 1-2-3 mode, if no DB name is defined (fresh installation)
-                       t3lib_utility_Http::redirect(TYPO3_mainDir.'install/index.php?mode=123&step=1&password=joh316');
-               }
-
-                       // sql_pconnect() can throw an Exception in case of some failures, or it returns FALSE
-               $link = $GLOBALS['TYPO3_DB']->sql_pconnect(TYPO3_db_host, TYPO3_db_username, TYPO3_db_password);
-               if ($link !== FALSE) {
-                               // Connection to DB server ok, now select the database
-                       if (!$GLOBALS['TYPO3_DB']->sql_select_db(TYPO3_db)) {
-                               $message = 'Cannot connect to the configured database "'.TYPO3_db.'"';
-                               if ($this->checkPageUnavailableHandler()) {
-                                       $this->pageUnavailableAndExit($message);
-                               } else {
-                                       t3lib_div::sysLog($message, 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
-                                       throw new t3lib_error_http_ServiceUnavailableException($message, 1301648782);
-                               }
-                       }
-               } else {
-                       $message = 'The current username, password or host was not accepted when the connection to the database was attempted to be established!';
-                       if ($this->checkPageUnavailableHandler()) {
-                               $this->pageUnavailableAndExit($message);
-                       } else {
-                               t3lib_div::sysLog($message, 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
-                               throw new t3lib_error_http_ServiceUnavailableException('Database Error: ' . $message, 1301648945);
+       public function connectToDB() {
+               try {
+                       $GLOBALS['TYPO3_DB']->connectDB();
+               } catch (RuntimeException $exception) {
+                       switch ($exception->getCode()) {
+                               case 1270853882:
+                                               // No database selected: Redirect to Install Tool 1-2-3 mode (fresh installation)
+                                       t3lib_utility_Http::redirect(TYPO3_mainDir . 'install/index.php?mode=123&step=1&password=joh316');
+                                       break;
+                               case 1270853883:
+                                               // Cannot connect to current database
+                                       $message = 'Cannot connect to the configured database "' . TYPO3_db . '"';
+                                       if ($this->checkPageUnavailableHandler()) {
+                                               $this->pageUnavailableAndExit($message);
+                                       } else {
+                                               t3lib_div::sysLog($message, 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
+                                               throw new t3lib_error_http_ServiceUnavailableException($message, 1301648782);
+                                       }
+                                       break;
+                               case 1270853884:
+                                                       // Username / password not accepted
+                                               $message = 'The current username, password or host was not accepted when' .
+                                                       ' the connection to the database was attempted to be established!';
+                                               if ($this->checkPageUnavailableHandler()) {
+                                                       $this->pageUnavailableAndExit($message);
+                                               } else {
+                                                       t3lib_div::sysLog($message, 'cms', t3lib_div::SYSLOG_SEVERITY_ERROR);
+                                                       throw new t3lib_error_http_ServiceUnavailableException(
+                                                               'Database Error: ' . $message,
+                                                               1301648945
+                                                       );
+                                               }
+                                       break;
+                               default:
+                                       throw $exception;
                        }
                }
 
-
                        // Call post processing function for DB connection:
                if (is_array($this->TYPO3_CONF_VARS['SC_OPTIONS']['tslib/class.tslib_fe.php']['connectToDB']))  {
                        $_params = array('pObj' => &$this);
index 15153f2..6b8b1a7 100644 (file)
@@ -219,8 +219,7 @@ class SC_tslib_showpic {
                }
 
                // Need to connect to database, because this is used (typo3temp_db_tracking, cached image dimensions).
-               $GLOBALS['TYPO3_DB']->sql_pconnect(TYPO3_db_host, TYPO3_db_username, TYPO3_db_password);
-               $GLOBALS['TYPO3_DB']->sql_select_db(TYPO3_db);
+               $GLOBALS['TYPO3_DB']->connectDB();
 
                if (strstr($this->width . $this->height, 'm')) {
                        $max = 'm';