Projet

Général

Profil

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

root / drupal7 / sites / all / modules / i18n / i18n_string / i18n_string.install @ 3dfa8105

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
}
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
}
40

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

    
102
    ),
103
    'primary key' => array('lid'),
104
    'indexes' => array(
105
      'group_context' => array('textgroup', 'context'),
106
    ),
107
  );
108
  return $schema;
109
}
110

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

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

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

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

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

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

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

    
224
/**
225
 * Drop obsoleted i18n_strings table if exists
226
 */
227
function i18n_string_update_7001() {
228
  if (db_table_exists('i18n_strings')) {
229
    db_drop_table('i18n_strings');
230
  }
231
}
232

    
233
/**
234
 * Notes for update script
235
 */
236
// Added fields: context, textgroup
237
//
238
// Drop all indexes from locales_source
239
// Update format field
240
// Update string names: profile, cck => field
241
// Update string names:
242

    
243
/**
244
 * Old strings to update. All these will be handled by i18n_field module
245
 *
246
 * 'cck:field:'. $content_type .'-'. $field_name .':widget_label'
247
 *  --> 'field:$field_name:$bundle:label' (though not used atm)
248
 * 'cck:field:'. $content_type .'-'. $field_name .':widget_description'
249
 *  --> 'field:$field_name:$bundle:description'
250
 * 'cck:fieldgroup:'. $content_type .'-'. $group_name .':display_description'
251
 * 'cck:fieldgroup:'. $content_type .'-'. $group_name .':form_description', $group['settings']['form']['description']);
252
 *
253
 * Profile:
254
 * profile:field:$field_name:title|explanation|options
255
 * "profile:category", $field->category
256
 *
257
 * Node type
258
 *  nodetype:type:[type]:[property] -> node:type:[type]:[property]
259
 *  Property names: title -> title_label
260
 */