Index: emfield.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/emfield/emfield.install,v
retrieving revision 1.1.6.8
diff -u -p -r1.1.6.8 emfield.install
--- emfield.install	25 Jul 2008 11:45:51 -0000	1.1.6.8
+++ emfield.install	30 Sep 2009 21:21:50 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: emfield.install,v 1.1.6.8 2008/07/25 11:45:51 alexua Exp $
+// $Id: emfield.install,v 1.1.6.10 2009/09/30 15:11:01 aaron Exp $
 
 /**
  * @file
@@ -29,3 +29,190 @@ function emfield_enable() {
  */
 function emfield_disable() {
 }
+
+/**
+ *  Botched this the first time. Redo in 6003.
+ */
+function emfield_update_6001() {
+  $ret = array();
+  return $ret;
+}
+
+/**
+ *  Rebuild themes and views.
+ */
+function emfield_update_6002() {
+  drupal_rebuild_theme_registry();
+
+  $ret = array();
+
+  if (module_exists('views')) {
+    $ret[] = update_sql("DELETE FROM {cache_views}");
+  }
+
+  return $ret;
+}
+
+/**
+ *  Add a provider data version column to existing fields.
+ */
+function emfield_update_6003() {
+  $ret = array();
+
+  include_once(drupal_get_path('module', 'content') .'/includes/content.admin.inc');
+
+  // Build a list of fields that need data updating.
+  $fields = array();
+  foreach (content_types_install() as $type_name => $type_fields) {
+    foreach ($type_fields as $field) {
+      if (in_array($field['module'], array('emvideo', 'emimage', 'emaudio'))) {
+        // We only process a given field once.
+        $fields[$field['field_name']] = $field;
+        $ret[] = array(
+          'query' => t('Added provider data version to the %field field.', array('%field' => $field['field_name'])),
+          'success' => TRUE,
+        );
+      }
+    }
+  }
+
+  // Update database storage.
+  foreach ($fields as $field) {
+    $new_field = $field;
+
+    unset($field['version']);
+    content_alter_db($field, $new_field);
+
+    $ret[] = array(
+      'query' => t('Add duration to the %field field.', array('%field' => $field['field_name'])),
+      'success' => TRUE,
+    );
+  }
+
+  content_clear_type_cache(TRUE);
+  content_associate_fields('emvideo');
+  content_associate_fields('emaudio');
+  content_associate_fields('emimage');
+
+  // Build a batch that grabs the duration for each video field.
+  $batch = array(
+    'title' => t('Importing provider data.'),
+    'operations' => array(),
+    'file' => drupal_get_path('module', 'emvideo') .'/emfield.install',
+  );
+
+  $old_data_keys = array(
+    'archive' => 'emvideo_archive_version',
+    'bliptv' => 'emvideo_bliptv_data_version',
+    'google' => 'emvideo_data_version',
+    'myspace' => 'emvideo_myspace_version',
+    'ustream' => 'ustream_api_version',
+    'ustreamlive' => 'ustreamlive_api_version',
+    'yahoomusic' => 'emvideo_yahoomusic_version',
+    'youtube' => 'emvideo_youtube_version',
+    '8tracks' => 'emaudio_8tracks',
+    'brightcove3' => 'emvideo_brightcove3_version',
+    'flickr_sets' => 'emvideo_data_version',
+    'hulu' => 'emvideo_hulu_version',
+  );
+
+  foreach ($fields as $field_name => $field) {
+    $batch['operations'][] = array('_emfield_update_fix_data', array($field, array(), $old_data_keys));
+    $ret[] = array(
+      'query' => t('Retrieved current provider data for the %field field.', array('%field' => $field['field_name'])),
+      'success' => TRUE,
+    );
+  }
+  batch_set($batch);
+
+  // Clear caches.
+  cache_clear_all('*', content_cache_tablename(), TRUE);
+  cache_clear_all('*', 'cache', TRUE);
+
+  return $ret;
+}
+
+/**
+ *  Batch function to retrieve the most recent data from providers.
+ *
+ *  @param $field
+ *    The field definition.
+ *  @param $providers
+ *    An optional array of providers to check.
+ *  @param $old_data_keys
+ *    An optional array of the original version key in the data,
+ *    keyed by provider.
+ *  @param &$context
+ *    The context for the batch operations.
+ */
+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.
+  $limit = 50;
+  if (empty($context['sandbox']['providers'])) {
+    $result = db_query_range("SELECT * FROM {{$context['sandbox']['table']}} WHERE vid > %d ORDER BY nid 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 nid 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;
+  }
+}
