1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Implementation of hook_install(). |
---|
5 | */ |
---|
6 | function locale_install() { |
---|
7 | // locales_source.source and locales_target.target are not used as binary |
---|
8 | // fields; non-MySQL database servers need to ensure the field type is text |
---|
9 | // and that LIKE produces a case-sensitive comparison. |
---|
10 | |
---|
11 | // Create tables. |
---|
12 | drupal_install_schema('locale'); |
---|
13 | |
---|
14 | db_query("INSERT INTO {languages} (language, name, native, direction, enabled, weight, javascript) VALUES ('en', 'English', 'English', '0', '1', '0', '')"); |
---|
15 | } |
---|
16 | |
---|
17 | /** |
---|
18 | * @addtogroup updates-5.x-to-6.x |
---|
19 | * @{ |
---|
20 | */ |
---|
21 | |
---|
22 | /** |
---|
23 | * {locales_meta} table became {languages}. |
---|
24 | */ |
---|
25 | function locale_update_6000() { |
---|
26 | $ret = array(); |
---|
27 | |
---|
28 | $schema['languages'] = array( |
---|
29 | 'fields' => array( |
---|
30 | 'language' => array( |
---|
31 | 'type' => 'varchar', |
---|
32 | 'length' => 12, |
---|
33 | 'not null' => TRUE, |
---|
34 | 'default' => '', |
---|
35 | ), |
---|
36 | 'name' => array( |
---|
37 | 'type' => 'varchar', |
---|
38 | 'length' => 64, |
---|
39 | 'not null' => TRUE, |
---|
40 | 'default' => '', |
---|
41 | ), |
---|
42 | 'native' => array( |
---|
43 | 'type' => 'varchar', |
---|
44 | 'length' => 64, |
---|
45 | 'not null' => TRUE, |
---|
46 | 'default' => '', |
---|
47 | ), |
---|
48 | 'direction' => array( |
---|
49 | 'type' => 'int', |
---|
50 | 'not null' => TRUE, |
---|
51 | 'default' => 0, |
---|
52 | ), |
---|
53 | 'enabled' => array( |
---|
54 | 'type' => 'int', |
---|
55 | 'not null' => TRUE, |
---|
56 | 'default' => 0, |
---|
57 | ), |
---|
58 | 'plurals' => array( |
---|
59 | 'type' => 'int', |
---|
60 | 'not null' => TRUE, |
---|
61 | 'default' => 0, |
---|
62 | ), |
---|
63 | 'formula' => array( |
---|
64 | 'type' => 'varchar', |
---|
65 | 'length' => 128, |
---|
66 | 'not null' => TRUE, |
---|
67 | 'default' => '', |
---|
68 | ), |
---|
69 | 'domain' => array( |
---|
70 | 'type' => 'varchar', |
---|
71 | 'length' => 128, |
---|
72 | 'not null' => TRUE, |
---|
73 | 'default' => '', |
---|
74 | ), |
---|
75 | 'prefix' => array( |
---|
76 | 'type' => 'varchar', |
---|
77 | 'length' => 128, |
---|
78 | 'not null' => TRUE, |
---|
79 | 'default' => '', |
---|
80 | ), |
---|
81 | 'weight' => array( |
---|
82 | 'type' => 'int', |
---|
83 | 'not null' => TRUE, |
---|
84 | 'default' => 0, |
---|
85 | ), |
---|
86 | 'javascript' => array( //Adds a column to store the filename of the JavaScript translation file. |
---|
87 | 'type' => 'varchar', |
---|
88 | 'length' => 32, |
---|
89 | 'not null' => TRUE, |
---|
90 | 'default' => '', |
---|
91 | ), |
---|
92 | ), |
---|
93 | 'primary key' => array('language'), |
---|
94 | 'indexes' => array( |
---|
95 | 'list' => array('weight', 'name'), |
---|
96 | ), |
---|
97 | ); |
---|
98 | |
---|
99 | db_create_table($ret, 'languages', $schema['languages']); |
---|
100 | |
---|
101 | // Save the languages |
---|
102 | $ret[] = update_sql("INSERT INTO {languages} (language, name, native, direction, enabled, plurals, formula, domain, prefix, weight) SELECT locale, name, name, 0, enabled, plurals, formula, '', locale, 0 FROM {locales_meta}"); |
---|
103 | |
---|
104 | // Save the language count in the variable table |
---|
105 | $count = db_result(db_query('SELECT COUNT(*) FROM {languages} WHERE enabled = 1')); |
---|
106 | variable_set('language_count', $count); |
---|
107 | |
---|
108 | // Save the default language in the variable table |
---|
109 | $default = db_fetch_object(db_query('SELECT * FROM {locales_meta} WHERE isdefault = 1')); |
---|
110 | variable_set('language_default', (object) array('language' => $default->locale, 'name' => $default->name, 'native' => '', 'direction' => 0, 'enabled' => 1, 'plurals' => $default->plurals, 'formula' => $default->formula, 'domain' => '', 'prefix' => $default->locale, 'weight' => 0)); |
---|
111 | |
---|
112 | $ret[] = update_sql("DROP TABLE {locales_meta}"); |
---|
113 | return $ret; |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Change locale column to language. The language column is added by |
---|
118 | * update_fix_d6_requirements() in update.php to avoid a large number |
---|
119 | * of error messages from update.php. All we need to do here is copy |
---|
120 | * locale to language and then drop locale. |
---|
121 | */ |
---|
122 | function locale_update_6001() { |
---|
123 | $ret = array(); |
---|
124 | $ret[] = update_sql('UPDATE {locales_target} SET language = locale'); |
---|
125 | db_drop_field($ret, 'locales_target', 'locale'); |
---|
126 | return $ret; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * Remove empty translations, we don't need these anymore. |
---|
131 | */ |
---|
132 | function locale_update_6002() { |
---|
133 | $ret = array(); |
---|
134 | $ret[] = update_sql("DELETE FROM {locales_target} WHERE translation = ''"); |
---|
135 | return $ret; |
---|
136 | } |
---|
137 | |
---|
138 | /** |
---|
139 | * Prune strings with no translations (will be automatically re-registered if still in use) |
---|
140 | */ |
---|
141 | function locale_update_6003() { |
---|
142 | $ret = array(); |
---|
143 | $ret[] = update_sql("DELETE FROM {locales_source} WHERE lid NOT IN (SELECT lid FROM {locales_target})"); |
---|
144 | return $ret; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * Fix remaining inconsistent indexes. |
---|
149 | */ |
---|
150 | function locale_update_6004() { |
---|
151 | $ret = array(); |
---|
152 | db_add_index($ret, 'locales_target', 'language', array('language')); |
---|
153 | |
---|
154 | switch ($GLOBALS['db_type']) { |
---|
155 | case 'pgsql': |
---|
156 | db_drop_index($ret, 'locales_source', 'source'); |
---|
157 | db_add_index($ret, 'locales_source', 'source', array(array('source', 30))); |
---|
158 | break; |
---|
159 | } |
---|
160 | |
---|
161 | return $ret; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * Change language setting variable of content types. |
---|
166 | * |
---|
167 | * Use language_content_type_<content_type> instead of language_<content_type> |
---|
168 | * so content types such as 'default', 'count' or 'negotiation' will not |
---|
169 | * interfere with language variables. |
---|
170 | */ |
---|
171 | function locale_update_6005() { |
---|
172 | foreach (node_get_types() as $type => $content_type) { |
---|
173 | // Default to NULL, so we can skip dealing with non-existent settings. |
---|
174 | $setting = variable_get('language_'. $type, NULL); |
---|
175 | if ($type == 'default' && is_numeric($setting)) { |
---|
176 | // language_default was overwritten with the content type setting, |
---|
177 | // so reset the default language and save the content type setting. |
---|
178 | variable_set('language_content_type_default', $setting); |
---|
179 | variable_del('language_default'); |
---|
180 | drupal_set_message('The default language setting has been reset to its default value. Check the '. l('language configuration page', 'admin/settings/language') .' to configure it correctly.'); |
---|
181 | } |
---|
182 | elseif ($type == 'negotiation') { |
---|
183 | // language_content_type_negotiation is an integer either if it is |
---|
184 | // the negotiation setting or the content type setting. |
---|
185 | // The language_negotiation setting is not reset, but |
---|
186 | // the user is alerted that this setting possibly was overwritten |
---|
187 | variable_set('language_content_type_negotiation', $setting); |
---|
188 | drupal_set_message('The language negotiation setting was possibly overwritten by a content type of the same name. Check the '. l('language configuration page', 'admin/settings/language/configure') .' and the '. l('<em>'. $content_type->name ."</em> content type's multilingual support settings", 'admin/content/types/negotiation', array('html' => TRUE)) .' to configure them correctly.'); |
---|
189 | } |
---|
190 | elseif (!is_null($setting)) { |
---|
191 | // Change the language setting variable for any other content type. |
---|
192 | // Do not worry about language_count, it will be updated below. |
---|
193 | variable_set('language_content_type_'. $type, $setting); |
---|
194 | variable_del('language_'. $type); |
---|
195 | } |
---|
196 | } |
---|
197 | // Update language count variable that might be overwritten. |
---|
198 | $count = db_result(db_query('SELECT COUNT(*) FROM {languages} WHERE enabled = 1')); |
---|
199 | variable_set('language_count', $count); |
---|
200 | return array(); |
---|
201 | } |
---|
202 | |
---|
203 | /** |
---|
204 | * Neutralize unsafe language names in the database. |
---|
205 | */ |
---|
206 | function locale_update_6006() { |
---|
207 | $ret = array(); |
---|
208 | $matches = db_result(db_query("SELECT 1 FROM {languages} WHERE native LIKE '%<%' OR native LIKE '%>%' OR name LIKE '%<%' OR name LIKE '%>%'")); |
---|
209 | if ($matches) { |
---|
210 | $ret[] = update_sql("UPDATE {languages} SET name = REPLACE(name, '<', ''), native = REPLACE(native, '<', '')"); |
---|
211 | $ret[] = update_sql("UPDATE {languages} SET name = REPLACE(name, '>', ''), native = REPLACE(native, '>', '')"); |
---|
212 | drupal_set_message('The language name in English and the native language name values of all the existing custom languages of your site have been sanitized for security purposes. Visit the <a href="'. url('admin/settings/language') .'">Languages</a> page to check these and fix them if necessary.', 'warning'); |
---|
213 | } |
---|
214 | // Check if some langcode values contain potentially dangerous characters and |
---|
215 | // warn the user if so. These are not fixed since they are referenced in other |
---|
216 | // tables (e.g. {node}). |
---|
217 | if (db_result(db_query("SELECT 1 FROM {languages} WHERE language LIKE '%<%' OR language LIKE '%>%' OR language LIKE '%\"%' OR language LIKE '%\\\\\%'"))) { |
---|
218 | drupal_set_message('Some of your custom language code values contain invalid characters. You should examine the <a href="'. url('admin/settings/language') .'">Languages</a> page. These must be fixed manually.', 'error'); |
---|
219 | } |
---|
220 | return $ret; |
---|
221 | } |
---|
222 | |
---|
223 | /** |
---|
224 | * @} End of "addtogroup updates-5.x-to-6.x". |
---|
225 | */ |
---|
226 | |
---|
227 | /** |
---|
228 | * @addtogroup updates-6.x-extra |
---|
229 | * @{ |
---|
230 | */ |
---|
231 | |
---|
232 | /** |
---|
233 | * Fix Drupal.formatPlural(). |
---|
234 | */ |
---|
235 | function locale_update_6007() { |
---|
236 | drupal_load('module', 'locale'); |
---|
237 | locale_inc_callback('_locale_invalidate_js'); |
---|
238 | return array(); |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * @} End of "addtogroup updates-6.x-extra". |
---|
243 | * The next series of updates should start at 7000. |
---|
244 | */ |
---|
245 | |
---|
246 | /** |
---|
247 | * Implementation of hook_uninstall(). |
---|
248 | */ |
---|
249 | function locale_uninstall() { |
---|
250 | // Delete all JavaScript translation files |
---|
251 | $files = db_query('SELECT javascript FROM {languages}'); |
---|
252 | while ($file = db_fetch_object($files)) { |
---|
253 | if (!empty($file)) { |
---|
254 | file_delete(file_create_path($file->javascript)); |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | // Clear variables. |
---|
259 | variable_del('language_default'); |
---|
260 | variable_del('language_count'); |
---|
261 | variable_del('language_content_type_default'); |
---|
262 | variable_del('language_content_type_negotiation'); |
---|
263 | variable_del('locale_cache_strings'); |
---|
264 | variable_del('locale_js_directory'); |
---|
265 | variable_del('javascript_parsed'); |
---|
266 | variable_del('language_negotiation'); |
---|
267 | |
---|
268 | foreach (node_get_types() as $type => $content_type) { |
---|
269 | variable_del("language_content_type_$type"); |
---|
270 | } |
---|
271 | |
---|
272 | // Switch back to English: with a $language->language value different from |
---|
273 | // 'en' successive calls of t() might result in calling locale(), which in |
---|
274 | // turn might try to query the unexisting {locales_source} and |
---|
275 | // {locales_target} tables. |
---|
276 | drupal_init_language(); |
---|
277 | |
---|
278 | // Remove tables. |
---|
279 | drupal_uninstall_schema('locale'); |
---|
280 | } |
---|
281 | |
---|
282 | /** |
---|
283 | * Implementation of hook_schema(). |
---|
284 | */ |
---|
285 | function locale_schema() { |
---|
286 | $schema['languages'] = array( |
---|
287 | 'description' => 'List of all available languages in the system.', |
---|
288 | 'fields' => array( |
---|
289 | 'language' => array( |
---|
290 | 'type' => 'varchar', |
---|
291 | 'length' => 12, |
---|
292 | 'not null' => TRUE, |
---|
293 | 'default' => '', |
---|
294 | 'description' => "Language code, e.g. 'de' or 'en-US'.", |
---|
295 | ), |
---|
296 | 'name' => array( |
---|
297 | 'type' => 'varchar', |
---|
298 | 'length' => 64, |
---|
299 | 'not null' => TRUE, |
---|
300 | 'default' => '', |
---|
301 | 'description' => 'Language name in English.', |
---|
302 | ), |
---|
303 | 'native' => array( |
---|
304 | 'type' => 'varchar', |
---|
305 | 'length' => 64, |
---|
306 | 'not null' => TRUE, |
---|
307 | 'default' => '', |
---|
308 | 'description' => 'Native language name.', |
---|
309 | ), |
---|
310 | 'direction' => array( |
---|
311 | 'type' => 'int', |
---|
312 | 'not null' => TRUE, |
---|
313 | 'default' => 0, |
---|
314 | 'description' => 'Direction of language (Left-to-Right = 0, Right-to-Left = 1).', |
---|
315 | ), |
---|
316 | 'enabled' => array( |
---|
317 | 'type' => 'int', |
---|
318 | 'not null' => TRUE, |
---|
319 | 'default' => 0, |
---|
320 | 'description' => 'Enabled flag (1 = Enabled, 0 = Disabled).', |
---|
321 | ), |
---|
322 | 'plurals' => array( |
---|
323 | 'type' => 'int', |
---|
324 | 'not null' => TRUE, |
---|
325 | 'default' => 0, |
---|
326 | 'description' => 'Number of plural indexes in this language.', |
---|
327 | ), |
---|
328 | 'formula' => array( |
---|
329 | 'type' => 'varchar', |
---|
330 | 'length' => 128, |
---|
331 | 'not null' => TRUE, |
---|
332 | 'default' => '', |
---|
333 | 'description' => 'Plural formula in PHP code to evaluate to get plural indexes.', |
---|
334 | ), |
---|
335 | 'domain' => array( |
---|
336 | 'type' => 'varchar', |
---|
337 | 'length' => 128, |
---|
338 | 'not null' => TRUE, |
---|
339 | 'default' => '', |
---|
340 | 'description' => 'Domain to use for this language.', |
---|
341 | ), |
---|
342 | 'prefix' => array( |
---|
343 | 'type' => 'varchar', |
---|
344 | 'length' => 128, |
---|
345 | 'not null' => TRUE, |
---|
346 | 'default' => '', |
---|
347 | 'description' => 'Path prefix to use for this language.', |
---|
348 | ), |
---|
349 | 'weight' => array( |
---|
350 | 'type' => 'int', |
---|
351 | 'not null' => TRUE, |
---|
352 | 'default' => 0, |
---|
353 | 'description' => 'Weight, used in lists of languages.', |
---|
354 | ), |
---|
355 | 'javascript' => array( |
---|
356 | 'type' => 'varchar', |
---|
357 | 'length' => 32, |
---|
358 | 'not null' => TRUE, |
---|
359 | 'default' => '', |
---|
360 | 'description' => 'Location of JavaScript translation file.', |
---|
361 | ), |
---|
362 | ), |
---|
363 | 'primary key' => array('language'), |
---|
364 | 'indexes' => array( |
---|
365 | 'list' => array('weight', 'name'), |
---|
366 | ), |
---|
367 | ); |
---|
368 | |
---|
369 | $schema['locales_source'] = array( |
---|
370 | 'description' => 'List of English source strings.', |
---|
371 | 'fields' => array( |
---|
372 | 'lid' => array( |
---|
373 | 'type' => 'serial', |
---|
374 | 'not null' => TRUE, |
---|
375 | 'description' => 'Unique identifier of this string.', |
---|
376 | ), |
---|
377 | 'location' => array( |
---|
378 | 'type' => 'varchar', |
---|
379 | 'length' => 255, |
---|
380 | 'not null' => TRUE, |
---|
381 | 'default' => '', |
---|
382 | 'description' => 'Drupal path in case of online discovered translations or file path in case of imported strings.', |
---|
383 | ), |
---|
384 | 'textgroup' => array( |
---|
385 | 'type' => 'varchar', |
---|
386 | 'length' => 255, |
---|
387 | 'not null' => TRUE, |
---|
388 | 'default' => 'default', |
---|
389 | 'description' => 'A module defined group of translations, see hook_locale().', |
---|
390 | ), |
---|
391 | 'source' => array( |
---|
392 | 'type' => 'text', |
---|
393 | 'mysql_type' => 'blob', |
---|
394 | 'not null' => TRUE, |
---|
395 | 'description' => 'The original string in English.', |
---|
396 | ), |
---|
397 | 'version' => array( |
---|
398 | 'type' => 'varchar', |
---|
399 | 'length' => 20, |
---|
400 | 'not null' => TRUE, |
---|
401 | 'default' => 'none', |
---|
402 | 'description' => 'Version of Drupal, where the string was last used (for locales optimization).', |
---|
403 | ), |
---|
404 | ), |
---|
405 | 'primary key' => array('lid'), |
---|
406 | 'indexes' => array( |
---|
407 | 'source' => array(array('source', 30)), |
---|
408 | ), |
---|
409 | ); |
---|
410 | |
---|
411 | $schema['locales_target'] = array( |
---|
412 | 'description' => 'Stores translated versions of strings.', |
---|
413 | 'fields' => array( |
---|
414 | 'lid' => array( |
---|
415 | 'type' => 'int', |
---|
416 | 'not null' => TRUE, |
---|
417 | 'default' => 0, |
---|
418 | 'description' => 'Source string ID. References {locales_source}.lid.', |
---|
419 | ), |
---|
420 | 'translation' => array( |
---|
421 | 'type' => 'text', |
---|
422 | 'mysql_type' => 'blob', |
---|
423 | 'not null' => TRUE, |
---|
424 | 'description' => 'Translation string value in this language.', |
---|
425 | ), |
---|
426 | 'language' => array( |
---|
427 | 'type' => 'varchar', |
---|
428 | 'length' => 12, |
---|
429 | 'not null' => TRUE, |
---|
430 | 'default' => '', |
---|
431 | 'description' => 'Language code. References {languages}.language.', |
---|
432 | ), |
---|
433 | 'plid' => array( |
---|
434 | 'type' => 'int', |
---|
435 | 'not null' => TRUE, // This should be NULL for no referenced string, not zero. |
---|
436 | 'default' => 0, |
---|
437 | 'description' => 'Parent lid (lid of the previous string in the plural chain) in case of plural strings. References {locales_source}.lid.', |
---|
438 | ), |
---|
439 | 'plural' => array( |
---|
440 | 'type' => 'int', |
---|
441 | 'not null' => TRUE, |
---|
442 | 'default' => 0, |
---|
443 | 'description' => 'Plural index number in case of plural strings.', |
---|
444 | ), |
---|
445 | ), |
---|
446 | 'primary key' => array('language', 'lid', 'plural'), |
---|
447 | 'indexes' => array( |
---|
448 | 'lid' => array('lid'), |
---|
449 | 'plid' => array('plid'), |
---|
450 | 'plural' => array('plural'), |
---|
451 | ), |
---|
452 | ); |
---|
453 | |
---|
454 | return $schema; |
---|
455 | } |
---|