All pastes #516756 Raw Edit

Unnamed

public text v1 · immutable
#516756 ·published 2007-05-28 20:54 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  if (!isset($_SESSION['book_update_6000'])) {
    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} bt (bt.nid, bt.parent, bt.weight) SELECT b.nid, b.parent, b.weight FROM {book} b INNER JOIN {node} 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 nid from {book_temp} WHERE parent = 0");
      
      // Collect all books - top-level nodes
      while ($a = db_fetch_array($r)) {
        $_SESSION['book_update_6000'][] = $a['nid'];
      }
      $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;
    }
  }

  if (!empty($_SESSION['book_update_6000'])) {

    $ret['#finished'] = FALSE;
  }

  //TODO batched updates - the hard part.  Pop each book's nid off the session
  // variable when all it's chlidren have been updated?
  
  $finished = empty($_SESSION['book_update_6000']);
  if ($finished) {
    $ret[] = update_sql("DROP TABLE {book_temp}");
    unset($_SESSION['book_update_6000']);
    unset($_SESSION['book_update_6000_stack']);
  }
  
  $ret['#finished'] = $finished;
  return $ret;
}