=== modified file 'includes/menu.inc'
--- includes/menu.inc	2007-12-22 23:24:24 +0000
+++ includes/menu.inc	2007-12-23 12:54:15 +0000
@@ -336,6 +336,9 @@ function menu_execute_active_handler($pa
   if (_menu_site_is_offline()) {
     return MENU_SITE_OFFLINE;
   }
+  if (!db_result(db_query('SELECT COUNT(*) FROM {menu_router}'))) {
+    menu_rebuild();
+  }
   if ($router_item = menu_get_item($path)) {
     if ($router_item['access']) {
       if ($router_item['file']) {
@@ -1534,15 +1537,25 @@ function menu_cache_clear_all() {
 }
 
 /**
- * Populate the database representation of the {menu_router} table (router items)
- * and the navigation menu in the {menu_links} table.
+ * (Re)populates the database tables used by various menu functions.
+ *
+ * This function will clear and populate the {menu_router} table and then
+ * remove stale items from {menu_links} and add entries to {menu_links} for
+ * new router items. If called from update.php or install.php, it will only
+ * truncate the {menu_router} table and menu_execute_active_handler will call
+ * this function on the first real page load.
  */
 function menu_rebuild() {
-  menu_cache_clear_all();
-  $menu = menu_router_build(TRUE);
-  _menu_navigation_links_rebuild($menu);
-  // Clear the page and block caches.
-  _menu_clear_page_cache();
+  if (defined('MAINTENANCE_MODE')) {
+    db_query('DELETE FROM {menu_router}');
+  }
+  else {
+    menu_cache_clear_all();
+    $menu = menu_router_build(TRUE);
+    _menu_navigation_links_rebuild($menu);
+    // Clear the page and block caches.
+    _menu_clear_page_cache();
+  }
 }
 
 /**

