1.20.x file.install file_update_1003()

Add a type column to the file_managed table, and add a file_metadata table.

File

modules/file/file.install, line 769
Install, update and uninstall functions for File module.

Code

function file_update_1003() {
  $schema = array();
  $schema['file_metadata'] = array(
    'description' => 'Cache images dimensions.',
    'fields' => array(
      'fid' => array(
        'description' => 'The {file_managed}.fid of the metadata.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'name' => array(
        'description' => "The name of the metadata (e.g. 'width').",
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
      ),
      'value' => array(
        'description' => "The value of the metadata (e.g. '200px').",
        'type' => 'blob',
        'not null' => FALSE,
        'size' => 'big',
        'serialize' => TRUE,
      ),
    ),
    'primary key' => array('fid', 'name'),
    'foreign keys' => array(
      'file_managed' => array(
        'table' => 'file_managed',
        'columns' => array('fid' => 'fid'),
      ),
    ),
  );
  $spec = array(
    'description' => 'The type of this file.',
    'type' => 'varchar',
    'length' => 50,
    'not null' => TRUE,
    'default' => 'undefined',
  );
  $indexes_new = array(
    'indexes' => array(
      'file_type' => array('type'),
    ),
  );

  // If another module (e.g., Media or File Entity) had added a
  // {file_managed}.type field, then change it to the expected specification.
  if (db_field_exists('file_managed', 'type')) {
    // db_change_field() will fail if any records have type=NULL, so update
    // them to the new default value.
    db_update('file_managed')->fields(array('type' => 'undefined'))->isNull('type')->execute();

    // Indexes using a field being changed must be dropped prior to calling
    // db_change_field().
    if (db_index_exists('file_managed', 'file_type')) {
      db_drop_index('file_managed', 'file_type');
    }

    // Since the database API doesn't provide a way to drop indexes without
    // knowing what the old indexes are, it is the responsibility of any module
    // that adds them to drop them prior to running this update..
    db_change_field('file_managed', 'type', 'type', $spec, $indexes_new);
  }

  // Otherwise, add the field.
  else {
    db_add_field('file_managed', 'type', $spec, $indexes_new);
  }

  // Create the {file_metadata} database table.
  if (!db_table_exists('file_metadata')) {
    db_create_table('file_metadata', $schema['file_metadata']);
  }

  // Set permission to view files.
  $roles = user_roles();
  foreach ($roles as $rid => $role) {
    user_role_grant_permissions($rid, array('view files'));
  }

  // Classify existing files according to the currently defined file types.
  // Queue all files to be classified during cron runs using the Queue API.
  $queue = BackdropQueue::get('file_type_determine');
  $result = db_query('SELECT fid FROM {file_managed}');
  foreach ($result as $record) {
    $queue->createItem($record->fid);
  }

  // Warn users that existing files will not have a file type until the queue
  // has been processed.
  if ($queue->numberOfItems()) {
    backdrop_set_message(t('All files must now be classified according to file types. Your files have been queued for processing, and will have their file type determined during cron runs.'));
  }
}