Projet

Général

Profil

Paste
Télécharger (8,88 ko) Statistiques
| Branche: | Révision:

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

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
  db_add_index('locales_source', 'textgroup_context', array('textgroup', 'context'));
33
}
34

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

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

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

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

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

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

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

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

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

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

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

    
237
/**
238
 *  Create new index in {locales_source}, performance improvement in sites with i18n.
239
 */
240
function i18n_string_update_7002() {
241
  db_add_index('locales_source', 'textgroup_context', array('textgroup', 'context'));
242
}
243

    
244

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

    
255
/**
256
 * Old strings to update. All these will be handled by i18n_field module
257
 *
258
 * 'cck:field:'. $content_type .'-'. $field_name .':widget_label'
259
 *  --> 'field:$field_name:$bundle:label' (though not used atm)
260
 * 'cck:field:'. $content_type .'-'. $field_name .':widget_description'
261
 *  --> 'field:$field_name:$bundle:description'
262
 * 'cck:fieldgroup:'. $content_type .'-'. $group_name .':display_description'
263
 * 'cck:fieldgroup:'. $content_type .'-'. $group_name .':form_description', $group['settings']['form']['description']);
264
 *
265
 * Profile:
266
 * profile:field:$field_name:title|explanation|options
267
 * "profile:category", $field->category
268
 *
269
 * Node type
270
 *  nodetype:type:[type]:[property] -> node:type:[type]:[property]
271
 *  Property names: title -> title_label
272
 */