1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Reusable API for creating and running l10n update batches.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
// module_load_include will not work in batch.
|
9 |
|
|
include_once 'l10n_update.check.inc';
|
10 |
|
|
|
11 |
|
|
/**
|
12 |
|
|
* Create a batch to just download files.
|
13 |
|
|
*
|
14 |
|
|
* @param $updates
|
15 |
|
|
* Translations sources to be downloaded.
|
16 |
|
|
* Note: All update sources must have a 'fileurl'.
|
17 |
|
|
* @return array
|
18 |
|
|
* A batch definition for this download.
|
19 |
|
|
*/
|
20 |
|
|
function l10n_update_batch_download($updates) {
|
21 |
|
|
foreach ($updates as $update) {
|
22 |
|
|
$id = $update->filename;
|
23 |
|
|
$operations[] = array('_l10n_update_batch_download', array($id, $update));
|
24 |
|
|
}
|
25 |
|
|
return _l10n_update_create_batch($operations);
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
/**
|
29 |
|
|
* Create a batch to just import files.
|
30 |
|
|
*
|
31 |
|
|
* All update sources must have a 'uri'.
|
32 |
|
|
*
|
33 |
|
|
* @param $updates
|
34 |
|
|
* Translations sources to be imported.
|
35 |
|
|
* Note: All update sources must have a 'fileurl'.
|
36 |
|
|
* @param $import_mode
|
37 |
|
|
* Import mode. How to treat existing and modified translations.
|
38 |
|
|
* @return array
|
39 |
|
|
* A batch definition for this import.
|
40 |
|
|
*/
|
41 |
|
|
function l10n_update_batch_import($updates, $import_mode) {
|
42 |
|
|
foreach ($updates as $update) {
|
43 |
|
|
$id = $update->filename;
|
44 |
|
|
$operations[] = array('_l10n_update_batch_import', array($id, $update, $import_mode));
|
45 |
|
|
}
|
46 |
|
|
return _l10n_update_create_batch($operations);
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* Create a big batch for multiple projects and languages.
|
51 |
|
|
*
|
52 |
|
|
* @param $updates
|
53 |
|
|
* Array of update sources to be run.
|
54 |
|
|
* @param $mode
|
55 |
|
|
* Import mode. How to treat existing and modified translations.
|
56 |
|
|
* @return array
|
57 |
|
|
*/
|
58 |
|
|
function l10n_update_batch_multiple($updates, $import_mode) {
|
59 |
|
|
foreach ($updates as $update) {
|
60 |
|
|
$id = $update->filename;
|
61 |
|
|
if ($update->type == 'download') {
|
62 |
|
|
$operations[] = array('_l10n_update_batch_download', array($id, $update));
|
63 |
|
|
$operations[] = array('_l10n_update_batch_import', array($id, NULL, $import_mode));
|
64 |
|
|
}
|
65 |
|
|
else {
|
66 |
|
|
$operations[] = array('_l10n_update_batch_import', array($id, $update, $import_mode));
|
67 |
|
|
}
|
68 |
|
|
// This one takes always parameters from results.
|
69 |
|
|
$operations[] = array('_l10n_update_batch_history', array($id));
|
70 |
|
|
}
|
71 |
|
|
if (!empty($operations)) {
|
72 |
|
|
return _l10n_update_create_batch($operations);
|
73 |
|
|
}
|
74 |
|
|
}
|
75 |
|
|
|
76 |
|
|
/**
|
77 |
|
|
* Create batch stub for this module.
|
78 |
|
|
*
|
79 |
|
|
* @param $operations
|
80 |
|
|
* Operations to perform in this batch.
|
81 |
|
|
* @return array
|
82 |
|
|
* A batch definition:
|
83 |
|
|
* - 'operations': Batch operations
|
84 |
|
|
* - 'title': Batch title.
|
85 |
|
|
* - 'init_message': Initial batch UI message.
|
86 |
|
|
* - 'error_message': Batch error message.
|
87 |
|
|
* - 'file': File containing callback function.
|
88 |
|
|
* - 'finished': Batch completed callback function.
|
89 |
|
|
*/
|
90 |
|
|
function _l10n_update_create_batch($operations = array()) {
|
91 |
|
|
$t = get_t();
|
92 |
|
|
$batch = array(
|
93 |
|
|
'operations' => $operations,
|
94 |
|
|
'title' => $t('Updating translation.'),
|
95 |
|
|
'init_message' => $t('Downloading and importing files.'),
|
96 |
|
|
'error_message' => $t('Error importing interface translations'),
|
97 |
|
|
'file' => drupal_get_path('module', 'l10n_update') . '/l10n_update.batch.inc',
|
98 |
|
|
'finished' => '_l10n_update_batch_finished',
|
99 |
|
|
);
|
100 |
|
|
return $batch;
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
/**
|
104 |
|
|
* Batch process: Download a file.
|
105 |
|
|
*
|
106 |
|
|
* @param $id
|
107 |
|
|
* Batch id to identify batch results.
|
108 |
|
|
* Result of this batch function are stored in $context['result']
|
109 |
|
|
* identified by this $id.
|
110 |
|
|
* @param $file
|
111 |
|
|
* File to be downloaded.
|
112 |
|
|
* @param $context
|
113 |
|
|
* Batch context array.
|
114 |
|
|
*/
|
115 |
|
|
function _l10n_update_batch_download($id, $file, &$context) {
|
116 |
|
|
$t = get_t();
|
117 |
|
|
if (l10n_update_source_download($file)) {
|
118 |
|
|
$context['message'] = $t('Importing: %name.', array('%name' => $file->filename));
|
119 |
|
|
$context['results'][$id] = array('file' => $file);
|
120 |
|
|
}
|
121 |
|
|
else {
|
122 |
|
|
$context['results'][$id] = array('file' => $file, 'fail' => TRUE);
|
123 |
|
|
}
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
/**
|
127 |
|
|
* Batch process: Update the download history table.
|
128 |
|
|
*
|
129 |
|
|
* @param $id
|
130 |
|
|
* Batch id to identify batch results.
|
131 |
|
|
* Result of this batch function are stored in $context['result']
|
132 |
|
|
* identified by this $id.
|
133 |
|
|
* @param $context
|
134 |
|
|
* Batch context array.
|
135 |
|
|
*/
|
136 |
|
|
function _l10n_update_batch_history($id, &$context) {
|
137 |
|
|
$t = get_t();
|
138 |
|
|
// The batch import is performed in a number of steps. History update is
|
139 |
|
|
// always the last step. The details of the downloaded/imported file are
|
140 |
|
|
// stored in $context['results'] array.
|
141 |
|
|
if (isset($context['results'][$id]['file']) && !isset($context['results'][$id]['fail'])) {
|
142 |
|
|
$file = $context['results'][$id]['file'];
|
143 |
|
|
l10n_update_source_history($file);
|
144 |
|
|
$context['message'] = $t('Imported: %name.', array('%name' => $file->filename));
|
145 |
|
|
}
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
/**
|
149 |
|
|
* Batch process: Import translation file.
|
150 |
|
|
*
|
151 |
|
|
* This takes a file parameter or continues from previous batch
|
152 |
|
|
* which should have downloaded a file.
|
153 |
|
|
*
|
154 |
|
|
* @param $id
|
155 |
|
|
* Batch id to identify batch results.
|
156 |
|
|
* Result of this batch function are stored in $context['result']
|
157 |
|
|
* identified by this $id.
|
158 |
|
|
* @param $file
|
159 |
|
|
* File to be imported. If empty, the file will be taken from $context['results'].
|
160 |
|
|
* @param $mode
|
161 |
|
|
* Import mode. How to treat existing and modified translations.
|
162 |
|
|
* @param $context
|
163 |
|
|
* Batch context array.
|
164 |
|
|
*/
|
165 |
|
|
function _l10n_update_batch_import($id, $file, $mode, &$context) {
|
166 |
|
|
$t = get_t();
|
167 |
|
|
// The batch import is performed in two or three steps.
|
168 |
|
|
// If import is performed after file download the file details of the download
|
169 |
|
|
// are used which are stored in the $context['results'] array.
|
170 |
|
|
// If a locally stored file is imported, the file details are placed in $file.
|
171 |
|
|
if (empty($file) && isset($context['results'][$id]['file']) && !isset($context['results'][$id]['fail'])) {
|
172 |
|
|
$file = $context['results'][$id]['file'];
|
173 |
|
|
}
|
174 |
|
|
if ($file) {
|
175 |
503b3f7b
|
Assos Assos
|
// Create a result if none exists yet.
|
176 |
|
|
if (!isset($context['results'][$id])) {
|
177 |
|
|
$context['results'][$id] = array();
|
178 |
|
|
}
|
179 |
85ad3d82
|
Assos Assos
|
if ($import_result = l10n_update_source_import($file, $mode)) {
|
180 |
|
|
$context['message'] = $t('Imported: %name.', array('%name' => $file->filename));
|
181 |
|
|
$context['results'][$id] = array_merge((array)$context['results'][$id], $import_result, array('file' => $file));
|
182 |
|
|
}
|
183 |
|
|
else {
|
184 |
|
|
$context['results'][$id] = array_merge((array)$context['results'][$id], array('fail' => TRUE), array('file' => $file));
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
}
|
188 |
|
|
|
189 |
|
|
/**
|
190 |
|
|
* Batch finished callback: Set result message.
|
191 |
|
|
*
|
192 |
|
|
* @param $success
|
193 |
|
|
* TRUE if batch succesfully completed.
|
194 |
|
|
* @param $results
|
195 |
|
|
* Batch results.
|
196 |
|
|
*/
|
197 |
|
|
function _l10n_update_batch_finished($success, $results) {
|
198 |
|
|
$totals = array(); // Sum of added, updated and deleted translations.
|
199 |
|
|
$total_skip = 0; // Sum of skipped translations
|
200 |
|
|
$messages = array(); // User feedback messages.
|
201 |
|
|
$project_fail = $project_success = array(); // Project names of succesfull and failed imports.
|
202 |
|
|
$t = get_t();
|
203 |
|
|
|
204 |
|
|
if ($success) {
|
205 |
|
|
// Summarize results of added, updated, deleted and skiped translations.
|
206 |
|
|
// Added, updated and deleted are summarized per language to be displayed accordingly.
|
207 |
|
|
foreach ($results as $result) {
|
208 |
|
|
if (isset($result['fail'])) {
|
209 |
|
|
// Collect project names of the failed imports.
|
210 |
|
|
$project_fail[$result['file']->name] = $result['file']->name;
|
211 |
|
|
}
|
212 |
|
|
else {
|
213 |
|
|
$language = $result['language'];
|
214 |
|
|
|
215 |
|
|
// Initialize variables to prevent PHP Notices.
|
216 |
|
|
if (!isset($totals[$language])) {
|
217 |
|
|
$totals[$language] = array();
|
218 |
|
|
$totals[$language]['add'] = $totals[$language]['update'] = $totals[$language]['delete'] = 0;
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
// Summarize added, updated, deleted and skiped translations.
|
222 |
|
|
$totals[$language]['add'] += $result['add'];
|
223 |
|
|
$totals[$language]['update'] += $result['update'];
|
224 |
|
|
$totals[$language]['delete'] += $result['delete'];
|
225 |
|
|
$total_skip += $result['skip'];
|
226 |
|
|
|
227 |
|
|
// Collect project names of the succesfull imports.
|
228 |
|
|
$project_success[$result['file']->name] = $result['file']->name;
|
229 |
|
|
}
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
// Messages of succesfull translation update results.
|
233 |
|
|
if ($project_success) {
|
234 |
|
|
$messages[] = format_plural(count($project_success), 'One project updated: @projects.', '@count projects updated: @projects.', array('@projects' => implode(', ', $project_success)));
|
235 |
|
|
$languages = language_list();
|
236 |
|
|
foreach ($totals as $language => $total) {
|
237 |
|
|
$messages[] = $t('%language translation strings added: !add, updated: !update, deleted: !delete.', array(
|
238 |
|
|
'%language' => $languages[$language]->name,
|
239 |
|
|
'!add' => $total['add'],
|
240 |
|
|
'!update' => $total['update'],
|
241 |
|
|
'!delete' => $total['delete'],
|
242 |
|
|
));
|
243 |
|
|
}
|
244 |
|
|
drupal_set_message(implode("<br />\n", $messages));
|
245 |
|
|
|
246 |
|
|
// Warning for disallowed HTML.
|
247 |
|
|
if ($total_skip) {
|
248 |
|
|
drupal_set_message(
|
249 |
|
|
format_plural(
|
250 |
|
|
$total_skip,
|
251 |
|
|
'One translation string was skipped because it contains disallowed HTML. See !log_messages for details.',
|
252 |
|
|
'@count translation strings were skipped because they contain disallowed HTML. See !log_messages for details.',
|
253 |
|
|
array('!log_messages' => l(t('Recent log messages'), 'admin/reports/dblog'))),
|
254 |
|
|
'warning');
|
255 |
|
|
}
|
256 |
503b3f7b
|
Assos Assos
|
|
257 |
|
|
// Clear cache and force refresh of JavaScript translations and rebuild
|
258 |
|
|
// the menu as strings may have changed.
|
259 |
|
|
foreach (array_keys($totals) as $langcode) {
|
260 |
|
|
_locale_invalidate_js($langcode);
|
261 |
|
|
}
|
262 |
|
|
cache_clear_all('locale:', 'cache', TRUE);
|
263 |
|
|
menu_rebuild();
|
264 |
85ad3d82
|
Assos Assos
|
}
|
265 |
|
|
|
266 |
|
|
// Error for failed imports.
|
267 |
|
|
if ($project_fail) {
|
268 |
|
|
drupal_set_message(
|
269 |
|
|
format_plural(
|
270 |
|
|
count($project_fail),
|
271 |
|
|
'Translations of one project were not imported: @projects.',
|
272 |
|
|
'Translations of @count projects were not imported: @projects',
|
273 |
|
|
array('@projects' => implode(', ', $project_fail))),
|
274 |
|
|
'error');
|
275 |
|
|
}
|
276 |
|
|
}
|
277 |
|
|
else {
|
278 |
|
|
drupal_set_message($t('Error importing translations.'), 'error');
|
279 |
|
|
}
|
280 |
|
|
} |