From 7c65844bc9af8ddc4ee393aa8b1080ff1a49026f Mon Sep 17 00:00:00 2001
From: Bob Vincent <bobvin@pillars.net>
Date: Sun, 25 Sep 2011 12:08:56 -0400
Subject: [PATCH] Issue #728702: Visiting index.php should redirect to
 install.php if settings.php already has database
 credentials but database is empty.

---
 includes/bootstrap.inc |   36 ++++++++++++++++++++++++------------
 1 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 9deacf37311b3bb8dea50e7d211a3130faaef881..7d58501a08db9292ff90a6c5fcb8af730adf7066 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -903,19 +903,31 @@ function variable_initialize($conf = array()) {
     $variables = $cached->data;
   }
   else {
-    // Cache miss. Avoid a stampede.
-    $name = 'variable_init';
-    if (!lock_acquire($name, 1)) {
-      // Another request is building the variable cache.
-      // Wait, then re-run this function.
-      lock_wait($name);
-      return variable_initialize($conf);
+    try {
+      // Cache miss. Avoid a stampede.
+      $name = 'variable_init';
+      if (!lock_acquire($name, 1)) {
+        // Another request is building the variable cache.
+        // Wait, then re-run this function.
+        lock_wait($name);
+        return variable_initialize($conf);
+      }
+      else {
+        // Proceed with variable rebuild.
+        $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')->fetchAllKeyed());
+        cache('bootstrap')->set('variables', $variables);
+        lock_release($name);
+      }
     }
-    else {
-      // Proceed with variable rebuild.
-      $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable}')->fetchAllKeyed());
-      cache('bootstrap')->set('variables', $variables);
-      lock_release($name);
+    catch (PDOException $e) {
+      // A PDO exception trying to acquire the lock might mean that we are
+      // running on an empty database. In that case, just redirect the user to
+      // install.php, unless we're already there.
+      if (!db_table_exists('variable') && !drupal_installation_attempted()) {
+        include_once DRUPAL_ROOT . '/includes/install.inc';
+        install_goto('install.php');
+      }
+      throw $e;
     }
   }
 
-- 
1.7.5.4

