You are here

emfield.install in Embedded Media Field 6

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

File

emfield.install
View source
<?php

/**
 * @file
 * This is the emfield.module's install, configuration, and removal file.
 */

/**
 * Implementation of hook_install().
 */
function emfield_install() {
}

/**
 * Implementation of hook_uninstall().
 */
function emfield_uninstall() {
}

/**
 * Implementation of hook_enable().
 */
function emfield_enable() {
}

/**
 * Implementation of hook_disable().
 */
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;
}

/**
 *  This caused an infinite loop in some cases. Redo in 6005.
 */
function emfield_update_6003() {
  return array();
}

/**
 *  Rebuild themes.
 */
function emfield_update_6004() {
  drupal_rebuild_theme_registry();
  return array();
}

/**
 *  Add a provider data version column to existing fields.
 */
function emfield_update_6005() {
  $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.
  // 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;
  }
}

Functions

Namesort descending Description
emfield_disable Implementation of hook_disable().
emfield_enable Implementation of hook_enable().
emfield_install Implementation of hook_install().
emfield_uninstall Implementation of hook_uninstall().
emfield_update_6001 Botched this the first time. Redo in 6003.
emfield_update_6002 Rebuild themes and views.
emfield_update_6003 This caused an infinite loop in some cases. Redo in 6005.
emfield_update_6004 Rebuild themes.
emfield_update_6005 Add a provider data version column to existing fields.
_emfield_update_fix_data Batch function to retrieve the most recent data from providers.