You are here

function media_update_7208 in D7 Media 7.4

Same name and namespace in other branches
  1. 7.2 media.install \media_update_7208()
  2. 7.3 media.install \media_update_7208()

Drop the media_types table and migrate files to file_entity types.

File

./media.install, line 860
Install, update and uninstall functions for the Media module.

Code

function media_update_7208() {

  // Reset static cache to ensure our new file types are recognized
  drupal_static_reset('ctools_export_load_object_table_exists');
  if (!db_table_exists('media_type')) {

    // No types to migrate.
    return;
  }

  // @see http://drupal.org/node/1292382
  if (!function_exists('file_type_get_enabled_types')) {
    throw new DrupalUpdateException('The File Entity module needs to be upgraded before continuing.');
  }
  else {
    $existing_types = db_select('media_type', 'mt')
      ->orderBy('weight')
      ->fields('mt')
      ->execute()
      ->fetchAllAssoc('name');
    foreach ($existing_types as &$type) {
      $type->type_callback_args = unserialize($type->type_callback_args);
    }
    include_once DRUPAL_ROOT . '/includes/file.mimetypes.inc';
    $mapping = file_mimetype_mapping();

    // We do not migrate this type, since there is no way to handle its weight.
    unset($existing_types['default']);
    foreach ($existing_types as $type) {
      $extensions = isset($type->type_callback_args['extensions']) ? $type->type_callback_args['extensions'] : array();
      $mimetypes = isset($type->type_callback_args['mimetypes']) ? $type->type_callback_args['mimetypes'] : array();

      // Add mimetypes by extensions.
      foreach ($extensions as $extension) {
        if (isset($mapping['extensions'][$extension])) {
          $type->mimetypes[] = $mapping['mimetypes'][$mapping['extensions'][$extension]];
        }
      }

      // Add rest mimetypes.
      foreach ($mimetypes as $mimetype) {

        // Mimetype is a regex pattern.
        foreach ($mapping['mimetypes'] as $mapping_mimetype) {
          if (preg_match($mimetype, $mapping_mimetype) && !in_array($mapping_mimetype, $type->mimetypes)) {
            $type->mimetypes[] = $mapping_mimetype;
          }
        }
      }
      $type->streams = isset($type->type_callback_args['streams']) ? $type->type_callback_args['streams'] : array();
      $type->type = $type->name;

      // Merge existing type with new ones.
      if ($new_type = file_type_load($type->name)) {
        $new_type->mimetypes = array_merge($type->mimetypes, $new_type->mimetypes);
        if (!empty($new_type->streams)) {
          $new_type->streams = array_merge($type->streams, $new_type->streams);
        }
        else {
          $new_type->streams = $type->streams;
        }
      }
      else {
        $new_type = $type;
      }
      file_type_save($new_type);
    }
    db_drop_table('media_type');

    // Special treatment for old media application type to new file_entity
    // document one. Add some more mimetypes to document.
    $document_type = file_type_load('document');
    if (!$document_type) {
      return;
    }
    foreach ($mapping['mimetypes'] as $mimetype) {
      $is_document = strpos($mimetype, 'document') !== FALSE || strpos($mimetype, 'application/vnd.ms-') !== FALSE;
      if ($is_document && !in_array($mimetype, $document_type->mimetypes)) {
        $document_type->mimetypes[] = $mimetype;
      }
    }
    file_type_save($document_type);
  }
}