Projet

Général

Profil

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

root / drupal7 / sites / all / modules / i18n / i18n_string / i18n_string.install @ e0d35157

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

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

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

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

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

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

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

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

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

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

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

    
250
/**
251
 * Change objectindex from int to bigint.
252
 */
253
function i18n_string_update_7003() {
254
  db_change_field('i18n_string', 'objectindex', 'objectindex', array(
255
    'type' => 'int',
256
    'size' => 'big',
257
    'length' => 20,
258
  ));
259
}
260

    
261
/**
262
 * Notes for update script
263
 */
264
// Added fields: context, textgroup
265
//
266
// Drop all indexes from locales_source
267
// Update format field
268
// Update string names: profile, cck => field
269
// Update string names:
270

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