i18n_string.install 9.1 KB

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