You are here

lingotek.install in Lingotek Translation 7.6

File

lingotek.install
View source
<?php

/**
 * @file
 * Installation for Lingotek Community Translation Module.
 */
require_once drupal_get_filename('module', 'lingotek');

/*
 * Implements hook_requirements() (see:
 */
function lingotek_requirements($phase) {
  $requirements = array();
  $t = get_t();
  $test = FALSE;

  // test requirement message appearance
  // Ensure that the PHP Zip library is loaded
  if ($test || !extension_loaded('zip')) {
    $requirements['zip'] = array(
      'title' => $t('Lingotek'),
      'value' => $t('PHP Zip support required.'),
      'description' => $t('The Lingotek Translation module requires the PHP module \'Zip\', but it is missing or not enabled on your server. This module allows PHP to work with ZIP compressed archives.  <br>For more information visit: <a href="http://php.net/manual/en/book.zip.php">http://php.net/manual/en/book.zip.php</a>'),
      'severity' => REQUIREMENT_ERROR,
    );
  }

  // Ensure that the PHP cURL support is enabled
  if ($test || !function_exists('curl_version')) {
    $requirements['curl'] = array(
      'title' => $t('Lingotek'),
      'value' => $t('PHP cURL support required'),
      'description' => $t('Lingotek requires the that PHP CURL be enabled on your server.  <br>For more information visit: <a href="http://www.php.net/manual/en/curl.installation.php">http://www.php.net/manual/en/curl.installation.php</a>'),
      'severity' => REQUIREMENT_ERROR,
    );
  }
  return $requirements;
}

/**
 * Implements hook_install().
 */
function lingotek_install() {
  spl_autoload_register('lingotek_auto_loader');
  lingotek_migration_1();
  lingotek_migration_2();
  $sandbox = array();
  lingotek_update_7401($sandbox);
}

/**
 * Implements hook_uninstall().
 */
function lingotek_uninstall() {
  global $conf;
  foreach (array_keys($conf) as $key) {
    if (strpos($key, 'lingotek_') === 0) {

      // It's important to use === instead of == with strpos()
      variable_del($key);
    }
  }
  db_drop_field('languages', 'lingotek_enabled');
  db_drop_field('languages', 'lingotek_locale');
  db_drop_field('locales_target', 'translation_agent_id');
  db_drop_table('lingotek');

  // leave this here until we move it to an update function
  db_drop_table('lingotek_config_metadata');
  db_drop_table('lingotek_entity_metadata');
  db_drop_table('lingotek_translation_agent');
}

/**
 * Implements hook_enable().
 */
function lingotek_enable() {
  lingotek_set_priority();
  lingotek_set_defaults();
}

/**
 * Implements hook_disable().
 */
function lingotek_disable() {
  variable_del('lingotek_setup_complete');

  // require setup again (after disabling the module)
}

/*
 * Implements hook_schema().
 */
function lingotek_schema() {
  $schema['lingotek_entity_metadata'] = array(
    'description' => 'Stores Lingotek-related metadata about Drupal entities.',
    'fields' => array(
      'entity_id' => array(
        'description' => 'The primary identifier for the entity.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'entity_type' => array(
        'description' => 'The entity type (node, comment, etc.).',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      'entity_key' => array(
        'description' => 'The ID for the Lingotek-associated value.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      'value' => array(
        'description' => 'Value for the specified key.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'type' => 'int',
        'description' => "created timestamp",
        'default' => 0,
        'not null' => TRUE,
      ),
      'modified' => array(
        'type' => 'int',
        'description' => "last modified timestamp",
        'default' => 0,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'entity_id',
      'entity_type',
      'entity_key',
    ),
    'indexes' => array(
      'idx_entity_id' => array(
        'entity_id',
      ),
      'idx_entity_key' => array(
        'entity_key',
      ),
      'idx_entity_type_and_key' => array(
        'entity_type',
        'entity_key',
      ),
      'idx_entity_key_and_value' => array(
        'entity_key',
        'value',
      ),
    ),
  );
  $schema['lingotek_config_metadata'] = array(
    'description' => 'Stores Lingotek-related metadata about Drupal translations from locales source and target tables.',
    'fields' => array(
      'id' => array(
        'description' => 'the segment ID assigned to the given range of strings.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'config_key' => array(
        'description' => 'The ID for the Lingotek-associated value.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'value' => array(
        'description' => 'Value for the specified key.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'type' => 'int',
        'description' => "created timestamp",
        'default' => 0,
        'not null' => TRUE,
      ),
      'modified' => array(
        'type' => 'int',
        'description' => "last modified timestamp",
        'default' => 0,
        'not null' => TRUE,
      ),
    ),
    'primary key' => array(
      'id',
      'config_key',
    ),
  );
  $schema['lingotek_translation_agent'] = array(
    'description' => 'An assignment of IDs to agents from which translations were added.',
    'fields' => array(
      'id' => array(
        'description' => 'the ID assigned to a given translation agent.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'name' => array(
        'description' => 'The name of a given tool for adding translations.',
        'type' => 'varchar',
        'length' => 63,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  $schema['lingotek_config_map'] = array(
    'description' => 'An assignment of lids from locales_source and locales_target to set_ids for translation by Lingotek.',
    'fields' => array(
      'lid' => array(
        'description' => 'The ID assigned by the i18n_string module.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'set_id' => array(
        'description' => 'The ID of the set assigned to the string by the Lingotek Translation module.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'current' => array(
        'description' => 'Tells whether the translation for the individual config item is dirty and needs to be updated or not.',
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array(
      'lid',
    ),
  );
  return $schema;
}

/*
 * Implements hook_schema_alter().
 */
function lingotek_schema_alter(&$schema) {

  // Add fields to existing schema.
  $schema['languages']['fields']['lingotek_enabled'] = array(
    'type' => 'int',
    'description' => "Lingotek enabled",
    'not null' => TRUE,
    'default' => 0,
  );
  $schema['languages']['fields']['lingotek_locale'] = array(
    'type' => 'varchar',
    'description' => "Locale mapping",
    'length' => 10,
    'not null' => TRUE,
    'default' => '',
  );
  $schema['locales_target']['fields']['translation_agent_id'] = array(
    'type' => 'int',
    'description' => "translation tool mapping",
    'length' => 10,
    'not null' => TRUE,
    'default' => '1',
  );
}
function lingotek_extend_metadata_tables() {
  $tables = array(
    'lingotek_entity_metadata',
    'lingotek_config_metadata',
  );
  $fields = array(
    'created',
    'modified',
  );
  $spec = array(
    'created' => array(
      'type' => 'int',
      'description' => "created timestamp",
      'default' => 0,
      'not null' => TRUE,
    ),
    'modified' => array(
      'type' => 'int',
      'description' => "last modified timestamp",
      'default' => 0,
      'not null' => TRUE,
    ),
  );
  foreach ($tables as $table) {
    foreach ($fields as $field) {
      try {
        db_add_field($table, $field, $spec[$field]);
      } catch (Exception $ex) {

        // already exists (no need to do anything)
      }
    }
  }
}

/*
 * Update
 * 1. Create the mt-queue
 * 2. delete the reference variable that was stored in lingotek_languages
 */
function lingotek_update_7200(&$sandbox) {
  drupal_load('module', 'lingotek');
  if (!isset($sandbox['progress'])) {
    $result = db_select('lingotek', 'n')
      ->distinct(TRUE)
      ->fields('n', array(
      db_escape_field('nid'),
    ))
      ->execute();
    $nodes = array();
    foreach ($result as $row) {
      array_push($nodes, $row->nid);
    }
    $sandbox['progress'] = 0;
    $sandbox['max'] = count($nodes);
    $sandbox['nodes'] = $nodes;
  }
  $node = lingotek_node_load_default($sandbox['nodes'][$sandbox['progress']]);
  if ($node !== FALSE) {
    if (!class_exists('LingotekApi')) {

      // The LingotekApi class might not have been introduced when this user last updated the Lingotek Translation module.
      registry_rebuild();
      require_once drupal_get_path('module', 'lingotek') . '/lib/Drupal/lingotek/LingotekApi.php';
    }
    if (class_exists('LingotekNode')) {
      LingotekApi::instance()
        ->updateContentDocument(LingotekNode::load($node));
    }
    else {
      throw new Exception('LingotekNode class missing. Please clear the Drupal cache and then run update.php again.');
    }
  }
  $sandbox['progress']++;
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
  if ($sandbox['#finished'] == 1) {
    lingotek_analyze_project();
  }
}

/**
 * Save per-node Lingotek project associations for all existing content.
 */
function lingotek_update_7201(&$sandbox) {
  $lingotek_nodes = array();
  $default_lingotek_project = variable_get('lingotek_project', 0);
  if ($default_lingotek_project) {
    if (!isset($sandbox['progress'])) {
      $result = db_select('lingotek', 'l')
        ->distinct(TRUE)
        ->fields('l', array(
        'nid',
      ))
        ->execute();
      foreach ($result as $row) {
        $lingotek_nodes[] = $row->nid;
      }
      $sandbox['progress'] = 0;
      $sandbox['max'] = count($lingotek_nodes);
      $sandbox['nodes'] = $lingotek_nodes;
    }
    $process_node = lingotek_node_load_default(array_shift($sandbox['nodes']));
    if ($process_node->nid && !lingotek_keystore('node', $process_node->nid, 'project_id')) {
      lingotek_keystore('node', $process_node->nid, 'project_id', $default_lingotek_project);
    }
    $sandbox['progress'] = (int) (count($sandbox['nodes']) / $sandbox['max']);
  }
  $sandbox['#finished'] = empty($sandbox['nodes']);
}

/**
 * Install default FPRM configuration data to support advanced content parsing.
 */
function lingotek_update_7203(&$sandbox) {
  if (!variable_get('lingotek_advanced_xml_config1', FALSE) && !variable_get('lingotek_advanced_xml_config2', FALSE)) {
    lingotek_set_default_advanced_xml();
  }
  else {
    return t('No action was taken, since your system already has data in the Primary and/or Secondary advanced parsing configuration fields.
      If you wish to review the default configuration files and compare them to your current settings, the files are in the fprm subfolder of the Lingotek Translation module.');
  }
}

/**
 * Ensure LingotekApi class is in autoload registry.
 */
function lingotek_update_7204(&$sandbox) {
  cache_clear_all();
}

/**
 * Ensure that the Lingotek Translation module has a weight higher than entity_translation.
 */
function lingotek_update_7205(&$sandbox) {
  lingotek_set_priority();
}

/**
 * Adds the lingotek_entity_metadata table.
 */
function lingotek_update_7206(&$sandbox) {
  $module = 'lingotek';
  $table = 'lingotek_entity_metadata';
  if (!db_table_exists($table)) {
    $schema = drupal_get_schema_unprocessed($module, $table);
    db_create_table($table, $schema);
    $output = t('Table @table was created.', array(
      '@table' => $table,
    ));
  }
  else {
    $output = t('The @table table already exists. No action taken.', array(
      '@table' => $table,
    ));
  }
  return $output;
}

/**
 * Change the value for LINGOTEK_ENABLED to a string to avoid collision.
 */
function lingotek_update_7207(&$sandbox) {
  $updated = array();
  foreach (array_keys(node_type_get_names()) as $type) {
    $name = 'language_content_type_' . $type;
    if (variable_get($name) == 5) {
      $updated[] = $type;
      variable_set($name, LINGOTEK_ENABLED);
    }
  }
  if (count($updated)) {
    $t_args = array(
      '@updated' => implode(', ', $updated),
    );
    return t("Updated @count content type(s) to use 'lingotek' instead of '5' for the identifier: @updated", $t_args);
  }
  else {
    return t('No updates necessary.');
  }
}

/**
 * Update lingokey name from 'document_id_xx' to 'document_id'
 */
function lingotek_update_7208(&$sandbox) {
  db_query("UPDATE {lingotek} SET lingokey = 'document_id' WHERE lingokey LIKE 'document_id_%'");
  return t('Updated lingotek lingokey name from \'document_id_xx\' to \'document_id\'');
}

/**
 * Extend languages table to have lingotek_enabled field
 */
function lingotek_update_7209(&$sandbox) {
  spl_autoload_register('lingotek_auto_loader');
  lingotek_migration_1();

  // Add the lingotek_enabled field to the languages table. Remove lingotek_target_languages variable
  return t('Upgraded the languages table to have the lingotek_enabled field (if not already present) and removed lingotek_target_languages variable.');
}

/**
 * Extend languages table to have lingotek_locale field
 */
function lingotek_update_7210(&$sandbox) {
  spl_autoload_register('lingotek_auto_loader');
  lingotek_migration_2();

  // Add the lingotek_locale field to the languages table
  lingotek_add_missing_locales();

  // Fill-in the lingotek_locale column based on drupal code
  return t('Upgraded the languages table to have the lingotek_locale field (if not already present) and filled-in any missing lingotek_locale values.');
}

/**
 * Upgrade lingotek table entries from drupal_codes to lingotek_locales
 */
function lingotek_update_7211(&$sandbox) {
  spl_autoload_register('lingotek_auto_loader');
  lingotek_migration_3();

  // Upgrade lingotek table entries from drupal_codes to lingotek_locales (whenever applicable)
  return t('Upgraded lingotek table entries from drupal_codes to lingotek_locales (as needed).');
}

/**
 * Adds the lingotek_entity_metadata table.
 */
function lingotek_update_7212(&$sandbox) {
  $module = 'lingotek';
  $table = 'lingotek_config_metadata';
  if (!db_table_exists($table)) {
    $schema = drupal_get_schema_unprocessed($module, $table);
    db_create_table($table, $schema);
    $output = t('Table @table was created.', array(
      '@table' => $table,
    ));
  }
  else {
    $output = t('The @table table already exists. No action taken.', array(
      '@table' => $table,
    ));
  }
  return $output;
}

/**
 * Ensure the Administrator role is granted correct permissions.
 */
function lingotek_update_7213(&$sandbox) {
  $rid = variable_get('user_admin_role', 0);
  if ($rid) {
    $permissions = array(
      'administer lingotek',
      'manage projects',
      'translation',
      'use lingotek developer tools',
    );
    user_role_grant_permissions($rid, $permissions);
    return t('Granted Lingotek permissions to the administrative security role.');
  }
}

/**
 * Adds the lingotek_translation_agent table and links it to the locales_target table
 */
function lingotek_update_7401(&$sandbox) {

  // add the translation_agent table
  $module = 'lingotek';
  $table = 'lingotek_translation_agent';
  if (!db_table_exists($table)) {
    $schema = drupal_get_schema_unprocessed($module, $table);
    db_create_table($table, $schema);
    $output = t('Table @table was created.', array(
      '@table' => $table,
    ));
  }
  else {
    $output = t('The @table table already exists. No action taken.', array(
      '@table' => $table,
    ));
  }

  // populate translation_agent table
  $agents = array(
    array(
      'id' => '1',
      'name' => 'unknown',
    ),
    array(
      'id' => '2',
      'name' => 'Drupal Translations',
    ),
    array(
      'id' => '3',
      'name' => 'Lingotek',
    ),
  );
  foreach ($agents as $a) {
    db_merge('lingotek_translation_agent')
      ->key(array(
      'id' => $a['id'],
    ))
      ->fields(array(
      'id' => $a['id'],
      'name' => $a['name'],
    ))
      ->execute();
  }
  $spec = array(
    'type' => 'int',
    'description' => "translation tool mapping",
    'length' => 10,
    'not null' => TRUE,
    'default' => '1',
  );
  try {
    db_add_field('locales_target', 'translation_agent_id', $spec);
  } catch (DatabaseSchemaObjectExistsException $e) {

    // already exists (no need to do anything)
  }
  drupal_static_reset('language_list');
  $output .= "\n" . t('Upgraded the locales target table to have the translation_agent_id field (if not already present).');
  return $output;
}

/**
 * Removes the deprecated "Lingotek Translatable" option from each field
 */
function lingotek_update_7403(&$sandbox) {
  $translate_fields = variable_get('lingotek_translate_fields');
  if (empty($translate_fields)) {
    $types = _node_types_build()->types;
    $translate_fields = array();
    foreach ($types as $t) {
      $translation_setting = variable_get('language_content_type_' . $t->type);
      if ($translation_setting == 'lingotek') {
        variable_set('language_content_type_' . $t->type, "1");
        $fields = field_info_instances("node", $t->type);
        foreach ($fields as $field_name => $instance) {
          $field = field_info_field($field_name);
          if ($field['translatable'] && $field['lingotek_translatable'] == 1) {
            $translate_fields[$t->type][] = $field_name;
          }
        }
      }
    }
    variable_set('lingotek_translate_fields', $translate_fields);
  }
}

/**
 * Upgrade the callback URL signature and adds translation profiles.
 */
function lingotek_update_7404(&$sandbox) {
  LingotekSync::updateNotifyUrl();
  lingotek_extend_metadata_tables();
  $query = db_select('lingotek', 'l');
  $query
    ->fields('l', array(
    'nid',
  ));
  $query
    ->distinct('nid');
  $result = $query
    ->execute();
  $nids = $result
    ->fetchCol();
  foreach ($nids as $nid) {
    $profile = lingotek_keystore('node', $nid, 'profile');
    if (empty($profile)) {
      lingotek_keystore('node', $nid, 'profile', LingotekSync::PROFILE_CUSTOM);
    }
  }
  $entity_profiles = variable_get('lingotek_entity_profiles');
  if (empty($entity_profiles)) {
    $fields = variable_get('lingotek_translate_fields');
    $entity_profiles = array();
    foreach (node_type_get_types() as $type => $details) {
      $entity_profiles['node'][$type] = isset($fields[$type]) ? LingotekSync::PROFILE_CUSTOM : LingotekSync::PROFILE_DISABLED;
    }
    variable_set('lingotek_entity_profiles', $entity_profiles);
  }
}

/**
 * Cleans up lingotek variables
 */
function lingotek_update_7406(&$sandbox) {
  $translate_fields = variable_get('lingotek_translate_fields');
  $enabled_fields = array(
    'node' => $translate_fields,
  );
  variable_set('lingotek_enabled_fields', $enabled_fields);
  variable_del('lingotek_translate_fields');
}

/**
 * Creates an upgrade path for existing translated content to be inserted into entity_translation module table if necessary
 */
function lingotek_update_7408(&$sandbox) {
  lingotek_extend_metadata_tables();
  if (module_exists('entity_translation')) {
    $nid_list = array();
    $results = db_select('lingotek', 'l')
      ->fields('l', array(
      'nid',
      'lingokey',
    ))
      ->condition('lingokey', 'target_sync_status_%', 'LIKE')
      ->condition('lingovalue', LingotekSync::STATUS_CURRENT)
      ->execute();
    foreach ($results as $r) {
      $nid_list[$r->nid] = str_replace('target_sync_status_', '', $r->lingokey);
    }
    foreach ($nid_list as $nid => $locale) {
      $node = lingotek_node_load_default($nid, NULL, TRUE);
      if ($node) {
        if (lingotek_managed_by_entity_translation($node->type)) {
          lingotek_entity_translation_save_status('node', $node, array(
            Lingotek::convertLingotek2Drupal($locale, FALSE),
          ));
        }
      }
    }
  }
}

/**
 * Adds support for translating additional entity types
 */
function lingotek_update_7500(&$sandbox) {
  lingotek_extend_metadata_tables();
  db_query("INSERT IGNORE INTO {lingotek_entity_metadata} (entity_id, entity_type, entity_key, value)\nSELECT nid as entity_id, 'node' as entity_type, lingokey as entity_key, lingovalue as value\nFROM lingotek");

  //db_drop_table('lingotek');// as soon as this is running in an update function remove it from the uninstall function above
}

/**
 * Updates 'lingotek_account_plan_type' to be 'advanced' when 'enterprise'
 */
function lingotek_update_7501(&$sandbox) {
  lingotek_extend_metadata_tables();
  $current_plan_type = variable_get('lingotek_account_plan_type');
  if ($current_plan_type == 'enterprise') {
    variable_set('lingotek_account_plan_type', 'advanced');
  }
}

/**
 * Migrates comment profile settings
 */
function lingotek_update_7502(&$sandbox) {
  lingotek_extend_metadata_tables();

  // migrate the old variable to the new
  $comment_settings = variable_get('lingotek_translate_comments_node_types');

  // old variable
  if (!is_null($comment_settings)) {
    $profiles = variable_get('lingotek_entity_profiles');

    // new variable where profiles will be stored
    $profiles['comment'] = isset($profiles['comment']) && is_array($profiles['comment']) ? $profiles['comment'] : array();
    foreach ($comment_settings as $setting_key => $setting_val) {
      $profiles['comment']['comment_node_' . $setting_key] = $setting_val === $setting_key ? LingotekSync::PROFILE_AUTOMATIC : LingotekSync::PROFILE_DISABLED;
    }
    variable_set('lingotek_entity_profiles', $profiles);
  }

  // deletes the old, now migrated, variables
  variable_del('lingotek_translate_comments_node_types');
  variable_del('lingotek_translate_comments');

  // set assumed status of CURRENT for all comment translations
  // select all comment entity ids in the lingotek_entity_metadata table
  $entity_type = 'comment';
  $query = db_select('lingotek_entity_metadata', 'l');
  $query
    ->fields('l', array(
    'entity_id',
  ))
    ->condition('entity_type', $entity_type);
  $query
    ->distinct('entity_id');
  $result = $query
    ->execute();
  $entity_ids = $result
    ->fetchCol();

  // foreach entity, set the target statuses
  if (!empty($entity_ids)) {
    $entities = entity_load('comment', $entity_ids);
    foreach ($entities as $entity) {
      $entity_id = $entity->cid;
      foreach (array_keys(Lingotek::getLanguages()) as $lingotek_locale) {
        if (Lingotek::convertDrupal2Lingotek($entity->language) != $lingotek_locale) {

          // only add status when not the source language
          LingotekSync::setTargetStatus($entity_type, $entity_id, $lingotek_locale, LingotekSync::STATUS_CURRENT);
        }
        else {

          // remove status when source language equal to target language
          lingotek_keystore_delete($entity_type, $entity_id, "target_sync_status_{$lingotek_locale}");
          lingotek_keystore($entity_type, $entity_id, 'upload_status', LingotekSync::STATUS_CURRENT);
        }
      }
    }
  }
}

/**
 * Migrate any CUSTOM profile usage within entity_profiles
 */
function lingotek_update_7503(&$sandbox) {
  lingotek_extend_metadata_tables();
  $profiles = variable_get('lingotek_entity_profiles');
  foreach ($profiles as $profile_key => $profile) {
    foreach ($profile as $bundle => $profile_id) {
      if ($profile_id === LingotekSync::PROFILE_CUSTOM) {
        $profiles[$profile_key][$bundle] = LingotekSync::PROFILE_AUTOMATIC;
      }
    }
  }
  variable_set('lingotek_entity_profiles', $profiles);
}

/**
 * Add indexes to the lingotek_entity_metadata table
 */
function lingotek_update_7504(&$sandbox) {
  lingotek_extend_metadata_tables();
  $table = 'lingotek_entity_metadata';
  $indexes = array(
    'idx_entity_id' => array(
      'entity_id',
    ),
    'idx_entity_key' => array(
      'entity_key',
    ),
    'idx_entity_type_and_key' => array(
      'entity_type',
      'entity_key',
    ),
    'idx_entity_key_and_value' => array(
      'entity_key',
      'value',
    ),
  );
  foreach ($indexes as $index_name => $fields) {
    if (!db_index_exists($table, $index_name)) {
      db_add_index($table, $index_name, $fields);
    }
  }
  return t('Added indexes to the <i>lingotek_entity_metadata</i> table.');
}

/**
 * Remove unused entries from lingotek_entity_metadata table
 */
function lingotek_update_7505(&$sandbox) {
  lingotek_extend_metadata_tables();
  $delete = db_delete('lingotek_entity_metadata');
  $or = db_or();
  $or
    ->condition('entity_key', 'target_sync_progress_%', 'LIKE');
  $or
    ->condition('entity_key', 'target_sync_last_progress_updated_%', 'LIKE');
  $or
    ->condition('entity_key', 'target_last_downloaded_%', 'LIKE');
  $or
    ->condition('entity_key', 'translation_progress');
  $count = $delete
    ->condition($or)
    ->execute();
  return format_plural($count, 'Removed one unused entry from the <i>lingotek_entity_metadata</i> table.', 'Removed @count unused entries from the <i>lingotek_entity_metadata</i> table.');
}

/**
 * Add created and modified timestamps to lingotek schemas.
 */
function lingotek_update_7506(&$sandbox) {

  // lingotek_keystore() was modified as of update #7506 to
  // require two additional fields: created and modified.  So, that
  // update function is called in all previous update functions prior to
  // running lingotek_keystore() as a forward-compatibility, since these
  // fields are missing at the point of previous updates, while the db
  // schema includes the fields already.  This is the final call to it.
  lingotek_extend_metadata_tables();
  $message = "Creation and modification timestamps for Lingotek metadata have successfully been added.";
  return $message;
}

/**
 * Reset any bundles currently set to legacy profile 'CUSTOM' to be 'MANUAL'.
 */
function lingotek_update_7507(&$sandbox) {
  $entity_profiles = variable_get('lingotek_entity_profiles', NULL);
  $custom_bundles = array();
  foreach ($entity_profiles as $entity_type => $bundles) {
    foreach ($bundles as $bundle_name => $profile) {
      if ($profile == LingotekSync::PROFILE_CUSTOM) {
        $entity_profiles[$entity_type][$bundle_name] = LingotekSync::PROFILE_MANUAL;
        $custom_bundles[] = "{$bundle_name} ({$entity_type})";
      }
    }
  }
  if (!empty($custom_bundles)) {
    variable_set('lingotek_entity_profiles', $entity_profiles);
    $message = "The following content types have been reset from profile 'CUSTOM' to profile 'MANUAL': " . implode(', ', $custom_bundles) . "  (The 'CUSTOM' profile is now reserved for individual entities only and is no longer supported at the content-type level.)";
    return $message;
  }
}

/**
 * Reduce the size of current entity_type and entity_key fields in the lingotek_entity_metadata table
 */
function lingotek_update_7508(&$sandbox) {
  $table = 'lingotek_entity_metadata';
  $entity_type_spec = array(
    'description' => 'The entity type (node, comment, etc.).',
    'type' => 'varchar',
    'length' => 128,
    'not null' => TRUE,
    'default' => '',
  );
  $entity_key_spec = array(
    'description' => 'The ID for the Lingotek-associated value.',
    'type' => 'varchar',
    'length' => 128,
    'not null' => TRUE,
    'default' => '',
  );
  $primary_key = array(
    'entity_id',
    'entity_type',
    'entity_key',
  );
  $indexes = array(
    'idx_entity_key' => array(
      'entity_key',
    ),
    'idx_entity_type_and_key' => array(
      'entity_type',
      'entity_key',
    ),
    'idx_entity_key_and_value' => array(
      'entity_key',
      'value',
    ),
  );

  // drop the related keys and indices
  db_drop_primary_key($table);
  foreach ($indexes as $index_name => $fields) {
    db_drop_index($table, $index_name);
  }

  // update the fields to be 128 instead of 255
  db_change_field($table, 'entity_type', 'entity_type', $entity_type_spec);
  db_change_field($table, 'entity_key', 'entity_key', $entity_key_spec);

  // add back related keys and indices
  db_add_primary_key($table, $primary_key);
  foreach ($indexes as $index_name => $fields) {
    db_add_index($table, $index_name, $fields);
  }
  return t('Reduced field size of entity types and keys in the Lingotek entity metadata table.');
}

/**
 * Reduce the size of current value field in the lingotek_entity_metadata table
 */
function lingotek_update_7509(&$sandbox) {
  $table = 'lingotek_entity_metadata';
  $entity_value_spec = array(
    'description' => 'Value for the specified key.',
    'type' => 'varchar',
    'length' => 128,
    'not null' => TRUE,
    'default' => '',
  );
  $indexes = array(
    'idx_entity_key_and_value' => array(
      'entity_key',
      'value',
    ),
  );

  // drop the related index
  foreach ($indexes as $index_name => $fields) {
    db_drop_index($table, $index_name);
  }

  // update the field to be 128 instead of 255
  db_change_field($table, 'value', 'value', $entity_value_spec);

  // add back related index
  foreach ($indexes as $index_name => $fields) {
    db_add_index($table, $index_name, $fields);
  }
  return t('Reduced field size of value column in the Lingotek entity metadata table.');
}

/**
 * Adds the lingotek_config_map table.
 */
function lingotek_update_7600(&$sandbox) {
  $module = 'lingotek';
  $table = 'lingotek_config_map';
  if (!db_table_exists($table)) {
    $schema = drupal_get_schema_unprocessed($module, $table);
    db_create_table($table, $schema);
    $output = t('Table @table was created.', array(
      '@table' => $table,
    ));
  }
  else {
    $output = t('The @table table already exists. No action taken.', array(
      '@table' => $table,
    ));
  }
  return $output;
}

/**
 * Truncate lingotek_config_metadata table in preparation for new config handling.
 */
function lingotek_update_7601(&$sandbox) {
  db_truncate('lingotek_config_metadata')
    ->execute();
  return t('Truncated table containing Lingotek config metadata such as document ID\'s and translation status for config strings.');
}

/**
 * Change node_sync_status to upload_status in lingotek-specific metadata tables
 */
function lingotek_update_7602(&$sandbox) {
  $num_updated = db_update('lingotek_entity_metadata')
    ->fields(array(
    'entity_key' => 'upload_status',
  ))
    ->condition('entity_key', 'node_sync_status')
    ->execute();
  return t('Changed the name of the node_sync_status variable in the Lingotek entity metadata table to be upload_status (@num_updated rows affected).', array(
    '@num_updated' => $num_updated,
  ));
}

/**
 * Create translation profile for Lingotek settings of config items.
 */
function lingotek_update_7603(&$sandbox) {
  registry_rebuild();
  $default_profile = array();
  lingotek_save_config_profile($default_profile);
  return t('Created Lingotek translation profile for config items.');
}

/**
 * Erase translation profiles for bundles that no longer exist.
 */
function lingotek_update_7604(&$sandbox) {
  $entity_profiles = variable_get('lingotek_entity_profiles');
  foreach ($entity_profiles as $entity_type => $bundles) {
    $entity_bundles = field_info_bundles($entity_type);
    foreach ($bundles as $bundle_name => $profile) {
      if (!array_key_exists($bundle_name, $entity_bundles)) {
        unset($entity_profiles[$entity_type][$bundle_name]);
      }
    }
  }
  variable_set('lingotek_entity_profiles', $entity_profiles);
  return t('Removed Lingotek translation profiles for bundles that not longer exist.');
}

/**
 * Remove unnecessary 'config' variable, created as a byproduct of the additional-translation form.
 */
function lingotek_update_7605(&$sandbox) {

  // Make sure this is not another module's variable.
  $config = variable_get('config', FALSE);
  if (isset($config['lingotek_translate_config_blocks'])) {
    variable_del('config');
  }
  return t("Removed unnecessary 'config' variable from the variables table.");
}

/**
 * If module was setup previously, add a setting confirming comment translation setup.
 */
function lingotek_update_7606(&$sandbox) {

  // When upgrading from 5.x, if no comments were enabled for translation, then
  // as long as the module has been setup, just initialize the comment variable
  // to save the user from having to go through the setup process again.
  $enabled_fields = variable_get('lingotek_enabled_fields', FALSE);
  if (isset($enabled_fields['node']) && !isset($enabled_fields['comment'])) {
    $enabled_fields['comment'] = array();
    variable_set('lingotek_enabled_fields', $enabled_fields);
    return t("Added flag confirming setup complete for comment translation (currently disabled).");
  }
}

Functions

Namesort descending Description
lingotek_disable Implements hook_disable().
lingotek_enable Implements hook_enable().
lingotek_extend_metadata_tables
lingotek_install Implements hook_install().
lingotek_requirements
lingotek_schema
lingotek_schema_alter
lingotek_uninstall Implements hook_uninstall().
lingotek_update_7200
lingotek_update_7201 Save per-node Lingotek project associations for all existing content.
lingotek_update_7203 Install default FPRM configuration data to support advanced content parsing.
lingotek_update_7204 Ensure LingotekApi class is in autoload registry.
lingotek_update_7205 Ensure that the Lingotek Translation module has a weight higher than entity_translation.
lingotek_update_7206 Adds the lingotek_entity_metadata table.
lingotek_update_7207 Change the value for LINGOTEK_ENABLED to a string to avoid collision.
lingotek_update_7208 Update lingokey name from 'document_id_xx' to 'document_id'
lingotek_update_7209 Extend languages table to have lingotek_enabled field
lingotek_update_7210 Extend languages table to have lingotek_locale field
lingotek_update_7211 Upgrade lingotek table entries from drupal_codes to lingotek_locales
lingotek_update_7212 Adds the lingotek_entity_metadata table.
lingotek_update_7213 Ensure the Administrator role is granted correct permissions.
lingotek_update_7401 Adds the lingotek_translation_agent table and links it to the locales_target table
lingotek_update_7403 Removes the deprecated "Lingotek Translatable" option from each field
lingotek_update_7404 Upgrade the callback URL signature and adds translation profiles.
lingotek_update_7406 Cleans up lingotek variables
lingotek_update_7408 Creates an upgrade path for existing translated content to be inserted into entity_translation module table if necessary
lingotek_update_7500 Adds support for translating additional entity types
lingotek_update_7501 Updates 'lingotek_account_plan_type' to be 'advanced' when 'enterprise'
lingotek_update_7502 Migrates comment profile settings
lingotek_update_7503 Migrate any CUSTOM profile usage within entity_profiles
lingotek_update_7504 Add indexes to the lingotek_entity_metadata table
lingotek_update_7505 Remove unused entries from lingotek_entity_metadata table
lingotek_update_7506 Add created and modified timestamps to lingotek schemas.
lingotek_update_7507 Reset any bundles currently set to legacy profile 'CUSTOM' to be 'MANUAL'.
lingotek_update_7508 Reduce the size of current entity_type and entity_key fields in the lingotek_entity_metadata table
lingotek_update_7509 Reduce the size of current value field in the lingotek_entity_metadata table
lingotek_update_7600 Adds the lingotek_config_map table.
lingotek_update_7601 Truncate lingotek_config_metadata table in preparation for new config handling.
lingotek_update_7602 Change node_sync_status to upload_status in lingotek-specific metadata tables
lingotek_update_7603 Create translation profile for Lingotek settings of config items.
lingotek_update_7604 Erase translation profiles for bundles that no longer exist.
lingotek_update_7605 Remove unnecessary 'config' variable, created as a byproduct of the additional-translation form.
lingotek_update_7606 If module was setup previously, add a setting confirming comment translation setup.