All pastes #517282 Raw Edit

pwolanin

public php v1 · immutable
#517282 ·published 2007-05-29 00:34 UTC
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();  // Multi-part update  if (!isset($_SESSION['book_update_6000'])) {      $book_node_type = array(        'type' => 'book',        'name' => t('Book page'),        'module' => 'node', // This is cheating, but it works        'description' => t("A simple node type like page.  Maybe be used "),        'custom' => TRUE,        'modified' => TRUE,        'locked' => FALSE,      );            $book_node_type = (object)_node_type_set_defaults($book_node_type);      node_type_save($book_node_type);    if (db_result(db_query("SELECT COUNT(*) FROM {book}"))) {  // Do the update            $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'),      );      $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} (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, $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;    }  }  else {  // Do the batched part of the update    $i = 20; // Update this many at a time    print_r($_SESSION['book_update_6000']);    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 = array_merge($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 = array_merge($book, db_fetch_array(db_query("SELECT title AS link_title, type AS child_type FROM {node} WHERE nid = %d", $book['nid'])));            if (menu_link_save($book)) { // Note - items with depth > 6 drop out of the hierarchy        db_query("INSERT INTO {book} (mlid, nid, book_id, child_type) VALUES (%d, %d, %d, '%s')", $book['mlid'], $book['nid'], $book['book_id'], $book['child_type']);      }      else { // Too deep        $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, book_id, child_type) VALUES (%d, %d, %d, '%s')", $book['mlid'], $book['nid'], $book['book_id'], $book['child_type']);        }      }      $i--;    }    $ret['#finished'] = FALSE;  }  $ret['#finished'] = empty($_SESSION['book_update_6000']);  if ($ret['#finished']) {    $ret[] = update_sql("DROP TABLE {book_temp}");    unset($_SESSION['book_update_6000']);  }  return $ret;}