Projet

Général

Profil

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

root / drupal7 / sites / all / modules / i18n / i18n_string / i18n_string.install @ 9faa5de0

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
        'size' => 'big',
98
        'not null' => TRUE,
99
        'default' => 0,
100
        'description' => 'Integer value of Object ID.',
101
      ),
102
      'format' => array(
103
        'type' => 'varchar',
104
        'length' => 255,
105
        'not null' => FALSE,
106
        'description' => 'The {filter_format}.format of the string.',
107
      ),
108

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

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

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

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

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

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

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

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

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

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

    
249
/**
250
 * Removed due to buggy upgrade for #2200647.
251
 */
252
function i18n_string_update_7003() {
253
}
254

    
255
/**
256
 * Change objectindex from int to bigint.
257
 */
258
function i18n_string_update_7004() {
259
  db_change_field('i18n_string', 'objectindex', 'objectindex', array(
260
    'type' => 'int',
261
    'size' => 'big',
262
    'not null' => TRUE,
263
    'default' => 0,
264
    'description' => 'Integer value of Object ID.',
265
  ));
266
}
267

    
268
/**
269
 * Notes for update script
270
 */
271
// Added fields: context, textgroup
272
//
273
// Drop all indexes from locales_source
274
// Update format field
275
// Update string names: profile, cck => field
276
// Update string names:
277

    
278
/**
279
 * Old strings to update. All these will be handled by i18n_field module
280
 *
281
 * 'cck:field:'. $content_type .'-'. $field_name .':widget_label'
282
 *  --> 'field:$field_name:$bundle:label' (though not used atm)
283
 * 'cck:field:'. $content_type .'-'. $field_name .':widget_description'
284
 *  --> 'field:$field_name:$bundle:description'
285
 * 'cck:fieldgroup:'. $content_type .'-'. $group_name .':display_description'
286
 * 'cck:fieldgroup:'. $content_type .'-'. $group_name .':form_description', $group['settings']['form']['description']);
287
 *
288
 * Profile:
289
 * profile:field:$field_name:title|explanation|options
290
 * "profile:category", $field->category
291
 *
292
 * Node type
293
 *  nodetype:type:[type]:[property] -> node:type:[type]:[property]
294
 *  Property names: title -> title_label
295
 */