Index: modules/system/system.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.install,v
retrieving revision 1.121
diff -u -p -r1.121 system.install
--- modules/system/system.install	7 Jun 2007 03:44:13 -0000	1.121
+++ modules/system/system.install	13 Jun 2007 19:29:12 -0000
@@ -3288,24 +3288,161 @@ function system_update_6019() {
 function system_update_6020() {
   $ret = array();
 
-  $schema['menu_router'] = drupal_get_schema_unprocessed('system', 'menu_router');
-  $schema['menu_links'] = drupal_get_schema_unprocessed('system', 'menu_links');
+  $schema['menu_router'] = array(
+    'fields' => array(
+      'path'             => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'load_functions'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'to_arg_functions' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'access_callback'  => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'access_arguments' => array('type' => 'text', 'not null' => FALSE),
+      'page_callback'    => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'page_arguments'   => array('type' => 'text', 'not null' => FALSE),
+      'fit'              => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
+      'number_parts'     => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
+      'tab_parent'       => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'tab_root'         => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'title'            => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'title_callback'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'title_arguments'  => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'type'             => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
+      'block_callback'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'description'      => array('type' => 'text', 'not null' => TRUE),
+      'position'         => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'weight'           => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
+      'file'             => array('type' => 'text', 'size' => 'medium')
+    ),
+    'indexes' => array(
+      'fit'        => array('fit'),
+      'tab_parent' => array('tab_parent')
+    ),
+    'primary key' => array('path'),
+  );
+
+  $schema['menu_links'] = array(
+    'fields' => array(
+      'menu_name'    => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => ''),
+      'mlid'         => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
+      'plid'         => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'link_path'    => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'router_path'  => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'hidden'       => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
+      'external'     => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
+      'has_children' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
+      'expanded'     => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
+      'weight'       => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
+      'depth'        => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
+      'customized'   => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
+      'p1'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'p2'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'p3'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'p4'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'p5'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'p6'           => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'module'       => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => 'system'),
+      'link_title'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'options'      => array('type' => 'text', 'not null' => FALSE)
+    ),
+    'indexes' => array(
+      'expanded_children' => array('expanded', 'has_children'),
+      'menu_name_path' => array('menu_name', 'link_path'),
+      'plid'=> array('plid'),
+      'parents' => array('p1', 'p2', 'p3', 'p4', 'p5'),
+      'router_path' => array('router_path'),
+    ),
+    'primary key' => array('mlid'),
+  );
+
   _drupal_initialize_schema('system', $schema);
   $ret = array();
   foreach ($schema as $table) {
     db_create_table($ret, $table);
   }
+
   return $ret;
 }
 
 function system_update_6021() {
   $ret = array();
-  // TODO - menu module updates. These need to happen before we do the menu_rebuild
-
-  menu_rebuild();
+  // We should die a horrible death here if this query fails.
+  $menu = unserialize(db_result(db_query("SELECT data FROM {cache_menu} WHERE cid LIKE '1:%%'")));
+  $new_menu = menu_router_build(TRUE);
+  if ($menu && module_exists('menu')) {
+    $items = $menu['items'];
+    drupal_install_schema('menu');
+    $menu_names = array();
+    $system_menus = array(
+      1 => 'navigation',
+      variable_get('menu_primary_menu', 2) => 'primary-links',
+      variable_get('menu_secondary_menu', 0) => 'secondary-links',
+    );
+    // These are the root menus.
+    foreach ($items[0]['children'] as $mid) {
+      $item = $items[$mid];
+      if (isset($system_menus[$mid])) {
+        $menu_name = $system_menus[$mid];
+      }
+      else {
+        $title = $item['title'];
+        $menu_name = preg_replace('[^a-z0-9]', '-', $title);
+        if (isset($menu_names[$menu_name])) {
+          $counter = 0;
+          while (isset($menu_names[$menu_name . ++$counter]));
+          $menu_name = $menu_name . $counter;
+        }
+        $description = db_result(db_query('SELECT description FROM {menu} WHERE mid = %d', $mid));
+        db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('%s', '%s', '%s')", $menu_name, $title, $description);
+      }
+      $menu_names[$menu_name] = $mid;
+      if (isset($item['children'])) {
+        foreach ($item['children'] as $child_mid) {
+          _system_update_6021($items, $menu_name, $child_mid);
+        }
+      }
+      menu_cache_clear($menu_name);
+    }
+    array_flip($menu_names);
+    $menu_parent_items = variable_get('menu_parent_items', NULL);
+    if (isset($menu_parent_items)) {
+      if ($menu_parent_items) {
+        variable_set('menu_parent_items', $menu_names[$menu_parent_items]);
+      }
+      else {
+        drupal_set_message(t('The menu name for on-the-fly creation of menu links has been set the navigation'), 'warning');
+        variable_set('menu_parent_items', 'navigation');
+      }
+    }
+  }
+  _menu_navigation_links_rebuild($new_menu);
+  $ret[] = update_sql("DROP TABLE {menu}");
   return $ret;
 }
 
+function _system_update_6021($items, $menu_name, $mid) {
+  static $map;
+  $item = $items[$mid];
+  if ($item['type'] & MENU_VISIBLE_IN_BREADCRUMB) {
+    $item = _menu_link_build($item);
+    $item['menu_name'] = $menu_name;
+    $item['plid'] = isset($map[$item['pid']]) ? $map[$item['pid']] : 0;
+    if ($mid > 0) {
+      $item['description'] = db_result(db_query('SELECT description FROM {menu} WHERE mid = %d', $mid));
+      if ($item['type'] & MENU_CREATED_BY_ADMIN) {
+        $item['module'] = 'menu';
+      }
+      elseif ($item['type'] & MENU_MODIFIED_BY_ADMIN) {
+        $item['customized'] = TRUE;
+      }
+    }
+    $item['expanded'] = (bool)($item['type'] & 0x0100);
+    $map[$mid] = menu_link_save($item);
+  }
+  if (isset($item['children'])) {
+    foreach ($item['children'] as $child_mid) {
+      _system_update_6021($items, $menu_name, $child_mid);
+    }
+  }
+}
+
 /**
  * Update files tables to associate files to a uid by default instead of a nid.
  * Rename file_revisions to upload since it should only be used by the upload
