i18n_string.install 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * @file
  4. * Installation file for i18n_string module.
  5. */
  6. /**
  7. * Implements hook_enable().
  8. */
  9. function i18n_string_enable() {
  10. // Refresh locales for enabled modules
  11. $modules = module_implements('i18n_string_refresh');
  12. i18n_string_modules_enabled($modules);
  13. }
  14. /**
  15. * Implements hook_install().
  16. */
  17. function i18n_string_install() {
  18. // Add a field to track whether a translation needs updating.
  19. module_load_install('i18n');
  20. i18n_install_create_fields('locales_target', array('i18n_status'));
  21. // Set module weight for it to run after core modules.
  22. db_query("UPDATE {system} SET weight = 10 WHERE name = 'i18n_string' AND type = 'module'");
  23. // If updating from D6, module changed name
  24. if (variable_get('i18n_drupal6_update')) {
  25. i18n_string_update_7000();
  26. i18n_string_update_7001();
  27. }
  28. }
  29. /**
  30. * Implements hook_uninstall().
  31. */
  32. function i18n_string_uninstall() {
  33. // Drop custom field.
  34. db_drop_field('locales_target', 'i18n_status');
  35. }
  36. /**
  37. * Implements hook_schema().
  38. */
  39. function i18n_string_schema() {
  40. $schema['i18n_string'] = array(
  41. 'description' => 'Metadata for source strings.',
  42. 'fields' => array(
  43. 'lid' => array(
  44. 'type' => 'int',
  45. 'not null' => TRUE,
  46. 'default' => 0,
  47. 'description' => 'Source string ID. References {locales_source}.lid.',
  48. ),
  49. 'textgroup' => array(
  50. 'type' => 'varchar',
  51. 'length' => 50,
  52. 'not null' => TRUE,
  53. 'default' => 'default',
  54. 'description' => 'A module defined group of translations, see hook_locale().',
  55. ),
  56. 'context' => array(
  57. 'type' => 'varchar',
  58. 'length' => 255,
  59. 'not null' => TRUE,
  60. 'default' => '',
  61. 'description' => 'Full string ID for quick search: type:objectid:property.',
  62. ),
  63. 'objectid' => array(
  64. 'type' => 'varchar',
  65. 'length' => 255,
  66. 'not null' => TRUE,
  67. 'default' => '',
  68. 'description' => 'Object ID.',
  69. ),
  70. 'type' => array(
  71. 'type' => 'varchar',
  72. 'length' => 255,
  73. 'not null' => TRUE,
  74. 'default' => '',
  75. 'description' => 'Object type for this string.',
  76. ),
  77. 'property' => array(
  78. 'type' => 'varchar',
  79. 'length' => 255,
  80. 'not null' => TRUE,
  81. 'default' => '',
  82. 'description' => 'Object property for this string.',
  83. ),
  84. 'objectindex' => array(
  85. 'type' => 'int',
  86. 'not null' => TRUE,
  87. 'default' => 0,
  88. 'description' => 'Integer value of Object ID.',
  89. ),
  90. 'format' => array(
  91. 'type' => 'varchar',
  92. 'length' => 255,
  93. 'not null' => FALSE,
  94. 'description' => 'The {filter_format}.format of the string.',
  95. ),
  96. ),
  97. 'primary key' => array('lid'),
  98. 'indexes' => array(
  99. 'group_context' => array('textgroup', 'context'),
  100. ),
  101. );
  102. return $schema;
  103. }
  104. /**
  105. * Implements hook_schema_alter().
  106. */
  107. function i18n_string_schema_alter(&$schema) {
  108. // Add field for tracking whether translations need updating.
  109. $schema['locales_target']['fields']['i18n_status'] = array(
  110. 'description' => 'A boolean indicating whether this translation needs to be updated.',
  111. 'type' => 'int',
  112. 'not null' => TRUE,
  113. 'default' => 0,
  114. );
  115. }
  116. /**
  117. * Helper function to upate strings
  118. */
  119. function i18n_string_install_update_string($string) {
  120. $string->context = $string->type . ':' . $string->objectid . ':' . $string->property;
  121. $string->location = $string->textgroup . ':' . $string->context;
  122. $string->objectindex = (int)$string->objectid;
  123. drupal_write_record('i18n_string', $string, 'lid');
  124. drupal_write_record('locales_source', $string, 'lid');
  125. }
  126. /**
  127. * Update context for strings.
  128. *
  129. * As some string locations depend on configurable values, the field needs sometimes to be updated
  130. * without losing existing translations. I.e:
  131. * - profile fields indexed by field name.
  132. * - content types indexted by low level content type name.
  133. *
  134. * Example:
  135. * 'profile:field:oldfield:*' -> 'profile:field:newfield:*'
  136. */
  137. function i18n_string_install_update_context($oldname, $newname) {
  138. // Get context replacing '*' with empty string.
  139. $oldcontext = explode(':', $oldname);
  140. $newcontext = explode(':', $newname);
  141. /*
  142. i18n_string_context(str_replace('*', '', $oldname));
  143. $newcontext = i18n_string_context(str_replace('*', '', $newname));
  144. */
  145. // Get location with placeholders.
  146. foreach (array('textgroup', 'type', 'objectid', 'property') as $index => $field) {
  147. if ($oldcontext[$index] != $newcontext[$index]) {
  148. $replace[$field] = $newcontext[$index];
  149. }
  150. }
  151. // Query and replace if there are any fields. It is possible that under some circumstances fields are the same
  152. if (!empty($replace)) {
  153. $textgroup = array_shift($oldcontext);
  154. $context = str_replace('*', '%', implode(':', $oldcontext));
  155. $count = 0;
  156. $query = db_select('i18n_string', 's')
  157. ->fields('s')
  158. ->condition('s.textgroup', $textgroup)
  159. ->condition('s.context', $context, 'LIKE');
  160. foreach ($query->execute()->fetchAll() as $source) {
  161. foreach ($replace as $field => $value) {
  162. $source->$field = $value;
  163. }
  164. // Recalculate location, context, objectindex
  165. $source->context = $source->type . ':' . $source->objectid . ':' . $source->property;
  166. $source->location = $source->textgroup . ':' . $source->context;
  167. $source->objectindex = (int)$source->objectid;
  168. // Update source string.
  169. $update = array(
  170. 'textgroup' => $source->textgroup,
  171. 'context' => $source->context,
  172. );
  173. db_update('locales_source')
  174. ->fields($update + array('location' => $source->location))
  175. ->condition('lid', $source->lid)
  176. ->execute();
  177. // Update object data.
  178. db_update('i18n_string')
  179. ->fields($update + array(
  180. 'type' => $source->type,
  181. 'objectid' => $source->objectid,
  182. 'property' => $source->property,
  183. 'objectindex' => $source->objectindex,
  184. ))
  185. ->condition('lid', $source->lid)
  186. ->execute();
  187. $count++;
  188. }
  189. drupal_set_message(t('Updated @count string names from %oldname to %newname.', array('@count' => $count, '%oldname' => $oldname, '%newname' => $newname)));
  190. }
  191. }
  192. /**
  193. * Populate fields from old locale table (textgroup, location) and drop indexes from locales_source
  194. */
  195. function i18n_string_update_7000() {
  196. // @todo Update from d6
  197. variable_del('i18nstrings_allowed_textgroups');
  198. // If we've got old table from D6, move data to new one
  199. if (db_table_exists('i18n_strings')) {
  200. // First of all clean up strings that don't have a locale source, see http://drupal.org/node/1186692
  201. db_query("DELETE FROM {i18n_strings} WHERE lid NOT IN (SELECT lid FROM {locales_source})");
  202. db_query("INSERT INTO {i18n_string}(lid, objectid, type, property, objectindex, format) SELECT lid, objectid, type, property, objectindex, format FROM {i18n_strings}");
  203. // Update and populate textgroup field
  204. db_query("UPDATE {i18n_string} s SET s.textgroup = (SELECT l.textgroup FROM {locales_source} l WHERE l.lid = s.lid)");
  205. // Populate context field. We could use CONCAT_WS but I guess this is more standard.
  206. db_query("UPDATE {i18n_string} SET context = CONCAT(type, ':', objectid, ':', property)");
  207. db_query("UPDATE {locales_source} s INNER JOIN {i18n_string} i ON s.lid = i.lid SET s.context = i.context");
  208. }
  209. }
  210. /**
  211. * Drop obsoleted i18n_strings table if exists
  212. */
  213. function i18n_string_update_7001() {
  214. if (db_table_exists('i18n_strings')) {
  215. db_drop_table('i18n_strings');
  216. }
  217. }
  218. /**
  219. * Notes for update script
  220. */
  221. // Added fields: context, textgroup
  222. //
  223. // Drop all indexes from locales_source
  224. // Update format field
  225. // Update string names: profile, cck => field
  226. // Update string names:
  227. /**
  228. * Old strings to update. All these will be handled by i18n_field module
  229. *
  230. * 'cck:field:'. $content_type .'-'. $field_name .':widget_label'
  231. * --> 'field:$field_name:$bundle:label' (though not used atm)
  232. * 'cck:field:'. $content_type .'-'. $field_name .':widget_description'
  233. * --> 'field:$field_name:$bundle:description'
  234. * 'cck:fieldgroup:'. $content_type .'-'. $group_name .':display_description'
  235. * 'cck:fieldgroup:'. $content_type .'-'. $group_name .':form_description', $group['settings']['form']['description']);
  236. *
  237. * Profile:
  238. * profile:field:$field_name:title|explanation|options
  239. * "profile:category", $field->category
  240. *
  241. * Node type
  242. * nodetype:type:[type]:[property] -> node:type:[type]:[property]
  243. * Property names: title -> title_label
  244. */