l10n_update.drush.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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.',
  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.',
  24. 'mode' => 'Allowed values: overwrite, keep. 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('admin.inc', 'l10n_update');
  34. // Fake $form_state to leverage _submit function.
  35. $form_state = array(
  36. 'values' => array('op' => t('Refresh information'))
  37. );
  38. l10n_update_admin_import_form_submit(NULL, $form_state);
  39. }
  40. /**
  41. * Validate command l10n-update-status.
  42. */
  43. function drush_l10n_update_status_validate() {
  44. _drush_l10n_update_validate_languages();
  45. }
  46. /**
  47. * Callback for command l10n-update-status.
  48. */
  49. function drush_l10n_update_status() {
  50. $updates = _drush_l10n_update_get_updates();
  51. if (!is_null($updates)) {
  52. $languages = drush_get_option('languages');
  53. // Table header.
  54. $table = array();
  55. $header = array(dt('Project'));
  56. foreach ($languages as $lang => $language) {
  57. $header[] = $language . ' status';
  58. }
  59. $table[] = $header;
  60. // Iterate projects to obtain per language status.
  61. $projects = l10n_update_get_projects();
  62. $history = l10n_update_get_history();
  63. foreach ($projects as $name => $project) {
  64. $row = array();
  65. // Project.
  66. $title = isset($project->title) ? $project->title : $project->name;
  67. $row[] = $title . ' ' . $project->version;
  68. // Language status.
  69. foreach ($languages as $lang => $language) {
  70. $current = isset($history[$name][$lang]) ? $history[$name][$lang] : NULL;
  71. $update = isset($updates[$name][$lang]) ? $updates[$name][$lang] : NULL;
  72. if ($update) {
  73. $row[] = ($update->type == 'download') ? t('Remote update available'):t('Local update available');
  74. }
  75. elseif ($current) {
  76. $row[] = t('Up to date');
  77. }
  78. else {
  79. $row[] = t('No information');
  80. }
  81. }
  82. $table[] = $row;
  83. }
  84. drush_print_table($table, TRUE);
  85. }
  86. else {
  87. drush_log(dt('No projects or languages to update.'), 'ok');
  88. }
  89. }
  90. /**
  91. * Validate command l10n-update.
  92. */
  93. function drush_l10n_update_validate() {
  94. _drush_l10n_update_validate_languages();
  95. // Check provided update mode is valid.
  96. $mode = drush_get_option('mode', 'keep');
  97. if (!in_array($mode, array('keep', 'overwrite'))) {
  98. return drush_set_error('L10N_UPDATE_INVALID_MODE', dt('Invalid update mode. Valid options are keep, overwrite.'));
  99. }
  100. }
  101. /**
  102. * Callback for command l10n-update.
  103. */
  104. function drush_l10n_update() {
  105. $updates = _drush_l10n_update_get_updates();
  106. if (!is_null($updates)) {
  107. if (count($updates) > 0) {
  108. drush_log(dt('Found @count projects to update.', array('@count' => count($updates))), 'status');
  109. // Batch update all projects for selected languages.
  110. $mode = drush_get_option('mode', 'keep');
  111. $languages = drush_get_option('languages');
  112. module_load_include('batch.inc', 'l10n_update');
  113. $updates = _l10n_update_prepare_updates($updates, NULL, array_keys($languages));
  114. $batch = l10n_update_batch_multiple($updates, $mode);
  115. drush_log($batch['title'], 'status');
  116. drush_log($batch['init_message'], 'status');
  117. batch_set($batch);
  118. drush_backend_batch_process();
  119. }
  120. else {
  121. drush_log(dt('All translations up to date'), 'status');
  122. }
  123. }
  124. }
  125. /**
  126. * Helper function to validate languages.
  127. *
  128. * Used by _validate hooks.
  129. * 1. Check other languages than english are available.
  130. * 2. Check user provided languages are valid.
  131. */
  132. function _drush_l10n_update_validate_languages() {
  133. // Check there're installed other languages than english.
  134. $installed_languages = l10n_update_language_list();
  135. if (empty($installed_languages)) {
  136. return drush_set_error('L10N_UPDATE_NO_LANGUAGES', dt('No languages to update.'));
  137. }
  138. // Check provided languages are valid.
  139. $languages = drush_get_option('languages', '');
  140. $languages = array_map('trim', _convert_csv_to_array($languages));
  141. if (count($languages)) {
  142. foreach ($languages as $key => $lang) {
  143. if (!isset($installed_languages[$lang])) {
  144. drush_set_error('L10N_UPDATE_INVALID_LANGUAGE', dt('Language @lang is not installed.', array('@lang' => $lang)));
  145. }
  146. else {
  147. unset($languages[$key]);
  148. $languages[$lang] = $installed_languages[$lang];
  149. }
  150. }
  151. if (drush_get_error() != DRUSH_SUCCESS) {
  152. drush_print(dt('Available languages: @languages', array('@languages' => implode(', ', array_keys($installed_languages)))));
  153. }
  154. }
  155. else {
  156. $languages = $installed_languages;
  157. }
  158. drush_set_option('languages', $languages);
  159. }
  160. /**
  161. * Helper function to obtain $updates.
  162. *
  163. * @return $updates array or NULL.
  164. */
  165. function _drush_l10n_update_get_updates() {
  166. $projects = l10n_update_get_projects();
  167. if ($projects) {
  168. $history = l10n_update_get_history();
  169. drush_log(dt('Fetching update information for all projects / all languages.'), 'status');
  170. module_load_include('check.inc', 'l10n_update');
  171. $available = l10n_update_available_releases();
  172. $updates = l10n_update_build_updates($history, $available);
  173. return $updates;
  174. }
  175. else {
  176. drush_log(dt('No projects or languages to update.'), 'ok');
  177. }
  178. }