﻿Index: includes/database.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.inc,v
retrieving revision 1.85
diff -u -p -r1.85 database.inc
--- includes/database.inc	30 Nov 2007 12:19:10 -0000	1.85
+++ includes/database.inc	13 Dec 2007 22:51:03 -0000
@@ -146,11 +146,7 @@ function db_set_active($name = 'default'
       include_once $handler;
     }
     else {
-      drupal_maintenance_theme();
-      drupal_set_title('Unsupported database type');
-      print theme('maintenance_page', '<p>The database type '. theme('placeholder', $db_type) .' is unsupported. Please use either <var>mysql</var> for MySQL 3.x &amp; 4.0.x databases, <var>mysqli</var> for MySQL 4.1.x+ databases, or <var>pgsql</var> for PostgreSQL databases. The database information is in your <code>settings.php</code> file.</p>
-<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>');
-      exit;
+      _db_error_page("The database type '". $db_type ."' is unsupported. Please use either 'mysql' or 'mysqli' for MySQL, or 'pgsql' for PostgreSQL databases.");
     }
 
     $db_conns[$name] = db_connect($connect_url);
@@ -164,6 +160,33 @@ function db_set_active($name = 'default'
 }
 
 /**
+ * Helper function to show fatal database errors.
+ *
+ * Prints a themed maintenance page with the 'Site off-line' text,
+ * adding the provided error message in the case of 'display_errors'
+ * set to on. Ends the page request; no return.
+ *
+ * @param $error
+ *   The error message to be appended if 'display_errors' is on.
+ */
+function _db_error_page($error = '') {
+  global $db_type;
+  drupal_maintenance_theme();
+  drupal_set_header('HTTP/1.1 503 Service Unavailable');
+  drupal_set_title('Site off-line');
+
+  $message = '<p>The site is currently not available due to technical problems. Please try again later. Thank you for your understanding.</p>';
+  $message .= '<hr /><p><small>If you are the maintainer of this site, please check your database settings in the <code>settings.php</code> file and ensure that your hosting provider\'s database server is running. For more help, see the <a href="http://drupal.org/node/258">handbook</a>, or contact your hosting provider.</small></p>';
+
+  if ($error && ini_get('display_errors')) {
+    $message .= '<p><small>The '. theme('placeholder', $db_type) .' error was: '. theme('placeholder', $error) .'.</small></p>';
+  }
+
+  print theme('maintenance_page', $message);
+  exit;
+}
+
+/**
  * Returns a boolean depending on the availability of the database.
  */
 function db_is_active() {
Index: includes/database.mysql.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.mysql.inc,v
retrieving revision 1.84
diff -u -p -r1.84 database.mysql.inc
--- includes/database.mysql.inc	8 Dec 2007 14:06:20 -0000	1.84
+++ includes/database.mysql.inc	13 Dec 2007 22:51:03 -0000
@@ -53,11 +53,7 @@ function db_connect($url) {
 
   // Check if MySQL support is present in PHP
   if (!function_exists('mysql_connect')) {
-    drupal_maintenance_theme();
-    drupal_set_title('PHP MySQL support not enabled');
-    print theme('maintenance_page', '<p>We were unable to use the MySQL database because the MySQL extension for PHP is not installed. Check your <code>PHP.ini</code> to see how you can enable it.</p>
-<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>');
-    exit;
+    _db_error_page('Unable to use the MySQL database because the MySQL extension for PHP is not installed. Check your <code>php.ini</code> to see how you can enable it.');
   }
 
   // Decode url-encoded information in the db connection string
@@ -84,37 +80,9 @@ function db_connect($url) {
   // - 2 means CLIENT_FOUND_ROWS: return the number of found
   //   (matched) rows, not the number of affected rows.
   $connection = @mysql_connect($url['host'], $url['user'], $url['pass'], TRUE, 2);
-  if (!$connection) {
+  if (!$connection || !mysql_select_db(substr($url['path'], 1))) {
     // Show error screen otherwise
-    drupal_maintenance_theme();
-    drupal_set_header('HTTP/1.1 503 Service Unavailable');
-    drupal_set_title('Unable to connect to database server');
-    print theme('maintenance_page', '<p>If you still have to install Drupal, proceed to the <a href="'. base_path() .'install.php">installation page</a>.</p>
-<p>If you have already finished installing Drupal, this either means that the username and password information in your <code>settings.php</code> file is incorrect or that we can\'t connect to the MySQL database server. This could mean your hosting provider\'s database server is down.</p>
-<p>The MySQL error was: '. theme('placeholder', mysql_error()) .'.</p>
-<p>Currently, the username is '. theme('placeholder', $url['user']) .' and the database server is '. theme('placeholder', $url['host']) .'.</p>
-<ul>
-  <li>Are you sure you have the correct username and password?</li>
-  <li>Are you sure that you have typed the correct hostname?</li>
-  <li>Are you sure that the database server is running?</li>
-</ul>
-<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>');
-    exit;
-  }
-
-  if (!mysql_select_db(substr($url['path'], 1))) {
-    drupal_maintenance_theme();
-    drupal_set_title('Unable to select database');
-    print theme('maintenance_page', '<p>We were able to connect to the MySQL database server (which means your username and password are okay) but not able to select the database.</p>
-<p>The MySQL error was: '. theme('placeholder', mysql_error($connection)) .'.</p>
-<p>Currently, the database is '. theme('placeholder', substr($url['path'], 1)) .'. The username is '. theme('placeholder', $url['user']) .' and the database server is '. theme('placeholder', $url['host']) .'.</p>
-<ul>
-  <li>Are you sure you have the correct database name?</li>
-  <li>Are you sure the database exists?</li>
-  <li>Are you sure the username has permission to access the database?</li>
-</ul>
-<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>');
-    exit;
+    _db_error_page(mysql_error());
   }
 
   /* On MySQL 4.1 and later, force UTF-8 */
Index: includes/database.mysqli.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.mysqli.inc,v
retrieving revision 1.50
diff -u -p -r1.50 database.mysqli.inc
--- includes/database.mysqli.inc	8 Dec 2007 14:06:20 -0000	1.50
+++ includes/database.mysqli.inc	13 Dec 2007 22:51:04 -0000
@@ -58,11 +58,7 @@ function db_version() {
 function db_connect($url) {
   // Check if MySQLi support is present in PHP
   if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
-    drupal_maintenance_theme();
-    drupal_set_title('PHP MySQLi support not enabled');
-    print theme('maintenance_page', '<p>We were unable to use the MySQLi database because the MySQLi extension for PHP is not installed. Check your <code>PHP.ini</code> to see how you can enable it.</p>
-<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>');
-    exit;
+    _db_error_page('Unable to use the MySQLi database because the MySQLi extension for PHP is not installed. Check your <code>php.ini</code> to see how you can enable it.');
   }
 
   $url = parse_url($url);
@@ -85,37 +81,8 @@ function db_connect($url) {
   $connection = mysqli_init();
   @mysqli_real_connect($connection, $url['host'], $url['user'], $url['pass'], substr($url['path'], 1), $url['port'], NULL, MYSQLI_CLIENT_FOUND_ROWS);
 
-  // Find all database connection errors and error 1045 for access denied for user account
-  if (mysqli_connect_errno() >= 2000 || mysqli_connect_errno() == 1045) {
-    drupal_maintenance_theme();
-    drupal_set_header('HTTP/1.1 503 Service Unavailable');
-    drupal_set_title('Unable to connect to database server');
-    print theme('maintenance_page', '<p>If you still have to install Drupal, proceed to the <a href="'. base_path() .'install.php">installation page</a>.</p>
-<p>If you have already finished installing Drupal, this either means that the username and password information in your <code>settings.php</code> file is incorrect or that we can\'t connect to the MySQL database server. This could mean your hosting provider\'s database server is down.</p>
-<p>The MySQL error was: '. theme('placeholder', mysqli_connect_error()) .'.</p>
-<p>Currently, the username is '. theme('placeholder', $url['user']) .' and the database server is '. theme('placeholder', $url['host']) .'.</p>
-<ul>
-  <li>Are you sure you have the correct username and password?</li>
-  <li>Are you sure that you have typed the correct hostname?</li>
-  <li>Are you sure that the database server is running?</li>
-  <li>Are you sure that the mysqli libraries are compiled in your PHP installation? Try using the mysql library instead by editing your <code>settings.php</code> configuration file in Drupal.</li>
-</ul>
-<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>');
-    exit;
-  }
-  else if (mysqli_connect_errno() > 0) {
-    drupal_maintenance_theme();
-    drupal_set_title('Unable to select database');
-    print theme('maintenance_page', '<p>We were able to connect to the MySQL database server (which means your username and password are okay) but not able to select the database.</p>
-<p>The MySQL error was: '. theme('placeholder', mysqli_connect_error()) .'.</p>
-<p>Currently, the database is '. theme('placeholder', substr($url['path'], 1)) .'. The username is '. theme('placeholder', $url['user']) .' and the database server is '. theme('placeholder', $url['host']) .'.</p>
-<ul>
-  <li>Are you sure you have the correct database name?</li>
-  <li>Are you sure the database exists?</li>
-  <li>Are you sure the username has permission to access the database?</li>
-</ul>
-<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>');
-    exit;
+  if (mysqli_connect_errno() > 0) {
+    _db_error_page(mysqli_connect_error());
   }
 
   /* Force UTF-8 */
Index: includes/database.pgsql.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database.pgsql.inc,v
retrieving revision 1.63
diff -u -p -r1.63 database.pgsql.inc
--- includes/database.pgsql.inc	17 Oct 2007 12:47:28 -0000	1.63
+++ includes/database.pgsql.inc	13 Dec 2007 22:51:05 -0000
@@ -47,11 +47,7 @@ function db_version() {
 function db_connect($url) {
   // Check if PostgreSQL support is present in PHP
   if (!function_exists('pg_connect')) {
-    drupal_maintenance_theme();
-    drupal_set_title('PHP PostgreSQL support not enabled');
-    print theme('maintenance_page', '<p>We were unable to use the PostgreSQL database because the PostgreSQL extension for PHP is not installed. Check your <code>PHP.ini</code> to see how you can enable it.</p>
-<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>');
-    exit;
+    _db_error_page('Unable to use the PostgreSQL database because the PostgreSQL extension for PHP is not installed. Check your <code>php.ini</code> to see how you can enable it.');
   }
 
   $url = parse_url($url);
@@ -82,21 +78,8 @@ function db_connect($url) {
 
   $connection = @pg_connect($conn_string);
   if (!$connection) {
-    drupal_maintenance_theme();
-    drupal_set_header('HTTP/1.1 503 Service Unavailable');
-    drupal_set_title('Unable to connect to database');
-    print theme('maintenance_page', '<p>If you still have to install Drupal, proceed to the <a href="'. base_path() .'install.php">installation page</a>.</p>
-<p>If you have already finished installing Drupal, this either means that the username and password information in your <code>settings.php</code> file is incorrect or that we can\'t connect to the PostgreSQL database server. This could mean your hosting provider\'s database server is down.</p>
-<p>The PostgreSQL error was: '. theme('placeholder', decode_entities($php_errormsg)) .'</p>
-<p>Currently, the database is '. theme('placeholder', substr($url['path'], 1)) .', the username is '. theme('placeholder', $url['user']) .', and the database server is '. theme('placeholder', $url['host']) .'.</p>
-<ul>
-  <li>Are you sure you have the correct username and password?</li>
-  <li>Are you sure that you have typed the correct hostname?</li>
-  <li>Are you sure you have the correct database name?</li>
-  <li>Are you sure that the database server is running?</li>
-</ul>
-<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>');
-    exit;
+    require_once './includes/unicode.inc';
+    _db_error_page(decode_entities($php_errormsg));
   }
 
   // Restore error tracking setting
