content_migrate.file.inc 12.4 KB
Newer Older
1 2
<?php
/**
3 4
 * @file content_migrate.file.inc
 * Code to implement Content Migrate hooks on behalf of the File module.
5 6 7 8
 */

/**
 * Implements hook_content_migrate_field_alter().
9
 *
10 11 12 13 14
 * Use this to tweak the conversion of field settings
 * from the D6 style to the D7 style for specific
 * situations not handled by basic conversion,
 * as when field types or settings are changed.
 */
15
function file_content_migrate_field_alter(&$field_value, $instance_value) {
16

17 18 19
  // There are a bunch of custom imagefield widgets. If they at least start the widget name with 'imagefield' this will work.
  if (substr($instance_value['widget']['type'], 0, 10) == 'imagefield') {
    // Module names and types changed.
20
    $field_value['messages'][] = t("Changed field type: The '@field' field type will be changed from '@type' to 'image'.", array('@type' => $field_value['type'], '@field' => $field_value['field_name'], '@widget' => $instance_value['widget']['type']));
21 22 23
    $field_value['module'] = 'image';
    $field_value['type'] = 'image';
    // default_image is now a field setting.
24 25 26
    if (array_key_exists('default_image', $instance_value['widget']['settings'])) {
      $field_value['settings']['default_image'] = $instance_value['widget']['settings']['default_image'];
    }
27 28 29 30
  }
  // There are a bunch of custom filefield widgets. If they at least start the widget name with 'filefield' this will work.
  if (substr($instance_value['widget']['type'], 0, 9) == 'filefield') {
    // Module names and types changed.
31
    $field_value['messages'][] = t("Changed field type: The '@field' field type will be changed from '@type' to 'file'.", array('@type' => $field_value['type'], '@field' => $field_value['field_name'], '@widget' => $instance_value['widget']['type']));
32 33
    $field_value['module'] = 'file';
    $field_value['type'] = 'file';
34 35
    // Some settings have changed names.
    $field_value['settings']['display_field'] = $field_value['settings']['list_field'];
36
    unset($field_value['settings']['list_field']);
37 38
    $field_value['settings']['display_default'] = $field_value['settings']['list_default'];
    unset($field_value['settings']['list_default']);
39 40 41 42 43
  }
}

/**
 * Implements hook_content_migrate_instance_alter().
44
 *
45 46 47 48 49
 * Use this to tweak the conversion of instance or widget settings
 * from the D6 style to the D7 style for specific
 * situations not handled by basic conversion, as when
 * formatter or widget names or settings are changed.
 */
50
function file_content_migrate_instance_alter(&$instance_value, $field_value) {
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  // Translate formatters.
  // There are a bunch of custom imagefield / filefield widgets. If they at
  // least start the widget name with 'imagefield' / 'filefield', we can detect
  // them and translate formatters accordingly
  if (substr($instance_value['widget']['type'], 0, 10) == 'imagefield') {
    foreach ($instance_value['display'] as $context => $settings) {
      switch ($settings['type']) {
        case 'image_plain':
          $instance_value['display'][$context]['type'] = 'image';
          $instance_value['display'][$context]['module'] = 'image';
          $instance_value['display'][$context]['settings'] = array(
            'image_style' => '',
            'image_link' => '',
          );
          break;

        case 'image_nodelink':
          $instance_value['display'][$context]['type'] = 'image';
          $instance_value['display'][$context]['module'] = 'image';
          $instance_value['display'][$context]['settings'] = array(
            'image_style' => '',
            'image_link' => 'content',
          );
          break;

        case 'image_imagelink':
          $instance_value['display'][$context]['type'] = 'image';
          $instance_value['display'][$context]['module'] = 'image';
          $instance_value['display'][$context]['settings'] = array(
            'image_style' => '',
            'image_link' => 'file',
          );
          break;
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

        default:
          // Handle formatters using imagecache. D7 does not store them in the same way.
          if (function_exists('image_styles')) {
            foreach (image_styles() as $style_name => $style) {
              if (substr($settings['type'], 0, strlen($style_name)) == $style_name) {
                switch (substr($settings['type'], strlen($style_name))) {
                  case '_default':
                    $instance_value['display'][$context]['type'] = 'image';
                    $instance_value['display'][$context]['module'] = 'image';
                    $instance_value['display'][$context]['settings'] = array(
                      'image_style' => $style_name,
                      'image_link' => '',
                    );
                    break;

                  case '_linked':
                    $instance_value['display'][$context]['type'] = 'image';
                    $instance_value['display'][$context]['module'] = 'image';
                    $instance_value['display'][$context]['settings'] = array(
                      'image_style' => $style_name,
                      'image_link' => 'content',
                    );
                    break;

                  case '_imagelink':
                    $instance_value['display'][$context]['type'] = 'image';
                    $instance_value['display'][$context]['module'] = 'image';
                    $instance_value['display'][$context]['settings'] = array(
                      'image_style' => $style_name,
                      'image_link' => 'file',
                    );
                    break;

                  case '_path':
                  case '_uri':
                    // Not supported in core D7.
                    break;
                }
              }
            }
          }
126 127 128 129 130 131 132 133
      }
    }
  }
  if (substr($instance_value['widget']['type'], 0, 9) == 'filefield') {
    foreach ($instance_value['display'] as $context => $settings) {
      // Note; the 'path_plain' D6 formatter has disappeared.
      if (in_array($settings['type'], array('default', 'url_plain'))) {
        $instance_value['display'][$context]['type'] = 'file_'. $settings['type'];
134
        $instance_value['display'][$context]['module'] = 'file';
135 136 137
      }
    }
  }
138

139
  // Translate the original imagefield and fielfield widgets.
140
  switch ($instance_value['widget']['module']) {
141

142 143 144 145 146 147
    case 'imagefield':
      // Module names and types changed.
      $instance_value['widget']['module'] = 'image';
      $instance_value['widget']['type'] = 'image';

      // Most settings became instance settings instead of widget settings, with a couple name changes.
148
      $instance_value['settings']['file_directory'] = isset($instance_value['widget']['settings']['file_path']) ? $instance_value['widget']['settings']['file_path'] : '';
149
      unset($instance_value['widget']['settings']['file_path']);
150 151 152
      $instance_value['settings']['max_filesize'] = isset($instance_value['widget']['settings']['max_filesize_per_file']) ? $instance_value['widget']['settings']['max_filesize_per_file'] : 0;
      unset($instance_value['widget']['settings']['max_filesize_per_file']);
      $instance_value['settings']['file_extensions'] = isset($instance_value['widget']['settings']['file_extensions']) ? $instance_value['widget']['settings']['file_extensions'] : '';
153
      unset($instance_value['widget']['settings']['file_extensions']);
154 155 156 157 158 159 160 161
      $settings = array(
        'max_resolution' => 0,
        'min_resolution' => 0,
      );
      foreach ($settings as $setting => $default_value) {
        $instance_value['settings'][$setting] = isset($instance_value['widget']['settings'][$setting]) ? $instance_value['widget']['settings'][$setting] : $default_value;
        unset($instance_value['widget']['settings'][$setting]);
      }
162 163 164 165 166 167 168 169 170 171 172 173

      // What is the difference between alt and custom_alt on the old field?
      $instance_value['settings']['alt_field'] = $instance_value['widget']['settings']['custom_alt'];
      unset($instance_value['widget']['settings']['custom_alt']);
      $instance_value['settings']['title_field'] = $instance_value['widget']['settings']['custom_title'];
      unset($instance_value['widget']['settings']['custom_title']);

      // Many settings have no place in the new field array.
      unset($instance_value['widget']['settings']['title']);
      unset($instance_value['widget']['settings']['alt']);
      unset($instance_value['widget']['settings']['max_filesize_per_node']);
      unset($instance_value['widget']['settings']['title_type']);
174

175 176 177 178 179 180 181 182 183
      // default_image is now a field setting.
      unset($instance_value['widget']['settings']['default_image']);
      break;

    case 'filefield':
      // Module names and types changed.
      $instance_value['widget']['module'] = 'file';
      $instance_value['widget']['type'] = 'file_generic';

184 185 186
      // Some settings had name changes, or moved from the widget to the
      // instance.
      $instance_value['settings']['file_directory'] = $instance_value['widget']['settings']['file_path'];
187
      unset($instance_value['widget']['settings']['file_path']);
188 189 190
      $instance_value['settings']['file_extensions'] = $instance_value['widget']['settings']['file_extensions'];
      unset($instance_value['widget']['settings']['file_extensions']);
      $instance_value['settings']['max_filesize'] = $instance_value['widget']['settings']['max_filesize_per_file'];
191 192
      unset($instance_value['widget']['settings']['max_filesize_per_file']);

193 194 195
      // This setting is gone.
      unset($instance_value['widget']['settings']['max_filesize_per_node']);

196 197 198 199 200 201 202 203 204 205
      break;
  }
}


/**
 * Implements hook_content_migrate_data_record_alter().
 *
 * Tweaks individual records in a field.
 */
206
function file_content_migrate_data_record_alter(&$record, $field, $instance) {
207 208 209 210

  switch($field['type']) {
    case 'image':
      // Map D6 imagefield field columns to D7 image field columns.
211
      if (!empty($record[$field['field_name'] . '_data']) && ($data = unserialize($record[$field['field_name'] . '_data']))) {
212 213 214 215 216 217 218 219 220 221 222
        $record[$field['field_name'] . '_alt'] = $data['alt'];
        $record[$field['field_name'] . '_title'] = $data['title'];
      }
      else {
        unset($record[$field['field_name'] . '_alt']);
        unset($record[$field['field_name'] . '_title']);
      }

    // Fall through.
    case 'file':
      // Map D6 filefield field columns to D7 file field columns.
223
      if (!empty($record[$field['field_name'] . '_data']) && ($data = unserialize($record[$field['field_name'] . '_data'])) && isset($data['description'])) {
224 225 226 227 228 229
        $record[$field['field_name'] . '_description'] = $data['description'];
      }
      else {
        unset($record[$field['field_name'] . '_description']);
      }

230
      if (isset($record[$field['field_name'] . '_list'])) {
231
        $record[$field['field_name'] . '_display'] = $record[$field['field_name'] . '_list'];
232
        unset($record[$field['field_name'] . '_list']);
233 234
      }

235 236 237 238 239 240 241 242 243 244 245
      // Copies imagefield data from the old 'files' table into 'files_managed' and sets file_usage
      // Mostly copied from system_update_7061, which does the same for the D6 core 'upload' module

      $nid = $record['entity_id'];
      $fid = $record[$field['field_name'] . '_fid'];

      if (empty($fid)) {
        $record = NULL;
        return;
      };

246 247 248 249 250 251 252
      $file = db_select('files', 'f')
        ->fields('f', array('fid', 'uid', 'filename', 'filepath', 'filemime', 'filesize', 'timestamp', 'status'))
        ->condition('fid', $fid)
        ->execute()
        ->fetchObject();

      // In case of unknown file, just abort.
253 254
      if ((!is_object($file)) || is_null($file->fid)) {
        break;
255
      }
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

      $basename = variable_get('file_directory_path', conf_path() . '/files');
      $scheme = file_default_scheme() . '://';

      // We will convert filepaths to uri using the default scheme
      // and stripping off the existing file directory path.
      $file->uri = $scheme . str_replace($basename, '', $file->filepath);
      $file->uri = file_stream_wrapper_uri_normalize($file->uri);
      unset($file->filepath);

      // Insert into the file_managed table.
      // Each fid should only be stored once in file_managed.
      db_merge('file_managed')
        ->key(array(
          'fid' => $file->fid,
        ))
        ->fields(array(
          'uid' => $file->uid,
          'filename' => $file->filename,
          'uri' => $file->uri,
          'filemime' => $file->filemime,
          'filesize' => $file->filesize,
          'status' => $file->status,
          'timestamp' => $file->timestamp,
        ))
        ->execute();

      // Add the usage entry for the file.
      file_usage_add($file, 'file', 'node', $nid);
      break;
  }
287
}