l10n_update.batch.inc 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * @file
  4. * Batch process to check the availability of remote or local po files.
  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. * Batch operation callback: Check status of a remote and local po file.
  14. *
  15. * Checks the presence and creation time po translation files in located at
  16. * remote server location and local file system.
  17. *
  18. * @param string $project
  19. * Machine name of the project for which to check the translation status.
  20. * @param string $langcode
  21. * Language code of the language for which to check the translation.
  22. * @param array $options
  23. * Optional, an array with options that can have the following elements:
  24. * - 'finish_feedback': Whether or not to give feedback to the user when the
  25. * batch is finished. Optional, defaults to TRUE.
  26. * - 'use_remote': Whether or not to check the remote translation file.
  27. * Optional, defaults to TRUE.
  28. * @param array $context
  29. * The batch context.
  30. */
  31. function l10n_update_batch_status_check($project, $langcode, $options = array(), &$context) {
  32. $failure = $checked = FALSE;
  33. $options += array(
  34. 'finish_feedback' => TRUE,
  35. 'use_remote' => TRUE,
  36. );
  37. $source = l10n_update_get_status(array($project), array($langcode));
  38. $source = $source[$project][$langcode];
  39. // Check the status of local translation files.
  40. if (isset($source->files[L10N_UPDATE_LOCAL])) {
  41. if ($file = l10n_update_source_check_file($source)) {
  42. l10n_update_status_save($source->name, $source->langcode, L10N_UPDATE_LOCAL, $file);
  43. }
  44. $checked = TRUE;
  45. }
  46. // Check the status of remote translation files.
  47. if ($options['use_remote'] && isset($source->files[L10N_UPDATE_REMOTE])) {
  48. $remote_file = $source->files[L10N_UPDATE_REMOTE];
  49. module_load_include('http.inc', 'l10n_update');
  50. if ($result = l10n_update_http_check($remote_file->uri)) {
  51. // Update the file object with the result data. In case of a redirect we
  52. // store the resulting uri.
  53. if (!empty($result->updated)) {
  54. $remote_file->uri = isset($result->redirect_url) ? $result->redirect_url : $remote_file->uri;
  55. $remote_file->timestamp = $result->updated;
  56. l10n_update_status_save($source->name, $source->langcode, L10N_UPDATE_REMOTE, $remote_file);
  57. }
  58. // @todo What to do with when the file is not found (404)? To prevent
  59. // re-checking within the TTL (1day, 1week) we can set a last_checked
  60. // timestamp or cache the result.
  61. $checked = TRUE;
  62. }
  63. else {
  64. $failure = TRUE;
  65. }
  66. }
  67. // Provide user feedback and record success or failure for reporting at the
  68. // end of the batch.
  69. if ($options['finish_feedback'] && $checked) {
  70. $context['results']['files'][] = $source->name;
  71. }
  72. if ($failure && !$checked) {
  73. $context['results']['failed_files'][] = $source->name;
  74. }
  75. $context['message'] = t('Checked translation for %project.', array('%project' => $source->project));
  76. }
  77. /**
  78. * Batch finished callback: Set result message.
  79. *
  80. * @param bool $success
  81. * TRUE if batch successfully completed.
  82. * @param array $results
  83. * Batch results.
  84. */
  85. function l10n_update_batch_status_finished($success, array $results) {
  86. if ($success) {
  87. if (isset($results['failed_files'])) {
  88. if (module_exists('dblog')) {
  89. $message = format_plural(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.', array('@url' => url('admin/reports/dblog')));
  90. }
  91. else {
  92. $message = format_plural(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.');
  93. }
  94. drupal_set_message($message, 'error');
  95. }
  96. if (isset($results['files'])) {
  97. drupal_set_message(format_plural(
  98. count($results['files']),
  99. 'Checked available interface translation updates for one project.',
  100. 'Checked available interface translation updates for @count projects.'
  101. ));
  102. }
  103. if (!isset($results['failed_files']) && !isset($results['files'])) {
  104. drupal_set_message(t('Nothing to check.'));
  105. }
  106. variable_set('l10n_update_last_check', REQUEST_TIME);
  107. }
  108. else {
  109. drupal_set_message(t('An error occurred trying to check available interface translation updates.'), 'error');
  110. }
  111. }
  112. /**
  113. * Batch operation: Download a remote translation file.
  114. *
  115. * Downloads a remote gettext file into the translations directory. When
  116. * successfully the translation status is updated.
  117. *
  118. * @param string $project
  119. * Name of the translatable project.
  120. * @param string $langcode
  121. * Language code.
  122. * @param array $context
  123. * The batch context.
  124. *
  125. * @see l10n_update_batch_fetch_import()
  126. */
  127. function l10n_update_batch_fetch_download($project, $langcode, &$context) {
  128. $sources = l10n_update_get_status(array($project), array($langcode));
  129. if (isset($sources[$project][$langcode])) {
  130. $source = $sources[$project][$langcode];
  131. if (isset($source->type) && $source->type == L10N_UPDATE_REMOTE) {
  132. if ($file = l10n_update_download_source($source->files[L10N_UPDATE_REMOTE], 'translations://')) {
  133. $context['message'] = t('Downloaded translation for %project.', array('%project' => $source->project));
  134. l10n_update_status_save($source->name, $source->langcode, L10N_UPDATE_LOCAL, $file);
  135. }
  136. else {
  137. $context['results']['failed_files'][] = $source->files[L10N_UPDATE_REMOTE];
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. * Batch process: Import translation file.
  144. *
  145. * Imports a gettext file from the translation directory. When successfully the
  146. * translation status is updated.
  147. *
  148. * @param string $project
  149. * Name of the translatable project.
  150. * @param string $langcode
  151. * Language code.
  152. * @param array $options
  153. * Array of import options.
  154. * @param array $context
  155. * The batch context.
  156. *
  157. * @see l10n_update_batch_import_files()
  158. * @see l10n_update_batch_fetch_download()
  159. */
  160. function l10n_update_batch_fetch_import($project, $langcode, array $options, &$context) {
  161. $sources = l10n_update_get_status(array($project), array($langcode));
  162. if (isset($sources[$project][$langcode])) {
  163. $source = $sources[$project][$langcode];
  164. if (isset($source->type)) {
  165. if ($source->type == L10N_UPDATE_REMOTE || $source->type == L10N_UPDATE_LOCAL) {
  166. $file = $source->files[L10N_UPDATE_LOCAL];
  167. module_load_include('bulk.inc', 'l10n_update');
  168. $options += array(
  169. 'message' => t('Importing translation for %project.', array('%project' => $source->project)),
  170. );
  171. // Import the translation file. For large files the batch operations is
  172. // progressive and will be called repeatedly until finished.
  173. l10n_update_batch_import($file, $options, $context);
  174. // The import is finished.
  175. if (isset($context['finished']) && $context['finished'] == 1) {
  176. // The import is successful.
  177. if (isset($context['results']['files'][$file->uri])) {
  178. $context['message'] = t('Imported translation for %project.', array('%project' => $source->project));
  179. // Save the data of imported source into the {l10n_update_file}
  180. // table and update the current translation status.
  181. l10n_update_status_save($project, $langcode, L10N_UPDATE_CURRENT, $source->files[L10N_UPDATE_LOCAL]);
  182. }
  183. }
  184. }
  185. }
  186. }
  187. }
  188. /**
  189. * Batch finished callback: Set result message.
  190. *
  191. * @param bool $success
  192. * TRUE if batch successfully completed.
  193. * @param array $results
  194. * Batch results.
  195. */
  196. function l10n_update_batch_fetch_finished($success, array $results) {
  197. module_load_include('bulk.inc', 'l10n_update');
  198. if ($success) {
  199. variable_set('l10n_update_last_check', REQUEST_TIME);
  200. }
  201. l10n_update_batch_finished($success, $results);
  202. }
  203. /**
  204. * Downloads a translation file from a remote server.
  205. *
  206. * @param object $source_file
  207. * Source file object with at least:
  208. * - "uri": uri to download the file from.
  209. * - "project": Project name.
  210. * - "langcode": Translation language.
  211. * - "version": Project version.
  212. * - "filename": File name.
  213. * @param string $directory
  214. * Directory where the downloaded file will be saved. Defaults to the
  215. * temporary file path.
  216. *
  217. * @return object|bool
  218. * File object if download was successful. FALSE on failure.
  219. */
  220. function l10n_update_download_source($source_file, $directory = 'temporary://') {
  221. if ($uri = system_retrieve_file($source_file->uri, $directory, FALSE, FILE_EXISTS_REPLACE)) {
  222. $file = clone $source_file;
  223. $file->type = L10N_UPDATE_LOCAL;
  224. $file->uri = $uri;
  225. $file->directory = $directory;
  226. $file->timestamp = filemtime($uri);
  227. return $file;
  228. }
  229. watchdog('l10n_update', 'Unable to download translation file @uri.', array('@uri' => $source_file->uri), WATCHDOG_ERROR);
  230. return FALSE;
  231. }