Projet

Général

Profil

Paste
Télécharger (6,49 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / l10n_update / l10n_update.drush.inc @ 503b3f7b

1
<?php
2

    
3
/**
4
 * @file
5
 * Drush interface to l10n-update functionalities.
6
 */
7

    
8
/**
9
 * Implements hook_drush_command().
10
 */
11
function l10n_update_drush_command() {
12
  $commands = array();
13
  $commands['l10n-update-refresh'] = array(
14
    'description' => 'Refresh available information.',
15
  );
16
  $commands['l10n-update-status'] = array(
17
    'description' => 'Show translation status of available projects.',
18
    'options' => array(
19
      'languages' => 'Comma separated list of languages. Defaults to all available languages.',
20
    )
21
  );
22
  $commands['l10n-update'] = array(
23
    'description' => 'Update translations.',
24
    'options' => array(
25
      'languages' => 'Comma separated list of languages. Defaults to all available languages.',
26
      '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'
27
    ),
28
  );
29
  return $commands;
30
}
31

    
32
/**
33
 * Callback for command l10n-update-refresh.
34
 */
35
function drush_l10n_update_refresh() {
36
  module_load_include('admin.inc', 'l10n_update');
37

    
38
  // Fake $form_state to leverage _submit function.
39
  $form_state = array(
40
    'values' => array('op' => t('Refresh information'))
41
  );
42
  l10n_update_admin_import_form_submit(NULL, $form_state);
43
}
44

    
45
/**
46
 * Validate command l10n-update-status.
47
 */
48
function drush_l10n_update_status_validate() {
49
  _drush_l10n_update_validate_languages();
50
}
51

    
52
/**
53
 * Callback for command l10n-update-status.
54
 */
55
function drush_l10n_update_status() {
56
  $updates = _drush_l10n_update_get_updates();
57
  if (!is_null($updates)) {
58
    $languages = drush_get_option('languages');
59
    // Table header.
60
    $table = array();
61
    $header = array(dt('Project'));
62
    foreach ($languages as $lang => $language) {
63
      $header[] = $language . ' status';
64
    }
65
    $table[] = $header;
66
    // Iterate projects to obtain per language status.
67
    $projects = l10n_update_get_projects();
68
    $history = l10n_update_get_history();
69
    foreach ($projects as $name => $project) {
70
      $row = array();
71
      // Project.
72
      $title = isset($project->title) ? $project->title : $project->name;
73
      $row[] = $title . ' ' . $project->version;
74
      // Language status.
75
      foreach ($languages as $lang => $language) {
76
        $current = isset($history[$name][$lang]) ? $history[$name][$lang] : NULL;
77
        $update = isset($updates[$name][$lang]) ? $updates[$name][$lang] : NULL;
78
        if ($update) {
79
          $row[] = ($update->type == 'download') ? t('Remote update available'):t('Local update available');
80
        }
81
        elseif ($current) {
82
          $row[] = t('Up to date');
83
        }
84
        else {
85
          $row[] = t('No information');
86
        }
87
      }
88
      $table[] = $row;
89
    }
90
    drush_print_table($table, TRUE);
91
  }
92
  else {
93
    drush_log(dt('No projects or languages to update.'), 'ok');
94
  }
95
}
96

    
97
/**
98
 * Validate command l10n-update.
99
 */
100
function drush_l10n_update_validate() {
101
  _drush_l10n_update_validate_languages();
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
/**
111
 * Callback for command l10n-update.
112
 */
113
function drush_l10n_update() {
114
  $updates = _drush_l10n_update_get_updates();
115
  if (!is_null($updates)) {
116
    if (count($updates) > 0) {
117
      drush_log(dt('Found @count projects to update.', array('@count' => count($updates))), 'status');
118

    
119
      // Batch update all projects for selected languages.
120
      $mode = drush_get_option('mode', 'keep');
121

    
122
      switch ($mode) {
123
        case 'keep':
124
          $mode = LOCALE_IMPORT_KEEP;
125
          break;
126
        case 'replace':
127
          $mode = LOCALE_UPDATE_OVERRIDE_DEFAULT;
128
          break;
129
        case 'overwrite':
130
          $mode = LOCALE_IMPORT_OVERWRITE;
131
          break;
132

    
133
        default:
134
          return drush_set_error('L10N_UPDATE_INVALID_MODE', dt('Invalid update mode. Valid options are keep, overwrite.'));
135
          break;
136
      }
137

    
138
      $languages = drush_get_option('languages');
139
      module_load_include('batch.inc', 'l10n_update');
140
      $updates = _l10n_update_prepare_updates($updates, NULL, array_keys($languages));
141
      $batch = l10n_update_batch_multiple($updates, $mode);
142
      drush_log($batch['title'], 'status');
143
      drush_log($batch['init_message'], 'status');
144
      batch_set($batch);
145
      drush_backend_batch_process();
146
    }
147
    else {
148
      drush_log(dt('All translations up to date'), 'status');
149
    }
150
  }
151
}
152

    
153
/**
154
 * Helper function to validate languages.
155
 *
156
 * Used by _validate hooks.
157
 *  1. Check other languages than english are available.
158
 *  2. Check user provided languages are valid.
159
 */
160
function _drush_l10n_update_validate_languages() {
161
  // Check there're installed other languages than english.
162
  $installed_languages = l10n_update_language_list();
163
  if (empty($installed_languages)) {
164
    return drush_set_error('L10N_UPDATE_NO_LANGUAGES', dt('No languages to update.'));
165
  }
166
  // Check provided languages are valid.
167
  $languages = drush_get_option('languages', '');
168
  $languages = array_map('trim', _convert_csv_to_array($languages));
169
  if (count($languages)) {
170
    foreach ($languages as $key => $lang) {
171
      if (!isset($installed_languages[$lang])) {
172
        drush_set_error('L10N_UPDATE_INVALID_LANGUAGE', dt('Language @lang is not installed.', array('@lang' => $lang)));
173
      }
174
      else {
175
        unset($languages[$key]);
176
        $languages[$lang] = $installed_languages[$lang];
177
      }
178
    }
179
    if (drush_get_error() != DRUSH_SUCCESS) {
180
      drush_print(dt('Available languages: @languages', array('@languages' => implode(', ', array_keys($installed_languages)))));
181
    }
182
  }
183
  else {
184
    $languages = $installed_languages;
185
  }
186
  drush_set_option('languages', $languages);
187
}
188

    
189
/**
190
 * Helper function to obtain $updates.
191
 *
192
 * @return $updates array or NULL.
193
 */
194
function _drush_l10n_update_get_updates() {
195
  $projects = l10n_update_get_projects();
196
  if ($projects) {
197
    $history = l10n_update_get_history();
198
    drush_log(dt('Fetching update information for all projects / all languages.'), 'status');
199
    module_load_include('check.inc', 'l10n_update');
200
    $available = l10n_update_available_releases();
201
    $updates = l10n_update_build_updates($history, $available);
202
    return $updates;
203
  }
204
  else {
205
    drush_log(dt('No projects or languages to update.'), 'ok');
206
  }
207
}