? book.diff ? book2.diff ? files Index: database/updates.inc =================================================================== RCS file: /cvs/drupal/drupal/database/updates.inc,v retrieving revision 1.231 diff -u -p -r1.231 updates.inc --- database/updates.inc 23 May 2006 19:03:50 -0000 1.231 +++ database/updates.inc 24 May 2006 17:10:57 -0000 @@ -2014,3 +2014,73 @@ function system_update_182() { return $ret; } + +/** + * Update book table definitions. + */ +function system_update_183() { + $ret = array(); + switch ($GLOBALS['db_type']) { + case 'mysql': + case 'mysqli': + $ret[] = update_sql("ALTER TABLE `{book}` RENAME `{book_pages}`;"); + $ret[] = update_sql("CREATE TABLE `{book}` ( + `bid` INT( 10 ) UNSIGNED NOT NULL PRIMARY KEY , + `uid` INT( 10 ) UNSIGNED NOT NULL , + `title` TEXT NOT NULL , + `weight` TINYINT( 3 ) NOT NULL + ) TYPE = MYISAM"); + $ret[] = update_sql("ALTER TABLE `{book_pages}` ADD `bid` INT( 10 ) UNSIGNED NOT NULL AFTER `nid` ;"); + $ret[] = update_sql("ALTER TABLE `{book_pages}` ADD INDEX ( `bid` );"); + break; + case 'pgsql': + //TODO + break; + } + return $ret; +} + +/** + * Create books definition and update book pages with their respective $bid. + */ +function system_update_184() { + $ret = array(); + + // Function to recursively update $bid for each book page. + function update_bid ($parent, $bid, $i) { + $sql = "SELECT DISTINCT(nid) FROM {book_pages} WHERE parent = %d"; + $result[$i] = db_query($sql,$parent); + if (db_num_rows($result[$i]) > 0) { + while ($page = db_fetch_object($result[$i])) { + $sql = "UPDATE {book_pages} SET bid = %d WHERE nid = %d"; + $result[$i+1] = db_query($sql, $bid, $page->nid); + update_bid($page->nid, $bid, $i+2); + } + } + } + + switch ($GLOBALS['db_type']) { + case 'mysql': + case 'mysqli': + case 'pgsql': + // Create the book 'covers' + $sql_books = "SELECT n.title, n.uid, n.vid, n.nid + FROM {book_pages} AS bp + JOIN {node} AS n ON bp.nid = n.nid AND bp.vid = n.vid + WHERE parent =0"; + $books = db_query($sql_books); + while ($row = db_fetch_object($books)) { + $bid = db_next_id('book_bid'); + $sql = 'INSERT INTO {book} (bid, uid, title) VALUES (%d, %d, "%s")'; + $result_a = db_query($sql, $bid, $row->uid, $row->title); + $sql = 'UPDATE {book_pages} SET bid = %d WHERE nid = %d'; + $result_b = db_query($sql, $bid, $row->nid); + update_bid ($row->nid, $bid, 1); + } + break; + case 'pgsql': + //Does pgsql need a separate update for this one? + break; + } + return $ret; +} Index: modules/book.module =================================================================== RCS file: /cvs/drupal/drupal/modules/book.module,v retrieving revision 1.366 diff -u -p -r1.366 book.module --- modules/book.module 24 May 2006 11:19:00 -0000 1.366 +++ modules/book.module 24 May 2006 17:10:58 -0000 @@ -153,7 +153,7 @@ function book_block($op = 'list', $delta else if ($op == 'view') { // Only display this block when the user is browsing a book: if (arg(0) == 'node' && is_numeric(arg(1))) { - $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d'), arg(1)); + $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book_pages} b ON n.vid = b.vid WHERE n.nid = %d'), arg(1)); if (db_num_rows($result) > 0) { $node = db_fetch_object($result); @@ -180,7 +180,7 @@ function book_block($op = 'list', $delta function book_load($node) { global $user; - $book = db_fetch_object(db_query('SELECT * FROM {book} WHERE vid = %d', $node->vid)); + $book = db_fetch_object(db_query('SELECT * FROM {book_pages} WHERE vid = %d', $node->vid)); if (arg(2) == 'edit' && !user_access('administer nodes')) { // If a user is about to update a book page, we overload some @@ -202,7 +202,11 @@ function book_load($node) { * Implementation of hook_insert(). */ function book_insert($node) { - db_query("INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)", $node->nid, $node->vid, $node->parent, $node->weight); + if ($node->parent == 0) { + $node->bid = db_next_id('book_bid'); + db_query("INSERT INTO {book} (bid, uid, title, weight) VALUES (%d, %d, '%s', %d)", $node->bid, $node->uid, $node->title, $node->weight); + } + db_query("INSERT INTO {book_pages} (nid, vid, bid, parent, weight) VALUES (%d, %d, %d, %d, %d)", $node->nid, $node->vid, $node->bid, $node->parent, $node->weight); } /** @@ -210,10 +214,10 @@ function book_insert($node) { */ function book_update($node) { if ($node->revision) { - db_query("INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)", $node->nid, $node->vid, $node->parent, $node->weight); + db_query("INSERT INTO {book_pages} (nid, vid, bid, parent, weight) VALUES (%d, %d, %d, %d, %d)", $node->nid, $node->vid, $node->bid, $node->parent, $node->weight); } else { - db_query("UPDATE {book} SET parent = %d, weight = %d WHERE vid = %d", $node->parent, $node->weight, $node->vid); + db_query("UPDATE {book_pages} SET parent = %d, weight = %d WHERE vid = %d", $node->parent, $node->weight, $node->vid); } } @@ -221,7 +225,7 @@ function book_update($node) { * Implementation of hook_delete(). */ function book_delete(&$node) { - db_query('DELETE FROM {book} WHERE nid = %d', $node->nid); + db_query('DELETE FROM {book_pages} WHERE nid = %d', $node->nid); } /** @@ -239,6 +243,14 @@ function book_submit(&$node) { * Implementation of hook_form(). */ function book_form(&$node) { + //* I am not sure what's the best way to retrieve bid. + $node->parent = $node->parent ? $node->parent : arg(4); + if ($node->parent) { + $sql = "SELECT bid FROM {book_pages} where nid = %d"; + $result = db_query($sql, $node->parent); + $node->bid = db_result($result); + } + /**/ if ($node->nid && !$node->parent && !user_access('create new books')) { $form['parent'] = array('#type' => 'value', '#value' => $node->parent); } @@ -246,12 +258,16 @@ function book_form(&$node) { $form['parent'] = array('#type' => 'select', '#title' => t('Parent'), '#default_value' => ($node->parent ? $node->parent : arg(4)), - '#options' => book_toc($node->nid), + '#options' => book_toc($node->nid, $node->bid), '#weight' => -4, '#description' => user_access('create new books') ? t('The parent section in which to place this page. Note that each page whose parent is <top-level> is an independent, top-level book.') : t('The parent that this page belongs in.'), ); } + $form['bid'] = array ('#type' => 'hidden', + '#value' => $node->bid, + ); + $form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#required' => TRUE, @@ -303,7 +319,7 @@ function book_outline($nid) { $form['parent'] = array('#type' => 'select', '#title' => t('Parent'), '#default_value' => $page->parent, - '#options' => book_toc($node->nid), + '#options' => book_toc($node->nid, $node->bid), '#description' => t('The parent page in the book.'), ); $form['weight'] = array('#type' => 'weight', @@ -344,17 +360,17 @@ function book_outline_submit($form_id, $ switch ($op) { case t('Add to book outline'): - db_query('INSERT INTO {book} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)', $node->nid, $node->vid, $form_values['parent'], $form_values['weight']); + db_query('INSERT INTO {book_pages} (nid, vid, parent, weight) VALUES (%d, %d, %d, %d)', $node->nid, $node->vid, $form_values['parent'], $form_values['weight']); db_query("UPDATE {node_revisions} SET log = '%s' WHERE vid = %d", $form_values['log'], $node->vid); drupal_set_message(t('The post has been added to the book.')); break; case t('Update book outline'): - db_query('UPDATE {book} SET parent = %d, weight = %d WHERE vid = %d', $form_values['parent'], $form_values['weight'], $node->vid); + db_query('UPDATE {book_pages} SET parent = %d, weight = %d WHERE vid = %d', $form_values['parent'], $form_values['weight'], $node->vid); db_query("UPDATE {node_revisions} SET log = '%s' WHERE vid = %d", $form_values['log'], $node->vid); drupal_set_message(t('The book outline has been updated.')); break; case t('Remove from book outline'): - db_query('DELETE FROM {book} WHERE nid = %d', $node->nid); + db_query('DELETE FROM {book_pages} WHERE nid = %d', $node->nid); drupal_set_message(t('The post has been removed from the book.')); break; } @@ -374,7 +390,7 @@ function book_outline_submit($form_id, $ * */ function book_location($node, $nodes = array()) { - $parent = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d'), $node->parent)); + $parent = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book_pages} b ON n.vid = b.vid WHERE n.nid = %d'), $node->parent)); if ($parent->title) { $nodes = book_location($parent, $nodes); $nodes[] = $parent; @@ -386,7 +402,7 @@ function book_location($node, $nodes = a * Accumulates the nodes up to the root of the book from the given node in the $nodes array. */ function book_location_down($node, $nodes = array()) { - $last_direct_child = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.nid = b.nid WHERE n.status = 1 AND b.parent = %d ORDER BY b.weight DESC, n.title DESC'), $node->nid)); + $last_direct_child = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book_pages} b ON n.nid = b.nid WHERE n.status = 1 AND b.parent = %d ORDER BY b.weight DESC, n.title DESC'), $node->nid)); if ($last_direct_child) { $nodes[] = $last_direct_child; $nodes = book_location_down($last_direct_child, $nodes); @@ -404,7 +420,7 @@ function book_prev($node) { } // Previous on the same level: - $direct_above = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = %d AND n.status = 1 AND n.moderate = 0 AND (b.weight < %d OR (b.weight = %d AND n.title < '%s')) ORDER BY b.weight DESC, n.title DESC"), $node->parent, $node->weight, $node->weight, $node->title)); + $direct_above = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book_pages} b ON n.vid = b.vid WHERE b.parent = %d AND n.status = 1 AND n.moderate = 0 AND (b.weight < %d OR (b.weight = %d AND n.title < '%s')) ORDER BY b.weight DESC, n.title DESC"), $node->parent, $node->weight, $node->weight, $node->title)); if ($direct_above) { // Get last leaf of $above. $path = book_location_down($direct_above); @@ -413,7 +429,7 @@ function book_prev($node) { } else { // Direct parent: - $prev = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d AND n.status = 1 AND n.moderate = 0'), $node->parent)); + $prev = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title FROM {node} n INNER JOIN {book_pages} b ON n.vid = b.vid WHERE n.nid = %d AND n.status = 1 AND n.moderate = 0'), $node->parent)); return $prev; } } @@ -423,7 +439,7 @@ function book_prev($node) { */ function book_next($node) { // get first direct child - $child = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = %d AND n.status = 1 AND n.moderate = 0 ORDER BY b.weight ASC, n.title ASC'), $node->nid)); + $child = db_fetch_object(db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book_pages} b ON n.vid = b.vid WHERE b.parent = %d AND n.status = 1 AND n.moderate = 0 ORDER BY b.weight ASC, n.title ASC'), $node->nid)); if ($child) { return $child; } @@ -433,7 +449,7 @@ function book_next($node) { $path[] = $node; while (($leaf = array_pop($path)) && count($path)) { - $next = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = %d AND n.status = 1 AND n.moderate = 0 AND (b.weight > %d OR (b.weight = %d AND n.title > '%s')) ORDER BY b.weight ASC, n.title ASC"), $leaf->parent, $leaf->weight, $leaf->weight, $leaf->title)); + $next = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book_pages} b ON n.vid = b.vid WHERE b.parent = %d AND n.status = 1 AND n.moderate = 0 AND (b.weight > %d OR (b.weight = %d AND n.title > '%s')) ORDER BY b.weight ASC, n.title ASC"), $leaf->parent, $leaf->weight, $leaf->weight, $leaf->title)); if ($next) { return $next; } @@ -470,7 +486,7 @@ function book_nodeapi(&$node, $op, $teas switch ($op) { case 'view': if (!$teaser) { - $book = db_fetch_array(db_query('SELECT * FROM {book} WHERE vid = %d', $node->vid)); + $book = db_fetch_array(db_query('SELECT * FROM {book_pages} WHERE vid = %d', $node->vid)); if ($book) { if ($node->moderate && user_access('administer nodes')) { drupal_set_message(t("The post has been submitted for moderation and won't be accessible until it has been approved.")); @@ -497,10 +513,10 @@ function book_nodeapi(&$node, $op, $teas } break; case 'delete revision': - db_query('DELETE FROM {book} WHERE vid = %d', $node->vid); + db_query('DELETE FROM {book_pages} WHERE vid = %d', $node->vid); break; case 'delete': - db_query('DELETE FROM {book} WHERE nid = %d', $node->nid); + db_query('DELETE FROM {book_pages} WHERE nid = %d', $node->nid); break; } } @@ -564,8 +580,12 @@ function book_toc_recurse($nid, $indent, /** * Returns an array of titles and nid entries of book pages in table of contents order. */ -function book_toc($exclude = 0) { - $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 ORDER BY b.weight, n.title')); +function book_toc($exclude = 0, $bid = FALSE) { + $one_book = ''; + if ($bid) { + $one_book = 'AND bp.bid = ' . $bid ; + } + $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, bp.parent, bp.weight FROM {node} n INNER JOIN {book_pages} bp ON n.vid = bp.vid WHERE n.status = 1 '.$one_book.' ORDER BY bp.weight, n.title')); while ($node = db_fetch_object($result)) { if (!$children[$node->parent]) { @@ -623,7 +643,7 @@ function book_tree_recurse($nid, $depth, * as a tree. */ function book_tree($parent = 0, $depth = 3, $unfold = array()) { - $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND n.moderate = 0 ORDER BY b.weight, n.title')); + $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent, b.weight FROM {node} n INNER JOIN {book_pages} b ON n.vid = b.vid WHERE n.status = 1 AND n.moderate = 0 ORDER BY b.weight, n.title')); while ($node = db_fetch_object($result)) { $list = $children[$node->parent] ? $children[$node->parent] : array(); @@ -640,14 +660,22 @@ function book_tree($parent = 0, $depth = * Menu callback; prints a listing of all books. */ function book_render() { - $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = 0 AND n.status = 1 AND n.moderate = 0 ORDER BY b.weight, n.title')); - - $books = array(); - while ($node = db_fetch_object($result)) { - $books[] = l($node->title, 'node/'. $node->nid); + if (arg(1) && is_numeric(arg(1))) { + $sql = "SELECT nid FROM {book_pages} WHERE parent = 0 AND bid = %d"; + $result = db_query($sql, arg(1)); + $nid = db_result($result); + drupal_goto('node/'.$nid); } + else { + $result = db_query('SELECT * FROM {book} ORDER BY weight, title'); + + $books = array(); + while ($book = db_fetch_object($result)) { + $books[] = l($book->title, 'book/'. $book->bid); + } return theme('item_list', $books); + } } /** @@ -670,7 +698,7 @@ function book_render() { */ function book_export($type = 'html', $nid = 0) { $type = drupal_strtolower($type); - $node_result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.nid = %d'), $nid); + $node_result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.parent FROM {node} n INNER JOIN {book_pages} b ON n.vid = b.vid WHERE n.nid = %d'), $nid); if (db_num_rows($node_result) > 0) { $node = db_fetch_object($node_result); } @@ -767,7 +795,7 @@ function theme_book_export_html($title, * - the output generated in visiting each node */ function book_recurse($nid = 0, $depth = 1, $visit_pre, $visit_post) { - $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND n.nid = %d AND n.moderate = 0 ORDER BY b.weight, n.title'), $nid); + $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book_pages} b ON n.vid = b.vid WHERE n.status = 1 AND n.nid = %d AND n.moderate = 0 ORDER BY b.weight, n.title'), $nid); while ($page = db_fetch_object($result)) { // Load the node: $node = node_load($page->nid); @@ -780,7 +808,7 @@ function book_recurse($nid = 0, $depth = $output .= book_node_visitor_html_pre($node, $depth, $nid); } - $children = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE n.status = 1 AND b.parent = %d AND n.moderate = 0 ORDER BY b.weight, n.title'), $node->nid); + $children = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book_pages} b ON n.vid = b.vid WHERE n.status = 1 AND b.parent = %d AND n.moderate = 0 ORDER BY b.weight, n.title'), $node->nid); while ($childpage = db_fetch_object($children)) { $childnode = node_load($childpage->nid); if ($childnode->nid != $node->nid) { @@ -872,7 +900,7 @@ function _book_admin_table_tree($node, $ ), ); - $children = db_query(db_rewrite_sql('SELECT n.nid, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = %d ORDER BY b.weight, n.title'), $node->nid); + $children = db_query(db_rewrite_sql('SELECT n.nid, b.weight FROM {node} n INNER JOIN {book_pages} b ON n.vid = b.vid WHERE b.parent = %d ORDER BY b.weight, n.title'), $node->nid); while ($child = db_fetch_object($children)) { $form = array_merge($form, _book_admin_table_tree(node_load($child->nid), $depth + 1)); } @@ -925,7 +953,7 @@ function book_admin_edit($nid) { * Menu callback; displays a listing of all orphaned book pages. */ function book_admin_orphan() { - $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, n.status, b.parent FROM {node} n INNER JOIN {book} b ON n.vid = b.vid')); + $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, n.status, b.parent FROM {node} n INNER JOIN {book_pages} b ON n.vid = b.vid')); $pages = array(); while ($page = db_fetch_object($result)) { @@ -997,7 +1025,7 @@ function book_admin($nid = 0) { * Returns an administrative overview of all books. */ function book_admin_overview() { - $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book} b ON n.vid = b.vid WHERE b.parent = 0 ORDER BY b.weight, n.title')); + $result = db_query(db_rewrite_sql('SELECT n.nid, n.title, b.weight FROM {node} n INNER JOIN {book_pages} b ON n.vid = b.vid WHERE b.parent = 0 ORDER BY b.weight, n.title')); while ($book = db_fetch_object($result)) { $rows[] = array(l($book->title, "node/$book->nid"), l(t('outline'), "admin/node/book/$book->nid")); }