l10n_update.drush.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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' => '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('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', 'replace', 'overwrite'))) {
  98. return drush_set_error('L10N_UPDATE_INVALID_MODE', dt('Invalid update mode. Valid options are keep, replace, 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. switch ($mode) {
  112. case 'keep':
  113. $mode = LOCALE_IMPORT_KEEP;
  114. break;
  115. case 'replace':
  116. $mode = LOCALE_UPDATE_OVERRIDE_DEFAULT;
  117. break;
  118. case 'overwrite':
  119. $mode = LOCALE_IMPORT_OVERWRITE;
  120. break;
  121. default:
  122. return drush_set_error('L10N_UPDATE_INVALID_MODE', dt('Invalid update mode. Valid options are keep, overwrite.'));
  123. break;
  124. }
  125. $languages = drush_get_option('languages');
  126. module_load_include('batch.inc', 'l10n_update');
  127. $updates = _l10n_update_prepare_updates($updates, NULL, array_keys($languages));
  128. $batch = l10n_update_batch_multiple($updates, $mode);
  129. drush_log($batch['title'], 'status');
  130. drush_log($batch['init_message'], 'status');
  131. batch_set($batch);
  132. drush_backend_batch_process();
  133. }
  134. else {
  135. drush_log(dt('All translations up to date'), 'status');
  136. }
  137. }
  138. }
  139. /**
  140. * Helper function to validate languages.
  141. *
  142. * Used by _validate hooks.
  143. * 1. Check other languages than english are available.
  144. * 2. Check user provided languages are valid.
  145. */
  146. function _drush_l10n_update_validate_languages() {
  147. // Check there're installed other languages than english.
  148. $installed_languages = l10n_update_language_list();
  149. if (empty($installed_languages)) {
  150. return drush_set_error('L10N_UPDATE_NO_LANGUAGES', dt('No languages to update.'));
  151. }
  152. // Check provided languages are valid.
  153. $languages = drush_get_option('languages', '');
  154. $languages = array_map('trim', _convert_csv_to_array($languages));
  155. if (count($languages)) {
  156. foreach ($languages as $key => $lang) {
  157. if (!isset($installed_languages[$lang])) {
  158. drush_set_error('L10N_UPDATE_INVALID_LANGUAGE', dt('Language @lang is not installed.', array('@lang' => $lang)));
  159. }
  160. else {
  161. unset($languages[$key]);
  162. $languages[$lang] = $installed_languages[$lang];
  163. }
  164. }
  165. if (drush_get_error() != DRUSH_SUCCESS) {
  166. drush_print(dt('Available languages: @languages', array('@languages' => implode(', ', array_keys($installed_languages)))));
  167. }
  168. }
  169. else {
  170. $languages = $installed_languages;
  171. }
  172. drush_set_option('languages', $languages);
  173. }
  174. /**
  175. * Helper function to obtain $updates.
  176. *
  177. * @return $updates array or NULL.
  178. */
  179. function _drush_l10n_update_get_updates() {
  180. $projects = l10n_update_get_projects();
  181. if ($projects) {
  182. $history = l10n_update_get_history();
  183. drush_log(dt('Fetching update information for all projects / all languages.'), 'status');
  184. module_load_include('check.inc', 'l10n_update');
  185. $available = l10n_update_available_releases();
  186. $updates = l10n_update_build_updates($history, $available);
  187. return $updates;
  188. }
  189. else {
  190. drush_log(dt('No projects or languages to update.'), 'ok');
  191. }
  192. }