l10n_update.drush.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * @file
  4. * Drush interface to l10n-update functionalities.
  5. */
  6. /**
  7. * Implements hook_drush_command().
  8. */
  9. function l10n_update_drush_command() {
  10. $commands = array();
  11. $commands['l10n-update-refresh'] = array(
  12. 'description' => 'Refresh available information.',
  13. );
  14. $commands['l10n-update-status'] = array(
  15. 'description' => 'Show translation status of available projects.',
  16. 'options' => array(
  17. 'languages' => 'Comma separated list of languages. Defaults to all available languages. Example: --languages="nl, fr, de"',
  18. )
  19. );
  20. $commands['l10n-update'] = array(
  21. 'description' => 'Update translations.',
  22. 'options' => array(
  23. 'languages' => 'Comma separated list of languages. Defaults to all available languages. Example: --languages="nl, fr, de"',
  24. 'mode' => 'Determine if existing translations are overwitten during import. Use "overwrite" to overwrite any existing translation, "replace" to replace previously imported translations but not overwrite edited strings, "keep" to keep any existing translation and only add new translations. Default value: "keep"'
  25. ),
  26. );
  27. return $commands;
  28. }
  29. /**
  30. * Callback for command l10n-update-refresh.
  31. */
  32. function drush_l10n_update_refresh() {
  33. module_load_include('compare.inc', 'l10n_update');
  34. // Check the translation status of all translatable projects in all languages.
  35. // First we clear the cached list of projects. Although not strictly
  36. // necessary, this is helpful in case the project list is out of sync.
  37. l10n_update_flush_projects();
  38. l10n_update_check_projects();
  39. // Execute a batch if required. A batch is only used when remote files
  40. // are checked.
  41. if (batch_get()) {
  42. drush_backend_batch_process();
  43. }
  44. }
  45. /**
  46. * Validate command l10n-update-status.
  47. */
  48. function drush_l10n_update_status_validate() {
  49. return _drush_l10n_update_validate_languages();
  50. }
  51. /**
  52. * Callback for command l10n-update-status.
  53. */
  54. function drush_l10n_update_status() {
  55. $status = l10n_update_get_status();
  56. if (!empty($status)) {
  57. $languages = drush_get_option('languages');
  58. // Build table header.
  59. $table = array();
  60. $header = array(dt('Project'));
  61. foreach ($languages as $langcode => $language) {
  62. $header[] = $language->name;
  63. }
  64. $table[] = $header;
  65. // Iterate projects to obtain per language status.
  66. foreach ($status as $name => $project) {
  67. $row = array();
  68. // First column: Project name & version number.
  69. $project_details = reset($project);
  70. $title = isset($project_details->title) ? $project_details->title : $project_details->name;
  71. $row[] = dt('@project (@version)', array('@project' => $title, '@version' => $project_details->version));
  72. // Other columns: Status per language.
  73. foreach ($languages as $langcode => $language) {
  74. $current = $project[$langcode]->type == L10N_UPDATE_CURRENT;
  75. $local_update = $project[$langcode]->type == L10N_UPDATE_LOCAL;
  76. $remote_update = $project[$langcode]->type == L10N_UPDATE_REMOTE;
  77. if ($local_update || $remote_update) {
  78. $row[] = $remote_update ? dt('Remote update available') : dt('Local update available');
  79. }
  80. elseif ($current) {
  81. $row[] = dt('Up to date');
  82. }
  83. else {
  84. $row[] = dt('No info');
  85. }
  86. }
  87. $table[] = $row;
  88. }
  89. drush_print_table($table, TRUE);
  90. }
  91. else {
  92. drush_log(dt('No languages to update.'), 'warning');
  93. }
  94. }
  95. /**
  96. * Validate command l10n-update.
  97. */
  98. function drush_l10n_update_validate() {
  99. $lang_validation = _drush_l10n_update_validate_languages();
  100. if ($lang_validation == FALSE) {
  101. return FALSE;
  102. }
  103. // Check provided update mode is valid.
  104. $mode = drush_get_option('mode', 'keep');
  105. if (!in_array($mode, array('keep', 'replace', 'overwrite'))) {
  106. return drush_set_error('L10N_UPDATE_INVALID_MODE', dt('Invalid update mode. Valid options are keep, replace, overwrite.'));
  107. }
  108. }
  109. /**
  110. * Callback for command l10n-update.
  111. */
  112. function drush_l10n_update() {
  113. module_load_include('fetch.inc', 'l10n_update');
  114. $updates = _drush_l10n_update_get_updates();
  115. if ($updates['projects']) {
  116. drush_log(dt('Found @count projects to update.', array('@count' => count($updates['projects']))), 'status');
  117. // Batch update all projects for selected languages.
  118. $mode = drush_get_option('mode', 'keep');
  119. $options = _l10n_update_default_update_options();
  120. switch ($mode) {
  121. case 'keep':
  122. $options['overwrite_options'] = array(
  123. 'not_customized' => FALSE,
  124. 'customized' => FALSE,
  125. );
  126. break;
  127. case 'replace':
  128. $options['overwrite_options'] = array(
  129. 'not_customized' => TRUE,
  130. 'customized' => FALSE,
  131. );
  132. break;
  133. case 'overwrite':
  134. $options['overwrite_options'] = array(
  135. 'not_customized' => TRUE,
  136. 'customized' => TRUE,
  137. );
  138. break;
  139. default:
  140. return drush_set_error('L10N_UPDATE_INVALID_MODE', dt('Invalid update mode. Valid options are keep, overwrite.'));
  141. break;
  142. }
  143. $languages = array_keys(drush_get_option('languages'));
  144. // Get translation status of the projects, download and update translations.
  145. $batch = l10n_update_batch_update_build(array(), $languages, $options);
  146. drush_log($batch['title'], 'status');
  147. drush_log($batch['init_message'], 'status');
  148. batch_set($batch);
  149. drush_backend_batch_process();
  150. }
  151. else {
  152. drush_log(dt('All project translations up to date'), 'status');
  153. }
  154. }
  155. /**
  156. * Helper function to validate languages.
  157. *
  158. * Used by _validate hooks.
  159. * 1. Check other languages than english are available.
  160. * 2. Check user provided languages are valid.
  161. */
  162. function _drush_l10n_update_validate_languages() {
  163. // Check if there are installed languages other than english.
  164. $installed_languages = l10n_update_translatable_language_list();
  165. // Indicate that there's nothing to do, only show a warning.
  166. if (empty($installed_languages)) {
  167. drush_log(dt('No languages to update.'), 'warning');
  168. return FALSE;
  169. }
  170. // Check provided languages are valid.
  171. $languages = drush_get_option('languages', '');
  172. $languages = array_map('trim', _convert_csv_to_array($languages));
  173. if (count($languages)) {
  174. foreach ($languages as $key => $langcode) {
  175. if (!isset($installed_languages[$langcode])) {
  176. if (is_numeric($langcode)) {
  177. drush_set_error('L10N_UPDATE_INVALID_LANGUAGE', dt('Invalid language "@langcode". Use for example: --languages="nl, fr, de"', array('@langcode' => $langcode)));
  178. }
  179. else {
  180. drush_set_error('L10N_UPDATE_INVALID_LANGUAGE', dt('Language "@langcode" is not installed.', array('@langcode' => $langcode)));
  181. }
  182. }
  183. else {
  184. unset($languages[$key]);
  185. $languages[$langcode] = $installed_languages[$langcode];
  186. }
  187. }
  188. if (drush_get_error() != DRUSH_SUCCESS) {
  189. drush_print(dt('Available languages: @languages', array('@languages' => implode(', ', array_keys($installed_languages)))));
  190. return FALSE;
  191. }
  192. }
  193. else {
  194. $languages = $installed_languages;
  195. }
  196. drush_set_option('languages', $languages);
  197. return TRUE;
  198. }
  199. /**
  200. * Helper function to obtain $updates.
  201. *
  202. * @return $updates array or NULL.
  203. */
  204. function _drush_l10n_update_get_updates() {
  205. $updates = array();
  206. $languages = l10n_update_translatable_language_list();
  207. $status = l10n_update_get_status();
  208. drush_log(dt('Fetching update information for all projects / all languages.'), 'status');
  209. // Prepare information about projects which have available translation
  210. // updates.
  211. if ($languages && $status) {
  212. foreach ($status as $project) {
  213. foreach ($project as $langcode => $project_info) {
  214. // Translation update found for this project-language combination.
  215. if ($project_info->type && ($project_info->type == L10N_UPDATE_LOCAL || $project_info->type == L10N_UPDATE_REMOTE)) {
  216. $updates['projects'][$project_info->name] = $project_info;
  217. $updates['languages'][$langcode] = $project_info;
  218. }
  219. }
  220. }
  221. }
  222. if ($updates) {
  223. return $updates;
  224. }
  225. else {
  226. drush_log(dt('No languages to update.'), 'warning');
  227. }
  228. }