View source  
  <?php
define('MEDIA_UPDATE_RECORDS_ON_INSTALL', 200);
function media_schema() {
  $schema['media_type'] = array(
    'description' => 'Stores the settings for media types.',
    'fields' => array(
      'name' => array(
        'description' => 'The machine name of the media type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'label' => array(
        'description' => 'The label of the media type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'base' => array(
        'description' => 'If this is a base type (i.e. cannot be deleted)',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'weight' => array(
        'description' => 'Weight of media type. Determines which one wins when claiming a piece of media (first wins)',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'normal',
      ),
      'type_callback' => array(
        'description' => 'Callback to determine if provided media is of this type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'type_callback_args' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of name value pairs that will be passed to the callback function',
      ),
    ),
    'primary key' => array(
      'name',
    ),
  );
  $schema['media_list_type'] = array(
    'description' => 'Stores the user preference for whether to list as table or images.',
    'fields' => array(
      'uid' => array(
        'description' => 'The {user}.uid of the user.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'type' => array(
        'description' => 'The type of display (table or images).',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array(
      'uid',
    ),
  );
  $schema['media_filter_usage'] = array(
    'description' => 'Stores fids that have been included in the media tag in formatted textareas.',
    'fields' => array(
      'fid' => array(
        'description' => 'The media {file_managed}.fid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'timestamp' => array(
        'description' => 'The timestamp the fid was last recorded by media_filter()',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'foreign keys' => array(
      'file_managed' => array(
        'table' => 'file_managed',
        'columns' => array(
          'fid' => 'fid',
        ),
      ),
    ),
    'primary key' => array(
      'fid',
    ),
    'indexes' => array(
      'timestamp' => array(
        'timestamp',
      ),
    ),
  );
  $schema['cache_media_xml'] = drupal_get_schema_unprocessed('system', 'cache');
  $schema['cache_media_xml']['description'] = 'Cache table for the the results of retreived XML documents for remote streams.';
  return $schema;
}
function media_field_schema($field) {
  return array(
    'columns' => array(
      'fid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => FALSE,
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
      'data' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
      ),
    ),
    'indexes' => array(
      'fid' => array(
        'fid',
      ),
    ),
    'foreign keys' => array(
      'file_managed' => array(
        'table' => 'file_managed',
        'columns' => array(
          'fid' => 'fid',
        ),
      ),
    ),
  );
}
function media_install() {
  
  $types['default'] = new stdClass();
  $types['default']->name = 'default';
  $types['default']->label = "Other";
  $types['default']->base = TRUE;
  $types['default']->weight = 1000;
  $types['default']->type_callback_args = array(
    'match_type' => 'any',
    'mimetypes' => array(
      '/.*/',
    ),
  );
  
  $types['image'] = new stdClass();
  $types['image']->name = 'image';
  $types['image']->label = "Image";
  $types['image']->base = TRUE;
  $types['image']->type_callback_args = array(
    'match_type' => 'all',
    'mimetypes' => array(
      '/^image/',
    ),
    'extensions' => array(
      'jpg',
      'jpeg',
      'gif',
      'png',
      'tiff',
    ),
    'streams' => array(
      'public',
      'private',
    ),
  );
  $types['audio'] = new stdClass();
  $types['audio']->name = 'audio';
  $types['audio']->label = "Audio";
  $types['audio']->base = TRUE;
  $types['audio']->type_callback_args = array(
    'match_type' => 'all',
    'mimetypes' => array(
      '/^audio/',
    ),
    'extensions' => array(
      'mp3',
      'ogg',
      'wma',
    ),
    'streams' => array(
      'public',
      'private',
    ),
  );
  $types['video'] = new stdClass();
  $types['video']->name = 'video';
  $types['video']->label = "Video";
  $types['video']->base = TRUE;
  $types['video']->type_callback_args = array(
    'match_type' => 'all',
    'mimetypes' => array(
      '/^video/',
    ),
    'extensions' => array(
      'mov',
      'mp4',
      'avi',
    ),
    'streams' => array(
      'public',
      'private',
    ),
  );
  
  foreach ($types as $name => $type) {
    media_type_save($type);
    
    $bundle_settings = field_bundle_settings('file', $name);
    $bundle_settings['extra_fields']['display']['file']['media_small'] = array(
      'weight' => 0,
      'visible' => FALSE,
    );
    field_bundle_settings('file', $name, $bundle_settings);
  }
  
  $roles = user_roles();
  foreach ($roles as $rid => $role) {
    user_role_grant_permissions($rid, array(
      'view media',
    ));
  }
  
  $invalid_files = media_type_invalid_files_count();
  if ($invalid_files <= MEDIA_UPDATE_RECORDS_ON_INSTALL) {
    media_type_batch_update(FALSE, MEDIA_UPDATE_RECORDS_ON_INSTALL);
  }
  $invalid_files = media_type_invalid_files_count();
  if ($invalid_files > 0) {
    
    media_variable_set('show_file_type_rebuild_nag', TRUE);
  }
}
function media_uninstall() {
  drupal_load('module', 'media');
  foreach (media_variable_default() as $name => $value) {
    media_variable_del($name);
  }
}
function media_update_dependencies() {
  
  $dependencies['mymodule'][7016] = array(
    'field' => 7002,
  );
  return $dependencies;
}
function media_update_7000() {
  $schema['media_list_type'] = array(
    'description' => 'Stores the user preference for whether to list as table or images.',
    'fields' => array(
      'uid' => array(
        'description' => 'The {user}.uid of the user.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'type' => array(
        'description' => 'The type of display (table or images).',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array(
      'uid',
    ),
  );
  db_create_table('media_list_type', $schema['media_list_type']);
}
function media_update_7001() {
  $schema['cache_media_xml'] = drupal_get_schema_unprocessed('system', 'cache');
  $schema['cache_media_xml']['description'] = 'Cache table for the the results of retreived XML documents for remote streams.';
  db_create_table('cache_media_xml', $schema['cache_media_xml']);
}
function media_update_7002() {
  if (db_table_exists('media_type')) {
    return;
  }
  $schema['media_type'] = array(
    'description' => 'Stores the settings for media types.',
    'fields' => array(
      'name' => array(
        'description' => 'The machine name of the media type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'label' => array(
        'description' => 'The label of the media type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'base' => array(
        'description' => 'If this is a base type (i.e. cannot be deleted)',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'tiny',
      ),
      'weight' => array(
        'description' => 'Weight of media type. Determines which one wins when claiming a piece of media (first wins)',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
        'size' => 'normal',
      ),
      'type_callback' => array(
        'description' => 'Callback to determine if provided media is of this type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'type_callback_args' => array(
        'type' => 'text',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
        'description' => 'A serialized array of name value pairs that will be passed to the callback function',
      ),
    ),
    'primary key' => array(
      'name',
    ),
  );
  db_create_table('media_type', $schema['media_type']);
  drupal_load('module', 'media');
  $old_types = variable_get('media_types', array());
  foreach ($old_types as $type) {
    
    if (isset($type->callbacks)) {
      unset($type->callbacks);
    }
    $type->name = $type->machine_name;
    unset($type->machine_name);
    media_type_save($type);
  }
  variable_del('media_types');
}
function media_update_7003() {
  drupal_load('module', 'media');
  foreach (media_variable_default() as $variable => $value) {
    if (($test = variable_get('media_' . $variable, TRUE)) == variable_get('media_' . $variable, FALSE)) {
      media_variable_set($variable, $test);
      variable_del('media_' . $variable);
    }
  }
}
function media_update_7004() {
  
}
function media_update_7005() {
}
function media_update_7006() {
  if (db_table_exists('file') && !db_table_exists('file_managed')) {
    db_rename_table('file', 'file_managed');
  }
}
function media_update_7007() {
  
  drupal_load('module', 'media');
  drupal_load('module', 'field');
  foreach (media_type_get_types() as $type => $info) {
    if ($type != 'image') {
      media_type_configure_formatters($type, array(
        'media_preview' => 'media_large_icon',
      ));
    }
  }
}
function media_update_7008() {
  $roles = user_roles();
  foreach ($roles as $rid => $role) {
    user_role_grant_permissions($rid, array(
      'view media',
    ));
  }
}
function media_update_7009() {
  
  drupal_load('module', 'media');
  drupal_load('module', 'field');
  media_type_configure_formatters('video', array(
    'media_preview' => 'styles_file_square_thumbnail',
  ));
}
function media_update_7010() {
  
  drupal_load('module', 'media');
  drupal_load('module', 'field');
  media_type_configure_formatters('video', array(
    'media_large' => 'styles_file_large',
  ));
}
function media_update_7011() {
}
function media_update_7012() {
  $schema['media_filter_usage'] = array(
    'description' => 'Stores fids that have been included in the media tag in formatted textareas.',
    'fields' => array(
      'fid' => array(
        'description' => 'The media {file_managed}.fid.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'timestamp' => array(
        'description' => 'The timestamp the fid was last recorded by media_filter()',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'foreign keys' => array(
      'file_managed' => array(
        'table' => 'file_managed',
        'columns' => array(
          'fid' => 'fid',
        ),
      ),
    ),
    'primary key' => array(
      'fid',
    ),
    'indexes' => array(
      'timestamp' => array(
        'timestamp',
      ),
    ),
  );
  db_create_table('media_filter_usage', $schema['media_filter_usage']);
}
function media_update_7013() {
  $formats = filter_formats();
  foreach ($formats as $format) {
    $format->filters = filter_list_format($format->format);
    
    foreach ($format->filters as $key => $value) {
      $format->filters[$key] = (array) $value;
    }
    filter_format_save($format);
  }
}
function media_update_7014() {
  if ($old_value = variable_get('media__dialog_get_theme_name')) {
    variable_del('media__dialog_get_theme_name');
    variable_set('media__dialog_theme', $old_value);
  }
}
function media_update_7015() {
}
function media_update_7016() {
  
  if (db_index_exists('file_managed', 'file_type')) {
    db_drop_index('file_managed', 'file_type');
  }
  module_enable(array(
    'file_entity',
  ));
  
  $instances = field_read_instances(array(
    'entity_type' => 'media',
  ), array(
    'include_inactive' => TRUE,
    'include_deleted' => TRUE,
  ));
  foreach ($instances as $instance) {
    
    if ($instance['field_name'] === 'file') {
      continue;
    }
    
    $fields = field_read_fields(array(
      'id' => $instance['field_id'],
    ), array(
      'include_inactive' => TRUE,
      'include_deleted' => TRUE,
    ));
    $field = $fields[$instance['field_id']];
    
    if ($field['storage']['type'] !== 'field_sql_storage' || !module_exists('field_sql_storage') || !$field['storage']['active']) {
      $messages[] = t('Cannot update field %id (%field_name) because it does not use the field_sql_storage storage type.', array(
        '%id' => $field['id'],
        '%field_name' => $field['field_name'],
      ));
      continue;
    }
    
    $table_name = _field_sql_storage_tablename($field);
    $revision_name = _field_sql_storage_revision_tablename($field);
    db_update($table_name)
      ->fields(array(
      'entity_type' => 'file',
    ))
      ->condition('entity_type', 'media')
      ->condition('bundle', $instance['bundle'])
      ->execute();
    db_update($revision_name)
      ->fields(array(
      'entity_type' => 'file',
    ))
      ->condition('entity_type', 'media')
      ->condition('bundle', $instance['bundle'])
      ->execute();
    
    db_update('field_config_instance')
      ->fields(array(
      'entity_type' => 'file',
    ))
      ->condition('id', $instance['id'])
      ->execute();
  }
  
  foreach ($instances as $instance) {
    if ($instance['field_name'] === 'file' && !$instance['deleted']) {
      $file_settings = field_bundle_settings('file', $instance['bundle']);
      $media_settings = field_bundle_settings('media', $instance['bundle']);
      $file_settings = array_merge($file_settings, $media_settings);
      if (isset($instance['widget']['weight'])) {
        $file_settings['extra_fields']['form']['file']['weight'] = $instance['widget']['weight'];
      }
      if (isset($instance['display'])) {
        foreach ($instance['display'] as $view_mode => $display) {
          if (isset($display['weight'])) {
            $file_settings['extra_fields']['display']['file'][$view_mode]['weight'] = $display['weight'];
          }
          if (isset($display['type'])) {
            $file_settings['extra_fields']['display']['file'][$view_mode]['visible'] = $display['type'] != 'hidden';
          }
        }
      }
      field_bundle_settings('file', $instance['bundle'], $file_settings);
    }
  }
  
  db_delete('variable')
    ->condition('name', db_like('field_bundle_settings_media__') . '%', 'LIKE')
    ->execute();
  
  $file_displays = variable_get('file_displays', array());
  foreach ($instances as $instance) {
    if ($instance['field_name'] === 'file' && !$instance['deleted']) {
      if (isset($instance['display'])) {
        foreach ($instance['display'] as $view_mode => $display) {
          if (isset($display['type']) && $display['type'] != 'hidden') {
            $file_formatter = 'file_field_' . $display['type'];
            $file_displays[$instance['bundle']][$view_mode][$file_formatter]['status'] = TRUE;
            if (isset($display['settings'])) {
              $file_displays[$instance['bundle']][$view_mode][$file_formatter]['settings'] = $display['settings'];
            }
          }
        }
      }
    }
  }
  variable_set('file_displays', $file_displays);
  
  foreach ($instances as $instance) {
    if ($instance['field_name'] === 'file' && !$instance['deleted']) {
      field_delete_instance($instance);
    }
  }
  field_cache_clear();
}
function media_update_7017() {
  
  if (db_table_exists('file_display')) {
    module_load_include('install', 'file_entity', 'file_entity');
    file_entity_update_7001();
  }
}
function media_update_7018() {
}
function media_update_7019() {
  $instances = array();
  $fields = field_read_fields(array(
    'type' => 'media',
  ), array(
    'include_inactive' => TRUE,
  ));
  foreach ($fields as $field) {
    $instances = array_merge($instances, field_read_instances(array(
      'field_id' => $field['id'],
    ), array(
      'include_inactive' => TRUE,
    )));
  }
  foreach ($instances as $instance) {
    $update_instance = FALSE;
    foreach ($instance['display'] as $view_mode => $display) {
      if (in_array($display['type'], array(
        'media_link',
        'media_preview',
        'media_small',
        'media_large',
        'media_original',
      ))) {
        $update_instance = TRUE;
        $instance['display'][$view_mode]['type'] = 'media';
        $instance['display'][$view_mode]['settings'] = array(
          'file_view_mode' => $display['type'],
        );
      }
    }
    if ($update_instance) {
      field_update_instance($instance);
    }
  }
}
function media_update_7020() {
  if (variable_get('media__wysiwyg_allowed_types') == array(
    'image',
    'video',
  )) {
    variable_del('media__wysiwyg_allowed_types');
  }
}
function media_update_7021() {
  media_update_7002();
}