public function Xliff::validateImport

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

Parameters

$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

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

Overrides FormatInterface::validateImport

File

translators/tmgmt_file/src/Plugin/tmgmt_file/Format/Xliff.php, line 259

Class

Xliff
Export to XLIFF format.

Namespace

Drupal\tmgmt_file\Plugin\tmgmt_file\Format

Code

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

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

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

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

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

  // Compare source language.
  if (!isset($xml->file['source-language']) || $job
    ->getRemoteSourceLanguage() != $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
        ->getRemoteSourceLanguage(),
    ), 'error');
    return FALSE;
  }

  // Compare target language.
  if (!isset($xml->file['target-language']) || $job
    ->getRemoteTargetLanguage() != $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
        ->getRemoteTargetLanguage(),
    ), '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 = \Drupal::service('tmgmt.data')
      ->ensureArrayKey($id);
    $job_item = JobItem::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' => \Drupal::service('tmgmt.data')
          ->ensureStringKey($array_key),
      ));
    }
  }

  // Validation successful.
  return $job;
}