i18n_string.install 8.9 KB

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