diff -r'uNF^f' -x CVS drupal/includes/bootstrap.inc ../drupal/includes/bootstrap.inc
--- drupal/includes/bootstrap.inc	2005-05-21 13:33:03.000000000 +0200
+++ ../drupal/includes/bootstrap.inc	2005-06-04 18:09:14.000000000 +0200
@@ -76,56 +76,6 @@ function timer_stop($name) {
 }
 
 /**
- * Locate the appropriate configuration file.
- *
- * Try finding a matching configuration directory by stripping the
- * website's hostname from left to right and pathname from right to
- * left.  The first configuration file found will be used, the
- * remaining will ignored.  If no configuration file is found,
- * return a default value '$confdir/default'.
- *
- * Example for a fictitious site installed at
- * http://www.drupal.org/mysite/test/ the 'settings.php' is
- * searched in the following directories:
- *
- *  1. $confdir/www.drupal.org.mysite.test
- *  2. $confdir/drupal.org.mysite.test
- *  3. $confdir/org.mysite.test
- *
- *  4. $confdir/www.drupal.org.mysite
- *  5. $confdir/drupal.org.mysite
- *  6. $confdir/org.mysite
- *
- *  7. $confdir/www.drupal.org
- *  8. $confdir/drupal.org
- *  9. $confdir/org
- *
- * 10. $confdir/default
- */
-function conf_init() {
-  static $conf = '';
-
-  if ($conf) {
-    return $conf;
-  }
-
-  $confdir = 'sites';
-  $uri = explode('/', $_SERVER['PHP_SELF']);
-  $server = explode('.', rtrim($_SERVER['HTTP_HOST'], '.'));
-  for ($i = count($uri) - 1; $i > 0; $i--) {
-    for ($j = count($server); $j > 0; $j--) {
-      $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
-      if (file_exists("$confdir/$dir/settings.php")) {
-        $conf = "$confdir/$dir";
-        return $conf;
-      }
-    }
-  }
-  $conf = "$confdir/default";
-  return $conf;
-}
-
-/**
  * Returns and optionally sets the filename for a system item (module,
  * theme, etc.).  The filename, whether provided, cached, or retrieved
  * from the database, is only returned if the file exists.
@@ -633,25 +583,6 @@ function bootstrap_hooks() {
 }
 
 /**
- * Unserializes and appends elements from a serialized string.
- *
- * @param $obj
- *   The object to which the elements are appended.
- * @param $field
- *   The attribute of $obj whose value should be unserialized.
- */
-function drupal_unpack($obj, $field = 'data') {
-  if ($obj->$field && $data = unserialize($obj->$field)) {
-    foreach ($data as $key => $value) {
-      if (!isset($obj->$key)) {
-        $obj->$key = $value;
-      }
-    }
-  }
-  return $obj;
-}
-
-/**
  * Return the URI of the referring page.
  */
 function referer_uri() {
@@ -778,19 +709,4 @@ function drupal_get_messages() {
 
   return $messages;
 }
-
-// Start a page timer:
-timer_start('page');
-
-unset($conf);
-$config = conf_init();
-
-include_once "$config/settings.php";
-include_once 'includes/database.inc';
-include_once 'includes/session.inc';
-include_once 'includes/module.inc';
-
-// Initialize configuration variables, using values from conf.php if available.
-$conf = variable_init(isset($conf) ? $conf : array());
-
 ?>
diff -r'uNF^f' -x CVS drupal/includes/common.inc ../drupal/includes/common.inc
--- drupal/includes/common.inc	2005-06-04 16:13:43.000000000 +0200
+++ ../drupal/includes/common.inc	2005-06-04 18:00:55.000000000 +0200
@@ -812,7 +812,7 @@ function format_plural($count, $singular
   if ($count == 1) return t($singular, array("%count" => $count));
 
   // get the plural index through the gettext formula
-  $index = (function_exists('locale')) ? locale_get_plural($count) : -1;
+  $index = (function_exists('locale_get_plural')) ? locale_get_plural($count) : -1;
   if ($index < 0) { // backward compatibility
     return t($plural, array("%count" => $count));
   }
@@ -1974,12 +1974,8 @@ function drupal_get_path($type, $name) {
 /**
  * Provide a substitute clone() function for PHP4.
  */
-if (version_compare(phpversion(), '5.0') < 0) {
-  eval('
-    function clone($object) {
-      return $object;
-    }
-  ');
+function drupal_clone($object) {
+  return version_compare(phpversion(), '5.0') < 0 ? $object : clone($object);
 }
 
 /**
@@ -2027,44 +2023,4 @@ function drupal_implode_autocomplete($ar
   return implode('||', $output);
 }
 
-// Set the Drupal custom error handler.
-set_error_handler('error_handler');
-
-include_once 'includes/theme.inc';
-include_once 'includes/pager.inc';
-include_once 'includes/menu.inc';
-include_once 'includes/tablesort.inc';
-include_once 'includes/file.inc';
-include_once 'includes/xmlrpc.inc';
-include_once 'includes/image.inc';
-
-// Emit the correct charset HTTP header.
-drupal_set_header('Content-Type: text/html; charset=utf-8');
-
-// Initialize $_GET['q'] prior to loading modules and invoking hook_init().
-if (!empty($_GET['q'])) {
-  $_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));
-}
-else {
-  $_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
-}
-
-// Initialize all enabled modules.
-module_init();
-
-if (!user_access('bypass input data check')) {
-  // We can't use $_REQUEST because it consists of the contents of $_POST,
-  // $_GET and $_COOKIE: if any of the input arrays share a key, only one
-  // value will be verified.
-  if (!valid_input_data($_GET)
-   || !valid_input_data($_POST)
-   || !valid_input_data($_COOKIE)
-   || !valid_input_data($_FILES)) {
-    die('Terminated request because of suspicious input data.');
-  }
-}
-
-// Initialize the localization system.
-$locale = locale_initialize();
-
 ?>
diff -r'uNF^f' -x CVS drupal/includes/database.inc ../drupal/includes/database.inc
--- drupal/includes/database.inc	2005-04-08 16:24:03.000000000 +0200
+++ ../drupal/includes/database.inc	2005-06-04 17:58:56.000000000 +0200
@@ -279,7 +279,4 @@ function db_rewrite_sql($query, $primary
  * @} End of "defgroup database".
  */
 
-// Initialize the default database.
-db_set_active();
-
 ?>
diff -r'uNF^f' -x CVS drupal/includes/database.mysql.inc ../drupal/includes/database.mysql.inc
--- drupal/includes/database.mysql.inc	2005-05-23 23:14:01.000000000 +0200
+++ ../drupal/includes/database.mysql.inc	2005-06-04 19:20:47.000000000 +0200
@@ -47,15 +47,20 @@ function db_connect($url) {
 function _db_query($query, $debug = 0) {
   global $active_db;
   global $queries;
+  static $dev_query;
 
-  if (variable_get('dev_query', 0)) {
+  if (!isset($dev_query)) {
+    $dev_query = function_exists('dev_query') ? variable_get('dev_query', 0) : $GLOBALS['conf']['dev_query'];
+  }
+
+  if ($dev_query) {
     list($usec, $sec) = explode(' ', microtime());
     $timer = (float)$usec + (float)$sec;
   }
 
   $result = mysql_query($query, $active_db);
 
-  if (variable_get('dev_query', 0)) {
+  if ($dev_query) {
     list($usec, $sec) = explode(' ', microtime());
     $stop = (float)$usec + (float)$sec;
     $diff = $stop - $timer;
diff -r'uNF^f' -x CVS drupal/includes/database.pgsql.inc ../drupal/includes/database.pgsql.inc
--- drupal/includes/database.pgsql.inc	2005-05-23 23:14:01.000000000 +0200
+++ ../drupal/includes/database.pgsql.inc	2005-06-04 19:16:30.000000000 +0200
@@ -36,15 +36,20 @@ function db_connect($url) {
 function _db_query($query, $debug = 0) {
   global $active_db, $last_result;
   global $queries;
+  static $dev_query;
 
-  if (variable_get('dev_query', 0)) {
+  if (!isset($dev_query)) {
+    $dev_query = function_exists('dev_query') ? variable_get('dev_query', 0) : $GLOBALS['conf']['dev_query'];
+  }
+
+  if ($dev_query) {
     list($usec, $sec) = explode(' ', microtime());
     $timer = (float)$usec + (float)$sec;
   }
 
   $last_result = pg_query($active_db, $query);
 
-  if (variable_get('dev_query', 0)) {
+  if ($dev_query) {
     list($usec, $sec) = explode(' ', microtime());
     $stop = (float)$usec + (float)$sec;
     $diff = $stop - $timer;
diff -r'uNF^f' -x CVS drupal/includes/init.inc ../drupal/includes/init.inc
--- drupal/includes/init.inc	1970-01-01 01:00:00.000000000 +0100
+++ ../drupal/includes/init.inc	2005-06-05 18:36:38.837597600 +0200
@@ -0,0 +1,155 @@
+<?php
+
+/**
+ * Locate the appropriate configuration file.
+ *
+ * Try finding a matching configuration directory by stripping the
+ * website's hostname from left to right and pathname from right to
+ * left.  The first configuration file found will be used, the
+ * remaining will ignored.  If no configuration file is found,
+ * return a default value '$confdir/default'.
+ *
+ * Example for a fictitious site installed at
+ * http://www.drupal.org/mysite/test/ the 'settings.php' is
+ * searched in the following directories:
+ *
+ *  1. $confdir/www.drupal.org.mysite.test
+ *  2. $confdir/drupal.org.mysite.test
+ *  3. $confdir/org.mysite.test
+ *
+ *  4. $confdir/www.drupal.org.mysite
+ *  5. $confdir/drupal.org.mysite
+ *  6. $confdir/org.mysite
+ *
+ *  7. $confdir/www.drupal.org
+ *  8. $confdir/drupal.org
+ *  9. $confdir/org
+ *
+ * 10. $confdir/default
+ */
+function conf_init() {
+  static $conf = '';
+
+  if ($conf) {
+    return $conf;
+  }
+
+  $confdir = 'sites';
+  $uri = explode('/', $_SERVER['PHP_SELF']);
+  $server = explode('.', rtrim($_SERVER['HTTP_HOST'], '.'));
+  for ($i = count($uri) - 1; $i > 0; $i--) {
+    for ($j = count($server); $j > 0; $j--) {
+      $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
+      if (file_exists("$confdir/$dir/settings.php")) {
+        $conf = "$confdir/$dir";
+        return $conf;
+      }
+    }
+  }
+  $conf = "$confdir/default";
+  return $conf;
+}
+
+/**
+ * Unserializes and appends elements from a serialized string.
+ *
+ * @param $obj
+ *   The object to which the elements are appended.
+ * @param $field
+ *   The attribute of $obj whose value should be unserialized.
+ */
+function drupal_unpack($obj, $field = 'data') {
+  if ($obj->$field && $data = unserialize($obj->$field)) {
+    foreach ($data as $key => $value) {
+      if (!isset($obj->$key)) {
+        $obj->$key = $value;
+      }
+    }
+  }
+  return $obj;
+}
+
+/**
+ * A string describing a level of Drupal to load. Each phase adds to the
+ * previous one, so invoking a later phase automatically runs the earlier
+ * phases too. The most important usage is that if you want to access
+ * Drupal database from a script without loading anything
+ * else, you can include init.inc, and call drupal_bootstrap('database').
+ *
+ * @param $phase
+ *   A string. Allowed values are:
+ *     database  Initialize Drupal database system.
+ *     session  Initialize Drupal session handling system.
+ *     bootstrap  Initialize bootstrap.inc and module.inc. The variable system
+ *                is started. It is possible to serve a cached page at this point.
+ *    common  common.inc loaded, and ready to use. input data checks and fixes are performed.
+ */
+function drupal_bootstrap($phase) {
+  static $phases = array('database', 'session', 'bootstrap', 'common');
+
+  while ($current_phase = array_shift($phases)) {
+    _drupal_bootstrap($current_phase);
+    if ($phase == $current_phase) {
+      return;
+    }
+  }
+}
+
+function _drupal_bootstrap($phase) {
+  global $conf;
+
+  switch ($phase) {
+    case 'database':
+      global $db_url, $base_url;
+      unset($conf);
+      include_once conf_init() .'/settings.php';
+      include_once 'includes/database.inc';
+      // Initialize the default database.
+      db_set_active();
+      break;
+    case 'session':
+      include_once 'includes/session.inc';
+      session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
+      session_start();
+      break;
+    case 'bootstrap':
+      include_once 'includes/bootstrap.inc';
+      include_once 'includes/module.inc';
+      // Start a page timer:
+      timer_start('page');
+      $conf = variable_init(isset($conf) ? $conf : array());
+    case 'common':
+      include_once 'includes/common.inc';
+      // Set the Drupal custom error handler.
+      set_error_handler('error_handler');
+      // Emit the correct charset HTTP header.
+      drupal_set_header('Content-Type: text/html; charset=utf-8');
+      // Initialize $_GET['q'] prior to loading modules and invoking hook_init().
+      if (!empty($_GET['q'])) {
+        $_GET['q'] = drupal_get_normal_path(trim($_GET['q'], '/'));
+      }
+      else {
+        $_GET['q'] = drupal_get_normal_path(variable_get('site_frontpage', 'node'));
+      }
+      // Initialize all enabled modules.
+      module_init();
+      if (!user_access('bypass input data check')) {
+        // We can't use $_REQUEST because it consists of the contents of $_POST,
+        // $_GET and $_COOKIE: if any of the input arrays share a key, only one
+        // value will be verified.
+        if (!valid_input_data($_GET)
+        || !valid_input_data($_POST)
+        || !valid_input_data($_COOKIE)
+        || !valid_input_data($_FILES)) {
+          die('Terminated request because of suspicious input data.');
+        }
+      }
+      fix_gpc_magic();
+      fix_checkboxes();
+      // Initialize the localization system.
+      $locale = locale_initialize();
+      break;
+  }
+}
+
+?>
diff -r'uNF^f' -x CVS drupal/includes/menu.inc ../drupal/includes/menu.inc
--- drupal/includes/menu.inc	2005-04-24 18:34:32.000000000 +0200
+++ ../drupal/includes/menu.inc	2005-06-04 17:58:56.000000000 +0200
@@ -351,7 +351,7 @@ function menu_execute_active_handler() {
     $arguments = array_merge($arguments, explode('/', $arg));
   }
 
-  return call_user_func_array($menu['items'][$mid]['callback'], $arguments);
+  return function_exists($menu['items'][$mid]['callback']) ?  call_user_func_array($menu['items'][$mid]['callback'], $arguments) : '';
 }
 
 /**
diff -r'uNF^f' -x CVS drupal/includes/session.inc ../drupal/includes/session.inc
--- drupal/includes/session.inc	2005-05-07 13:39:54.000000000 +0200
+++ ../drupal/includes/session.inc	2005-06-04 17:58:56.000000000 +0200
@@ -6,9 +6,6 @@
  * User session handling functions.
  */
 
-session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
-session_start();
-
 /*** Session functions *****************************************************/
 
 function sess_open($save_path, $session_name) {
diff -r'uNF^f' -x CVS drupal/includes/theme.inc ../drupal/includes/theme.inc
--- drupal/includes/theme.inc	2005-05-31 23:14:26.000000000 +0200
+++ ../drupal/includes/theme.inc	2005-06-04 17:58:56.000000000 +0200
@@ -34,6 +34,7 @@
 function init_theme() {
   global $user, $custom_theme, $theme_engine, $theme_key;
 
+  drupal_bootstrap('database');
   $themes = list_themes();
 
   // Only select the user selected theme if it is available in the
diff -r'uNF^f' -x CVS drupal/index.php ../drupal/index.php
--- drupal/index.php	2005-05-21 20:33:58.000000000 +0200
+++ ../drupal/index.php	2005-06-04 18:17:32.000000000 +0200
@@ -9,12 +9,17 @@
  * prints the appropriate page.
  */
 
-include_once 'includes/bootstrap.inc';
+include_once 'includes/init.inc';
+drupal_bootstrap('bootstrap');
 drupal_page_header();
-include_once 'includes/common.inc';
-
-fix_gpc_magic();
-fix_checkboxes();
+include_once 'includes/theme.inc';
+include_once 'includes/pager.inc';
+include_once 'includes/menu.inc';
+include_once 'includes/tablesort.inc';
+include_once 'includes/file.inc';
+include_once 'includes/xmlrpc.inc';
+include_once 'includes/image.inc';
+drupal_bootstrap('common');
 
 $return = menu_execute_active_handler();
 switch ($return) {
diff -r'uNF^f' -x CVS drupal/modules/node.module ../drupal/modules/node.module
--- drupal/modules/node.module	2005-06-01 05:42:47.000000000 +0200
+++ ../drupal/modules/node.module	2005-06-04 17:58:56.000000000 +0200
@@ -1472,7 +1472,7 @@ function node_preview($node) {
 
     // Display a preview of the node:
     // Previewing alters $node so it needs to be cloned.
-    $output = theme('node_preview', clone($node));
+    $output = theme('node_preview', drupal_clone($node));
 
     $output .= node_form($node);
 
