You are here

function _media_youtube_update_fetch_metadata in Media: YouTube 6

Batch function to retrieve the most recent data from providers.

Parameters

$field: The field definition.

&$context: The context for the batch operations.

1 string reference to '_media_youtube_update_fetch_metadata'
media_youtube_update_6012 in ./media_youtube.install
Rebuild youtube data to account for not saving raw by default.

File

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

Code

function _media_youtube_update_fetch_metadata($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'])) {
    $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;
  }

  // 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)) {

    // Set delta to zero if it's not set.
    if (!isset($row['delta'])) {
      $row['delta'] = 0;
    }

    // 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']],
    );
    $item['data'] = is_array($item['data']) ? $item['data'] : array();
    if ($item['provider'] == 'youtube') {
      if (!db_result(db_query("SELECT value FROM {media_youtube_metadata} WHERE value = '%s'", $item['value']))) {
        $item['status'] = media_youtube_check_status($item['value']);
        $item['last_touched'] = time();
        drupal_write_record('media_youtube_metadata', $item);
      }
      if (!db_result(db_query("SELECT value FROM {media_youtube_node_data} WHERE value = '%s' AND vid = %d AND field_name = '%s' AND delta = %d", $item['value'], $row['vid'], $context['sandbox']['field']['field_name'], $row['delta']))) {
        $item['field_name'] = $context['sandbox']['field']['field_name'];
        $item['delta'] = $row['delta'];
        drupal_write_record('media_youtube_node_data', $item);
      }
      if ($item['version'] != MEDIA_YOUTUBE_DATA_VERSION) {
        unset($item['data']['raw']);

        // Ensure we keep any emthumb data.
        $item[$context['sandbox']['col_data']] = serialize(array_merge($item['data'], media_youtube_emfield_data($item['value'])));
        $item[$context['sandbox']['col_version']] = MEDIA_YOUTUBE_DATA_VERSION;
        drupal_write_record($context['sandbox']['table'], $item, array(
          'vid',
        ));
      }
    }

    // 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;
  }
}