You are here

lingotek.install in Lingotek Translation 7.4

Installation for Lingotek Community Translation Module.

File

lingotek.install
View source
<?php

/**
 * @file
 * Installation for Lingotek Community Translation Module.
 */

/*
 * 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_config_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'] = array(
    'description' => 'Table for storing node related data.',
    'fields' => array(
      'nid' => array(
        'description' => 'The primary identifier for a node',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'lingokey' => array(
        'description' => 'Key in key/value pairs',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'lingovalue' => array(
        'description' => 'Value of the specified key',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'primary key' => array(
      'nid',
      'lingokey',
    ),
  );
  $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' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'entity_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' => '',
      ),
    ),
    'primary key' => array(
      'entity_id',
      'entity_type',
      'entity_key',
    ),
  );
  $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' => '',
      ),
    ),
    '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',
    ),
  );
  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',
  );
}

/*
 * 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_lingonode($process_node->nid, 'project_id')) {
      lingotek_lingonode($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_insert('lingotek_translation_agent')
      ->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();
  $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_lingonode($nid, 'profile');
    if (empty($profile)) {
      lingotek_lingonode($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);
  }
}

/**
 * Makes lingotek handle entities better.
 */
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) {
  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, array(
            Lingotek::convertLingotek2Drupal($locale),
          ));
        }
      }
    }
  }
}

Functions

Namesort descending Description
lingotek_disable Implements hook_disable().
lingotek_enable Implements hook_enable().
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 Makes lingotek handle entities better.
lingotek_update_7408 Creates an upgrade path for existing translated content to be inserted into entity_translation module table if necessary