You are here

function lingotek_sync_upload_config_set in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.6 lingotek.batch.inc \lingotek_sync_upload_config_set()

Upload Batch Worker Function: Upload Config Set for Translation

1 string reference to 'lingotek_sync_upload_config_set'
lingotek_get_sync_upload_config_batch_elements in ./lingotek.batch.inc
Sync - Upload Config Batch Elements: Creates the batch elements for config (ie. menus, taxonomies, etc.), that need to be uploaded.

File

./lingotek.batch.inc, line 404
Central location for batch create functions, before control is handed off to individual batch command files.

Code

function lingotek_sync_upload_config_set($set_id, $lid_map, &$context) {
  if ($context) {
    $context['message'] = t('Uploading configuration set #@sid for translation', array(
      '@sid' => $set_id,
    ));
  }
  $api = LingotekApi::instance();
  $set = LingotekConfigSet::loadById($set_id);
  $query = db_select('lingotek_config_metadata', 'lcm');
  $query
    ->addField('lcm', 'value');
  $query
    ->condition('id', $set_id, '=');
  $query
    ->condition('config_key', 'workflow_id', '=');
  $workflow_id = $query
    ->execute()
    ->fetchField();
  if ($workflow_id !== NULL) {
    $set
      ->setWorkflowId($workflow_id);
  }
  module_invoke_all('lingotek_pre_upload', $set);
  $existing_document_id = $set
    ->hasLingotekDocId();
  if ($existing_document_id) {
    $is_complete = FALSE;
    $params = array(
      'id' => $existing_document_id,
    );
    LingotekLog::trace('existing document: @existing', array(
      '@existing' => $existing_document_id,
    ));

    // There is a race condition here that depends on whether the config document
    // has been uploaded to the TMS before other members of the config set try
    // to update the document. This loop waits for up to 30 seconds for the document
    // to be uploaded before updating. If the response is 'Process not found.',
    // we assume that the document has finished uploading since there's no process.
    for ($i = 0; $i < 10; $i++) {
      $import_response = $api
        ->request('getDocumentImportStatus', $params);
      $import_completed = !is_null($import_response->status) && $import_response->status === 'COMPLETE';
      $no_process_found = !is_null($import_response->error) && $import_response->error === 'Process not found.';
      if ($import_completed || $no_process_found) {
        $result = $api
          ->updateContentDocument($set);
        $is_complete = TRUE;
        break;
      }
      else {
        sleep(3);
      }
    }
    if (!$is_complete) {
      foreach ($lid_map as $group => $lids) {
        LingotekConfigSet::removeLids($lids);
      }
    }
  }
  else {
    $result = $api
      ->addContentDocument($set, TRUE);
  }
  if ($result) {
    $context['results']['uploads'] = isset($context['results']['uploads']) && is_numeric($context['results']['uploads']) ? $context['results']['uploads'] + 1 : 1;
    if (!isset($context['results']['uploaded_cids']) || !is_array($context['results']['uploaded_cids'])) {
      $context['results']['uploaded_cids'] = array();
    }
    $context['results']['uploaded_cids'][] = $set_id;
  }
  else {
    $context['results']['upload_fails'] = isset($context['results']['upload_fails']) && is_numeric($context['results']['upload_fails']) ? $context['results']['upload_fails'] + 1 : 1;
    if (!isset($context['results']['upload_fail_cids']) || !is_array($context['results']['upload_fail_cids'])) {
      $context['results']['upload_fail_cids'] = array();
    }
    $context['results']['upload_fail_cids'][] = $set_id;
  }
}