You are here

function filefield_update_6001 in FileField 6.3

Same name and namespace in other branches
  1. 6.2 filefield.install \filefield_update_6001()

Upgrade FileField to Drupal 6.

File

./filefield.install, line 112

Code

function filefield_update_6001() {
  if ($abort = content_check_update('filefield')) {
    return $abort;
  }
  $ret = array();
  module_load_include('inc', 'content', 'includes/content.admin');

  // Rename the field type from file to filefield. adhere to module namespace.
  $ret[] = update_sql("UPDATE {content_node_field} SET type = 'filefield', module = 'filefield', active = 1 WHERE type = 'file'");

  // Rename default widget to filefield_widget. adhere to module namespace.
  $ret[] = update_sql("UPDATE {content_node_field_instance} SET widget_type = 'filefield_widget', widget_module = 'filefield', widget_active = 1 WHERE widget_type = 'file'");

  // Update list default value and force list settings.
  $result = db_query("SELECT * FROM {content_node_field} WHERE type = 'filefield'");
  while ($field = db_fetch_object($result)) {
    $updated = FALSE;
    $field_settings = unserialize($field->global_settings);
    if (!isset($field_settings['list_default']) || !is_numeric($field_settings['list_default'])) {
      $field_settings['list_default'] = 1;
      $updated = TRUE;
    }

    // Set behavior to match old force_list behavior.
    if (!empty($field_settings['force_list'])) {
      $field_settings['list_default'] = 1;
      $field_settings['force_list_default'] = 1;
      $updated = TRUE;
    }
    if ($updated) {
      db_query("UPDATE {content_node_field} SET global_settings = '%s' WHERE field_name = '%s'", serialize($field_settings), $field->field_name);
    }
  }

  // Re-enable all the FileFields on the site.
  content_associate_fields('filefield');

  // Build a list of fields that need data updating.
  $fields = array();
  foreach (content_types_install() as $type_name => $type_fields) {
    foreach ($type_fields as $field) {
      if ($field['type'] == 'filefield') {

        // We only process a given field once.
        $fields[$field['field_name']] = $field;
      }
    }
  }

  // Update database storage (add data column, remove description, set NOT NULL).
  foreach ($fields as $field) {
    $new_field = $field;

    // Setup the previous definition.
    $field['columns']['description'] = array(
      'type' => 'varchar',
    );
    $field['columns']['fid']['not null'] = TRUE;
    $field['columns']['list']['not null'] = TRUE;
    unset($field['columns']['data']);

    // Setup the new definition.
    $new_field['columns']['data'] = array(
      'type' => 'text',
      'serialize' => TRUE,
    );
    $new_field['columns']['fid']['not null'] = FALSE;
    $new_field['columns']['list']['size'] = 'tiny';
    $new_field['columns']['list']['not null'] = FALSE;
    unset($new_field['columns']['description']);
    content_alter_db($field, $new_field);
  }

  // Build a batch that migrates the data in each filefield
  $batch = array(
    'title' => t('Migrating filefield values'),
    'operations' => array(),
    'file' => drupal_get_path('module', 'filefield') . '/filefield.install',
  );
  foreach ($fields as $field_name => $field) {
    if ($field['type'] == 'filefield') {
      $batch['operations'][] = array(
        '_filefield_update_6001_move_operation',
        array(
          $field,
        ),
      );
      $batch['operations'][] = array(
        '_filefield_update_6001_drop_operation',
        array(
          $field,
        ),
      );
    }
  }
  batch_set($batch);

  // Clear caches.
  cache_clear_all('*', content_cache_tablename(), TRUE);
  cache_clear_all('*', 'cache', TRUE);
  return $ret;
}