Projet

Général

Profil

Paste
Télécharger (5,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / l10n_update / l10n_update.drush.inc @ 64ad485a

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' => 'Allowed values: overwrite, keep. 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', 'overwrite'))) {
106
    return drush_set_error('L10N_UPDATE_INVALID_MODE', dt('Invalid update mode. Valid options are keep, 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
      $languages = drush_get_option('languages');
122
      module_load_include('batch.inc', 'l10n_update');
123
      $updates = _l10n_update_prepare_updates($updates, NULL, array_keys($languages));
124
      $batch = l10n_update_batch_multiple($updates, $mode);
125
      drush_log($batch['title'], 'status');
126
      drush_log($batch['init_message'], 'status');
127
      batch_set($batch);
128
      drush_backend_batch_process();
129
    }
130
    else {
131
      drush_log(dt('All translations up to date'), 'status');
132
    }
133
  }
134
}
135

    
136
/**
137
 * Helper function to validate languages.
138
 *
139
 * Used by _validate hooks.
140
 *  1. Check other languages than english are available.
141
 *  2. Check user provided languages are valid.
142
 */
143
function _drush_l10n_update_validate_languages() {
144
  // Check there're installed other languages than english.
145
  $installed_languages = l10n_update_language_list();
146
  if (empty($installed_languages)) {
147
    return drush_set_error('L10N_UPDATE_NO_LANGUAGES', dt('No languages to update.'));
148
  }
149
  // Check provided languages are valid.
150
  $languages = drush_get_option('languages', '');
151
  $languages = array_map('trim', _convert_csv_to_array($languages));
152
  if (count($languages)) {
153
    foreach ($languages as $key => $lang) {
154
      if (!isset($installed_languages[$lang])) {
155
        drush_set_error('L10N_UPDATE_INVALID_LANGUAGE', dt('Language @lang is not installed.', array('@lang' => $lang)));
156
      }
157
      else {
158
        unset($languages[$key]);
159
        $languages[$lang] = $installed_languages[$lang];
160
      }
161
    }
162
    if (drush_get_error() != DRUSH_SUCCESS) {
163
      drush_print(dt('Available languages: @languages', array('@languages' => implode(', ', array_keys($installed_languages)))));
164
    }
165
  }
166
  else {
167
    $languages = $installed_languages;
168
  }
169
  drush_set_option('languages', $languages);
170
}
171

    
172
/**
173
 * Helper function to obtain $updates.
174
 *
175
 * @return $updates array or NULL.
176
 */
177
function _drush_l10n_update_get_updates() {
178
  $projects = l10n_update_get_projects();
179
  if ($projects) {
180
    $history = l10n_update_get_history();
181
    drush_log(dt('Fetching update information for all projects / all languages.'), 'status');
182
    module_load_include('check.inc', 'l10n_update');
183
    $available = l10n_update_available_releases();
184
    $updates = l10n_update_build_updates($history, $available);
185
    return $updates;
186
  }
187
  else {
188
    drush_log(dt('No projects or languages to update.'), 'ok');
189
  }
190
}