webmaster@1: 'book', webmaster@1: 'name' => t('Book page'), webmaster@1: 'module' => 'node', webmaster@1: 'description' => t('A book page is a page of content, organized into a collection of related entries collectively known as a book. A book page automatically displays links to adjacent pages, providing a simple navigation system for organizing and reviewing structured content.'), webmaster@1: 'custom' => TRUE, webmaster@1: 'modified' => TRUE, webmaster@1: 'locked' => FALSE, webmaster@1: ); webmaster@1: webmaster@1: $book_node_type = (object)_node_type_set_defaults($book_node_type); webmaster@1: node_type_save($book_node_type); webmaster@1: // Default to not promoted. webmaster@1: variable_set('node_options_book', array('status')); webmaster@1: // Use this default type for adding content to books. webmaster@1: variable_set('book_allowed_types', array('book')); webmaster@1: variable_set('book_child_type', 'book'); webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Drupal 5.x to 6.x update. webmaster@1: * webmaster@1: * This function moves any existing book hierarchy into the new structure used webmaster@1: * in the 6.x module. Rather than storing the hierarchy in the {book} table, webmaster@1: * the menu API is used to store the hierarchy in the {menu_links} table and the webmaster@1: * {book} table serves to uniquely connect a node to a menu link. webmaster@1: * webmaster@1: * In order to accomplish this, the current hierarchy is processed using a stack. webmaster@1: * The stack insures that each parent is processed before any of its children webmaster@1: * in the book hierarchy, and is compatible with batched update processing. webmaster@1: * webmaster@1: */ webmaster@1: function book_update_6000() { webmaster@1: $ret = array(); webmaster@1: webmaster@1: // Set up for a multi-part update. webmaster@1: if (!isset($_SESSION['book_update_6000'])) { webmaster@1: webmaster@1: $schema['book'] = array( webmaster@1: 'fields' => array( webmaster@1: 'mlid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), webmaster@1: 'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), webmaster@1: 'bid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), webmaster@1: ), webmaster@1: 'primary key' => array('mlid'), webmaster@1: 'unique keys' => array( webmaster@1: 'nid' => array('nid'), webmaster@1: ), webmaster@1: 'indexes' => array( webmaster@1: 'bid' => array('bid'), webmaster@1: ), webmaster@1: ); webmaster@1: // Add the node type. webmaster@1: _book_install_type_create(); webmaster@1: webmaster@1: // Fix role permissions to account for the changed names webmaster@1: // Setup the array holding strings to match and the corresponding webmaster@1: // strings to replace them with. webmaster@1: $replace = array( webmaster@1: 'outline posts in books' => 'administer book outlines', webmaster@1: 'create book pages' => 'create book content', webmaster@1: 'edit book pages' => 'edit any book content', webmaster@1: 'edit own book pages' => 'edit own book content', webmaster@1: 'see printer-friendly version' => 'access printer-friendly version', webmaster@1: ); webmaster@1: webmaster@1: // Loop over all the roles, and do the necessary transformations. webmaster@1: $query = db_query("SELECT rid, perm FROM {permission} ORDER BY rid"); webmaster@1: while ($role = db_fetch_object($query)) { webmaster@1: // Replace all the old permissions with the corresponding new permissions. webmaster@1: $fixed_perm = strtr($role->perm, $replace); webmaster@1: // If the user could previously create book pages, they should get the new webmaster@1: // 'add content to books' permission. webmaster@1: if (strpos($role->perm, 'create book pages') !== FALSE) { webmaster@1: $fixed_perm .= ', add content to books'; webmaster@1: } webmaster@1: // Only save if the permissions have changed. webmaster@1: if ($fixed_perm != $role->perm) { webmaster@1: $ret[] = update_sql("UPDATE {permission} SET perm = '$fixed_perm' WHERE rid = $role->rid"); webmaster@1: } webmaster@1: } webmaster@1: webmaster@1: // Determine whether there are any existing nodes in the book hierarchy. webmaster@1: if (db_result(db_query("SELECT COUNT(*) FROM {book}"))) { webmaster@1: // Temporary table for the old book hierarchy; we'll discard revision info. webmaster@1: $schema['book_temp'] = array( webmaster@1: 'fields' => array( webmaster@1: 'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0), webmaster@1: 'parent' => array('type' => 'int', 'not null' => TRUE, 'default' => 0), webmaster@1: 'weight' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'tiny') webmaster@1: ), webmaster@1: 'indexes' => array( webmaster@1: 'parent' => array('parent') webmaster@1: ), webmaster@1: 'primary key' => array('nid'), webmaster@1: ); webmaster@1: webmaster@1: db_create_table($ret, 'book_temp', $schema['book_temp']); webmaster@1: webmaster@1: // Insert each node in the old table into the temporary table. webmaster@1: $ret[] = update_sql("INSERT INTO {book_temp} (nid, parent, weight) SELECT b.nid, b.parent, b.weight FROM {book} b INNER JOIN {node} n on b.vid = n.vid"); webmaster@1: $ret[] = update_sql("DROP TABLE {book}"); webmaster@1: webmaster@1: db_create_table($ret, 'book', $schema['book']); webmaster@1: webmaster@1: $_SESSION['book_update_6000_orphans']['from'] = 0; webmaster@1: $_SESSION['book_update_6000'] = array(); webmaster@1: $result = db_query("SELECT * from {book_temp} WHERE parent = 0"); webmaster@1: webmaster@1: // Collect all books - top-level nodes. webmaster@1: while ($a = db_fetch_array($result)) { webmaster@1: $_SESSION['book_update_6000'][] = $a; webmaster@1: } webmaster@1: $ret['#finished'] = FALSE; webmaster@1: return $ret; webmaster@1: } webmaster@1: else { webmaster@1: // No exising nodes in the hierarchy, so drop the table and re-create it. webmaster@1: $ret[] = update_sql("DROP TABLE {book}"); webmaster@1: db_create_table($ret, 'book', $schema['book']); webmaster@1: return $ret; webmaster@1: } webmaster@1: } webmaster@1: elseif ($_SESSION['book_update_6000_orphans']) { webmaster@1: // Do the first batched part of the update - collect orphans. webmaster@1: $update_count = 400; // Update this many at a time webmaster@1: webmaster@1: $result = db_query_range("SELECT * FROM {book_temp}", $_SESSION['book_update_6000_orphans']['from'], $update_count); webmaster@1: $has_rows = FALSE; webmaster@1: // Go through the next $update_count book pages and locate the orphans. webmaster@1: while ($book = db_fetch_array($result)) { webmaster@1: $has_rows = TRUE; webmaster@1: // Orphans are defined as nodes whose parent does not exist in the table. webmaster@1: if ($book['parent'] && !db_result(db_query("SELECT COUNT(*) FROM {book_temp} WHERE nid = %d", $book['parent']))) { webmaster@1: if (empty($_SESSION['book_update_6000_orphans']['book'])) { webmaster@1: // The first orphan becomes the parent for all other orphans. webmaster@1: $book['parent'] = 0; webmaster@1: $_SESSION['book_update_6000_orphans']['book'] = $book; webmaster@1: $ret[] = array('success' => TRUE, 'query' => 'Relocated orphan book pages.'); webmaster@1: } webmaster@1: else { webmaster@1: // Re-assign the parent value of the book, and add it to the stack. webmaster@1: $book['parent'] = $_SESSION['book_update_6000_orphans']['book']['nid']; webmaster@1: $_SESSION['book_update_6000'][] = $book; webmaster@1: } webmaster@1: } webmaster@1: } webmaster@1: if ($has_rows) { webmaster@1: $_SESSION['book_update_6000_orphans']['from'] += $update_count; webmaster@1: } webmaster@1: else { webmaster@1: // Done with this part webmaster@1: if (!empty($_SESSION['book_update_6000_orphans']['book'])) { webmaster@1: // The orphans' parent is added last, so it will be processed first. webmaster@1: $_SESSION['book_update_6000'][] = $_SESSION['book_update_6000_orphans']['book']; webmaster@1: } webmaster@1: $_SESSION['book_update_6000_orphans'] = FALSE; webmaster@1: } webmaster@1: $ret['#finished'] = FALSE; webmaster@1: return $ret; webmaster@1: } webmaster@1: else { webmaster@1: // Do the next batched part of the update webmaster@1: $update_count = 100; // Update this many at a time webmaster@1: webmaster@1: while ($update_count && $_SESSION['book_update_6000']) { webmaster@1: // Get the last node off the stack. webmaster@1: $book = array_pop($_SESSION['book_update_6000']); webmaster@1: webmaster@1: // Add all of this node's children to the stack webmaster@1: $result = db_query("SELECT * FROM {book_temp} WHERE parent = %d", $book['nid']); webmaster@1: while ($a = db_fetch_array($result)) { webmaster@1: $_SESSION['book_update_6000'][] = $a; webmaster@1: } webmaster@1: webmaster@1: if ($book['parent']) { webmaster@1: // If its not a top level page, get its parent's mlid. webmaster@1: $parent = db_fetch_array(db_query("SELECT b.mlid AS plid, b.bid FROM {book} b WHERE b.nid = %d", $book['parent'])); webmaster@1: $book = array_merge($book, $parent); webmaster@1: } webmaster@1: else { webmaster@1: // There is not a parent - this is a new book. webmaster@1: $book['plid'] = 0; webmaster@1: $book['bid'] = $book['nid']; webmaster@1: } webmaster@1: webmaster@1: $book += array( webmaster@1: 'module' => 'book', webmaster@1: 'link_path' => 'node/'. $book['nid'], webmaster@1: 'router_path' => 'node/%', webmaster@1: 'menu_name' => 'book-toc-'. $book['bid'], webmaster@1: ); webmaster@1: $book = array_merge($book, db_fetch_array(db_query("SELECT title AS link_title FROM {node} WHERE nid = %d", $book['nid']))); webmaster@1: webmaster@1: // Items with depth > MENU_MAX_DEPTH cannot be saved. webmaster@1: if (menu_link_save($book)) { webmaster@1: db_query("INSERT INTO {book} (mlid, nid, bid) VALUES (%d, %d, %d)", $book['mlid'], $book['nid'], $book['bid']); webmaster@1: } webmaster@1: else { webmaster@1: // The depth was greater then MENU_MAX_DEPTH, so attach it to the webmaster@1: // closest valid parent. webmaster@1: $book['plid'] = db_result(db_query("SELECT plid FROM {menu_links} WHERE mlid = %d", $book['plid'])); webmaster@1: if (menu_link_save($book)) { webmaster@1: db_query("INSERT INTO {book} (mlid, nid, bid) VALUES (%d, %d, %d)", $book['mlid'], $book['nid'], $book['bid']); webmaster@1: } webmaster@1: } webmaster@1: $update_count--; webmaster@1: } webmaster@1: $ret['#finished'] = FALSE; webmaster@1: } webmaster@1: webmaster@1: if (empty($_SESSION['book_update_6000'])) { webmaster@1: $ret['#finished'] = TRUE; webmaster@1: $ret[] = array('success' => TRUE, 'query' => 'Relocated existing book pages.'); webmaster@1: $ret[] = update_sql("DROP TABLE {book_temp}"); webmaster@1: unset($_SESSION['book_update_6000']); webmaster@1: unset($_SESSION['book_update_6000_orphans']); webmaster@1: } webmaster@1: webmaster@1: return $ret; webmaster@1: } webmaster@1: webmaster@1: /** webmaster@1: * Implementation of hook_schema(). webmaster@1: */ webmaster@1: function book_schema() { webmaster@1: $schema['book'] = array( webmaster@1: 'description' => t('Stores book outline information. Uniquely connects each node in the outline to a link in {menu_links}'), webmaster@1: 'fields' => array( webmaster@1: 'mlid' => array( webmaster@1: 'type' => 'int', webmaster@1: 'unsigned' => TRUE, webmaster@1: 'not null' => TRUE, webmaster@1: 'default' => 0, webmaster@1: 'description' => t("The book page's {menu_links}.mlid."), webmaster@1: ), webmaster@1: 'nid' => array( webmaster@1: 'type' => 'int', webmaster@1: 'unsigned' => TRUE, webmaster@1: 'not null' => TRUE, webmaster@1: 'default' => 0, webmaster@1: 'description' => t("The book page's {node}.nid."), webmaster@1: ), webmaster@1: 'bid' => array( webmaster@1: 'type' => 'int', webmaster@1: 'unsigned' => TRUE, webmaster@1: 'not null' => TRUE, webmaster@1: 'default' => 0, webmaster@1: 'description' => t("The book ID is the {book}.nid of the top-level page."), webmaster@1: ), webmaster@1: ), webmaster@1: 'primary key' => array('mlid'), webmaster@1: 'unique keys' => array( webmaster@1: 'nid' => array('nid'), webmaster@1: ), webmaster@1: 'indexes' => array( webmaster@1: 'bid' => array('bid'), webmaster@1: ), webmaster@1: ); webmaster@1: webmaster@1: return $schema; webmaster@1: } webmaster@1: webmaster@1: