You are here

function file_entity_update_7212 in File Entity (fieldable files) 7.3

Same name and namespace in other branches
  1. 7.2 file_entity.install \file_entity_update_7212()

Migrate the image_dimensions table to the new file_metadata table.

File

./file_entity.install, line 981
Install, update and uninstall functions for the file_entity module.

Code

function file_entity_update_7212(&$sandbox) {
  if (!db_table_exists('image_dimensions')) {
    return;
  }
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['current_fid'] = 0;
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT fid) FROM {image_dimensions}')
      ->fetchField();
  }
  $results = db_query_range("SELECT fid, width, height FROM {image_dimensions} WHERE fid > :fid ORDER BY fid ASC", 0, 20, array(
    ':fid' => $sandbox['current_fid'],
  ))
    ->fetchAllAssoc('fid');

  // Clear any existing records in the metadata table in case they exist
  // because we only want to do one insert.
  if (!empty($results)) {
    db_delete('file_metadata')
      ->condition('fid', array_keys($results), 'IN')
      ->condition(db_or()
      ->condition('name', 'width')
      ->condition('name', 'height'))
      ->execute();
  }
  $values = array();
  foreach ($results as $result) {
    foreach (array(
      'width',
      'height',
    ) as $key) {
      $values[] = array(
        'fid' => $result->fid,
        'name' => $key,
        'value' => serialize((int) $result->{$key}),
      );
    }
    $sandbox['current_fid'] = $result->fid;
  }
  $sandbox['progress'] += count($results);
  if (!empty($values)) {
    $query = db_insert('file_metadata');
    $query
      ->fields(array(
      'fid',
      'name',
      'value',
    ));
    foreach ($values as $value) {
      $query
        ->values($value);
    }
    $query
      ->execute();
  }
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  if ($sandbox['#finished'] >= 1) {
    db_drop_table('image_dimensions');
  }
}