You are here

text.install in Content Construction Kit (CCK) 5

File

text.install
View source
<?php

/**
 * Implementation of hook_install().
 */
function text_install() {
}

/**
 * Add node ID column so we can delete old revisions at node delete time.
 */
function text_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'pgsql':
      db_add_column($ret, 'node_field_shorttext_data', 'nid', 'integer', array(
        'not null' => TRUE,
        'default' => 0,
      ));
      db_add_column($ret, 'node_field_longtext_data', 'nid', 'integer', array(
        'not null' => TRUE,
        'default' => 0,
      ));
      break;
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {node_field_shorttext_data} ADD COLUMN nid int(10) NOT NULL DEFAULT 0");
      $ret[] = update_sql("ALTER TABLE {node_field_longtext_data} ADD COLUMN nid int(10) NOT NULL DEFAULT 0");
      break;
  }
  return $ret;
}

/**
 * Populate nid column in existing short-text fields.
 */
function text_update_2() {

  // Multi-part update
  if (!isset($_SESSION['text_update_2'])) {
    $_SESSION['text_update_2'] = 0;
    $_SESSION['text_update_2_max'] = db_result(db_query("SELECT COUNT(*) FROM {node_field_shorttext_data}"));
  }
  $limit = 20;
  $result = db_query_range("SELECT nr.nid, nfsd.vid, nfsd.field_name, nfsd.delta FROM {node_field_shorttext_data} nfsd LEFT JOIN {node_revisions} nr ON nr.vid = nfsd.vid", $_SESSION['text_update_2'], $limit);
  if (db_num_rows($result) == 0) {
    unset($_SESSION['text_update_2']);
    unset($_SESSION['text_update_2_max']);
    return array();
  }
  while ($data = db_fetch_object($result)) {
    $_SESSION['text_update_2']++;
    db_query("UPDATE {node_field_shorttext_data} SET nid = %d WHERE vid = %d AND field_name = '%s' AND delta = %d", $data->nid, $data->vid, $data->field_name, $data->delta);
  }
  return array(
    '#finished' => $_SESSION['text_update_2'] / $_SESSION['text_update_2_max'],
  );
}

/**
 * Populate nid column in existing long-text fields.
 */
function text_update_3() {

  // Multi-part update
  if (!isset($_SESSION['text_update_3'])) {
    $_SESSION['text_update_3'] = 0;
    $_SESSION['text_update_3_max'] = db_result(db_query("SELECT COUNT(*) FROM {node_field_shorttext_data}"));
  }
  $limit = 20;
  $result = db_query_range("SELECT nr.nid, nfld.vid, nfld.field_name, nfld.delta FROM {node_field_longtext_data} nfld LEFT JOIN {node_revisions} nr ON nr.vid = nfld.vid", $_SESSION['text_update_3'], $limit);
  if (db_num_rows($result) == 0) {
    unset($_SESSION['text_update_3']);
    unset($_SESSION['text_update_3_max']);
    return array();
  }
  while ($data = db_fetch_object($result)) {
    $_SESSION['text_update_3']++;
    db_query("UPDATE {node_field_longtext_data} SET nid = %d WHERE vid = %d AND field_name = '%s' AND delta = %d", $data->nid, $data->vid, $data->field_name, $data->delta);
  }
  return array(
    '#finished' => $_SESSION['text_update_3'] / $_SESSION['text_update_3_max'],
  );
}

/**
 * Data is now stored in per-field tables.
 */
function text_update_4() {
  $ret = array();
  include_once './' . drupal_get_path('module', 'content') . '/content.module';
  include_once './' . drupal_get_path('module', 'content') . '/content_admin.inc';
  content_clear_type_cache();
  $fields = content_fields();
  foreach ($fields as $field) {
    switch ($field['type']) {
      case 'text_shorttext':
        $columns = array(
          'value' => array(
            'type' => 'varchar',
            'length' => 255,
            'not null' => TRUE,
            'default' => "''",
          ),
          'format' => array(
            'type' => 'int',
            'length' => 10,
            'unsigned' => TRUE,
            'not null' => TRUE,
            'default' => 0,
          ),
        );
        if ($field['text_processing'] == 0) {
          unset($columns['format']);
        }

        // the following line will trigger (negligible) warnings if content_update_5 was run before
        // (column already exists)
        @content_alter_db_field(array(), array(), $field, $columns);
        $db_info = content_database_info($field);
        $table = $db_info['table'];
        if ($field['multiple']) {
          if ($field['text_processing']) {
            $ret[] = update_sql('INSERT INTO {' . $table . '} (vid, delta, nid, ' . $field['field_name'] . '_value, ' . $field['field_name'] . "_format) SELECT vid, delta, nid, field_shorttext, format FROM {node_field_shorttext_data} WHERE field_name = '" . $field['field_name'] . "'");
          }
          else {
            $ret[] = update_sql('INSERT INTO {' . $table . '} (vid, delta, nid, ' . $field['field_name'] . "_value) SELECT vid, delta, nid, field_shorttext FROM {node_field_shorttext_data} WHERE field_name = '" . $field['field_name'] . "'");
          }
        }
        else {
          if ($field['text_processing']) {
            $ret[] = update_sql('INSERT INTO {' . $table . '} (vid, nid, ' . $field['field_name'] . '_value, ' . $field['field_name'] . "_format) SELECT vid, nid, field_shorttext, format FROM {node_field_shorttext_data} WHERE field_name = '" . $field['field_name'] . "'");
          }
          else {
            $ret[] = update_sql('INSERT INTO {' . $table . '} (vid, nid, ' . $field['field_name'] . "_value) SELECT vid, nid, field_shorttext FROM {node_field_shorttext_data} WHERE field_name = '" . $field['field_name'] . "'");
          }
        }
        break;
      case 'text_longtext':
        $columns = array(
          'value' => array(
            'type' => 'mediumtext',
            'not null' => TRUE,
            'default' => "''",
          ),
          'format' => array(
            'type' => 'int',
            'length' => 10,
            'unsigned' => TRUE,
            'not null' => TRUE,
            'default' => 0,
          ),
        );
        if ($field['text_processing'] == 0) {
          unset($columns['format']);
        }

        // the following line will trigger (negligible) warnings if content_update_5 was run before
        // (column already exists)
        @content_alter_db_field(array(), array(), $field, $columns);
        $db_info = content_database_info($field);
        $table = $db_info['table'];
        if ($field['multiple']) {
          if ($field['text_processing']) {
            $ret[] = update_sql('INSERT INTO {' . $table . '} (vid, delta, nid, ' . $field['field_name'] . '_value, ' . $field['field_name'] . "_format) SELECT vid, delta, nid, field_longtext, format FROM {node_field_longtext_data} WHERE field_name = '" . $field['field_name'] . "'");
          }
          else {
            $ret[] = update_sql('INSERT INTO {' . $table . '} (vid, delta, nid, ' . $field['field_name'] . "_value) SELECT vid, delta, nid, field_longtext FROM {node_field_longtext_data} WHERE field_name = '" . $field['field_name'] . "'");
          }
        }
        else {
          if ($field['text_processing']) {
            $ret[] = update_sql('INSERT INTO {' . $table . '} (vid, nid, ' . $field['field_name'] . '_value, ' . $field['field_name'] . "_format) SELECT vid, nid, field_longtext, format FROM {node_field_longtext_data} WHERE field_name = '" . $field['field_name'] . "'");
          }
          else {
            $ret[] = update_sql('INSERT INTO {' . $table . '} (vid, nid, ' . $field['field_name'] . "_value) SELECT vid, nid, field_longtext FROM {node_field_longtext_data} WHERE field_name = '" . $field['field_name'] . "'");
          }
        }
        break;
    }
  }
  $ret[] = update_sql('DROP TABLE {node_field_shorttext_data}');
  $ret[] = update_sql('DROP TABLE {node_field_longtext_data}');
  db_query('DELETE FROM {cache}');
  return $ret;
}

/**
 * Consolidate into a single text field type.
 */
function text_update_5() {
  $ret = array();
  $result = db_query("SELECT field_name, type, global_settings FROM {node_field} WHERE type IN ('text_shorttext', 'text_longtext')");
  while ($field = db_fetch_object($result)) {
    $global_settings = array();
    if ($field->global_settings) {
      $global_settings = unserialize($field->global_settings);
    }
    if ($field->type == 'text_shorttext') {
      $global_settings['max_length'] = '255';
    }
    else {
      $global_settings['max_length'] = '';
    }
    db_query("UPDATE {node_field} SET type = 'text', global_settings = '%s' WHERE field_name = '%s'", serialize($global_settings), $field->field_name);
  }
  db_query('DELETE FROM {cache}');
  return $ret;
}

Functions

Namesort descending Description
text_install Implementation of hook_install().
text_update_1 Add node ID column so we can delete old revisions at node delete time.
text_update_2 Populate nid column in existing short-text fields.
text_update_3 Populate nid column in existing long-text fields.
text_update_4 Data is now stored in per-field tables.
text_update_5 Consolidate into a single text field type.