=== modified file 'includes/menu.inc'
--- includes/menu.inc	2007-12-22 23:24:24 +0000
+++ includes/menu.inc	2007-12-23 12:24:20 +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(TRUE);
+  }
   if ($router_item = menu_get_item($path)) {
     if ($router_item['access']) {
       if ($router_item['file']) {
@@ -1536,13 +1539,24 @@ 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.
+ *
+ * @param $force
+ *   Normally menu_rebuild will only rebuild the menu_router table when run
+ *   from index.php. You can enforce it from other scripts by this parameter,
+ *   though doing so during install or update can result in a broken router
+ *   table.
  */
-function menu_rebuild() {
+function menu_rebuild($force = FALSE) {
   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 ($force || basename($_SERVER['SCRIPT_NAME'] ? $_SERVER['SCRIPT_NAME'] : $_SERVER['SCRIPT_FILENAME']) == 'index.php') {
+    $menu = menu_router_build(TRUE);
+    _menu_navigation_links_rebuild($menu);
+    // Clear the page and block caches.
+    _menu_clear_page_cache();
+  }
+  else {
+    db_query('DELETE FROM {menu_router}');
+  }
 }
 
 /**

