l10n_update.drush.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 $language) {
  62. $header[] = $language->name;
  63. }
  64. $table[] = $header;
  65. // Iterate projects to obtain per language status.
  66. foreach ($status as $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. }
  142. $languages = array_keys(drush_get_option('languages'));
  143. // Get translation status of the projects, download and update translations.
  144. $batch = l10n_update_batch_update_build(array_keys($updates['projects']), $languages, $options);
  145. drush_log($batch['title'], 'status');
  146. drush_log($batch['init_message'], 'status');
  147. batch_set($batch);
  148. drush_backend_batch_process();
  149. }
  150. else {
  151. drush_log(dt('All project translations up to date'), 'status');
  152. }
  153. }
  154. /**
  155. * Helper function to validate languages.
  156. *
  157. * Used by _validate hooks.
  158. * 1. Check other languages than english are available.
  159. * 2. Check user provided languages are valid.
  160. */
  161. function _drush_l10n_update_validate_languages() {
  162. // Check if there are installed languages other than english.
  163. $installed_languages = l10n_update_translatable_language_list();
  164. // Indicate that there's nothing to do, only show a warning.
  165. if (empty($installed_languages)) {
  166. drush_log(dt('No languages to update.'), 'warning');
  167. return FALSE;
  168. }
  169. // Check provided languages are valid.
  170. $languages = drush_get_option('languages', '');
  171. $languages = array_map('trim', _convert_csv_to_array($languages));
  172. if (count($languages)) {
  173. foreach ($languages as $key => $langcode) {
  174. if (!isset($installed_languages[$langcode])) {
  175. if (is_numeric($langcode)) {
  176. drush_set_error('L10N_UPDATE_INVALID_LANGUAGE', dt('Invalid language "@langcode". Use for example: --languages="nl, fr, de"', array('@langcode' => $langcode)));
  177. }
  178. else {
  179. drush_set_error('L10N_UPDATE_INVALID_LANGUAGE', dt('Language "@langcode" is not installed.', array('@langcode' => $langcode)));
  180. }
  181. }
  182. else {
  183. unset($languages[$key]);
  184. $languages[$langcode] = $installed_languages[$langcode];
  185. }
  186. }
  187. if (drush_get_error() != DRUSH_SUCCESS) {
  188. drush_print(dt('Available languages: @languages', array('@languages' => implode(', ', array_keys($installed_languages)))));
  189. return FALSE;
  190. }
  191. }
  192. else {
  193. $languages = $installed_languages;
  194. }
  195. drush_set_option('languages', $languages);
  196. return TRUE;
  197. }
  198. /**
  199. * Helper function to obtain $updates.
  200. *
  201. * @return array|null
  202. * Array of projects and languages to be updated. Array keys:
  203. * - projects: Associative array of projects to be updated. Key: Project name.
  204. * Value: Project info array.
  205. * - languages: Associative array of languages to be updated. Key: Language
  206. * code. Value: Project info array.
  207. */
  208. function _drush_l10n_update_get_updates() {
  209. $updates = array();
  210. $languages = l10n_update_translatable_language_list();
  211. $status = l10n_update_get_status();
  212. drush_log(dt('Fetching update information for all projects / all languages.'), 'status');
  213. // Prepare information about projects which have available translation
  214. // updates.
  215. if ($languages && $status) {
  216. foreach ($status as $project) {
  217. foreach ($project as $langcode => $project_info) {
  218. // Translation update found for this project-language combination.
  219. if ($project_info->type && ($project_info->type == L10N_UPDATE_LOCAL || $project_info->type == L10N_UPDATE_REMOTE)) {
  220. $updates['projects'][$project_info->name] = $project_info;
  221. $updates['languages'][$langcode] = $project_info;
  222. }
  223. }
  224. }
  225. if ($updates) {
  226. return $updates;
  227. }
  228. else {
  229. drush_log(dt('All project translations up to date.'), 'status');
  230. }
  231. }
  232. else {
  233. if (empty($languages)) {
  234. drush_log(dt('No languages to update.'), 'warning');
  235. }
  236. else {
  237. drush_log(dt('No translation status available. Run drush l10n-update-status first.'), 'warning');
  238. }
  239. }
  240. }