i18n_string.install 9.5 KB

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