array( 'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), 'parent' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), 'weight' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny') ), 'indexes' => array( 'parent' => array('parent') ), 'primary key' => array('nid'), ); $schema['book'] = drupal_get_schema_unprocessed('book', 'book'); _drupal_initialize_schema('book', $schema); db_create_table($ret, $schema['book_temp']); $ret[] = update_sql("INSERT INTO {book_temp} bt (bt.nid, bt.parent, bt.weight) SELECT b.nid, b.parent, b.weight FROM {book} b INNER JOIN {node} on b.vid = n.vid"); $ret[] = update_sql("DROP TABLE {book}"); db_create_table($ret, $schema['book']); $_SESSION['book_update_6000'] = array(); $r = db_query("SELECT * from {book_temp} WHERE parent = 0"); // Collect all books - top-level nodes while ($a = db_fetch_array($r)) { $_SESSION['book_update_6000'][] = $a; } $ret['#finished'] = FALSE; return $ret; } else { // no exising nodes in the hierarchy $ret[] = update_sql("DROP TABLE {book}"); $schema['book'] = drupal_get_schema_unprocessed('book', 'book'); _drupal_initialize_schema('book', $schema); db_create_table($ret, $schema['book']); return $ret; } } if ($_SESSION['book_update_6000']) { $i = 20; // Update this many at a time while ($i && $_SESSION['book_update_6000']) { $book = array_pop($_SESSION['book_update_6000']); $r = db_query("SELECT * FROM {book_temp} WHERE parent = %d", $book['nid']); while ($a = db_fetch_array($r)) { $_SESSION['book_update_6000'][] = $a; } if ($book['parent']) { $book += db_fetch_array(db_query("SELECT b.mlid AS plid, b.book_id FROM {book} b WHERE b.nid = %d", $book['parent']); } else { $book['plid'] = 0; $book['book_id'] = $book['nid']; } $book += array( 'module' => 'book', 'link_path' => 'node/'. $book['nid'], 'router_path' => 'node/%', 'menu_name' => book_menu_name($book['book_id']); ); $book['link_title'] = db_result(db_query("SELECT title FROM {node} WHERE nid = %d")); if ($menu_link_save($book)) { } $i--; } $ret['#finished'] = FALSE; } //TODO batched updates - the hard part. Pop each book's nid off the session // variable when all it's chlidren have been updated? $finished = empty($_SESSION['book_update_6000']); if ($finished) { $ret[] = update_sql("DROP TABLE {book_temp}"); unset($_SESSION['book_update_6000']); } $ret['#finished'] = $finished; return $ret; }