You are here

function _emfield_update_fix_data in Embedded Media Field 6.2

Same name and namespace in other branches
  1. 6.3 emfield.install \_emfield_update_fix_data()
  2. 6 emfield.install \_emfield_update_fix_data()

Batch function to retrieve the most recent data from providers.

Parameters

$field: The field definition.

$providers: An optional array of providers to check.

$old_data_keys: An optional array of the original version key in the data, keyed by provider.

&$context: The context for the batch operations.

1 string reference to '_emfield_update_fix_data'
emfield_update_6005 in ./emfield.install
Add a provider data version column to existing fields.

File

./emfield.install, line 317
This is the emfield.module's install, configuration, and removal file.

Code

function _emfield_update_fix_data($field, $providers = array(), $old_data_keys = array(), &$context) {

  // Setup the sandbox the first time through.
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['field'] = $field;
    $db_info = content_database_info($field);
    $context['sandbox']['db_info'] = $db_info;
    $context['sandbox']['table'] = $db_info['table'];
    $context['sandbox']['col_embed'] = $db_info['columns']['embed']['column'];
    $context['sandbox']['col_value'] = $db_info['columns']['value']['column'];
    $context['sandbox']['col_provider'] = $db_info['columns']['provider']['column'];
    $context['sandbox']['col_data'] = $db_info['columns']['data']['column'];
    $context['sandbox']['col_version'] = $db_info['columns']['version']['column'];
    $context['sandbox']['module'] = $field['module'];
    $context['sandbox']['providers'] = $providers;
    $context['sandbox']['old_data_key'] = $old_data_keys;
    if (empty($context['sandbox']['providers'])) {
      $context['sandbox']['max'] = db_result(db_query("SELECT COUNT(*) FROM {" . $db_info['table'] . "}"));
    }
    else {
      $context['sandbox']['provider_placeholders'] = db_placeholders($context['sandbox']['providers'], 'varchar');
      $context['sandbox']['max'] = db_result(db_query("SELECT COUNT(*) FROM {" . $db_info['table'] . "} WHERE " . $context['sandbox']['col_provider'] . " in (" . $context['sandbox']['provider_placeholders'] . ")", $context['sandbox']['providers']));
    }
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_node'] = 0;
  }

  // Work our way through the field values 50 rows at a time.
  // Note that if PHP times out here, you can set the
  // emfield_install_fix_data_rows variable in settings.php to a lower number.
  // If you get this error, please report it so we can find a better limit
  // to work with; I'm not sure if this value will work in the majority of
  // cases. Thanks, Aaron.
  $limit = variable_get('emfield_install_fix_data_rows', 50);
  if (empty($context['sandbox']['providers'])) {
    $result = db_query_range("SELECT * FROM {{$context['sandbox']['table']}} WHERE vid > %d ORDER BY vid ASC", $context['sandbox']['current_node'], 0, $limit);
  }
  else {
    $result = db_query_range("SELECT * FROM {{$context['sandbox']['table']}} WHERE vid > %d AND " . $context['sandbox']['col_provider'] . " in (" . $context['sandbox']['provider_placeholders'] . ") ORDER BY vid ASC", $context['sandbox']['current_node'], implode(', ', $context['sandbox']['providers']), 0, $limit);
  }
  while ($row = db_fetch_array($result)) {

    // Fetch the duration from the provider.
    $item = array(
      'embed' => $row[$context['sandbox']['col_embed']],
      'value' => $row[$context['sandbox']['col_value']],
      'provider' => $row[$context['sandbox']['col_provider']],
      'data' => unserialize($row[$context['sandbox']['col_data']]),
      'version' => $row[$context['sandbox']['col_version']],
    );
    if ($item['provider'] && !$item['version']) {
      $version = emfield_include_invoke($context['sandbox']['module'], $item['provider'], 'data_version', $item);
      $data_version = isset($item['data']['old_data_key'][$item['provider']]) ? $item['data']['old_data_key'][$item['provider']] : 0;
      if ($version && $version == $data_version) {
        db_query("UPDATE {{$context['sandbox']['table']}} SET {$context['sandbox']['col_version']} = %d WHERE vid = %d", $version, $row['vid']);
      }
      else {
        if ($version) {
          $item['data'] = emfield_include_invoke($context['sandbox']['module'], $item['provider'], 'data', $context['sandbox']['field'], $item);
          $version = emfield_include_invoke($context['sandbox']['module'], $item['provider'], 'data_version', $item);
          db_query("UPDATE {{$context['sandbox']['table']}} SET {$context['sandbox']['col_version']} = %d WHERE vid = %d", $version, $row['vid']);
          db_query("UPDATE {{$context['sandbox']['table']}} SET {$context['sandbox']['col_data']} = '%s' WHERE vid = %d", serialize($item['data']), $row['vid']);
        }
      }
    }

    // Update our progress information.
    $context['sandbox']['progress']++;
    $context['sandbox']['current_node'] = $row['vid'];
  }

  // Inform the batch engine that we are not finished,
  // and provide an estimation of the completion level we reached.
  if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
  else {
    $context['finished'] = 1;
  }
}