1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
|
|
* Helper functions for string administration.
|
6 |
|
|
*/
|
7 |
|
|
|
8 |
|
|
// Load locale library
|
9 |
|
|
include_once DRUPAL_ROOT . '/includes/locale.inc';
|
10 |
|
|
|
11 |
|
|
/**
|
12 |
|
|
* Form callback. Refresh textgroups.
|
13 |
|
|
*/
|
14 |
|
|
function i18n_string_admin_refresh_form() {
|
15 |
|
|
// Select textgroup/s. Just the ones that have a 'refresh callback'
|
16 |
|
|
$groups = array();
|
17 |
|
|
foreach (i18n_string_group_info() as $name => $info) {
|
18 |
|
|
$groups[$name] = $info['title'];
|
19 |
|
|
}
|
20 |
|
|
$form['groups'] = array(
|
21 |
|
|
'#type' => 'checkboxes',
|
22 |
|
|
'#title' => t('Select text groups'),
|
23 |
|
|
'#options' => $groups,
|
24 |
|
|
'#description' => t('If a text group is no showing up here it means this feature is not implemented for it.'),
|
25 |
|
|
);
|
26 |
|
|
$form['delete'] = array(
|
27 |
|
|
'#type' => 'checkbox',
|
28 |
|
|
'#title' => t('Clean up left over strings.'),
|
29 |
|
|
'#default_value' => TRUE,
|
30 |
|
|
);
|
31 |
|
|
$form['refresh'] = array(
|
32 |
|
|
'#type' => 'submit',
|
33 |
|
|
'#value' => t('Refresh strings'),
|
34 |
|
|
'#suffix' => '<p>' . t('This will create all the missing strings for the selected text groups.') . '</p>',
|
35 |
|
|
);
|
36 |
|
|
return $form;
|
37 |
|
|
}
|
38 |
|
|
|
39 |
|
|
/**
|
40 |
|
|
* Form submission.
|
41 |
|
|
*/
|
42 |
|
|
function i18n_string_admin_refresh_form_submit($form, &$form_state) {
|
43 |
|
|
$op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
|
44 |
|
|
$groups = array_filter($form_state['values']['groups']);
|
45 |
|
|
$group_names = module_invoke_all('locale', 'groups');
|
46 |
|
|
if ($op == t('Refresh strings') && $groups) {
|
47 |
|
|
$batch = i18n_string_refresh_batch($groups, $form_state['values']['delete']);
|
48 |
|
|
batch_set($batch);
|
49 |
|
|
}
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
/**
|
54 |
|
|
* Refresh all user defined strings for a given text group.
|
55 |
|
|
*
|
56 |
|
|
* @param $group
|
57 |
|
|
* Text group to refresh
|
58 |
|
|
* @param $delete
|
59 |
|
|
* Optional, delete existing (but not refresed, strings and translations)
|
60 |
|
|
* @return Boolean
|
61 |
|
|
* True if the strings have been refreshed successfully. False otherwise.
|
62 |
|
|
*/
|
63 |
|
|
function i18n_string_refresh_group($group, $delete = FALSE) {
|
64 |
|
|
$result = FALSE;
|
65 |
|
|
|
66 |
|
|
// Compile all strings for this group
|
67 |
|
|
if ($strings = i18n_string_group_string_list($group)) {
|
68 |
|
|
i18n_string_refresh_string_list($strings);
|
69 |
|
|
$result = TRUE;
|
70 |
|
|
}
|
71 |
|
|
// Invoke refresh hook
|
72 |
|
|
$result = $result || module_invoke_all('i18n_string_refresh', $group);
|
73 |
|
|
|
74 |
|
|
// Now delete all source strings that were not refreshed (they don't have a row in i18n_string)
|
75 |
|
|
if ($result && $delete) {
|
76 |
|
|
i18n_string_refresh_cleanup($group);
|
77 |
|
|
}
|
78 |
|
|
return $result;
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
/**
|
82 |
|
|
* Clean up left over strings for text group
|
83 |
|
|
*/
|
84 |
|
|
function i18n_string_refresh_cleanup($group) {
|
85 |
|
|
$lids = db_select('locales_source', 's')
|
86 |
|
|
->fields('s', array('lid'))
|
87 |
|
|
->condition('textgroup', $group)
|
88 |
|
|
->condition('version', 0)
|
89 |
|
|
->execute()
|
90 |
|
|
->fetchCol();
|
91 |
|
|
if ($lids) {
|
92 |
|
|
drupal_set_message(t('Performing cleanup for text group %textgroup, deleting @count left over strings.', array('%textgroup' => $group, '@count' => count($lids))));
|
93 |
|
|
db_delete('locales_target')->condition('lid', $lids)->execute();
|
94 |
|
|
db_delete('locales_source')->condition('lid', $lids)->execute();
|
95 |
|
|
db_delete('i18n_string')->condition('lid', $lids)->execute();
|
96 |
|
|
return count($lids);
|
97 |
|
|
}
|
98 |
|
|
else {
|
99 |
|
|
return 0;
|
100 |
|
|
}
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
/**
|
104 |
|
|
* Prepare group for refreshing, reset version, count strings
|
105 |
|
|
*/
|
106 |
|
|
function i18n_string_refresh_reset($group) {
|
107 |
|
|
// Mark data on locales_source setting version = 0
|
108 |
|
|
db_update('locales_source')
|
109 |
|
|
->fields(array('version' => 0))
|
110 |
|
|
->condition('textgroup', $group)
|
111 |
|
|
->execute();
|
112 |
|
|
return (int)db_query("SELECT COUNT(*) FROM {locales_source} WHERE textgroup = :textgroup", array(':textgroup' => $group))->fetchField();
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
/**
|
116 |
|
|
* Refresh string list
|
117 |
|
|
*/
|
118 |
|
|
function i18n_string_refresh_string_list($strings) {
|
119 |
|
|
$count = 0;
|
120 |
|
|
foreach ($strings as $textgroup => $group_strings) {
|
121 |
|
|
foreach ($group_strings as $type => $type_strings) {
|
122 |
|
|
foreach ($type_strings as $id => $object_strings) {
|
123 |
|
|
foreach ($object_strings as $key => $string) {
|
124 |
|
|
if (is_array($string)) {
|
125 |
|
|
$format = isset($string['format']) ? $string['format'] : NULL;
|
126 |
|
|
$string = $string['string'];
|
127 |
|
|
}
|
128 |
|
|
else {
|
129 |
|
|
$format = NULL;
|
130 |
|
|
}
|
131 |
|
|
i18n_string_update(array($textgroup, $type, $id, $key), $string, array('format' => $format));
|
132 |
|
|
$count++;
|
133 |
|
|
}
|
134 |
|
|
}
|
135 |
|
|
}
|
136 |
|
|
}
|
137 |
|
|
return $count;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
/**
|
141 |
|
|
* Create batch for refreshing strings
|
142 |
|
|
*
|
143 |
|
|
* @param $groups
|
144 |
|
|
* Array of text groups to refresh
|
145 |
|
|
* @param $delete
|
146 |
|
|
* Optional, delete existing (but not refresed, strings and translations)
|
147 |
|
|
*/
|
148 |
|
|
function i18n_string_refresh_batch($groups, $delete = FALSE) {
|
149 |
|
|
$operations = array();
|
150 |
|
|
foreach ($groups as $group) {
|
151 |
|
|
$operations[] = array('_i18n_string_batch_refresh_prepare', array($group));
|
152 |
|
|
// First try to find string list
|
153 |
|
|
$operations[] = array('_i18n_string_batch_refresh_list', array($group));
|
154 |
|
|
// Then invoke refresh callback
|
155 |
|
|
$operations[] = array('_i18n_string_batch_refresh_callback', array($group));
|
156 |
|
|
if ($delete) {
|
157 |
|
|
$operations[] = array('_i18n_string_batch_refresh_cleanup', array($group));
|
158 |
|
|
}
|
159 |
|
|
// Output group summary
|
160 |
|
|
$operations[] = array('_i18n_string_batch_refresh_summary', array($group));
|
161 |
|
|
}
|
162 |
|
|
$batch = array(
|
163 |
|
|
'operations' => $operations,
|
164 |
|
|
'title' => t('Refreshing user defined strings'),
|
165 |
|
|
'init_message' => t('Starting string refresh'),
|
166 |
|
|
'error_message' => t('Error refreshing user defined strings'),
|
167 |
|
|
'file' => drupal_get_path('module', 'i18n_string') . '/i18n_string.admin.inc',
|
168 |
|
|
);
|
169 |
|
|
return $batch;
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
/**
|
173 |
|
|
* Refresh strings for enabled modules
|
174 |
|
|
*/
|
175 |
|
|
function i18n_string_refresh_enabled_modules($modules) {
|
176 |
|
|
// Check if any of the modules has strings to update
|
177 |
|
|
$count = 0;
|
178 |
|
|
foreach ($modules as $module) {
|
179 |
|
|
if ($strings = i18n_string_module_string_list($module)) {
|
180 |
|
|
$count += i18n_string_refresh_string_list($strings);
|
181 |
|
|
|
182 |
|
|
}
|
183 |
|
|
// Call module refresh if exists
|
184 |
|
|
module_invoke($module, 'i18n_string_refresh', 'all');
|
185 |
|
|
}
|
186 |
|
|
if ($count) {
|
187 |
|
|
drupal_set_message(format_plural($count, 'Refreshed one string for enabled modules.', 'Refreshed @count strings for the enabled modules.'));
|
188 |
|
|
}
|
189 |
|
|
}
|
190 |
|
|
|
191 |
|
|
/**
|
192 |
|
|
* Purge uninstalled modules.
|
193 |
|
|
*/
|
194 |
|
|
function i18n_string_refresh_uninstalled_modules($modules) {
|
195 |
|
|
foreach ($modules as $module) {
|
196 |
|
|
// If the module defines any textgroup, purge all strings.
|
197 |
|
|
module_load_include('i18n.inc', $module);
|
198 |
|
|
if ($string_info = module_invoke($module, 'i18n_string_info')) {
|
199 |
|
|
foreach ($string_info as $group => $group_info) {
|
200 |
|
|
i18n_string_refresh_reset($group);
|
201 |
|
|
i18n_string_refresh_cleanup($group);
|
202 |
|
|
}
|
203 |
|
|
}
|
204 |
|
|
}
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
/**
|
208 |
|
|
* Prepare group for refreshing
|
209 |
|
|
*/
|
210 |
|
|
function _i18n_string_batch_refresh_prepare($group, &$context) {
|
211 |
|
|
$context['results'][$group] = array(
|
212 |
|
|
'count' => i18n_string_refresh_reset($group),
|
213 |
|
|
'result' => FALSE,
|
214 |
|
|
);
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
/**
|
218 |
|
|
* Batch operation: Refresh string list for group
|
219 |
|
|
*/
|
220 |
|
|
function _i18n_string_batch_refresh_list($group, &$context) {
|
221 |
|
|
$count = 0;
|
222 |
|
|
if ($strings = i18n_string_group_string_list($group)) {
|
223 |
|
|
$count = i18n_string_refresh_string_list($strings);
|
224 |
|
|
$context['results'][$group]['result'] = TRUE;
|
225 |
|
|
}
|
226 |
|
|
$context['results'][$group]['refresh'] = $count;
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
/**
|
230 |
|
|
* Batch operation: Invoke i18n_string_refresh
|
231 |
|
|
*/
|
232 |
|
|
function _i18n_string_batch_refresh_callback($group, &$context) {
|
233 |
|
|
$result = module_invoke_all('i18n_string_refresh', $group);
|
234 |
|
|
$count = $result ? array_sum($result) : 0;
|
235 |
|
|
$context['results'][$group]['refresh'] += $count;
|
236 |
|
|
if ($count) {
|
237 |
|
|
$context['results'][$group]['result'] = TRUE;
|
238 |
|
|
}
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
/**
|
242 |
|
|
* Batch callback, delete old strings
|
243 |
|
|
*/
|
244 |
|
|
function _i18n_string_batch_refresh_cleanup($group, &$context) {
|
245 |
|
|
if (!empty($context['results'][$group]['result'])) {
|
246 |
|
|
$context['results'][$group]['deleted'] = i18n_string_refresh_cleanup($group);
|
247 |
|
|
}
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
/**
|
251 |
|
|
* Batch operations: Print refresh summary for group
|
252 |
|
|
*/
|
253 |
|
|
function _i18n_string_batch_refresh_summary($group, &$context) {
|
254 |
|
|
if ($context['results'][$group]['result']) {
|
255 |
|
|
drupal_set_message(t("Successfully refreshed @count strings for %group", array('@count' => $context['results'][$group]['refresh'], '%group' => i18n_string_group_info($group, 'title'))));
|
256 |
|
|
if (!empty($context['results'][$group]['deleted'])) {
|
257 |
|
|
drupal_set_message(t('Deleted @count left over strings.', array('@count' => $context['results'][$group]['deleted'])));
|
258 |
|
|
}
|
259 |
|
|
}
|
260 |
|
|
else {
|
261 |
|
|
drupal_set_message(t("Cannot refresh strings for %group.", array('%group' => i18n_string_group_info($group, 'title'))), 'warning');
|
262 |
|
|
}
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
/**
|
266 |
|
|
* Get all strings for a text group
|
267 |
|
|
*/
|
268 |
|
|
function i18n_string_group_string_list($group) {
|
269 |
|
|
// Add strings provided by all modules on hook_string_list().
|
270 |
|
|
// Note that i18n_string module itself will also collect all strings for this group's objects
|
271 |
|
|
$strings = module_invoke_all('i18n_string_list', $group);
|
272 |
|
|
// Invoke hook_i18n_string_list_TEXTGROUP_alter()
|
273 |
|
|
drupal_alter('i18n_string_list_' . $group, $strings);
|
274 |
|
|
return $strings;
|
275 |
|
|
}
|
276 |
|
|
|
277 |
|
|
/**
|
278 |
|
|
* Get all strings from a module.
|
279 |
|
|
*/
|
280 |
|
|
function i18n_string_module_string_list($module) {
|
281 |
|
|
$strings = array();
|
282 |
|
|
// Try loading i18n.inc for the module and some library functions.
|
283 |
|
|
module_load_include('i18n.inc', $module);
|
284 |
|
|
module_load_include('i18n.inc', 'i18n_string');
|
285 |
|
|
// If the module defines any textgroup, get all strings for this group
|
286 |
|
|
if ($groups = module_invoke($module, 'i18n_string_info')) {
|
287 |
|
|
foreach (array_keys($groups) as $group) {
|
288 |
|
|
$strings = i18n_string_array_merge($strings, i18n_string_group_string_list($group));
|
289 |
|
|
}
|
290 |
|
|
}
|
291 |
|
|
else {
|
292 |
|
|
$groups = array();
|
293 |
|
|
}
|
294 |
|
|
// The module may implement i18n_string_list()
|
295 |
|
|
if ($string_list = module_invoke($module, 'i18n_string_list', 'all')) {
|
296 |
|
|
foreach ($string_list as $group => $group_strings) {
|
297 |
|
|
if (!in_array($group, $groups)) {
|
298 |
|
|
$strings[$group] = $group_strings;
|
299 |
|
|
}
|
300 |
|
|
}
|
301 |
|
|
}
|
302 |
|
|
// If the module defines any object that has strings in another textgroup
|
303 |
|
|
if ($object_types = module_invoke($module, 'i18n_object_info')) {
|
304 |
|
|
foreach ($object_types as $type => $type_info) {
|
305 |
|
|
if (($group = i18n_string_object_info($type, 'textgroup')) && !in_array($group, $groups)) {
|
306 |
|
|
if ($group_strings = i18n_string_object_type_string_list($type)) {
|
307 |
|
|
$strings = i18n_string_array_merge($strings, $group_strings);
|
308 |
|
|
}
|
309 |
|
|
}
|
310 |
|
|
}
|
311 |
|
|
}
|
312 |
|
|
return $strings;
|
313 |
|
|
} |