i18n_string.admin.inc 9.9 KB

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