Verified Commit 16e91ba0 authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

task: #3581303 Convert locale batch callbacks

By: nicxvan
By: berdir
By: amateescu
parent 733f95f8
Loading
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -19471,12 +19471,6 @@
	'count' => 1,
	'path' => __DIR__ . '/modules/link/tests/src/Unit/Plugin/migrate/process/FieldLinkTest.php',
];
$ignoreErrors[] = [
	'message' => '#^Function locale_config_batch_update_components\\(\\) should return array but return statement is missing\\.$#',
	'identifier' => 'return.missing',
	'count' => 1,
	'path' => __DIR__ . '/modules/locale/locale.bulk.inc',
];
$ignoreErrors[] = [
	'message' => '#^Function locale_string_is_safe\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
+6 −5
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@
use Drupal\locale\LocaleDefaultOptions;
use Drupal\locale\LocaleFetch;
use Drupal\locale\File\LocaleFileManager;
use Drupal\locale\LocaleConfigBatch;
use Drupal\locale\LocaleProjectChecker;
use Drupal\locale\LocaleProjectRepository;
use Drupal\locale\LocaleTranslatableProject;
@@ -1783,12 +1784,12 @@ function install_import_translations(&$install_state) {
    foreach ($languages as $language) {
      if (locale_translation_use_remote_source()) {
        $batch_builder->addOperation(
          'locale_translation_batch_fetch_download',
          LocaleFetch::class . ':batchDownload',
          ['drupal', $language->getId()],
        );
      }
      $batch_builder->addOperation(
        'locale_translation_batch_fetch_import',
        LocaleFetch::class . ':batchImport',
        ['drupal', $language->getId(), []],
      );
    }
@@ -1797,7 +1798,7 @@ function install_import_translations(&$install_state) {
      ->setTitle(t('Updating translations.'))
      ->setProgressMessage('')
      ->setErrorMessage(t('Error importing translation files'))
      ->setFinishCallback('locale_translation_batch_fetch_finished');
      ->setFinishCallback(LocaleFetch::class . ':batchFinished');
    return $batch_builder->toArray();
  }
}
@@ -1873,7 +1874,7 @@ function install_finish_translations(&$install_state): array {
  $batches = [];
  if (count($projects) > 1) {
    $options = LocaleDefaultOptions::updateOptions();
    if ($batch = \Drupal::service(LocaleFetch::class)->batchUpdateBuild([], array_keys($languages), $options)) {
    if ($batch = \Drupal::service(LocaleFetch::class)->buildUpdateBatch([], array_keys($languages), $options)) {
      $batches[] = $batch;
    }
  }
@@ -1885,7 +1886,7 @@ function install_finish_translations(&$install_state): array {
  }

  // Creates configuration translations.
  $batches[] = locale_config_batch_update_components([], array_keys($languages), [], TRUE);
  $batches[] = \Drupal::service(LocaleConfigBatch::class)->buildBatch([], array_keys($languages), [], TRUE);
  return $batches;
}

+54 −170
Original line number Diff line number Diff line
@@ -4,11 +4,11 @@
 * @file
 */

use Drupal\Core\Url;
use Drupal\locale\File\LocaleFile;
use Drupal\locale\File\LocaleFileManager;
use Drupal\locale\File\RemoteFileStatus;
use Drupal\locale\LocaleProjectRepository;
use Drupal\locale\LocaleFetch;
use Drupal\locale\LocaleProjectChecker;
use Drupal\locale\LocaleSource;

/**
@@ -28,28 +28,17 @@
 *   Language code of the language for which to check the translation.
 * @param array|\ArrayAccess $context
 *   The batch context.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
 *   \Drupal::service(LocaleFetch::class)->batchVersionCheck()
 *   instead.
 *
 * @see https://www.drupal.org/node/3589759
 */
function locale_translation_batch_version_check(string $project, string $langcode, array|\ArrayAccess &$context): void {
  $locale_project = \Drupal::service(LocaleProjectRepository::class)->getMultiple([$project])[$project] ?? [];

  if (empty($locale_project)) {
    return;
  }

  $status = \Drupal::keyValue('locale.translation_status')->get($project);
  if (!isset($status[$langcode])) {
    return;
  }

  if ($locale_project->version == $status[$langcode]->version) {
    return;
  }

  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal::service(LocaleFetch::class)->batchVersionCheck() instead. See https://www.drupal.org/node/3589759', E_USER_DEPRECATED);
  \Drupal::moduleHandler()->loadInclude('locale', 'bulk.inc');
  locale_translation_status_delete_projects([$project]);
  \Drupal::service(LocaleFileManager::class)->deleteTranslationFiles([$project]);

  $context['message'] = t('Checked version of %project.', ['%project' => $project]);
  \Drupal::service(LocaleFetch::class)->batchVersionCheck($project, $langcode, $context);
}

/**
@@ -70,66 +59,16 @@ function locale_translation_batch_version_check(string $project, string $langcod
 *     Optional, defaults to TRUE.
 * @param array|\ArrayAccess $context
 *   The batch context.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
 *   \Drupal::service(LocaleFetch::class)->batchStatusCheck()
 *   instead.
 *
 * @see https://www.drupal.org/node/3589759
 */
function locale_translation_batch_status_check($project, $langcode, array $options, &$context): void {
  $failure = $checked = FALSE;
  $options += [
    'finish_feedback' => TRUE,
    'use_remote' => TRUE,
  ];
  $source = locale_translation_get_status([$project], [$langcode]);
  $source = $source[$project][$langcode];

  // Check the status of local translation files.
  if (isset($source->files[LOCALE_TRANSLATION_LOCAL])) {
    if ($file = \Drupal::service(LocaleSource::class)->sourceCheckFile($source)) {
      locale_translation_status_save($source->name, $source->langcode, LOCALE_TRANSLATION_LOCAL, $file);
    }
    $checked = TRUE;
  }

  // Check the status of remote translation files.
  if ($options['use_remote'] && isset($source->files[LOCALE_TRANSLATION_REMOTE])) {
    $remote_file = $source->files[LOCALE_TRANSLATION_REMOTE];
    if ($langcode === 'en') {
      // drupal.org does not support english as translation.
      $uri = \Drupal::service(LocaleSource::class)->buildServerPattern($source, strtr(\Drupal::TRANSLATION_DEFAULT_SERVER_PATTERN, ['%language' => $langcode]));
      if ($uri === $remote_file->uri) {
        $failure = TRUE;
      }
    }

    $remoteFileInfo = \Drupal::service(LocaleFileManager::class)->checkRemoteFileStatus($remote_file->uri);
    if (!$failure && $remoteFileInfo->status !== RemoteFileStatus::Error) {
      // Update the file object with the result data. In case of a redirect we
      // store the resulting uri.
      if ($remoteFileInfo->lastModified) {
        $remote_file->uri = $remoteFileInfo->location ?? $remote_file->uri;
        $remote_file->timestamp = $remoteFileInfo->lastModified;
        locale_translation_status_save($source->name, $source->langcode, LOCALE_TRANSLATION_REMOTE, $remote_file);
      }
      // @todo What to do with when the file is not found (404)? To prevent
      //   re-checking within the TTL (1day, 1week) we can set a last_checked
      //   timestamp or cache the result.
      $checked = TRUE;
    }
    else {
      $failure = TRUE;
    }
  }

  // Provide user feedback and record success or failure for reporting at the
  // end of the batch.
  if ($options['finish_feedback'] && $checked) {
    $context['results']['files'][] = $source->name;
  }
  if ($failure && !$checked) {
    $context['results']['failed_files'][] = $source->name;
  }
  $context['message'] = t('Checked %langcode translation for %project.', [
    '%langcode' => $langcode,
    '%project' => $source->project,
  ]);
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal::service(LocaleFetch::class)->batchStatusCheck() instead. See https://www.drupal.org/node/3589759', E_USER_DEPRECATED);
  \Drupal::service(LocaleFetch::class)->batchStatusCheck($project, $langcode, $options, $context);
}

/**
@@ -141,33 +80,16 @@ function locale_translation_batch_status_check($project, $langcode, array $optio
 *   TRUE if batch successfully completed.
 * @param array $results
 *   Batch results.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
 *   \Drupal::service(LocaleProjectChecker::class)->batchFinished()
 *   instead.
 *
 * @see https://www.drupal.org/node/3589759
 */
function locale_translation_batch_status_finished($success, $results): void {
  if ($success) {
    if (isset($results['failed_files'])) {
      if (\Drupal::moduleHandler()->moduleExists('dblog') && \Drupal::currentUser()->hasPermission('access site reports')) {
        $message = \Drupal::translation()->formatPlural(count($results['failed_files']), 'One translation file could not be checked. <a href=":url">See the log</a> for details.', '@count translation files could not be checked. <a href=":url">See the log</a> for details.', [':url' => Url::fromRoute('dblog.overview')->toString()]);
      }
      else {
        $message = \Drupal::translation()->formatPlural(count($results['failed_files']), 'One translation files could not be checked. See the log for details.', '@count translation files could not be checked. See the log for details.');
      }
      \Drupal::messenger()->addError($message);
    }
    if (isset($results['files'])) {
      \Drupal::messenger()->addStatus(\Drupal::translation()->formatPlural(
        count($results['files']),
        'Checked available interface translation updates for one project.',
        'Checked available interface translation updates for @count projects.'
      ));
    }
    if (!isset($results['failed_files']) && !isset($results['files'])) {
      \Drupal::messenger()->addStatus(t('Nothing to check.'));
    }
    \Drupal::state()->set('locale.translation_last_checked', \Drupal::time()->getRequestTime());
  }
  else {
    \Drupal::messenger()->addError(t('An error occurred trying to check available interface translation updates.'));
  }
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal::service(LocaleProjectChecker::class)->batchFinished() instead. See https://www.drupal.org/node/3589759', E_USER_DEPRECATED);
  \Drupal::service(LocaleProjectChecker::class)->batchFinished($success, $results);
}

/**
@@ -183,25 +105,17 @@ function locale_translation_batch_status_finished($success, $results): void {
 * @param array $context
 *   The batch context.
 *
 * @see locale_translation_batch_fetch_import()
 * @see \Drupal\locale\LocaleFetch::batchImport()
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
 *   \Drupal::service(LocaleFetch::class)->batchDownload()
 *   instead.
 *
 * @see https://www.drupal.org/node/3589759
 */
function locale_translation_batch_fetch_download($project, $langcode, &$context): void {
  $sources = locale_translation_get_status([$project], [$langcode]);
  if (isset($sources[$project][$langcode])) {
    $source = $sources[$project][$langcode];
    if (isset($source->type) && $source->type == LOCALE_TRANSLATION_REMOTE) {
      if ($file = \Drupal::service(LocaleFileManager::class)->downloadTranslationSource($source->files[LOCALE_TRANSLATION_REMOTE], 'translations://')) {
        $context['message'] = t('Downloaded %langcode translation for %project.', [
          '%langcode' => $langcode,
          '%project' => $source->project,
        ]);
        locale_translation_status_save($source->name, $source->langcode, LOCALE_TRANSLATION_LOCAL, $file);
      }
      else {
        $context['results']['failed_files'][] = $source->files[LOCALE_TRANSLATION_REMOTE];
      }
    }
  }
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal::service(LocaleFetch::class)->batchDownload() instead. See https://www.drupal.org/node/3589759', E_USER_DEPRECATED);
  \Drupal::service(LocaleFetch::class)->batchDownload($project, $langcode, $context);
}

/**
@@ -219,53 +133,19 @@ function locale_translation_batch_fetch_download($project, $langcode, &$context)
 * @param array $context
 *   The batch context.
 *
 * @see locale_translate_batch_build()
 * @see locale_translation_batch_fetch_download()
 * @see \Drupal\locale\LocaleImportBatch::buildBatch()
 * @see \Drupal\locale\LocaleFetch::batchDownload()
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
 *   \Drupal::service(LocaleFetch::class)->batchImport()
 *   instead.
 *
 * @see https://www.drupal.org/node/3589759
 */
function locale_translation_batch_fetch_import($project, $langcode, $options, &$context): void {
  $sources = locale_translation_get_status([$project], [$langcode]);
  if (isset($sources[$project][$langcode])) {
    $source = $sources[$project][$langcode];
    if (isset($source->type)) {
      if ($source->type == LOCALE_TRANSLATION_REMOTE || $source->type == LOCALE_TRANSLATION_LOCAL) {
        $file = $source->files[LOCALE_TRANSLATION_LOCAL];
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal::service(LocaleFetch::class)->batchImport() instead. See https://www.drupal.org/node/3589759', E_USER_DEPRECATED);
  \Drupal::moduleHandler()->loadInclude('locale', 'inc', 'locale.bulk');
        $options += [
          'message' => t('Importing %langcode translation for %project.', [
            '%langcode' => $langcode,
            '%project' => $source->project,
          ]),
        ];
        // Import the translation file. For large files the batch operations is
        // progressive and will be called repeatedly until finished.
        locale_translate_batch_import($file, $options, $context);

        // The import is finished.
        if (isset($context['finished']) && $context['finished'] == 1) {
          // The import is successful.
          if (isset($context['results']['files'][$file->uri])) {
            $context['message'] = t('Imported %langcode translation for %project.', [
              '%langcode' => $langcode,
              '%project' => $source->project,
            ]);

            // Save the data of imported source into the {locale_file} table and
            // update the current translation status.
            locale_translation_status_save($project, $langcode, LOCALE_TRANSLATION_CURRENT, $source->files[LOCALE_TRANSLATION_LOCAL]);
          }
        }
      }
      elseif ($source->type == LOCALE_TRANSLATION_CURRENT) {
        /*
         * This can happen if the locale_translation_batch_fetch_import
         * batch was interrupted
         * and the translation was imported by another batch.
         */
        $context['message'] = t('Ignoring already imported translation for %project.', ['%project' => $source->project]);
        $context['finished'] = 1;
      }
    }
  }
  \Drupal::service(LocaleFetch::class)->batchImport($project, $langcode, $options, $context);
}

/**
@@ -277,13 +157,17 @@ function locale_translation_batch_fetch_import($project, $langcode, $options, &$
 *   TRUE if batch successfully completed.
 * @param array $results
 *   Batch results.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
 *   \Drupal::service(LocaleFetch::class)->batchFinished()
 *   instead.
 *
 * @see https://www.drupal.org/node/3589759
 */
function locale_translation_batch_fetch_finished($success, $results): void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal::service(LocaleFetch::class)->batchFinished() instead. See https://www.drupal.org/node/3589759', E_USER_DEPRECATED);
  \Drupal::moduleHandler()->loadInclude('locale', 'inc', 'locale.bulk');
  if ($success) {
    \Drupal::state()->set('locale.translation_last_checked', \Drupal::time()->getRequestTime());
  }
  locale_translate_batch_finished($success, $results);
  \Drupal::service(LocaleFetch::class)->batchFinished($success, $results);
}

/**
+90 −274

File changed.

Preview size limit exceeded, changes collapsed.

+27 −17
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Utility\ProjectInfo;
use Drupal\locale\LocaleDefaultOptions;
use Drupal\locale\LocaleFetch;
use Drupal\locale\LocaleProjectChecker;
use Drupal\locale\LocaleProjectRepository;

@@ -188,11 +189,16 @@ function locale_translation_check_projects($projects = [], $langcodes = []): voi
 *   Array of project names to check. Defaults to all translatable projects.
 * @param array $langcodes
 *   Array of language codes. Defaults to all translatable languages.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
 *   \Drupal::service(LocaleProjectChecker::class)->triggerBatch()
 *   instead.
 *
 * @see https://www.drupal.org/node/3589759
 */
function locale_translation_check_projects_batch($projects = [], $langcodes = []): void {
  // Build and set the batch process.
  $batch = locale_translation_batch_status_build($projects, $langcodes);
  batch_set($batch);
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal::service(LocaleProjectChecker::class)->triggerBatch() instead. See https://www.drupal.org/node/3589759', E_USER_DEPRECATED);
  \Drupal::service(LocaleProjectChecker::class)->triggerBatch($projects, $langcodes);
}

/**
@@ -212,19 +218,26 @@ function locale_translation_check_projects_batch($projects = [], $langcodes = []
 *
 * @return array
 *   Batch definition array.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
 *   \Drupal::service(LocaleProjectChecker::class)->triggerBatch()
 *   instead, this now calls batch_set immediately instead of returning the
 *   array.
 *
 * @see https://www.drupal.org/node/3589759
 */
function locale_translation_batch_status_build($projects = [], $langcodes = []) {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal::service(LocaleProjectChecker::class)->triggerBatch() instead, this now calls batch_set immediately instead of returning the array. See https://www.drupal.org/node/3589759', E_USER_DEPRECATED);
  $projects = $projects ?: array_keys(\Drupal::service(LocaleProjectRepository::class)->getAll());
  $langcodes = $langcodes ?: array_keys(locale_translatable_language_list());
  $options = LocaleDefaultOptions::updateOptions();

  $operations = _locale_translation_batch_status_operations($projects, $langcodes, $options);
  $operations = \Drupal::service(LocaleFetch::class)->getStatusOperations($projects, $langcodes, $options);

  $batch_builder = (new BatchBuilder())
    ->setFile(\Drupal::service('extension.list.module')->getPath('locale') . '/locale.batch.inc')
    ->setTitle(t('Checking translations'))
    ->setErrorMessage(t('Error checking translation updates.'))
    ->setFinishCallback('locale_translation_batch_status_finished');
    ->setFinishCallback(LocaleProjectChecker::class . ':batchFinished');

  array_walk($operations, function ($operation) use ($batch_builder) {
    call_user_func_array([$batch_builder, 'addOperation'], $operation);
@@ -245,19 +258,16 @@ function locale_translation_batch_status_build($projects = [], $langcodes = [])
 *
 * @return array
 *   Array of batch operations.
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
 *   \Drupal::service(LocaleFetch::class)->getStatusOperations()
 *   instead.
 *
 * @see https://www.drupal.org/node/3589759
 */
function _locale_translation_batch_status_operations($projects, $langcodes, $options = []): array {
  $operations = [];

  foreach ($projects as $project) {
    foreach ($langcodes as $langcode) {
      // Check version and status translation sources.
      $operations[] = ['locale_translation_batch_version_check', [$project, $langcode]];
      $operations[] = ['locale_translation_batch_status_check', [$project, $langcode, $options]];
    }
  }

  return $operations;
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use \Drupal::service(LocaleFetch::class)->getStatusOperations(). See https://www.drupal.org/node/3589759', E_USER_DEPRECATED);
  return \Drupal::service(LocaleFetch::class)->getStatusOperations($projects, $langcodes, $options);
}

/**
Loading