Projet

Général

Profil

Paste
Télécharger (9,05 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / i18n / i18n_string / i18n_string.install @ 76df55b7

1
<?php
2

    
3
/**
4
 * @file
5
 * Installation file for i18n_string module.
6
 */
7

    
8
/**
9
 * Implements hook_enable().
10
 */
11
function i18n_string_enable() {
12
  // Refresh locales for enabled modules
13
  $modules = module_implements('i18n_string_refresh');
14
  i18n_string_modules_enabled($modules);
15
}
16

    
17
/**
18
 * Implements hook_install().
19
 */
20
function i18n_string_install() {
21
  // Add a field to track whether a translation needs updating.
22
  module_load_install('i18n');
23
  i18n_install_create_fields('locales_target', array('i18n_status'));
24
  // Set module weight for it to run after core modules.
25
  db_query("UPDATE {system} SET weight = 10 WHERE name = 'i18n_string' AND type = 'module'");
26
  // If updating from D6, module changed name
27
  if (variable_get('i18n_drupal6_update')) {
28
    i18n_string_update_7000();
29
    i18n_string_update_7001();
30
  }
31
  // Create new index in {locales_source}, performance improvement in sites with i18n.
32
  if (!db_index_exists('locales_source', 'textgroup_context')) {
33
    db_add_index('locales_source', 'textgroup_context', array('textgroup', array('context', 50)));
34
  }
35
}
36

    
37
/**
38
 * Implements hook_uninstall().
39
 */
40
function i18n_string_uninstall() {
41
  // Drop custom field.
42
  db_drop_field('locales_target', 'i18n_status');
43
  // Drop custom index in locales_source table
44
  db_drop_index('locales_source', 'textgroup_context');
45
}
46

    
47
/**
48
 * Implements hook_schema().
49
 */
50
function i18n_string_schema() {
51
  $schema['i18n_string'] = array(
52
    'description' => 'Metadata for source strings.',
53
    'fields' => array(
54
      'lid' => array(
55
        'type' => 'int',
56
        'not null' => TRUE,
57
        'default' => 0,
58
        'description' => 'Source string ID. References {locales_source}.lid.',
59
      ),
60
      'textgroup' => array(
61
        'type' => 'varchar',
62
        'length' => 50,
63
        'not null' => TRUE,
64
        'default' => 'default',
65
        'description' => 'A module defined group of translations, see hook_locale().',
66
      ),
67
      'context' => array(
68
        'type' => 'varchar',
69
        'length' => 255,
70
        'not null' => TRUE,
71
        'default' => '',
72
        'description' => 'Full string ID for quick search: type:objectid:property.',
73
      ),
74
      'objectid' => array(
75
        'type' => 'varchar',
76
        'length' => 255,
77
        'not null' => TRUE,
78
        'default' => '',
79
        'description' => 'Object ID.',
80
      ),
81
      'type' => array(
82
        'type' => 'varchar',
83
        'length' => 255,
84
        'not null' => TRUE,
85
        'default' => '',
86
        'description' => 'Object type for this string.',
87
      ),
88
      'property' => array(
89
        'type' => 'varchar',
90
        'length' => 255,
91
        'not null' => TRUE,
92
        'default' => '',
93
        'description' => 'Object property for this string.',
94
      ),
95
      'objectindex' => array(
96
        'type' => 'int',
97
        'not null' => TRUE,
98
        'default' => 0,
99
        'description' => 'Integer value of Object ID.',
100
      ),
101
      'format' => array(
102
        'type' => 'varchar',
103
        'length' => 255,
104
        'not null' => FALSE,
105
        'description' => 'The {filter_format}.format of the string.',
106
      ),
107

    
108
    ),
109
    'primary key' => array('lid'),
110
    'indexes' => array(
111
      'group_context' => array('textgroup', array('context', 50)),
112
    ),
113
  );
114
  return $schema;
115
}
116

    
117
/**
118
 * Implements hook_schema_alter().
119
 */
120
function i18n_string_schema_alter(&$schema) {
121
  // Add field for tracking whether translations need updating.
122
  $schema['locales_target']['fields']['i18n_status'] = array(
123
    'description' => 'A boolean indicating whether this translation needs to be updated.',
124
    'type' => 'int',
125
    'not null' => TRUE,
126
    'default' => 0,
127
  );
128
}
129

    
130
/**
131
 * Helper function to upate strings
132
 */
133
function i18n_string_install_update_string($string) {
134
  $string->context = $string->type . ':' . $string->objectid . ':' . $string->property;
135
  $string->location = $string->textgroup . ':' . $string->context;
136
  $string->objectindex = (int)$string->objectid;
137
  drupal_write_record('i18n_string', $string, 'lid');
138
  drupal_write_record('locales_source', $string, 'lid');
139
}
140

    
141
/**
142
 * Update context for strings.
143
 *
144
 * As some string locations depend on configurable values, the field needs sometimes to be updated
145
 * without losing existing translations. I.e:
146
 * - profile fields indexed by field name.
147
 * - content types indexted by low level content type name.
148
 *
149
 * Example:
150
 *  'profile:field:oldfield:*' -> 'profile:field:newfield:*'
151
 */
152
function i18n_string_install_update_context($oldname, $newname) {
153
  // Get context replacing '*' with empty string.
154
  $oldcontext = explode(':', $oldname);
155
  $newcontext = explode(':', $newname);
156
  /*
157
  i18n_string_context(str_replace('*', '', $oldname));
158
  $newcontext = i18n_string_context(str_replace('*', '', $newname));
159
  */
160
  // Get location with placeholders.
161
  foreach (array('textgroup', 'type', 'objectid', 'property') as $index => $field) {
162
    if ($oldcontext[$index] != $newcontext[$index]) {
163
      $replace[$field] = $newcontext[$index];
164
    }
165
  }
166

    
167
  // Query and replace if there are any fields. It is possible that under some circumstances fields are the same
168
  if (!empty($replace)) {
169
    $textgroup = array_shift($oldcontext);
170
    $context = str_replace('*', '%', implode(':', $oldcontext));
171
    $count = 0;
172
    $query = db_select('i18n_string', 's')
173
      ->fields('s')
174
      ->condition('s.textgroup', $textgroup)
175
      ->condition('s.context', $context, 'LIKE');
176

    
177
    foreach ($query->execute()->fetchAll() as $source) {
178
      foreach ($replace as $field => $value) {
179
        $source->$field = $value;
180
      }
181
      // Recalculate location, context, objectindex
182
      $source->context = $source->type . ':' . $source->objectid . ':' . $source->property;
183
      $source->location = $source->textgroup . ':' . $source->context;
184
      $source->objectindex = (int)$source->objectid;
185
      // Update source string.
186
      $update = array(
187
        'textgroup' => $source->textgroup,
188
        'context' => $source->context,
189
      );
190
      db_update('locales_source')
191
        ->fields($update + array('location' => $source->location))
192
        ->condition('lid', $source->lid)
193
        ->execute();
194

    
195
      // Update object data.
196
      db_update('i18n_string')
197
      ->fields($update + array(
198
        'type' => $source->type,
199
        'objectid' => $source->objectid,
200
        'property' => $source->property,
201
        'objectindex' => $source->objectindex,
202
      ))
203
      ->condition('lid', $source->lid)
204
      ->execute();
205
      $count++;
206
    }
207
    drupal_set_message(t('Updated @count string names from %oldname to %newname.', array('@count' => $count, '%oldname' => $oldname, '%newname' => $newname)));
208
  }
209
}
210

    
211
/**
212
 * Populate fields from old locale table (textgroup, location) and drop indexes from locales_source
213
 */
214
function i18n_string_update_7000() {
215
  // @todo Update from d6
216
  variable_del('i18nstrings_allowed_textgroups');
217
  // If we've got old table from D6, move data to new one
218
  if (db_table_exists('i18n_strings')) {
219
    // First of all clean up strings that don't have a locale source, see http://drupal.org/node/1186692
220
    db_query("DELETE FROM {i18n_strings} WHERE lid NOT IN (SELECT lid FROM {locales_source})");
221
    db_query("INSERT INTO {i18n_string}(lid, objectid, type, property, objectindex, format) SELECT lid, objectid, type, property, objectindex, format FROM {i18n_strings}");
222
    // Update and populate textgroup field
223
    db_query("UPDATE {i18n_string} s SET s.textgroup = (SELECT l.textgroup FROM {locales_source} l WHERE l.lid = s.lid)");
224
    // Populate context field. We could use CONCAT_WS but I guess this is more standard.
225
    db_query("UPDATE {i18n_string} SET context = CONCAT(type, ':', objectid, ':', property)");
226
    db_query("UPDATE {locales_source} s INNER JOIN {i18n_string} i ON s.lid = i.lid SET s.context = i.context");
227
  }
228
}
229

    
230
/**
231
 * Drop obsoleted i18n_strings table if exists
232
 */
233
function i18n_string_update_7001() {
234
  if (db_table_exists('i18n_strings')) {
235
    db_drop_table('i18n_strings');
236
  }
237
}
238

    
239
/**
240
 *  Create new index in {locales_source}, performance improvement in sites with i18n.
241
 */
242
function i18n_string_update_7002() {
243
  if (!db_index_exists('locales_source', 'textgroup_context')) {
244
    db_add_index('locales_source', 'textgroup_context', array('textgroup', array('context', 50)));
245
  }
246
}
247

    
248

    
249
/**
250
 * Notes for update script
251
 */
252
// Added fields: context, textgroup
253
//
254
// Drop all indexes from locales_source
255
// Update format field
256
// Update string names: profile, cck => field
257
// Update string names:
258

    
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
 */