=== modified file 'includes/bootstrap.inc'
--- includes/bootstrap.inc	2008-04-14 17:48:33 +0000
+++ includes/bootstrap.inc	2008-04-25 22:08:50 +0000
@@ -132,6 +132,27 @@ define('LANGUAGE_NEGOTIATION_PATH', 2);
 define('LANGUAGE_NEGOTIATION_DOMAIN', 3);
 
 /**
+ * Do not load anything just return the files already loaded.
+ */
+define('DRUPAL_LOAD_NONE', 0);
+
+/**
+ * Load the file immediately.
+ */
+define('DRUPAL_LOAD_IMMEDIATELY', 1);
+
+/**
+ * Load the file later.
+ */
+define('DRUPAL_LOAD_DEFERRED', 2);
+
+/**
+ * Forget about files loaded.
+ */
+define('DRUPAL_LOAD_RESET', 3);
+
+
+/**
  * Start the timer with the specified name. If you start and stop
  * the same timer multiple times, the measured intervals will be
  * accumulated.
@@ -518,37 +539,69 @@ function page_get_cache() {
  *   The name of the bootstrap hook we wish to invoke.
  */
 function bootstrap_invoke_all($hook) {
-  foreach (module_list(TRUE, TRUE) as $module) {
-    drupal_load('module', $module);
-    module_invoke($module, $hook);
-  }
+  module_fetch_all(TRUE);
+  module_load_all();
+  module_invoke_all($hook);
 }
 
 /**
- * Includes a file with the provided type and name. This prevents
- * including a theme, engine, module, etc., more than once.
+ * Includes a file with the provided type and name.
+ *
+ * The function prevents a theme, engine, module, etc., more than once. There
+ * are a number of argument combinations that are for internal use only. You
+ * should only call drupal_load with exactly two strings, a type and a name.
  *
  * @param $type
- *   The type of item to load (i.e. theme, theme_engine, module).
+ *   The type of item to load (i.e. theme, theme_engine, module). If this is
+ *   not set ie. the function is called without arguments then the deferred
+ *   files are loaded. The latter is for internal use only.
  * @param $name
  *   The name of the item to load.
+ * @param $mode
+ *   Whether to load the file immediately (DRUPAL_LOAD_IMMEDIATELY, default),
+ *   to load the file later (DRUPAL_LOAD_DEFERRED) or to just reset the
+ *   internal cache of files (DRUPAL_LOAD_RESET). These are for internal use
+ *   only, do not specify this argument.
  *
  * @return
- *   TRUE if the item is loaded or has already been loaded.
- */
-function drupal_load($type, $name) {
-  static $files = array();
+ *   TRUE if the item is loaded or has already been loaded. FALSE if the file
+ *   can not found. An array of files if $name is not given, the keys are
+ *   names
+ */
+function drupal_load($type = NULL, $name = NULL, $mode = DRUPAL_LOAD_IMMEDIATELY) {
+  static $files = array(), $deferred = array();
+
+  if (!isset($type)) {
+    foreach ($deferred as $filename) {
+      include_once "./$filename";
+    }
+    return TRUE;
+  }
+
+  if (!isset($files[$type]) || $mode == DRUPAL_LOAD_RESET) {
+    $files[$type] = array();
+  }
 
+  // When module_fetch_all calls with DRUPAL_LOAD_RESET then $name is not set
+  // so no need to check for that further.
+  if (!isset($name)) {
+    return $files[$type];
+  }
+
+  // We know that type and name are both set at this point.
   if (isset($files[$type][$name])) {
     return TRUE;
   }
 
   $filename = drupal_get_filename($type, $name);
-
   if ($filename) {
-    include_once "./$filename";
-    $files[$type][$name] = TRUE;
-
+    if ($mode == DRUPAL_LOAD_IMMEDIATELY) {
+      include_once "./$filename";
+    }
+    elseif ($mode == DRUPAL_LOAD_DEFERRED) {
+      $deferred[] = $filename;
+    }
+    $files[$type][$name] = $filename;
     return TRUE;
   }
 
@@ -998,6 +1051,9 @@ function _drupal_bootstrap($phase) {
 
     case DRUPAL_BOOTSTRAP_PATH:
       require_once './includes/path.inc';
+      // We need to know whether path module exists, however it is a waste of
+      // queries not to load information about all modules.
+      module_fetch_all();
       // Initialize $_GET['q'] prior to loading modules and invoking hook_init().
       drupal_init_path();
       break;

=== modified file 'includes/form.inc'
--- includes/form.inc	2008-04-14 17:51:38 +0000
+++ includes/form.inc	2008-04-25 21:03:27 +0000
@@ -883,7 +883,11 @@ function form_builder($form_id, $form, &
     if (!isset($form[$key]['#parents'])) {
       // Check to see if a tree of child elements is present. If so,
       // continue down the tree if required.
-      $form[$key]['#parents'] = $form[$key]['#tree'] && $form['#tree'] ? array_merge($form['#parents'], array($key)) : array($key);
+      $parents = $form['#parents'];
+      if ($form['#tree'] === 'skip') {
+        array_pop($parents);
+      }
+      $form[$key]['#parents'] = $form[$key]['#tree'] && $form['#tree'] ? array_merge($parents, array($key)) : array($key);
       $array_parents = isset($form['#array_parents']) ? $form['#array_parents'] : array();
       $array_parents[] = $key;
       $form[$key]['#array_parents'] = $array_parents;

=== modified file 'includes/module.inc'
--- includes/module.inc	2008-04-16 11:35:51 +0000
+++ includes/module.inc	2008-04-25 22:09:13 +0000
@@ -10,12 +10,48 @@
  * Load all the modules that have been enabled in the system table.
  */
 function module_load_all() {
-  foreach (module_list(TRUE, FALSE) as $module) {
+  drupal_load();
+}
+
+/**
+ * Load an absolute minimal set of modules.
+ */
+function module_load_minimal() {
+  foreach (array('system', 'filter') as $module) {
+    // We want to avoid file searches when possible.
+    drupal_get_filename('module', $module, "modules/$module/$module.module");
     drupal_load('module', $module);
   }
 }
 
 /**
+ * Fetches a list of modules from the database.
+ *
+ * @param $bootstrap
+ *   Whether to fetch the reduced set of modules loaded in "bootstrap mode"
+ *   for cached pages. See bootstrap.inc.
+ * @param $force
+ *   Force module list to be reloaded from database. This should not be
+ *   necessary unless a module is disabled.
+ */
+function module_fetch_all($bootstrap = FALSE, $force = FALSE) {
+  static $done;
+  if (!isset($done[$bootstrap]) || $force) {
+    drupal_load('module', NULL, DRUPAL_LOAD_RESET);
+    $done[$bootstrap] = TRUE;
+    $bootstrap = $bootstrap ? 'AND bootstrap = 1' : '';
+    $result = db_query("SELECT name, filename FROM {system} WHERE type = 'module' AND status = 1 $bootstrap ORDER BY weight ASC, name ASC");
+    while ($module = db_fetch_object($result)) {
+      if (file_exists($module->filename)) {
+        // We want to avoid file searches when possible.
+        drupal_get_filename('module', $module->name, $module->filename);
+        drupal_load('module', $module->name, DRUPAL_LOAD_DEFERRED);
+      }
+    }
+  }
+}
+
+/**
  * Call a function repeatedly with each module in turn as an argument.
  */
 function module_iterate($function, $argument = '') {
@@ -28,57 +64,24 @@ function module_iterate($function, $argu
  * Collect a list of all loaded modules. During the bootstrap, return only
  * vital modules. See bootstrap.inc
  *
- * @param $refresh
- *   Whether to force the module list to be regenerated (such as after the
- *   administrator has changed the system settings).
- * @param $bootstrap
- *   Whether to return the reduced set of modules loaded in "bootstrap mode"
- *   for cached pages. See bootstrap.inc.
  * @param $sort
  *   By default, modules are ordered by weight and filename, settings this option
  *   to TRUE, module list will be ordered by module name.
- * @param $fixed_list
- *   (Optional) Override the module list with the given modules. Stays until the
- *   next call with $refresh = TRUE.
  * @return
- *   An associative array whose keys and values are the names of all loaded
- *   modules.
- */
-function module_list($refresh = FALSE, $bootstrap = TRUE, $sort = FALSE, $fixed_list = NULL) {
-  static $list, $sorted_list;
-
-  if ($refresh || $fixed_list) {
-    unset($sorted_list);
-    $list = array();
-    if ($fixed_list) {
-      foreach ($fixed_list as $name => $module) {
-        drupal_get_filename('module', $name, $module['filename']);
-        $list[$name] = $name;
-      }
-    }
-    else {
-      if ($bootstrap) {
-        $result = db_query("SELECT name, filename FROM {system} WHERE type = 'module' AND status = 1 AND bootstrap = 1 ORDER BY weight ASC, filename ASC");
-      }
-      else {
-        $result = db_query("SELECT name, filename FROM {system} WHERE type = 'module' AND status = 1 ORDER BY weight ASC, filename ASC");
-      }
-      while ($module = db_fetch_object($result)) {
-        if (file_exists($module->filename)) {
-          drupal_get_filename('module', $module->name, $module->filename);
-          $list[$module->name] = $module->name;
-        }
-      }
-    }
-  }
-  if ($sort) {
-    if (!isset($sorted_list)) {
-      $sorted_list = $list;
-      ksort($sorted_list);
-    }
-    return $sorted_list;
+ *   A list of the names of all loaded modules if $full is FALSE or an
+ *   associative array where the keys are the names of the loaded modules and
+ *   the values are their filename.
+ */
+function module_list($sort = FALSE, $full = FALSE) {
+  static $stored_list = array(), $sorted_list;
+
+  $list = drupal_load('module', NULL, DRUPAL_LOAD_NONE);
+  if ($sort && $stored_list != $list) {
+    $stored_list = $list;
+    ksort($list);
+    $sorted_list = $list;
   }
-  return $list;
+  return $full ? $list : array_keys($list);
 }
 
 /**
@@ -220,8 +223,8 @@ function _module_build_dependencies($fil
  *   TRUE if the module is both installed and enabled.
  */
 function module_exists($module) {
-  $list = module_list();
-  return array_key_exists($module, $list);
+  $list = module_list(FALSE, TRUE);
+  return isset($list[$module]);
 }
 
 /**
@@ -289,8 +292,6 @@ function module_enable($module_list) {
   }
 
   if (!empty($invoke_modules)) {
-    // Refresh the module list to include the new enabled module.
-    module_list(TRUE, FALSE);
     // Force to regenerate the stored list of hook implementations.
     module_implements('', FALSE, TRUE);
   }
@@ -331,7 +332,7 @@ function module_disable($module_list) {
 
   if (!empty($invoke_modules)) {
     // Refresh the module list to exclude the disabled modules.
-    module_list(TRUE, FALSE);
+    module_fetch_all(FALSE, TRUE);
     // Force to regenerate the stored list of hook implementations.
     module_implements('', FALSE, TRUE);
   }
@@ -404,7 +405,7 @@ function module_implements($hook, $sort 
 
   if (!isset($implementations[$hook])) {
     $implementations[$hook] = array();
-    $list = module_list(FALSE, TRUE, $sort);
+    $list = module_list($sort);
     foreach ($list as $module) {
       if (module_hook($module, $hook)) {
         $implementations[$hook][] = $module;

=== modified file 'includes/theme.maintenance.inc'
--- includes/theme.maintenance.inc	2008-04-14 17:48:33 +0000
+++ includes/theme.maintenance.inc	2008-04-24 08:57:57 +0000
@@ -36,13 +36,7 @@ function _drupal_maintenance_theme() {
     $theme = 'minnelli';
   }
   else {
-    // Load module basics (needed for hook invokes).
-    $module_list['system']['filename'] = 'modules/system/system.module';
-    $module_list['filter']['filename'] = 'modules/filter/filter.module';
-    module_list(TRUE, FALSE, FALSE, $module_list);
-    drupal_load('module', 'system');
-    drupal_load('module', 'filter');
-
+    module_load_minimal();
     $theme = variable_get('maintenance_theme', 'minnelli');
   }
 

=== modified file 'install.php'
--- install.php	2008-04-14 17:48:33 +0000
+++ install.php	2008-04-24 08:57:57 +0000
@@ -34,11 +34,7 @@ function install_main() {
 
   // Load module basics (needed for hook invokes).
   include_once './includes/module.inc';
-  $module_list['system']['filename'] = 'modules/system/system.module';
-  $module_list['filter']['filename'] = 'modules/filter/filter.module';
-  module_list(TRUE, FALSE, FALSE, $module_list);
-  drupal_load('module', 'system');
-  drupal_load('module', 'filter');
+  module_load_minimal();
 
   // Set up theme system for the maintenance page.
   drupal_maintenance_theme();

=== removed file 'modules/README.txt'
--- modules/README.txt	2008-01-25 19:47:58 +0000
+++ modules/README.txt	1970-01-01 00:00:00 +0000
@@ -1,8 +0,0 @@
-This directory is reserved for core module files. Custom or contributed
-modules should be placed in their own subdirectory of the sites/all/modules
-directory. For multisite installations, they can also be placed in a subdirectory
-under /sites/{sitename}/modules/, where {sitename} is the name of your site
-(e.g., www.example.com). This will allow you to more easily update Drupal core files.
-
-For more details, see: http://drupal.org/node/176043
-

=== modified file 'modules/simpletest/drupal_web_test_case.php'
--- modules/simpletest/drupal_web_test_case.php	2008-04-20 18:34:43 +0000
+++ modules/simpletest/drupal_web_test_case.php	2008-04-25 21:55:08 +0000
@@ -369,6 +369,8 @@ class DrupalWebTestCase extends UnitTest
     variable_set('install_profile', 'default');
     variable_set('install_task', 'profile-finished');
     variable_set('clean_url', $clean_url_original);
+    // We might have removed some modules.
+    module_fetch_all(FALSE, TRUE);
 
     // Use temporary files directory with the same prefix as database.
     $this->original_file_directory = file_directory_path();
@@ -394,6 +396,8 @@ class DrupalWebTestCase extends UnitTest
         db_drop_table($ret, $name);
       }
       $db_prefix = $this->db_prefix_original;
+      // We might have removed some modules.
+      module_fetch_all(FALSE, TRUE);
       $this->_logged_in = FALSE;
       $this->curlClose();
     }

=== modified file 'modules/system/system.admin.inc'
--- modules/system/system.admin.inc	2008-04-16 11:35:51 +0000
+++ modules/system/system.admin.inc	2008-04-24 08:57:57 +0000
@@ -889,7 +889,7 @@ function system_modules_submit($form, &$
   }
   drupal_install_modules($new_modules);
 
-  $current_module_list = module_list(TRUE, FALSE);
+  $current_module_list = module_list();
   if ($old_module_list != $current_module_list) {
     drupal_set_message(t('The configuration options have been saved.'));
   }

=== modified file 'modules/system/system.test'
--- modules/system/system.test	2008-04-20 18:23:21 +0000
+++ modules/system/system.test	2008-04-25 22:28:02 +0000
@@ -31,7 +31,6 @@ class EnableDisableCoreTestCase extends 
     $admin_user = $this->drupalCreateUser(array('access administration pages', 'administer site configuration'));
     $this->drupalLogin($admin_user);
 
-    // Enable aggregator, and check tables.
     $this->assertModules(array('aggregator'), FALSE);
     $this->assertTableCount('aggregator', FALSE);
 
@@ -115,7 +114,7 @@ class EnableDisableCoreTestCase extends 
    * @param boolean $enabled Module state.
    */
   function assertModules(array $modules, $enabled) {
-    module_list(TRUE, FALSE);
+    module_fetch_all(FALSE, TRUE);
     foreach ($modules as $module) {
       if ($enabled) {
         $this->assertTrue(module_exists($module) == $enabled, t('Module "@module" is enabled.', array('@module' => $module)));

=== modified file 'modules/user/user.admin.inc'
--- modules/user/user.admin.inc	2008-04-14 17:48:33 +0000
+++ modules/user/user.admin.inc	2008-04-24 08:57:57 +0000
@@ -517,7 +517,7 @@ function user_admin_perm($form_state, $r
   // Render role/permission overview:
   $options = array();
   $hide_descriptions = !system_admin_compact_mode();
-  foreach (module_list(FALSE, FALSE, TRUE) as $module) {
+  foreach (module_list(TRUE) as $module) {
     if ($permissions = module_invoke($module, 'perm')) {
       $form['permission'][] = array(
         '#value' => $module,

=== modified file 'update.php'
--- update.php	2008-04-14 17:48:33 +0000
+++ update.php	2008-04-24 15:16:37 +0000
@@ -582,11 +582,7 @@ if (empty($op)) {
 
   // Load module basics.
   include_once './includes/module.inc';
-  $module_list['system']['filename'] = 'modules/system/system.module';
-  $module_list['filter']['filename'] = 'modules/filter/filter.module';
-  module_list(TRUE, FALSE, FALSE, $module_list);
-  drupal_load('module', 'system');
-  drupal_load('module', 'filter');
+  module_load_minimal();
 
   // Set up $language, since the installer components require it.
   drupal_init_language();

