You are here

public function TMGMTFileformatXLIFF::validateImport in Translation Management Tool 7

Validates that the given file is valid and can be imported.

@todo this function should NOT return a job. We need a import processor instance instead to deal with the import context.

Parameters

string $imported_file: File path to the file to be imported.

bool $is_file: (optional) Whether $imported_file is the path to a file or not.

Return value

TMGMTJob Returns the corresponding translation job entity if the import file is valid, FALSE otherwise.

Overrides TMGMTFileFormatInterface::validateImport

File

translators/file/tmgmt_file.format.xliff.inc, line 204

Class

TMGMTFileformatXLIFF
Export to XLIFF format.

Code

public function validateImport($imported_file, $is_file = TRUE) {

  // Validates imported XLIFF file.
  // Checks:
  // - Job ID
  // - Target ans source languages
  // - Content integrity.
  if (!($xml = $this
    ->getImportedXML($imported_file, $is_file))) {
    drupal_set_message(t('The imported file is not a valid XML.'), 'error');
    return FALSE;
  }

  // Check if our phase information is there.
  $phase = $xml
    ->xpath("//xliff:phase[@phase-name='extraction']");
  if ($phase) {
    $phase = reset($phase);
  }
  else {
    drupal_set_message(t('The imported file is missing required XLIFF phase information.'), 'error');
    return FALSE;
  }

  // Check if the job has a valid job reference.
  if (!isset($phase['job-id'])) {
    drupal_set_message(t('The imported file does not contain a job reference.'), 'error');
    return FALSE;
  }

  // Attempt to load the job if none passed.
  $job = tmgmt_job_load((int) $phase['job-id']);
  if (empty($job)) {
    drupal_set_message(t('The imported file job id @file_tjid is not available.', array(
      '@file_tjid' => $phase['job-id'],
    )), 'error');
    return FALSE;
  }

  // @todo We use the $job to addMessage in case of failure. However the job
  //   context is not safe at this point.
  // Compare source language.
  if (!isset($xml->file['source-language']) || $job
    ->getTranslator()
    ->mapToRemoteLanguage($job->source_language) != $xml->file['source-language']) {
    $job
      ->addMessage('The imported file source language @file_language does not match the job source language @job_language.', array(
      '@file_language' => empty($xml->file['source-language']) ? t('none') : $xml->file['source-language'],
      '@job_language' => $job->source_language,
    ), 'error');
    return FALSE;
  }

  // Compare target language.
  if (!isset($xml->file['target-language']) || $job
    ->getTranslator()
    ->mapToRemoteLanguage($job->target_language) != $xml->file['target-language']) {
    $job
      ->addMessage('The imported file target language @file_language does not match the job target language @job_language.', array(
      '@file_language' => empty($xml->file['target-language']) ? t('none') : $xml->file['target-language'],
      '@job_language' => $job->target_language,
    ), 'error');
    return FALSE;
  }
  $targets = $this
    ->getImportedTargets($job);
  if (empty($targets)) {
    $job
      ->addMessage('The imported file seems to be missing translation.', 'error');
    return FALSE;
  }

  // In case we do not do xliff processing we cannot do the elements
  // count validation.
  if (!$job
    ->getSetting('xliff_processing')) {
    return $job;
  }
  $reader = new XMLReader();
  $xliff_validation = $job
    ->getSetting('xliff_validation');
  foreach ($targets as $id => $target) {
    $array_key = tmgmt_ensure_keys_array($id);
    $job_item = tmgmt_job_item_load(array_shift($array_key));
    $count = 0;
    $reader
      ->XML('<translation>' . $target['#text'] . '</translation>');
    while ($reader
      ->read()) {
      if (in_array($reader->name, array(
        'translation',
        '#text',
      ))) {
        continue;
      }
      $count++;
    }
    if (!isset($xliff_validation[$id]) || $xliff_validation[$id] != $count) {
      $job_item
        ->addMessage('Failed to validate semantic integrity of %key element. Please check also the HTML code of the element in the review process.', array(
        '%key' => tmgmt_ensure_keys_string($array_key),
      ));
    }
  }

  // Validation successful.
  return $job;
}