You are here

function _media_youtube_update_retrieve_missing_thumbs in Media: YouTube 6

Batch function to restore possibly missing thumbnails from a bad update.

Parameters

$field: The field definition.

&$context: The context for the batch operations.

1 string reference to '_media_youtube_update_retrieve_missing_thumbs'
media_youtube_update_6014 in ./media_youtube.install
Self-correct for missing thumbnails from a previous mistake.

File

./media_youtube.install, line 406
This is Media: YouTube's installation, configuration, and removal file.

Code

function _media_youtube_update_retrieve_missing_thumbs($field, &$context) {

  // Autoload the Zend_Loader class.
  spl_autoload_register('media_youtube_autoload');

  // Setup the sandbox the first time through.
  if (!isset($context['sandbox']['progress'])) {

    // Don't bother if this field doesn't store local thumbnails.
    if (!$field['widget']['emthumb_store_local_thumbnail']) {
      $context['finished'] = 1;
      return;
    }
    $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']['max'] = db_result(db_query("SELECT COUNT(*) FROM {" . $db_info['table'] . "}"));
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_vid'] = 0;
    $context['sandbox']['nids'] = array();
  }

  // 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);
  $result = db_query_range("SELECT * FROM {{$context['sandbox']['table']}} WHERE vid > %d ORDER BY vid ASC", $context['sandbox']['current_vid'], 0, $limit);
  while ($row = db_fetch_array($result)) {

    // Fetch the duration from the provider.
    $item = array(
      'vid' => $row['vid'],
      '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'] == 'youtube' && !isset($item['data']['emthumb']) && !isset($context['sandbox']['nids'][$row['nid']])) {
      emfield_operations_reload(array(
        $row['nid'],
      ), FALSE);

      // Only save the node once per field iteration, even if it has more than
      // one thumbnail missing, as the above function is a sledgehammer.
      $context['sandbox']['nids'][$row['nid']] = TRUE;
    }

    // Update our progress information.
    $context['sandbox']['progress']++;
    $context['sandbox']['current_vid'] = $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;
  }
}