rendered paste body<?php
// $Id: book.install,v 1.7 2007/05/25 12:46:43 dries Exp $
/**
* Implementation of hook_install().
*/
function book_install() {
// Create tables.
drupal_install_schema('book');
}
/**
* Implementation of hook_uninstall().
*/
function book_uninstall() {
// Delete menu links
db_query("DELETE FROM {menu_links} WHERE module = 'book'");
menu_cache_clear_all();
// Remove tables.
drupal_uninstall_schema('book');
}
function book_update_6000() {
$ret = array();
$ret['#finished'] = FALSE;
// The number of items to do in a batch
$batch = 50;
$step = ($_SESSION['book_update_6000']['step']? $_SESSION['book_update_6000']['step'] : 1);
switch($step) {
case 1:
// Killswitch for doing the update
$count = db_num_rows(db_query("SELECT * FROM {book}"));
if($count < 1) {
// No exising nodes in the hierarchy, so might as well drop the table
// and re-create it
$ret[] = update_sql("DROP TABLE {book}");
drupal_install_schema('book');
$ret['#finished'] = TRUE;
}
// Do the update
else {
// Create a temporary book table
$schema['book_temp'] = array(
'fields' => array(
'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'parent' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'weight' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
),
'primary key' => array('nid'),
'name' => 'book_temp',
'module' => 'book',
);
db_create_table($ret, $schema['book_temp']);
$_SESSION['book_update_6000']['step']++;
// Only very little here, because the next step is expensive
break;
case 2:
$current_batch = $_SESSION['book_update_6000']['move_batch'] = ($_SESSION['book_update_6000']['move_batch']? $_SESSION['book_update_6000']['move_batch'] : 0);
$next_batch = $_SESSION['book_update_6000']['move_batch'] + 1;
// If it hasn't gone through every node
if(db_num_rows(db_query_range("SELECT nid FROM {book}", $current_batch * $batch)) != 0) {
db_query_range("INSERT INTO {book_temp} (nid, parent, weight) SELECT nid, parent, weight FROM {book}", $current_batch * $batch, $next_barch * $batch);
}
else {
$_SESSION['book_update_6000']['step']++;
}
$_SESSION['book_update_6000']['move_batch']++;
break;
case 3:
$current_batch = $_SESSION['book_update_6000']['process_1_batch'] = ($_SESSION['book_update_6000']['process_1_batch']? $_SESSION['book_update_6000']['process_1_batch'] : 0);
$next_batch = $_SESSION['book_update_6000']['process_1_batch'] + 1;
// If it hasn't gone through every node
if(db_num_rows(db_query_range("SELECT nid FROM {book}", $current_batch * $batch, $next_barch * $batch)) != 0) {
$book_nodes = db_query_range("SELECT b.nid, b.weight, b.parent, n.title FROM {book_temp} b INNER JOIN {node} n ON b.nid = n.nid", $current_batch * $batch, $next_barch * $batc);
while($book_node = db_fetch_object($book_nodes)) {
$nodes[$book_node->nid] = $book_node;
$nodes[$book_node->nid]->depth = 1;
}
$_SESSION['book_update_6000']['nodes'] = $nodes;
}
else {
$_SESSION['book_update_6000']['step']++;
}
$_SESSION['book_update_6000']['process_1_batch']++;
break;
case 4:
$current_batch = $_SESSION['book_update_6000']['process_2_batch'] = ($_SESSION['book_update_6000']['process_2_batch']? $_SESSION['book_update_6000']['process_2_batch'] : 0);
$next_batch = $current_batch + 1;
$nodes = $_SESSION['book_update_6000']['nodes'];
// If it hasn't gone through every node
if(count($nodes) < $current_batch * $batch) {
$nodes = array_slice($nodes, $current_batch * $batch, $next_batch * $batch);
foreach($nodes as $node) {
$node->book_id = _book_get_top_level($node, $nodes, $flag);
$node = $nodes[$node->nid];
unset($nodes[$node->nid]);
if ($flag) {
$nodes[$node->nid] = $node;
}
}
$_SESSION['book_update_6000']['nodes'] = $nodes;
}
else {
$_SESSION['book_update_6000']['step']++;
}
$_SESSION['book_update_6000']['process_2_batch']++;
break;
case 5:
$current_batch = $_SESSION['book_update_6000']['process_2_batch'] = ($_SESSION['book_update_6000']['process_2_batch']? $_SESSION['book_update_6000']['process_2_batch'] : 0);
$next_batch = $current_batch + 1;
$nodes = $_SESSION['book_update_6000']['nodes'];
// If it hasn't gone through every node
if(count($node) < $current_batch * $batch) {
$nodes = array_slice($nodes, $current_batch * $batch, $next_batch * $batch);
foreach($nodes as $node) {
$node->book_id = _book_get_top_level($node, $nodes, $flag);
if (!$flag) {
unset($nodes[$node->nid]);
}
}
$_SESSION['book_update_6000']['nodes'] = $nodes;
}
else {
$_SESSION['book_update_6000']['step']++;
}
$_SESSION['book_update_6000']['process_2_batch']++;
break;
case 6:
// Update the schema
$ret[] = update_sql("DROP TABLE {book}");
include_once './modules/book/book.schema';
$schema = book_schema();
_drupal_initialize_schema('book', $schema);
db_create_table($ret, $schema['book']);
case 7:
foreach($nodes as $node) {
if($node->depth == 1) {
$plid = 0;
}
else {
$mlid = db_fetch_array(db_query("SELECT mlid FROM {book} WHERE nid = %d", $node->book_id));
$plid = $mlid['mlid'];
}
$link = array('link_title' => $node->title,
'link_path' => 'node/'. $node->nid,
'router_path' => 'node/%',
'menu_name' => 'book-toc-'. $node->book_id,
'module' => 'book',
'plid' => $plid,
);
if(menu_link_save($link)) {
$ret[] = update_sql("INSERT INTO {book} (nid, mlid, book_id, child_type) VALUES (%d, %d, %d, '%s')", $node->nid, $link['mlid'], $node->book_id, 'book');
}
}
}
return $ret;
}
function _book_get_top_level($node, $nodes, &$flag) {
$flag = FALSE;
if($node->parent == 0) {
return $node->nid;
}
else {
$node->depth++;
if(in_array($node->parent, $nodes)) {
return _book_get_top_level($nodes[$node->parent], $nodes);
}
else {
$flag = TRUE;
return $node->nid;
}
}
}
include_once './modules/node/node.module';
include_once './modules/node/content_types.inc';
$book_node_type = array(
'type' => 'book',
'name' => t('Book page'),
'module' => 'node', // This is cheating, but it works
'description' => t("A book is a collaborative writing effort: users can collaborate writing the pages of the book, positioning the pages in the right order, and reviewing or modifying pages previously written. So when you have some information to share or when you read a page of the book and you didn't like it, or if you think a certain page could have been written better, you can do something about it."),
'custom' => TRUE,
'modified' => TRUE,
'locked' => FALSE,
);
$book_node_type = (object) _node_type_set_defaults($book_node_type);
node_type_save($book_node_type);