View source
<?php
function book_install() {
drupal_install_schema('book');
_book_install_type_create();
}
function book_uninstall() {
db_query("DELETE FROM {menu_links} WHERE module = 'book'");
menu_cache_clear_all();
drupal_uninstall_schema('book');
}
function _book_install_type_create() {
$book_node_type = array(
'type' => 'book',
'name' => t('Book page'),
'module' => 'node',
'description' => t('A <em>book page</em> is a page of content, organized into a collection of related entries collectively known as a <em>book</em>. A <em>book page</em> automatically displays links to adjacent pages, providing a simple navigation system for organizing and reviewing structured content.'),
'custom' => TRUE,
'modified' => TRUE,
'locked' => FALSE,
);
$book_node_type = (object) _node_type_set_defaults($book_node_type);
node_type_save($book_node_type);
variable_set('node_options_book', array(
'status',
));
variable_set('book_allowed_types', array(
'book',
));
variable_set('book_child_type', 'book');
}
function book_update_6000() {
$ret = array();
if (!isset($_SESSION['book_update_6000'])) {
$schema['book'] = array(
'fields' => array(
'mlid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'bid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
),
'primary key' => array(
'mlid',
),
'unique keys' => array(
'nid' => array(
'nid',
),
),
'indexes' => array(
'bid' => array(
'bid',
),
),
);
_book_install_type_create();
$replace = array(
'outline posts in books' => 'administer book outlines',
'create book pages' => 'create book content',
'edit book pages' => 'edit any book content',
'edit own book pages' => 'edit own book content',
'see printer-friendly version' => 'access printer-friendly version',
);
$query = db_query("SELECT rid, perm FROM {permission} ORDER BY rid");
while ($role = db_fetch_object($query)) {
$fixed_perm = strtr($role->perm, $replace);
if (strpos($role->perm, 'create book pages') !== FALSE) {
$fixed_perm .= ', add content to books';
}
if ($fixed_perm != $role->perm) {
$ret[] = update_sql("UPDATE {permission} SET perm = '{$fixed_perm}' WHERE rid = {$role->rid}");
}
}
if (db_result(db_query("SELECT COUNT(*) FROM {book}"))) {
$schema['book_temp'] = array(
'fields' => 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',
),
);
db_create_table($ret, 'book_temp', $schema['book_temp']);
$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");
$ret[] = update_sql("DROP TABLE {book}");
db_create_table($ret, 'book', $schema['book']);
$_SESSION['book_update_6000_orphans']['from'] = 0;
$_SESSION['book_update_6000'] = array();
$result = db_query("SELECT * from {book_temp} WHERE parent = 0");
while ($a = db_fetch_array($result)) {
$_SESSION['book_update_6000'][] = $a;
}
$ret['#finished'] = FALSE;
return $ret;
}
else {
$ret[] = update_sql("DROP TABLE {book}");
db_create_table($ret, 'book', $schema['book']);
return $ret;
}
}
elseif ($_SESSION['book_update_6000_orphans']) {
$update_count = 400;
$result = db_query_range("SELECT * FROM {book_temp}", $_SESSION['book_update_6000_orphans']['from'], $update_count);
$has_rows = FALSE;
while ($book = db_fetch_array($result)) {
$has_rows = TRUE;
if ($book['parent'] && !db_result(db_query("SELECT COUNT(*) FROM {book_temp} WHERE nid = %d", $book['parent']))) {
if (empty($_SESSION['book_update_6000_orphans']['book'])) {
$book['parent'] = 0;
$_SESSION['book_update_6000_orphans']['book'] = $book;
$ret[] = array(
'success' => TRUE,
'query' => 'Relocated orphan book pages.',
);
}
else {
$book['parent'] = $_SESSION['book_update_6000_orphans']['book']['nid'];
$_SESSION['book_update_6000'][] = $book;
}
}
}
if ($has_rows) {
$_SESSION['book_update_6000_orphans']['from'] += $update_count;
}
else {
if (!empty($_SESSION['book_update_6000_orphans']['book'])) {
$_SESSION['book_update_6000'][] = $_SESSION['book_update_6000_orphans']['book'];
}
$_SESSION['book_update_6000_orphans'] = FALSE;
}
$ret['#finished'] = FALSE;
return $ret;
}
else {
$update_count = 100;
while ($update_count && $_SESSION['book_update_6000']) {
$book = array_pop($_SESSION['book_update_6000']);
$result = db_query("SELECT * FROM {book_temp} WHERE parent = %d", $book['nid']);
while ($a = db_fetch_array($result)) {
$_SESSION['book_update_6000'][] = $a;
}
if ($book['parent']) {
$parent = db_fetch_array(db_query("SELECT b.mlid AS plid, b.bid FROM {book} b WHERE b.nid = %d", $book['parent']));
$book = array_merge($book, $parent);
}
else {
$book['plid'] = 0;
$book['bid'] = $book['nid'];
}
$book += array(
'module' => 'book',
'link_path' => 'node/' . $book['nid'],
'router_path' => 'node/%',
'menu_name' => 'book-toc-' . $book['bid'],
);
$book = array_merge($book, db_fetch_array(db_query("SELECT title AS link_title FROM {node} WHERE nid = %d", $book['nid'])));
if (menu_link_save($book)) {
db_query("INSERT INTO {book} (mlid, nid, bid) VALUES (%d, %d, %d)", $book['mlid'], $book['nid'], $book['bid']);
}
else {
$book['plid'] = db_result(db_query("SELECT plid FROM {menu_links} WHERE mlid = %d", $book['plid']));
if (menu_link_save($book)) {
db_query("INSERT INTO {book} (mlid, nid, bid) VALUES (%d, %d, %d)", $book['mlid'], $book['nid'], $book['bid']);
}
}
$update_count--;
}
$ret['#finished'] = FALSE;
}
if (empty($_SESSION['book_update_6000'])) {
$ret['#finished'] = TRUE;
$ret[] = array(
'success' => TRUE,
'query' => 'Relocated existing book pages.',
);
$ret[] = update_sql("DROP TABLE {book_temp}");
unset($_SESSION['book_update_6000']);
unset($_SESSION['book_update_6000_orphans']);
}
return $ret;
}
function book_schema() {
$schema['book'] = array(
'description' => 'Stores book outline information. Uniquely connects each node in the outline to a link in {menu_links}',
'fields' => array(
'mlid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => "The book page's {menu_links}.mlid.",
),
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => "The book page's {node}.nid.",
),
'bid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => "The book ID is the {book}.nid of the top-level page.",
),
),
'primary key' => array(
'mlid',
),
'unique keys' => array(
'nid' => array(
'nid',
),
),
'indexes' => array(
'bid' => array(
'bid',
),
),
);
return $schema;
}