l10n_update.fetch.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * @file
  4. * The API for download and import of translations.
  5. */
  6. /**
  7. * Load the common translation API.
  8. */
  9. // @todo Combine functions differently in files to avoid unnecessary includes.
  10. // Follow-up issue https://www.drupal.org/node/1834298
  11. require_once __DIR__ . '/l10n_update.translation.inc';
  12. /**
  13. * Builds a batch to check, download and import project translations.
  14. *
  15. * @param array $projects
  16. * Array of project names for which to update the translations. Defaults to
  17. * all translatable projects.
  18. * @param array $langcodes
  19. * Array of language codes. Defaults to all translatable languages.
  20. * @param array $options
  21. * Array of import options. See locale_translate_batch_import_files().
  22. *
  23. * @return array
  24. * Batch definition array.
  25. */
  26. function l10n_update_batch_update_build($projects = array(), $langcodes = array(), $options = array()) {
  27. module_load_include('compare.inc', 'l10n_update');
  28. $projects = $projects ? $projects : array_keys(l10n_update_get_projects());
  29. $langcodes = $langcodes ? $langcodes : array_keys(l10n_update_translatable_language_list());
  30. $status_options = $options;
  31. $status_options['finish_feedback'] = FALSE;
  32. // Check status of local and remote translation files.
  33. $operations = _l10n_update_batch_status_operations($projects, $langcodes, $status_options);
  34. // Download and import translations.
  35. $operations = array_merge($operations, _l10n_update_fetch_operations($projects, $langcodes, $options));
  36. $batch = array(
  37. 'operations' => $operations,
  38. 'title' => t('Updating translations'),
  39. 'progress_message' => '',
  40. 'error_message' => t('Error importing translation files'),
  41. 'finished' => 'l10n_update_batch_fetch_finished',
  42. 'file' => drupal_get_path('module', 'l10n_update') . '/l10n_update.batch.inc',
  43. );
  44. return $batch;
  45. }
  46. /**
  47. * Builds a batch to download and import project translations.
  48. *
  49. * @param array $projects
  50. * Array of project names for which to check the state of translation files.
  51. * Defaults to all translatable projects.
  52. * @param array $langcodes
  53. * Array of language codes. Defaults to all translatable languages.
  54. * @param array $options
  55. * Array of import options. See l10n_update_batch_import_files().
  56. *
  57. * @return array
  58. * Batch definition array.
  59. */
  60. function l10n_update_batch_fetch_build($projects = array(), $langcodes = array(), $options = array()) {
  61. $projects = $projects ? $projects : array_keys(l10n_update_get_projects());
  62. $langcodes = $langcodes ? $langcodes : array_keys(l10n_update_translatable_language_list());
  63. $batch = array(
  64. 'operations' => _l10n_update_fetch_operations($projects, $langcodes, $options),
  65. 'title' => t('Updating translations.'),
  66. 'progress_message' => '',
  67. 'error_message' => t('Error importing translation files'),
  68. 'finished' => 'l10n_update_batch_fetch_finished',
  69. 'file' => drupal_get_path('module', 'l10n_update') . '/l10n_update.batch.inc',
  70. );
  71. return $batch;
  72. }
  73. /**
  74. * Helper function to construct the batch operations to fetch translations.
  75. *
  76. * @param array $projects
  77. * Array of project names for which to check the state of translation files.
  78. * Defaults to all translatable projects.
  79. * @param array $langcodes
  80. * Array of language codes. Defaults to all translatable languages.
  81. * @param array $options
  82. * Array of import options.
  83. *
  84. * @return array
  85. * Array of batch operations.
  86. */
  87. function _l10n_update_fetch_operations(array $projects, array $langcodes, array $options) {
  88. $operations = array();
  89. // If the process is going to download files from a remote, make sure the
  90. // files can be saved in the download directory.
  91. if (!empty($projects) && l10n_update_use_remote_source()) {
  92. $directory = variable_get('l10n_update_download_store', L10N_UPDATE_DEFAULT_TRANSLATION_PATH);
  93. // Do not download files if the directory is missing or can not be created.
  94. if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
  95. drupal_set_message(t('The directory %directory does not exist or is not writable.', array('%directory' => $directory)), 'error');
  96. watchdog('file system', 'The directory %directory does not exist or is not writable.', array('%directory' => $directory), WATCHDOG_ERROR);
  97. return $operations;
  98. }
  99. l10n_update_ensure_htaccess();
  100. }
  101. foreach ($projects as $project) {
  102. foreach ($langcodes as $langcode) {
  103. if (l10n_update_use_remote_source()) {
  104. $operations[] = array(
  105. 'l10n_update_batch_fetch_download',
  106. array(
  107. $project,
  108. $langcode,
  109. ),
  110. );
  111. }
  112. $operations[] = array('l10n_update_batch_fetch_import', array(
  113. $project,
  114. $langcode,
  115. $options,
  116. ),
  117. );
  118. }
  119. }
  120. return $operations;
  121. }