You are here

emfield.install in Embedded Media Field 6.2

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() {
  $success = drupal_install_schema('emfield');
  if ($success) {
    drupal_set_message(st('The Embedded Media Field module installed tables successfully.'));
  }
  else {
    drupal_set_message(st('The installation of Embedded Media Field module failed.'), 'error');
  }
}

/**
 * Implementation of hook_uninstall().
 */
function emfield_uninstall() {
  drupal_uninstall_schema('emfield');

  // Remove all variables that live in our namespace.
  $result = db_query("SELECT name FROM {variable} WHERE name LIKE 'emfield_%'");
  while ($row = db_fetch_object($result)) {
    variable_del($row->name);
  }
  drupal_set_message(t('Embedded Media Field has been uninstalled.'));
}

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

/**
 * Implementation of hook_disable().
 */
function emfield_disable() {
  cache_clear_all('*', 'cache_emfield_xml', TRUE);
}
function emfield_requirements($phase) {
  $requirements = array();

  // Ensure translations don't break at install time
  $t = get_t();

  // Check the existance of third party media providers.
  if ($phase == 'runtime') {
    $providers = emfield_audit();
    $emvideo_list = module_exists('emvideo') && emfield_system_list('emvideo');
    $emimage_list = module_exists('emimage') && emfield_system_list('emimage');
    $emaudio_list = module_exists('emaudio') && emfield_system_list('emaudio');
    if (empty($providers) && empty($emvideo_list) && empty($emimage_list) && empty($emaudio_list)) {
      $value = t('No providers');
      $description = t('You have no third party media providers enabled. You will need to install additional modules for these; see the !emfield module page for a listing of available modules.', array(
        '!emfield' => l(t('Embedded Media Field'), 'http://drupal.org/project/emfield', array(
          'attributes' => array(
            'target' => '_blank',
          ),
        )),
      ));
      $severity = REQUIREMENT_WARNING;
    }
    else {
      if (empty($providers)) {
        $value = t('Providers enabled');
        $description = t('All media fields are properly associated with third party media providers. You may still wish to install additional modules; see the !emfield module page for a listing of available modules.', array(
          '!emfield' => l(t('Embedded Media Field'), 'http://drupal.org/project/emfield', array(
            'attributes' => array(
              'target' => '_blank',
            ),
          )),
        ));
        $severity = REQUIREMENT_OK;
      }
      else {
        $value = t('Missing providers');
        $description = t('The following third party media providers found in existing media fields are currently missing from the system: %providers. You must install the corresponding modules supporting the provider(s) to support them. See the !emfield module page for a listing of available modules.', array(
          '%providers' => implode(', ', $providers),
          '!emfield' => l(t('Embedded Media Field'), 'http://drupal.org/project/emfield', array(
            'attributes' => array(
              'target' => '_blank',
            ),
          )),
        ));
        $items = array();
        foreach ($providers as $provider) {
          $items[] = l($provider, 'http://drupal.org/project/media_' . $provider, array(
            'attributes' => array(
              'target' => '_blank',
            ),
          ));
        }
        $description .= t('<p>You may be able to locate the missing provider(s) at one or more of the following project pages. Note that these are educated guesses, and may actually not be an existing Drupal project:</p>!list', array(
          '!list' => theme('item_list', $items),
        ));
        $description .= t('<p>Finally, you might be able to locate the missing provider(s) at one of the %flotsam project pages, such as !video, !image, or !audio. Note that these three are placeholder projects, seriously in need of proper homes. Please contribute if you are able!</p>', array(
          '%flotsam' => t('Media: Flotsam'),
          '!video' => l(t('Media: Video Flotsam'), 'http://drupal.org/project/media_video_flotsam', array(
            'attributes' => array(
              'target' => '_blank',
            ),
          )),
          '!image' => l(t('Media: Image Flotsam'), 'http://drupal.org/project/media_image_flotsam', array(
            'attributes' => array(
              'target' => '_blank',
            ),
          )),
          '!audio' => l(t('Media: Audio Flotsam'), 'http://drupal.org/project/media_audio_flotsam', array(
            'attributes' => array(
              'target' => '_blank',
            ),
          )),
        ));
        $severity = REQUIREMENT_ERROR;
      }
    }
    $requirements['emfield'] = array(
      'title' => $t('Embedded Media Field'),
      'value' => $value,
      'description' => $description,
      'severity' => $severity,
    );
  }
  return $requirements;
}

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

/**
 * Add new menu item for removing messages for obsolete providers.
 */
function emfield_update_6006() {
  menu_rebuild();
  return array();
}

/**
 * Build a table to cache XML results.
 */
function emfield_update_6200() {
  $schema = array();
  $schema['cache_emfield_xml'] = drupal_get_schema_unprocessed('system', 'cache');
  $schema['cache_emfield_xml']['description'] = 'Cache table used to store third party XML files for Embedded Media Field.';
  $ret = array();
  db_create_table($ret, 'cache_emfield_xml', $schema['cache_emfield_xml']);
  return $ret;
}

/**
 * Rebuild menu for new admin settings pages.
 */
function emfield_update_6201() {
  menu_rebuild();
  return array();
}

/**
 * Add a new column to fields for media status.
 */
function emfield_update_6202() {
  $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;
      }
    }
  }

  // Update database storage.
  foreach ($fields as $field) {
    $new_field = $field;
    unset($field['version']);
    content_alter_db($field, $new_field);
    $ret[] = array(
      'query' => t('Added media availability status 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');
  return $ret;
}

/**
 * Rebuild themes and views.
 */
function emfield_update_6203() {
  drupal_rebuild_theme_registry();
  $ret = array();
  if (module_exists('views')) {
    $ret[] = update_sql("DELETE FROM {cache_views}");
  }
  return $ret;
}

/**
 * Give a warning regarding obsolete providers.
 */
function emfield_update_6204() {
  $warning = t('Embedded Media Field no longer offers default third party media providers. If you have not already done so, you will most likely need to visit the !project project page and install the module(s) supporting the provider(s) you wish to include.', array(
    '!project' => l(t('Embedded Media Field'), 'http://drupal.org/project/emfield', array(
      'attributes' => array(
        'target' => '_blank',
      ),
    )),
  ));
  drupal_set_message($warning, 'warning');
  return array(
    array(
      'query' => $warning,
      'success' => TRUE,
    ),
  );
}

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

/**
 * Implementation of hook_schema().
 */
function emfield_schema() {
  $schema = array();
  $schema['cache_emfield_xml'] = drupal_get_schema_unprocessed('system', 'cache');
  $schema['cache_emfield_xml']['description'] = 'Cache table used to store third party XML files for Embedded Media Field.';
  return $schema;
}

Functions

Namesort descending Description
emfield_disable Implementation of hook_disable().
emfield_enable Implementation of hook_enable().
emfield_install Implementation of hook_install().
emfield_requirements
emfield_schema Implementation of hook_schema().
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_6006 Add new menu item for removing messages for obsolete providers.
emfield_update_6200 Build a table to cache XML results.
emfield_update_6201 Rebuild menu for new admin settings pages.
emfield_update_6202 Add a new column to fields for media status.
emfield_update_6203 Rebuild themes and views.
emfield_update_6204 Give a warning regarding obsolete providers.
_emfield_update_fix_data Batch function to retrieve the most recent data from providers.